Debian Codegraph @

drivers/dahdi/voicebus/voicebus.c

raw symbols SCIP
1 /*
2 * VoiceBus(tm) Interface Library.
3 *
4 * Written by Shaun Ruffell <[email protected]>
5 * and based on previous work by Mark Spencer <[email protected]>,
6 * Matthew Fredrickson <[email protected]>, and
7 * Michael Spiceland <[email protected]>
8 *
9 * Copyright (C) 2007-2010 Digium, Inc.
10 *
11 * All rights reserved.
12
13 * VoiceBus is a registered trademark of Digium.
14 *
15 */
16
17 /*
18 * See http://www.asterisk.org for more information about
19 * the Asterisk project. Please do not directly contact
20 * any of the maintainers of this project for assistance;
21 * the project provides a web site, mailing lists and IRC
22 * channels for your use.
23 *
24 * This program is free software, distributed under the terms of
25 * the GNU General Public License Version 2 as published by the
26 * Free Software Foundation. See the LICENSE file included with
27 * this program for more details.
28 */
29
30 #include <linux/version.h>
31 #include <linux/slab.h>
32 #include <linux/pci.h>
33 #include <linux/ioport.h>
34 #include <linux/delay.h>
35 #include <linux/interrupt.h>
36 #include <linux/timer.h>
37 #include <linux/module.h>
38 #include <linux/sched.h>
39
40 #include <dahdi/kernel.h>
41 #include "voicebus.h"
42 #include "voicebus_net.h"
43 #include "vpmadtreg.h"
44 #include "GpakCust.h"
45
46 #if VOICEBUS_DEFERRED == TIMER
47 #if HZ < 1000
48 /* \todo Put an error message here. */
49 #endif
50 #endif
51
52 /* Interrupt status' reported in SR_CSR5 */
53 #define TX_COMPLETE_INTERRUPT 0x00000001
54 #define TX_STOPPED_INTERRUPT 0x00000002
55 #define TX_UNAVAILABLE_INTERRUPT 0x00000004
56 #define TX_JABBER_TIMEOUT_INTERRUPT 0x00000008
57 #define TX_UNDERFLOW_INTERRUPT 0x00000020
58 #define RX_COMPLETE_INTERRUPT 0x00000040
59 #define RX_UNAVAILABLE_INTERRUPT 0x00000080
60 #define RX_STOPPED_INTERRUPT 0x00000100
61 #define RX_WATCHDOG_TIMEOUT_INTERRUPT 0x00000200
62 #define TIMER_INTERRUPT 0x00000800
63 #define FATAL_BUS_ERROR_INTERRUPT 0x00002000
64 #define ABNORMAL_INTERRUPT_SUMMARY 0x00008000
65 #define NORMAL_INTERRUPT_SUMMARY 0x00010000
66
67 #define SR_CSR5 0x0028
68 #define NAR_CSR6 0x0030
69
70 #define IER_CSR7 0x0038
71 #define CSR7_TCIE 0x00000001 /* tx complete */
72 #define CSR7_TPSIE 0x00000002 /* tx processor stopped */
73 #define CSR7_TDUIE 0x00000004 /* tx desc unavailable */
74 #define CSR7_TUIE 0x00000020 /* tx underflow */
75 #define CSR7_RCIE 0x00000040 /* rx complete */
76 #define CSR7_RUIE 0x00000080 /* rx desc unavailable */
77 #define CSR7_RSIE 0x00000100 /* rx processor stopped */
78 #define CSR7_FBEIE 0x00002000 /* fatal bus error */
79 #define CSR7_AIE 0x00008000 /* abnormal enable */
80 #define CSR7_NIE 0x00010000 /* normal enable */
81
82 #define DEFAULT_COMMON_INTERRUPTS (CSR7_TCIE|CSR7_TPSIE|CSR7_RUIE|CSR7_RSIE|\
83 CSR7_FBEIE|CSR7_AIE|CSR7_NIE)
84
85 #define DEFAULT_NORMAL_INTERRUPTS (DEFAULT_COMMON_INTERRUPTS|CSR7_TDUIE)
86
87 #define DEFAULT_NO_IDLE_INTERRUPTS (DEFAULT_COMMON_INTERRUPTS|CSR7_RCIE)
88
89 #define CSR9 0x0048
90 #define CSR9_MDC 0x00010000
91 #define CSR9_MDO 0x00020000
92 #define CSR9_MMC 0x00040000
93 #define CSR9_MDI 0x00080000
94
95 #define OWN_BIT cpu_to_le32(1 << 31)
96
97 #ifdef CONFIG_VOICEBUS_ECREFERENCE
98
99 /*
100 * These dahdi_fifo_xxx functions are currently only used by the voicebus
101 * drivers, but are named more generally to facilitate moving out in the
102 * future. They probably also could stand to be changed in order to use a
103 * kfifo implementation from the kernel if one is available.
104 *
105 */
106
107 struct dahdi_fifo {
108 size_t total_size;
109 u32 start;
110 u32 end;
111 u8 data[0];
112 };
113
114 static unsigned int dahdi_fifo_used_space(struct dahdi_fifo *fifo)
115 {
116 return (fifo->end >= fifo->start) ? fifo->end - fifo->start :
117 fifo->total_size - fifo->start + fifo->end;
118 }
119
120 unsigned int __dahdi_fifo_put(struct dahdi_fifo *fifo, u8 *data, size_t size)
121 {
122 int newinsertposition;
123 int cpy_one_len, cpy_two_len;
124
125 if ((size + dahdi_fifo_used_space(fifo)) > (fifo->total_size - 1))
126 return -1;
127
128 if ((fifo->end + size) >= fifo->total_size) {
129 cpy_one_len = fifo->total_size - fifo->end;
130 cpy_two_len = fifo->end + size - fifo->total_size;
131 newinsertposition = cpy_two_len;
132 } else {
133 cpy_one_len = size;
134 cpy_two_len = 0;
135 newinsertposition = fifo->end + size;
136 }
137
138 memcpy(&fifo->data[fifo->end], data, cpy_one_len);
139
140 if (cpy_two_len)
141 memcpy(&fifo->data[0], &data[cpy_one_len], cpy_two_len);
142
143 fifo->end = newinsertposition;
144
145 return size;
146 }
147 EXPORT_SYMBOL(__dahdi_fifo_put);
148
149 unsigned int __dahdi_fifo_get(struct dahdi_fifo *fifo, u8 *data, size_t size)
150 {
151 int newbegin;
152 int cpy_one_len, cpy_two_len;
153
154 if (size > dahdi_fifo_used_space(fifo))
155 return 0;
156
157 if ((fifo->start + size) >= fifo->total_size) {
158 cpy_one_len = fifo->total_size - fifo->start;
159 cpy_two_len = fifo->start + size - fifo->total_size;
160 newbegin = cpy_two_len;
161 } else {
162 cpy_one_len = size;
163 cpy_two_len = 0;
164 newbegin = fifo->start + size;
165 }
166
167 memcpy(&data[0], &fifo->data[fifo->start], cpy_one_len);
168
169 if (cpy_two_len)
170 memcpy(&data[cpy_one_len], &fifo->data[0], cpy_two_len);
171
172 fifo->start = newbegin;
173
174 return size;
175 }
176 EXPORT_SYMBOL(__dahdi_fifo_get);
177
178 void dahdi_fifo_free(struct dahdi_fifo *fifo)
179 {
180 kfree(fifo);
181 }
182 EXPORT_SYMBOL(dahdi_fifo_free);
183
184 struct dahdi_fifo *dahdi_fifo_alloc(u32 maxsize, gfp_t alloc_flags)
185 {
186 struct dahdi_fifo *fifo;
187
188 fifo = kmalloc(maxsize + sizeof(*fifo) + 1, alloc_flags);
189
190 if (!fifo)
191 return NULL;
192
193 fifo->start = fifo->end = 0;
194 fifo->total_size = maxsize + 1;
195
196 return fifo;
197 }
198 EXPORT_SYMBOL(dahdi_fifo_alloc);
199 #endif /* CONFIG_VOICEBUS_ECREFERENCE */
200
201
202 /* In memory structure shared by the host and the adapter. */
203 struct voicebus_descriptor {
204 volatile __le32 des0;
205 volatile __le32 des1;
206 volatile __le32 buffer1;
207 volatile __le32 container; /* Unused */
208 } __attribute__((packed));
209
210 static inline void
211 handle_transmit(struct voicebus *vb, struct list_head *buffers)
212 {
213 vb->ops->handle_transmit(vb, buffers);
214 }
215
216 static inline void
217 handle_receive(struct voicebus *vb, struct list_head *buffers)
218 {
219 vb->ops->handle_receive(vb, buffers);
220 }
221
222 static inline struct voicebus_descriptor *
223 vb_descriptor(const struct voicebus_descriptor_list *dl,
224 const unsigned int index)
225 {
226 struct voicebus_descriptor *d;
227 d = (struct voicebus_descriptor *)((u8*)dl->desc +
228 ((sizeof(*d) + dl->padding) * index));
229 return d;
230 }
231
232 static int
233 vb_initialize_descriptors(struct voicebus *vb, struct voicebus_descriptor_list *dl,
234 u32 des1, unsigned int direction)
235 {
236 int i;
237 struct voicebus_descriptor *d;
238 const u32 END_OF_RING = 0x02000000;
239 u8 cacheline_size;
240
241 BUG_ON(!dl);
242
243 /*
244 * Add some padding to each descriptor to ensure that they are
245 * aligned on host system cache-line boundaries, but only for the
246 * cache-line sizes that we support.
247 *
248 */
249 if (pci_read_config_byte(vb->pdev, PCI_CACHE_LINE_SIZE,
250 &cacheline_size)) {
251 dev_err(&vb->pdev->dev, "Failed read of cache line "
252 "size from PCI configuration space.\n");
253 return -EIO;
254 }
255
256 if ((0x08 == cacheline_size) || (0x10 == cacheline_size) ||
257 (0x20 == cacheline_size)) {
258 dl->padding = (cacheline_size*sizeof(u32)) - sizeof(*d);
259 } else {
260 dl->padding = 0;
261 }
262
263 dl->desc = pci_alloc_consistent(vb->pdev,
264 (sizeof(*d) + dl->padding) * DRING_SIZE, &dl->desc_dma);
265 if (!dl->desc)
266 return -ENOMEM;
267
268 memset(dl->desc, 0, (sizeof(*d) + dl->padding) * DRING_SIZE);
269 for (i = 0; i < DRING_SIZE; ++i) {
270 d = vb_descriptor(dl, i);
271 d->des1 = cpu_to_le32(des1);
272 }
273 d->des1 |= cpu_to_le32(END_OF_RING);
274 dl->count = 0;
275 return 0;
276 }
277
278 #define OWNED(_d_) (((_d_)->des0)&OWN_BIT)
279 #define SET_OWNED(_d_) do { wmb(); (_d_)->des0 |= OWN_BIT; wmb(); } while (0)
280
281 static int
282 vb_initialize_tx_descriptors(struct voicebus *vb)
283 {
284 int i;
285 int des1 = 0xe4800000 | VOICEBUS_SFRAME_SIZE;
286 struct voicebus_descriptor *d;
287 struct voicebus_descriptor_list *dl = &vb->txd;
288 const u32 END_OF_RING = 0x02000000;
289 u8 cacheline_size;
290
291 WARN_ON(!dl);
292 WARN_ON((NULL == vb->idle_vbb) || (0 == vb->idle_vbb_dma_addr));
293
294 /*
295 * Add some padding to each descriptor to ensure that they are
296 * aligned on host system cache-line boundaries, but only for the
297 * cache-line sizes that we support.
298 *
299 */
300 if (pci_read_config_byte(vb->pdev, PCI_CACHE_LINE_SIZE,
301 &cacheline_size)) {
302 dev_err(&vb->pdev->dev, "Failed read of cache line "
303 "size from PCI configuration space.\n");
304 return -EIO;
305 }
306
307 if ((0x08 == cacheline_size) || (0x10 == cacheline_size) ||
308 (0x20 == cacheline_size)) {
309 dl->padding = (cacheline_size*sizeof(u32)) - sizeof(*d);
310 } else {
311 dl->padding = 0;
312 }
313
314 dl->desc = pci_alloc_consistent(vb->pdev,
315 (sizeof(*d) + dl->padding) *
316 DRING_SIZE, &dl->desc_dma);
317 if (!dl->desc)
318 return -ENOMEM;
319
320 memset(dl->desc, 0, (sizeof(*d) + dl->padding) * DRING_SIZE);
321
322 for (i = 0; i < DRING_SIZE; ++i) {
323 d = vb_descriptor(dl, i);
324 d->des1 = cpu_to_le32(des1);
325 dl->pending[i] = NULL;
326 d->buffer1 = 0;
327 }
328 d->des1 |= cpu_to_le32(END_OF_RING);
329 dl->count = 0;
330 return 0;
331 }
332
333 static int
334 vb_initialize_rx_descriptors(struct voicebus *vb)
335 {
336 return vb_initialize_descriptors(
337 vb, &vb->rxd, VOICEBUS_SFRAME_SIZE, DMA_FROM_DEVICE);
338 }
339
340 /*! \brief Use to set the minimum number of buffers queued to the hardware
341 * before enabling interrupts.
342 */
343 int
344 voicebus_set_minlatency(struct voicebus *vb, unsigned int ms)
345 {
346 unsigned long flags;
347 /*
348 * One millisecond of latency means that we have 3 buffers pending,
349 * since two are always going to be waiting in the TX fifo on the
350 * interface chip.
351 *
352 */
353 #define MESSAGE "%d ms is an invalid value for minumum latency. Setting to %d ms.\n"
354 if (DRING_SIZE < ms) {
355 dev_warn(&vb->pdev->dev, MESSAGE, ms, DRING_SIZE);
356 return -EINVAL;
357 } else if (VOICEBUS_DEFAULT_LATENCY > ms) {
358 dev_warn(&vb->pdev->dev, MESSAGE, ms, VOICEBUS_DEFAULT_LATENCY);
359 return -EINVAL;
360 }
361 spin_lock_irqsave(&vb->lock, flags);
362 vb->min_tx_buffer_count = ms;
363 spin_unlock_irqrestore(&vb->lock, flags);
364 return 0;
365 }
366 EXPORT_SYMBOL(voicebus_set_minlatency);
367
368 /*! \brief Returns the number of buffers currently on the transmit queue. */
369 int
370 voicebus_current_latency(struct voicebus *vb)
371 {
372 int latency;
373 unsigned long flags;
374 spin_lock_irqsave(&vb->lock, flags);
375 latency = vb->min_tx_buffer_count;
376 spin_unlock_irqrestore(&vb->lock, flags);
377 return latency;
378 }
379 EXPORT_SYMBOL(voicebus_current_latency);
380
381
382 /*!
383 * \brief Read one of the hardware control registers without acquiring locks.
384 */
385 static inline u32
386 __vb_getctl(struct voicebus *vb, u32 addr)
387 {
388 u32 ret;
389 ret = readl(vb->iobase + addr);
390 rmb();
391 return ret;
392 }
393
394 /*!
395 * \brief Read one of the hardware control registers with locks held.
396 */
397 static inline u32
398 vb_getctl(struct voicebus *vb, u32 addr)
399 {
400 unsigned long flags;
401 u32 val;
402 spin_lock_irqsave(&vb->lock, flags);
403 val = __vb_getctl(vb, addr);
404 spin_unlock_irqrestore(&vb->lock, flags);
405 return val;
406 }
407
408 static int
409 __vb_is_stopped(struct voicebus *vb)
410 {
411 u32 reg;
412 reg = __vb_getctl(vb, SR_CSR5);
413 reg = (reg >> 17) & 0x3f;
414 return ((0 == reg) || (3 == reg)) ? 1 : 0;
415 }
416 /*!
417 * \brief Returns whether or not the interface is running.
418 *
419 * NOTE: Running in this case means whether or not the hardware reports the
420 * transmit processor in any state but stopped.
421 *
422 * \return 1 of the process is stopped, 0 if running.
423 */
424 static int
425 vb_is_stopped(struct voicebus *vb)
426 {
427 int ret;
428 unsigned long flags;
429 spin_lock_irqsave(&vb->lock, flags);
430 ret = __vb_is_stopped(vb);
431 spin_unlock_irqrestore(&vb->lock, flags);
432 return ret;
433 }
434
435 #if defined(CONFIG_VOICEBUS_INTERRUPT)
436
437 static inline void vb_disable_deferred(struct voicebus *vb)
438 {
439 if (atomic_inc_return(&vb->deferred_disabled_count) == 1)
440 disable_irq(vb->pdev->irq);
441 }
442
443 static inline void vb_enable_deferred(struct voicebus *vb)
444 {
445 if (atomic_dec_return(&vb->deferred_disabled_count) == 0)
446 enable_irq(vb->pdev->irq);
447 }
448
449 #else
450
451 static inline void vb_disable_deferred(struct voicebus *vb)
452 {
453 tasklet_disable(&vb->tasklet);
454 }
455
456 static inline void vb_enable_deferred(struct voicebus *vb)
457 {
458 tasklet_enable(&vb-tasklet);
459 }
460
461 #endif
462
463 static void
464 vb_cleanup_tx_descriptors(struct voicebus *vb)
465 {
466 unsigned int i;
467 struct voicebus_descriptor_list *dl = &vb->txd;
468 struct voicebus_descriptor *d;
469 struct vbb *vbb;
470
471 vb_disable_deferred(vb);
472
473 while (!list_empty(&vb->tx_complete)) {
474 vbb = list_entry(vb->tx_complete.next, struct vbb, entry);
475 list_del(&vbb->entry);
476 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
477 }
478
479 for (i = 0; i < DRING_SIZE; ++i) {
480 d = vb_descriptor(dl, i);
481 if (d->buffer1 &&
482 (d->buffer1 != le32_to_cpu(vb->idle_vbb_dma_addr))) {
483 WARN_ON(!dl->pending[i]);
484 vbb = dl->pending[i];
485 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
486 }
487 if (NORMAL == vb->mode) {
488 d->des1 |= cpu_to_le32(0x80000000);
489 d->buffer1 = cpu_to_le32(vb->idle_vbb_dma_addr);
490 dl->pending[i] = vb->idle_vbb;
491 SET_OWNED(d);
492 } else {
493 d->buffer1 = 0;
494 dl->pending[i] = NULL;
495 d->des0 &= ~OWN_BIT;
496 }
497 }
498
499 dl->head = dl->tail = 0;
500 dl->count = 0;
501 vb_enable_deferred(vb);
502 }
503
504 static void vb_cleanup_rx_descriptors(struct voicebus *vb)
505 {
506 unsigned int i;
507 struct voicebus_descriptor_list *dl = &vb->rxd;
508 struct voicebus_descriptor *d;
509 struct vbb *vbb;
510
511 vb_disable_deferred(vb);
512 for (i = 0; i < DRING_SIZE; ++i) {
513 d = vb_descriptor(dl, i);
514 if (d->buffer1) {
515 d->buffer1 = 0;
516 BUG_ON(!dl->pending[i]);
517 vbb = dl->pending[i];
518 list_add_tail(&vbb->entry, &vb->free_rx);
519 dl->pending[i] = NULL;
520 }
521 d->des0 &= ~OWN_BIT;
522 }
523 dl->head = 0;
524 dl->tail = 0;
525 dl->count = 0;
526 vb_enable_deferred(vb);
527 }
528
529 static void vb_cleanup_descriptors(struct voicebus *vb,
530 struct voicebus_descriptor_list *dl)
531 {
532 if (dl == &vb->txd)
533 vb_cleanup_tx_descriptors(vb);
534 else
535 vb_cleanup_rx_descriptors(vb);
536 }
537
538 static void
539 vb_free_descriptors(struct voicebus *vb, struct voicebus_descriptor_list *dl)
540 {
541 struct vbb *vbb;
542 if (NULL == dl->desc) {
543 WARN_ON(1);
544 return;
545 }
546 vb_cleanup_descriptors(vb, dl);
547 pci_free_consistent(
548 vb->pdev,
549 (sizeof(struct voicebus_descriptor)+dl->padding)*DRING_SIZE,
550 dl->desc, dl->desc_dma);
551 while (!list_empty(&vb->free_rx)) {
552 vbb = list_entry(vb->free_rx.next, struct vbb, entry);
553 list_del(&vbb->entry);
554 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
555 }
556 }
557
558 /*!
559 * \brief Write one of the hardware control registers without acquiring locks.
560 */
561 static inline void
562 __vb_setctl(struct voicebus *vb, u32 addr, u32 val)
563 {
564 wmb();
565 writel(val, vb->iobase + addr);
566 readl(vb->iobase + addr);
567 }
568
569 /*!
570 * \brief Write one of the hardware control registers with locks held.
571 */
572 static inline void
573 vb_setctl(struct voicebus *vb, u32 addr, u32 val)
574 {
575 unsigned long flags;
576 spin_lock_irqsave(&vb->lock, flags);
577 __vb_setctl(vb, addr, val);
578 spin_unlock_irqrestore(&vb->lock, flags);
579 }
580
581 static int
582 __vb_sdi_clk(struct voicebus *vb, u32 *sdi)
583 {
584 unsigned int ret;
585 *sdi &= ~CSR9_MDC;
586 __vb_setctl(vb, 0x0048, *sdi);
587 ret = __vb_getctl(vb, 0x0048);
588 *sdi |= CSR9_MDC;
589 __vb_setctl(vb, 0x0048, *sdi);
590 return (ret & CSR9_MDI) ? 1 : 0;
591 }
592
593 static void
594 __vb_sdi_sendbits(struct voicebus *vb, u32 bits, int count, u32 *sdi)
595 {
596 *sdi &= ~CSR9_MMC;
597 __vb_setctl(vb, 0x0048, *sdi);
598 while (count--) {
599
600 if (bits & (1 << count))
601 *sdi |= CSR9_MDO;
602 else
603 *sdi &= ~CSR9_MDO;
604
605 __vb_sdi_clk(vb, sdi);
606 }
607 }
608
609 static void
610 vb_setsdi(struct voicebus *vb, int addr, u16 val)
611 {
612 u32 bits;
613 u32 sdi = 0;
614 unsigned long flags;
615 /* Send preamble */
616 bits = 0xffffffff;
617 spin_lock_irqsave(&vb->lock, flags);
618 __vb_sdi_sendbits(vb, bits, 32, &sdi);
619 bits = (0x5 << 12) | (1 << 7) | (addr << 2) | 0x2;
620 __vb_sdi_sendbits(vb, bits, 16, &sdi);
621 __vb_sdi_sendbits(vb, val, 16, &sdi);
622 spin_unlock_irqrestore(&vb->lock, flags);
623 }
624
625 /*! \brief Resets the voicebus hardware interface. */
626 static int
627 vb_reset_interface(struct voicebus *vb)
628 {
629 unsigned long timeout;
630 u32 reg;
631 u32 pci_access;
632 enum {
633 /* Software Reset */
634 SWR = (1 << 0),
635 /* Bus Arbitration (1 for priority transmit) */
636 BAR = (1 << 1),
637 /* Memory Write Invalidate */
638 MWI = (1 << 24),
639 /* Memory Read Line */
640 MRL = (1 << 23),
641 /* Descriptor Skip Length */
642 DSLShift = 2,
643 /* Cache Alignment */
644 CALShift = 14,
645 /* Transmit Auto Pollling */
646 TAPShift = 17,
647 };
648 const u32 DEFAULT_PCI_ACCESS = MWI | MRL | (0x2 << TAPShift) | BAR;
649 u8 cacheline_size;
650 BUG_ON(in_interrupt());
651
652 if (pci_read_config_byte(vb->pdev, PCI_CACHE_LINE_SIZE,
653 &cacheline_size)) {
654 dev_err(&vb->pdev->dev, "Failed read of cache line "
655 "size from PCI configuration space.\n");
656 return -EIO;
657 }
658
659 switch (cacheline_size) {
660 case 0x08:
661 pci_access = DEFAULT_PCI_ACCESS | (0x1 << CALShift);
662 break;
663 case 0x10:
664 pci_access = DEFAULT_PCI_ACCESS | (0x2 << CALShift);
665 break;
666 case 0x20:
667 pci_access = DEFAULT_PCI_ACCESS | (0x3 << CALShift);
668 break;
669 default:
670 if (*vb->debug) {
671 dev_warn(&vb->pdev->dev, "Host system set a cache "
672 "size of %d which is not supported. "
673 "Disabling memory write line and memory "
674 "read line.\n", cacheline_size);
675 }
676 pci_access = 0xfe584202;
677 break;
678 }
679
680 /* The transmit and receive descriptors will have the same padding. */
681 pci_access |= ((vb->txd.padding / sizeof(u32)) << DSLShift) & 0x7c;
682
683 vb_setctl(vb, 0x0000, pci_access | SWR);
684
685 timeout = jiffies + HZ/10; /* 100ms interval */
686 do {
687 reg = vb_getctl(vb, 0x0000);
688 } while ((reg & SWR) && time_before(jiffies, timeout));
689
690 if (reg & SWR) {
691 if (-1 == reg) {
692 dev_err(&vb->pdev->dev,
693 "Unable to read I/O registers.\n");
694 } else {
695 dev_err(&vb->pdev->dev, "Did not come out of reset "
696 "within 100ms\n");
697 }
698 return -EIO;
699 }
700
701 vb_setctl(vb, 0x0000, pci_access);
702
703 return 0;
704 }
705
706 /*!
707 * \brief Give a frame to the hardware to use for receiving.
708 *
709 */
710 static inline int
711 vb_submit_rxb(struct voicebus *vb, struct vbb *vbb)
712 {
713 struct voicebus_descriptor *d;
714 struct voicebus_descriptor_list *dl = &vb->rxd;
715 unsigned int tail = dl->tail;
716
717 d = vb_descriptor(dl, tail);
718
719 if (unlikely(d->buffer1)) {
720 /* Do not overwrite a buffer that is still in progress. */
721 WARN_ON(1);
722 list_add_tail(&vbb->entry, &vb->free_rx);
723 return -EBUSY;
724 }
725
726 dl->pending[tail] = vbb;
727 dl->tail = (++tail) & DRING_MASK;
728 d->buffer1 = cpu_to_le32(vbb->dma_addr);
729 SET_OWNED(d); /* That's it until the hardware is done with it. */
730 ++dl->count;
731 return 0;
732 }
733
734 static int __voicebus_transmit(struct voicebus *vb, struct vbb *vbb)
735 {
736 struct voicebus_descriptor *d;
737 struct voicebus_descriptor_list *dl = &vb->txd;
738
739 d = vb_descriptor(dl, dl->tail);
740
741 if (unlikely((le32_to_cpu(d->buffer1) != vb->idle_vbb_dma_addr) &&
742 d->buffer1)) {
743 if (printk_ratelimit())
744 dev_warn(&vb->pdev->dev, "Dropping tx buffer buffer\n");
745 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
746 /* Schedule the underrun handler to run here, since we'll need
747 * to cleanup as best we can. */
748 schedule_work(&vb->underrun_work);
749 return -EFAULT;
750 }
751
752 dl->pending[dl->tail] = vbb;
753 d->buffer1 = cpu_to_le32(vbb->dma_addr);
754 dl->tail = (dl->tail + 1) & DRING_MASK;
755 SET_OWNED(d); /* That's it until the hardware is done with it. */
756 ++dl->count;
757 return 0;
758 }
759
760 /**
761 * voicebus_transmit - Queue a buffer on the hardware descriptor ring.
762 *
763 */
764 int voicebus_transmit(struct voicebus *vb, struct vbb *vbb)
765 {
766 int res = __voicebus_transmit(vb, vbb);
767 __vb_setctl(vb, 0x0008, 0x00000000);
768 return res;
769 }
770 EXPORT_SYMBOL(voicebus_transmit);
771
772
773 /*!
774 * \brief Instruct the hardware to check for a new tx descriptor.
775 */
776 static inline void
777 __vb_tx_demand_poll(struct voicebus *vb)
778 {
779 u32 status = __vb_getctl(vb, 0x0028);
780 if ((status & 0x00700000) == 0x00600000)
781 __vb_setctl(vb, 0x0008, 0x00000000);
782 }
783
784 static void setup_descriptors(struct voicebus *vb)
785 {
786 int i;
787 struct vbb *vbb;
788 dma_addr_t dma_addr;
789 LIST_HEAD(buffers);
790 unsigned long flags;
791
792 might_sleep();
793
794 vb_cleanup_tx_descriptors(vb);
795 vb_cleanup_rx_descriptors(vb);
796
797 /* Tell the card where the descriptors are in host memory. */
798 vb_setctl(vb, 0x0020, (u32)vb->txd.desc_dma);
799 vb_setctl(vb, 0x0018, (u32)vb->rxd.desc_dma);
800
801 for (i = 0; i < DRING_SIZE; ++i) {
802 if (list_empty(&vb->free_rx)) {
803 vbb = dma_pool_alloc(vb->pool, GFP_KERNEL,
804 &dma_addr);
805 if (vbb)
806 vbb->dma_addr = dma_addr;
807 } else {
808 vbb = list_entry(vb->free_rx.next, struct vbb, entry);
809 list_del(&vbb->entry);
810 }
811 if (unlikely(NULL == vbb))
812 BUG_ON(1);
813 list_add_tail(&vbb->entry, &buffers);
814 }
815
816 vb_disable_deferred(vb);
817 while (!list_empty(&buffers)) {
818 vbb = list_entry(buffers.next, struct vbb, entry);
819 list_del(&vbb->entry);
820 vb_submit_rxb(vb, vbb);
821 }
822 vb_enable_deferred(vb);
823
824 if (BOOT != vb->mode) {
825 for (i = 0; i < vb->min_tx_buffer_count; ++i) {
826 vbb = dma_pool_alloc(vb->pool, GFP_KERNEL,
827 &dma_addr);
828 if (unlikely(NULL == vbb))
829 BUG_ON(1);
830
831 vbb->dma_addr = dma_addr;
832 list_add_tail(&vbb->entry, &buffers);
833 }
834
835 local_irq_save(flags);
836 handle_transmit(vb, &buffers);
837 local_irq_restore(flags);
838
839 vb_disable_deferred(vb);
840 while (!list_empty(&buffers)) {
841 vbb = list_entry(buffers.next, struct vbb, entry);
842 list_del_init(&vbb->entry);
843 __voicebus_transmit(vb, vbb);
844 }
845 __vb_setctl(vb, 0x0008, 0x00000000);
846 vb_enable_deferred(vb);
847 }
848
849 }
850
851 static void
852 __vb_set_control_defaults(struct voicebus *vb)
853 {
854 /* Pass bad packets, runt packets, disable SQE function,
855 * store-and-forward */
856 vb_setctl(vb, 0x0030, 0x00280048);
857 /* ...disable jabber and the receive watchdog. */
858 vb_setctl(vb, 0x0078, 0x00000013);
859 vb_getctl(vb, 0x0078);
860 }
861
862 static void __vb_set_mac_only_mode(struct voicebus *vb)
863 {
864 u32 reg;
865 reg = __vb_getctl(vb, 0x00fc);
866 __vb_setctl(vb, 0x00fc, (reg & ~0x7) | 0x4);
867 __vb_getctl(vb, 0x00fc);
868 }
869
870 static int
871 vb_initialize_interface(struct voicebus *vb)
872 {
873 u32 reg;
874
875 setup_descriptors(vb);
876
877 __vb_set_control_defaults(vb);
878
879 reg = vb_getctl(vb, 0x00fc);
880 vb_setctl(vb, 0x00fc, (reg & ~0x7) | 0x7);
881 vb_setsdi(vb, 0x00, 0x0100);
882 vb_setsdi(vb, 0x16, 0x2100);
883
884 __vb_set_mac_only_mode(vb);
885 vb_setsdi(vb, 0x00, 0x0100);
886 vb_setsdi(vb, 0x16, 0x2100);
887 reg = vb_getctl(vb, 0x00fc);
888
889 /*
890 * The calls to setsdi above toggle the reset line of the CPLD. Wait
891 * here to give the CPLD time to stabilize after reset.
892 */
893 msleep(10);
894
895 return ((reg&0x7) == 0x4) ? 0 : -EIO;
896 }
897
898 #ifdef DBG
899 static void
900 dump_descriptor(struct voicebus *vb, struct voicebus_descriptor *d)
901 {
902 VB_PRINTK(vb, DEBUG, "Displaying descriptor at address %08x\n", (unsigned int)d);
903 VB_PRINTK(vb, DEBUG, " des0: %08x\n", le32_to_cpu(d->des0));
904 VB_PRINTK(vb, DEBUG, " des1: %08x\n", le32_to_cpu(d->des1));
905 VB_PRINTK(vb, DEBUG, " buffer1: %08x\n", le32_to_cpu(d->buffer1));
906 VB_PRINTK(vb, DEBUG, " container: %08x\n", le32_to_cpu(d->container));
907 }
908
909 static void
910 show_buffer(struct voicebus *vb, struct vbb *vbb)
911 {
912 int x;
913 unsigned char *c;
914 c = vbb;
915 printk(KERN_DEBUG "Packet %d\n", count);
916 printk(KERN_DEBUG "");
917 for (x = 1; x <= VOICEBUS_SFRAME_SIZE; ++x) {
918 printk("%02x ", c[x]);
919 if (x % 16 == 0)
920 printk("\n");
921 }
922 printk(KERN_DEBUG "\n\n");
923 }
924 #endif
925
926 /*!
927 * \brief Remove the next completed transmit buffer (txb) from the tx
928 * descriptor ring.
929 *
930 * NOTE: This function doesn't need any locking because only one instance is
931 * ever running on the deferred processing routine and it only looks at
932 * the head pointer. The deferred routine should only ever be running
933 * on one processor at a time (no multithreaded workqueues allowed!)
934 *
935 * Context: Must be called from the voicebus deferred workqueue.
936 *
937 * \return Pointer to buffer, or NULL if not available.
938 */
939 static void *
940 vb_get_completed_txb(struct voicebus *vb)
941 {
942 struct voicebus_descriptor_list *dl = &vb->txd;
943 struct voicebus_descriptor *d;
944 struct vbb *vbb;
945 unsigned int head = dl->head;
946
947 d = vb_descriptor(dl, head);
948
949 if (OWNED(d) || !d->buffer1 ||
950 (le32_to_cpu(d->buffer1) == vb->idle_vbb_dma_addr))
951 return NULL;
952
953 vbb = dl->pending[head];
954 if (NORMAL == vb->mode) {
955 d->buffer1 = cpu_to_le32(vb->idle_vbb_dma_addr);
956 dl->pending[head] = vb->idle_vbb;
957 SET_OWNED(d);
958 } else {
959 d->buffer1 = 0;
960 dl->pending[head] = NULL;
961 }
962 dl->head = (++head) & DRING_MASK;
963 --dl->count;
964 vb_net_capture_vbb(vb, vbb, 1, d->des0, d->container);
965 return vbb;
966 }
967
968 static void *
969 vb_get_completed_rxb(struct voicebus *vb, u32 *des0)
970 {
971 struct voicebus_descriptor *d;
972 struct voicebus_descriptor_list *dl = &vb->rxd;
973 unsigned int head = dl->head;
974 struct vbb *vbb;
975
976 d = vb_descriptor(dl, head);
977
978 if ((0 == d->buffer1) || OWNED(d))
979 return NULL;
980
981 vbb = dl->pending[head];
982 dl->head = (++head) & DRING_MASK;
983 d->buffer1 = 0;
984 --dl->count;
985 # ifdef VOICEBUS_NET_DEBUG
986 vb_net_capture_vbb(vb, vbb, 0, d->des0, d->container);
987 # endif
988 *des0 = le32_to_cpu(d->des0);
989 return vbb;
990 }
991
992 /*!
993 * \brief Command the hardware to check if it owns the next receive
994 * descriptor.
995 */
996 static inline void
997 __vb_rx_demand_poll(struct voicebus *vb)
998 {
999 if (((__vb_getctl(vb, 0x0028) >> 17) & 0x7) == 0x4)
1000 __vb_setctl(vb, 0x0010, 0x00000000);
1001 }
1002
1003 static void
1004 __vb_enable_interrupts(struct voicebus *vb)
1005 {
1006 if (BOOT == vb->mode)
1007 __vb_setctl(vb, IER_CSR7, DEFAULT_NO_IDLE_INTERRUPTS);
1008 else
1009 __vb_setctl(vb, IER_CSR7, DEFAULT_NORMAL_INTERRUPTS);
1010 }
1011
1012 static void
1013 __vb_disable_interrupts(struct voicebus *vb)
1014 {
1015 __vb_setctl(vb, IER_CSR7, 0);
1016 }
1017
1018 static void
1019 vb_disable_interrupts(struct voicebus *vb)
1020 {
1021 unsigned long flags;
1022 spin_lock_irqsave(&vb->lock, flags);
1023 __vb_disable_interrupts(vb);
1024 spin_unlock_irqrestore(&vb->lock, flags);
1025 }
1026
1027 static void start_packet_processing(struct voicebus *vb)
1028 {
1029 u32 reg;
1030 unsigned long flags;
1031
1032 spin_lock_irqsave(&vb->lock, flags);
1033 clear_bit(VOICEBUS_STOP, &vb->flags);
1034 clear_bit(VOICEBUS_STOPPED, &vb->flags);
1035 #if defined(CONFIG_VOICEBUS_TIMER)
1036 vb->timer.expires = jiffies + HZ/1000;
1037 add_timer(&vb->timer);
1038 #else
1039 /* Clear the interrupt status register. */
1040 __vb_setctl(vb, SR_CSR5, 0xffffffff);
1041 __vb_enable_interrupts(vb);
1042 #endif
1043 /* Start the transmit and receive processors. */
1044 reg = __vb_getctl(vb, 0x0030);
1045 __vb_setctl(vb, 0x0030, reg|0x00002002);
1046 __vb_getctl(vb, 0x0030);
1047 __vb_rx_demand_poll(vb);
1048 __vb_tx_demand_poll(vb);
1049 __vb_getctl(vb, 0x0030);
1050 spin_unlock_irqrestore(&vb->lock, flags);
1051 }
1052
1053 static void vb_tasklet_boot(unsigned long data);
1054 static void vb_tasklet_hx8(unsigned long data);
1055 static void vb_tasklet_normal(unsigned long data);
1056
1057 /*!
1058 * \brief Starts the VoiceBus interface.
1059 *
1060 * When the VoiceBus interface is started, it is actively transferring
1061 * frames to and from the backend of the card. This means the card will
1062 * generate interrupts.
1063 *
1064 * This function should only be called from process context, with interrupts
1065 * enabled, since it can sleep while running the self checks.
1066 *
1067 * \return zero on success. -EBUSY if device is already running.
1068 */
1069 int
1070 voicebus_start(struct voicebus *vb)
1071 {
1072 int ret;
1073
1074 if (!vb_is_stopped(vb))
1075 return -EBUSY;
1076
1077 if (NORMAL == vb->mode) {
1078 tasklet_init(&vb->tasklet, vb_tasklet_normal,
1079 (unsigned long)vb);
1080 } else if (BOOT == vb->mode) {
1081 tasklet_init(&vb->tasklet, vb_tasklet_boot,
1082 (unsigned long)vb);
1083 } else if (HX8 == vb->mode) {
1084 tasklet_init(&vb->tasklet, vb_tasklet_hx8,
1085 (unsigned long)vb);
1086 } else {
1087 return -EINVAL;
1088 }
1089
1090 ret = vb_reset_interface(vb);
1091 if (ret)
1092 return ret;
1093 ret = vb_initialize_interface(vb);
1094 if (ret)
1095 return ret;
1096
1097 start_packet_processing(vb);
1098
1099 BUG_ON(vb_is_stopped(vb));
1100
1101 return 0;
1102 }
1103 EXPORT_SYMBOL(voicebus_start);
1104
1105 static void vb_stop_txrx_processors(struct voicebus *vb)
1106 {
1107 unsigned long flags;
1108 u32 reg;
1109 int i;
1110
1111 spin_lock_irqsave(&vb->lock, flags);
1112 reg = __vb_getctl(vb, NAR_CSR6);
1113 reg &= ~(0x2002);
1114 __vb_setctl(vb, NAR_CSR6, reg);
1115 spin_unlock_irqrestore(&vb->lock, flags);
1116
1117 barrier();
1118 i = 150;
1119 while (--i && (__vb_getctl(vb, SR_CSR5) & (0x007e0000)))
1120 udelay(100);
1121 }
1122
1123 /*!
1124 * \brief Stops the VoiceBus interface.
1125 *
1126 * Stops the VoiceBus interface and waits for any outstanding DMA transactions
1127 * to complete. When this functions returns the VoiceBus interface tx and rx
1128 * states will both be suspended.
1129 *
1130 * Only call this function from process context, with interrupt enabled,
1131 * without any locks held since it sleeps.
1132 *
1133 * \return zero on success, -1 on error.
1134 */
1135 void voicebus_stop(struct voicebus *vb)
1136 {
1137 static DEFINE_SEMAPHORE(stop);
1138
1139 down(&stop);
1140
1141 if (test_bit(VOICEBUS_STOP, &vb->flags) || vb_is_stopped(vb)) {
1142 up(&stop);
1143 return;
1144 }
1145
1146 set_bit(VOICEBUS_STOP, &vb->flags);
1147 vb_stop_txrx_processors(vb);
1148
1149 WARN_ON(!vb_is_stopped(vb));
1150 set_bit(VOICEBUS_STOPPED, &vb->flags);
1151
1152 #if defined(CONFIG_VOICEBUS_TIMER)
1153 del_timer_sync(&vb->timer);
1154 #endif
1155 vb_disable_interrupts(vb);
1156 up(&stop);
1157 }
1158 EXPORT_SYMBOL(voicebus_stop);
1159
1160 /**
1161 * voicebus_quiesce - Halt the voicebus interface.
1162 * @vb: The voicebus structure to quiet
1163 *
1164 * This ensures that the device is not engaged in any DMA transactions or
1165 * interrupting. It does not grab any locks since it may be called by a dying
1166 * kernel.
1167 */
1168 void voicebus_quiesce(struct voicebus *vb)
1169 {
1170 if (!vb)
1171 return;
1172
1173 /* Reset the device */
1174 __vb_disable_interrupts(vb);
1175 __vb_setctl(vb, 0x0000, 0x1);
1176 __vb_getctl(vb, 0x0000);
1177 }
1178 EXPORT_SYMBOL(voicebus_quiesce);
1179
1180 /*!
1181 * \brief Prepare the interface for module unload.
1182 *
1183 * Stop the interface and free all the resources allocated by the driver. The
1184 * caller should have returned all VoiceBus buffers to the VoiceBus layer
1185 * before calling this function.
1186 *
1187 * context: !in_interrupt()
1188 */
1189 void
1190 voicebus_release(struct voicebus *vb)
1191 {
1192 set_bit(VOICEBUS_SHUTDOWN, &vb->flags);
1193
1194 #ifdef VOICEBUS_NET_DEBUG
1195 vb_net_unregister(vb);
1196 #endif
1197
1198 /* Make sure the underrun_work isn't running or going to run. */
1199 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
1200 flush_scheduled_work();
1201 #else
1202 cancel_work_sync(&vb->underrun_work);
1203 #endif
1204 /* quiesce the hardware */
1205 voicebus_stop(vb);
1206
1207 vb_reset_interface(vb);
1208
1209 tasklet_kill(&vb->tasklet);
1210
1211 #if !defined(CONFIG_VOICEBUS_TIMER)
1212 free_irq(vb->pdev->irq, vb);
1213 #endif
1214
1215 /* Cleanup memory and software resources. */
1216 vb_free_descriptors(vb, &vb->txd);
1217 vb_free_descriptors(vb, &vb->rxd);
1218 if (vb->idle_vbb_dma_addr) {
1219 dma_free_coherent(&vb->pdev->dev, VOICEBUS_SFRAME_SIZE,
1220 vb->idle_vbb, vb->idle_vbb_dma_addr);
1221 }
1222
1223 release_mem_region(pci_resource_start(vb->pdev, 1),
1224 pci_resource_len(vb->pdev, 1));
1225
1226 pci_iounmap(vb->pdev, vb->iobase);
1227 pci_clear_mwi(vb->pdev);
1228 pci_disable_device(vb->pdev);
1229 dma_pool_destroy(vb->pool);
1230 }
1231 EXPORT_SYMBOL(voicebus_release);
1232
1233 static void
1234 vb_increase_latency(struct voicebus *vb, unsigned int increase,
1235 struct list_head *buffers)
1236 {
1237 struct vbb *vbb;
1238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
1239 struct vbb *n;
1240 #endif
1241 int i;
1242 LIST_HEAD(local);
1243
1244 if (0 == increase)
1245 return;
1246
1247 if (test_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags))
1248 return;
1249
1250 if (unlikely(increase > VOICEBUS_MAXLATENCY_BUMP))
1251 increase = VOICEBUS_MAXLATENCY_BUMP;
1252
1253 if ((increase + vb->min_tx_buffer_count) > vb->max_latency)
1254 increase = vb->max_latency - vb->min_tx_buffer_count;
1255
1256 /* Because there are 2 buffers in the transmit FIFO on the hardware,
1257 * setting 3 ms of latency means that the host needs to be able to
1258 * service the cards within 1ms. This is because the interface will
1259 * load up 2 buffers into the TX FIFO then attempt to read the 3rd
1260 * descriptor. If the OWN bit isn't set, then the hardware will set the
1261 * TX descriptor not available interrupt. */
1262
1263 /* Set the minimum latency in case we're restarted...we don't want to
1264 * wait for the buffer to grow to this depth again in that case. */
1265 for (i = 0; i < increase; ++i) {
1266 dma_addr_t dma_addr;
1267 vbb = dma_pool_alloc(vb->pool, GFP_ATOMIC, &dma_addr);
1268 WARN_ON(NULL == vbb);
1269 if (likely(NULL != vbb)) {
1270 vbb->dma_addr = dma_addr;
1271 list_add_tail(&vbb->entry, &local);
1272 }
1273 }
1274
1275 handle_transmit(vb, &local);
1276 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
1277 list_for_each_entry_safe(vbb, n, &local, entry)
1278 list_move_tail(&vbb->entry, buffers);
1279 #else
1280 list_splice_tail(&local, buffers);
1281 #endif
1282
1283 /* Set the new latency (but we want to ensure that there aren't any
1284 * printks to the console, so we don't call the function) */
1285 vb->min_tx_buffer_count += increase;
1286 }
1287
1288 static void vb_schedule_deferred(struct voicebus *vb)
1289 {
1290 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1291 tasklet_hi_schedule(&vb->tasklet);
1292 #else
1293 vb->tasklet.func(vb->tasklet.data);
1294 #endif
1295 }
1296
1297 /**
1298 * vb_tasklet_boot() - When vb->mode == BOOT
1299 *
1300 * This deferred processing routine is for hx8 boards during initialization. It
1301 * simply services any completed tx / rx packets without any concerns about what
1302 * the current latency is.
1303 */
1304 static void vb_tasklet_boot(unsigned long data)
1305 {
1306 struct voicebus *vb = (struct voicebus *)data;
1307 LIST_HEAD(buffers);
1308 struct vbb *vbb;
1309 const int DEFAULT_COUNT = 5;
1310 int count = DEFAULT_COUNT;
1311 u32 des0 = 0;
1312
1313 /* First, temporarily store any non-idle buffers that the hardware has
1314 * indicated it's finished transmitting. Non idle buffers are those
1315 * buffers that contain actual data and was filled out by the client
1316 * driver (as of this writing, the wcte12xp or wctdm24xxp drivers) when
1317 * passed up through the handle_transmit callback.
1318 *
1319 * On the other hand, idle buffers are "dummy" buffers that solely exist
1320 * to in order to prevent the transmit descriptor ring from ever
1321 * completely draining. */
1322 while ((vbb = vb_get_completed_txb(vb)))
1323 list_add_tail(&vbb->entry, &vb->tx_complete);
1324
1325 while (--count && !list_empty(&vb->tx_complete))
1326 list_move_tail(vb->tx_complete.next, &buffers);
1327
1328 /* Prep all the new buffers for transmit before actually sending any
1329 * of them. */
1330 handle_transmit(vb, &buffers);
1331
1332 while (!list_empty(&buffers)) {
1333 vbb = list_entry(buffers.next, struct vbb, entry);
1334 list_del(&vbb->entry);
1335 __voicebus_transmit(vb, vbb);
1336 }
1337 __vb_setctl(vb, 0x0008, 0x00000000);
1338
1339 /* If there may still be buffers in the descriptor rings, reschedule
1340 * ourself to run again. We essentially yield here to allow any other
1341 * cards a chance to run. */
1342 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1343 if (unlikely(!count && !test_bit(VOICEBUS_STOP, &vb->flags)))
1344 vb_schedule_deferred(vb);
1345 #endif
1346
1347 /* And finally, pass up any receive buffers. */
1348 count = DEFAULT_COUNT;
1349 while (--count && (vbb = vb_get_completed_rxb(vb, &des0))) {
1350 if (likely((des0 & (0x7fff << 16)) ==
1351 (VOICEBUS_SFRAME_SIZE << 16)))
1352 list_add_tail(&vbb->entry, &buffers);
1353 else
1354 vb_submit_rxb(vb, vbb);
1355 }
1356
1357 handle_receive(vb, &buffers);
1358 while (!list_empty(&buffers)) {
1359 vbb = list_entry(buffers.next, struct vbb, entry);
1360 list_del(&vbb->entry);
1361 vb_submit_rxb(vb, vbb);
1362 }
1363 return;
1364 }
1365
1366 /**
1367 * vb_tasklet_hx8() - When vb->mode == HX8
1368 *
1369 * The normal deferred processing routine for the Hx8 boards. This deferred
1370 * processing routine doesn't configure any idle buffers and increases the
1371 * latency when there is a hard underrun. There are not any softunderruns here,
1372 * unlike in vb_tasklet_normal.
1373 */
1374 static void vb_tasklet_hx8(unsigned long data)
1375 {
1376 struct voicebus *vb = (struct voicebus *)data;
1377 int hardunderrun;
1378 LIST_HEAD(buffers);
1379 struct vbb *vbb;
1380 const int DEFAULT_COUNT = 5;
1381 int count = DEFAULT_COUNT;
1382 u32 des0 = 0;
1383
1384 hardunderrun = test_and_clear_bit(VOICEBUS_HARD_UNDERRUN, &vb->flags);
1385 /* First, temporarily store any non-idle buffers that the hardware has
1386 * indicated it's finished transmitting. Non idle buffers are those
1387 * buffers that contain actual data and was filled out by the client
1388 * driver (as of this writing, the wcte12xp or wctdm24xxp drivers) when
1389 * passed up through the handle_transmit callback.
1390 *
1391 * On the other hand, idle buffers are "dummy" buffers that solely exist
1392 * to in order to prevent the transmit descriptor ring from ever
1393 * completely draining. */
1394 while ((vbb = vb_get_completed_txb(vb)))
1395 list_add_tail(&vbb->entry, &vb->tx_complete);
1396
1397 while (--count && !list_empty(&vb->tx_complete))
1398 list_move_tail(vb->tx_complete.next, &buffers);
1399
1400 /* Prep all the new buffers for transmit before actually sending any
1401 * of them. */
1402 handle_transmit(vb, &buffers);
1403
1404 if (unlikely(hardunderrun))
1405 vb_increase_latency(vb, 1, &buffers);
1406
1407 /* Now we can send all our buffers together in a group. */
1408 while (!list_empty(&buffers)) {
1409 vbb = list_entry(buffers.next, struct vbb, entry);
1410 list_del(&vbb->entry);
1411 __voicebus_transmit(vb, vbb);
1412 }
1413 __vb_setctl(vb, 0x0008, 0x00000000);
1414
1415 /* Print any messages about soft latency bumps after we fix the transmit
1416 * descriptor ring. Otherwise it's possible to take so much time
1417 * printing the dmesg output that we lose the lead that we got on the
1418 * hardware, resulting in a hard underrun condition. */
1419 if (unlikely(hardunderrun)) {
1420 #if !defined(CONFIG_VOICEBUS_SYSFS)
1421 if (!test_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags) &&
1422 printk_ratelimit()) {
1423 if (vb->max_latency != vb->min_tx_buffer_count) {
1424 dev_info(&vb->pdev->dev, "Missed interrupt. "
1425 "Increasing latency to %d ms in "
1426 "order to compensate.\n",
1427 vb->min_tx_buffer_count);
1428 } else {
1429 dev_info(&vb->pdev->dev, "ERROR: Unable to "
1430 "service card within %d ms and "
1431 "unable to further increase "
1432 "latency.\n", vb->max_latency);
1433 }
1434 }
1435 #endif
1436 }
1437
1438 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1439 /* If there may still be buffers in the descriptor rings, reschedule
1440 * ourself to run again. We essentially yield here to allow any other
1441 * cards a chance to run. */
1442 if (unlikely(!count && !test_bit(VOICEBUS_STOP, &vb->flags)))
1443 vb_schedule_deferred(vb);
1444 #endif
1445
1446 /* And finally, pass up any receive buffers. */
1447 count = DEFAULT_COUNT;
1448 while (--count && (vbb = vb_get_completed_rxb(vb, &des0))) {
1449 if (((des0 >> 16) & 0x7fff) == VOICEBUS_SFRAME_SIZE)
1450 list_add_tail(&vbb->entry, &buffers);
1451 else
1452 vb_submit_rxb(vb, vbb);
1453 }
1454
1455 handle_receive(vb, &buffers);
1456
1457 while (!list_empty(&buffers)) {
1458 vbb = list_entry(buffers.next, struct vbb, entry);
1459 list_del(&vbb->entry);
1460 vb_submit_rxb(vb, vbb);
1461 }
1462
1463 return;
1464 }
1465
1466 /**
1467 * vb_tasklet_relaxed() - When vb->mode == NORMAL
1468 *
1469 * This is the standard deferred processing routine for CPLD based cards
1470 * (essentially the non-hx8 cards).
1471 */
1472 static void vb_tasklet_normal(unsigned long data)
1473 {
1474 struct voicebus *vb = (struct voicebus *)data;
1475 int softunderrun;
1476 LIST_HEAD(buffers);
1477 struct vbb *vbb;
1478 struct voicebus_descriptor_list *const dl = &vb->txd;
1479 struct voicebus_descriptor *d;
1480 int behind = 0;
1481 const int DEFAULT_COUNT = 5;
1482 int count = DEFAULT_COUNT;
1483 u32 des0 = 0;
1484
1485 BUG_ON(NORMAL != vb->mode);
1486 /* First, temporarily store any non-idle buffers that the hardware has
1487 * indicated it's finished transmitting. Non idle buffers are those
1488 * buffers that contain actual data and was filled out by the client
1489 * driver (as of this writing, the wcte12xp or wctdm24xxp drivers) when
1490 * passed up through the handle_transmit callback.
1491 *
1492 * On the other hand, idle buffers are "dummy" buffers that solely exist
1493 * to in order to prevent the transmit descriptor ring from ever
1494 * completely draining. */
1495 while ((vbb = vb_get_completed_txb(vb)))
1496 list_add_tail(&vbb->entry, &vb->tx_complete);
1497
1498 if (unlikely(dl->count < 2)) {
1499 softunderrun = 1;
1500 d = vb_descriptor(dl, dl->head);
1501 if (1 == dl->count)
1502 return;
1503
1504 behind = 2;
1505 while (!OWNED(d)) {
1506 if (le32_to_cpu(d->buffer1) != vb->idle_vbb_dma_addr)
1507 goto tx_error_exit;
1508 SET_OWNED(d);
1509 dl->head = (dl->head + 1) & DRING_MASK;
1510 d = vb_descriptor(dl, dl->head);
1511 ++behind;
1512 }
1513
1514 } else {
1515 softunderrun = 0;
1516 }
1517
1518 while (--count && !list_empty(&vb->tx_complete))
1519 list_move_tail(vb->tx_complete.next, &buffers);
1520
1521 /* Prep all the new buffers for transmit before actually sending any
1522 * of them. */
1523 handle_transmit(vb, &buffers);
1524
1525 if (unlikely(softunderrun)) {
1526 int i;
1527 unsigned long flags;
1528
1529 /* Disable interrupts on the local processor. We don't want
1530 * the following process interrupted. We're 'racing' against
1531 * the hardware here.... */
1532 local_irq_save(flags);
1533 vb_increase_latency(vb, behind, &buffers);
1534 d = vb_descriptor(dl, dl->head);
1535 while (!OWNED(d)) {
1536 if (le32_to_cpu(d->buffer1) != vb->idle_vbb_dma_addr) {
1537 local_irq_restore(flags);
1538 goto tx_error_exit;
1539 }
1540 SET_OWNED(d);
1541 dl->head = (dl->head + 1) & DRING_MASK;
1542 d = vb_descriptor(dl, dl->head);
1543 ++behind;
1544 }
1545 /* Now we'll get a little further ahead of the hardware. */
1546 for (i = 0; i < 5; ++i) {
1547 d = vb_descriptor(dl, dl->head);
1548 d->buffer1 = cpu_to_le32(vb->idle_vbb_dma_addr);
1549 dl->pending[dl->head] = vb->idle_vbb;
1550 d->des0 |= OWN_BIT;
1551 dl->head = (dl->head + 1) & DRING_MASK;
1552 }
1553 dl->tail = dl->head;
1554 local_irq_restore(flags);
1555 }
1556
1557 d = vb_descriptor(dl, dl->tail);
1558 if (le32_to_cpu(d->buffer1) != vb->idle_vbb_dma_addr)
1559 goto tx_error_exit;
1560
1561 /* Now we can send all our buffers together in a group. */
1562 while (!list_empty(&buffers)) {
1563 vbb = list_entry(buffers.next, struct vbb, entry);
1564 list_del(&vbb->entry);
1565 __voicebus_transmit(vb, vbb);
1566 }
1567
1568 writel(0, vb->iobase + 0x8);
1569
1570 /* Print any messages about soft latency bumps after we fix the transmit
1571 * descriptor ring. Otherwise it's possible to take so much time
1572 * printing the dmesg output that we lose the lead that we got on the
1573 * hardware, resulting in a hard underrun condition. */
1574 if (unlikely(softunderrun)) {
1575 #if !defined(CONFIG_VOICEBUS_SYSFS)
1576 if (!test_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags) &&
1577 printk_ratelimit()) {
1578 if (vb->max_latency != vb->min_tx_buffer_count) {
1579 dev_info(&vb->pdev->dev, "Missed interrupt. "
1580 "Increasing latency to %d ms in "
1581 "order to compensate.\n",
1582 vb->min_tx_buffer_count);
1583 } else {
1584 dev_info(&vb->pdev->dev, "ERROR: Unable to "
1585 "service card within %d ms and "
1586 "unable to further increase "
1587 "latency.\n", vb->max_latency);
1588 }
1589 }
1590 #endif
1591 }
1592
1593 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1594 /* If there may still be buffers in the descriptor rings, reschedule
1595 * ourself to run again. We essentially yield here to allow any other
1596 * cards a chance to run. */
1597 if (unlikely(!count && !test_bit(VOICEBUS_STOP, &vb->flags)))
1598 vb_schedule_deferred(vb);
1599 #endif
1600
1601 /* And finally, pass up any receive buffers. */
1602 count = DEFAULT_COUNT;
1603 while (--count && (vbb = vb_get_completed_rxb(vb, &des0))) {
1604 if (((des0 >> 16) & 0x7fff) == VOICEBUS_SFRAME_SIZE)
1605 list_add_tail(&vbb->entry, &buffers);
1606 else
1607 vb_submit_rxb(vb, vbb);
1608 }
1609
1610 handle_receive(vb, &buffers);
1611
1612 while (!list_empty(&buffers)) {
1613 vbb = list_entry(buffers.next, struct vbb, entry);
1614 list_del(&vbb->entry);
1615 vb_submit_rxb(vb, vbb);
1616 }
1617
1618 return;
1619 tx_error_exit:
1620 vb_disable_interrupts(vb);
1621 schedule_work(&vb->underrun_work);
1622 while (!list_empty(&buffers)) {
1623 vbb = list_entry(buffers.next, struct vbb, entry);
1624 list_del(&vbb->entry);
1625 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
1626 }
1627 return;
1628 }
1629
1630 /**
1631 * handle_hardunderrun() - reset the AN983 after experiencing a hardunderrun.
1632 * @work: The work_struct used to queue this function.
1633 *
1634 */
1635 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1636 static void handle_hardunderrun(void *data)
1637 {
1638 struct voicebus *vb = data;
1639 #else
1640 static void handle_hardunderrun(struct work_struct *work)
1641 {
1642 struct voicebus *vb = container_of(work, struct voicebus,
1643 underrun_work);
1644 #endif
1645 if (test_bit(VOICEBUS_STOP, &vb->flags) ||
1646 test_bit(VOICEBUS_STOPPED, &vb->flags))
1647 return;
1648
1649 voicebus_stop(vb);
1650
1651 if (!test_bit(VOICEBUS_SHUTDOWN, &vb->flags)) {
1652
1653 if (printk_ratelimit()) {
1654 dev_info(&vb->pdev->dev, "Host failed to service "
1655 "card interrupt within %d ms which is a "
1656 "hardunderun.\n", DRING_SIZE);
1657 }
1658
1659 if (vb->ops->handle_error)
1660 vb->ops->handle_error(vb);
1661
1662 vb_disable_deferred(vb);
1663 setup_descriptors(vb);
1664 start_packet_processing(vb);
1665 vb_enable_deferred(vb);
1666 }
1667 }
1668
1669 /*!
1670 * \brief Interrupt handler for VoiceBus interface.
1671 *
1672 * NOTE: This handler is optimized for the case where only a single interrupt
1673 * condition will be generated at a time.
1674 *
1675 * ALSO NOTE: Only access the interrupt status register from this function
1676 * since it doesn't employ any locking on the voicebus interface.
1677 */
1678 static irqreturn_t
1679 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1680 vb_isr(int irq, void *dev_id, struct pt_regs *regs)
1681 #else
1682 vb_isr(int irq, void *dev_id)
1683 #endif
1684 {
1685 struct voicebus *vb = dev_id;
1686 unsigned long flags;
1687 u32 int_status;
1688
1689 int_status = __vb_getctl(vb, SR_CSR5);
1690 /* Mask out the reserved bits. */
1691 int_status &= ~(0xfc004010);
1692 int_status &= 0x7fff;
1693
1694 if (!int_status)
1695 return IRQ_NONE;
1696
1697 local_irq_save(flags);
1698
1699 if (unlikely((int_status &
1700 (TX_UNAVAILABLE_INTERRUPT|RX_UNAVAILABLE_INTERRUPT)) &&
1701 !test_bit(VOICEBUS_STOP, &vb->flags) &&
1702 (BOOT != vb->mode))) {
1703 if (NORMAL == vb->mode) {
1704 __vb_disable_interrupts(vb);
1705 __vb_setctl(vb, SR_CSR5, int_status);
1706 schedule_work(&vb->underrun_work);
1707 } else if (HX8 == vb->mode) {
1708 set_bit(VOICEBUS_HARD_UNDERRUN, &vb->flags);
1709 vb_schedule_deferred(vb);
1710 __vb_setctl(vb, SR_CSR5, int_status);
1711 }
1712 } else if (likely(int_status &
1713 (TX_COMPLETE_INTERRUPT|RX_COMPLETE_INTERRUPT))) {
1714 /* ******************************************************** */
1715 /* NORMAL INTERRUPT CASE */
1716 /* ******************************************************** */
1717 vb_schedule_deferred(vb);
1718 __vb_setctl(vb, SR_CSR5, TX_COMPLETE_INTERRUPT|RX_COMPLETE_INTERRUPT);
1719 } else {
1720 if (int_status & FATAL_BUS_ERROR_INTERRUPT)
1721 dev_err(&vb->pdev->dev, "Fatal Bus Error detected!\n");
1722
1723 if (int_status & TX_STOPPED_INTERRUPT) {
1724 BUG_ON(!test_bit(VOICEBUS_STOP, &vb->flags));
1725 if (__vb_is_stopped(vb)) {
1726 __vb_disable_interrupts(vb);
1727 }
1728 }
1729 if (int_status & RX_STOPPED_INTERRUPT) {
1730 BUG_ON(!test_bit(VOICEBUS_STOP, &vb->flags));
1731 if (__vb_is_stopped(vb)) {
1732 __vb_disable_interrupts(vb);
1733 }
1734 }
1735
1736 /* Clear the interrupt(s) */
1737 __vb_setctl(vb, SR_CSR5, int_status);
1738 }
1739 local_irq_restore(flags);
1740
1741 return IRQ_HANDLED;
1742 }
1743
1744 #if defined(CONFIG_VOICEBUS_TIMER)
1745 /*! \brief Called if the deferred processing is to happen in the context of
1746 * the timer.
1747 */
1748 static void
1749 vb_timer(unsigned long data)
1750 {
1751 unsigned long start = jiffies;
1752 struct voicebus *vb = (struct voicebus *)data;
1753 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1754 vb_isr(0, vb, 0);
1755 #else
1756 vb_isr(0, vb);
1757 #endif
1758 if (!test_bit(VOICEBUS_STOPPED, &vb->flags)) {
1759 vb->timer.expires = start + HZ/1000;
1760 add_timer(&vb->timer);
1761 }
1762 }
1763 #endif
1764
1765 /*!
1766 * \brief Initalize the voicebus interface.
1767 *
1768 * This function must be called in process context since it may sleep.
1769 * \todo Complete this description.
1770 */
1771 int
1772 __voicebus_init(struct voicebus *vb, const char *board_name,
1773 enum voicebus_mode mode)
1774 {
1775 int retval = 0;
1776 int reserved_iomem = 0;
1777
1778 BUG_ON(NULL == vb);
1779 BUG_ON(NULL == board_name);
1780 BUG_ON(NULL == vb->ops);
1781 BUG_ON(NULL == vb->pdev);
1782 BUG_ON(NULL == vb->debug);
1783
1784 /* ----------------------------------------------------------------
1785 Initialize the pure software constructs.
1786 ---------------------------------------------------------------- */
1787 vb->max_latency = VOICEBUS_DEFAULT_MAXLATENCY;
1788
1789 spin_lock_init(&vb->lock);
1790 set_bit(VOICEBUS_STOP, &vb->flags);
1791
1792 if ((NORMAL != mode) && (BOOT != mode) && (HX8 != mode))
1793 return -EINVAL;
1794
1795 vb->mode = mode;
1796
1797 vb->min_tx_buffer_count = VOICEBUS_DEFAULT_LATENCY;
1798
1799 INIT_LIST_HEAD(&vb->tx_complete);
1800 INIT_LIST_HEAD(&vb->free_rx);
1801
1802 #if defined(CONFIG_VOICEBUS_TIMER)
1803 init_timer(&vb->timer);
1804 vb->timer.function = vb_timer;
1805 vb->timer.data = (unsigned long)vb;
1806 #endif
1807
1808 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1809 INIT_WORK(&vb->underrun_work, handle_hardunderrun, vb);
1810 #else
1811 INIT_WORK(&vb->underrun_work, handle_hardunderrun);
1812 #endif
1813
1814 /* ----------------------------------------------------------------
1815 Configure the hardware / kernel module interfaces.
1816 ---------------------------------------------------------------- */
1817 if (pci_set_dma_mask(vb->pdev, DMA_BIT_MASK(32))) {
1818 dev_err(&vb->pdev->dev, "No suitable DMA available.\n");
1819 goto cleanup;
1820 }
1821
1822 if (pci_enable_device(vb->pdev)) {
1823 dev_err(&vb->pdev->dev, "Failed call to pci_enable_device.\n");
1824 retval = -EIO;
1825 goto cleanup;
1826 }
1827
1828 if (0 == (pci_resource_flags(vb->pdev, 0)&IORESOURCE_IO)) {
1829 dev_err(&vb->pdev->dev, "BAR0 is not IO Memory.\n");
1830 retval = -EIO;
1831 goto cleanup;
1832 }
1833 vb->iobase = pci_iomap(vb->pdev, 1, 0);
1834 if (request_mem_region(pci_resource_start(vb->pdev, 1),
1835 pci_resource_len(vb->pdev, 1),
1836 board_name)) {
1837 reserved_iomem = 1;
1838 } else {
1839 dev_err(&vb->pdev->dev, "IO Registers are in use by another "
1840 "module.\n");
1841 if (!(*vb->debug)) {
1842 retval = -EIO;
1843 goto cleanup;
1844 }
1845 }
1846
1847 vb->pool = dma_pool_create(board_name, &vb->pdev->dev,
1848 sizeof(struct vbb), 64, 0);
1849 if (!vb->pool) {
1850 retval = -ENOMEM;
1851 goto cleanup;
1852 }
1853
1854 vb->idle_vbb = dma_alloc_coherent(&vb->pdev->dev, VOICEBUS_SFRAME_SIZE,
1855 &vb->idle_vbb_dma_addr, GFP_KERNEL);
1856
1857 /* ----------------------------------------------------------------
1858 Configure the hardware interface.
1859 ---------------------------------------------------------------- */
1860 if (pci_set_dma_mask(vb->pdev, DMA_BIT_MASK(32))) {
1861 dev_warn(&vb->pdev->dev, "No suitable DMA available.\n");
1862 goto cleanup;
1863 }
1864
1865 retval = pci_set_mwi(vb->pdev);
1866 if (retval) {
1867 dev_warn(&vb->pdev->dev, "Failed to set Memory-Write " \
1868 "Invalidate Command Bit..\n");
1869 }
1870
1871 pci_set_master(vb->pdev);
1872
1873 if (vb_reset_interface(vb)) {
1874 retval = -EIO;
1875 dev_warn(&vb->pdev->dev, "Failed reset.\n");
1876 goto cleanup;
1877 }
1878
1879 retval = vb_initialize_tx_descriptors(vb);
1880 if (retval)
1881 goto cleanup;
1882
1883 retval = vb_initialize_rx_descriptors(vb);
1884 if (retval)
1885 goto cleanup;
1886
1887 #if !defined(CONFIG_VOICEBUS_TIMER)
1888 retval = request_irq(vb->pdev->irq, vb_isr, IRQF_SHARED,
1889 board_name, vb);
1890 if (retval) {
1891 dev_warn(&vb->pdev->dev, "Failed to request interrupt line.\n");
1892 goto cleanup;
1893 }
1894 #endif
1895
1896 #ifdef VOICEBUS_NET_DEBUG
1897 vb_net_register(vb, board_name);
1898 #endif
1899 return retval;
1900 cleanup:
1901
1902 tasklet_kill(&vb->tasklet);
1903
1904 if (vb->pool)
1905 dma_pool_destroy(vb->pool);
1906 /* Cleanup memory and software resources. */
1907 if (vb->txd.desc)
1908 vb_free_descriptors(vb, &vb->txd);
1909
1910 if (vb->rxd.desc)
1911 vb_free_descriptors(vb, &vb->rxd);
1912
1913 dma_free_coherent(&vb->pdev->dev, VOICEBUS_SFRAME_SIZE,
1914 vb->idle_vbb, vb->idle_vbb_dma_addr);
1915
1916 if (vb->iobase)
1917 pci_iounmap(vb->pdev, vb->iobase);
1918
1919 if (vb->pdev)
1920 pci_disable_device(vb->pdev);
1921
1922 if (reserved_iomem) {
1923 release_mem_region(pci_resource_start(vb->pdev, 1),
1924 pci_resource_len(vb->pdev, 1));
1925 }
1926
1927 if (0 == retval)
1928 retval = -EIO;
1929 return retval;
1930 }
1931 EXPORT_SYMBOL(__voicebus_init);
1932
1933 static spinlock_t loader_list_lock;
1934 static LIST_HEAD(binary_loader_list);
1935
1936 /**
1937 * vpmadtreg_loadfirmware - Load the vpmadt032 firmware.
1938 * @vb: The voicebus device to load.
1939 */
1940 int vpmadtreg_loadfirmware(struct voicebus *vb)
1941 {
1942 struct vpmadt_loader *loader;
1943 int ret = 0;
1944 int loader_present = 0;
1945 unsigned long stop;
1946 might_sleep();
1947
1948 /* First check to see if a loader is already loaded into memory. */
1949 spin_lock(&loader_list_lock);
1950 loader_present = !(list_empty(&binary_loader_list));
1951 spin_unlock(&loader_list_lock);
1952
1953 if (!loader_present) {
1954 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
1955 ret = request_module("dahdi_vpmadt032_loader");
1956 #else
1957 /* If we use the blocking 'request_module' here and we are
1958 * loading the client boards with async_schedule we will hang
1959 * here. The module loader will wait for our asynchronous tasks
1960 * to finish, but we can't because we're waiting for the load
1961 * the finish. */
1962 ret = request_module_nowait("dahdi_vpmadt032_loader");
1963 #endif
1964 if (ret)
1965 return ret;
1966 stop = jiffies + HZ;
1967 while (time_after(stop, jiffies)) {
1968 spin_lock(&loader_list_lock);
1969 loader_present = !(list_empty(&binary_loader_list));
1970 spin_unlock(&loader_list_lock);
1971 if (loader_present)
1972 break;
1973 msleep(10);
1974 }
1975 }
1976
1977 spin_lock(&loader_list_lock);
1978 if (!list_empty(&binary_loader_list)) {
1979 loader = list_entry(binary_loader_list.next,
1980 struct vpmadt_loader, node);
1981 if (try_module_get(loader->owner)) {
1982 spin_unlock(&loader_list_lock);
1983 ret = loader->load(vb);
1984 module_put(loader->owner);
1985 } else {
1986 spin_unlock(&loader_list_lock);
1987 dev_info(&vb->pdev->dev, "Failed to find a "
1988 "registered loader after loading module.\n");
1989 ret = -ENODEV;
1990 }
1991 } else {
1992 spin_unlock(&loader_list_lock);
1993 dev_info(&vb->pdev->dev, "Failed to find a registered "
1994 "loader after loading module.\n");
1995 ret = -ENODEV;
1996 }
1997 return ret;
1998 }
1999
2000 /* Called by the binary loader module when it is ready to start loading
2001 * firmware. */
2002 int vpmadtreg_register(struct vpmadt_loader *loader)
2003 {
2004 spin_lock(&loader_list_lock);
2005 list_add_tail(&loader->node, &binary_loader_list);
2006 spin_unlock(&loader_list_lock);
2007 return 0;
2008 }
2009 EXPORT_SYMBOL(vpmadtreg_register);
2010
2011 int vpmadtreg_unregister(struct vpmadt_loader *loader)
2012 {
2013 int removed = 0;
2014 struct vpmadt_loader *cur, *temp;
2015 list_for_each_entry_safe(cur, temp, &binary_loader_list, node) {
2016 if (loader == cur) {
2017 list_del_init(&cur->node);
2018 removed = 1;
2019 break;
2020 }
2021 }
2022 WARN_ON(!removed);
2023 return 0;
2024 }
2025 EXPORT_SYMBOL(vpmadtreg_unregister);
2026
2027 static int __init voicebus_module_init(void)
2028 {
2029 /* This registration with dahdi.ko will fail since the span is not
2030 * defined, but it will make sure that this module is a dependency of
2031 * dahdi.ko, so that when it is being unloded, this module will be
2032 * unloaded as well. */
2033 dahdi_register_device(NULL, NULL);
2034 spin_lock_init(&loader_list_lock);
2035 return 0;
2036 }
2037
2038 static void __exit voicebus_module_cleanup(void)
2039 {
2040 WARN_ON(!list_empty(&binary_loader_list));
2041 }
2042
2043 MODULE_DESCRIPTION("Voicebus Interface w/VPMADT032 support");
2044 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
2045 MODULE_LICENSE("GPL");
2046
2047 module_init(voicebus_module_init);
2048 module_exit(voicebus_module_cleanup);