linux2.4和2.6在注册设备驱动函数…

来源:互联网 发布:美达施膳食纤维粉 知乎 编辑:程序博客网 时间:2024/06/09 12:58

2.4内核注册驱动要用:
int register_chrdev (unsigned int major,const char *name, struct file_operations*fops);
2.4内核注销驱动要用:
int unregister_chrdev( unsigned int major,const char *name );
2.4内核驱动注册完后,要用以下代码创建设备文件
static devfs_handle_t devfs_handle;
devfs_handle = devfs_register( NULL, DEVICE_NAME,DEVFS_FL_DEFAULT,
        BUTTON_MAJOR,&sbc2410_buttons_fops, NULL);
2.4内核驱动要用以下代码移除设备文件:
devfs_unregister( devfs_handle);
------------------------------------------------------------------------------------------- 

2.6驱动i注册设备号要用:
(1)如果主设备号事先知道,要用:
int register_chrdev_region( dev_t first,unsigned int count, char *name );
(2)如果主设备号为0,则要用动态分配:
int alloc_chrdev_region( dev_t *dev, unsignedint firstminor,unsigned int count, char *name);
2.6释放设备号要用:
void unregister_chrdev_region( dev_t first,unsigned int count );
2.6内核字符设备驱动注册要用:
struct cdev *my_cdev = cdev_alloc();
my_cdev->ops = &chr_fops;
void cdev_init( struct cdev *cdev, structfile_operations *fops);
int cdev_add( struct cdev *dev, dev_t num, unsigned intcount);
2.6内核字符设备驱动移除要用:
void cdev_del( struct cdev *dev);
2.6内核驱动注册完后,要用以下代码创建设备文件

从linux内核2.6的某个版本之后,devfs不复存在,udev成为devfs的替代。相比devfs,udev有很多优势,在此就不罗嗦了,提醒一点,udev是应用层的东东,不要试图在内核的配置选项里找到它;加入对udev的支持很简单,以作者所写的一个字符设备驱动为例,在驱动初始化的代码里调用class_create为该设备创建一个class,再为每个设备调用class_device_create创建对应的设备。大致用法如下:
struct class *myclass =class_create(THIS_MODULE, “my_device_driver”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL,“my_device”);

这样的module被加载时
udevdaemon就会自动在/dev下创建my_device设备文件
class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module*owner, const char *name)
    class_create- create a struct class structure
    @owner:pointer to the module that is to "own" this struct class
    @name:pointer to a string for the name of this class.
在/sys/class/下创建类目录
class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *
class_device_create(structclass       *cls,
                                        struct class_device *parent,
                                        dev_t              devt,
                                        structdevice      *device,
                                        constchar         *fmt, ...)

   class_device_create - creates a class device and registers it withsysfs
    @cls:pointer to the struct class that this device should be registeredto.
    @parent:pointer to the parent struct class_device of this new device, ifany.
    @devt: thedev_t for the char device to be added.
    @device: apointer to a struct device that is assiociated with this classdevice.
    @fmt: stringfor the class device's name
 
linux2.4和2.6在注册设备驱动函数上的区别


以上也可以用命令创建设备文件:
mknod/dev/设备文件名字符设备(c是字符设备,b是块设备)  主设备号次设备号
例如:mknod /dev/testChar c  100 0
删除设备入口:
rm /dev/testChar
-------------------------------------------------------------------------------------------------- 2.4驱动总体编写框架:
static int __init my_init(void)
{
 //注册设备驱动
  register_chrdev (unsigned intmajor, const char *name, struct file_operations *fops);
 //创建设备文件
  static devfs_handle_tdevfs_handle;
 devfs_handle = devfs_register( NULL, DEVICE_NAME,DEVFS_FL_DEFAULT,
  BUTTON_MAJOR,&sbc2410_buttons_fops,NULL);
}
static int __exit my_exit(void)
{
 //移除设备文件
  devfs_unregister(devfs_handle);
 //注销设备驱动
  unregister_chrdev( unsigned intmajor, const char *name );
}
module_init( my_init );
module_exit( my_exit );
--------------------------------------------------------------------------------------------------
2.6驱动总体编写框架:
static int __init my_init(void)
{
 static struct class*sbc2440_leds_class = NULL;
 if(主设备号)  //分配设备编号
 {
  sbc2440_leds_dev = MKDEV(LED_MAJOR, LED_MINOR);
  result = register_chrdev_region(sbc2440_leds_dev, count, DEVICE_NAME);
 }
 else
 {
  result = alloc_chrdev_region(&sbc2440_leds_dev, LED_MINOR, count,DEVICE_NAME);
  LED_MAJOR = MAJOR(sbc2440_leds_dev);
 }
 //注册字符设备驱动
 sbc2440_leds_cdev = cdev_alloc();
 if (sbc2440_leds_cdev != NULL)
 {
  cdev_init (sbc2440_leds_cdev,&sbc2440_leds_fops);
  sbc2440_leds_cdev->ops= &sbc2440_leds_fops;
  sbc2440_leds_cdev->owner= THIS_MODULE;
  if (cdev_add(sbc2440_leds_cdev, sbc2440_leds_dev, count) )
   printk(KERN_NOTICE "Someting wrong when addingsbc2440_leds_cdev!/n");
  else
   printk("Success adding sbc2440_leds_cdev!/n");
 }
 
 //创建设备文件

sbc2440_leds_class =class_create(THIS_MODULE, “ sbc2440_leds_class”);
class_device_create(sbc2440_leds_class,NULL,MKDEV(LED_MAJOR,0),NULL,"DEVICE_NAME");

 
}
static int __exit my_exit(void)
{
 class_destroy(sbc2440_leds_class );

 class_device_destroy(sbc2440_leds_class,MKDEV(LED_MAJOR,0));
 //注销字符设备
 cdev_del (sbc2440_leds_cdev);
 //释放设备编号:
 unregister_chrdev_region (sbc2440_leds_dev,count);
}
module_init( my_init );
module_exit( my_exit );

 

红色部分改了有一些改动,可能也存在以下问题,如果网友发现,欢迎指正。谢谢大家!

0 0