杂项设备(misc device)
杂项设备也是在嵌入式系统中用得比较多的一种设备驱动。在 Linux 内核的include/linux目录下有Miscdevice.h文件,要把自己定义的misc device从设备定义在这里。其实是因为这些字符设备不符合预先确定的字符设备范畴,所有这些设备采用主编号10 ,一起归于misc device,其实misc_register就是用主标号10调用register_chrdev()的。
也就是说,misc设备其实也就是特殊的字符设备。
字符设备(char device)
使用register_chrdev(LED_MAJOR,DEVICE_NAME,&dev_fops)注册字符设备驱动程序时,如果有多个设备使用该函数注册驱动程序,LED_MAJOR不能相同,否则几个设备都无法注册(我已验证)。如果模块使用该方式注册并且 LED_MAJOR为0(自动分配主设备号 ),使用insmod命令加载模块时会在终端显示分配的主设备号和次设备号,在/dev目录下建立该节点,比如设备leds,如果加载该模块时分配的主设备号和次设备号为253和0,则建立节点:mknod leds c 253 0。使用register_chrdev (LED_MAJOR,DEVICE_NAME,&dev_fops)注册字符设备驱动程序时都要手动建立节点 ,否则在应用程序无法打开该设备。
mise device在linux内核源码中的定义如下:
- 01.#ifndef _LINUX_MISCDEVICE_H
- 02.#define _LINUX_MISCDEVICE_H
- 03.#include <linux/module.h>
- 04.#include <linux/major.h>
- 05.
- 06.#define PSMOUSE_MINOR 1
- 07.#define MS_BUSMOUSE_MINOR 2
- 08.#define ATIXL_BUSMOUSE_MINOR 3
- 09./*#define AMIGAMOUSE_MINOR 4 FIXME OBSOLETE */
- 10.#define ATARIMOUSE_MINOR 5
- 11.#define SUN_MOUSE_MINOR 6
- 12.#define APOLLO_MOUSE_MINOR 7
- 13.#define PC110PAD_MINOR 9
- 14./*#define ADB_MOUSE_MINOR 10 FIXME OBSOLETE */
- 15.#define WATCHDOG_MINOR 130 /* Watchdog timer */
- 16.#define TEMP_MINOR 131 /* Temperature Sensor */
- 17.#define RTC_MINOR 135
- 18.#define EFI_RTC_MINOR 136 /* EFI Time services */
- 19.#define SUN_OPENPROM_MINOR 139
- 20.#define DMAPI_MINOR 140 /* DMAPI */
- 21.#define NVRAM_MINOR 144
- 22.#define SGI_MMTIMER 153
- 23.#define STORE_QUEUE_MINOR 155
- 24.#define I2O_MINOR 166
- 25.#define MICROCODE_MINOR 184
- 26.#define TUN_MINOR 200
- 27.#define MWAVE_MINOR 219 /* ACP/Mwave Modem */
- 28.#define MPT_MINOR 220
- 29.#define HPET_MINOR 228
- 30.#define FUSE_MINOR 229
- 31.#define KVM_MINOR 232
- 32.#define MISC_DYNAMIC_MINOR 255
- 33.
- 34.struct device;
- 35.
- 36.struct miscdevice {
- 37. int minor;
- 38. const char *name;
- 39. const struct file_operations *fops;
- 40. struct list_head list;
- 41. struct device *parent;
- 42. struct device *this_device;
- 43.};
- 44.
- 45.extern int misc_register(struct miscdevice * misc);
- 46.extern int misc_deregister(struct miscdevice *misc);
- 47.
- 48.#define MODULE_ALIAS_MISCDEV(minor) /
- 49. MODULE_ALIAS("char-major-" __stringify(MISC_MAJOR) /
- 50. "-" __stringify(minor))
- 51.#endif