总结
核心函数是spi_sync, 设备驱动->核心函数-> 控制器驱动
实例分析
(gdb) c
Continuing.Thread 115 hit Breakpoint 1, bcm2835_spi_transfer_one (master=0xffffffc07b8e6000, spi=0xffffffc07b911800, tfr=0xffffff8009f53c40) at drivers/spi/spi-bcm2835.c:534
534 {
(gdb) bt
#0 bcm2835_spi_transfer_one (master=0xffffffc07b8e6000, spi=0xffffffc07b911800, tfr=0xffffff8009f53c40) at drivers/spi/spi-bcm2835.c:534
#1 0xffffff80086cbe8c in spi_transfer_one_message (ctlr=0xffffffc07b8e6000, msg=0xffffff8009f53ca0) at drivers/spi/spi.c:1031
#2 0xffffff80086cc990 in __spi_pump_messages (ctlr=0xffffffc07b8e6000, in_kthread=<optimized out>) at drivers/spi/spi.c:1265
#3 0xffffff80086cccb8 in __spi_sync (spi=0xffffffc07b911800, message=0xffffff8009f53ca0) at drivers/spi/spi.c:3129
#4 0xffffff80086ccd04 in spi_sync (spi=0xffffffc07b911800, message=0xffffff8009f53ca0) at drivers/spi/spi.c:3165
#5 0xffffff80086ce14c in spidev_sync (spidev=<optimized out>, message=0xffffff8009f53ca0) at drivers/spi/spidev.c:112
#6 0xffffff80086ce2f0 in spidev_sync_write (len=<optimized out>, spidev=<optimized out>) at drivers/spi/spidev.c:132
#7 spidev_write (filp=<optimized out>, buf=<optimized out>, count=4, f_pos=<optimized out>) at drivers/spi/spidev.c:199
#8 0xffffff80082c919c in __vfs_write (file=0xffffffc072ea2500, p=0x5587f7cb60 "555\n/dev/spidev0.0\n", count=4, pos=0xffffff8009f53db0) at fs/read_write.c:485
#9 0xffffff80082c93e0 in vfs_write (file=0xffffffc072ea2500, buf=0x5587f7cb60 "555\n/dev/spidev0.0\n", count=4, pos=0xffffff8009f53db0) at fs/read_write.c:549
#10 0xffffff80082c9768 in ksys_write (fd=<optimized out>, buf=0x5587f7cb60 "555\n/dev/spidev0.0\n", count=4) at fs/read_write.c:599
#11 0xffffff80082c9810 in __do_sys_write (count=<optimized out>, buf=<optimized out>, fd=<optimized out>) at fs/read_write.c:611
#12 __se_sys_write (fd=1, buf=367353383776, count=4) at fs/read_write.c:608
#13 0xffffff80082c984c in __arm64_sys_write (regs=0xffffff8009f53ec0) at fs/read_write.c:608
#14 0xffffff8008099254 in __invoke_syscall (regs=0xffffff8009f53ec0, syscall_fn=0xffffff80082c9820 <__arm64_sys_write>) at arch/arm64/kernel/syscall.c:36
#15 0xffffff80080993a4 in invoke_syscall (regs=0xffffff8009f53ec0, scno=<optimized out>, sc_nr=<optimized out>, syscall_table=0xffffff80089c0778 <sys_call_table>) at arch/arm64/kernel/syscall.c:48
#16 0xffffff800809943c in el0_svc_common (regs=0xffffff8009f53ec0, scno=64, sc_nr=294, syscall_table=0xffffff80089c0778 <sys_call_table>) at arch/arm64/kernel/syscall.c:114
#17 0xffffff8008099524 in el0_svc_handler (regs=0xffffff8009f53ec0) at arch/arm64/kernel/syscall.c:160
#18 0xffffff8008084088 in el0_svc () at arch/arm64/kernel/entry.S:917
源码分析
1. 注册字符设备并提供操作函数 spidev_fops
static int __init spidev_init(void)status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
}static const struct file_operations spidev_fops = {.....write = spidev_write,....
};
2. 分析spidev_write 调用spi_sync
static ssize_t
spidev_write(struct file *filp, const char __user *buf,size_t count, loff_t *f_pos)
{struct spidev_data *spidev;ssize_t status = 0;unsigned long missing;spidev = filp->private_data;missing = copy_from_user(spidev->tx_buffer, buf, count);if (missing == 0)status = spidev_sync_write(spidev, count);....}
static inline ssize_t
spidev_sync_write(struct spidev_data *spidev, size_t len)
{struct spi_transfer t = {.tx_buf = spidev->tx_buffer,.len = len,.speed_hz = spidev->speed_hz,};struct spi_message m;spi_message_init(&m);spi_message_add_tail(&t, &m);return spidev_sync(spidev, &m);
}static ssize_t
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
{int status;struct spi_device *spi;spi = spidev->spi;status = spi_sync(spi, message);return status;
}
3. 分析 spi_sync 调用 ctlr->transfer_one_message
int spi_sync(struct spi_device *spi, struct spi_message *message)
{ret = __spi_sync(spi, message);
}static int __spi_sync(struct spi_device *spi, struct spi_message *message)
{DECLARE_COMPLETION_ONSTACK(done);int status;struct spi_controller *ctlr = spi->controller;unsigned long flags;status = __spi_validate(spi, message);if (status != 0)return status;message->complete = spi_complete;message->context = &done;message->spi = spi;SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);if (ctlr->transfer == spi_queued_transfer) { //新方法status = __spi_queued_transfer(spi, message, false);} else { //legacy method //老方法status = spi_async_locked(spi, message);}if (status == 0) {/* Push out the messages in the calling context if we* can.*/if (ctlr->transfer == spi_queued_transfer) {SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync_immediate);SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync_immediate);__spi_pump_messages(ctlr, false); //处理spi 消息}wait_for_completion(&done);status = message->status;}message->context = NULL;return status;
}
static int __spi_queued_transfer(struct spi_device *spi,struct spi_message *msg,bool need_pump)
{struct spi_controller *ctlr = spi->controller;unsigned long flags;...list_add_tail(&msg->queue, &ctlr->queue); 将消息队列添加到队列的尾部if (!ctlr->busy && need_pump)kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);....return 0;
}
static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
{unsigned long flags;bool was_busy = false;int ret;/* Lock queue */spin_lock_irqsave(&ctlr->queue_lock, flags);/* Make sure we are not already running a message */if (ctlr->cur_msg) {spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* If another context is idling the device then defer */if (ctlr->idling) {kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* Check if the queue is idle */if (list_empty(&ctlr->queue) || !ctlr->running) {if (!ctlr->busy) {spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* Only do teardown in the thread */if (!in_kthread) {kthread_queue_work(&ctlr->kworker,&ctlr->pump_messages);spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}ctlr->busy = false;ctlr->idling = true;spin_unlock_irqrestore(&ctlr->queue_lock, flags);kfree(ctlr->dummy_rx);ctlr->dummy_rx = NULL;kfree(ctlr->dummy_tx);ctlr->dummy_tx = NULL;if (ctlr->unprepare_transfer_hardware &&ctlr->unprepare_transfer_hardware(ctlr))dev_err(&ctlr->dev,"failed to unprepare transfer hardware\n");if (ctlr->auto_runtime_pm) {pm_runtime_mark_last_busy(ctlr->dev.parent);pm_runtime_put_autosuspend(ctlr->dev.parent);}trace_spi_controller_idle(ctlr);spin_lock_irqsave(&ctlr->queue_lock, flags);ctlr->idling = false;spin_unlock_irqrestore(&ctlr->queue_lock, flags);return;}/* Extract head of queue */ctlr->cur_msg =list_first_entry(&ctlr->queue, struct spi_message, queue);list_del_init(&ctlr->cur_msg->queue);if (ctlr->busy)was_busy = true;elsectlr->busy = true;spin_unlock_irqrestore(&ctlr->queue_lock, flags);mutex_lock(&ctlr->io_mutex);if (!was_busy && ctlr->auto_runtime_pm) {ret = pm_runtime_get_sync(ctlr->dev.parent);if (ret < 0) {pm_runtime_put_noidle(ctlr->dev.parent);dev_err(&ctlr->dev, "Failed to power device: %d\n",ret);mutex_unlock(&ctlr->io_mutex);return;}}if (!was_busy)trace_spi_controller_busy(ctlr);if (!was_busy && ctlr->prepare_transfer_hardware) {ret = ctlr->prepare_transfer_hardware(ctlr);if (ret) {dev_err(&ctlr->dev,"failed to prepare transfer hardware\n");if (ctlr->auto_runtime_pm)pm_runtime_put(ctlr->dev.parent);mutex_unlock(&ctlr->io_mutex);return;}}trace_spi_message_start(ctlr->cur_msg);if (ctlr->prepare_message) {ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);if (ret) {dev_err(&ctlr->dev, "failed to prepare message: %d\n",ret);ctlr->cur_msg->status = ret;spi_finalize_current_message(ctlr);goto out;}ctlr->cur_msg_prepared = true;}ret = spi_map_msg(ctlr, ctlr->cur_msg);if (ret) {ctlr->cur_msg->status = ret;spi_finalize_current_message(ctlr);goto out;}ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg); 调用控制器的传输函数if (ret) {dev_err(&ctlr->dev,"failed to transfer one message from queue\n");goto out;}out:mutex_unlock(&ctlr->io_mutex);/* Prod the scheduler in case transfer_one() was busy waiting */if (!ret)cond_resched();
}
4. spi controler "传输函数" 之间的相互调用
# ctlr->transfer = spi_queued_transfer; // 调用 ctlr->transfer_one_message
ctlr->transfer_one_message = spi_transfer_one_message; //ctlr->transfer_one
ctrl->transfer_one = bcm2835_spi_transfer_one;