8.5 spi_bitbang_transfer
下列代码位于drivers/spi/spi_bitbang.c中。
- /**
- * spi_bitbang_transfer - default submit to transfer queue
- */
- int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
- {
- struct spi_bitbang *bitbang;
- unsigned long flags;
- int status = 0;
- m->actual_length = 0;
- m->status = -EINPROGRESS;
- bitbang = spi_master_get_devdata(spi->master);
- spin_lock_irqsave(&bitbang->lock, flags);
- if (!spi->max_speed_hz)
- status = -ENETDOWN;
- else {
- /*下面的工作队列和queue在spi_bitbang_start函数中初始化*/
- list_add_tail(&m->queue, &bitbang->queue); /*将message添加到bitbang的queue链表中*/
- queue_work(bitbang->workqueue, &bitbang->work); /*提交工作到工作队列*/
- }
- spin_unlock_irqrestore(&bitbang->lock, flags);
- return status;
- }
这里将message添加到了bitbang的queue链表中。然后提交了一个工作到工作队列,随后函数返回到spi_async,又返回到spidev_sync中。为方便将spidev_sync的部分代码列出:
- status = spi_async(spidev->spi, message);/*异步,用complete来完成同步*/
- spin_unlock_irq(&spidev->spi_lock);
- if (status == 0) {
- wait_for_completion(&done); /*在bitbang_work中调用complete方法来唤醒*/
- status = message->status;
- if (status == 0)
- status = message->actual_length; /*返回发送的字节数*/
- }
- return status;
当spi_async函数返回后,需要发送的数据已经通过工作的形式添加到了工作队列,在稍后的工作执行时,将完成数据的发送。随后调用了wait_for_completion等待数据的发送完成。到此,可以看出completion的使用是用来完成同步I/O的
8.6 bitbang_work
在上一节最后添加了工作bitbang->work到工作队列中,在过一段时候后,内核将以进程执行该work。而work即为在spi_bitbang_start中定义的bitbang_work函数。我们来看下这个函数。
下列代码位于drivers/spi/spi_bitbang.c中。
- /*
- * SECOND PART ... simple transfer queue runner.
- *
- * This costs a task context per controller, running the queue by
- * performing each transfer in sequence. Smarter hardware can queue
- * several DMA transfers at once, and process several controller queues
- * in parallel; this driver doesn't match such hardware very well.
- *
- * Drivers can provide word-at-a-time i/o primitives, or provide
- * transfer-at-a-time ones to leverage dma or fifo hardware.
- */
- static void bitbang_work(struct work_struct *work)
- {
- struct spi_bitbang *bitbang =
- container_of(work, struct spi_bitbang, work); /*获取spi_bitbang*/
- unsigned long flags;
- spin_lock_irqsave(&bitbang->lock, flags); /*自旋锁加锁*/
- bitbang->busy = 1; /*bitbang忙碌*/
- while (!list_empty(&bitbang->queue)) { /*有spi_message*/
- struct spi_message *m;
- struct spi_device *spi;
- unsigned nsecs;
- struct spi_transfer *t = NULL;
- unsigned tmp;
- unsigned cs_change;
- int status;
- int (*setup_transfer)(struct spi_device *,
- struct spi_transfer *);
- m = container_of(bitbang->queue.next, struct spi_message,/*获取spi_message*/
- queue);
- list_del_init(&m->queue); /*以获取spi_message,删除该spi_message*/
- spin_unlock_irqrestore(&bitbang->lock, flags);/*释放自旋锁*/
- /* FIXME this is made-up ... the correct value is known to
- * word-at-a-time bitbang code, and presumably chipselect()
- * should enforce these requirements too?
- */
- nsecs = 100;
- spi = m->spi;
- tmp = 0;
- cs_change = 1;
- status = 0;
- setup_transfer = NULL;
- /*遍历,获取所有的spi_transfer*/
- list_for_each_entry (t, &m->transfers, transfer_list) {
- /* override or restore speed and wordsize */
- if (t->speed_hz || t->bits_per_word) { /*如果这两个参数有任何一个已经设置了,本例中没有定义*/
- setup_transfer = bitbang->setup_transfer;
- if (!setup_transfer) {
- status = -ENOPROTOOPT;
- break;
- }
- }
- if (setup_transfer) { /*本例中为NULL*/
- status = setup_transfer(spi, t);
- if (status < 0)
- break;
- }
- /* set up default clock polarity, and activate chip;
- * this implicitly updates clock and spi modes as
- * previously recorded for this device via setup().
- * (and also deselects any other chip that might be
- * selected ...)
- */
- if (cs_change) { /*初值为1*/
- bitbang->chipselect(spi, BITBANG_CS_ACTIVE);/*即调用s3c24xx_spi_chipsel,激活CS信号,写寄存器,设置SPI模式*/
- ndelay(nsecs); /*延迟100纳秒*/
- }
- cs_change = t->cs_change; /*保存cs_change*/
- if (!t->tx_buf && !t->rx_buf && t->len) { /*检查参数*/
- status = -EINVAL;
- break;
- }
- /* transfer data. the lower level code handles any
- * new dma mappings it needs. our caller always gave
- * us dma-safe buffers.
- */
- if (t->len) {
- /* REVISIT dma API still needs a designated
- * DMA_ADDR_INVALID; ~0 might be better.
- */
- if (!m->is_dma_mapped)
- t->rx_dma = t->tx_dma = 0; /*不使用DMA*/
- status = bitbang->txrx_bufs(spi, t); /*即调用s3c24xx_spi_txrx,开始发送数据,status为已发送数据的大小*/
- }
- if (status > 0)
- m->actual_length += status; /*保存已发送字节*/
- if (status != t->len) { /*要求发送和已发送的大小不同*/
- /* always report some kind of error */
- if (status >= 0)
- status = -EREMOTEIO;
- break;
- }
- status = 0;
- /* protocol tweaks before next transfer */
- if (t->delay_usecs)
- udelay(t->delay_usecs); /*延迟*/
- if (!cs_change)/*判断是否需要禁止CS,为1表示要求在两次数据传输之间禁止CS*/
- continue;
- if (t->transfer_list.next == &m->transfers) /*没有transfer*/
- break;
- /* sometimes a short mid-message deselect of the chip
- * may be needed to terminate a mode or command
- */
- ndelay(nsecs); /*延迟*/
- bitbang->chipselect(spi, BITBANG_CS_INACTIVE); /*禁止CS*/
- ndelay(nsecs);
- } /*遍历spi_transfer结束*/
- m->status = status;
- m->complete(m->context); /*调用complete,一个message处理完毕*/
- /* restore speed and wordsize */
- if (setup_transfer)
- setup_transfer(spi, NULL);
- /* normally deactivate chipselect ... unless no error and
- * cs_change has hinted that the next message will probably
- * be for this chip too.
- */
- if (!(status == 0 && cs_change)) {
- ndelay(nsecs);
- bitbang->chipselect(spi, BITBANG_CS_INACTIVE); /*禁止CS*/
- ndelay(nsecs);
- }
- spin_lock_irqsave(&bitbang->lock, flags);
- }
- bitbang->busy = 0;
- spin_unlock_irqrestore(&bitbang->lock, flags);
- }
本函数中,调用了两个方法bibang->chipselect和bitbang->txrx_bufs,这两个方法实际调用了s3c24xx_spi_chipsel和s3c24xx_spi_txrx函数,这两个函数都是master驱动层提供的函数。s3c24xx_spi_chipsel已经在4.2.2节中给出,该函数设置控制寄存器并激活CS信号。s3c24xx_spi_txrx函数的实参t,即为spi_transfer,函数完成该spi_transfer中数据的发送,并返回已发送的字节数。然后,判断是否需要禁止CS。接着遍历到下一个spi_transfer,再次发送数据。当所有spi_transfer发送完成以后,将调用complete方法,从而让在spidev_sync函数中等待completion的函数返回。下面,先来来看下数据是怎么发送出去的,也就是s3c24xx_spi_txrx函数。最后,看看complete方法。
