Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563308
  • 博文数量: 199
  • 博客积分: 5087
  • 博客等级: 大校
  • 技术积分: 2165
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-26 21:53
文章存档

2010年(199)

我的朋友

分类: LINUX

2010-05-04 00:05:36

如果要详细的分驱动的类型,分为miscdev、character、block、net。
这里两天在学习mini2440的时候发现,很多的驱动都是miscdev类,本人学识浅薄,尽然不知道miscdev为何物,经过查证终于知其所以然。
 
什么是miscdev类驱动?答:miscdev类驱动又叫混杂驱动驱动 ,是一个比较原始的东西,在1998年就已经提出来了,在2.0以上的kernel都有支持。可以认为小驱动程序,主要用于简单的硬件驱动或者简单的测试。
为什么在miscdev类驱动中没有看见主设备号之类的东西?答:miscdev类的驱动,默认的主设备号是10,所以这类驱动,你只需要指定从设备号就可以了,实质上,你也不能指定它的主设备好。 struct miscdevice {
        int minor;
        const char *name;
        struct file_operations *fops;
        struct miscdevice *next, *prev;
};

 
上边是它的结构,从结构中可以看到,你只需要指定从设备号,struct file_operations 和设备名就可以了
主要用于什么?
答: 当然用于测试简单的程序和一些简单的I/O外部硬件或设备了,可以节省主设备号。其他就没什么了,所以比如LED、按键、RTC等,都可以用这个小驱动程序写了。
以下是linux官方1998年的说法:
Kernel Korner
Miscellaneous Character Drivers
(January 1998)
Alessandro tells us how to register a small device needing a single entry point with the misc driver.
by Alessandro Rubini
Sometimes people need to write ``small'' device drivers, to support custom hacks--either hardware or software ones. To this end, as well as to host some real drivers, the Linux kernel exports an interface to allow modules to register their own small drivers. The misc driver was designed for this purpose. The code introduced here is meant to run with version 2.0 of the Linux kernel.
In UNIX, Linux and similar operating systems, every device is identified by two numbers: a ``major'' number and a ``minor'' number. These numbers can be seen by invoking ls -l /dev. Every device driver registers its major number with the kernel and is completely responsible for managing its minor numbers. Use of any device with that major number will fall on the same device driver, regardless of the minor number. As a result, every driver needs to register a major number, even if it only deals with a single device, like a pointing tool.
Since the kernel keeps a static table of device drivers, frivolous allocation of major numbers is rather wasteful of RAM. The Linux kernel, therefore, offers a simplified interface for simple drivers--those that will register a single entry point. Note that, in general, allocating the whole name space of a major number to every device is beneficial. This allows the handling of multiple terminals, multiple serial ports and several disk partitions without any overhead in the kernel proper: a single driver takes care of all of them, and uses the minor number to differentiate.
Major number 10 is officially assigned to the misc driver. Modules can register individual minor numbers with the misc driver and take care of a small device, needing only a single entry point.

Registering a Minor Number
The misc driver exports two functions for user modules to register and unregister their own minor number:
#include int misc_register(struct miscdevice * misc); int misc_deregister(struct miscdevice * misc);
Each user module can use the register function to create its own entry point for a minor number, and deregister to release resources at unload time.
The miscdevice.h file also declares struct miscdevice in the following way:
struct miscdevice { int minor; const char *name; struct file_operations *fops; struct miscdevice *next, *prev; };
The five fields have the following meaning:

minor is the minor number being registered. Every misc device must feature a different minor number, because such a number is the only link between the file in /dev and the driver.
name is the name for this device, meant for human consumption: users will find the name in the /proc/misc file.
fops is a pointer to the file operations which must be used to act on the device. File operations have been described in a previous ``Kernel Korner'' in April 1996. (That article is available on the web at
.) Anyway, the topic is refreshed later in this article.
next and prev are used to manage a circularly-linked list of registered drivers.
The code calling misc_register is expected to clear prev and next before invoking the function and to fill the first three fields with sensible values.
The real question with the misc device driver is ``what is a sensible value for the minor field?'' Assignment of minor numbers is performed in two ways: either you can use an ``officially assigned'' number, or you can resort to dynamic assignment. In the latter case, your driver asks for a free minor number, and the kernel returns one.
The typical code sequence for assigning a dynamic minor number is as follows:
static struct miscdevice my_dev; int init_module(void) { int retval; my_dev.minor = MISC_DYNAMIC_MINOR; my_dev.name = "my"; my_dev.fops = &my_fops; retval = misc_register(&my_dev); if (retval) return retval; printk("my: got minor %i\n",my_dev.minor); return 0; }
Needless to say, a real module will perform some other tasks within init_module. After successful registration, the new misc device will appear in /proc/misc. This informative file reports which misc drivers are available and their minor numbers. After loading my, the file will include the following line:
63 my
This shows that 63 is the minor number returned. If you want to create an entry point in /dev for your misc module, you can use a script like the one shown in Listing 1 below. The script takes care of creating the device node and giving it the desired permission and ownership.

--------------------------------------------------------------------------------
Listing 1
#!/bin/sh if [ "x$1" = "x" ]; then echo "usage: load_misc [ [ []]]" fi insmod $1 || exit 1 major=10 minor=`grep $DEV /proc/devices | awk "{print }"` mknod /dev/$1 c $major $minor if [ "x$2" != "x"]; then chmod $2 /dev/$1 fi if [ "x$3" != "x"]; then chown $3 /dev/$1 fi if [ "x$4" != "x"]; then chgrp $4 /dev/$1 fi exit 0
--------------------------------------------------------------------------------
You might choose to find an unused minor number and hardwire it in your driver. This would save invoking a script to load the module, but the practice is strongly discouraged. To keep the code compact, drivers/char/misc.c doesn't check for duplication of minor numbers. If the number you chose is later assigned to an o
阅读(825) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~