Linux字符设备驱动编程(3)

/*模块卸载函数*/
static void memdev_exit(void)
{
  cdev_del(&cdev); /*注销设备*/
  kfree(mem_devp); /*释放设备结构体内存*/
  unregister_chrdev_region(MKDEV(mem_major, 0), 2); /*释放设备号*/
}

MODULE_AUTHOR("David Xie");
MODULE_LICENSE("GPL");

module_init(memdev_init);
module_exit(memdev_exit);

scull.c

-----------------------------------------------------------------

1 obj-m +=scull.o
2 all:
3    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
4 clean:
5    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

-----------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/fcntl.h>

int main()
{
 int fd;
 char buf[]="this is a test!";

char buf_read[4096];


 if((fd=open("/dev/scull",O_RDWR))==-1)

printf("open scull WRONG!\n");
 else
  printf("open scull SUCCESS!\n");
 
 printf("buf is %s\n",buf);

write(fd,buf,sizeof(buf));


 lseek(fd,0,SEEK_SET);


 read(fd,buf_read,sizeof(buf));


 printf("buf_read is %s\n",buf_read);
 
 return 0;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e8a511346f02d502ada55a50e4a82a66.html