分享

devfs_mk_cdev 与devfs_register引用 ARM嵌入式LINUX设备驱动设计入 - 嵌入式驱动开发 - wuyunzdh

 jason zhai 2010-08-30
devfs_mk_cdev 与devfs_register引用 ARM嵌入式LINUX设备驱动设计入
 设备驱动第三版:   

如果你深入浏览 2.6 内核的大量驱动代码, 你可能注意到有许多字符驱动不使用我们刚刚描述过的 cdev 接口. 你见到的是还没有更新到 2.6 内核接口的老代码. 因为那个代码实际上能用, 这个更新可能很长时间不会发生. 为完整, 我们描述老的字符设备注册接口, 但是新代码不应当使用它; 这个机制在将来内核中可能会消失.

注册一个字符设备的经典方法是使用:

int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);
                                    

这里, major 是感兴趣的主编号, name 是驱动的名子(出现在 /proc/devices), fops 是缺省的 file_operations 结构. 一个对 register_chrdev 的调用为给定的主编号注册 0 - 255 的次编号, 并且为每一个建立一个缺省的 cdev 结构. 使用这个接口的驱动必须准备好处理对所有 256 个次编号的 open 调用( 不管它们是否对应真实设备 ), 它们不能使用大于 255 的主或次编号.

如果你使用 register_chrdev, 从系统中去除你的设备的正确的函数是:

int unregister_chrdev(unsigned int major, const char *name);
                                    

major 和 name 必须和传递给 register_chrdev 的相同, 否则调用会失败

 
  经过一段时间的学习之后,也开发了一些小型的驱动,正如我之前一篇中写到得,现在我就来写一下在ARM嵌入式LINUX下如何设计驱动的框架。

        在这里我用的板子是micro2440板子,板子上的linux版本是2.6.13。因为我在前一篇介绍了驱动编程的两种框架设计,所以现在我就来分别写一下这两种框架设计的程序。

/*注意

*之所以不用devfs_register,而用devfs_mk_cdev,原因是因为在linux2.6内核里没有devfs_register函数,而改用*devfs_mk_cdev

*/

下面是网上摘录的LED驱动!!!!!!!

开发平台:RED HAT LINUX 9(Linux 2.4.18)

开发板:micro2440(友善之臂)(Linux 2.6.13)

交叉编译工具:arm-linux-gcc-3.4.1

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

2.4内核驱动框架:

static int __init leds_init(void)
{
 int result;
 int i;

 result = register_chrdev(LED_MAJOR, DEVICE_NAME, &leds_fops);
 if(result < 0){
  printk(DEVICE_NAME "can't register major number\n");
  return result;
 }
 
// static devfs_handle_t devfs_handle;
// devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, LED_MAJOR, &leds_fops, NULL);

/*注意

*之所以不用devfs_register,而用devfs_mk_cdev,原因是因为在linux2.6内核里没有devfs_register函数,而改用*devfs_mk_cdev

*/
 devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);

 for(i = 0; i < 4; i++){
  s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
  s3c2410_gpio_setpin(led_table[i], 1);
 }

 printk(DEVICE_NAME "initialized\n");
 return 0;
}

static void __exit leds_exit(void)
{
 devfs_remove(DEVICE_NAME);
 unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

2.6内核驱动框架:

static int __init leds_init(void)
{
 int result;
 int i;
 dev_t dev = 0;

 dev = MKDEV(LED_MAJOR, 0);
 result = register_chrdev_region(dev, 1, DEVICE_NAME);
 if (result < 0)
 {
  printk(KERN_WARNING "DEMO: can't get major %d\n", LED_MAJOR);
  return result;
 }
 
 LED_devices = kmalloc(sizeof(struct DEMO_dev), GFP_KERNEL);
 if (!LED_devices)
 {
  result = -ENOMEM;
  goto fail;
 }
 memset(LED_devices, 0, sizeof(struct DEMO_dev));

 cdev_init(&LED_devices->cdev, &leds_fops);
 LED_devices->cdev.owner = THIS_MODULE;
 LED_devices->cdev.ops = &leds_fops;
 result = cdev_add (&LED_devices->cdev, dev, 1);
 if(result)
 {
  printk(KERN_NOTICE "Error %d adding DEMO\n", result);
  goto fail;
 }
 devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);

 for(i = 0; i < 4; i++){
  s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
  s3c2410_gpio_setpin(led_table[i], 1);
 }

 printk(DEVICE_NAME "initialized\n");
  return 0;
fail:
 leds_exit();
 return result;

}

static void __exit leds_exit(void)
{
 dev_t devno = MKDEV(LED_MAJOR, 0);

 if (LED_devices)
 {
  devfs_remove(DEVICE_NAME);
  cdev_del(&LED_devices->cdev);
  kfree(LED_devices);
 }

 unregister_chrdev_region(devno,1);
}

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

在这里我事先都写好了文件才做结构体,和操作函数ioctl,下面我就来介绍介绍

static int leds_ioctl(
 struct inode *inode,
 struct file *file,
 unsigned int cmd,
 unsigned long arg)
{
 switch(cmd){
 case 0:
 case 1:
  if(arg > 4){
   return -EINVAL;
  }
  s3c2410_gpio_setpin(led_table[arg], !cmd);
  return 0;
 default:
  return -EINVAL;
 }
}

源程序下载:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多