ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / net / caif / caif_spi.c
blob57e639373815e59cb00b36aaee7f1e5ea71a835c
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 */
8 #include <linux/version.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/platform_device.h>
13 #include <linux/string.h>
14 #include <linux/workqueue.h>
15 #include <linux/completion.h>
16 #include <linux/list.h>
17 #include <linux/interrupt.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/delay.h>
20 #include <linux/sched.h>
21 #include <linux/debugfs.h>
22 #include <linux/if_arp.h>
23 #include <net/caif/caif_layer.h>
24 #include <net/caif/caif_spi.h>
26 #ifndef CONFIG_CAIF_SPI_SYNC
27 #define FLAVOR "Flavour: Vanilla.\n"
28 #else
29 #define FLAVOR "Flavour: Master CMD&LEN at start.\n"
30 #endif /* CONFIG_CAIF_SPI_SYNC */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
34 MODULE_DESCRIPTION("CAIF SPI driver");
36 /* Returns the number of padding bytes for alignment. */
37 #define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1)))))
39 static int spi_loop;
40 module_param(spi_loop, bool, S_IRUGO);
41 MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode.");
43 /* SPI frame alignment. */
44 module_param(spi_frm_align, int, S_IRUGO);
45 MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment.");
48 * SPI padding options.
49 * Warning: must be a base of 2 (& operation used) and can not be zero !
51 module_param(spi_up_head_align, int, S_IRUGO);
52 MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment.");
54 module_param(spi_up_tail_align, int, S_IRUGO);
55 MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment.");
57 module_param(spi_down_head_align, int, S_IRUGO);
58 MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment.");
60 module_param(spi_down_tail_align, int, S_IRUGO);
61 MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment.");
63 #ifdef CONFIG_ARM
64 #define BYTE_HEX_FMT "%02X"
65 #else
66 #define BYTE_HEX_FMT "%02hhX"
67 #endif
69 #define SPI_MAX_PAYLOAD_SIZE 4096
71 * Threshold values for the SPI packet queue. Flowcontrol will be asserted
72 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
73 * deasserted before the number of packets drops below LOW_WATER_MARK.
75 #define LOW_WATER_MARK 100
76 #define HIGH_WATER_MARK (LOW_WATER_MARK*5)
78 #ifdef CONFIG_UML
81 * We sometimes use UML for debugging, but it cannot handle
82 * dma_alloc_coherent so we have to wrap it.
84 static inline void *dma_alloc(dma_addr_t *daddr)
86 return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL);
89 static inline void dma_free(void *cpu_addr, dma_addr_t handle)
91 kfree(cpu_addr);
94 #else
96 static inline void *dma_alloc(dma_addr_t *daddr)
98 return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr,
99 GFP_KERNEL);
102 static inline void dma_free(void *cpu_addr, dma_addr_t handle)
104 dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle);
106 #endif /* CONFIG_UML */
108 #ifdef CONFIG_DEBUG_FS
110 #define DEBUGFS_BUF_SIZE 4096
112 static struct dentry *dbgfs_root;
114 static inline void driver_debugfs_create(void)
116 dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL);
119 static inline void driver_debugfs_remove(void)
121 debugfs_remove(dbgfs_root);
124 static inline void dev_debugfs_rem(struct cfspi *cfspi)
126 debugfs_remove(cfspi->dbgfs_frame);
127 debugfs_remove(cfspi->dbgfs_state);
128 debugfs_remove(cfspi->dbgfs_dir);
131 static int dbgfs_open(struct inode *inode, struct file *file)
133 file->private_data = inode->i_private;
134 return 0;
137 static ssize_t dbgfs_state(struct file *file, char __user *user_buf,
138 size_t count, loff_t *ppos)
140 char *buf;
141 int len = 0;
142 ssize_t size;
143 struct cfspi *cfspi = file->private_data;
145 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
146 if (!buf)
147 return 0;
149 /* Print out debug information. */
150 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
151 "CAIF SPI debug information:\n");
153 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR);
155 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
156 "STATE: %d\n", cfspi->dbg_state);
157 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
158 "Previous CMD: 0x%x\n", cfspi->pcmd);
159 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
160 "Current CMD: 0x%x\n", cfspi->cmd);
161 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
162 "Previous TX len: %d\n", cfspi->tx_ppck_len);
163 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
164 "Previous RX len: %d\n", cfspi->rx_ppck_len);
165 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
166 "Current TX len: %d\n", cfspi->tx_cpck_len);
167 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
168 "Current RX len: %d\n", cfspi->rx_cpck_len);
169 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
170 "Next TX len: %d\n", cfspi->tx_npck_len);
171 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
172 "Next RX len: %d\n", cfspi->rx_npck_len);
174 if (len > DEBUGFS_BUF_SIZE)
175 len = DEBUGFS_BUF_SIZE;
177 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
178 kfree(buf);
180 return size;
183 static ssize_t print_frame(char *buf, size_t size, char *frm,
184 size_t count, size_t cut)
186 int len = 0;
187 int i;
188 for (i = 0; i < count; i++) {
189 len += snprintf((buf + len), (size - len),
190 "[0x" BYTE_HEX_FMT "]",
191 frm[i]);
192 if ((i == cut) && (count > (cut * 2))) {
193 /* Fast forward. */
194 i = count - cut;
195 len += snprintf((buf + len), (size - len),
196 "--- %u bytes skipped ---\n",
197 (int)(count - (cut * 2)));
200 if ((!(i % 10)) && i) {
201 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
202 "\n");
205 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n");
206 return len;
209 static ssize_t dbgfs_frame(struct file *file, char __user *user_buf,
210 size_t count, loff_t *ppos)
212 char *buf;
213 int len = 0;
214 ssize_t size;
215 struct cfspi *cfspi;
217 cfspi = file->private_data;
218 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
219 if (!buf)
220 return 0;
222 /* Print out debug information. */
223 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
224 "Current frame:\n");
226 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
227 "Tx data (Len: %d):\n", cfspi->tx_cpck_len);
229 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
230 cfspi->xfer.va_tx,
231 (cfspi->tx_cpck_len + SPI_CMD_SZ), 100);
233 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
234 "Rx data (Len: %d):\n", cfspi->rx_cpck_len);
236 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
237 cfspi->xfer.va_rx,
238 (cfspi->rx_cpck_len + SPI_CMD_SZ), 100);
240 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
241 kfree(buf);
243 return size;
246 static const struct file_operations dbgfs_state_fops = {
247 .open = dbgfs_open,
248 .read = dbgfs_state,
249 .owner = THIS_MODULE
252 static const struct file_operations dbgfs_frame_fops = {
253 .open = dbgfs_open,
254 .read = dbgfs_frame,
255 .owner = THIS_MODULE
258 static inline void dev_debugfs_add(struct cfspi *cfspi)
260 cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root);
261 cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO,
262 cfspi->dbgfs_dir, cfspi,
263 &dbgfs_state_fops);
264 cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO,
265 cfspi->dbgfs_dir, cfspi,
266 &dbgfs_frame_fops);
269 inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
271 cfspi->dbg_state = state;
273 #else
275 static inline void driver_debugfs_create(void)
279 static inline void driver_debugfs_remove(void)
283 static inline void dev_debugfs_add(struct cfspi *cfspi)
287 static inline void dev_debugfs_rem(struct cfspi *cfspi)
291 inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
294 #endif /* CONFIG_DEBUG_FS */
296 static LIST_HEAD(cfspi_list);
297 static spinlock_t cfspi_list_lock;
299 /* SPI uplink head alignment. */
300 static ssize_t show_up_head_align(struct device_driver *driver, char *buf)
302 return sprintf(buf, "%d\n", spi_up_head_align);
305 static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL);
307 /* SPI uplink tail alignment. */
308 static ssize_t show_up_tail_align(struct device_driver *driver, char *buf)
310 return sprintf(buf, "%d\n", spi_up_tail_align);
313 static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL);
315 /* SPI downlink head alignment. */
316 static ssize_t show_down_head_align(struct device_driver *driver, char *buf)
318 return sprintf(buf, "%d\n", spi_down_head_align);
321 static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL);
323 /* SPI downlink tail alignment. */
324 static ssize_t show_down_tail_align(struct device_driver *driver, char *buf)
326 return sprintf(buf, "%d\n", spi_down_tail_align);
329 static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL);
331 /* SPI frame alignment. */
332 static ssize_t show_frame_align(struct device_driver *driver, char *buf)
334 return sprintf(buf, "%d\n", spi_frm_align);
337 static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL);
339 int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len)
341 u8 *dst = buf;
342 caif_assert(buf);
344 if (cfspi->slave && !cfspi->slave_talked)
345 cfspi->slave_talked = true;
347 do {
348 struct sk_buff *skb;
349 struct caif_payload_info *info;
350 int spad = 0;
351 int epad;
353 skb = skb_dequeue(&cfspi->chead);
354 if (!skb)
355 break;
358 * Calculate length of frame including SPI padding.
359 * The payload position is found in the control buffer.
361 info = (struct caif_payload_info *)&skb->cb;
364 * Compute head offset i.e. number of bytes to add to
365 * get the start of the payload aligned.
367 if (spi_up_head_align > 1) {
368 spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
369 *dst = (u8)(spad - 1);
370 dst += spad;
373 /* Copy in CAIF frame. */
374 skb_copy_bits(skb, 0, dst, skb->len);
375 dst += skb->len;
376 cfspi->ndev->stats.tx_packets++;
377 cfspi->ndev->stats.tx_bytes += skb->len;
380 * Compute tail offset i.e. number of bytes to add to
381 * get the complete CAIF frame aligned.
383 epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
384 dst += epad;
386 dev_kfree_skb(skb);
388 } while ((dst - buf) < len);
390 return dst - buf;
393 int cfspi_xmitlen(struct cfspi *cfspi)
395 struct sk_buff *skb = NULL;
396 int frm_len = 0;
397 int pkts = 0;
400 * Decommit previously committed frames.
401 * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
403 while (skb_peek(&cfspi->chead)) {
404 skb = skb_dequeue_tail(&cfspi->chead);
405 skb_queue_head(&cfspi->qhead, skb);
408 do {
409 struct caif_payload_info *info = NULL;
410 int spad = 0;
411 int epad = 0;
413 skb = skb_dequeue(&cfspi->qhead);
414 if (!skb)
415 break;
418 * Calculate length of frame including SPI padding.
419 * The payload position is found in the control buffer.
421 info = (struct caif_payload_info *)&skb->cb;
424 * Compute head offset i.e. number of bytes to add to
425 * get the start of the payload aligned.
427 if (spi_up_head_align > 1)
428 spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
431 * Compute tail offset i.e. number of bytes to add to
432 * get the complete CAIF frame aligned.
434 epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
436 if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
437 skb_queue_tail(&cfspi->chead, skb);
438 pkts++;
439 frm_len += skb->len + spad + epad;
440 } else {
441 /* Put back packet. */
442 skb_queue_head(&cfspi->qhead, skb);
443 break;
445 } while (pkts <= CAIF_MAX_SPI_PKTS);
448 * Send flow on if previously sent flow off
449 * and now go below the low water mark
451 if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
452 cfspi->cfdev.flowctrl) {
453 cfspi->flow_off_sent = 0;
454 cfspi->cfdev.flowctrl(cfspi->ndev, 1);
457 return frm_len;
460 static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
462 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
465 * The slave device is the master on the link. Interrupts before the
466 * slave has transmitted are considered spurious.
468 if (cfspi->slave && !cfspi->slave_talked) {
469 printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n");
470 return;
473 if (!in_interrupt())
474 spin_lock(&cfspi->lock);
475 if (assert) {
476 set_bit(SPI_SS_ON, &cfspi->state);
477 set_bit(SPI_XFER, &cfspi->state);
478 } else {
479 set_bit(SPI_SS_OFF, &cfspi->state);
481 if (!in_interrupt())
482 spin_unlock(&cfspi->lock);
484 /* Wake up the xfer thread. */
485 if (assert)
486 wake_up_interruptible(&cfspi->wait);
489 static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
491 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
493 /* Transfer done, complete work queue */
494 complete(&cfspi->comp);
497 static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
499 struct cfspi *cfspi = NULL;
500 unsigned long flags;
501 if (!dev)
502 return -EINVAL;
504 cfspi = netdev_priv(dev);
506 skb_queue_tail(&cfspi->qhead, skb);
508 spin_lock_irqsave(&cfspi->lock, flags);
509 if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
510 /* Wake up xfer thread. */
511 wake_up_interruptible(&cfspi->wait);
513 spin_unlock_irqrestore(&cfspi->lock, flags);
515 /* Send flow off if number of bytes is above high water mark */
516 if (!cfspi->flow_off_sent &&
517 cfspi->qhead.qlen > cfspi->qd_high_mark &&
518 cfspi->cfdev.flowctrl) {
519 cfspi->flow_off_sent = 1;
520 cfspi->cfdev.flowctrl(cfspi->ndev, 0);
523 return 0;
526 int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
528 u8 *src = buf;
530 caif_assert(buf != NULL);
532 do {
533 int res;
534 struct sk_buff *skb = NULL;
535 int spad = 0;
536 int epad = 0;
537 u8 *dst = NULL;
538 int pkt_len = 0;
541 * Compute head offset i.e. number of bytes added to
542 * get the start of the payload aligned.
544 if (spi_down_head_align > 1) {
545 spad = 1 + *src;
546 src += spad;
549 /* Read length of CAIF frame (little endian). */
550 pkt_len = *src;
551 pkt_len |= ((*(src+1)) << 8) & 0xFF00;
552 pkt_len += 2; /* Add FCS fields. */
554 /* Get a suitable caif packet and copy in data. */
556 skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
557 caif_assert(skb != NULL);
559 dst = skb_put(skb, pkt_len);
560 memcpy(dst, src, pkt_len);
561 src += pkt_len;
563 skb->protocol = htons(ETH_P_CAIF);
564 skb_reset_mac_header(skb);
565 skb->dev = cfspi->ndev;
568 * Push received packet up the stack.
570 if (!spi_loop)
571 res = netif_rx_ni(skb);
572 else
573 res = cfspi_xmit(skb, cfspi->ndev);
575 if (!res) {
576 cfspi->ndev->stats.rx_packets++;
577 cfspi->ndev->stats.rx_bytes += pkt_len;
578 } else
579 cfspi->ndev->stats.rx_dropped++;
582 * Compute tail offset i.e. number of bytes added to
583 * get the complete CAIF frame aligned.
585 epad = PAD_POW2((pkt_len + spad), spi_down_tail_align);
586 src += epad;
587 } while ((src - buf) < len);
589 return src - buf;
592 static int cfspi_open(struct net_device *dev)
594 netif_wake_queue(dev);
595 return 0;
598 static int cfspi_close(struct net_device *dev)
600 netif_stop_queue(dev);
601 return 0;
603 static const struct net_device_ops cfspi_ops = {
604 .ndo_open = cfspi_open,
605 .ndo_stop = cfspi_close,
606 .ndo_start_xmit = cfspi_xmit
609 static void cfspi_setup(struct net_device *dev)
611 struct cfspi *cfspi = netdev_priv(dev);
612 dev->features = 0;
613 dev->netdev_ops = &cfspi_ops;
614 dev->type = ARPHRD_CAIF;
615 dev->flags = IFF_NOARP | IFF_POINTOPOINT;
616 dev->tx_queue_len = 0;
617 dev->mtu = SPI_MAX_PAYLOAD_SIZE;
618 dev->destructor = free_netdev;
619 skb_queue_head_init(&cfspi->qhead);
620 skb_queue_head_init(&cfspi->chead);
621 cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
622 cfspi->cfdev.use_frag = false;
623 cfspi->cfdev.use_stx = false;
624 cfspi->cfdev.use_fcs = false;
625 cfspi->ndev = dev;
628 int cfspi_spi_probe(struct platform_device *pdev)
630 struct cfspi *cfspi = NULL;
631 struct net_device *ndev;
632 struct cfspi_dev *dev;
633 int res;
634 dev = (struct cfspi_dev *)pdev->dev.platform_data;
636 ndev = alloc_netdev(sizeof(struct cfspi),
637 "cfspi%d", cfspi_setup);
638 if (!ndev)
639 return -ENOMEM;
641 cfspi = netdev_priv(ndev);
642 netif_stop_queue(ndev);
643 cfspi->ndev = ndev;
644 cfspi->pdev = pdev;
646 /* Set flow info. */
647 cfspi->flow_off_sent = 0;
648 cfspi->qd_low_mark = LOW_WATER_MARK;
649 cfspi->qd_high_mark = HIGH_WATER_MARK;
651 /* Set slave info. */
652 if (!strncmp(cfspi_spi_driver.driver.name, "cfspi_sspi", 10)) {
653 cfspi->slave = true;
654 cfspi->slave_talked = false;
655 } else {
656 cfspi->slave = false;
657 cfspi->slave_talked = false;
660 /* Assign the SPI device. */
661 cfspi->dev = dev;
662 /* Assign the device ifc to this SPI interface. */
663 dev->ifc = &cfspi->ifc;
665 /* Allocate DMA buffers. */
666 cfspi->xfer.va_tx = dma_alloc(&cfspi->xfer.pa_tx);
667 if (!cfspi->xfer.va_tx) {
668 printk(KERN_WARNING
669 "CFSPI: failed to allocate dma TX buffer.\n");
670 res = -ENODEV;
671 goto err_dma_alloc_tx;
674 cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx);
676 if (!cfspi->xfer.va_rx) {
677 printk(KERN_WARNING
678 "CFSPI: failed to allocate dma TX buffer.\n");
679 res = -ENODEV;
680 goto err_dma_alloc_rx;
683 /* Initialize the work queue. */
684 INIT_WORK(&cfspi->work, cfspi_xfer);
686 /* Initialize spin locks. */
687 spin_lock_init(&cfspi->lock);
689 /* Initialize flow control state. */
690 cfspi->flow_stop = false;
692 /* Initialize wait queue. */
693 init_waitqueue_head(&cfspi->wait);
695 /* Create work thread. */
696 cfspi->wq = create_singlethread_workqueue(dev->name);
697 if (!cfspi->wq) {
698 printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
699 res = -ENODEV;
700 goto err_create_wq;
703 /* Initialize work queue. */
704 init_completion(&cfspi->comp);
706 /* Create debugfs entries. */
707 dev_debugfs_add(cfspi);
709 /* Set up the ifc. */
710 cfspi->ifc.ss_cb = cfspi_ss_cb;
711 cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
712 cfspi->ifc.priv = cfspi;
714 /* Add CAIF SPI device to list. */
715 spin_lock(&cfspi_list_lock);
716 list_add_tail(&cfspi->list, &cfspi_list);
717 spin_unlock(&cfspi_list_lock);
719 /* Schedule the work queue. */
720 queue_work(cfspi->wq, &cfspi->work);
722 /* Register network device. */
723 res = register_netdev(ndev);
724 if (res) {
725 printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
726 goto err_net_reg;
728 return res;
730 err_net_reg:
731 dev_debugfs_rem(cfspi);
732 set_bit(SPI_TERMINATE, &cfspi->state);
733 wake_up_interruptible(&cfspi->wait);
734 destroy_workqueue(cfspi->wq);
735 err_create_wq:
736 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
737 err_dma_alloc_rx:
738 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
739 err_dma_alloc_tx:
740 free_netdev(ndev);
742 return res;
745 int cfspi_spi_remove(struct platform_device *pdev)
747 struct list_head *list_node;
748 struct list_head *n;
749 struct cfspi *cfspi = NULL;
750 struct cfspi_dev *dev;
752 dev = (struct cfspi_dev *)pdev->dev.platform_data;
753 spin_lock(&cfspi_list_lock);
754 list_for_each_safe(list_node, n, &cfspi_list) {
755 cfspi = list_entry(list_node, struct cfspi, list);
756 /* Find the corresponding device. */
757 if (cfspi->dev == dev) {
758 /* Remove from list. */
759 list_del(list_node);
760 /* Free DMA buffers. */
761 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
762 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
763 set_bit(SPI_TERMINATE, &cfspi->state);
764 wake_up_interruptible(&cfspi->wait);
765 destroy_workqueue(cfspi->wq);
766 /* Destroy debugfs directory and files. */
767 dev_debugfs_rem(cfspi);
768 unregister_netdev(cfspi->ndev);
769 spin_unlock(&cfspi_list_lock);
770 return 0;
773 spin_unlock(&cfspi_list_lock);
774 return -ENODEV;
777 static void __exit cfspi_exit_module(void)
779 struct list_head *list_node;
780 struct list_head *n;
781 struct cfspi *cfspi = NULL;
783 list_for_each_safe(list_node, n, &cfspi_list) {
784 cfspi = list_entry(list_node, struct cfspi, list);
785 platform_device_unregister(cfspi->pdev);
788 /* Destroy sysfs files. */
789 driver_remove_file(&cfspi_spi_driver.driver,
790 &driver_attr_up_head_align);
791 driver_remove_file(&cfspi_spi_driver.driver,
792 &driver_attr_up_tail_align);
793 driver_remove_file(&cfspi_spi_driver.driver,
794 &driver_attr_down_head_align);
795 driver_remove_file(&cfspi_spi_driver.driver,
796 &driver_attr_down_tail_align);
797 driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
798 /* Unregister platform driver. */
799 platform_driver_unregister(&cfspi_spi_driver);
800 /* Destroy debugfs root directory. */
801 driver_debugfs_remove();
804 static int __init cfspi_init_module(void)
806 int result;
808 /* Initialize spin lock. */
809 spin_lock_init(&cfspi_list_lock);
811 /* Register platform driver. */
812 result = platform_driver_register(&cfspi_spi_driver);
813 if (result) {
814 printk(KERN_ERR "Could not register platform SPI driver.\n");
815 goto err_dev_register;
818 /* Create sysfs files. */
819 result =
820 driver_create_file(&cfspi_spi_driver.driver,
821 &driver_attr_up_head_align);
822 if (result) {
823 printk(KERN_ERR "Sysfs creation failed 1.\n");
824 goto err_create_up_head_align;
827 result =
828 driver_create_file(&cfspi_spi_driver.driver,
829 &driver_attr_up_tail_align);
830 if (result) {
831 printk(KERN_ERR "Sysfs creation failed 2.\n");
832 goto err_create_up_tail_align;
835 result =
836 driver_create_file(&cfspi_spi_driver.driver,
837 &driver_attr_down_head_align);
838 if (result) {
839 printk(KERN_ERR "Sysfs creation failed 3.\n");
840 goto err_create_down_head_align;
843 result =
844 driver_create_file(&cfspi_spi_driver.driver,
845 &driver_attr_down_tail_align);
846 if (result) {
847 printk(KERN_ERR "Sysfs creation failed 4.\n");
848 goto err_create_down_tail_align;
851 result =
852 driver_create_file(&cfspi_spi_driver.driver,
853 &driver_attr_frame_align);
854 if (result) {
855 printk(KERN_ERR "Sysfs creation failed 5.\n");
856 goto err_create_frame_align;
858 driver_debugfs_create();
859 return result;
861 err_create_frame_align:
862 driver_remove_file(&cfspi_spi_driver.driver,
863 &driver_attr_down_tail_align);
864 err_create_down_tail_align:
865 driver_remove_file(&cfspi_spi_driver.driver,
866 &driver_attr_down_head_align);
867 err_create_down_head_align:
868 driver_remove_file(&cfspi_spi_driver.driver,
869 &driver_attr_up_tail_align);
870 err_create_up_tail_align:
871 driver_remove_file(&cfspi_spi_driver.driver,
872 &driver_attr_up_head_align);
873 err_create_up_head_align:
874 err_dev_register:
875 return result;
878 module_init(cfspi_init_module);
879 module_exit(cfspi_exit_module);