Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170143
  • 博文数量: 28
  • 博客积分: 1065
  • 博客等级: 少尉
  • 技术积分: 385
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-11 13:20
文章分类

全部博文(28)

文章存档

2014年(1)

2012年(6)

2011年(19)

2010年(2)

我的朋友

分类: LINUX

2012-12-16 10:40:18

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, unsigned int 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, struct file_operations *fops);
int cdev_add( struct cdev *dev, dev_t num, unsigned int count);
2.6内核字符设备驱动移除要用:
void cdev_del( struct cdev *dev );
2.6内核驱动注册完后,要用以下代码创建设备文件
struct device *device_create(struct class *class, struct device *parent,
        dev_t devt, void *drvdata, const char *fmt, ...)
2.6内核驱动要用以下代码移除设备文件:
void device_destroy(struct class *class, dev_t devt)
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
以上也可以用命令创建设备文件:
mknod /dev/设备文件名 字符设备(c是字符设备,b是块设备)   主设备号 次设备号
例如:mknod /dev/testChar c  100 0
删除设备入口:
    rm /dev/testChar
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
2.4驱动总体编写框架:
static int __init my_init(void)
{
//注册设备驱动
register_chrdev (unsigned int major, const char *name, struct file_operations *fops);
//创建设备文件
static devfs_handle_t devfs_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 int major, const char *name ); 
}
module_init( my_init );
module_exit( my_exit );
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
2.6驱动总体编写框架:
static int __init my_init(void)
{
//分配设备编号
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 adding sbc2440_leds_cdev!\n");
  else
   printk ("Success adding sbc2440_leds_cdev!\n");
}
//创建设备文件
struct device *device_create(struct class *class, struct device *parent,
        dev_t devt, void *drvdata, const char *fmt, ...);
}
static int __exit my_exit(void)
{
//移除设备文件
void device_destroy(struct class *class, dev_t devt);
//注销字符设备
cdev_del (sbc2440_leds_cdev); 
//释放设备编号:
unregister_chrdev_region (sbc2440_leds_dev, count);
}
module_init( my_init );
module_exit( my_exit );
------------------------
devfs_mk_cdev()  and devfs_register()  is too old for linux 2.6 ,so we replace them with device_create()
 
阅读(1716) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~