drm/i915: range-restricted eviction support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / caif / caif_spi.c
blob8427533fe313c35cb38727e533cd77dd35cdb4af
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,
244 .llseek = default_llseek,
247 static const struct file_operations dbgfs_frame_fops = {
248 .open = dbgfs_open,
249 .read = dbgfs_frame,
250 .owner = THIS_MODULE,
251 .llseek = default_llseek,
254 static inline void dev_debugfs_add(struct cfspi *cfspi)
256 cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root);
257 cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO,
258 cfspi->dbgfs_dir, cfspi,
259 &dbgfs_state_fops);
260 cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO,
261 cfspi->dbgfs_dir, cfspi,
262 &dbgfs_frame_fops);
265 inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
267 cfspi->dbg_state = state;
269 #else
271 static inline void driver_debugfs_create(void)
275 static inline void driver_debugfs_remove(void)
279 static inline void dev_debugfs_add(struct cfspi *cfspi)
283 static inline void dev_debugfs_rem(struct cfspi *cfspi)
287 inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
290 #endif /* CONFIG_DEBUG_FS */
292 static LIST_HEAD(cfspi_list);
293 static spinlock_t cfspi_list_lock;
295 /* SPI uplink head alignment. */
296 static ssize_t show_up_head_align(struct device_driver *driver, char *buf)
298 return sprintf(buf, "%d\n", spi_up_head_align);
301 static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL);
303 /* SPI uplink tail alignment. */
304 static ssize_t show_up_tail_align(struct device_driver *driver, char *buf)
306 return sprintf(buf, "%d\n", spi_up_tail_align);
309 static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL);
311 /* SPI downlink head alignment. */
312 static ssize_t show_down_head_align(struct device_driver *driver, char *buf)
314 return sprintf(buf, "%d\n", spi_down_head_align);
317 static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL);
319 /* SPI downlink tail alignment. */
320 static ssize_t show_down_tail_align(struct device_driver *driver, char *buf)
322 return sprintf(buf, "%d\n", spi_down_tail_align);
325 static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL);
327 /* SPI frame alignment. */
328 static ssize_t show_frame_align(struct device_driver *driver, char *buf)
330 return sprintf(buf, "%d\n", spi_frm_align);
333 static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL);
335 int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len)
337 u8 *dst = buf;
338 caif_assert(buf);
340 do {
341 struct sk_buff *skb;
342 struct caif_payload_info *info;
343 int spad = 0;
344 int epad;
346 skb = skb_dequeue(&cfspi->chead);
347 if (!skb)
348 break;
351 * Calculate length of frame including SPI padding.
352 * The payload position is found in the control buffer.
354 info = (struct caif_payload_info *)&skb->cb;
357 * Compute head offset i.e. number of bytes to add to
358 * get the start of the payload aligned.
360 if (spi_up_head_align) {
361 spad = 1 + ((info->hdr_len + 1) & spi_up_head_align);
362 *dst = (u8)(spad - 1);
363 dst += spad;
366 /* Copy in CAIF frame. */
367 skb_copy_bits(skb, 0, dst, skb->len);
368 dst += skb->len;
369 cfspi->ndev->stats.tx_packets++;
370 cfspi->ndev->stats.tx_bytes += skb->len;
373 * Compute tail offset i.e. number of bytes to add to
374 * get the complete CAIF frame aligned.
376 epad = (skb->len + spad) & spi_up_tail_align;
377 dst += epad;
379 dev_kfree_skb(skb);
381 } while ((dst - buf) < len);
383 return dst - buf;
386 int cfspi_xmitlen(struct cfspi *cfspi)
388 struct sk_buff *skb = NULL;
389 int frm_len = 0;
390 int pkts = 0;
393 * Decommit previously commited frames.
394 * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
396 while (skb_peek(&cfspi->chead)) {
397 skb = skb_dequeue_tail(&cfspi->chead);
398 skb_queue_head(&cfspi->qhead, skb);
401 do {
402 struct caif_payload_info *info = NULL;
403 int spad = 0;
404 int epad = 0;
406 skb = skb_dequeue(&cfspi->qhead);
407 if (!skb)
408 break;
411 * Calculate length of frame including SPI padding.
412 * The payload position is found in the control buffer.
414 info = (struct caif_payload_info *)&skb->cb;
417 * Compute head offset i.e. number of bytes to add to
418 * get the start of the payload aligned.
420 if (spi_up_head_align)
421 spad = 1 + ((info->hdr_len + 1) & spi_up_head_align);
424 * Compute tail offset i.e. number of bytes to add to
425 * get the complete CAIF frame aligned.
427 epad = (skb->len + spad) & spi_up_tail_align;
429 if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
430 skb_queue_tail(&cfspi->chead, skb);
431 pkts++;
432 frm_len += skb->len + spad + epad;
433 } else {
434 /* Put back packet. */
435 skb_queue_head(&cfspi->qhead, skb);
437 } while (pkts <= CAIF_MAX_SPI_PKTS);
440 * Send flow on if previously sent flow off
441 * and now go below the low water mark
443 if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
444 cfspi->cfdev.flowctrl) {
445 cfspi->flow_off_sent = 0;
446 cfspi->cfdev.flowctrl(cfspi->ndev, 1);
449 return frm_len;
452 static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
454 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
456 if (!in_interrupt())
457 spin_lock(&cfspi->lock);
458 if (assert) {
459 set_bit(SPI_SS_ON, &cfspi->state);
460 set_bit(SPI_XFER, &cfspi->state);
461 } else {
462 set_bit(SPI_SS_OFF, &cfspi->state);
464 if (!in_interrupt())
465 spin_unlock(&cfspi->lock);
467 /* Wake up the xfer thread. */
468 wake_up_interruptible(&cfspi->wait);
471 static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
473 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
475 /* Transfer done, complete work queue */
476 complete(&cfspi->comp);
479 static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
481 struct cfspi *cfspi = NULL;
482 unsigned long flags;
483 if (!dev)
484 return -EINVAL;
486 cfspi = netdev_priv(dev);
488 skb_queue_tail(&cfspi->qhead, skb);
490 spin_lock_irqsave(&cfspi->lock, flags);
491 if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
492 /* Wake up xfer thread. */
493 wake_up_interruptible(&cfspi->wait);
495 spin_unlock_irqrestore(&cfspi->lock, flags);
497 /* Send flow off if number of bytes is above high water mark */
498 if (!cfspi->flow_off_sent &&
499 cfspi->qhead.qlen > cfspi->qd_high_mark &&
500 cfspi->cfdev.flowctrl) {
501 cfspi->flow_off_sent = 1;
502 cfspi->cfdev.flowctrl(cfspi->ndev, 0);
505 return 0;
508 int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
510 u8 *src = buf;
512 caif_assert(buf != NULL);
514 do {
515 int res;
516 struct sk_buff *skb = NULL;
517 int spad = 0;
518 int epad = 0;
519 u8 *dst = NULL;
520 int pkt_len = 0;
523 * Compute head offset i.e. number of bytes added to
524 * get the start of the payload aligned.
526 if (spi_down_head_align) {
527 spad = 1 + *src;
528 src += spad;
531 /* Read length of CAIF frame (little endian). */
532 pkt_len = *src;
533 pkt_len |= ((*(src+1)) << 8) & 0xFF00;
534 pkt_len += 2; /* Add FCS fields. */
536 /* Get a suitable caif packet and copy in data. */
538 skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
539 caif_assert(skb != NULL);
541 dst = skb_put(skb, pkt_len);
542 memcpy(dst, src, pkt_len);
543 src += pkt_len;
545 skb->protocol = htons(ETH_P_CAIF);
546 skb_reset_mac_header(skb);
547 skb->dev = cfspi->ndev;
550 * Push received packet up the stack.
552 if (!spi_loop)
553 res = netif_rx_ni(skb);
554 else
555 res = cfspi_xmit(skb, cfspi->ndev);
557 if (!res) {
558 cfspi->ndev->stats.rx_packets++;
559 cfspi->ndev->stats.rx_bytes += pkt_len;
560 } else
561 cfspi->ndev->stats.rx_dropped++;
564 * Compute tail offset i.e. number of bytes added to
565 * get the complete CAIF frame aligned.
567 epad = (pkt_len + spad) & spi_down_tail_align;
568 src += epad;
569 } while ((src - buf) < len);
571 return src - buf;
574 static int cfspi_open(struct net_device *dev)
576 netif_wake_queue(dev);
577 return 0;
580 static int cfspi_close(struct net_device *dev)
582 netif_stop_queue(dev);
583 return 0;
585 static const struct net_device_ops cfspi_ops = {
586 .ndo_open = cfspi_open,
587 .ndo_stop = cfspi_close,
588 .ndo_start_xmit = cfspi_xmit
591 static void cfspi_setup(struct net_device *dev)
593 struct cfspi *cfspi = netdev_priv(dev);
594 dev->features = 0;
595 dev->netdev_ops = &cfspi_ops;
596 dev->type = ARPHRD_CAIF;
597 dev->flags = IFF_NOARP | IFF_POINTOPOINT;
598 dev->tx_queue_len = 0;
599 dev->mtu = SPI_MAX_PAYLOAD_SIZE;
600 dev->destructor = free_netdev;
601 skb_queue_head_init(&cfspi->qhead);
602 skb_queue_head_init(&cfspi->chead);
603 cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
604 cfspi->cfdev.use_frag = false;
605 cfspi->cfdev.use_stx = false;
606 cfspi->cfdev.use_fcs = false;
607 cfspi->ndev = dev;
610 int cfspi_spi_probe(struct platform_device *pdev)
612 struct cfspi *cfspi = NULL;
613 struct net_device *ndev;
614 struct cfspi_dev *dev;
615 int res;
616 dev = (struct cfspi_dev *)pdev->dev.platform_data;
618 ndev = alloc_netdev(sizeof(struct cfspi),
619 "cfspi%d", cfspi_setup);
620 if (!dev)
621 return -ENODEV;
623 cfspi = netdev_priv(ndev);
624 netif_stop_queue(ndev);
625 cfspi->ndev = ndev;
626 cfspi->pdev = pdev;
628 /* Set flow info */
629 cfspi->flow_off_sent = 0;
630 cfspi->qd_low_mark = LOW_WATER_MARK;
631 cfspi->qd_high_mark = HIGH_WATER_MARK;
633 /* Assign the SPI device. */
634 cfspi->dev = dev;
635 /* Assign the device ifc to this SPI interface. */
636 dev->ifc = &cfspi->ifc;
638 /* Allocate DMA buffers. */
639 cfspi->xfer.va_tx = dma_alloc(&cfspi->xfer.pa_tx);
640 if (!cfspi->xfer.va_tx) {
641 printk(KERN_WARNING
642 "CFSPI: failed to allocate dma TX buffer.\n");
643 res = -ENODEV;
644 goto err_dma_alloc_tx;
647 cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx);
649 if (!cfspi->xfer.va_rx) {
650 printk(KERN_WARNING
651 "CFSPI: failed to allocate dma TX buffer.\n");
652 res = -ENODEV;
653 goto err_dma_alloc_rx;
656 /* Initialize the work queue. */
657 INIT_WORK(&cfspi->work, cfspi_xfer);
659 /* Initialize spin locks. */
660 spin_lock_init(&cfspi->lock);
662 /* Initialize flow control state. */
663 cfspi->flow_stop = false;
665 /* Initialize wait queue. */
666 init_waitqueue_head(&cfspi->wait);
668 /* Create work thread. */
669 cfspi->wq = create_singlethread_workqueue(dev->name);
670 if (!cfspi->wq) {
671 printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
672 res = -ENODEV;
673 goto err_create_wq;
676 /* Initialize work queue. */
677 init_completion(&cfspi->comp);
679 /* Create debugfs entries. */
680 dev_debugfs_add(cfspi);
682 /* Set up the ifc. */
683 cfspi->ifc.ss_cb = cfspi_ss_cb;
684 cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
685 cfspi->ifc.priv = cfspi;
687 /* Add CAIF SPI device to list. */
688 spin_lock(&cfspi_list_lock);
689 list_add_tail(&cfspi->list, &cfspi_list);
690 spin_unlock(&cfspi_list_lock);
692 /* Schedule the work queue. */
693 queue_work(cfspi->wq, &cfspi->work);
695 /* Register network device. */
696 res = register_netdev(ndev);
697 if (res) {
698 printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
699 goto err_net_reg;
701 return res;
703 err_net_reg:
704 dev_debugfs_rem(cfspi);
705 set_bit(SPI_TERMINATE, &cfspi->state);
706 wake_up_interruptible(&cfspi->wait);
707 destroy_workqueue(cfspi->wq);
708 err_create_wq:
709 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
710 err_dma_alloc_rx:
711 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
712 err_dma_alloc_tx:
713 free_netdev(ndev);
715 return res;
718 int cfspi_spi_remove(struct platform_device *pdev)
720 struct list_head *list_node;
721 struct list_head *n;
722 struct cfspi *cfspi = NULL;
723 struct cfspi_dev *dev;
725 dev = (struct cfspi_dev *)pdev->dev.platform_data;
726 spin_lock(&cfspi_list_lock);
727 list_for_each_safe(list_node, n, &cfspi_list) {
728 cfspi = list_entry(list_node, struct cfspi, list);
729 /* Find the corresponding device. */
730 if (cfspi->dev == dev) {
731 /* Remove from list. */
732 list_del(list_node);
733 /* Free DMA buffers. */
734 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
735 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
736 set_bit(SPI_TERMINATE, &cfspi->state);
737 wake_up_interruptible(&cfspi->wait);
738 destroy_workqueue(cfspi->wq);
739 /* Destroy debugfs directory and files. */
740 dev_debugfs_rem(cfspi);
741 unregister_netdev(cfspi->ndev);
742 spin_unlock(&cfspi_list_lock);
743 return 0;
746 spin_unlock(&cfspi_list_lock);
747 return -ENODEV;
750 static void __exit cfspi_exit_module(void)
752 struct list_head *list_node;
753 struct list_head *n;
754 struct cfspi *cfspi = NULL;
756 list_for_each_safe(list_node, n, &cfspi_list) {
757 cfspi = list_entry(list_node, struct cfspi, list);
758 platform_device_unregister(cfspi->pdev);
761 /* Destroy sysfs files. */
762 driver_remove_file(&cfspi_spi_driver.driver,
763 &driver_attr_up_head_align);
764 driver_remove_file(&cfspi_spi_driver.driver,
765 &driver_attr_up_tail_align);
766 driver_remove_file(&cfspi_spi_driver.driver,
767 &driver_attr_down_head_align);
768 driver_remove_file(&cfspi_spi_driver.driver,
769 &driver_attr_down_tail_align);
770 driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
771 /* Unregister platform driver. */
772 platform_driver_unregister(&cfspi_spi_driver);
773 /* Destroy debugfs root directory. */
774 driver_debugfs_remove();
777 static int __init cfspi_init_module(void)
779 int result;
781 /* Initialize spin lock. */
782 spin_lock_init(&cfspi_list_lock);
784 /* Register platform driver. */
785 result = platform_driver_register(&cfspi_spi_driver);
786 if (result) {
787 printk(KERN_ERR "Could not register platform SPI driver.\n");
788 goto err_dev_register;
791 /* Create sysfs files. */
792 result =
793 driver_create_file(&cfspi_spi_driver.driver,
794 &driver_attr_up_head_align);
795 if (result) {
796 printk(KERN_ERR "Sysfs creation failed 1.\n");
797 goto err_create_up_head_align;
800 result =
801 driver_create_file(&cfspi_spi_driver.driver,
802 &driver_attr_up_tail_align);
803 if (result) {
804 printk(KERN_ERR "Sysfs creation failed 2.\n");
805 goto err_create_up_tail_align;
808 result =
809 driver_create_file(&cfspi_spi_driver.driver,
810 &driver_attr_down_head_align);
811 if (result) {
812 printk(KERN_ERR "Sysfs creation failed 3.\n");
813 goto err_create_down_head_align;
816 result =
817 driver_create_file(&cfspi_spi_driver.driver,
818 &driver_attr_down_tail_align);
819 if (result) {
820 printk(KERN_ERR "Sysfs creation failed 4.\n");
821 goto err_create_down_tail_align;
824 result =
825 driver_create_file(&cfspi_spi_driver.driver,
826 &driver_attr_frame_align);
827 if (result) {
828 printk(KERN_ERR "Sysfs creation failed 5.\n");
829 goto err_create_frame_align;
831 driver_debugfs_create();
832 return result;
834 err_create_frame_align:
835 driver_remove_file(&cfspi_spi_driver.driver,
836 &driver_attr_down_tail_align);
837 err_create_down_tail_align:
838 driver_remove_file(&cfspi_spi_driver.driver,
839 &driver_attr_down_head_align);
840 err_create_down_head_align:
841 driver_remove_file(&cfspi_spi_driver.driver,
842 &driver_attr_up_tail_align);
843 err_create_up_tail_align:
844 driver_remove_file(&cfspi_spi_driver.driver,
845 &driver_attr_up_head_align);
846 err_create_up_head_align:
847 err_dev_register:
848 return result;
851 module_init(cfspi_init_module);
852 module_exit(cfspi_exit_module);