caif_spi.c 21 KB

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