caif_spi.c 21 KB

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