Chinaunix首页 | 论坛 | 博客
  • 博客访问: 168400
  • 博文数量: 43
  • 博客积分: 95
  • 博客等级: 民兵
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 17:23
文章分类

全部博文(43)

文章存档

2016年(1)

2015年(5)

2014年(10)

2013年(24)

2012年(3)

我的朋友

分类: LINUX

2012-09-29 15:32:12

    杂项设备也是在嵌入式系统中用得比较多的一种设备驱动。在 Linux 内核的include/linux目录下有Miscdevice.h文件,要把自己定义的misc device从设备定义在这里。其实是因为这些字符设备不符合预先确定的字符设备范畴,所有这些设备采用主编号10 ,一起归于misc device,其实misc_register就是用主标号10调用register_chrdev()的。也就是说,misc设备其实也就是特殊的字符设备,可自动生成设备节点。
 
\include\linux\miscdevice.h
在这个头文件中主要是misc(混合)设备注册和注销:
其它类型---不能严格划分的设备类型,也叫混合类型
有:

点击(此处)折叠或打开

  1. #ifndef _LINUX_MISCDEVICE_H
  2. #define _LINUX_MISCDEVICE_H
  3. #include <linux/module.h>
  4. #include <linux/major.h>
  5. #define PSMOUSE_MINOR 1
  6. #define MS_BUSMOUSE_MINOR 2
  7. #define ATIXL_BUSMOUSE_MINOR 3
  8. /*#define AMIGAMOUSE_MINOR 4 FIXME OBSOLETE */
  9. #define ATARIMOUSE_MINOR 5
  10. #define SUN_MOUSE_MINOR 6
  11. #define APOLLO_MOUSE_MINOR 7
  12. #define PC110PAD_MINOR 9
  13. /*#define ADB_MOUSE_MINOR 10 FIXME OBSOLETE */
  14. #define WATCHDOG_MINOR 130 /* Watchdog timer */
  15. #define TEMP_MINOR 131 /* Temperature Sensor */
  16. #define RTC_MINOR 135
  17. #define EFI_RTC_MINOR 136 /* EFI Time services */
  18. #define SUN_OPENPROM_MINOR 139
  19. #define DMAPI_MINOR 140 /* DMAPI */
  20. #define NVRAM_MINOR 144
  21. #define SGI_MMTIMER 153
  22. #define STORE_QUEUE_MINOR 155
  23. #define I2O_MINOR 166
  24. #define MICROCODE_MINOR 184
  25. #define TUN_MINOR 200
  26. #define MWAVE_MINOR 219 /* ACP/Mwave Modem */
  27. #define MPT_MINOR 220
  28. #define MPT2SAS_MINOR 221
  29. #define HPET_MINOR 228
  30. #define FUSE_MINOR 229
  31. #define KVM_MINOR 232
  32. #define MISC_DYNAMIC_MINOR 255
  33. struct device;
  34. struct miscdevice {
  35.  int minor;/*次设备号*/
  36.  const char *name;/*misc设备名*/
  37.  const struct file_operations *fops;/*misc设备文件操作结构体*/
  38.  struct list_head list;
  39.  struct device *parent;
  40.  struct device *this_device;
  41.  const char *nodename;
  42.  mode_t mode;
  43. };
minor:所有的misc设备共用一个主设备号,所以注册misc设备时只要次
设备号就可以了。利用次设备号来区分设备的。

misc设备注册:
extern int misc_register(struct miscdevice * misc);
misc设备注销:
extern int misc_deregister(struct miscdevice * misc);

drivers/char/misc.c

点击(此处)折叠或打开

  1. int misc_register(struct miscdevice * misc)
  2. {
  3.    struct miscdevice *c;
  4.    dev_t dev;
  5.    int err = 0;

  6.    down(&misc_sem);
  7.    list_for_each_entry(c, &misc_list, list) {
  8.      if (c->minor == misc->minor) {
  9.         up(&misc_sem);
  10.         return -EBUSY;
  11.      }
  12.    }

  13.    if (misc->minor == MISC_DYNAMIC_MINOR) {
  14.       int i = DYNAMIC_MINORS;
  15.       while (--i >= 0)
  16.         if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
  17.              break;
  18.       if (i<0) {
  19.         up(&misc_sem);
  20.         return -EBUSY;
  21.       }
  22.       misc->minor = i;
  23.    }

  24.    if (misc->minor < DYNAMIC_MINORS)
  25.       misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
  26.    dev = MKDEV(MISC_MAJOR, misc->minor);
  27.   
  28.    misc->class = class_device_create(misc_class, NULL, dev, misc->dev,"%s", misc->name);
  29.    if (IS_ERR(misc->class)) {
  30.        err = PTR_ERR(misc->class);
  31.       goto out;
  32.    }
  33. /*
  34. * Add it to the front, so that later devices can "override"
  35. * earlier defaults
  36. */
  37.    list_add(&misc->list, &misc_list);
  38. out:
  39.     up(&misc_sem);
  40.     return err;
  41. }
  42. /**
  43. * misc_deregister - unregister a miscellaneous device
  44. * @misc: device to unregister
  45. *
  46. * Unregister a miscellaneous device that was previously
  47. * successfully registered with misc_register(). Success
  48. * is indicated by a zero return, a negative errno code
  49. * indicates an error.
  50. */

  51. int misc_deregister(struct miscdevice * misc)
  52. {
  53.    int i = misc->minor;
  54.    if (list_empty(&misc->list))
  55.        return -EINVAL;
  56.    down(&misc_sem);
  57.    list_del(&misc->list);
  58.    class_device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
  59.    if (i < DYNAMIC_MINORS && i>0) {
  60.        misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
  61.    }
  62.    up(&misc_sem);
  63.    return 0;
  64. }
  65. EXPORT_SYMBOL(misc_register);
  66. EXPORT_SYMBOL(misc_deregister);
  67. static int __init misc_init(void)
  68. {
  69. #ifdef CONFIG_PROC_FS
  70.    struct proc_dir_entry *ent;
  71.    ent = create_proc_entry("misc", 0, NULL);
  72.    if (ent)
  73.       ent->proc_fops = &misc_proc_fops;
  74. #endif
  75.    misc_class = class_create(THIS_MODULE, "misc");
  76.    if (IS_ERR(misc_class))
  77.       return PTR_ERR(misc_class);

  78.    if (register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
  79.        printk("unable to get major %d for misc devices/n",MISC_MAJOR);
  80.        class_destroy(misc_class);
  81.        return -EIO;
  82.    }
  83.     return 0;
  84. }
  85. subsys_initcall(misc_init);
阅读(2186) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~