使用register_chrdev注册字符设备

摘要:
注销字符设备可以使用unregister_chrdev函数。intunregister_chrdev;例1.3register_chrdev注册字符设备实例代码见光盘src1drivermodel1-3register_chrdev。核心代码如下所示:staticunsignedcharsimple_inc=0;staticunsignedchardemoBuffer[256];intsimple_open{ifreturn-ERESTARTSYS;simple_inc++;return0;}intsimple_release{simple_inc--;return0;}ssize_tsimple_read{/*把数据复制到应用程序空间*/if{count=-EFAULT;}returncount;}ssize_tsimple_write{/*把数据复制到内核空间*/if{count=-EFAULT;}returncount;}structfile_operationssimple_fops={.owner=THIS_MODULE,.read=simple_read,.write=simple_write,.open=simple_open,.release=simple_release,};/*******************************************************MODULEROUTINE*******************************************************/voidsimple_cleanup_module{unregister_chrdev;printk("simple_cleanup_module!");}intsimple_init_module{intret;//根据设备号与设备名注册字符设备ret=register_chrdev;if{printk("Unabletoregistercharacterdevice%d!

1.2.2 使用register_chrdev注册字符设备

注册字符设备可以使用register_chrdev函数。

  1. intregister_chrdev(unsignedintmajor,constchar*name,structfile_operations*fops);

register_chrdev函数的major参数如果等于0,则表示采用系统动态分配的主设备号。

注销字符设备可以使用unregister_chrdev函数。

  1. intunregister_chrdev(unsignedintmajor,constchar*name);

例1.3 register_chrdev注册字符设备实例

代码见光盘src1drivermodel1-3register_chrdev。核心代码如下所示:

  1. staticunsignedcharsimple_inc=0;
  2. staticunsignedchardemoBuffer[256];
  3. intsimple_open(structinode*inode,structfile*filp)
  4. {
  5. if(simple_inc>0)return-ERESTARTSYS;
  6. simple_inc++;
  7. return0;
  8. }
  9. intsimple_release(structinode*inode,structfile*filp)
  10. {
  11. simple_inc--;
  12. return0;
  13. }
  14. ssize_tsimple_read(structfile*filp,char__user*buf,size_tcount,loff_t*f_pos)
  15. {
  16. /*把数据复制到应用程序空间*/
  17. if(copy_to_user(buf,demoBuffer,count))
  18. {
  19. count=-EFAULT;
  20. }
  21. returncount;
  22. }
  23. ssize_tsimple_write(structfile*filp,constchar__user*buf,size_tcount,loff_t*f_pos)
  24. {
  25. /*把数据复制到内核空间*/
  26. if(copy_from_user(demoBuffer+*f_pos,buf,count))
  27. {
  28. count=-EFAULT;
  29. }
  30. returncount;
  31. }
  32. structfile_operationssimple_fops={
  33. .owner=THIS_MODULE,
  34. .read=simple_read,
  35. .write=simple_write,
  36. .open=simple_open,
  37. .release=simple_release,
  38. };
  39. /*******************************************************
  40. MODULEROUTINE
  41. *******************************************************/
  42. voidsimple_cleanup_module(void)
  43. {
  44. unregister_chrdev(simple_MAJOR,"simple");
  45. printk("simple_cleanup_module! ");
  46. }
  47. intsimple_init_module(void)
  48. {
  49. intret;
  50. //根据设备号与设备名注册字符设备
  51. ret=register_chrdev(simple_MAJOR,"simple",&simple_fops);
  52. if(ret<0)
  53. {
  54. printk("Unabletoregistercharacterdevice%d! ",simple_MAJOR);
  55. returnret;
  56. }
  57. return0;
  58. }
  59. module_init(simple_init_module);
  60. module_exit(simple_cleanup_module);

应用程序的代码如下:

  1. voidmain(void)
  2. {
  3. intfd;
  4. inti;
  5. chardata[256];
  6. intretval;
  7. fd=open("/dev/fgj",O_RDWR);
  8. if(fd==-1)
  9. {
  10. perror("erroropen ");
  11. exit(-1);
  12. }
  13. printf("open/dev/fgjsuccessfully ");
  14. //写数据
  15. retval=write(fd,"fgj",3);
  16. if(retval==-1)
  17. {
  18. perror("writeerror ");
  19. exit(-1);
  20. }
  21. //读数据
  22. retval=read(fd,data,3);
  23. if(retval==-1)
  24. {
  25. perror("readerror ");
  26. exit(-1);
  27. }
  28. data[retval]=0;
  29. printf("readsuccessfully:%s ",data);
  30. //关闭设备
  31. close(fd);
  32. }

使用register_chrdev(LED_MAJOR,DEVICE_NAME,&dev_fops)注册字符设备驱动程序时都要手动建立节点,否则在应用程序无法打开该设备

字符设备模块使用insmod加载,加载完毕需要在/dev目录下使用mkmod命令建立相应的文件结点,编译生成的应用层可执行程序为test。本例运行结果如下:

[root@/home]#insmod demo.ko  
[root@urbetter /home]# mknod /dev/fgj c 224 0
[root@urbetter /home]# ./test   
open /dev/fgj successfully  
read successfully:fgj  

免责声明:文章转载自《使用register_chrdev注册字符设备》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇记录所有的登录操作日志(一)Python入门-3序列:16字典-表格数据存储-列表和字典综合嵌套下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

arduino入门学习实现语音控制LED灯

需要的准备的硬件arduino+PC+麦克风实现语音命令控制LED灯的亮灭。 首先需要将写好的arduino程序烧录到arduino uno主板中,下面是代码如下: int val;//定义变量val int ledpin=10;//定义数字接口13 void setup() { Serial.begin(9600);//设置波特率为9600,这里要跟软...

管理aix的密码策略

aix 中/etc/security/user 存放用户的概要 常用参数参数如下1.account_locked defines whether the account is locked.locked accounts can not be used for login .possible values:true or false.定义账户是否被锁,被锁...

3、Python字符编码区分utf-8和utf-8-sig

Python 读取文件首行多了"ufeff"字符串 python读取B.txt文件时,控制台打印首行正常,但是若是用首行内容打开文本的话,就会报错: Traceback (most recent call last): A File "E:/python project/multiProcess/test.py", line 32, in <mo...

Mysql字符集设置

最近,在项目组使用的mysql数据库中,插入数据出现乱码,关于这个问题做了下总结,我们从最基本的地方说起,到错误产生的深层次原因和解决办法。 基本概念 • 字符(Character)是指人类语言中最小的表义符号。例如’A'、’B'等;• 给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符,这一数值就是字符的编码(Encoding)。例如,我们给...

那些年java MD5加密字符编码的坑

相信做过MD5加密的童鞋都遇到过字符编码的坑,一般加密出来的结果和其他人不一样都是字符编码不一致导致的,比如类文件的字符编码、浏览器的字符编码等和对方不一致,所以就需要转码统一字符。 以下是笔者转码过程中遇到的坑: 不要new String("XXXX".getBytes("UTF-8")),之后将转码后的字串传入MD5去加密,会遇到意想不到的效果,有的字...

Oracle实例解析:编码与字符集

字符集:人们根据需要把某些字符收集到一处,并赋以名称,于是便有了某某字符集。 编码:当前面收集的工作完成以后,为了让只认识数字的“愚蠢”的计算机也能够存储字符,人们不得不为集合里的每一个字符分配”身份证号码”,这就是编码,从此,终于可以以存储编码的方式在计算机中存储字符了。 在字符集与编码世界的漫漫历史长河里(伪),出现过若干个让计算机工作者们如雷贯耳的名...