星空网 > 软件开发 > 操作系统

linux字符设备

Linux字符设备-自动创建设备号和设备节点

 

先写一个自动分配字符设备号和设备节点的例子及APP

手动安装步骤:

Insmod my_char_dev.ko

 linux字符设备images/loading.gif' data-original="http://images2015.cnblogs.com/blog/437242/201602/437242-20160221222228889-736011749.png" />

不需要再安装设备节点

 

然后是测试app

./my_char_dev_app 1

 

 1 #include <linux/module.h> 2 #include <linux/init.h> 3 #include <linux/io.h> 4 #include <linux/fs.h> 5 #include <asm/device.h> //下面这三个头文件是由于动态创建需要加的 6 #include <linux/device.h> 7 #include <linux/cdev.h> 8 #include "my_cdev.h" 9 struct cdev cdev;10 dev_t devno;//这里是动态分配设备号和动态创建设备结点需要用到的11 struct class *cdev_class;12 13 int my_cdev_open(struct inode *node,struct file *filp)14 {15   printk("my_cdev_open sucess!\n");16   return 0;17 }18 19 long my_cdev_ioctl(struct file *filp ,unsigned int cmd ,unsigned long arg)20 {21   switch(cmd)22   {23     case LED_ON:24       printk("LED_ON is set!\n");25       return 0;26     case LED_OFF:27       printk("LED_OFF is set!\n");28       return 0;29     default :30       return -EINVAL;31   }32 }33 34 struct file_operations my_cdev_fops=35 {36   .open = my_cdev_open,37   .unlocked_ioctl = my_cdev_ioctl,38 39 };40 41 static int my_cdev_init(void)42 {43   int ret;44   /**动态分配设备号*/45   ret = alloc_chrdev_region(&devno,0,1,"my_chardev");46   if(ret)47   {48     printk("alloc_chrdev_region fail!\n");49     unregister_chrdev_region(devno,1);50     return ret;51   }52   else53   {54     printk("alloc_chrdev_region sucess!\n");55   }56   /**描述结构初始化*/57   cdev_init(&cdev,&my_cdev_fops);58   /**描述结构注册*/59   ret = cdev_add(&cdev,devno,1);60   if(ret)61   {62     printk("cdev add fail.\n");63     unregister_chrdev_region(devno,1);64     return ret;65   }66   else67   {68     printk("cdev add sucess!\n");69   }70 71   cdev_class = class_create(THIS_MODULE,"my_chardev");72   if(IS_ERR(cdev_class))73   {74     printk("Create class fail!\n");75     unregister_chrdev_region(devno,1);76     return -1;77   }78   else79   {80     printk("Create class sucess!\n");81   }82 83   device_create(cdev_class,NULL,devno,0,"my_chardev");84   85   return 0;86 }87 static void my_cdev_exit(void)88 {89   device_destroy(cdev_class,devno);90   class_destroy(cdev_class);91   cdev_del(&cdev);92   unregister_chrdev_region(devno,1);93   printk("my_cdev_exit sucess!\n");94 }95 module_init(my_cdev_init);96 module_exit(my_cdev_exit);97 MODULE_LICENSE("GPL");98 MODULE_AUTHOR("YEFEI");99 MODULE_DESCRIPTION("YEFEI Driver");

 

1 #ifndef __MY_CDEV_H__2 #define __MY_CDEV_H__3 4 #define LED_MAGIC 'L'5 #define LED_ON _IO(LED_MAGIC,0)6 #define LED_OFF _IO(LED_MAGIC,1)7 8 #endif

 

 1 #include <sys/stat.h> 2 #include <sys/types.h> 3 #include <sys/ioctl.h> 4 #include <fcntl.h> 5 #include <stdio.h> 6 #include "my_cdev.h" 7  8 int main(int argc,char *argv[]) 9 {10   int fd;11   int cmd;12   if(argc < 2)13   {14     printf("Please enter secend param!\n");15     return 0;16   }17   cmd = atoi(argv[1]);18   fd = open("/dev/my_chardev",O_RDWR);19   if(fd < 0)20   {21     printf("Open dev/my_chardev fail!\n");22     close(fd);23     return 0;24   }25   switch(cmd)26   {27     case 1:28       ioctl(fd,LED_ON);29       break;30     case 2:31       ioctl(fd,LED_OFF);32       break;33     default:34       break;35   }36   close(fd);37   return 0;38 }

 

1 obj-m := my_char_dev.o2 KDIR := /home/win/dn377org/trunk/bcm7252/linux/3 all:4   make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm5 clean:6   rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order

 




原标题:linux字符设备

关键词:linux

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

全程清醒惨叫:https://www.goluckyvip.com/tag/2559.html
Soolve:https://www.goluckyvip.com/tag/25590.html
Soovle:https://www.goluckyvip.com/tag/25591.html
sooyinpaint:https://www.goluckyvip.com/tag/25592.html
Sophia Feng:https://www.goluckyvip.com/tag/25593.html
sophias tea company:https://www.goluckyvip.com/tag/25594.html
商机!速卖通推出小B采销节,挖掘机、莫桑钻、电动车近期在海外走俏!:https://www.xlkjsw.com/news/93297.html
Zalando对欧盟在线平台监管费提出质疑:https://www.kjdsnews.com/a/1842241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流