2.5 spi_transfer
该数据结构是对一次完整的数据传输的描述。
- /*
- * I/O INTERFACE between SPI controller and protocol drivers
- *
- * Protocol drivers use a queue of spi_messages, each transferring data
- * between the controller and memory buffers.
- *
- * The spi_messages themselves consist of a series of read+write transfer
- * segments. Those segments always read the same number of bits as they
- * write; but one or the other is easily ignored by passing a null buffer
- * pointer. (This is unlike most types of I/O API, because SPI hardware
- * is full duplex.)
- *
- * NOTE: Allocation of spi_transfer and spi_message memory is entirely
- * up to the protocol driver, which guarantees the integrity of both (as
- * well as the data buffers) for as long as the message is queued.
- */
- /**
- * struct spi_transfer - a read/write buffer pair
- * @tx_buf: data to be written (dma-safe memory), or NULL
- * @rx_buf: data to be read (dma-safe memory), or NULL
- * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped
- * @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped
- * @len: size of rx and tx buffers (in bytes)
- * @speed_hz: Select a speed other than the device default for this
- * transfer. If 0 the default (from @spi_device) is used.
- * @bits_per_word: select a bits_per_word other than the device default
- * for this transfer. If 0 the default (from @spi_device) is used.
- * @cs_change: affects chipselect after this transfer completes
- * @delay_usecs: microseconds to delay after this transfer before
- * (optionally) changing the chipselect status, then starting
- * the next transfer or completing this @spi_message.
- * @transfer_list: transfers are sequenced through @spi_message.transfers
- *
- * SPI transfers always write the same number of bytes as they read.
- * Protocol drivers should always provide @rx_buf and/or @tx_buf.
- * In some cases, they may also want to provide DMA addresses for
- * the data being transferred; that may reduce overhead, when the
- * underlying driver uses dma.
- *
- * If the transmit buffer is null, zeroes will be shifted out
- * while filling @rx_buf. If the receive buffer is null, the data
- * shifted in will be discarded. Only "len" bytes shift out (or in).
- * It's an error to try to shift out a partial word. (For example, by
- * shifting out three bytes with word size of sixteen or twenty bits;
- * the former uses two bytes per word, the latter uses four bytes.)
- *
- * In-memory data values are always in native CPU byte order, translated
- * from the wire byte order (big-endian except with SPI_LSB_FIRST). So
- * for example when bits_per_word is sixteen, buffers are 2N bytes long
- * (@len = 2N) and hold N sixteen bit words in CPU byte order.
- *
- * When the word size of the SPI transfer is not a power-of-two multiple
- * of eight bits, those in-memory words include extra bits. In-memory
- * words are always seen by protocol drivers as right-justified, so the
- * undefined (rx) or unused (tx) bits are always the most significant bits.
- *
- * All SPI transfers start with the relevant chipselect active. Normally
- * it stays selected until after the last transfer in a message. Drivers
- * can affect the chipselect signal using cs_change.
- *
- * (i) If the transfer isn't the last one in the message, this flag is
- * used to make the chipselect briefly go inactive in the middle of the
- * message. Toggling chipselect in this way may be needed to terminate
- * a chip command, letting a single spi_message perform all of group of
- * chip transactions together.
- *
- * (ii) When the transfer is the last one in the message, the chip may
- * stay selected until the next transfer. On multi-device SPI busses
- * with nothing blocking messages going to other devices, this is just
- * a performance hint; starting a message to another device deselects
- * this one. But in other cases, this can be used to ensure correctness.
- * Some devices need protocol transactions to be built from a series of
- * spi_message submissions, where the content of one message is determined
- * by the results of previous messages and where the whole transaction
- * ends when the chipselect goes intactive.
- *
- * The code that submits an spi_message (and its spi_transfers)
- * to the lower layers is responsible for managing its memory.
- * Zero-initialize every field you don't set up explicitly, to
- * insulate against future API updates. After you submit a message
- * and its transfers, ignore them until its completion callback.
- */
- struct spi_transfer {
- /* it's ok if tx_buf == rx_buf (right?)
- * for MicroWire, one buffer must be null
- * buffers must work with dma_*map_single() calls, unless
- * spi_message.is_dma_mapped reports a pre-existing mapping
- */
- const void *tx_buf;
- void *rx_buf;
- unsigned len;
- dma_addr_t tx_dma;
- dma_addr_t rx_dma;
- unsigned cs_change:1;
- u8 bits_per_word;
- u16 delay_usecs;
- u32 speed_hz;
- struct list_head transfer_list;
- };
2.6 spi_message
该结构就是对多个spi_transfer的封装。
-
/**
-
- * struct spi_message - one multi-segment SPI transaction
- * @transfers: list of transfer segments in this transaction
- * @spi: SPI device to which the transaction is queued
- * @is_dma_mapped: if true, the caller provided both dma and cpu virtual
- * addresses for each transfer buffer
- * @complete: called to report transaction completions
- * @context: the argument to complete() when it's called
- * @actual_length: the total number of bytes that were transferred in all
- * successful segments
- * @status: zero for success, else negative errno
- * @queue: for use by whichever driver currently owns the message
- * @state: for use by whichever driver currently owns the message
- *
- * A @spi_message is used to execute an atomic sequence of data transfers,
- * each represented by a struct spi_transfer. The sequence is "atomic"
- * in the sense that no other spi_message may use that SPI bus until that
- * sequence completes. On some systems, many such sequences can execute as
- * as single programmed DMA transfer. On all systems, these messages are
- * queued, and might complete after transactions to other devices. Messages
- * sent to a given spi_device are alway executed in FIFO order.
- *
- * The code that submits an spi_message (and its spi_transfers)
- * to the lower layers is responsible for managing its memory.
- * Zero-initialize every field you don't set up explicitly, to
- * insulate against future API updates. After you submit a message
- * and its transfers, ignore them until its completion callback.
- */
- struct spi_message {
- struct list_head transfers;
- struct spi_device *spi;
- unsigned is_dma_mapped:1;
- /* REVISIT: we might want a flag affecting the behavior of the
- * last transfer ... allowing things like "read 16 bit length L"
- * immediately followed by "read L bytes". Basically imposing
- * a specific message scheduling algorithm.
- *
- * Some controller drivers (message-at-a-time queue processing)
- * could provide that as their default scheduling algorithm. But
- * others (with multi-message pipelines) could need a flag to
- * tell them about such special cases.
- */
- /* completion is reported through a callback */
- void (*complete)(void *context);
- void *context;
- unsigned actual_length;
- int status;
- /* for optional use by whatever driver currently owns the
- * spi_message ... between calls to spi_async and then later
- * complete(), that's the spi_master controller driver.
- */
- struct list_head queue;
- void *state;
- };
2.7 spi_bitbang
- struct spi_bitbang {
- struct workqueue_struct *workqueue;
- struct work_struct work;
- spinlock_t lock;
- struct list_head queue;
- u8 busy;
- u8 use_dma;
- u8 flags; /* extra spi->mode support */
- struct spi_master *master;
- /* setup_transfer() changes clock and/or wordsize to match settings
- * for this transfer; zeroes restore defaults from spi_device.
- */
- int (*setup_transfer)(struct spi_device *spi,
- struct spi_transfer *t);
- void (*chipselect)(struct spi_device *spi, int is_on);
- #define BITBANG_CS_ACTIVE 1 /* normally nCS, active low */
- #define BITBANG_CS_INACTIVE 0
- /* txrx_bufs() may handle dma mapping for transfers that don't
- * already have one (transfer.{tx,rx}_dma is zero), or use PIO
- */
- int (*txrx_bufs)(struct spi_device *spi, struct spi_transfer *t);
- /* txrx_word[SPI_MODE_*]() just looks like a shift register */
- u32 (*txrx_word[4])(struct spi_device *spi,
- unsigned nsecs,
- u32 word, u8 bits);
- };
