手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

基于S3C2440的嵌入式Linux驱动——SPI子系统解读(三)

[日期:2012-08-16] 来源:Linux社区  作者:yj4231 [字体:大 中 小]

5.2 probe方法

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

  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. static int 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结构如下:

  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. };  

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

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

linux
相关资讯       嵌入式Linux  S3C2440 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款