GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / caif / caif_spi.c
blobf5058ff2b210da07dd1411bdd8a12f1dafe9ddb2
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 static int spi_loop;
37 module_param(spi_loop, bool, S_IRUGO);
38 MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode.");
40 /* SPI frame alignment. */
41 module_param(spi_frm_align, int, S_IRUGO);
42 MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment.");
44 /* SPI padding options. */
45 module_param(spi_up_head_align, int, S_IRUGO);
46 MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment.");
48 module_param(spi_up_tail_align, int, S_IRUGO);
49 MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment.");
51 module_param(spi_down_head_align, int, S_IRUGO);
52 MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment.");
54 module_param(spi_down_tail_align, int, S_IRUGO);
55 MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment.");
57 #ifdef CONFIG_ARM
58 #define BYTE_HEX_FMT "%02X"
59 #else
60 #define BYTE_HEX_FMT "%02hhX"
61 #endif
63 #define SPI_MAX_PAYLOAD_SIZE 4096
65 * Threshold values for the SPI packet queue. Flowcontrol will be asserted
66 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
67 * deasserted before the number of packets drops below LOW_WATER_MARK.
69 #define LOW_WATER_MARK 100
70 #define HIGH_WATER_MARK (LOW_WATER_MARK*5)
72 #ifdef CONFIG_UML
75 * We sometimes use UML for debugging, but it cannot handle
76 * dma_alloc_coherent so we have to wrap it.
78 static inline void *dma_alloc(dma_addr_t *daddr)
80 return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL);
83 static inline void dma_free(void *cpu_addr, dma_addr_t handle)
85 kfree(cpu_addr);
88 #else
90 static inline void *dma_alloc(dma_addr_t *daddr)
92 return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr,
93 GFP_KERNEL);
96 static inline void dma_free(void *cpu_addr, dma_addr_t handle)
98 dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle);
100 #endif /* CONFIG_UML */
102 #ifdef CONFIG_DEBUG_FS
104 #define DEBUGFS_BUF_SIZE 4096
106 static struct dentry *dbgfs_root;
108 static inline void driver_debugfs_create(void)
110 dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL);
113 static inline void driver_debugfs_remove(void)
115 debugfs_remove(dbgfs_root);
118 static inline void dev_debugfs_rem(struct cfspi *cfspi)
120 debugfs_remove(cfspi->dbgfs_frame);
121 debugfs_remove(cfspi->dbgfs_state);
122 debugfs_remove(cfspi->dbgfs_dir);
125 static int dbgfs_open(struct inode *inode, struct file *file)
127 file->private_data = inode->i_private;
128 return 0;
131 static ssize_t dbgfs_state(struct file *file, char __user *user_buf,
132 size_t count, loff_t *ppos)
134 char *buf;
135 int len = 0;
136 ssize_t size;
137 struct cfspi *cfspi = file->private_data;
139 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
140 if (!buf)
141 return 0;
143 /* Print out debug information. */
144 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
145 "CAIF SPI debug information:\n");
147 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR);
149 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
150 "STATE: %d\n", cfspi->dbg_state);
151 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
152 "Previous CMD: 0x%x\n", cfspi->pcmd);
153 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
154 "Current CMD: 0x%x\n", cfspi->cmd);
155 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
156 "Previous TX len: %d\n", cfspi->tx_ppck_len);
157 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
158 "Previous RX len: %d\n", cfspi->rx_ppck_len);
159 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
160 "Current TX len: %d\n", cfspi->tx_cpck_len);
161 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
162 "Current RX len: %d\n", cfspi->rx_cpck_len);
163 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
164 "Next TX len: %d\n", cfspi->tx_npck_len);
165 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
166 "Next RX len: %d\n", cfspi->rx_npck_len);
168 if (len > DEBUGFS_BUF_SIZE)
169 len = DEBUGFS_BUF_SIZE;
171 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
172 kfree(buf);
174 return size;
177 static ssize_t print_frame(char *buf, size_t size, char *frm,
178 size_t count, size_t cut)
180 int len = 0;
181 int i;
182 for (i = 0; i < count; i++) {
183 len += snprintf((buf + len), (size - len),
184 "[0x" BYTE_HEX_FMT "]",
185 frm[i]);
186 if ((i == cut) && (count > (cut * 2))) {
187 /* Fast forward. */
188 i = count - cut;
189 len += snprintf((buf + len), (size - len),
190 "--- %u bytes skipped ---\n",
191 (int)(count - (cut * 2)));
194 if ((!(i % 10)) && i) {
195 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
196 "\n");
199 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n");
200 return len;
203 static ssize_t dbgfs_frame(struct file *file, char __user *user_buf,
204 size_t count, loff_t *ppos)
206 char *buf;
207 int len = 0;
208 ssize_t size;
209 struct cfspi *cfspi;
211 cfspi = file->private_data;
212 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
213 if (!buf)
214 return 0;
216 /* Print out debug information. */
217 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
218 "Current frame:\n");
220 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
221 "Tx data (Len: %d):\n", cfspi->tx_cpck_len);
223 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
224 cfspi->xfer.va_tx,
225 (cfspi->tx_cpck_len + SPI_CMD_SZ), 100);
227 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
228 "Rx data (Len: %d):\n", cfspi->rx_cpck_len);
230 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
231 cfspi->xfer.va_rx,
232 (cfspi->rx_cpck_len + SPI_CMD_SZ), 100);
234 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
235 kfree(buf);
237 return size;
240 static const struct file_operations dbgfs_state_fops = {
241 .open = dbgfs_open,
242 .read = dbgfs_state,
243 .owner = THIS_MODULE
246 static const struct file_operations dbgfs_frame_fops = {
247 .open = dbgfs_open,
248 .read = dbgfs_frame,
249 .owner = THIS_MODULE
252 static inline void dev_debugfs_add(struct cfspi *cfspi)
254 cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root);
255 cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO,
256 cfspi->dbgfs_dir, cfspi,
257 &dbgfs_state_fops);
258 cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO,
259 cfspi->dbgfs_dir, cfspi,
260 &dbgfs_frame_fops);
263 inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
265 cfspi->dbg_state = state;
267 #else
269 static inline void driver_debugfs_create(void)
273 static inline void driver_debugfs_remove(void)
277 static inline void dev_debugfs_add(struct cfspi *cfspi)
281 static inline void dev_debugfs_rem(struct cfspi *cfspi)
285 inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
288 #endif /* CONFIG_DEBUG_FS */
290 static LIST_HEAD(cfspi_list);
291 static spinlock_t cfspi_list_lock;
293 /* SPI uplink head alignment. */
294 static ssize_t show_up_head_align(struct device_driver *driver, char *buf)
296 return sprintf(buf, "%d\n", spi_up_head_align);
299 static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL);
301 /* SPI uplink tail alignment. */
302 static ssize_t show_up_tail_align(struct device_driver *driver, char *buf)
304 return sprintf(buf, "%d\n", spi_up_tail_align);
307 static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL);
309 /* SPI downlink head alignment. */
310 static ssize_t show_down_head_align(struct device_driver *driver, char *buf)
312 return sprintf(buf, "%d\n", spi_down_head_align);
315 static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL);
317 /* SPI downlink tail alignment. */
318 static ssize_t show_down_tail_align(struct device_driver *driver, char *buf)
320 return sprintf(buf, "%d\n", spi_down_tail_align);
323 static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL);
325 /* SPI frame alignment. */
326 static ssize_t show_frame_align(struct device_driver *driver, char *buf)
328 return sprintf(buf, "%d\n", spi_frm_align);
331 static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL);
333 int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len)
335 u8 *dst = buf;
336 caif_assert(buf);
338 do {
339 struct sk_buff *skb;
340 struct caif_payload_info *info;
341 int spad = 0;
342 int epad;
344 skb = skb_dequeue(&cfspi->chead);
345 if (!skb)
346 break;
349 * Calculate length of frame including SPI padding.
350 * The payload position is found in the control buffer.
352 info = (struct caif_payload_info *)&skb->cb;
355 * Compute head offset i.e. number of bytes to add to
356 * get the start of the payload aligned.
358 if (spi_up_head_align) {
359 spad = 1 + ((info->hdr_len + 1) & spi_up_head_align);
360 *dst = (u8)(spad - 1);
361 dst += spad;
364 /* Copy in CAIF frame. */
365 skb_copy_bits(skb, 0, dst, skb->len);
366 dst += skb->len;
367 cfspi->ndev->stats.tx_packets++;
368 cfspi->ndev->stats.tx_bytes += skb->len;
371 * Compute tail offset i.e. number of bytes to add to
372 * get the complete CAIF frame aligned.
374 epad = (skb->len + spad) & spi_up_tail_align;
375 dst += epad;
377 dev_kfree_skb(skb);
379 } while ((dst - buf) < len);
381 return dst - buf;
384 int cfspi_xmitlen(struct cfspi *cfspi)
386 struct sk_buff *skb = NULL;
387 int frm_len = 0;
388 int pkts = 0;
391 * Decommit previously commited frames.
392 * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
394 while (skb_peek(&cfspi->chead)) {
395 skb = skb_dequeue_tail(&cfspi->chead);
396 skb_queue_head(&cfspi->qhead, skb);
399 do {
400 struct caif_payload_info *info = NULL;
401 int spad = 0;
402 int epad = 0;
404 skb = skb_dequeue(&cfspi->qhead);
405 if (!skb)
406 break;
409 * Calculate length of frame including SPI padding.
410 * The payload position is found in the control buffer.
412 info = (struct caif_payload_info *)&skb->cb;
415 * Compute head offset i.e. number of bytes to add to
416 * get the start of the payload aligned.
418 if (spi_up_head_align)
419 spad = 1 + ((info->hdr_len + 1) & spi_up_head_align);
422 * Compute tail offset i.e. number of bytes to add to
423 * get the complete CAIF frame aligned.
425 epad = (skb->len + spad) & spi_up_tail_align;
427 if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
428 skb_queue_tail(&cfspi->chead, skb);
429 pkts++;
430 frm_len += skb->len + spad + epad;
431 } else {
432 /* Put back packet. */
433 skb_queue_head(&cfspi->qhead, skb);
435 } while (pkts <= CAIF_MAX_SPI_PKTS);
438 * Send flow on if previously sent flow off
439 * and now go below the low water mark
441 if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
442 cfspi->cfdev.flowctrl) {
443 cfspi->flow_off_sent = 0;
444 cfspi->cfdev.flowctrl(cfspi->ndev, 1);
447 return frm_len;
450 static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
452 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
454 if (!in_interrupt())
455 spin_lock(&cfspi->lock);
456 if (assert) {
457 set_bit(SPI_SS_ON, &cfspi->state);
458 set_bit(SPI_XFER, &cfspi->state);
459 } else {
460 set_bit(SPI_SS_OFF, &cfspi->state);
462 if (!in_interrupt())
463 spin_unlock(&cfspi->lock);
465 /* Wake up the xfer thread. */
466 wake_up_interruptible(&cfspi->wait);
469 static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
471 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
473 /* Transfer done, complete work queue */
474 complete(&cfspi->comp);
477 static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
479 struct cfspi *cfspi = NULL;
480 unsigned long flags;
481 if (!dev)
482 return -EINVAL;
484 cfspi = netdev_priv(dev);
486 skb_queue_tail(&cfspi->qhead, skb);
488 spin_lock_irqsave(&cfspi->lock, flags);
489 if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
490 /* Wake up xfer thread. */
491 wake_up_interruptible(&cfspi->wait);
493 spin_unlock_irqrestore(&cfspi->lock, flags);
495 /* Send flow off if number of bytes is above high water mark */
496 if (!cfspi->flow_off_sent &&
497 cfspi->qhead.qlen > cfspi->qd_high_mark &&
498 cfspi->cfdev.flowctrl) {
499 cfspi->flow_off_sent = 1;
500 cfspi->cfdev.flowctrl(cfspi->ndev, 0);
503 return 0;
506 int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
508 u8 *src = buf;
510 caif_assert(buf != NULL);
512 do {
513 int res;
514 struct sk_buff *skb = NULL;
515 int spad = 0;
516 int epad = 0;
517 u8 *dst = NULL;
518 int pkt_len = 0;
521 * Compute head offset i.e. number of bytes added to
522 * get the start of the payload aligned.
524 if (spi_down_head_align) {
525 spad = 1 + *src;
526 src += spad;
529 /* Read length of CAIF frame (little endian). */
530 pkt_len = *src;
531 pkt_len |= ((*(src+1)) << 8) & 0xFF00;
532 pkt_len += 2; /* Add FCS fields. */
534 /* Get a suitable caif packet and copy in data. */
536 skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
537 caif_assert(skb != NULL);
539 dst = skb_put(skb, pkt_len);
540 memcpy(dst, src, pkt_len);
541 src += pkt_len;
543 skb->protocol = htons(ETH_P_CAIF);
544 skb_reset_mac_header(skb);
545 skb->dev = cfspi->ndev;
548 * Push received packet up the stack.
550 if (!spi_loop)
551 res = netif_rx_ni(skb);
552 else
553 res = cfspi_xmit(skb, cfspi->ndev);
555 if (!res) {
556 cfspi->ndev->stats.rx_packets++;
557 cfspi->ndev->stats.rx_bytes += pkt_len;
558 } else
559 cfspi->ndev->stats.rx_dropped++;
562 * Compute tail offset i.e. number of bytes added to
563 * get the complete CAIF frame aligned.
565 epad = (pkt_len + spad) & spi_down_tail_align;
566 src += epad;
567 } while ((src - buf) < len);
569 return src - buf;
572 static int cfspi_open(struct net_device *dev)
574 netif_wake_queue(dev);
575 return 0;
578 static int cfspi_close(struct net_device *dev)
580 netif_stop_queue(dev);
581 return 0;
583 static const struct net_device_ops cfspi_ops = {
584 .ndo_open = cfspi_open,
585 .ndo_stop = cfspi_close,
586 .ndo_start_xmit = cfspi_xmit
589 static void cfspi_setup(struct net_device *dev)
591 struct cfspi *cfspi = netdev_priv(dev);
592 dev->features = 0;
593 dev->netdev_ops = &cfspi_ops;
594 dev->type = ARPHRD_CAIF;
595 dev->flags = IFF_NOARP | IFF_POINTOPOINT;
596 dev->tx_queue_len = 0;
597 dev->mtu = SPI_MAX_PAYLOAD_SIZE;
598 dev->destructor = free_netdev;
599 skb_queue_head_init(&cfspi->qhead);
600 skb_queue_head_init(&cfspi->chead);
601 cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
602 cfspi->cfdev.use_frag = false;
603 cfspi->cfdev.use_stx = false;
604 cfspi->cfdev.use_fcs = false;
605 cfspi->ndev = dev;
608 int cfspi_spi_probe(struct platform_device *pdev)
610 struct cfspi *cfspi = NULL;
611 struct net_device *ndev;
612 struct cfspi_dev *dev;
613 int res;
614 dev = (struct cfspi_dev *)pdev->dev.platform_data;
616 ndev = alloc_netdev(sizeof(struct cfspi),
617 "cfspi%d", cfspi_setup);
618 if (!dev)
619 return -ENODEV;
621 cfspi = netdev_priv(ndev);
622 netif_stop_queue(ndev);
623 cfspi->ndev = ndev;
624 cfspi->pdev = pdev;
626 /* Set flow info */
627 cfspi->flow_off_sent = 0;
628 cfspi->qd_low_mark = LOW_WATER_MARK;
629 cfspi->qd_high_mark = HIGH_WATER_MARK;
631 /* Assign the SPI device. */
632 cfspi->dev = dev;
633 /* Assign the device ifc to this SPI interface. */
634 dev->ifc = &cfspi->ifc;
636 /* Allocate DMA buffers. */
637 cfspi->xfer.va_tx = dma_alloc(&cfspi->xfer.pa_tx);
638 if (!cfspi->xfer.va_tx) {
639 printk(KERN_WARNING
640 "CFSPI: failed to allocate dma TX buffer.\n");
641 res = -ENODEV;
642 goto err_dma_alloc_tx;
645 cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx);
647 if (!cfspi->xfer.va_rx) {
648 printk(KERN_WARNING
649 "CFSPI: failed to allocate dma TX buffer.\n");
650 res = -ENODEV;
651 goto err_dma_alloc_rx;
654 /* Initialize the work queue. */
655 INIT_WORK(&cfspi->work, cfspi_xfer);
657 /* Initialize spin locks. */
658 spin_lock_init(&cfspi->lock);
660 /* Initialize flow control state. */
661 cfspi->flow_stop = false;
663 /* Initialize wait queue. */
664 init_waitqueue_head(&cfspi->wait);
666 /* Create work thread. */
667 cfspi->wq = create_singlethread_workqueue(dev->name);
668 if (!cfspi->wq) {
669 printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
670 res = -ENODEV;
671 goto err_create_wq;
674 /* Initialize work queue. */
675 init_completion(&cfspi->comp);
677 /* Create debugfs entries. */
678 dev_debugfs_add(cfspi);
680 /* Set up the ifc. */
681 cfspi->ifc.ss_cb = cfspi_ss_cb;
682 cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
683 cfspi->ifc.priv = cfspi;
685 /* Add CAIF SPI device to list. */
686 spin_lock(&cfspi_list_lock);
687 list_add_tail(&cfspi->list, &cfspi_list);
688 spin_unlock(&cfspi_list_lock);
690 /* Schedule the work queue. */
691 queue_work(cfspi->wq, &cfspi->work);
693 /* Register network device. */
694 res = register_netdev(ndev);
695 if (res) {
696 printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
697 goto err_net_reg;
699 return res;
701 err_net_reg:
702 dev_debugfs_rem(cfspi);
703 set_bit(SPI_TERMINATE, &cfspi->state);
704 wake_up_interruptible(&cfspi->wait);
705 destroy_workqueue(cfspi->wq);
706 err_create_wq:
707 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
708 err_dma_alloc_rx:
709 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
710 err_dma_alloc_tx:
711 free_netdev(ndev);
713 return res;
716 int cfspi_spi_remove(struct platform_device *pdev)
718 struct list_head *list_node;
719 struct list_head *n;
720 struct cfspi *cfspi = NULL;
721 struct cfspi_dev *dev;
723 dev = (struct cfspi_dev *)pdev->dev.platform_data;
724 spin_lock(&cfspi_list_lock);
725 list_for_each_safe(list_node, n, &cfspi_list) {
726 cfspi = list_entry(list_node, struct cfspi, list);
727 /* Find the corresponding device. */
728 if (cfspi->dev == dev) {
729 /* Remove from list. */
730 list_del(list_node);
731 /* Free DMA buffers. */
732 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
733 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
734 set_bit(SPI_TERMINATE, &cfspi->state);
735 wake_up_interruptible(&cfspi->wait);
736 destroy_workqueue(cfspi->wq);
737 /* Destroy debugfs directory and files. */
738 dev_debugfs_rem(cfspi);
739 unregister_netdev(cfspi->ndev);
740 spin_unlock(&cfspi_list_lock);
741 return 0;
744 spin_unlock(&cfspi_list_lock);
745 return -ENODEV;
748 static void __exit cfspi_exit_module(void)
750 struct list_head *list_node;
751 struct list_head *n;
752 struct cfspi *cfspi = NULL;
754 list_for_each_safe(list_node, n, &cfspi_list) {
755 cfspi = list_entry(list_node, struct cfspi, list);
756 platform_device_unregister(cfspi->pdev);
759 /* Destroy sysfs files. */
760 driver_remove_file(&cfspi_spi_driver.driver,
761 &driver_attr_up_head_align);
762 driver_remove_file(&cfspi_spi_driver.driver,
763 &driver_attr_up_tail_align);
764 driver_remove_file(&cfspi_spi_driver.driver,
765 &driver_attr_down_head_align);
766 driver_remove_file(&cfspi_spi_driver.driver,
767 &driver_attr_down_tail_align);
768 driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
769 /* Unregister platform driver. */
770 platform_driver_unregister(&cfspi_spi_driver);
771 /* Destroy debugfs root directory. */
772 driver_debugfs_remove();
775 static int __init cfspi_init_module(void)
777 int result;
779 /* Initialize spin lock. */
780 spin_lock_init(&cfspi_list_lock);
782 /* Register platform driver. */
783 result = platform_driver_register(&cfspi_spi_driver);
784 if (result) {
785 printk(KERN_ERR "Could not register platform SPI driver.\n");
786 goto err_dev_register;
789 /* Create sysfs files. */
790 result =
791 driver_create_file(&cfspi_spi_driver.driver,
792 &driver_attr_up_head_align);
793 if (result) {
794 printk(KERN_ERR "Sysfs creation failed 1.\n");
795 goto err_create_up_head_align;
798 result =
799 driver_create_file(&cfspi_spi_driver.driver,
800 &driver_attr_up_tail_align);
801 if (result) {
802 printk(KERN_ERR "Sysfs creation failed 2.\n");
803 goto err_create_up_tail_align;
806 result =
807 driver_create_file(&cfspi_spi_driver.driver,
808 &driver_attr_down_head_align);
809 if (result) {
810 printk(KERN_ERR "Sysfs creation failed 3.\n");
811 goto err_create_down_head_align;
814 result =
815 driver_create_file(&cfspi_spi_driver.driver,
816 &driver_attr_down_tail_align);
817 if (result) {
818 printk(KERN_ERR "Sysfs creation failed 4.\n");
819 goto err_create_down_tail_align;
822 result =
823 driver_create_file(&cfspi_spi_driver.driver,
824 &driver_attr_frame_align);
825 if (result) {
826 printk(KERN_ERR "Sysfs creation failed 5.\n");
827 goto err_create_frame_align;
829 driver_debugfs_create();
830 return result;
832 err_create_frame_align:
833 driver_remove_file(&cfspi_spi_driver.driver,
834 &driver_attr_down_tail_align);
835 err_create_down_tail_align:
836 driver_remove_file(&cfspi_spi_driver.driver,
837 &driver_attr_down_head_align);
838 err_create_down_head_align:
839 driver_remove_file(&cfspi_spi_driver.driver,
840 &driver_attr_up_tail_align);
841 err_create_up_tail_align:
842 driver_remove_file(&cfspi_spi_driver.driver,
843 &driver_attr_up_head_align);
844 err_create_up_head_align:
845 err_dev_register:
846 return result;
849 module_init(cfspi_init_module);
850 module_exit(cfspi_exit_module);