Debian Codegraph @

drivers/dahdi/voicebus/voicebus.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 #ifndef CONFIG_VOICEBUS_TIMER
1004 static void
1005 __vb_enable_interrupts(struct voicebus *vb)
1006 {
1007 if (BOOT == vb->mode)
1008 __vb_setctl(vb, IER_CSR7, DEFAULT_NO_IDLE_INTERRUPTS);
1009 else
1010 __vb_setctl(vb, IER_CSR7, DEFAULT_NORMAL_INTERRUPTS);
1011 }
1012 #endif
1013
1014 static void
1015 __vb_disable_interrupts(struct voicebus *vb)
1016 {
1017 __vb_setctl(vb, IER_CSR7, 0);
1018 }
1019
1020 static void
1021 vb_disable_interrupts(struct voicebus *vb)
1022 {
1023 unsigned long flags;
1024 spin_lock_irqsave(&vb->lock, flags);
1025 __vb_disable_interrupts(vb);
1026 spin_unlock_irqrestore(&vb->lock, flags);
1027 }
1028
1029 static void start_packet_processing(struct voicebus *vb)
1030 {
1031 u32 reg;
1032 unsigned long flags;
1033
1034 spin_lock_irqsave(&vb->lock, flags);
1035 clear_bit(VOICEBUS_STOP, &vb->flags);
1036 clear_bit(VOICEBUS_STOPPED, &vb->flags);
1037 #if defined(CONFIG_VOICEBUS_TIMER)
1038 mod_timer(&vb->timer, jiffies + HZ/1000);
1039 #else
1040 /* Clear the interrupt status register. */
1041 __vb_setctl(vb, SR_CSR5, 0xffffffff);
1042 __vb_enable_interrupts(vb);
1043 #endif
1044 /* Start the transmit and receive processors. */
1045 reg = __vb_getctl(vb, 0x0030);
1046 __vb_setctl(vb, 0x0030, reg|0x00002002);
1047 __vb_getctl(vb, 0x0030);
1048 __vb_rx_demand_poll(vb);
1049 __vb_tx_demand_poll(vb);
1050 __vb_getctl(vb, 0x0030);
1051 spin_unlock_irqrestore(&vb->lock, flags);
1052 }
1053
1054 static void vb_tasklet_boot(unsigned long data);
1055 static void vb_tasklet_hx8(unsigned long data);
1056 static void vb_tasklet_normal(unsigned long data);
1057
1058 /*!
1059 * \brief Starts the VoiceBus interface.
1060 *
1061 * When the VoiceBus interface is started, it is actively transferring
1062 * frames to and from the backend of the card. This means the card will
1063 * generate interrupts.
1064 *
1065 * This function should only be called from process context, with interrupts
1066 * enabled, since it can sleep while running the self checks.
1067 *
1068 * \return zero on success. -EBUSY if device is already running.
1069 */
1070 int
1071 voicebus_start(struct voicebus *vb)
1072 {
1073 int ret;
1074
1075 if (!vb_is_stopped(vb))
1076 return -EBUSY;
1077
1078 if (NORMAL == vb->mode) {
1079 tasklet_init(&vb->tasklet, vb_tasklet_normal,
1080 (unsigned long)vb);
1081 } else if (BOOT == vb->mode) {
1082 tasklet_init(&vb->tasklet, vb_tasklet_boot,
1083 (unsigned long)vb);
1084 } else if (HX8 == vb->mode) {
1085 tasklet_init(&vb->tasklet, vb_tasklet_hx8,
1086 (unsigned long)vb);
1087 } else {
1088 return -EINVAL;
1089 }
1090
1091 ret = vb_reset_interface(vb);
1092 if (ret)
1093 return ret;
1094 ret = vb_initialize_interface(vb);
1095 if (ret)
1096 return ret;
1097
1098 start_packet_processing(vb);
1099
1100 BUG_ON(vb_is_stopped(vb));
1101
1102 return 0;
1103 }
1104 EXPORT_SYMBOL(voicebus_start);
1105
1106 static void vb_stop_txrx_processors(struct voicebus *vb)
1107 {
1108 unsigned long flags;
1109 u32 reg;
1110 int i;
1111
1112 spin_lock_irqsave(&vb->lock, flags);
1113 reg = __vb_getctl(vb, NAR_CSR6);
1114 reg &= ~(0x2002);
1115 __vb_setctl(vb, NAR_CSR6, reg);
1116 spin_unlock_irqrestore(&vb->lock, flags);
1117
1118 barrier();
1119 i = 150;
1120 while (--i && (__vb_getctl(vb, SR_CSR5) & (0x007e0000)))
1121 udelay(100);
1122 }
1123
1124 /*!
1125 * \brief Stops the VoiceBus interface.
1126 *
1127 * Stops the VoiceBus interface and waits for any outstanding DMA transactions
1128 * to complete. When this functions returns the VoiceBus interface tx and rx
1129 * states will both be suspended.
1130 *
1131 * Only call this function from process context, with interrupt enabled,
1132 * without any locks held since it sleeps.
1133 *
1134 * \return zero on success, -1 on error.
1135 */
1136 void voicebus_stop(struct voicebus *vb)
1137 {
1138 static DEFINE_SEMAPHORE(stop);
1139
1140 down(&stop);
1141
1142 if (test_bit(VOICEBUS_STOP, &vb->flags) || vb_is_stopped(vb)) {
1143 up(&stop);
1144 return;
1145 }
1146
1147 set_bit(VOICEBUS_STOP, &vb->flags);
1148 vb_stop_txrx_processors(vb);
1149
1150 WARN_ON(!vb_is_stopped(vb));
1151 set_bit(VOICEBUS_STOPPED, &vb->flags);
1152
1153 #if defined(CONFIG_VOICEBUS_TIMER)
•1154 timer_delete_sync(&vb->timer);
1155 #endif
1156 vb_disable_interrupts(vb);
1157 up(&stop);
1158 }
1159 EXPORT_SYMBOL(voicebus_stop);
1160
1161 /**
1162 * voicebus_quiesce - Halt the voicebus interface.
1163 * @vb: The voicebus structure to quiet
1164 *
1165 * This ensures that the device is not engaged in any DMA transactions or
1166 * interrupting. It does not grab any locks since it may be called by a dying
1167 * kernel.
1168 */
1169 void voicebus_quiesce(struct voicebus *vb)
1170 {
1171 if (!vb)
1172 return;
1173
1174 /* Reset the device */
1175 __vb_disable_interrupts(vb);
1176 __vb_setctl(vb, 0x0000, 0x1);
1177 __vb_getctl(vb, 0x0000);
1178 }
1179 EXPORT_SYMBOL(voicebus_quiesce);
1180
1181 /*!
1182 * \brief Prepare the interface for module unload.
1183 *
1184 * Stop the interface and free all the resources allocated by the driver. The
1185 * caller should have returned all VoiceBus buffers to the VoiceBus layer
1186 * before calling this function.
1187 *
1188 * context: !in_interrupt()
1189 */
1190 void
1191 voicebus_release(struct voicebus *vb)
1192 {
1193 set_bit(VOICEBUS_SHUTDOWN, &vb->flags);
1194
1195 #ifdef VOICEBUS_NET_DEBUG
1196 vb_net_unregister(vb);
1197 #endif
1198
1199 /* Make sure the underrun_work isn't running or going to run. */
1200 cancel_work_sync(&vb->underrun_work);
1201
1202 /* quiesce the hardware */
1203 voicebus_stop(vb);
1204
1205 vb_reset_interface(vb);
1206
1207 tasklet_kill(&vb->tasklet);
1208
1209 #if !defined(CONFIG_VOICEBUS_TIMER)
1210 free_irq(vb->pdev->irq, vb);
1211 #endif
1212
1213 /* Cleanup memory and software resources. */
1214 vb_free_descriptors(vb, &vb->txd);
1215 vb_free_descriptors(vb, &vb->rxd);
1216 if (vb->idle_vbb_dma_addr) {
1217 dma_free_coherent(&vb->pdev->dev, VOICEBUS_SFRAME_SIZE,
1218 vb->idle_vbb, vb->idle_vbb_dma_addr);
1219 }
1220
1221 release_mem_region(pci_resource_start(vb->pdev, 1),
1222 pci_resource_len(vb->pdev, 1));
1223
1224 pci_iounmap(vb->pdev, vb->iobase);
1225 pci_clear_mwi(vb->pdev);
1226 pci_disable_device(vb->pdev);
1227 dma_pool_destroy(vb->pool);
1228 }
1229 EXPORT_SYMBOL(voicebus_release);
1230
1231 static void
1232 vb_increase_latency(struct voicebus *vb, unsigned int increase,
1233 struct list_head *buffers)
1234 {
1235 struct vbb *vbb;
1236 int i;
1237 LIST_HEAD(local);
1238
1239 if (0 == increase)
1240 return;
1241
1242 if (test_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags))
1243 return;
1244
1245 if (unlikely(increase > VOICEBUS_MAXLATENCY_BUMP))
1246 increase = VOICEBUS_MAXLATENCY_BUMP;
1247
1248 if ((increase + vb->min_tx_buffer_count) > vb->max_latency)
1249 increase = vb->max_latency - vb->min_tx_buffer_count;
1250
1251 /* Because there are 2 buffers in the transmit FIFO on the hardware,
1252 * setting 3 ms of latency means that the host needs to be able to
1253 * service the cards within 1ms. This is because the interface will
1254 * load up 2 buffers into the TX FIFO then attempt to read the 3rd
1255 * descriptor. If the OWN bit isn't set, then the hardware will set the
1256 * TX descriptor not available interrupt. */
1257
1258 /* Set the minimum latency in case we're restarted...we don't want to
1259 * wait for the buffer to grow to this depth again in that case. */
1260 for (i = 0; i < increase; ++i) {
1261 dma_addr_t dma_addr;
1262 vbb = dma_pool_alloc(vb->pool, GFP_ATOMIC, &dma_addr);
1263 WARN_ON(NULL == vbb);
1264 if (likely(NULL != vbb)) {
1265 vbb->dma_addr = dma_addr;
1266 list_add_tail(&vbb->entry, &local);
1267 }
1268 }
1269
1270 handle_transmit(vb, &local);
1271 list_splice_tail(&local, buffers);
1272
1273 /* Set the new latency (but we want to ensure that there aren't any
1274 * printks to the console, so we don't call the function) */
1275 vb->min_tx_buffer_count += increase;
1276 }
1277
1278 static void vb_schedule_deferred(struct voicebus *vb)
1279 {
1280 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1281 tasklet_hi_schedule(&vb->tasklet);
1282 #else
1283 vb->tasklet.func(vb->tasklet.data);
1284 #endif
1285 }
1286
1287 /**
1288 * vb_tasklet_boot() - When vb->mode == BOOT
1289 *
1290 * This deferred processing routine is for hx8 boards during initialization. It
1291 * simply services any completed tx / rx packets without any concerns about what
1292 * the current latency is.
1293 */
1294 static void vb_tasklet_boot(unsigned long data)
1295 {
1296 struct voicebus *vb = (struct voicebus *)data;
1297 LIST_HEAD(buffers);
1298 struct vbb *vbb;
1299 const int DEFAULT_COUNT = 5;
1300 int count = DEFAULT_COUNT;
1301 u32 des0 = 0;
1302
1303 /* First, temporarily store any non-idle buffers that the hardware has
1304 * indicated it's finished transmitting. Non idle buffers are those
1305 * buffers that contain actual data and was filled out by the client
1306 * driver (as of this writing, the wcte12xp or wctdm24xxp drivers) when
1307 * passed up through the handle_transmit callback.
1308 *
1309 * On the other hand, idle buffers are "dummy" buffers that solely exist
1310 * to in order to prevent the transmit descriptor ring from ever
1311 * completely draining. */
1312 while ((vbb = vb_get_completed_txb(vb)))
1313 list_add_tail(&vbb->entry, &vb->tx_complete);
1314
1315 while (--count && !list_empty(&vb->tx_complete))
1316 list_move_tail(vb->tx_complete.next, &buffers);
1317
1318 /* Prep all the new buffers for transmit before actually sending any
1319 * of them. */
1320 handle_transmit(vb, &buffers);
1321
1322 while (!list_empty(&buffers)) {
1323 vbb = list_entry(buffers.next, struct vbb, entry);
1324 list_del(&vbb->entry);
1325 __voicebus_transmit(vb, vbb);
1326 }
1327 __vb_setctl(vb, 0x0008, 0x00000000);
1328
1329 /* If there may still be buffers in the descriptor rings, reschedule
1330 * ourself to run again. We essentially yield here to allow any other
1331 * cards a chance to run. */
1332 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1333 if (unlikely(!count && !test_bit(VOICEBUS_STOP, &vb->flags)))
1334 vb_schedule_deferred(vb);
1335 #endif
1336
1337 /* And finally, pass up any receive buffers. */
1338 count = DEFAULT_COUNT;
1339 while (--count && (vbb = vb_get_completed_rxb(vb, &des0))) {
1340 if (likely((des0 & (0x7fff << 16)) ==
1341 (VOICEBUS_SFRAME_SIZE << 16)))
1342 list_add_tail(&vbb->entry, &buffers);
1343 else
1344 vb_submit_rxb(vb, vbb);
1345 }
1346
1347 handle_receive(vb, &buffers);
1348 while (!list_empty(&buffers)) {
1349 vbb = list_entry(buffers.next, struct vbb, entry);
1350 list_del(&vbb->entry);
1351 vb_submit_rxb(vb, vbb);
1352 }
1353 return;
1354 }
1355
1356 /**
1357 * vb_tasklet_hx8() - When vb->mode == HX8
1358 *
1359 * The normal deferred processing routine for the Hx8 boards. This deferred
1360 * processing routine doesn't configure any idle buffers and increases the
1361 * latency when there is a hard underrun. There are not any softunderruns here,
1362 * unlike in vb_tasklet_normal.
1363 */
1364 static void vb_tasklet_hx8(unsigned long data)
1365 {
1366 struct voicebus *vb = (struct voicebus *)data;
1367 int hardunderrun;
1368 LIST_HEAD(buffers);
1369 struct vbb *vbb;
1370 const int DEFAULT_COUNT = 5;
1371 int count = DEFAULT_COUNT;
1372 u32 des0 = 0;
1373
1374 hardunderrun = test_and_clear_bit(VOICEBUS_HARD_UNDERRUN, &vb->flags);
1375 /* First, temporarily store any non-idle buffers that the hardware has
1376 * indicated it's finished transmitting. Non idle buffers are those
1377 * buffers that contain actual data and was filled out by the client
1378 * driver (as of this writing, the wcte12xp or wctdm24xxp drivers) when
1379 * passed up through the handle_transmit callback.
1380 *
1381 * On the other hand, idle buffers are "dummy" buffers that solely exist
1382 * to in order to prevent the transmit descriptor ring from ever
1383 * completely draining. */
1384 while ((vbb = vb_get_completed_txb(vb)))
1385 list_add_tail(&vbb->entry, &vb->tx_complete);
1386
1387 while (--count && !list_empty(&vb->tx_complete))
1388 list_move_tail(vb->tx_complete.next, &buffers);
1389
1390 /* Prep all the new buffers for transmit before actually sending any
1391 * of them. */
1392 handle_transmit(vb, &buffers);
1393
1394 if (unlikely(hardunderrun))
1395 vb_increase_latency(vb, 1, &buffers);
1396
1397 /* Now we can send all our buffers together in a group. */
1398 while (!list_empty(&buffers)) {
1399 vbb = list_entry(buffers.next, struct vbb, entry);
1400 list_del(&vbb->entry);
1401 __voicebus_transmit(vb, vbb);
1402 }
1403 __vb_setctl(vb, 0x0008, 0x00000000);
1404
1405 /* Print any messages about soft latency bumps after we fix the transmit
1406 * descriptor ring. Otherwise it's possible to take so much time
1407 * printing the dmesg output that we lose the lead that we got on the
1408 * hardware, resulting in a hard underrun condition. */
1409 if (unlikely(hardunderrun)) {
1410 #if !defined(CONFIG_VOICEBUS_SYSFS)
1411 if (!test_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags) &&
1412 printk_ratelimit()) {
1413 if (vb->max_latency != vb->min_tx_buffer_count) {
1414 dev_info(&vb->pdev->dev, "Missed interrupt. "
1415 "Increasing latency to %d ms in "
1416 "order to compensate.\n",
1417 vb->min_tx_buffer_count);
1418 } else {
1419 dev_info(&vb->pdev->dev, "ERROR: Unable to "
1420 "service card within %d ms and "
1421 "unable to further increase "
1422 "latency.\n", vb->max_latency);
1423 }
1424 }
1425 #endif
1426 }
1427
1428 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1429 /* If there may still be buffers in the descriptor rings, reschedule
1430 * ourself to run again. We essentially yield here to allow any other
1431 * cards a chance to run. */
1432 if (unlikely(!count && !test_bit(VOICEBUS_STOP, &vb->flags)))
1433 vb_schedule_deferred(vb);
1434 #endif
1435
1436 /* And finally, pass up any receive buffers. */
1437 count = DEFAULT_COUNT;
1438 while (--count && (vbb = vb_get_completed_rxb(vb, &des0))) {
1439 if (((des0 >> 16) & 0x7fff) == VOICEBUS_SFRAME_SIZE)
1440 list_add_tail(&vbb->entry, &buffers);
1441 else
1442 vb_submit_rxb(vb, vbb);
1443 }
1444
1445 handle_receive(vb, &buffers);
1446
1447 while (!list_empty(&buffers)) {
1448 vbb = list_entry(buffers.next, struct vbb, entry);
1449 list_del(&vbb->entry);
1450 vb_submit_rxb(vb, vbb);
1451 }
1452
1453 return;
1454 }
1455
1456 /**
1457 * vb_tasklet_relaxed() - When vb->mode == NORMAL
1458 *
1459 * This is the standard deferred processing routine for CPLD based cards
1460 * (essentially the non-hx8 cards).
1461 */
1462 static void vb_tasklet_normal(unsigned long data)
1463 {
1464 struct voicebus *vb = (struct voicebus *)data;
1465 int softunderrun;
1466 LIST_HEAD(buffers);
1467 struct vbb *vbb;
1468 struct voicebus_descriptor_list *const dl = &vb->txd;
1469 struct voicebus_descriptor *d;
1470 int behind = 0;
1471 const int DEFAULT_COUNT = 5;
1472 int count = DEFAULT_COUNT;
1473 u32 des0 = 0;
1474
1475 BUG_ON(NORMAL != vb->mode);
1476 /* First, temporarily store any non-idle buffers that the hardware has
1477 * indicated it's finished transmitting. Non idle buffers are those
1478 * buffers that contain actual data and was filled out by the client
1479 * driver (as of this writing, the wcte12xp or wctdm24xxp drivers) when
1480 * passed up through the handle_transmit callback.
1481 *
1482 * On the other hand, idle buffers are "dummy" buffers that solely exist
1483 * to in order to prevent the transmit descriptor ring from ever
1484 * completely draining. */
1485 while ((vbb = vb_get_completed_txb(vb)))
1486 list_add_tail(&vbb->entry, &vb->tx_complete);
1487
1488 if (unlikely(dl->count < 2)) {
1489 softunderrun = 1;
1490 d = vb_descriptor(dl, dl->head);
1491 if (1 == dl->count)
1492 return;
1493
1494 behind = 2;
1495 while (!OWNED(d)) {
1496 if (le32_to_cpu(d->buffer1) != vb->idle_vbb_dma_addr)
1497 goto tx_error_exit;
1498 SET_OWNED(d);
1499 dl->head = (dl->head + 1) & DRING_MASK;
1500 d = vb_descriptor(dl, dl->head);
1501 ++behind;
1502 }
1503
1504 } else {
1505 softunderrun = 0;
1506 }
1507
1508 while (--count && !list_empty(&vb->tx_complete))
1509 list_move_tail(vb->tx_complete.next, &buffers);
1510
1511 /* Prep all the new buffers for transmit before actually sending any
1512 * of them. */
1513 handle_transmit(vb, &buffers);
1514
1515 if (unlikely(softunderrun)) {
1516 int i;
1517 unsigned long flags;
1518
1519 /* Disable interrupts on the local processor. We don't want
1520 * the following process interrupted. We're 'racing' against
1521 * the hardware here.... */
1522 local_irq_save(flags);
1523 vb_increase_latency(vb, behind, &buffers);
1524 d = vb_descriptor(dl, dl->head);
1525 while (!OWNED(d)) {
1526 if (le32_to_cpu(d->buffer1) != vb->idle_vbb_dma_addr) {
1527 local_irq_restore(flags);
1528 goto tx_error_exit;
1529 }
1530 SET_OWNED(d);
1531 dl->head = (dl->head + 1) & DRING_MASK;
1532 d = vb_descriptor(dl, dl->head);
1533 ++behind;
1534 }
1535 /* Now we'll get a little further ahead of the hardware. */
1536 for (i = 0; i < 5; ++i) {
1537 d = vb_descriptor(dl, dl->head);
1538 d->buffer1 = cpu_to_le32(vb->idle_vbb_dma_addr);
1539 dl->pending[dl->head] = vb->idle_vbb;
1540 d->des0 |= OWN_BIT;
1541 dl->head = (dl->head + 1) & DRING_MASK;
1542 }
1543 dl->tail = dl->head;
1544 local_irq_restore(flags);
1545 }
1546
1547 d = vb_descriptor(dl, dl->tail);
1548 if (le32_to_cpu(d->buffer1) != vb->idle_vbb_dma_addr)
1549 goto tx_error_exit;
1550
1551 /* Now we can send all our buffers together in a group. */
1552 while (!list_empty(&buffers)) {
1553 vbb = list_entry(buffers.next, struct vbb, entry);
1554 list_del(&vbb->entry);
1555 __voicebus_transmit(vb, vbb);
1556 }
1557
1558 writel(0, vb->iobase + 0x8);
1559
1560 /* Print any messages about soft latency bumps after we fix the transmit
1561 * descriptor ring. Otherwise it's possible to take so much time
1562 * printing the dmesg output that we lose the lead that we got on the
1563 * hardware, resulting in a hard underrun condition. */
1564 if (unlikely(softunderrun)) {
1565 #if !defined(CONFIG_VOICEBUS_SYSFS)
1566 if (!test_bit(VOICEBUS_LATENCY_LOCKED, &vb->flags) &&
1567 printk_ratelimit()) {
1568 if (vb->max_latency != vb->min_tx_buffer_count) {
1569 dev_info(&vb->pdev->dev, "Missed interrupt. "
1570 "Increasing latency to %d ms in "
1571 "order to compensate.\n",
1572 vb->min_tx_buffer_count);
1573 } else {
1574 dev_info(&vb->pdev->dev, "ERROR: Unable to "
1575 "service card within %d ms and "
1576 "unable to further increase "
1577 "latency.\n", vb->max_latency);
1578 }
1579 }
1580 #endif
1581 }
1582
1583 #if !defined(CONFIG_VOICEBUS_INTERRUPT)
1584 /* If there may still be buffers in the descriptor rings, reschedule
1585 * ourself to run again. We essentially yield here to allow any other
1586 * cards a chance to run. */
1587 if (unlikely(!count && !test_bit(VOICEBUS_STOP, &vb->flags)))
1588 vb_schedule_deferred(vb);
1589 #endif
1590
1591 /* And finally, pass up any receive buffers. */
1592 count = DEFAULT_COUNT;
1593 while (--count && (vbb = vb_get_completed_rxb(vb, &des0))) {
1594 if (((des0 >> 16) & 0x7fff) == VOICEBUS_SFRAME_SIZE)
1595 list_add_tail(&vbb->entry, &buffers);
1596 else
1597 vb_submit_rxb(vb, vbb);
1598 }
1599
1600 handle_receive(vb, &buffers);
1601
1602 while (!list_empty(&buffers)) {
1603 vbb = list_entry(buffers.next, struct vbb, entry);
1604 list_del(&vbb->entry);
1605 vb_submit_rxb(vb, vbb);
1606 }
1607
1608 return;
1609 tx_error_exit:
1610 vb_disable_interrupts(vb);
1611 schedule_work(&vb->underrun_work);
1612 while (!list_empty(&buffers)) {
1613 vbb = list_entry(buffers.next, struct vbb, entry);
1614 list_del(&vbb->entry);
1615 dma_pool_free(vb->pool, vbb, vbb->dma_addr);
1616 }
1617 return;
1618 }
1619
1620 /**
1621 * handle_hardunderrun() - reset the AN983 after experiencing a hardunderrun.
1622 * @work: The work_struct used to queue this function.
1623 *
1624 */
1625 static void handle_hardunderrun(struct work_struct *work)
1626 {
1627 struct voicebus *vb = container_of(work, struct voicebus,
1628 underrun_work);
1629 if (test_bit(VOICEBUS_STOP, &vb->flags) ||
1630 test_bit(VOICEBUS_STOPPED, &vb->flags))
1631 return;
1632
1633 voicebus_stop(vb);
1634
1635 if (!test_bit(VOICEBUS_SHUTDOWN, &vb->flags)) {
1636
1637 if (printk_ratelimit()) {
1638 dev_info(&vb->pdev->dev, "Host failed to service "
1639 "card interrupt within %d ms which is a "
1640 "hardunderun.\n", DRING_SIZE);
1641 }
1642
1643 if (vb->ops->handle_error)
1644 vb->ops->handle_error(vb);
1645
1646 vb_disable_deferred(vb);
1647 setup_descriptors(vb);
1648 start_packet_processing(vb);
1649 vb_enable_deferred(vb);
1650 }
1651 }
1652
1653 /*!
1654 * \brief Interrupt handler for VoiceBus interface.
1655 *
1656 * NOTE: This handler is optimized for the case where only a single interrupt
1657 * condition will be generated at a time.
1658 *
1659 * ALSO NOTE: Only access the interrupt status register from this function
1660 * since it doesn't employ any locking on the voicebus interface.
1661 */
1662 static irqreturn_t
1663 vb_isr(int irq, void *dev_id)
1664 {
1665 struct voicebus *vb = dev_id;
1666 unsigned long flags;
1667 u32 int_status;
1668
1669 int_status = __vb_getctl(vb, SR_CSR5);
1670 /* Mask out the reserved bits. */
1671 int_status &= ~(0xfc004010);
1672 int_status &= 0x7fff;
1673
1674 if (!int_status)
1675 return IRQ_NONE;
1676
1677 local_irq_save(flags);
1678
1679 if (unlikely((int_status &
1680 (TX_UNAVAILABLE_INTERRUPT|RX_UNAVAILABLE_INTERRUPT)) &&
1681 !test_bit(VOICEBUS_STOP, &vb->flags) &&
1682 (BOOT != vb->mode))) {
1683 if (NORMAL == vb->mode) {
1684 __vb_disable_interrupts(vb);
1685 __vb_setctl(vb, SR_CSR5, int_status);
1686 schedule_work(&vb->underrun_work);
1687 } else if (HX8 == vb->mode) {
1688 set_bit(VOICEBUS_HARD_UNDERRUN, &vb->flags);
1689 vb_schedule_deferred(vb);
1690 __vb_setctl(vb, SR_CSR5, int_status);
1691 }
1692 } else if (likely(int_status &
1693 (TX_COMPLETE_INTERRUPT|RX_COMPLETE_INTERRUPT))) {
1694 /* ******************************************************** */
1695 /* NORMAL INTERRUPT CASE */
1696 /* ******************************************************** */
1697 vb_schedule_deferred(vb);
1698 __vb_setctl(vb, SR_CSR5, TX_COMPLETE_INTERRUPT|RX_COMPLETE_INTERRUPT);
1699 } else {
1700 if (int_status & FATAL_BUS_ERROR_INTERRUPT)
1701 dev_err(&vb->pdev->dev, "Fatal Bus Error detected!\n");
1702
1703 if (int_status & TX_STOPPED_INTERRUPT) {
1704 BUG_ON(!test_bit(VOICEBUS_STOP, &vb->flags));
1705 if (__vb_is_stopped(vb)) {
1706 __vb_disable_interrupts(vb);
1707 }
1708 }
1709 if (int_status & RX_STOPPED_INTERRUPT) {
1710 BUG_ON(!test_bit(VOICEBUS_STOP, &vb->flags));
1711 if (__vb_is_stopped(vb)) {
1712 __vb_disable_interrupts(vb);
1713 }
1714 }
1715
1716 /* Clear the interrupt(s) */
1717 __vb_setctl(vb, SR_CSR5, int_status);
1718 }
1719 local_irq_restore(flags);
1720
1721 return IRQ_HANDLED;
1722 }
1723
1724 #if defined(CONFIG_VOICEBUS_TIMER)
1725 /*! \brief Called if the deferred processing is to happen in the context of
1726 * the timer.
1727 */
1728 static void
1729 vb_timer(TIMER_DATA_TYPE timer)
1730 {
1731 unsigned long start = jiffies;
•1732 struct voicebus *vb = timer_container_of(vb, timer, timer);
1733 vb_isr(0, vb);
1734 if (!test_bit(VOICEBUS_STOPPED, &vb->flags)) {
1735 mod_timer(&vb->timer, start + HZ/1000);
1736 }
1737 }
1738 #endif
1739
1740 /*!
1741 * \brief Initalize the voicebus interface.
1742 *
1743 * This function must be called in process context since it may sleep.
1744 * \todo Complete this description.
1745 */
1746 int
1747 __voicebus_init(struct voicebus *vb, const char *board_name,
1748 enum voicebus_mode mode)
1749 {
1750 int retval = 0;
1751 int reserved_iomem = 0;
1752
1753 BUG_ON(NULL == vb);
1754 BUG_ON(NULL == board_name);
1755 BUG_ON(NULL == vb->ops);
1756 BUG_ON(NULL == vb->pdev);
1757 BUG_ON(NULL == vb->debug);
1758
1759 /* ----------------------------------------------------------------
1760 Initialize the pure software constructs.
1761 ---------------------------------------------------------------- */
1762 vb->max_latency = VOICEBUS_DEFAULT_MAXLATENCY;
1763
1764 spin_lock_init(&vb->lock);
1765 set_bit(VOICEBUS_STOP, &vb->flags);
1766
1767 if ((NORMAL != mode) && (BOOT != mode) && (HX8 != mode))
1768 return -EINVAL;
1769
1770 vb->mode = mode;
1771
1772 vb->min_tx_buffer_count = VOICEBUS_DEFAULT_LATENCY;
1773
1774 INIT_LIST_HEAD(&vb->tx_complete);
1775 INIT_LIST_HEAD(&vb->free_rx);
1776
1777 #if defined(CONFIG_VOICEBUS_TIMER)
1778 timer_setup(&vb->timer, vb_timer, 0);
1779 #endif
1780
1781 INIT_WORK(&vb->underrun_work, handle_hardunderrun);
1782
1783 /* ----------------------------------------------------------------
1784 Configure the hardware / kernel module interfaces.
1785 ---------------------------------------------------------------- */
1786 if (pci_set_dma_mask(vb->pdev, DMA_BIT_MASK(32))) {
1787 dev_err(&vb->pdev->dev, "No suitable DMA available.\n");
1788 goto cleanup;
1789 }
1790
1791 if (pci_enable_device(vb->pdev)) {
1792 dev_err(&vb->pdev->dev, "Failed call to pci_enable_device.\n");
1793 retval = -EIO;
1794 goto cleanup;
1795 }
1796
1797 vb->iobase = pci_iomap(vb->pdev, 1, 0);
1798 if (request_mem_region(pci_resource_start(vb->pdev, 1),
1799 pci_resource_len(vb->pdev, 1),
1800 board_name)) {
1801 reserved_iomem = 1;
1802 } else {
1803 dev_err(&vb->pdev->dev, "IO Registers are in use by another "
1804 "module.\n");
1805 if (!(*vb->debug)) {
1806 retval = -EIO;
1807 goto cleanup;
1808 }
1809 }
1810
1811 vb->pool = dma_pool_create(board_name, &vb->pdev->dev,
1812 sizeof(struct vbb), 64, 0);
1813 if (!vb->pool) {
1814 retval = -ENOMEM;
1815 goto cleanup;
1816 }
1817
1818 vb->idle_vbb = dma_alloc_coherent(&vb->pdev->dev, VOICEBUS_SFRAME_SIZE,
1819 &vb->idle_vbb_dma_addr, GFP_KERNEL);
1820
1821 /* ----------------------------------------------------------------
1822 Configure the hardware interface.
1823 ---------------------------------------------------------------- */
1824 if (pci_set_dma_mask(vb->pdev, DMA_BIT_MASK(32))) {
1825 dev_warn(&vb->pdev->dev, "No suitable DMA available.\n");
1826 goto cleanup;
1827 }
1828
1829 retval = pci_set_mwi(vb->pdev);
1830 if (retval) {
1831 dev_warn(&vb->pdev->dev, "Failed to set Memory-Write " \
1832 "Invalidate Command Bit..\n");
1833 }
1834
1835 pci_set_master(vb->pdev);
1836
1837 if (vb_reset_interface(vb)) {
1838 retval = -EIO;
1839 dev_warn(&vb->pdev->dev, "Failed reset.\n");
1840 goto cleanup;
1841 }
1842
1843 retval = vb_initialize_tx_descriptors(vb);
1844 if (retval)
1845 goto cleanup;
1846
1847 retval = vb_initialize_rx_descriptors(vb);
1848 if (retval)
1849 goto cleanup;
1850
1851 #if !defined(CONFIG_VOICEBUS_TIMER)
1852 retval = request_irq(vb->pdev->irq, vb_isr, IRQF_SHARED,
1853 board_name, vb);
1854 if (retval) {
1855 dev_warn(&vb->pdev->dev, "Failed to request interrupt line.\n");
1856 goto cleanup;
1857 }
1858 #endif
1859
1860 #ifdef VOICEBUS_NET_DEBUG
1861 vb_net_register(vb, board_name);
1862 #endif
1863 return retval;
1864 cleanup:
1865
1866 tasklet_kill(&vb->tasklet);
1867
1868 if (vb->pool)
1869 dma_pool_destroy(vb->pool);
1870 /* Cleanup memory and software resources. */
1871 if (vb->txd.desc)
1872 vb_free_descriptors(vb, &vb->txd);
1873
1874 if (vb->rxd.desc)
1875 vb_free_descriptors(vb, &vb->rxd);
1876
1877 dma_free_coherent(&vb->pdev->dev, VOICEBUS_SFRAME_SIZE,
1878 vb->idle_vbb, vb->idle_vbb_dma_addr);
1879
1880 if (vb->iobase)
1881 pci_iounmap(vb->pdev, vb->iobase);
1882
1883 if (vb->pdev)
1884 pci_disable_device(vb->pdev);
1885
1886 if (reserved_iomem) {
1887 release_mem_region(pci_resource_start(vb->pdev, 1),
1888 pci_resource_len(vb->pdev, 1));
1889 }
1890
1891 if (0 == retval)
1892 retval = -EIO;
1893 return retval;
1894 }
1895 EXPORT_SYMBOL(__voicebus_init);
1896
1897 static spinlock_t loader_list_lock;
1898 static LIST_HEAD(binary_loader_list);
1899
1900 /**
1901 * vpmadtreg_loadfirmware - Load the vpmadt032 firmware.
1902 * @vb: The voicebus device to load.
1903 */
1904 int vpmadtreg_loadfirmware(struct voicebus *vb)
1905 {
1906 struct vpmadt_loader *loader;
1907 int ret = 0;
1908 int loader_present = 0;
1909 unsigned long stop;
1910 might_sleep();
1911
1912 /* First check to see if a loader is already loaded into memory. */
1913 spin_lock(&loader_list_lock);
1914 loader_present = !(list_empty(&binary_loader_list));
1915 spin_unlock(&loader_list_lock);
1916
1917 if (!loader_present) {
1918 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
1919 ret = request_module("dahdi_vpmadt032_loader");
1920 #else
1921 /* If we use the blocking 'request_module' here and we are
1922 * loading the client boards with async_schedule we will hang
1923 * here. The module loader will wait for our asynchronous tasks
1924 * to finish, but we can't because we're waiting for the load
1925 * the finish. */
1926 ret = request_module_nowait("dahdi_vpmadt032_loader");
1927 #endif
1928 if (ret)
1929 return ret;
1930 stop = jiffies + HZ;
1931 while (time_after(stop, jiffies)) {
1932 spin_lock(&loader_list_lock);
1933 loader_present = !(list_empty(&binary_loader_list));
1934 spin_unlock(&loader_list_lock);
1935 if (loader_present)
1936 break;
1937 msleep(10);
1938 }
1939 }
1940
1941 spin_lock(&loader_list_lock);
1942 if (!list_empty(&binary_loader_list)) {
1943 loader = list_entry(binary_loader_list.next,
1944 struct vpmadt_loader, node);
1945 if (try_module_get(loader->owner)) {
1946 spin_unlock(&loader_list_lock);
1947 ret = loader->load(vb);
1948 module_put(loader->owner);
1949 } else {
1950 spin_unlock(&loader_list_lock);
1951 dev_info(&vb->pdev->dev, "Failed to find a "
1952 "registered loader after loading module.\n");
1953 ret = -ENODEV;
1954 }
1955 } else {
1956 spin_unlock(&loader_list_lock);
1957 dev_info(&vb->pdev->dev, "Failed to find a registered "
1958 "loader after loading module.\n");
1959 ret = -ENODEV;
1960 }
1961 return ret;
1962 }
1963
1964 /* Called by the binary loader module when it is ready to start loading
1965 * firmware. */
1966 int vpmadtreg_register(struct vpmadt_loader *loader)
1967 {
1968 spin_lock(&loader_list_lock);
1969 list_add_tail(&loader->node, &binary_loader_list);
1970 spin_unlock(&loader_list_lock);
1971 return 0;
1972 }
1973 EXPORT_SYMBOL(vpmadtreg_register);
1974
1975 int vpmadtreg_unregister(struct vpmadt_loader *loader)
1976 {
1977 int removed = 0;
1978 struct vpmadt_loader *cur, *temp;
1979 list_for_each_entry_safe(cur, temp, &binary_loader_list, node) {
1980 if (loader == cur) {
1981 list_del_init(&cur->node);
1982 removed = 1;
1983 break;
1984 }
1985 }
1986 WARN_ON(!removed);
1987 return 0;
1988 }
1989 EXPORT_SYMBOL(vpmadtreg_unregister);
1990
1991 static int __init voicebus_module_init(void)
1992 {
1993 /* This registration with dahdi.ko will fail since the span is not
1994 * defined, but it will make sure that this module is a dependency of
1995 * dahdi.ko, so that when it is being unloded, this module will be
1996 * unloaded as well. */
1997 dahdi_register_device(NULL, NULL);
1998 spin_lock_init(&loader_list_lock);
1999 return 0;
2000 }
2001
2002 static void __exit voicebus_module_cleanup(void)
2003 {
2004 WARN_ON(!list_empty(&binary_loader_list));
2005 }
2006
2007 MODULE_DESCRIPTION("Voicebus Interface w/VPMADT032 support");
2008 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
2009 MODULE_LICENSE("GPL");
2010
2011 module_init(voicebus_module_init);
2012 module_exit(voicebus_module_cleanup);