Linux character devices-automatically create device numbers and device nodes, linux nodes
Linux character devices-automatically create device numbers and device nodes
First, write an example and APP that automatically assigns a character device number and a device node.
Manual installation steps:
Insmod my_char_dev.ko
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> // The following three headers are the 6 # include <linux/device. h> 7 # include <linux/cdev. h> 8 # include "my_cdev.h" 9 struct cdev; 10 dev_t devno; // here is the 11 struct class * cdev_class required for dynamic allocation of device numbers and dynamic creation of device nodes; 12 13 int my_cdev_open (struct inode * node, struct file * filp) 14 {15 printk ("my_cdev_ope N 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/** dynamically allocate device numbers */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/** description structure initialization */57 cdev_init (& cdev, & my_cdev_fops ); 58/** description structure registration */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 outputs (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