8.8 complete方法
在8.6节的bitbang_work中,当一个message的所有数据发送完成以后,将会调用complete函数。该函数如下:
- /*
- * We can't use the standard synchronous wrappers for file I/O; we
- * need to protect against async removal of the underlying spi_device.
- */
- static void spidev_complete(void *arg)
- {
- complete(arg);
- }
该函数将使在spidev_sync函数中的wait_for_completion得以返回,从而完成了同步IO。
至此,整个write系统调用的流程均以讲解完毕,在这其中也对在master和protocol中未曾给出的函数做出了一一讲解,最后,对第8章进行小结。
8.9 小结

从示意图中,我们可以很清除看到函数的调用过程:先调用spi设备驱动层,随后调用bitbang中间层,最后调用了master驱动层来完成数据的传输。
9. read方法
read方法和write方法基本差不多,关键的区别在于其发送的数据为0,而在s3c24xx_spi_txrx中断服务程序中将读取数据寄存器。下面仅仅给出函数调用示意图。

在这里给出spidev_read和spidev_sync_read,方便读者进行对比。
- /* Read-only message with current device setup */
- 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; /*获取spidev*/
- mutex_lock(&spidev->buf_lock); /*加锁,对buffer进行互斥房屋内*/
- 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;
- }
- 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); /*初始化message*/
- spi_message_add_tail(&t, &m); /*添加transfer*/
- return spidev_sync(spidev, &m);
- }
10. ioctl方法
这一章节中,我们将看一下SPI子系统是如何使用ioctl系统调用来实现全双工读写。
10.1 spi_ioc_transfer
在使用ioctl时,用户空间要使用一个数据结构来封装需要传输的数据,该结构为spi_ioc_transfe。而在write系统调用时,只是简单的从用户空间复制数据过来。该结构中的很多字段将被复制到spi_transfer结构中相应的字段。也就是说一个spi_ioc_transfer表示一个spi_transfer,用户空间可以定义多个spi_ioc_transfe,最后以数组形式传递给ioctl。
下面同时给出ioctl中cmd的值。其中SPI_IOC_MASSAGE用于实现全双工IO,而其他的用于设置或者读取某个特定值。
下列数据结构位于:include/linux/spi/spidev.h。
- /**
- * struct spi_ioc_transfer - describes a single SPI transfer
- * @tx_buf: Holds pointer to userspace buffer with transmit data, or null.
- * If no data is provided, zeroes are shifted out.
- * @rx_buf: Holds pointer to userspace buffer for receive data, or null.
- * @len: Length of tx and rx buffers, in bytes.
- * @speed_hz: Temporary override of the device's bitrate.
- * @bits_per_word: Temporary override of the device's wordsize.
- * @delay_usecs: If nonzero, how long to delay after the last bit transfer
- * before optionally deselecting the device before the next transfer.
- * @cs_change: True to deselect device before starting the next transfer.
- *
- * This structure is mapped directly to the kernel spi_transfer structure;
- * the fields have the same meanings, except of course that the pointers
- * are in a different address space (and may be of different sizes in some
- * cases, such as 32-bit i386 userspace over a 64-bit x86_64 kernel).
- * Zero-initialize the structure, including currently unused fields, to
- * accomodate potential future updates.
- *
- * SPI_IOC_MESSAGE gives userspace the equivalent of kernel spi_sync().
- * Pass it an array of related transfers, they'll execute together.
- * Each transfer may be half duplex (either direction) or full duplex.
- *
- * struct spi_ioc_transfer mesg[4];
- * ...
- * status = ioctl(fd, SPI_IOC_MESSAGE(4), mesg);
- *
- * So for example one transfer might send a nine bit command (right aligned
- * in a 16-bit word), the next could read a block of 8-bit data before
- * terminating that command by temporarily deselecting the chip; the next
- * could send a different nine bit command (re-selecting the chip), and the
- * last transfer might write some register values.
- */
- struct spi_ioc_transfer {
- __u64 tx_buf;
- __u64 rx_buf;
- __u32 len;
- __u32 speed_hz;
- __u16 delay_usecs;
- __u8 bits_per_word;
- __u8 cs_change;
- __u32 pad;
- /* If the contents of 'struct spi_ioc_transfer' ever change
- * incompatibly, then the ioctl number (currently 0) must change;
- * ioctls with constant size fields get a bit more in the way of
- * error checking than ones (like this) where that field varies.
- *
- * NOTE: struct layout is the same in 64bit and 32bit userspace.
- */
- };
- /* not all platforms use <asm-generic/ioctl.h> or _IOC_TYPECHECK() ... */
- #define SPI_MSGSIZE(N) \ /*SPI_MSGSIZE不能大于4KB*/
- ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \
- ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0)
- #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)])
- /* Read / Write of SPI mode (SPI_MODE_0..SPI_MODE_3) */
- #define SPI_IOC_RD_MODE _IOR(SPI_IOC_MAGIC, 1, __u8)
- #define SPI_IOC_WR_MODE _IOW(SPI_IOC_MAGIC, 1, __u8)
- /* Read / Write SPI bit justification */
- #define SPI_IOC_RD_LSB_FIRST _IOR(SPI_IOC_MAGIC, 2, __u8)
- #define SPI_IOC_WR_LSB_FIRST _IOW(SPI_IOC_MAGIC, 2, __u8)
- /* Read / Write SPI device word length (1..N) */
- #define SPI_IOC_RD_BITS_PER_WORD _IOR(SPI_IOC_MAGIC, 3, __u8)
- #define SPI_IOC_WR_BITS_PER_WORD _IOW(SPI_IOC_MAGIC, 3, __u8)
- /* Read / Write SPI device default max speed hz */
- #define SPI_IOC_RD_MAX_SPEED_HZ _IOR(SPI_IOC_MAGIC, 4, __u32)
- #define SPI_IOC_WR_MAX_SPEED_HZ _IOW(SPI_IOC_MAGIC, 4, __u32)
