三、spi设备文件的自动产生代码分析
这部分代码相当于注册了一个spi实际driver,既是核心平台无关代码,又是个具体实例,我们来看下一下具体做了什么,也好学习一spi_driver的各部分具体都需要做些什么,代码位于driver/spi/spidev.c下:
- static int __init spidev_init(void)
- {
- int status;
- /* Claim our 256 reserved device numbers. Then register a class
- * that will key udev/mdev to add/remove /dev nodes. Last, register
- * the driver which manages those device numbers.
- */
- BUILD_BUG_ON(N_SPI_MINORS > 256);
- //注册主设备号为153的spi字符设备,spidev_fops结构下面会介绍
- //暂且不表
- status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
- if (status < 0)
- return status;
- //在sys/class下产生spidev这个节点,udev会利用其在dev下产生spidev
- //这个设备节点
- spidev_class = class_create(THIS_MODULE, "spidev");
- if (IS_ERR(spidev_class)) {
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
- return PTR_ERR(spidev_class);
- }
- //注册spi驱动,该函数上面已经分析过
- status = spi_register_driver(&spidev_spi_driver);
- if (status < 0) {
- class_destroy(spidev_class);
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
- }
- return status;
- }
- //先看一下spidev_spi_driver具体的结构
- static struct spi_driver spidev_spi_driver = {
- .driver = {
- .name = "spidev",
- .owner = THIS_MODULE,
- },
- .probe = spidev_probe,
- .remove = __devexit_p(spidev_remove),
- /* NOTE: suspend/resume methods are not necessary here.
- * We don't do anything except pass the requests to/from
- * the underlying controller. The refrigerator handles
- * most issues; the controller driver handles the rest.
- */
- };
- //利用bus的match函数匹配成功后将首先调用spidev_probe函数,我们具体看下该函数:
- static int __devinit spidev_probe(struct spi_device *spi)
- {
- struct spidev_data *spidev;
- int status;
- unsigned long minor;
- /* Allocate driver data */
- //申请spidev_data所需内存,前面已经讲过该结构
- spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
- if (!spidev)
- return -ENOMEM;
- /* Initialize the driver data */
- //赋值
- spidev->spi = spi;
- spin_lock_init(&spidev->spi_lock);
- mutex_init(&spidev->buf_lock);
- INIT_LIST_HEAD(&spidev->device_entry);
- /* If we can allocate a minor number, hook up this device.
- * Reusing minors is fine so long as udev or mdev is working.
- */
- mutex_lock(&device_list_lock);
- //找到一个最小的次设备号
- //addr为内存区的起始地址,size为要查找的最大长度
- //返回第一个位为0的位号
- minor = find_first_zero_bit(minors, N_SPI_MINORS);
- if (minor < N_SPI_MINORS) {
- struct device *dev;
- //下面两句用于在sys/class/spidev下产生类似于
- //spidev%d.%d形式的节点,这样,udev等工具就可以
- //自动在dev下创建相应设备号的设备节点
- spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
- dev = device_create(spidev_class, &spi->dev, spidev->devt,
- spidev, "spidev%d.%d",
- spi->master->bus_num, spi->chip_select);
- status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
- } else {
- dev_dbg(&spi->dev, "no minor number available!/n");
- status = -ENODEV;
- }
- //如果成功标明刚才的次设备号已被占用
- //并且将该设备挂接到device_list链表
- if (status == 0) {
- set_bit(minor, minors);
- list_add(&spidev->device_entry, &device_list);
- }
- mutex_unlock(&device_list_lock);
- //设置spi->dev->p = spidev
- if (status == 0)
- spi_set_drvdata(spi, spidev);
- else
- kfree(spidev);
- return status;
- }
当我们利用板级信息添加一个设备的时候,该driver如果匹配到这个设备,那么就会自动为其创建设备节点,封装spidev_data
信息,并且挂到全局设备链表device_list。
最后看一下刚才注册的字符设备的统一操作集:
- static const struct file_operations spidev_fops = {
- .owner = THIS_MODULE,
- /* REVISIT switch to aio primitives, so that userspace
- * gets more complete API coverage. It'll simplify things
- * too, except for the locking.
- */
- .write = spidev_write,
- .read = spidev_read,
- .unlocked_ioctl = spidev_ioctl,
- .open = spidev_open,
- .release = spidev_release,
- };
- //按应用的操作顺序先看下open函数
- static int spidev_open(struct inode *inode, struct file *filp)
- {
- struct spidev_data *spidev;
- int status = -ENXIO;
- mutex_lock(&device_list_lock);
- //遍历spi设备链表device_list,根据设备号找到spidev_data结构
- list_for_each_entry(spidev, &device_list, device_entry) {
- if (spidev->devt == inode->i_rdev) {
- status = 0;
- break;
- }
- }
- if (status == 0) {
- //buffer为空,为其申请内存
- if (!spidev->buffer) {
- spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
- if (!spidev->buffer) {
- dev_dbg(&spidev->spi->dev, "open/ENOMEM/n");
- status = -ENOMEM;
- }
- }
- //设备用户使用量+1
- if (status == 0) {
- spidev->users++;
- //传到filp的私有成员,在read,write的时候可以从其得到该结构
- filp->private_data = spidev;
- nonseekable_open(inode, filp);
- }
- } else
- pr_debug("spidev: nothing for minor %d/n", iminor(inode));
- mutex_unlock(&device_list_lock);
- return status;
- }
- //再看read函数
- static ssize_t
- spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
- {
- struct spidev_data *spidev;
- ssize_t status = 0;
- /* chipselect only toggles at start or end of operation */
- if (count > bufsiz)
- return -EMSGSIZE;
- //得到传递的结构
- spidev = filp->private_data;
- mutex_lock(&spidev->buf_lock);
- //调用��体的读函数
- status = spidev_sync_read(spidev, count);
- if (status > 0) {
- unsigned long missing;
- //将得到的信息传递给用户
- missing = copy_to_user(buf, spidev->buffer, status);
- if (missing == status)
- status = -EFAULT;
- else
- status = status - missing;
- }
- mutex_unlock(&spidev->buf_lock);
- return status;
- }
- //跟进spidev_sync_read函数,下面的操作比较直白,简要分析下
- //只关注主流程
- static inline ssize_t
- spidev_sync_read(struct spidev_data *spidev, size_t len)
- {
- //临时传输操作结构
- struct spi_transfer t = {
- .rx_buf = spidev->buffer,
- .len = len,
- };
- //传输所用的信息结构
- struct spi_message m;
- //初始化该结构
- spi_message_init(&m);
- //加到请求队列尾
- spi_message_add_tail(&t, &m);
- //调用spidev_sync继续
- return spidev_sync(spidev, &m);
- }
- //继续spidev_sync函数
- static ssize_t
- spidev_sync(struct spidev_data *spidev, struct spi_message *message)
- {
- DECLARE_COMPLETION_ONSTACK(done);
- int status;
- message->complete = spidev_complete;
- //设置个完成量
- message->context = &done;
- spin_lock_irq(&spidev->spi_lock);
- if (spidev->spi == NULL)
- status = -ESHUTDOWN;
- else
- //继续调用spi_async函数
- status = spi_async(spidev->spi, message);
- spin_unlock_irq(&spidev->spi_lock);
- if (status == 0) {
- //等待完成
- wait_for_completion(&done);
- status = message->status;
- if (status == 0)
- status = message->actual_length;
- }
- return status;
- }
- //spi_async函数
- int spi_async(struct spi_device *spi, struct spi_message *message)
- {
- struct spi_master *master = spi->master;
- int ret;
- unsigned long flags;
- spin_lock_irqsave(&master->bus_lock_spinlock, flags);
- if (master->bus_lock_flag)
- ret = -EBUSY;
- else
- //好长...继续跟进
- ret = __spi_async(spi, message);
- spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
- return ret;
- }
- //__spi_async函数
- static int __spi_async(struct spi_device *spi, struct spi_message *message)
- {
- struct spi_master *master = spi->master;
- /* Half-duplex links include original MicroWire, and ones with
- * only one data pin like SPI_3WIRE (switches direction) or where
- * either MOSI or MISO is missing. They can also be caused by
- * software limitations.
- */
- if ((master->flags & SPI_MASTER_HALF_DUPLEX)
- || (spi->mode & SPI_3WIRE)) {
- struct spi_transfer *xfer;
- unsigned flags = master->flags;
- list_for_each_entry(xfer, &message->transfers, transfer_list) {
- if (xfer->rx_buf && xfer->tx_buf)
- return -EINVAL;
- if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
- return -EINVAL;
- if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
- return -EINVAL;
- }
- }
- message->spi = spi;
- message->status = -EINPROGRESS;
- //最后调用master->transfer具体的平台相关的结构
- return master->transfer(spi, message);
- }
read函数就分析到此,至于write函数就不具体分析了,流程是相似的!
四、总结
根据前面的积累,这次简要分析了下spi设备驱动的模型,由于和i2c很相似,就不给出具体框图了,后面有时间会结合具体平台分析一下spi的实例,这次就到这里了 @^.^@
