基于s3c2440的嵌入式linux驱动——spi子系统解读(三) -凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 1125429
  • 博文数量: 146
  • 博客积分: 190
  • 博客等级: 入伍新兵
  • 技术积分: 5225
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-06 08:24
个人简介

慢行者

文章分类

全部博文(146)

文章存档

2013年(145)

2012年(1)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: linux

2013-04-26 11:20:09

该系列文章将分为四个部分:

   第一部分,将对spi子系统整体进行描述,同时给出spi的相关数据结构,最后描述spi总线的注册。

   第二部分,该文将对spi的主控制器(master)驱动进行描述。

   第三部分,即本篇文章,该文将对spi设备驱动,也称protocol 驱动,进行讲解。

   第四部分,通过spi设备驱动留给用户层的api,我们将从上到下描述数据是如何通过spi的protocol 驱动,由bitbang中转,最后由master驱动将数据传输出

                   去。

本文属于第三部分。

5. spi设备驱动

    在主控制器驱动中,spi_device已经注册了,在设备驱动中,首先要做的就是注册spi_driver,并提供用户层相应的api。

5.1 spi设备驱动的注册

下列数据结构及函数位于drivers/spi/spidev.c。


[cpp]
  1. staticstruct file_operations spidev_fops = { 
  2.     .owner =    this_module, 
  3.     /* revisit switch to aio primitives, so that userspace
  4.      * gets more complete api coverage.  it'll simplify things
  5.      * too, except for the locking.
  6.      */ 
  7.     .write =    spidev_write, 
  8.     .read =        spidev_read, 
  9.     .unlocked_ioctl = spidev_ioctl, 
  10.     .open =        spidev_open, 
  11.     .release =    spidev_release, 
  12. }; 
  13.  
  14. /* the main reason to have this class is to make mdev/udev create the
  15. * /dev/spidevb.c character device nodes exposing our userspace api.
  16. * it also simplifies memory management.
  17. */ 
  18.  
  19. staticstructclass *spidev_class; 
  20.  
  21. staticstruct spi_driver spidev_spi = { 
  22.     .driver = { 
  23.         .name =        "spidev", 
  24.         .owner =    this_module, 
  25.     }, 
  26.     .probe =    spidev_probe, 
  27.     .remove =    __devexit_p(spidev_remove), 
  28.  
  29.     /* note:  suspend/resume methods are not necessary here.
  30.      * we don't do anything except pass the requests to/from
  31.      * the underlying controller.  the refrigerator handles
  32.      * most issues; the controller driver handles the rest.
  33.      */ 
  34. }; 
  35.  
  36. staticint __init spidev_init(void) 
  37. { 
  38.     int status; 
  39.  
  40.     /* claim our 256 reserved device numbers.  then register a class
  41.      * that will key udev/mdev to add/remove /dev nodes.  last, register
  42.      * the driver which manages those device numbers.
  43.      */ 
  44.     build_bug_on(n_spi_minors > 256);    /*检查次设备号*/ 
  45.     status = register_chrdev(spidev_major, "spi", &spidev_fops); /*注册字符设备,major=153*/ 
  46.     if (status < 0) 
  47.         return status; 
  48.  
  49.     spidev_class = class_create(this_module, "spidev");     /*创建spidev类*/ 
  50.     if (is_err(spidev_class)) { 
  51.         unregister_chrdev(spidev_major, spidev_spi.driver.name); 
  52.         return ptr_err(spidev_class); 
  53.     } 
  54.  
  55.     status = spi_register_driver(&spidev_spi);      /*注册spi_driver,并调用probe方法*/ 
  56.     if (status < 0) { 
  57.         class_destroy(spidev_class); 
  58.         unregister_chrdev(spidev_major, spidev_spi.driver.name); 
  59.     } 
  60.     return status; 
  61. } 
  62. module_init(spidev_init); 
  63.  
  64. staticvoid __exit spidev_exit(void) 
  65. { 
  66.     spi_unregister_driver(&spidev_spi);         /*注销spi_driver*/ 
  67.     class_destroy(spidev_class);                /*注销类*/ 
  68.     unregister_chrdev(spidev_major, spidev_spi.driver.name);/*注销字符设备*/ 
  69. } 
  70. module_exit(spidev_exit); 


该函数中,创建了一个字符设备以提供api给用户层,同时创建了一个spidev类,最后注册spi_driver到内核中。


在这里我们看到了spi设备驱动是如何提供api给用户层的,那就是通过再熟悉不过的字符设备。通过字符设备,给用户层提供了5个api:open,release,write,read和ioctl。本文在后面将介绍open和close,剩余3个将在本系列的第四篇文章中介绍。

接着看下spi_register_driver函数, 该函数位于drivers/spi/spidev.c。

