]>
Commit | Line | Data |
---|---|---|
529d6dad SB |
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 | ||
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> | |
25 | ||
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 */ | |
31 | ||
32 | MODULE_LICENSE("GPL"); | |
33 | MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>"); | |
34 | MODULE_DESCRIPTION("CAIF SPI driver"); | |
35 | ||
36 | static int spi_loop; | |
37 | module_param(spi_loop, bool, S_IRUGO); | |
38 | MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode."); | |
39 | ||
40 | /* SPI frame alignment. */ | |
41 | module_param(spi_frm_align, int, S_IRUGO); | |
42 | MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment."); | |
43 | ||
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."); | |
47 | ||
48 | module_param(spi_up_tail_align, int, S_IRUGO); | |
49 | MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment."); | |
50 | ||
51 | module_param(spi_down_head_align, int, S_IRUGO); | |
52 | MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment."); | |
53 | ||
54 | module_param(spi_down_tail_align, int, S_IRUGO); | |
55 | MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment."); | |
56 | ||
57 | #ifdef CONFIG_ARM | |
58 | #define BYTE_HEX_FMT "%02X" | |
59 | #else | |
60 | #define BYTE_HEX_FMT "%02hhX" | |
61 | #endif | |
62 | ||
63 | #define SPI_MAX_PAYLOAD_SIZE 4096 | |
64 | /* | |
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. | |
68 | */ | |
69 | #define LOW_WATER_MARK 100 | |
70 | #define HIGH_WATER_MARK (LOW_WATER_MARK*5) | |
71 | ||
72 | #ifdef CONFIG_UML | |
73 | ||
74 | /* | |
75 | * We sometimes use UML for debugging, but it cannot handle | |
76 | * dma_alloc_coherent so we have to wrap it. | |
77 | */ | |
78 | static inline void *dma_alloc(dma_addr_t *daddr) | |
79 | { | |
80 | return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL); | |
81 | } | |
82 | ||
83 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) | |
84 | { | |
85 | kfree(cpu_addr); | |
86 | } | |
87 | ||
88 | #else | |
89 | ||
90 | static inline void *dma_alloc(dma_addr_t *daddr) | |
91 | { | |
92 | return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr, | |
93 | GFP_KERNEL); | |
94 | } | |
95 | ||
96 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) | |
97 | { | |
98 | dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle); | |
99 | } | |
100 | #endif /* CONFIG_UML */ | |
101 | ||
102 | #ifdef CONFIG_DEBUG_FS | |
103 | ||
104 | #define DEBUGFS_BUF_SIZE 4096 | |
105 | ||
106 | static struct dentry *dbgfs_root; | |
107 | ||
108 | static inline void driver_debugfs_create(void) | |
109 | { | |
110 | dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL); | |
111 | } | |
112 | ||
113 | static inline void driver_debugfs_remove(void) | |
114 | { | |
115 | debugfs_remove(dbgfs_root); | |
116 | } | |
117 | ||
118 | static inline void dev_debugfs_rem(struct cfspi *cfspi) | |
119 | { | |
120 | debugfs_remove(cfspi->dbgfs_frame); | |
121 | debugfs_remove(cfspi->dbgfs_state); | |
122 | debugfs_remove(cfspi->dbgfs_dir); | |
123 | } | |
124 | ||
125 | static int dbgfs_open(struct inode *inode, struct file *file) | |
126 | { | |
127 | file->private_data = inode->i_private; | |
128 | return 0; | |
129 | } | |
130 | ||
131 | static ssize_t dbgfs_state(struct file *file, char __user *user_buf, | |
132 | size_t count, loff_t *ppos) | |
133 | { | |
134 | char *buf; | |
135 | int len = 0; | |
136 | ssize_t size; | |
cb8b6a93 | 137 | struct cfspi *cfspi = file->private_data; |
529d6dad SB |
138 | |
139 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); | |
140 | if (!buf) | |
141 | return 0; | |
142 | ||
143 | /* Print out debug information. */ | |
144 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), | |
145 | "CAIF SPI debug information:\n"); | |
146 | ||
147 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR); | |
148 | ||
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); | |
167 | ||
7b7b0b90 DC |
168 | if (len > DEBUGFS_BUF_SIZE) |
169 | len = DEBUGFS_BUF_SIZE; | |
170 | ||
529d6dad SB |
171 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
172 | kfree(buf); | |
173 | ||
174 | return size; | |
175 | } | |
176 | ||
177 | static ssize_t print_frame(char *buf, size_t size, char *frm, | |
178 | size_t count, size_t cut) | |
179 | { | |
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))); | |
192 | } | |
193 | ||
194 | if ((!(i % 10)) && i) { | |
195 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), | |
196 | "\n"); | |
197 | } | |
198 | } | |
199 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n"); | |
200 | return len; | |
201 | } | |
202 | ||
203 | static ssize_t dbgfs_frame(struct file *file, char __user *user_buf, | |
204 | size_t count, loff_t *ppos) | |
205 | { | |
206 | char *buf; | |
207 | int len = 0; | |
208 | ssize_t size; | |
209 | struct cfspi *cfspi; | |
210 | ||
cb8b6a93 | 211 | cfspi = file->private_data; |
529d6dad SB |
212 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); |
213 | if (!buf) | |
214 | return 0; | |
215 | ||
216 | /* Print out debug information. */ | |
217 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), | |
218 | "Current frame:\n"); | |
219 | ||
220 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), | |
221 | "Tx data (Len: %d):\n", cfspi->tx_cpck_len); | |
222 | ||
223 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), | |
224 | cfspi->xfer.va_tx, | |
225 | (cfspi->tx_cpck_len + SPI_CMD_SZ), 100); | |
226 | ||
227 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), | |
228 | "Rx data (Len: %d):\n", cfspi->rx_cpck_len); | |
229 | ||
230 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), | |
231 | cfspi->xfer.va_rx, | |
232 | (cfspi->rx_cpck_len + SPI_CMD_SZ), 100); | |
233 | ||
234 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); | |
235 | kfree(buf); | |
236 | ||
237 | return size; | |
238 | } | |
239 | ||
240 | static const struct file_operations dbgfs_state_fops = { | |
241 | .open = dbgfs_open, | |
242 | .read = dbgfs_state, | |
243 | .owner = THIS_MODULE | |
244 | }; | |
245 | ||
246 | static const struct file_operations dbgfs_frame_fops = { | |
247 | .open = dbgfs_open, | |
248 | .read = dbgfs_frame, | |
249 | .owner = THIS_MODULE | |
250 | }; | |
251 | ||
252 | static inline void dev_debugfs_add(struct cfspi *cfspi) | |
253 | { | |
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); | |
261 | } | |
262 | ||
263 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) | |
264 | { | |
265 | cfspi->dbg_state = state; | |
266 | }; | |
267 | #else | |
268 | ||
269 | static inline void driver_debugfs_create(void) | |
270 | { | |
271 | } | |
272 | ||
273 | static inline void driver_debugfs_remove(void) | |
274 | { | |
275 | } | |
276 | ||
277 | static inline void dev_debugfs_add(struct cfspi *cfspi) | |
278 | { | |
279 | } | |
280 | ||
281 | static inline void dev_debugfs_rem(struct cfspi *cfspi) | |
282 | { | |
283 | } | |
284 | ||
285 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) | |
286 | { | |
287 | } | |
288 | #endif /* CONFIG_DEBUG_FS */ | |
289 | ||
290 | static LIST_HEAD(cfspi_list); | |
291 | static spinlock_t cfspi_list_lock; | |
292 | ||
293 | /* SPI uplink head alignment. */ | |
294 | static ssize_t show_up_head_align(struct device_driver *driver, char *buf) | |
295 | { | |
296 | return sprintf(buf, "%d\n", spi_up_head_align); | |
297 | } | |
298 | ||
299 | static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL); | |
300 | ||
301 | /* SPI uplink tail alignment. */ | |
302 | static ssize_t show_up_tail_align(struct device_driver *driver, char *buf) | |
303 | { | |
304 | return sprintf(buf, "%d\n", spi_up_tail_align); | |
305 | } | |
306 | ||
307 | static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL); | |
308 | ||
309 | /* SPI downlink head alignment. */ | |
310 | static ssize_t show_down_head_align(struct device_driver *driver, char *buf) | |
311 | { | |
312 | return sprintf(buf, "%d\n", spi_down_head_align); | |
313 | } | |
314 | ||
315 | static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL); | |
316 | ||
317 | /* SPI downlink tail alignment. */ | |
318 | static ssize_t show_down_tail_align(struct device_driver *driver, char *buf) | |
319 | { | |
320 | return sprintf(buf, "%d\n", spi_down_tail_align); | |
321 | } | |
322 | ||
323 | static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL); | |
324 | ||
325 | /* SPI frame alignment. */ | |
326 | static ssize_t show_frame_align(struct device_driver *driver, char *buf) | |
327 | { | |
328 | return sprintf(buf, "%d\n", spi_frm_align); | |
329 | } | |
330 | ||
331 | static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL); | |
332 | ||
333 | int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len) | |
334 | { | |
335 | u8 *dst = buf; | |
336 | caif_assert(buf); | |
337 | ||
338 | do { | |
339 | struct sk_buff *skb; | |
340 | struct caif_payload_info *info; | |
341 | int spad = 0; | |
342 | int epad; | |
343 | ||
344 | skb = skb_dequeue(&cfspi->chead); | |
345 | if (!skb) | |
346 | break; | |
347 | ||
348 | /* | |
349 | * Calculate length of frame including SPI padding. | |
350 | * The payload position is found in the control buffer. | |
351 | */ | |
352 | info = (struct caif_payload_info *)&skb->cb; | |
353 | ||
354 | /* | |
355 | * Compute head offset i.e. number of bytes to add to | |
356 | * get the start of the payload aligned. | |
357 | */ | |
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; | |
362 | } | |
363 | ||
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; | |
369 | ||
370 | /* | |
371 | * Compute tail offset i.e. number of bytes to add to | |
372 | * get the complete CAIF frame aligned. | |
373 | */ | |
374 | epad = (skb->len + spad) & spi_up_tail_align; | |
375 | dst += epad; | |
376 | ||
377 | dev_kfree_skb(skb); | |
378 | ||
379 | } while ((dst - buf) < len); | |
380 | ||
381 | return dst - buf; | |
382 | } | |
383 | ||
384 | int cfspi_xmitlen(struct cfspi *cfspi) | |
385 | { | |
386 | struct sk_buff *skb = NULL; | |
387 | int frm_len = 0; | |
388 | int pkts = 0; | |
389 | ||
390 | /* | |
391 | * Decommit previously commited frames. | |
392 | * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead) | |
393 | */ | |
394 | while (skb_peek(&cfspi->chead)) { | |
395 | skb = skb_dequeue_tail(&cfspi->chead); | |
396 | skb_queue_head(&cfspi->qhead, skb); | |
397 | } | |
398 | ||
399 | do { | |
400 | struct caif_payload_info *info = NULL; | |
401 | int spad = 0; | |
402 | int epad = 0; | |
403 | ||
404 | skb = skb_dequeue(&cfspi->qhead); | |
405 | if (!skb) | |
406 | break; | |
407 | ||
408 | /* | |
409 | * Calculate length of frame including SPI padding. | |
410 | * The payload position is found in the control buffer. | |
411 | */ | |
412 | info = (struct caif_payload_info *)&skb->cb; | |
413 | ||
414 | /* | |
415 | * Compute head offset i.e. number of bytes to add to | |
416 | * get the start of the payload aligned. | |
417 | */ | |
418 | if (spi_up_head_align) | |
419 | spad = 1 + ((info->hdr_len + 1) & spi_up_head_align); | |
420 | ||
421 | /* | |
422 | * Compute tail offset i.e. number of bytes to add to | |
423 | * get the complete CAIF frame aligned. | |
424 | */ | |
425 | epad = (skb->len + spad) & spi_up_tail_align; | |
426 | ||
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); | |
434 | } | |
435 | } while (pkts <= CAIF_MAX_SPI_PKTS); | |
436 | ||
437 | /* | |
438 | * Send flow on if previously sent flow off | |
439 | * and now go below the low water mark | |
440 | */ | |
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); | |
445 | } | |
446 | ||
447 | return frm_len; | |
448 | } | |
449 | ||
450 | static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc) | |
451 | { | |
452 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; | |
453 | ||
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); | |
461 | } | |
462 | if (!in_interrupt()) | |
463 | spin_unlock(&cfspi->lock); | |
464 | ||
465 | /* Wake up the xfer thread. */ | |
466 | wake_up_interruptible(&cfspi->wait); | |
467 | } | |
468 | ||
469 | static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc) | |
470 | { | |
471 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; | |
472 | ||
473 | /* Transfer done, complete work queue */ | |
474 | complete(&cfspi->comp); | |
475 | } | |
476 | ||
477 | static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev) | |
478 | { | |
479 | struct cfspi *cfspi = NULL; | |
480 | unsigned long flags; | |
481 | if (!dev) | |
482 | return -EINVAL; | |
483 | ||
484 | cfspi = netdev_priv(dev); | |
485 | ||
486 | skb_queue_tail(&cfspi->qhead, skb); | |
487 | ||
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); | |
492 | } | |
493 | spin_unlock_irqrestore(&cfspi->lock, flags); | |
494 | ||
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); | |
501 | } | |
502 | ||
503 | return 0; | |
504 | } | |
505 | ||
506 | int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len) | |
507 | { | |
508 | u8 *src = buf; | |
509 | ||
510 | caif_assert(buf != NULL); | |
511 | ||
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; | |
519 | ||
520 | /* | |
521 | * Compute head offset i.e. number of bytes added to | |
522 | * get the start of the payload aligned. | |
523 | */ | |
524 | if (spi_down_head_align) { | |
525 | spad = 1 + *src; | |
526 | src += spad; | |
527 | } | |
528 | ||
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. */ | |
533 | ||
534 | /* Get a suitable caif packet and copy in data. */ | |
535 | ||
536 | skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1); | |
537 | caif_assert(skb != NULL); | |
538 | ||
539 | dst = skb_put(skb, pkt_len); | |
540 | memcpy(dst, src, pkt_len); | |
541 | src += pkt_len; | |
542 | ||
543 | skb->protocol = htons(ETH_P_CAIF); | |
544 | skb_reset_mac_header(skb); | |
545 | skb->dev = cfspi->ndev; | |
546 | ||
547 | /* | |
548 | * Push received packet up the stack. | |
549 | */ | |
550 | if (!spi_loop) | |
551 | res = netif_rx_ni(skb); | |
552 | else | |
553 | res = cfspi_xmit(skb, cfspi->ndev); | |
554 | ||
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++; | |
560 | ||
561 | /* | |
562 | * Compute tail offset i.e. number of bytes added to | |
563 | * get the complete CAIF frame aligned. | |
564 | */ | |
565 | epad = (pkt_len + spad) & spi_down_tail_align; | |
566 | src += epad; | |
567 | } while ((src - buf) < len); | |
568 | ||
569 | return src - buf; | |
570 | } | |
571 | ||
572 | static int cfspi_open(struct net_device *dev) | |
573 | { | |
574 | netif_wake_queue(dev); | |
575 | return 0; | |
576 | } | |
577 | ||
578 | static int cfspi_close(struct net_device *dev) | |
579 | { | |
580 | netif_stop_queue(dev); | |
581 | return 0; | |
582 | } | |
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 | |
587 | }; | |
588 | ||
589 | static void cfspi_setup(struct net_device *dev) | |
590 | { | |
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; | |
606 | } | |
607 | ||
608 | int cfspi_spi_probe(struct platform_device *pdev) | |
609 | { | |
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; | |
615 | ||
616 | ndev = alloc_netdev(sizeof(struct cfspi), | |
617 | "cfspi%d", cfspi_setup); | |
618 | if (!dev) | |
619 | return -ENODEV; | |
620 | ||
621 | cfspi = netdev_priv(ndev); | |
622 | netif_stop_queue(ndev); | |
623 | cfspi->ndev = ndev; | |
624 | cfspi->pdev = pdev; | |
625 | ||
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; | |
630 | ||
631 | /* Assign the SPI device. */ | |
632 | cfspi->dev = dev; | |
633 | /* Assign the device ifc to this SPI interface. */ | |
634 | dev->ifc = &cfspi->ifc; | |
635 | ||
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; | |
643 | } | |
644 | ||
645 | cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx); | |
646 | ||
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; | |
652 | } | |
653 | ||
654 | /* Initialize the work queue. */ | |
655 | INIT_WORK(&cfspi->work, cfspi_xfer); | |
656 | ||
657 | /* Initialize spin locks. */ | |
658 | spin_lock_init(&cfspi->lock); | |
659 | ||
660 | /* Initialize flow control state. */ | |
661 | cfspi->flow_stop = false; | |
662 | ||
663 | /* Initialize wait queue. */ | |
664 | init_waitqueue_head(&cfspi->wait); | |
665 | ||
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; | |
672 | } | |
673 | ||
674 | /* Initialize work queue. */ | |
675 | init_completion(&cfspi->comp); | |
676 | ||
677 | /* Create debugfs entries. */ | |
678 | dev_debugfs_add(cfspi); | |
679 | ||
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; | |
684 | ||
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); | |
689 | ||
690 | /* Schedule the work queue. */ | |
691 | queue_work(cfspi->wq, &cfspi->work); | |
692 | ||
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; | |
698 | } | |
699 | return res; | |
700 | ||
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); | |
712 | ||
713 | return res; | |
714 | } | |
715 | ||
716 | int cfspi_spi_remove(struct platform_device *pdev) | |
717 | { | |
718 | struct list_head *list_node; | |
719 | struct list_head *n; | |
720 | struct cfspi *cfspi = NULL; | |
721 | struct cfspi_dev *dev; | |
722 | ||
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; | |
742 | } | |
743 | } | |
744 | spin_unlock(&cfspi_list_lock); | |
745 | return -ENODEV; | |
746 | } | |
747 | ||
748 | static void __exit cfspi_exit_module(void) | |
749 | { | |
750 | struct list_head *list_node; | |
751 | struct list_head *n; | |
752 | struct cfspi *cfspi = NULL; | |
753 | ||
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); | |
757 | } | |
758 | ||
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(); | |
773 | } | |
774 | ||
775 | static int __init cfspi_init_module(void) | |
776 | { | |
777 | int result; | |
778 | ||
779 | /* Initialize spin lock. */ | |
780 | spin_lock_init(&cfspi_list_lock); | |
781 | ||
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; | |
787 | } | |
788 | ||
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; | |
796 | } | |
797 | ||
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; | |
804 | } | |
805 | ||
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; | |
812 | } | |
813 | ||
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; | |
820 | } | |
821 | ||
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; | |
828 | } | |
829 | driver_debugfs_create(); | |
830 | return result; | |
831 | ||
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; | |
847 | } | |
848 | ||
849 | module_init(cfspi_init_module); | |
850 | module_exit(cfspi_exit_module); |