caif_spi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Daniel Martensson
  4. * License terms: GNU General Public License (GPL) version 2.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/string.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/completion.h>
  13. #include <linux/list.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/delay.h>
  17. #include <linux/sched.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/if_arp.h>
  20. #include <net/caif/caif_layer.h>
  21. #include <net/caif/caif_spi.h>
  22. #ifndef CONFIG_CAIF_SPI_SYNC
  23. #define FLAVOR "Flavour: Vanilla.\n"
  24. #else
  25. #define FLAVOR "Flavour: Master CMD&LEN at start.\n"
  26. #endif /* CONFIG_CAIF_SPI_SYNC */
  27. MODULE_LICENSE("GPL");
  28. MODULE_AUTHOR("Daniel Martensson");
  29. MODULE_DESCRIPTION("CAIF SPI driver");
  30. /* Returns the number of padding bytes for alignment. */
  31. #define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1)))))
  32. static bool spi_loop;
  33. module_param(spi_loop, bool, S_IRUGO);
  34. MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode.");
  35. /* SPI frame alignment. */
  36. module_param(spi_frm_align, int, S_IRUGO);
  37. MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment.");
  38. /*
  39. * SPI padding options.
  40. * Warning: must be a base of 2 (& operation used) and can not be zero !
  41. */
  42. module_param(spi_up_head_align, int, S_IRUGO);
  43. MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment.");
  44. module_param(spi_up_tail_align, int, S_IRUGO);
  45. MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment.");
  46. module_param(spi_down_head_align, int, S_IRUGO);
  47. MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment.");
  48. module_param(spi_down_tail_align, int, S_IRUGO);
  49. MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment.");
  50. #ifdef CONFIG_ARM
  51. #define BYTE_HEX_FMT "%02X"
  52. #else
  53. #define BYTE_HEX_FMT "%02hhX"
  54. #endif
  55. #define SPI_MAX_PAYLOAD_SIZE 4096
  56. /*
  57. * Threshold values for the SPI packet queue. Flowcontrol will be asserted
  58. * when the number of packets exceeds HIGH_WATER_MARK. It will not be
  59. * deasserted before the number of packets drops below LOW_WATER_MARK.
  60. */
  61. #define LOW_WATER_MARK 100
  62. #define HIGH_WATER_MARK (LOW_WATER_MARK*5)
  63. #ifdef CONFIG_UML
  64. /*
  65. * We sometimes use UML for debugging, but it cannot handle
  66. * dma_alloc_coherent so we have to wrap it.
  67. */
  68. static inline void *dma_alloc(dma_addr_t *daddr)
  69. {
  70. return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL);
  71. }
  72. static inline void dma_free(void *cpu_addr, dma_addr_t handle)
  73. {
  74. kfree(cpu_addr);
  75. }
  76. #else
  77. static inline void *dma_alloc(dma_addr_t *daddr)
  78. {
  79. return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr,
  80. GFP_KERNEL);
  81. }
  82. static inline void dma_free(void *cpu_addr, dma_addr_t handle)
  83. {
  84. dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle);
  85. }
  86. #endif /* CONFIG_UML */
  87. #ifdef CONFIG_DEBUG_FS
  88. #define DEBUGFS_BUF_SIZE 4096
  89. static struct dentry *dbgfs_root;
  90. static inline void driver_debugfs_create(void)
  91. {
  92. dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL);
  93. }
  94. static inline void driver_debugfs_remove(void)
  95. {
  96. debugfs_remove(dbgfs_root);
  97. }
  98. static inline void dev_debugfs_rem(struct cfspi *cfspi)
  99. {
  100. debugfs_remove(cfspi->dbgfs_frame);
  101. debugfs_remove(cfspi->dbgfs_state);
  102. debugfs_remove(cfspi->dbgfs_dir);
  103. }
  104. static ssize_t dbgfs_state(struct file *file, char __user *user_buf,
  105. size_t count, loff_t *ppos)
  106. {
  107. char *buf;
  108. int len = 0;
  109. ssize_t size;
  110. struct cfspi *cfspi = file->private_data;
  111. buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
  112. if (!buf)
  113. return 0;
  114. /* Print out debug information. */
  115. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  116. "CAIF SPI debug information:\n");
  117. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR);
  118. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  119. "STATE: %d\n", cfspi->dbg_state);
  120. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  121. "Previous CMD: 0x%x\n", cfspi->pcmd);
  122. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  123. "Current CMD: 0x%x\n", cfspi->cmd);
  124. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  125. "Previous TX len: %d\n", cfspi->tx_ppck_len);
  126. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  127. "Previous RX len: %d\n", cfspi->rx_ppck_len);
  128. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  129. "Current TX len: %d\n", cfspi->tx_cpck_len);
  130. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  131. "Current RX len: %d\n", cfspi->rx_cpck_len);
  132. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  133. "Next TX len: %d\n", cfspi->tx_npck_len);
  134. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  135. "Next RX len: %d\n", cfspi->rx_npck_len);
  136. if (len > DEBUGFS_BUF_SIZE)
  137. len = DEBUGFS_BUF_SIZE;
  138. size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  139. kfree(buf);
  140. return size;
  141. }
  142. static ssize_t print_frame(char *buf, size_t size, char *frm,
  143. size_t count, size_t cut)
  144. {
  145. int len = 0;
  146. int i;
  147. for (i = 0; i < count; i++) {
  148. len += snprintf((buf + len), (size - len),
  149. "[0x" BYTE_HEX_FMT "]",
  150. frm[i]);
  151. if ((i == cut) && (count > (cut * 2))) {
  152. /* Fast forward. */
  153. i = count - cut;
  154. len += snprintf((buf + len), (size - len),
  155. "--- %u bytes skipped ---\n",
  156. (int)(count - (cut * 2)));
  157. }
  158. if ((!(i % 10)) && i) {
  159. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  160. "\n");
  161. }
  162. }
  163. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n");
  164. return len;
  165. }
  166. static ssize_t dbgfs_frame(struct file *file, char __user *user_buf,
  167. size_t count, loff_t *ppos)
  168. {
  169. char *buf;
  170. int len = 0;
  171. ssize_t size;
  172. struct cfspi *cfspi;
  173. cfspi = file->private_data;
  174. buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
  175. if (!buf)
  176. return 0;
  177. /* Print out debug information. */
  178. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  179. "Current frame:\n");
  180. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  181. "Tx data (Len: %d):\n", cfspi->tx_cpck_len);
  182. len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
  183. cfspi->xfer.va_tx[0],
  184. (cfspi->tx_cpck_len + SPI_CMD_SZ), 100);
  185. len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
  186. "Rx data (Len: %d):\n", cfspi->rx_cpck_len);
  187. len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
  188. cfspi->xfer.va_rx,
  189. (cfspi->rx_cpck_len + SPI_CMD_SZ), 100);
  190. size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  191. kfree(buf);
  192. return size;
  193. }
  194. static const struct file_operations dbgfs_state_fops = {
  195. .open = simple_open,
  196. .read = dbgfs_state,
  197. .owner = THIS_MODULE
  198. };
  199. static const struct file_operations dbgfs_frame_fops = {
  200. .open = simple_open,
  201. .read = dbgfs_frame,
  202. .owner = THIS_MODULE
  203. };
  204. static inline void dev_debugfs_add(struct cfspi *cfspi)
  205. {
  206. cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root);
  207. cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO,
  208. cfspi->dbgfs_dir, cfspi,
  209. &dbgfs_state_fops);
  210. cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO,
  211. cfspi->dbgfs_dir, cfspi,
  212. &dbgfs_frame_fops);
  213. }
  214. inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
  215. {
  216. cfspi->dbg_state = state;
  217. };
  218. #else
  219. static inline void driver_debugfs_create(void)
  220. {
  221. }
  222. static inline void driver_debugfs_remove(void)
  223. {
  224. }
  225. static inline void dev_debugfs_add(struct cfspi *cfspi)
  226. {
  227. }
  228. static inline void dev_debugfs_rem(struct cfspi *cfspi)
  229. {
  230. }
  231. inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
  232. {
  233. }
  234. #endif /* CONFIG_DEBUG_FS */
  235. static LIST_HEAD(cfspi_list);
  236. static spinlock_t cfspi_list_lock;
  237. /* SPI uplink head alignment. */
  238. static ssize_t show_up_head_align(struct device_driver *driver, char *buf)
  239. {
  240. return sprintf(buf, "%d\n", spi_up_head_align);
  241. }
  242. static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL);
  243. /* SPI uplink tail alignment. */
  244. static ssize_t show_up_tail_align(struct device_driver *driver, char *buf)
  245. {
  246. return sprintf(buf, "%d\n", spi_up_tail_align);
  247. }
  248. static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL);
  249. /* SPI downlink head alignment. */
  250. static ssize_t show_down_head_align(struct device_driver *driver, char *buf)
  251. {
  252. return sprintf(buf, "%d\n", spi_down_head_align);
  253. }
  254. static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL);
  255. /* SPI downlink tail alignment. */
  256. static ssize_t show_down_tail_align(struct device_driver *driver, char *buf)
  257. {
  258. return sprintf(buf, "%d\n", spi_down_tail_align);
  259. }
  260. static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL);
  261. /* SPI frame alignment. */
  262. static ssize_t show_frame_align(struct device_driver *driver, char *buf)
  263. {
  264. return sprintf(buf, "%d\n", spi_frm_align);
  265. }
  266. static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL);
  267. int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len)
  268. {
  269. u8 *dst = buf;
  270. caif_assert(buf);
  271. if (cfspi->slave && !cfspi->slave_talked)
  272. cfspi->slave_talked = true;
  273. do {
  274. struct sk_buff *skb;
  275. struct caif_payload_info *info;
  276. int spad = 0;
  277. int epad;
  278. skb = skb_dequeue(&cfspi->chead);
  279. if (!skb)
  280. break;
  281. /*
  282. * Calculate length of frame including SPI padding.
  283. * The payload position is found in the control buffer.
  284. */
  285. info = (struct caif_payload_info *)&skb->cb;
  286. /*
  287. * Compute head offset i.e. number of bytes to add to
  288. * get the start of the payload aligned.
  289. */
  290. if (spi_up_head_align > 1) {
  291. spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
  292. *dst = (u8)(spad - 1);
  293. dst += spad;
  294. }
  295. /* Copy in CAIF frame. */
  296. skb_copy_bits(skb, 0, dst, skb->len);
  297. dst += skb->len;
  298. cfspi->ndev->stats.tx_packets++;
  299. cfspi->ndev->stats.tx_bytes += skb->len;
  300. /*
  301. * Compute tail offset i.e. number of bytes to add to
  302. * get the complete CAIF frame aligned.
  303. */
  304. epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
  305. dst += epad;
  306. dev_kfree_skb(skb);
  307. } while ((dst - buf) < len);
  308. return dst - buf;
  309. }
  310. int cfspi_xmitlen(struct cfspi *cfspi)
  311. {
  312. struct sk_buff *skb = NULL;
  313. int frm_len = 0;
  314. int pkts = 0;
  315. /*
  316. * Decommit previously committed frames.
  317. * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
  318. */
  319. while (skb_peek(&cfspi->chead)) {
  320. skb = skb_dequeue_tail(&cfspi->chead);
  321. skb_queue_head(&cfspi->qhead, skb);
  322. }
  323. do {
  324. struct caif_payload_info *info = NULL;
  325. int spad = 0;
  326. int epad = 0;
  327. skb = skb_dequeue(&cfspi->qhead);
  328. if (!skb)
  329. break;
  330. /*
  331. * Calculate length of frame including SPI padding.
  332. * The payload position is found in the control buffer.
  333. */
  334. info = (struct caif_payload_info *)&skb->cb;
  335. /*
  336. * Compute head offset i.e. number of bytes to add to
  337. * get the start of the payload aligned.
  338. */
  339. if (spi_up_head_align > 1)
  340. spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
  341. /*
  342. * Compute tail offset i.e. number of bytes to add to
  343. * get the complete CAIF frame aligned.
  344. */
  345. epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
  346. if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
  347. skb_queue_tail(&cfspi->chead, skb);
  348. pkts++;
  349. frm_len += skb->len + spad + epad;
  350. } else {
  351. /* Put back packet. */
  352. skb_queue_head(&cfspi->qhead, skb);
  353. break;
  354. }
  355. } while (pkts <= CAIF_MAX_SPI_PKTS);
  356. /*
  357. * Send flow on if previously sent flow off
  358. * and now go below the low water mark
  359. */
  360. if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
  361. cfspi->cfdev.flowctrl) {
  362. cfspi->flow_off_sent = 0;
  363. cfspi->cfdev.flowctrl(cfspi->ndev, 1);
  364. }
  365. return frm_len;
  366. }
  367. static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
  368. {
  369. struct cfspi *cfspi = (struct cfspi *)ifc->priv;
  370. /*
  371. * The slave device is the master on the link. Interrupts before the
  372. * slave has transmitted are considered spurious.
  373. */
  374. if (cfspi->slave && !cfspi->slave_talked) {
  375. printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n");
  376. return;
  377. }
  378. if (!in_interrupt())
  379. spin_lock(&cfspi->lock);
  380. if (assert) {
  381. set_bit(SPI_SS_ON, &cfspi->state);
  382. set_bit(SPI_XFER, &cfspi->state);
  383. } else {
  384. set_bit(SPI_SS_OFF, &cfspi->state);
  385. }
  386. if (!in_interrupt())
  387. spin_unlock(&cfspi->lock);
  388. /* Wake up the xfer thread. */
  389. if (assert)
  390. wake_up_interruptible(&cfspi->wait);
  391. }
  392. static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
  393. {
  394. struct cfspi *cfspi = (struct cfspi *)ifc->priv;
  395. /* Transfer done, complete work queue */
  396. complete(&cfspi->comp);
  397. }
  398. static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
  399. {
  400. struct cfspi *cfspi = NULL;
  401. unsigned long flags;
  402. if (!dev)
  403. return -EINVAL;
  404. cfspi = netdev_priv(dev);
  405. skb_queue_tail(&cfspi->qhead, skb);
  406. spin_lock_irqsave(&cfspi->lock, flags);
  407. if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
  408. /* Wake up xfer thread. */
  409. wake_up_interruptible(&cfspi->wait);
  410. }
  411. spin_unlock_irqrestore(&cfspi->lock, flags);
  412. /* Send flow off if number of bytes is above high water mark */
  413. if (!cfspi->flow_off_sent &&
  414. cfspi->qhead.qlen > cfspi->qd_high_mark &&
  415. cfspi->cfdev.flowctrl) {
  416. cfspi->flow_off_sent = 1;
  417. cfspi->cfdev.flowctrl(cfspi->ndev, 0);
  418. }
  419. return 0;
  420. }
  421. int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
  422. {
  423. u8 *src = buf;
  424. caif_assert(buf != NULL);
  425. do {
  426. int res;
  427. struct sk_buff *skb = NULL;
  428. int spad = 0;
  429. int epad = 0;
  430. u8 *dst = NULL;
  431. int pkt_len = 0;
  432. /*
  433. * Compute head offset i.e. number of bytes added to
  434. * get the start of the payload aligned.
  435. */
  436. if (spi_down_head_align > 1) {
  437. spad = 1 + *src;
  438. src += spad;
  439. }
  440. /* Read length of CAIF frame (little endian). */
  441. pkt_len = *src;
  442. pkt_len |= ((*(src+1)) << 8) & 0xFF00;
  443. pkt_len += 2; /* Add FCS fields. */
  444. /* Get a suitable caif packet and copy in data. */
  445. skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
  446. caif_assert(skb != NULL);
  447. dst = skb_put(skb, pkt_len);
  448. memcpy(dst, src, pkt_len);
  449. src += pkt_len;
  450. skb->protocol = htons(ETH_P_CAIF);
  451. skb_reset_mac_header(skb);
  452. /*
  453. * Push received packet up the stack.
  454. */
  455. if (!spi_loop)
  456. res = netif_rx_ni(skb);
  457. else
  458. res = cfspi_xmit(skb, cfspi->ndev);
  459. if (!res) {
  460. cfspi->ndev->stats.rx_packets++;
  461. cfspi->ndev->stats.rx_bytes += pkt_len;
  462. } else
  463. cfspi->ndev->stats.rx_dropped++;
  464. /*
  465. * Compute tail offset i.e. number of bytes added to
  466. * get the complete CAIF frame aligned.
  467. */
  468. epad = PAD_POW2((pkt_len + spad), spi_down_tail_align);
  469. src += epad;
  470. } while ((src - buf) < len);
  471. return src - buf;
  472. }
  473. static int cfspi_open(struct net_device *dev)
  474. {
  475. netif_wake_queue(dev);
  476. return 0;
  477. }
  478. static int cfspi_close(struct net_device *dev)
  479. {
  480. netif_stop_queue(dev);
  481. return 0;
  482. }
  483. static int cfspi_init(struct net_device *dev)
  484. {
  485. int res = 0;
  486. struct cfspi *cfspi = netdev_priv(dev);
  487. /* Set flow info. */
  488. cfspi->flow_off_sent = 0;
  489. cfspi->qd_low_mark = LOW_WATER_MARK;
  490. cfspi->qd_high_mark = HIGH_WATER_MARK;
  491. /* Set slave info. */
  492. if (!strncmp(cfspi_spi_driver.driver.name, "cfspi_sspi", 10)) {
  493. cfspi->slave = true;
  494. cfspi->slave_talked = false;
  495. } else {
  496. cfspi->slave = false;
  497. cfspi->slave_talked = false;
  498. }
  499. /* Allocate DMA buffers. */
  500. cfspi->xfer.va_tx[0] = dma_alloc(&cfspi->xfer.pa_tx[0]);
  501. if (!cfspi->xfer.va_tx[0]) {
  502. res = -ENODEV;
  503. goto err_dma_alloc_tx_0;
  504. }
  505. cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx);
  506. if (!cfspi->xfer.va_rx) {
  507. res = -ENODEV;
  508. goto err_dma_alloc_rx;
  509. }
  510. /* Initialize the work queue. */
  511. INIT_WORK(&cfspi->work, cfspi_xfer);
  512. /* Initialize spin locks. */
  513. spin_lock_init(&cfspi->lock);
  514. /* Initialize flow control state. */
  515. cfspi->flow_stop = false;
  516. /* Initialize wait queue. */
  517. init_waitqueue_head(&cfspi->wait);
  518. /* Create work thread. */
  519. cfspi->wq = create_singlethread_workqueue(dev->name);
  520. if (!cfspi->wq) {
  521. printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
  522. res = -ENODEV;
  523. goto err_create_wq;
  524. }
  525. /* Initialize work queue. */
  526. init_completion(&cfspi->comp);
  527. /* Create debugfs entries. */
  528. dev_debugfs_add(cfspi);
  529. /* Set up the ifc. */
  530. cfspi->ifc.ss_cb = cfspi_ss_cb;
  531. cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
  532. cfspi->ifc.priv = cfspi;
  533. /* Add CAIF SPI device to list. */
  534. spin_lock(&cfspi_list_lock);
  535. list_add_tail(&cfspi->list, &cfspi_list);
  536. spin_unlock(&cfspi_list_lock);
  537. /* Schedule the work queue. */
  538. queue_work(cfspi->wq, &cfspi->work);
  539. return 0;
  540. err_create_wq:
  541. dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
  542. err_dma_alloc_rx:
  543. dma_free(cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]);
  544. err_dma_alloc_tx_0:
  545. return res;
  546. }
  547. static void cfspi_uninit(struct net_device *dev)
  548. {
  549. struct cfspi *cfspi = netdev_priv(dev);
  550. /* Remove from list. */
  551. spin_lock(&cfspi_list_lock);
  552. list_del(&cfspi->list);
  553. spin_unlock(&cfspi_list_lock);
  554. cfspi->ndev = NULL;
  555. /* Free DMA buffers. */
  556. dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
  557. dma_free(cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]);
  558. set_bit(SPI_TERMINATE, &cfspi->state);
  559. wake_up_interruptible(&cfspi->wait);
  560. destroy_workqueue(cfspi->wq);
  561. /* Destroy debugfs directory and files. */
  562. dev_debugfs_rem(cfspi);
  563. return;
  564. }
  565. static const struct net_device_ops cfspi_ops = {
  566. .ndo_open = cfspi_open,
  567. .ndo_stop = cfspi_close,
  568. .ndo_init = cfspi_init,
  569. .ndo_uninit = cfspi_uninit,
  570. .ndo_start_xmit = cfspi_xmit
  571. };
  572. static void cfspi_setup(struct net_device *dev)
  573. {
  574. struct cfspi *cfspi = netdev_priv(dev);
  575. dev->features = 0;
  576. dev->netdev_ops = &cfspi_ops;
  577. dev->type = ARPHRD_CAIF;
  578. dev->flags = IFF_NOARP | IFF_POINTOPOINT;
  579. dev->priv_flags |= IFF_NO_QUEUE;
  580. dev->mtu = SPI_MAX_PAYLOAD_SIZE;
  581. dev->destructor = free_netdev;
  582. skb_queue_head_init(&cfspi->qhead);
  583. skb_queue_head_init(&cfspi->chead);
  584. cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
  585. cfspi->cfdev.use_frag = false;
  586. cfspi->cfdev.use_stx = false;
  587. cfspi->cfdev.use_fcs = false;
  588. cfspi->ndev = dev;
  589. }
  590. int cfspi_spi_probe(struct platform_device *pdev)
  591. {
  592. struct cfspi *cfspi = NULL;
  593. struct net_device *ndev;
  594. struct cfspi_dev *dev;
  595. int res;
  596. dev = (struct cfspi_dev *)pdev->dev.platform_data;
  597. if (!dev)
  598. return -ENODEV;
  599. ndev = alloc_netdev(sizeof(struct cfspi), "cfspi%d",
  600. NET_NAME_UNKNOWN, cfspi_setup);
  601. if (!ndev)
  602. return -ENOMEM;
  603. cfspi = netdev_priv(ndev);
  604. netif_stop_queue(ndev);
  605. cfspi->ndev = ndev;
  606. cfspi->pdev = pdev;
  607. /* Assign the SPI device. */
  608. cfspi->dev = dev;
  609. /* Assign the device ifc to this SPI interface. */
  610. dev->ifc = &cfspi->ifc;
  611. /* Register network device. */
  612. res = register_netdev(ndev);
  613. if (res) {
  614. printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
  615. goto err_net_reg;
  616. }
  617. return res;
  618. err_net_reg:
  619. free_netdev(ndev);
  620. return res;
  621. }
  622. int cfspi_spi_remove(struct platform_device *pdev)
  623. {
  624. /* Everything is done in cfspi_uninit(). */
  625. return 0;
  626. }
  627. static void __exit cfspi_exit_module(void)
  628. {
  629. struct list_head *list_node;
  630. struct list_head *n;
  631. struct cfspi *cfspi = NULL;
  632. list_for_each_safe(list_node, n, &cfspi_list) {
  633. cfspi = list_entry(list_node, struct cfspi, list);
  634. unregister_netdev(cfspi->ndev);
  635. }
  636. /* Destroy sysfs files. */
  637. driver_remove_file(&cfspi_spi_driver.driver,
  638. &driver_attr_up_head_align);
  639. driver_remove_file(&cfspi_spi_driver.driver,
  640. &driver_attr_up_tail_align);
  641. driver_remove_file(&cfspi_spi_driver.driver,
  642. &driver_attr_down_head_align);
  643. driver_remove_file(&cfspi_spi_driver.driver,
  644. &driver_attr_down_tail_align);
  645. driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
  646. /* Unregister platform driver. */
  647. platform_driver_unregister(&cfspi_spi_driver);
  648. /* Destroy debugfs root directory. */
  649. driver_debugfs_remove();
  650. }
  651. static int __init cfspi_init_module(void)
  652. {
  653. int result;
  654. /* Initialize spin lock. */
  655. spin_lock_init(&cfspi_list_lock);
  656. /* Register platform driver. */
  657. result = platform_driver_register(&cfspi_spi_driver);
  658. if (result) {
  659. printk(KERN_ERR "Could not register platform SPI driver.\n");
  660. goto err_dev_register;
  661. }
  662. /* Create sysfs files. */
  663. result =
  664. driver_create_file(&cfspi_spi_driver.driver,
  665. &driver_attr_up_head_align);
  666. if (result) {
  667. printk(KERN_ERR "Sysfs creation failed 1.\n");
  668. goto err_create_up_head_align;
  669. }
  670. result =
  671. driver_create_file(&cfspi_spi_driver.driver,
  672. &driver_attr_up_tail_align);
  673. if (result) {
  674. printk(KERN_ERR "Sysfs creation failed 2.\n");
  675. goto err_create_up_tail_align;
  676. }
  677. result =
  678. driver_create_file(&cfspi_spi_driver.driver,
  679. &driver_attr_down_head_align);
  680. if (result) {
  681. printk(KERN_ERR "Sysfs creation failed 3.\n");
  682. goto err_create_down_head_align;
  683. }
  684. result =
  685. driver_create_file(&cfspi_spi_driver.driver,
  686. &driver_attr_down_tail_align);
  687. if (result) {
  688. printk(KERN_ERR "Sysfs creation failed 4.\n");
  689. goto err_create_down_tail_align;
  690. }
  691. result =
  692. driver_create_file(&cfspi_spi_driver.driver,
  693. &driver_attr_frame_align);
  694. if (result) {
  695. printk(KERN_ERR "Sysfs creation failed 5.\n");
  696. goto err_create_frame_align;
  697. }
  698. driver_debugfs_create();
  699. return result;
  700. err_create_frame_align:
  701. driver_remove_file(&cfspi_spi_driver.driver,
  702. &driver_attr_down_tail_align);
  703. err_create_down_tail_align:
  704. driver_remove_file(&cfspi_spi_driver.driver,
  705. &driver_attr_down_head_align);
  706. err_create_down_head_align:
  707. driver_remove_file(&cfspi_spi_driver.driver,
  708. &driver_attr_up_tail_align);
  709. err_create_up_tail_align:
  710. driver_remove_file(&cfspi_spi_driver.driver,
  711. &driver_attr_up_head_align);
  712. err_create_up_head_align:
  713. platform_driver_unregister(&cfspi_spi_driver);
  714. err_dev_register:
  715. return result;
  716. }
  717. module_init(cfspi_init_module);
  718. module_exit(cfspi_exit_module);