[cpp]
  1. /**
  2. * spi_register_driver - register a spi driver
  3. * @sdrv: the driver to register
  4. * context: can sleep
  5. */ 
  6. int spi_register_driver(struct spi_driver *sdrv) 
  7. { 
  8.     sdrv->driver.bus = &spi_bus_type; 
  9.     if (sdrv->probe) 
  10.         sdrv->driver.probe = spi_drv_probe; 
  11.     if (sdrv->remove) 
  12.         sdrv->driver.remove = spi_drv_remove; 
  13.     if (sdrv->shutdown) 
  14.         sdrv->driver.shutdown = spi_drv_shutdown; 
  15.     return driver_register(&sdrv->driver); 
  16. } 
  17. export_symbol_gpl(spi_register_driver); 


在调用driver_register的过程中,将用driver.name和spi_device的modalias字段进行比较,两者相等则将该spi_driver和spi_device进行绑定。


当spi_driver注册成功以后,将调用probe方法:spidev_probe函数。

5.2 probe方法

我们来看看spidev_probe这个函数,该函数位于drivers/spi/spidev.c。

[cpp]
  1. #define spidev_major            153    /* assigned */ 
  2. #define n_spi_minors            32    /* ... up to 256 */ 
  3.  
  4. static unsigned long    minors[n_spi_minors / bits_per_long];    /**/ 
  5.  
  6. static list_head(device_list); 
  7. static define_mutex(device_list_lock); 
  8.  
  9. staticint spidev_probe(struct spi_device *spi) 
  10. { 
  11.     struct spidev_data  *spidev; 
  12.     int         status; 
  13.     unsigned long       minor; 
  14.  
  15.     /* allocate driver data */ 
  16.     spidev = kzalloc(sizeof(*spidev), gfp_kernel);  /*以kmalloc分配内存,并清0*/ 
  17.     if (!spidev) 
  18.         return -enomem; 
  19.  
  20.     /* initialize the driver data */ 
  21.     spidev->spi = spi;                   /*保存spi_device*/ 
  22.     spin_lock_init(&spidev->spi_lock);   /*初始化自旋锁*/ 
  23.     mutex_init(&spidev->buf_lock);       /*初始化互斥体*/ 
  24.  
  25.     init_list_head(&spidev->device_entry);   /*初始化链表头,链表为双向循环链表*/ 
  26.  
  27.     /* if we can allocate a minor number, hook up this device.
  28.      * reusing minors is fine so long as udev or mdev is working.
  29.      */ 
  30.     mutex_lock(&device_list_lock);      /*上锁*/ 
  31.     minor = find_first_zero_bit(minors, n_spi_minors);  /*分配次设备号*/ 
  32.     if (minor < n_spi_minors) { 
  33.         struct device *dev; 
  34.  
  35.         spidev->devt = mkdev(spidev_major, minor);   /*根据主次设备号来获取设备号*/ 
  36.         dev = device_create(spidev_class, &spi->dev, spidev->devt,    /*创建设备节点*/ 
  37.                     spidev, "spidev%d.%d", 
  38.                     spi->master->bus_num, spi->chip_select); 
  39.         status = is_err(dev) ? ptr_err(dev) : 0; 
  40.     } else { 
  41.         dev_dbg(&spi->dev, "no minor number available!\n"); 
  42.         status = -enodev; 
  43.     } 
  44.     if (status == 0) { 
  45.         set_bit(minor, minors);     /*保存已使用的次设备号*/ 
  46.         list_add(&spidev->device_entry, &device_list);/*在链表头list后面添加entry*/ 
  47.     } 
  48.     mutex_unlock(&device_list_lock);/*解锁互斥体*/ 
  49.  
  50.     if (status == 0) 
  51.         spi_set_drvdata(spi, spidev);/*spi->dev.driver_data=spidev*/ 
  52.     else 
  53.         kfree(spidev); 
  54.  
  55.     return status; 
  56. } 


其中用到的的struct spidev_data结构如下:


[cpp]
  1. struct spidev_data { 
  2.     dev_t            devt; 
  3.     spinlock_t        spi_lock; 
  4.     struct spi_device    *spi; 
  5.     struct list_head    device_entry; 
  6.  
  7.     /* buffer is null unless this device is open (users > 0) */ 
  8.     struct mutex        buf_lock; 
  9.     unsigned        users; 
  10.     u8            *buffer; 
  11. }; 
  12. "font-size: 12px;"> 
  13.  


   这个函数中,分配了spidev_data和次设备号,随后根据主次设备号创建了设备节点。设备节点的名字是spidev“bus_num””.chip_select",意思就是该设备是在第几个spi接口上的第几个设备。

   此外,将spidev添加到device_list中,这样做就方便查找该spidev。

5.3 remove方法

下列函数位于drivers/spi/spidev.c。


[cpp]
  1. staticint spidev_remove(struct spi_device *spi) 
  2. { 
  3.     struct spidev_data  *spidev = spi_get_drvdata(spi); 
  4.  
  5.     /* make sure ops on existing fds can abort cleanly */ 
  6.     spin_lock_irq(&spidev->spi_lock); 
  7.     spidev->spi = null; 
  8.     spi_set_drvdata(spi, null); 
  9.     spin_unlock_irq(&spidev->spi_lock); 
  10.  
  11.     /* prevent new opens */ 
  12.     mutex_lock(&device_list_lock); 
  13.     list_del(&spidev->device_entry); /*删除entry*/ 
  14.     device_destroy(spidev_class, spidev->devt);  /*删除设备节点*/ 
  15.     clear_bit(minor(spidev->devt), minors);/*删除使用的次设备号信息*/ 
  16.     if (spidev->users == 0) 
  17.         kfree(spidev); 
  18.     mutex_unlock(&device_list_lock); 
  19.  
  20.     return 0; 
  21. } 



6. open和release


接着来看下open和release系统调用的api接口,其余3个接口将在本系列的第四篇文章中给出。

6.1 open方法

下列函数位于drivers/spi/spidev.c。


[cpp]
  1. staticint spidev_open(struct inode *inode, struct file *filp) 
  2. { 
  3.     struct spidev_data  *spidev; 
  4.     int         status = -enxio; 
  5.  
  6.     lock_kernel();          /*加锁大内核锁,可以睡眠,只能在进程上下文使用*/ 
  7.     mutex_lock(&device_list_lock);  /*加锁互斥体*/ 
  8.  
  9.     list_for_each_entry(spidev, &device_list, device_entry) {/*从list开始遍历entry,即遍历所有的spidev*/ 
  10.         if (spidev->devt == inode->i_rdev) {  /*判断设备号是否相等*/ 
  11.             status = 0;                         /*找到匹配的spi设备*/ 
  12.             break; 
  13.         } 
  14.     } 
  15.     if (status == 0) { 
  16.         /*note:多个程序调用open方法,但他们共享一个buffer,因此对buufer需要进行互斥保护*/ 
  17.         if (!spidev->buffer) {           /*buffer为空*/  
  18.             spidev->buffer = kmalloc(bufsiz, gfp_kernel);/*分配buffer缓冲区,默认4kb*/ 
  19.             if (!spidev->buffer) { 
  20.                 dev_dbg(&spidev->spi->dev, "open/enomem\n"); 
  21.                 status = -enomem; 
  22.             } 
  23.         } 
  24.         if (status == 0) { 
  25.             spidev->users ;     /*成功open以后,增加用户计数*/ 
  26.             filp->private_data = spidev; /*保存spidev指针*/ 
  27.             nonseekable_open(inode, filp);  /*禁用lseek*/ 
  28.         } 
  29.     } else 
  30.         pr_debug("spidev: nothing for minor %d\n", iminor(inode)); 
  31.  
  32.     mutex_unlock(&device_list_lock);/*释放互斥体*/ 
  33.     unlock_kernel();                /*释放大内核锁*/ 
  34.     return status; 
  35. } 


在这里,以device_list为链表头,遍历所有的spidev_data结构,通过设备节点的设备号和spidev_data中保存的设备号进行匹配,来找到属于该设备节点的spi设备。随后,分配了spi设备驱动层所使用的缓冲区,最后增加打开计数。


6.2 release方法

下列函数位于drivers/spi/spidev.c。


[cpp]
  1. staticint spidev_release(struct inode *inode, struct file *filp) 
  2. { 
  3.     struct spidev_data  *spidev; 
  4.     int         status = 0; 
  5.  
  6.     mutex_lock(&device_list_lock);  /*加锁互斥体*/ 
  7.     spidev = filp->private_data; /*获取spidev*/ 
  8.     filp->private_data = null;        
  9.  
  10.     /* last close? */ 
  11.     spidev->users--;             /*关闭设备文件,减少用户计数*/ 
  12.     if (!spidev->users) {            /*如果用户数为0*/ 
  13.         int     dofree; 
  14.  
  15.         kfree(spidev->buffer);       /*释放缓冲区*/ 
  16.         spidev->buffer = null; 
  17.  
  18.         /* ... after we unbound from the underlying device? */ 
  19.         spin_lock_irq(&spidev->spi_lock);    /*加锁互斥体*/ 
  20.         dofree = (spidev->spi == null);      /*????*/ 
  21.         spin_unlock_irq(&spidev->spi_lock);  /*释放互斥体*/ 
  22.  
  23.         if (dofree) 
  24.             kfree(spidev);          /*释放spidev,在probe中分配*/ 
  25.     } 
  26.     mutex_unlock(&device_list_lock);/*释放互斥体*/ 
  27.  
  28.     return status; 
  29. } 



至此,对于protocol驱动层的框架进行了简单的分析,在下一篇将对该驱动层很多未分析的函数进行一一讲解。下一篇的内容非常的重要哦。

阅读(2809) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图