- 论坛徽章:
- 5
|
- [root@localhost liuqi]# cat -n /home/liuqi/drivercode/charioctl/chrdev.c
- 1 #include <linux/module.h>
- 2 #include <linux/kernel.h>
- 3 #include <linux/init.h>
- 4 #include <linux/fs.h>
- 5 #include <linux/device.h>
- 6 #include <linux/cdev.h>
- 7 #include <linux/major.h>
- 8 static ssize_t flash_env_dev_open(struct inode *inode,struct file *file)
- 9 {
- 10 return 0;
- 11 }
- 12
- 13
- 14
- 15 static ssize_t flash_env_dev_read(struct file *file, char __user buf, size_t count, loff_t *ppos)
- 16 {
- 17 return 0;
- 18 }
- 19
- 20 static ssize_t flash_env_dev_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
- 21 {
- 22 printk("<1> %d\n",cmd);
- 23 switch(cmd){
- 24 case 0:
- 25 printk("<1> in flash 0\n");
- 26 break;
- 27 case 1:
- 28 printk("<1> in flash 1\n");
- 29 break;
- 30 default:
- 31 printk("<1> others\n");
- 32
- 33 }
- 34 return 0;
- 35 }
- 36
- 37 static const struct file_operations flash_fops = {
- 38 .owner =THIS_MODULE,
- 39 .open = flash_env_dev_open,
- 40 .read = flash_env_dev_read,
- 41 .ioctl = flash_env_dev_ioctl,
- 42 };
- 43
- 44 #define MAX_FLASH_ENV_MINORS 262
- 45
- 46 static struct cdev flash_cdev;
- 47 dev_t dev;
- 48 static __init int flash_env_dev_init(void)
- 49 {
- 50 int res;
- 51 dev=MKDEV(263,262);
- 52 cdev_init(&flash_cdev,&flash_fops);
- 53
- 54 res=register_chrdev_region(dev, MAX_FLASH_ENV_MINORS, "/dev/env_dev");
- 55 if(res)
- 56 printk("<1> fuck\n");
- 57
- 58
- 59 res=cdev_add(&flash_cdev,dev,MAX_FLASH_ENV_MINORS);
- 60 if(res)
- 61 printk("<1> fuck2 \n");
- 62 /* devfs_mk_dir("flash_env_dev");*/
- 63 printk("<1> Hello World\n");
- 64 return 0;
- 65 }
- 66
- 67 static void __exit flash_env_dev_exit(void)
- 68 {
- 69 unregister_chrdev_region(dev,MAX_FLASH_ENV_MINORS);
- 70 printk("<1> exit Hello World\n");
- 71 }
- 72
- 73 module_init(flash_env_dev_init);
- 74 module_exit(flash_env_dev_exit);
- [root@localhost liuqi]#
复制代码
给你个最简单的字符设备的例子
mknod -m 666 /dev/env_dev c 263 262 |
|