caif_spi.c 21 KB

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