linux驱动设备号

一、设备号基础

  一般来说,使用ls -l命令在时间一列的前一列的数字表示的是文件大小,但如果该文件表示的是一个设备的话,那时间一列的前一列将有两个数字,用逗号分隔开,如下图:

  

   前一个数字表示主设备号,后一个数组表示次设备号。主设备号用来区分不同种类的设备,而次设备号用来区分同一类型的多个设备。

  实际上主设备号和次设备号组合起来就是一个32位的数字,用dev_t类型表示,前12位表示主设备号,后20位表示次设备号。

  可以通过MAJOR提取出主设备号,可以通过MINOR提取出次设备号,或者用MKDEV将主设备号和次设备号组合成一个dev_t类型的设备号,它们的定义见下图:

   

   注意是在linux/kdev_t.h中定义的

  在内核代码Documentation目录下的devices.txt中记录了已经使用的各个设备的主设备号和次设备号如下图:

   

  比如/dev/mem主设备号为1,次设备号为1,这么说/dev/ram1主次设备号与/dev/mem一样,是不是就冲突了呢,事实上并没有/dev/ram1这个文件,如下图:

  

二、分配主设备号

  方法一:静态申请

    使用register_chrdev_region函数,该函数的原型如下:

/**
 * register_chrdev_region() - register a range of device numbers
 * @from: the first in the desired range of device numbers; must include
 *        the major number.
 * @count: the number of consecutive device numbers required
 * @name: the name of the device or driver.
 *
 * Return value is zero on success, a negative error code on failure.
 */
int register_chrdev_region(dev_t from, unsigned count, const char *name)

    功能:注册从from开始的count个设备号

    参数:

      from:希望注册的设备号。必须包含主设备号

      count:希望注册的设备号数目

      name:设备名(体现在/proc/devices)

    返回值:

      成功返回0,失败返回错误码

    注意:这个函数注册的设备号并不是主设备号不变,次设备号增加,比如from写的是(1, (2^20 - 1)),注册两个设备,则将会注册(1, (2^20 - 1))和(2, 0)两个设备,这两个设备的主设备号是不一样的,也就是说在内核里边认为这是两个不同类型的设备。

 

  方法二:动态分配

    使用alloc_chrdev_region函数,该函数的原型如下:

/**
 * alloc_chrdev_region() - register a range of char device numbers
 * @dev: output parameter for first assigned number
 * @baseminor: first of the requested range of minor numbers
 * @count: the number of minor numbers required
 * @name: the name of the associated device or driver
 *
 * Allocates a range of char device numbers.  The major number will be
 * chosen dynamically, and returned (along with the first minor number)
 * in @dev.  Returns zero or a negative error code.
 */
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)

    功能:申请一个范围的设备号

    参数:

      dev:输出参数,为申请到的第一个设备号

      baseminor:起始次设备号

      count:需要分配的设备号数量

      mame:设备名(体现在/proc/devices)

    返回值:

      成功返回0,失败返回错误码

 三、注销设备号

  使用函数unregister_chrdev_region,原型如下:

/**
 * unregister_chrdev_region() - unregister a range of device numbers
 * @from: the first in the range of numbers to unregister
 * @count: the number of device numbers to unregister
 *
 * This function will unregister a range of @count device numbers,
 * starting with @from.  The caller should normally be the one who
 * allocated those numbers in the first place...
 */
void unregister_chrdev_region(dev_t from, unsigned count)

  

猜你喜欢

转载自www.cnblogs.com/Suzkfly/p/11766029.html