caif_spi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. 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,
  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 = dbgfs_open,
  196. .read = dbgfs_state,
  197. .owner = THIS_MODULE
  198. };
  199. static const struct file_operations dbgfs_frame_fops = {
  200. .open = dbgfs_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. do {
  272. struct sk_buff *skb;
  273. struct caif_payload_info *info;
  274. int spad = 0;
  275. int epad;
  276. skb = skb_dequeue(&cfspi->chead);
  277. if (!skb)
  278. break;
  279. /*
  280. * Calculate length of frame including SPI padding.
  281. * The payload position is found in the control buffer.
  282. */
  283. info = (struct caif_payload_info *)&skb->cb;
  284. /*
  285. * Compute head offset i.e. number of bytes to add to
  286. * get the start of the payload aligned.
  287. */
  288. if (spi_up_head_align) {
  289. spad = 1 + ((info->hdr_len + 1) & spi_up_head_align);
  290. *dst = (u8)(spad - 1);
  291. dst += spad;
  292. }
  293. /* Copy in CAIF frame. */
  294. skb_copy_bits(skb, 0, dst, skb->len);
  295. dst += skb->len;
  296. cfspi->ndev->stats.tx_packets++;
  297. cfspi->ndev->stats.tx_bytes += skb->len;
  298. /*
  299. * Compute tail offset i.e. number of bytes to add to
  300. * get the complete CAIF frame aligned.
  301. */
  302. epad = (skb->len + spad) & spi_up_tail_align;
  303. dst += epad;
  304. dev_kfree_skb(skb);
  305. } while ((dst - buf) < len);
  306. return dst - buf;
  307. }
  308. int cfspi_xmitlen(struct cfspi *cfspi)
  309. {
  310. struct sk_buff *skb = NULL;
  311. int frm_len = 0;
  312. int pkts = 0;
  313. /*
  314. * Decommit previously commited frames.
  315. * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
  316. */
  317. while (skb_peek(&cfspi->chead)) {
  318. skb = skb_dequeue_tail(&cfspi->chead);
  319. skb_queue_head(&cfspi->qhead, skb);
  320. }
  321. do {
  322. struct caif_payload_info *info = NULL;
  323. int spad = 0;
  324. int epad = 0;
  325. skb = skb_dequeue(&cfspi->qhead);
  326. if (!skb)
  327. break;
  328. /*
  329. * Calculate length of frame including SPI padding.
  330. * The payload position is found in the control buffer.
  331. */
  332. info = (struct caif_payload_info *)&skb->cb;
  333. /*
  334. * Compute head offset i.e. number of bytes to add to
  335. * get the start of the payload aligned.
  336. */
  337. if (spi_up_head_align)
  338. spad = 1 + ((info->hdr_len + 1) & spi_up_head_align);
  339. /*
  340. * Compute tail offset i.e. number of bytes to add to
  341. * get the complete CAIF frame aligned.
  342. */
  343. epad = (skb->len + spad) & spi_up_tail_align;
  344. if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
  345. skb_queue_tail(&cfspi->chead, skb);
  346. pkts++;
  347. frm_len += skb->len + spad + epad;
  348. } else {
  349. /* Put back packet. */
  350. skb_queue_head(&cfspi->qhead, skb);
  351. }
  352. } while (pkts <= CAIF_MAX_SPI_PKTS);
  353. /*
  354. * Send flow on if previously sent flow off
  355. * and now go below the low water mark
  356. */
  357. if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
  358. cfspi->cfdev.flowctrl) {
  359. cfspi->flow_off_sent = 0;
  360. cfspi->cfdev.flowctrl(cfspi->ndev, 1);
  361. }
  362. return frm_len;
  363. }
  364. static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
  365. {
  366. struct cfspi *cfspi = (struct cfspi *)ifc->priv;
  367. if (!in_interrupt())
  368. spin_lock(&cfspi->lock);
  369. if (assert) {
  370. set_bit(SPI_SS_ON, &cfspi->state);
  371. set_bit(SPI_XFER, &cfspi->state);
  372. } else {
  373. set_bit(SPI_SS_OFF, &cfspi->state);
  374. }
  375. if (!in_interrupt())
  376. spin_unlock(&cfspi->lock);
  377. /* Wake up the xfer thread. */
  378. wake_up_interruptible(&cfspi->wait);
  379. }
  380. static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
  381. {
  382. struct cfspi *cfspi = (struct cfspi *)ifc->priv;
  383. /* Transfer done, complete work queue */
  384. complete(&cfspi->comp);
  385. }
  386. static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
  387. {
  388. struct cfspi *cfspi = NULL;
  389. unsigned long flags;
  390. if (!dev)
  391. return -EINVAL;
  392. cfspi = netdev_priv(dev);
  393. skb_queue_tail(&cfspi->qhead, skb);
  394. spin_lock_irqsave(&cfspi->lock, flags);
  395. if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
  396. /* Wake up xfer thread. */
  397. wake_up_interruptible(&cfspi->wait);
  398. }
  399. spin_unlock_irqrestore(&cfspi->lock, flags);
  400. /* Send flow off if number of bytes is above high water mark */
  401. if (!cfspi->flow_off_sent &&
  402. cfspi->qhead.qlen > cfspi->qd_high_mark &&
  403. cfspi->cfdev.flowctrl) {
  404. cfspi->flow_off_sent = 1;
  405. cfspi->cfdev.flowctrl(cfspi->ndev, 0);
  406. }
  407. return 0;
  408. }
  409. int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
  410. {
  411. u8 *src = buf;
  412. caif_assert(buf != NULL);
  413. do {
  414. int res;
  415. struct sk_buff *skb = NULL;
  416. int spad = 0;
  417. int epad = 0;
  418. u8 *dst = NULL;
  419. int pkt_len = 0;
  420. /*
  421. * Compute head offset i.e. number of bytes added to
  422. * get the start of the payload aligned.
  423. */
  424. if (spi_down_head_align) {
  425. spad = 1 + *src;
  426. src += spad;
  427. }
  428. /* Read length of CAIF frame (little endian). */
  429. pkt_len = *src;
  430. pkt_len |= ((*(src+1)) << 8) & 0xFF00;
  431. pkt_len += 2; /* Add FCS fields. */
  432. /* Get a suitable caif packet and copy in data. */
  433. skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
  434. caif_assert(skb != NULL);
  435. dst = skb_put(skb, pkt_len);
  436. memcpy(dst, src, pkt_len);
  437. src += pkt_len;
  438. skb->protocol = htons(ETH_P_CAIF);
  439. skb_reset_mac_header(skb);
  440. skb->dev = cfspi->ndev;
  441. /*
  442. * Push received packet up the stack.
  443. */
  444. if (!spi_loop)
  445. res = netif_rx_ni(skb);
  446. else
  447. res = cfspi_xmit(skb, cfspi->ndev);
  448. if (!res) {
  449. cfspi->ndev->stats.rx_packets++;
  450. cfspi->ndev->stats.rx_bytes += pkt_len;
  451. } else
  452. cfspi->ndev->stats.rx_dropped++;
  453. /*
  454. * Compute tail offset i.e. number of bytes added to
  455. * get the complete CAIF frame aligned.
  456. */
  457. epad = (pkt_len + spad) & spi_down_tail_align;
  458. src += epad;
  459. } while ((src - buf) < len);
  460. return src - buf;
  461. }
  462. static int cfspi_open(struct net_device *dev)
  463. {
  464. netif_wake_queue(dev);
  465. return 0;
  466. }
  467. static int cfspi_close(struct net_device *dev)
  468. {
  469. netif_stop_queue(dev);
  470. return 0;
  471. }
  472. static const struct net_device_ops cfspi_ops = {
  473. .ndo_open = cfspi_open,
  474. .ndo_stop = cfspi_close,
  475. .ndo_start_xmit = cfspi_xmit
  476. };
  477. static void cfspi_setup(struct net_device *dev)
  478. {
  479. struct cfspi *cfspi = netdev_priv(dev);
  480. dev->features = 0;
  481. dev->netdev_ops = &cfspi_ops;
  482. dev->type = ARPHRD_CAIF;
  483. dev->flags = IFF_NOARP | IFF_POINTOPOINT;
  484. dev->tx_queue_len = 0;
  485. dev->mtu = SPI_MAX_PAYLOAD_SIZE;
  486. dev->destructor = free_netdev;
  487. skb_queue_head_init(&cfspi->qhead);
  488. skb_queue_head_init(&cfspi->chead);
  489. cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
  490. cfspi->cfdev.use_frag = false;
  491. cfspi->cfdev.use_stx = false;
  492. cfspi->cfdev.use_fcs = false;
  493. cfspi->ndev = dev;
  494. }
  495. int cfspi_spi_probe(struct platform_device *pdev)
  496. {
  497. struct cfspi *cfspi = NULL;
  498. struct net_device *ndev;
  499. struct cfspi_dev *dev;
  500. int res;
  501. dev = (struct cfspi_dev *)pdev->dev.platform_data;
  502. ndev = alloc_netdev(sizeof(struct cfspi),
  503. "cfspi%d", cfspi_setup);
  504. if (!dev)
  505. return -ENODEV;
  506. cfspi = netdev_priv(ndev);
  507. netif_stop_queue(ndev);
  508. cfspi->ndev = ndev;
  509. cfspi->pdev = pdev;
  510. /* Set flow info */
  511. cfspi->flow_off_sent = 0;
  512. cfspi->qd_low_mark = LOW_WATER_MARK;
  513. cfspi->qd_high_mark = HIGH_WATER_MARK;
  514. /* Assign the SPI device. */
  515. cfspi->dev = dev;
  516. /* Assign the device ifc to this SPI interface. */
  517. dev->ifc = &cfspi->ifc;
  518. /* Allocate DMA buffers. */
  519. cfspi->xfer.va_tx = dma_alloc(&cfspi->xfer.pa_tx);
  520. if (!cfspi->xfer.va_tx) {
  521. printk(KERN_WARNING
  522. "CFSPI: failed to allocate dma TX buffer.\n");
  523. res = -ENODEV;
  524. goto err_dma_alloc_tx;
  525. }
  526. cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx);
  527. if (!cfspi->xfer.va_rx) {
  528. printk(KERN_WARNING
  529. "CFSPI: failed to allocate dma TX buffer.\n");
  530. res = -ENODEV;
  531. goto err_dma_alloc_rx;
  532. }
  533. /* Initialize the work queue. */
  534. INIT_WORK(&cfspi->work, cfspi_xfer);
  535. /* Initialize spin locks. */
  536. spin_lock_init(&cfspi->lock);
  537. /* Initialize flow control state. */
  538. cfspi->flow_stop = false;
  539. /* Initialize wait queue. */
  540. init_waitqueue_head(&cfspi->wait);
  541. /* Create work thread. */
  542. cfspi->wq = create_singlethread_workqueue(dev->name);
  543. if (!cfspi->wq) {
  544. printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
  545. res = -ENODEV;
  546. goto err_create_wq;
  547. }
  548. /* Initialize work queue. */
  549. init_completion(&cfspi->comp);
  550. /* Create debugfs entries. */
  551. dev_debugfs_add(cfspi);
  552. /* Set up the ifc. */
  553. cfspi->ifc.ss_cb = cfspi_ss_cb;
  554. cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
  555. cfspi->ifc.priv = cfspi;
  556. /* Add CAIF SPI device to list. */
  557. spin_lock(&cfspi_list_lock);
  558. list_add_tail(&cfspi->list, &cfspi_list);
  559. spin_unlock(&cfspi_list_lock);
  560. /* Schedule the work queue. */
  561. queue_work(cfspi->wq, &cfspi->work);
  562. /* Register network device. */
  563. res = register_netdev(ndev);
  564. if (res) {
  565. printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
  566. goto err_net_reg;
  567. }
  568. return res;
  569. err_net_reg:
  570. dev_debugfs_rem(cfspi);
  571. set_bit(SPI_TERMINATE, &cfspi->state);
  572. wake_up_interruptible(&cfspi->wait);
  573. destroy_workqueue(cfspi->wq);
  574. err_create_wq:
  575. dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
  576. err_dma_alloc_rx:
  577. dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
  578. err_dma_alloc_tx:
  579. free_netdev(ndev);
  580. return res;
  581. }
  582. int cfspi_spi_remove(struct platform_device *pdev)
  583. {
  584. struct list_head *list_node;
  585. struct list_head *n;
  586. struct cfspi *cfspi = NULL;
  587. struct cfspi_dev *dev;
  588. dev = (struct cfspi_dev *)pdev->dev.platform_data;
  589. spin_lock(&cfspi_list_lock);
  590. list_for_each_safe(list_node, n, &cfspi_list) {
  591. cfspi = list_entry(list_node, struct cfspi, list);
  592. /* Find the corresponding device. */
  593. if (cfspi->dev == dev) {
  594. /* Remove from list. */
  595. list_del(list_node);
  596. /* Free DMA buffers. */
  597. dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
  598. dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
  599. set_bit(SPI_TERMINATE, &cfspi->state);
  600. wake_up_interruptible(&cfspi->wait);
  601. destroy_workqueue(cfspi->wq);
  602. /* Destroy debugfs directory and files. */
  603. dev_debugfs_rem(cfspi);
  604. unregister_netdev(cfspi->ndev);
  605. spin_unlock(&cfspi_list_lock);
  606. return 0;
  607. }
  608. }
  609. spin_unlock(&cfspi_list_lock);
  610. return -ENODEV;
  611. }
  612. static void __exit cfspi_exit_module(void)
  613. {
  614. struct list_head *list_node;
  615. struct list_head *n;
  616. struct cfspi *cfspi = NULL;
  617. list_for_each_safe(list_node, n, &cfspi_list) {
  618. cfspi = list_entry(list_node, struct cfspi, list);
  619. platform_device_unregister(cfspi->pdev);
  620. }
  621. /* Destroy sysfs files. */
  622. driver_remove_file(&cfspi_spi_driver.driver,
  623. &driver_attr_up_head_align);
  624. driver_remove_file(&cfspi_spi_driver.driver,
  625. &driver_attr_up_tail_align);
  626. driver_remove_file(&cfspi_spi_driver.driver,
  627. &driver_attr_down_head_align);
  628. driver_remove_file(&cfspi_spi_driver.driver,
  629. &driver_attr_down_tail_align);
  630. driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
  631. /* Unregister platform driver. */
  632. platform_driver_unregister(&cfspi_spi_driver);
  633. /* Destroy debugfs root directory. */
  634. driver_debugfs_remove();
  635. }
  636. static int __init cfspi_init_module(void)
  637. {
  638. int result;
  639. /* Initialize spin lock. */
  640. spin_lock_init(&cfspi_list_lock);
  641. /* Register platform driver. */
  642. result = platform_driver_register(&cfspi_spi_driver);
  643. if (result) {
  644. printk(KERN_ERR "Could not register platform SPI driver.\n");
  645. goto err_dev_register;
  646. }
  647. /* Create sysfs files. */
  648. result =
  649. driver_create_file(&cfspi_spi_driver.driver,
  650. &driver_attr_up_head_align);
  651. if (result) {
  652. printk(KERN_ERR "Sysfs creation failed 1.\n");
  653. goto err_create_up_head_align;
  654. }
  655. result =
  656. driver_create_file(&cfspi_spi_driver.driver,
  657. &driver_attr_up_tail_align);
  658. if (result) {
  659. printk(KERN_ERR "Sysfs creation failed 2.\n");
  660. goto err_create_up_tail_align;
  661. }
  662. result =
  663. driver_create_file(&cfspi_spi_driver.driver,
  664. &driver_attr_down_head_align);
  665. if (result) {
  666. printk(KERN_ERR "Sysfs creation failed 3.\n");
  667. goto err_create_down_head_align;
  668. }
  669. result =
  670. driver_create_file(&cfspi_spi_driver.driver,
  671. &driver_attr_down_tail_align);
  672. if (result) {
  673. printk(KERN_ERR "Sysfs creation failed 4.\n");
  674. goto err_create_down_tail_align;
  675. }
  676. result =
  677. driver_create_file(&cfspi_spi_driver.driver,
  678. &driver_attr_frame_align);
  679. if (result) {
  680. printk(KERN_ERR "Sysfs creation failed 5.\n");
  681. goto err_create_frame_align;
  682. }
  683. driver_debugfs_create();
  684. return result;
  685. err_create_frame_align:
  686. driver_remove_file(&cfspi_spi_driver.driver,
  687. &driver_attr_down_tail_align);
  688. err_create_down_tail_align:
  689. driver_remove_file(&cfspi_spi_driver.driver,
  690. &driver_attr_down_head_align);
  691. err_create_down_head_align:
  692. driver_remove_file(&cfspi_spi_driver.driver,
  693. &driver_attr_up_tail_align);
  694. err_create_up_tail_align:
  695. driver_remove_file(&cfspi_spi_driver.driver,
  696. &driver_attr_up_head_align);
  697. err_create_up_head_align:
  698. err_dev_register:
  699. return result;
  700. }
  701. module_init(cfspi_init_module);
  702. module_exit(cfspi_exit_module);