9 #include <linux/module.h>
10 #include <linux/device.h>
12 #include <linux/string.h>
15 #include <linux/list.h>
19 #include <linux/sched.h>
21 #include <linux/if_arp.h>
25 #ifndef CONFIG_CAIF_SPI_SYNC
26 #define FLAVOR "Flavour: Vanilla.\n"
28 #define FLAVOR "Flavour: Master CMD&LEN at start.\n"
36 #define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1)))))
63 #define BYTE_HEX_FMT "%02X"
65 #define BYTE_HEX_FMT "%02hhX"
68 #define SPI_MAX_PAYLOAD_SIZE 4096
74 #define LOW_WATER_MARK 100
75 #define HIGH_WATER_MARK (LOW_WATER_MARK*5)
107 #ifdef CONFIG_DEBUG_FS
109 #define DEBUGFS_BUF_SIZE 4096
111 static struct dentry *dbgfs_root;
113 static inline void driver_debugfs_create(
void)
118 static inline void driver_debugfs_remove(
void)
123 static inline void dev_debugfs_rem(
struct cfspi *
cfspi)
131 size_t count, loff_t *ppos)
143 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
144 "CAIF SPI debug information:\n");
148 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
149 "STATE: %d\n", cfspi->dbg_state);
150 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
151 "Previous CMD: 0x%x\n", cfspi->pcmd);
152 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
153 "Current CMD: 0x%x\n", cfspi->
cmd);
154 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
155 "Previous TX len: %d\n", cfspi->tx_ppck_len);
156 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
157 "Previous RX len: %d\n", cfspi->rx_ppck_len);
158 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
160 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
162 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
164 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
167 if (len > DEBUGFS_BUF_SIZE)
168 len = DEBUGFS_BUF_SIZE;
176 static ssize_t print_frame(
char *buf,
size_t size,
char *frm,
177 size_t count,
size_t cut)
181 for (i = 0; i <
count; i++) {
182 len +=
snprintf((buf + len), (size - len),
185 if ((i == cut) && (count > (cut * 2))) {
188 len +=
snprintf((buf + len), (size - len),
189 "--- %u bytes skipped ---\n",
190 (
int)(count - (cut * 2)));
193 if ((!(i % 10)) && i) {
194 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
198 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
"\n");
202 static ssize_t dbgfs_frame(
struct file *file,
char __user *user_buf,
203 size_t count, loff_t *ppos)
216 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
219 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
222 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
223 cfspi->
xfer.va_tx[0],
226 len +=
snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
229 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
251 static inline void dev_debugfs_add(
struct cfspi *cfspi)
255 cfspi->dbgfs_dir, cfspi,
258 cfspi->dbgfs_dir, cfspi,
264 cfspi->dbg_state =
state;
268 static inline void driver_debugfs_create(
void)
272 static inline void driver_debugfs_remove(
void)
276 static inline void dev_debugfs_add(
struct cfspi *cfspi)
280 static inline void dev_debugfs_rem(
struct cfspi *cfspi)
362 *dst = (
u8)(spad - 1);
369 cfspi->
ndev->stats.tx_packets++;
370 cfspi->
ndev->stats.tx_bytes += skb->
len;
381 }
while ((dst - buf) < len);
396 while (skb_peek(&cfspi->
chead)) {
432 frm_len += skb->
len + spad + epad;
445 cfspi->
cfdev.flowctrl) {
455 struct cfspi *cfspi = (
struct cfspi *)ifc->
priv;
467 spin_lock(&cfspi->
lock);
475 spin_unlock(&cfspi->
lock);
482 static void cfspi_xfer_done_cb(
struct cfspi_ifc *ifc)
484 struct cfspi *cfspi = (
struct cfspi *)ifc->
priv;
492 struct cfspi *cfspi =
NULL;
497 cfspi = netdev_priv(dev);
506 spin_unlock_irqrestore(&cfspi->
lock, flags);
511 cfspi->
cfdev.flowctrl) {
544 pkt_len |= ((*(src+1)) << 8) & 0xFF00;
549 skb = netdev_alloc_skb(cfspi->
ndev, pkt_len + 1);
553 memcpy(dst, src, pkt_len);
557 skb_reset_mac_header(skb);
566 res = cfspi_xmit(skb, cfspi->
ndev);
569 cfspi->
ndev->stats.rx_packets++;
572 cfspi->
ndev->stats.rx_dropped++;
580 }
while ((src - buf) < len);
587 netif_wake_queue(dev);
591 static int cfspi_close(
struct net_device *dev)
593 netif_stop_queue(dev);
600 struct cfspi *cfspi = netdev_priv(dev);
612 cfspi->
slave =
false;
617 cfspi->
xfer.va_tx[0] = dma_alloc(&cfspi->
xfer.pa_tx[0]);
618 if (!cfspi->
xfer.va_tx[0]) {
620 goto err_dma_alloc_tx_0;
623 cfspi->
xfer.va_rx = dma_alloc(&cfspi->
xfer.pa_rx);
625 if (!cfspi->
xfer.va_rx) {
627 goto err_dma_alloc_rx;
651 init_completion(&cfspi->
comp);
654 dev_debugfs_add(cfspi);
657 cfspi->
ifc.ss_cb = cfspi_ss_cb;
658 cfspi->
ifc.xfer_done_cb = cfspi_xfer_done_cb;
659 cfspi->
ifc.priv = cfspi;
662 spin_lock(&cfspi_list_lock);
664 spin_unlock(&cfspi_list_lock);
672 dma_free(cfspi->
xfer.va_rx, cfspi->
xfer.pa_rx);
674 dma_free(cfspi->
xfer.va_tx[0], cfspi->
xfer.pa_tx[0]);
679 static void cfspi_uninit(
struct net_device *dev)
681 struct cfspi *cfspi = netdev_priv(dev);
684 spin_lock(&cfspi_list_lock);
686 spin_unlock(&cfspi_list_lock);
690 dma_free(cfspi->
xfer.va_rx, cfspi->
xfer.pa_rx);
691 dma_free(cfspi->
xfer.va_tx[0], cfspi->
xfer.pa_tx[0]);
696 dev_debugfs_rem(cfspi);
701 .ndo_open = cfspi_open,
702 .ndo_stop = cfspi_close,
703 .ndo_init = cfspi_init,
704 .ndo_uninit = cfspi_uninit,
705 .ndo_start_xmit = cfspi_xmit
708 static void cfspi_setup(
struct net_device *dev)
710 struct cfspi *cfspi = netdev_priv(dev);
718 skb_queue_head_init(&cfspi->
qhead);
719 skb_queue_head_init(&cfspi->
chead);
721 cfspi->
cfdev.use_frag =
false;
722 cfspi->
cfdev.use_stx =
false;
723 cfspi->
cfdev.use_fcs =
false;
729 struct cfspi *cfspi =
NULL;
736 "cfspi%d", cfspi_setup);
740 cfspi = netdev_priv(ndev);
741 netif_stop_queue(ndev);
770 static void __exit cfspi_exit_module(
void)
774 struct cfspi *cfspi =
NULL;
783 &driver_attr_up_head_align);
785 &driver_attr_up_tail_align);
787 &driver_attr_down_head_align);
789 &driver_attr_down_tail_align);
794 driver_debugfs_remove();
797 static int __init cfspi_init_module(
void)
808 goto err_dev_register;
814 &driver_attr_up_head_align);
817 goto err_create_up_head_align;
822 &driver_attr_up_tail_align);
825 goto err_create_up_tail_align;
830 &driver_attr_down_head_align);
833 goto err_create_down_head_align;
838 &driver_attr_down_tail_align);
841 goto err_create_down_tail_align;
846 &driver_attr_frame_align);
849 goto err_create_frame_align;
851 driver_debugfs_create();
854 err_create_frame_align:
856 &driver_attr_down_tail_align);
857 err_create_down_tail_align:
859 &driver_attr_down_head_align);
860 err_create_down_head_align:
862 &driver_attr_up_tail_align);
863 err_create_up_tail_align:
865 &driver_attr_up_head_align);
866 err_create_up_head_align: