Debian Codegraph @

drivers/dahdi/wcfxo.c

raw
1 /*
2 * Wildcard X100P FXO Interface Driver for DAHDI Telephony interface
3 *
4 * Written by Mark Spencer <[email protected]>
5 * Matthew Fredrickson <[email protected]>
6 *
7 * Copyright (C) 2001-2008, Digium, Inc.
8 *
9 * All rights reserved.
10 *
11 */
12
13 /*
14 * See http://www.asterisk.org for more information about
15 * the Asterisk project. Please do not directly contact
16 * any of the maintainers of this project for assistance;
17 * the project provides a web site, mailing lists and IRC
18 * channels for your use.
19 *
20 * This program is free software, distributed under the terms of
21 * the GNU General Public License Version 2 as published by the
22 * Free Software Foundation. See the LICENSE file included with
23 * this program for more details.
24 */
25
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/usb.h>
31 #include <linux/pci.h>
32 #include <asm/io.h>
33 #include <linux/moduleparam.h>
34 #include <linux/slab.h>
35
36 #include <dahdi/kernel.h>
37
38 /* Uncomment to enable tasklet handling in the FXO driver. Not recommended
39 in general, but may improve interactive performance */
40
41 /* #define ENABLE_TASKLETS */
42
43 /* Un-comment the following for POTS line support for Japan */
44 /* #define JAPAN */
45
46 /* Un-comment for lines (eg from and ISDN TA) that remove */
47 /* phone power during ringing */
48 /* #define ZERO_BATT_RING */
49
50 #define WC_MAX_IFACES 128
51
52 #define WC_CNTL 0x00
53 #define WC_OPER 0x01
54 #define WC_AUXC 0x02
55 #define WC_AUXD 0x03
56 #define WC_MASK0 0x04
57 #define WC_MASK1 0x05
58 #define WC_INTSTAT 0x06
59
60 #define WC_DMAWS 0x08
61 #define WC_DMAWI 0x0c
62 #define WC_DMAWE 0x10
63 #define WC_DMARS 0x18
64 #define WC_DMARI 0x1c
65 #define WC_DMARE 0x20
66
67 #define WC_AUXFUNC 0x2b
68 #define WC_SERCTL 0x2d
69 #define WC_FSCDELAY 0x2f
70
71
72 /* DAA registers */
73 #define WC_DAA_CTL1 1
74 #define WC_DAA_CTL2 2
75 #define WC_DAA_DCTL1 5
76 #define WC_DAA_DCTL2 6
77 #define WC_DAA_PLL1_N1 7
78 #define WC_DAA_PLL1_M1 8
79 #define WC_DAA_PLL2_N2_M2 9
80 #define WC_DAA_PLL_CTL 10
81 #define WC_DAA_CHIPA_REV 11
82 #define WC_DAA_LINE_STAT 12
83 #define WC_DAA_CHIPB_REV 13
84 #define WC_DAA_DAISY_CTL 14
85 #define WC_DAA_TXRX_GCTL 15
86 #define WC_DAA_INT_CTL1 16
87 #define WC_DAA_INT_CTL2 17
88 #define WC_DAA_INT_CTL3 18
89 #define WC_DAA_INT_CTL4 19
90
91
92 #define FLAG_EMPTY 0
93 #define FLAG_WRITE 1
94 #define FLAG_READ 2
95
96 #ifdef ZERO_BATT_RING /* Need to debounce Off/On hook too */
97 #define JAPAN
98 #endif
99
100 #define RING_DEBOUNCE 64 /* Ringer Debounce (in ms) */
101 #ifdef JAPAN
102 #define BATT_DEBOUNCE 30 /* Battery debounce (in ms) */
103 #define OH_DEBOUNCE 350 /* Off/On hook debounce (in ms) */
104 #else
105 #define BATT_DEBOUNCE 80 /* Battery debounce (in ms) */
106 #endif
107
108 #define MINPEGTIME 10 * 8 /* 30 ms peak to peak gets us no more than 100 Hz */
109 #define PEGTIME 50 * 8 /* 50ms peak to peak gets us rings of 10 Hz or more */
110 #define PEGCOUNT 5 /* 5 cycles of pegging means RING */
111
112 #define wcfxo_printk(level, span, fmt, ...) \
113 printk(KERN_ ## level "%s-%s: %s: " fmt, #level, \
114 THIS_MODULE->name, (span).name, ## __VA_ARGS__)
115
116 #define wcfxo_notice(span, fmt, ...) \
117 wcfxo_printk(NOTICE, span, fmt, ## __VA_ARGS__)
118
119 #define wcfxo_dbg(span, fmt, ...) \
120 ((void)((debug) && wcfxo_printk(DEBUG, span, "%s: " fmt, \
121 __FUNCTION__, ## __VA_ARGS__) ) )
122
123 struct reg {
124 unsigned long flags;
125 unsigned char index;
126 unsigned char reg;
127 unsigned char value;
128 };
129
130 static int wecareregs[] =
131 {
132 WC_DAA_DCTL1, WC_DAA_DCTL2, WC_DAA_PLL2_N2_M2, WC_DAA_CHIPA_REV,
133 WC_DAA_LINE_STAT, WC_DAA_CHIPB_REV, WC_DAA_INT_CTL2, WC_DAA_INT_CTL4,
134 };
135
136 struct wcfxo {
137 struct pci_dev *dev;
138 char *variety;
139 struct dahdi_device *ddev;
140 struct dahdi_span span;
141 struct dahdi_chan _chan;
142 struct dahdi_chan *chan;
143 int usecount;
144 int dead;
145 int pos;
146 unsigned long flags;
147 int freeregion;
148 int ring;
149 int offhook;
150 int battery;
151 int wregcount;
152 int readpos;
153 int rreadpos;
154 unsigned int pegtimer;
155 int pegcount;
156 int peg;
157 int battdebounce;
158 int nobatttimer;
159 int ringdebounce;
160 #ifdef JAPAN
161 int ohdebounce;
162 #endif
163 int allread;
164 int regoffset; /* How far off our registers are from what we expect */
165 int alt;
166 int ignoreread;
167 int reset;
168 /* Up to 6 register can be written at a time */
169 struct reg regs[DAHDI_CHUNKSIZE];
170 struct reg oldregs[DAHDI_CHUNKSIZE];
171 unsigned char lasttx[DAHDI_CHUNKSIZE];
172 /* Up to 32 registers of whatever we most recently read */
173 unsigned char readregs[32];
174 unsigned long ioaddr;
175 dma_addr_t readdma;
176 dma_addr_t writedma;
177 volatile int *writechunk; /* Double-word aligned write memory */
178 volatile int *readchunk; /* Double-word aligned read memory */
179 #ifdef ZERO_BATT_RING
180 int onhook;
181 #endif
182 #ifdef ENABLE_TASKLETS
183 int taskletrun;
184 int taskletsched;
185 int taskletpending;
186 int taskletexec;
187 int txerrors;
188 int ints;
189 struct tasklet_struct wcfxo_tlet;
190 #endif
191 };
192
193 #define FLAG_INVERTSER (1 << 0)
194 #define FLAG_USE_XTAL (1 << 1)
195 #define FLAG_DOUBLE_CLOCK (1 << 2)
196 #define FLAG_RESET_ON_AUX5 (1 << 3)
197 #define FLAG_NO_I18N_REGS (1 << 4) /*!< Uses si3035, rather si3034 */
198
199 struct wcfxo_desc {
200 char *name;
201 unsigned long flags;
202 };
203
204
205 static struct wcfxo_desc wcx100p = { "Wildcard X100P",
206 FLAG_INVERTSER | FLAG_USE_XTAL | FLAG_DOUBLE_CLOCK };
207
208 static struct wcfxo_desc wcx101p = { "Wildcard X101P",
209 FLAG_USE_XTAL | FLAG_DOUBLE_CLOCK };
210
211 static struct wcfxo_desc generic = { "Generic Clone",
212 FLAG_USE_XTAL | FLAG_DOUBLE_CLOCK };
213
214 static struct wcfxo *ifaces[WC_MAX_IFACES];
215
216 static void wcfxo_release(struct wcfxo *wc);
217
218 static int debug = 0;
219
220 static int monitor = 0;
221
222 static int quiet = 0;
223
224 static int boost = 0;
225
226 static int opermode = 0;
227
228 static struct fxo_mode {
229 char *name;
230 int ohs;
231 int act;
232 int dct;
233 int rz;
234 int rt;
235 int lim;
236 int vol;
237 } fxo_modes[] =
238 {
239 { "FCC", 0, 0, 2, 0, 0, 0, 0 }, /* US */
240 { "CTR21", 0, 0, 3, 0, 0, 3, 0 }, /* Austria, Belgium, Denmark, Finland, France, Germany,
241 Greece, Iceland, Ireland, Italy, Luxembourg, Netherlands,
242 Norway, Portugal, Spain, Sweden, Switzerland, and UK */
243 };
244
245 static inline void wcfxo_transmitprep(struct wcfxo *wc, unsigned char ints)
246 {
247 volatile int *writechunk;
248 int x;
249 int written=0;
250 unsigned short cmd;
251
252 /* if nothing to transmit, have to do the dahdi_transmit() anyway */
253 if (!(ints & 3)) {
254 /* Calculate Transmission */
255 dahdi_transmit(&wc->span);
256 return;
257 }
258
259 /* Remember what it was we just sent */
260 memcpy(wc->lasttx, wc->chan->writechunk, DAHDI_CHUNKSIZE);
261
262 if (ints & 0x01) {
263 /* Write is at interrupt address. Start writing from normal offset */
264 writechunk = wc->writechunk;
265 } else {
266 writechunk = wc->writechunk + DAHDI_CHUNKSIZE * 2;
267 }
268
269 dahdi_transmit(&wc->span);
270
271 for (x=0;x<DAHDI_CHUNKSIZE;x++) {
272 /* Send a sample, as a 32-bit word, and be sure to indicate that a command follows */
273 if (wc->flags & FLAG_INVERTSER)
274 writechunk[x << 1] = cpu_to_le32(
275 ~((unsigned short)(DAHDI_XLAW(wc->chan->writechunk[x], wc->chan))| 0x1) << 16
276 );
277 else
278 writechunk[x << 1] = cpu_to_le32(
279 ((unsigned short)(DAHDI_XLAW(wc->chan->writechunk[x], wc->chan))| 0x1) << 16
280 );
281
282 /* We always have a command to follow our signal */
283 if (!wc->regs[x].flags) {
284 /* Fill in an empty register command with a read for a potentially useful register */
285 wc->regs[x].flags = FLAG_READ;
286 wc->regs[x].reg = wecareregs[wc->readpos];
287 wc->regs[x].index = wc->readpos;
288 wc->readpos++;
289 if (wc->readpos >= (sizeof(wecareregs) / sizeof(wecareregs[0]))) {
290 wc->allread = 1;
291 wc->readpos = 0;
292 }
293 }
294
295 /* Prepare the command to follow it */
296 switch(wc->regs[x].flags) {
297 case FLAG_READ:
298 cmd = (wc->regs[x].reg | 0x20) << 8;
299 break;
300 case FLAG_WRITE:
301 cmd = (wc->regs[x].reg << 8) | (wc->regs[x].value & 0xff);
302 written = 1;
303 /* Wait at least four samples before reading */
304 wc->ignoreread = 4;
305 break;
306 default:
307 printk(KERN_DEBUG "wcfxo: Huh? No read or write??\n");
308 cmd = 0;
309 }
310 /* Setup the write chunk */
311 if (wc->flags & FLAG_INVERTSER)
312 writechunk[(x << 1) + 1] = cpu_to_le32(~(cmd << 16));
313 else
314 writechunk[(x << 1) + 1] = cpu_to_le32(cmd << 16);
315 }
316 if (written)
317 wc->readpos = 0;
318 wc->wregcount = 0;
319
320 for (x=0;x<DAHDI_CHUNKSIZE;x++) {
321 /* Rotate through registers */
322 wc->oldregs[x] = wc->regs[x];
323 wc->regs[x].flags = FLAG_EMPTY;
324 }
325
326 }
327
328 static inline void wcfxo_receiveprep(struct wcfxo *wc, unsigned char ints)
329 {
330 volatile int *readchunk;
331 int x;
332 int realreg;
333 int realval;
334 int sample;
335 if (ints & 0x04)
336 /* Read is at interrupt address. Valid data is available at normal offset */
337 readchunk = wc->readchunk;
338 else
339 readchunk = wc->readchunk + DAHDI_CHUNKSIZE * 2;
340
341 /* Keep track of how quickly our peg alternates */
342 wc->pegtimer+=DAHDI_CHUNKSIZE;
343 for (x=0;x<DAHDI_CHUNKSIZE;x++) {
344
345 /* We always have a command to follow our signal. */
346 if (wc->oldregs[x].flags == FLAG_READ && !wc->ignoreread) {
347 realreg = wecareregs[(wc->regs[x].index + wc->regoffset) %
348 (sizeof(wecareregs) / sizeof(wecareregs[0]))];
349 realval = (le32_to_cpu(readchunk[(x << 1) +wc->alt]) >> 16) & 0xff;
350 if ((realval == 0x89) && (realreg != WC_DAA_PLL2_N2_M2)) {
351 /* Some sort of slippage, correct for it */
352 while(realreg != WC_DAA_PLL2_N2_M2) {
353 /* Find register 9 */
354 realreg = wecareregs[(wc->regs[x].index + ++wc->regoffset) %
355 (sizeof(wecareregs) / sizeof(wecareregs[0]))];
356 wc->regoffset = wc->regoffset % (sizeof(wecareregs) / sizeof(wecareregs[0]));
357 }
358 if (debug)
359 printk(KERN_DEBUG "New regoffset: %d\n", wc->regoffset);
360 }
361 /* Receive into the proper register */
362 wc->readregs[realreg] = realval;
363 }
364 /* Look for pegging to indicate ringing */
365 sample = (short)(le32_to_cpu(readchunk[(x << 1) + (1 - wc->alt)]) >> 16);
366 if ((sample > 32000) && (wc->peg != 1)) {
367 if ((wc->pegtimer < PEGTIME) && (wc->pegtimer > MINPEGTIME))
368 wc->pegcount++;
369 wc->pegtimer = 0;
370 wc->peg = 1;
371 } else if ((sample < -32000) && (wc->peg != -1)) {
372 if ((wc->pegtimer < PEGTIME) && (wc->pegtimer > MINPEGTIME))
373 wc->pegcount++;
374 wc->pegtimer = 0;
375 wc->peg = -1;
376 }
377 wc->chan->readchunk[x] = DAHDI_LIN2X((sample), (wc->chan));
378 }
379 if (wc->pegtimer > PEGTIME) {
380 /* Reset pegcount if our timer expires */
381 wc->pegcount = 0;
382 }
383 /* Decrement debouncer if appropriate */
384 if (wc->ringdebounce)
385 wc->ringdebounce--;
386 if (!wc->offhook && !wc->ringdebounce) {
387 if (!wc->ring && (wc->pegcount > PEGCOUNT)) {
388 /* It's ringing */
389 if (debug)
390 printk(KERN_DEBUG "RING!\n");
391 dahdi_hooksig(wc->chan, DAHDI_RXSIG_RING);
392 wc->ring = 1;
393 }
394 if (wc->ring && !wc->pegcount) {
395 /* No more ring */
396 if (debug)
397 printk(KERN_DEBUG "NO RING!\n");
398 dahdi_hooksig(wc->chan, DAHDI_RXSIG_OFFHOOK);
399 wc->ring = 0;
400 }
401 }
402 if (wc->ignoreread)
403 wc->ignoreread--;
404
405 /* Do the echo cancellation... We are echo cancelling against
406 what we sent two chunks ago*/
407 dahdi_ec_chunk(wc->chan, wc->chan->readchunk, wc->lasttx);
408
409 /* Receive the result */
410 dahdi_receive(&wc->span);
411 }
412
413 #ifdef ENABLE_TASKLETS
414 static void wcfxo_tasklet(unsigned long data)
415 {
416 struct wcfxo *wc = (struct wcfxo *)data;
417 wc->taskletrun++;
418 /* Run tasklet */
419 if (wc->taskletpending) {
420 wc->taskletexec++;
421 wcfxo_receiveprep(wc, wc->ints);
422 wcfxo_transmitprep(wc, wc->ints);
423 }
424 wc->taskletpending = 0;
425 }
426 #endif
427
428 static void wcfxo_stop_dma(struct wcfxo *wc);
429 static void wcfxo_restart_dma(struct wcfxo *wc);
430
431 DAHDI_IRQ_HANDLER(wcfxo_interrupt)
432 {
433 struct wcfxo *wc = dev_id;
434 unsigned char ints;
435 unsigned char b;
436 #ifdef DEBUG_RING
437 static int oldb = 0;
438 static int oldcnt = 0;
439 #endif
440
441 ints = inb(wc->ioaddr + WC_INTSTAT);
442
443
444 if (!ints)
445 return IRQ_NONE;
446
447 outb(ints, wc->ioaddr + WC_INTSTAT);
448
449 if (ints & 0x0c) { /* if there is a rx interrupt pending */
450 #ifdef ENABLE_TASKLETS
451 wc->ints = ints;
452 if (!wc->taskletpending) {
453 wc->taskletpending = 1;
454 wc->taskletsched++;
455 tasklet_hi_schedule(&wc->wcfxo_tlet);
456 } else
457 wc->txerrors++;
458 #else
459 wcfxo_receiveprep(wc, ints);
460 /* transmitprep looks to see if there is anything to transmit
461 and returns by itself if there is nothing */
462 wcfxo_transmitprep(wc, ints);
463 #endif
464 }
465
466 if (ints & 0x10) {
467 printk(KERN_INFO "FXO PCI Master abort\n");
468 /* Stop DMA andlet the watchdog start it again */
469 wcfxo_stop_dma(wc);
470 return IRQ_RETVAL(1);
471 }
472
473 if (ints & 0x20) {
474 printk(KERN_INFO "PCI Target abort\n");
475 return IRQ_RETVAL(1);
476 }
477 if (1 /* !(wc->report % 0xf) */) {
478 /* Check for BATTERY from register and debounce for 8 ms */
479 b = wc->readregs[WC_DAA_LINE_STAT] & 0xf;
480 if (!b) {
481 wc->nobatttimer++;
482 #if 0
483 if (wc->battery)
484 printk(KERN_DEBUG "Battery loss: %d (%d debounce)\n", b, wc->battdebounce);
485 #endif
486 if (wc->battery && !wc->battdebounce) {
487 if (debug)
488 printk(KERN_DEBUG "NO BATTERY!\n");
489 wc->battery = 0;
490 #ifdef JAPAN
491 if ((!wc->ohdebounce) && wc->offhook) {
492 dahdi_hooksig(wc->chan, DAHDI_RXSIG_ONHOOK);
493 if (debug)
494 printk(KERN_DEBUG "Signalled On Hook\n");
495 #ifdef ZERO_BATT_RING
496 wc->onhook++;
497 #endif
498 }
499 #else
500 dahdi_hooksig(wc->chan, DAHDI_RXSIG_ONHOOK);
501 #endif
502 wc->battdebounce = BATT_DEBOUNCE;
503 } else if (!wc->battery)
504 wc->battdebounce = BATT_DEBOUNCE;
505 if ((wc->nobatttimer > 5000) &&
506 #ifdef ZERO_BATT_RING
507 !(wc->readregs[WC_DAA_DCTL1] & 0x04) &&
508 #endif
509 (!wc->span.alarms)) {
510 wc->span.alarms = DAHDI_ALARM_RED;
511 dahdi_alarm_notify(&wc->span);
512 }
513 } else if (b == 0xf) {
514 if (!wc->battery && !wc->battdebounce) {
515 if (debug)
516 printk(KERN_DEBUG "BATTERY!\n");
517 #ifdef ZERO_BATT_RING
518 if (wc->onhook) {
519 wc->onhook = 0;
520 dahdi_hooksig(wc->chan, DAHDI_RXSIG_OFFHOOK);
521 if (debug)
522 printk(KERN_DEBUG "Signalled Off Hook\n");
523 }
524 #else
525 dahdi_hooksig(wc->chan, DAHDI_RXSIG_OFFHOOK);
526 #endif
527 wc->battery = 1;
528 wc->nobatttimer = 0;
529 wc->battdebounce = BATT_DEBOUNCE;
530 if (wc->span.alarms) {
531 wc->span.alarms = 0;
532 dahdi_alarm_notify(&wc->span);
533 }
534 } else if (wc->battery)
535 wc->battdebounce = BATT_DEBOUNCE;
536 } else {
537 /* It's something else... */
538 wc->battdebounce = BATT_DEBOUNCE;
539 }
540
541 if (wc->battdebounce)
542 wc->battdebounce--;
543 #ifdef JAPAN
544 if (wc->ohdebounce)
545 wc->ohdebounce--;
546 #endif
547
548 }
549
550 return IRQ_RETVAL(1);
551 }
552
553 static int wcfxo_setreg(struct wcfxo *wc, unsigned char reg, unsigned char value)
554 {
555 int x;
556 if (wc->wregcount < DAHDI_CHUNKSIZE) {
557 x = wc->wregcount;
558 wc->regs[x].reg = reg;
559 wc->regs[x].value = value;
560 wc->regs[x].flags = FLAG_WRITE;
561 wc->wregcount++;
562 return 0;
563 }
564 printk(KERN_NOTICE "wcfxo: Out of space to write register %02x with %02x\n", reg, value);
565 return -1;
566 }
567
568 static inline struct wcfxo *wcfxo_from_span(struct dahdi_span *span)
569 {
570 return container_of(span, struct wcfxo, span);
571 }
572
573 static int _wcfxo_open(struct dahdi_chan *chan)
574 {
575 struct wcfxo *wc = chan->pvt;
576 if (wc->dead)
577 return -ENODEV;
578 wc->usecount++;
579 return 0;
580 }
581
582 static int wcfxo_open(struct dahdi_chan *chan)
583 {
584 int res;
585 unsigned long flags;
586 spin_lock_irqsave(&chan->lock, flags);
587 res = _wcfxo_open(chan);
588 spin_unlock_irqrestore(&chan->lock, flags);
589 return res;
590 }
591
592 static int wcfxo_watchdog(struct dahdi_span *span, int event)
593 {
594 printk(KERN_INFO "FXO: Restarting DMA\n");
595 wcfxo_restart_dma(wcfxo_from_span(span));
596 return 0;
597 }
598
599 static int wcfxo_close(struct dahdi_chan *chan)
600 {
601 struct wcfxo *wc = chan->pvt;
602 wc->usecount--;
603 /* If we're dead, release us now */
604 if (!wc->usecount && wc->dead)
605 wcfxo_release(wc);
606 return 0;
607 }
608
609 static int wcfxo_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
610 {
611 struct wcfxo *wc = chan->pvt;
612 int reg=0;
613 switch(txsig) {
614 case DAHDI_TXSIG_START:
615 case DAHDI_TXSIG_OFFHOOK:
616 /* Take off hook and enable normal mode reception. This must
617 be done in two steps because of a hardware bug. */
618 reg = wc->readregs[WC_DAA_DCTL1] & ~0x08;
619 wcfxo_setreg(wc, WC_DAA_DCTL1, reg);
620
621 reg = reg | 0x1;
622 wcfxo_setreg(wc, WC_DAA_DCTL1, reg);
623 wc->offhook = 1;
624 #ifdef JAPAN
625 wc->battery = 1;
626 wc->battdebounce = BATT_DEBOUNCE;
627 wc->ohdebounce = OH_DEBOUNCE;
628 #endif
629 break;
630 case DAHDI_TXSIG_ONHOOK:
631 /* Put on hook and enable on hook line monitor */
632 reg = wc->readregs[WC_DAA_DCTL1] & 0xfe;
633 wcfxo_setreg(wc, WC_DAA_DCTL1, reg);
634
635 reg = reg | 0x08;
636 wcfxo_setreg(wc, WC_DAA_DCTL1, reg);
637 wc->offhook = 0;
638 /* Don't accept a ring for another 1000 ms */
639 wc->ringdebounce = 1000;
640 #ifdef JAPAN
641 wc->ohdebounce = OH_DEBOUNCE;
642 #endif
643 break;
644 default:
645 printk(KERN_NOTICE "wcfxo: Can't set tx state to %d\n", txsig);
646 }
647 if (debug)
648 printk(KERN_DEBUG "Setting hook state to %d (%02x)\n", txsig, reg);
649 return 0;
650 }
651
652 static const struct dahdi_span_ops wcfxo_span_ops = {
653 .owner = THIS_MODULE,
654 .hooksig = wcfxo_hooksig,
655 .open = wcfxo_open,
656 .close = wcfxo_close,
657 .watchdog = wcfxo_watchdog,
658 };
659
660 static int wcfxo_initialize(struct wcfxo *wc)
661 {
662 wc->ddev = dahdi_create_device();
663
664 /* DAHDI stuff */
665 sprintf(wc->span.name, "WCFXO/%d", wc->pos);
666 snprintf(wc->span.desc, sizeof(wc->span.desc) - 1, "%s Board %d", wc->variety, wc->pos + 1);
667 sprintf(wc->chan->name, "WCFXO/%d/%d", wc->pos, 0);
668 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
669 wc->dev->bus->number,
670 PCI_SLOT(wc->dev->devfn) + 1);
671 if (!wc->ddev->location)
672 return -ENOMEM;
673
674 wc->ddev->manufacturer = "Digium";
675 wc->ddev->devicetype = wc->variety;
676 wc->chan->sigcap = DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS | DAHDI_SIG_SF;
677 wc->chan->chanpos = 1;
678 wc->span.chans = &wc->chan;
679 wc->span.channels = 1;
680 wc->span.flags = DAHDI_FLAG_RBS;
681 wc->span.deflaw = DAHDI_LAW_MULAW;
682 #ifdef ENABLE_TASKLETS
683 tasklet_init(&wc->wcfxo_tlet, wcfxo_tasklet, (unsigned long)wc);
684 #endif
685
686 wc->chan->pvt = wc;
687 wc->span.ops = &wcfxo_span_ops;
688 wc->span.spantype = SPANTYPE_ANALOG_FXO;
689 list_add_tail(&wc->span.device_node, &wc->ddev->spans);
690 if (dahdi_register_device(wc->ddev, &wc->dev->dev)) {
691 printk(KERN_NOTICE "Unable to register span with DAHDI\n");
692 return -1;
693 }
694 return 0;
695 }
696
697 static int wcfxo_hardware_init(struct wcfxo *wc)
698 {
699 /* Hardware stuff */
700 /* Reset PCI Interface chip and registers */
701 outb(0x0e, wc->ioaddr + WC_CNTL);
702
703 /* Set all to outputs except AUX 4, which is an input */
704 outb(0xef, wc->ioaddr + WC_AUXC);
705
706 /* Reset the DAA (DAA uses AUX5 for reset) */
707 outb(0x00, wc->ioaddr + WC_AUXD);
708 set_current_state(TASK_INTERRUPTIBLE);
709 schedule_timeout(1 + HZ / 800);
710
711 /* Set hook state to on hook & un-reset the DAA */
712 if (wc->flags & FLAG_RESET_ON_AUX5) {
713 /* Set hook state to on hook for when we switch.
714 Make sure reset is high */
715 outb(0x34, wc->ioaddr + WC_AUXD);
716 } else {
717 /* Set hook state to on hook for when we switch */
718 outb(0x24, wc->ioaddr + WC_AUXD);
719 }
720
721 /* Back to normal, with automatic DMA wrap around */
722 outb(0x01, wc->ioaddr + WC_CNTL);
723
724 /* Make sure serial port and DMA are out of reset */
725 outb(inb(wc->ioaddr + WC_CNTL) & 0xf9, wc->ioaddr + WC_CNTL);
726
727 /* Configure serial port for MSB->LSB operation */
728 if (wc->flags & FLAG_DOUBLE_CLOCK)
729 outb(0xc1, wc->ioaddr + WC_SERCTL);
730 else
731 outb(0xc0, wc->ioaddr + WC_SERCTL);
732
733 if (wc->flags & FLAG_USE_XTAL) {
734 /* Use the crystal oscillator */
735 outb(0x04, wc->ioaddr + WC_AUXFUNC);
736 }
737
738 /* Delay FSC by 2 so it's properly aligned */
739 outb(0x2, wc->ioaddr + WC_FSCDELAY);
740
741 /* Setup DMA Addresses */
742 outl(wc->writedma, wc->ioaddr + WC_DMAWS); /* Write start */
743 outl(wc->writedma + DAHDI_CHUNKSIZE * 8 - 4, wc->ioaddr + WC_DMAWI); /* Middle (interrupt) */
744 outl(wc->writedma + DAHDI_CHUNKSIZE * 16 - 4, wc->ioaddr + WC_DMAWE); /* End */
745
746 outl(wc->readdma, wc->ioaddr + WC_DMARS); /* Read start */
747 outl(wc->readdma + DAHDI_CHUNKSIZE * 8 - 4, wc->ioaddr + WC_DMARI); /* Middle (interrupt) */
748 outl(wc->readdma + DAHDI_CHUNKSIZE * 16 - 4, wc->ioaddr + WC_DMARE); /* End */
749
750 /* Clear interrupts */
751 outb(0xff, wc->ioaddr + WC_INTSTAT);
752 return 0;
753 }
754
755 static void wcfxo_enable_interrupts(struct wcfxo *wc)
756 {
757 /* Enable interrupts (we care about all of them) */
758 outb(0x3f, wc->ioaddr + WC_MASK0);
759 /* No external interrupts */
760 outb(0x00, wc->ioaddr + WC_MASK1);
761 }
762
763 static void wcfxo_start_dma(struct wcfxo *wc)
764 {
765 /* Reset Master and TDM */
766 outb(0x0f, wc->ioaddr + WC_CNTL);
767 set_current_state(TASK_INTERRUPTIBLE);
768 schedule_timeout(1);
769 outb(0x01, wc->ioaddr + WC_CNTL);
770 outb(0x01, wc->ioaddr + WC_OPER);
771 }
772
773 static void wcfxo_restart_dma(struct wcfxo *wc)
774 {
775 /* Reset Master and TDM */
776 outb(0x01, wc->ioaddr + WC_CNTL);
777 outb(0x01, wc->ioaddr + WC_OPER);
778 }
779
780
781 static void wcfxo_stop_dma(struct wcfxo *wc)
782 {
783 outb(0x00, wc->ioaddr + WC_OPER);
784 }
785
786 static void wcfxo_reset_tdm(struct wcfxo *wc)
787 {
788 /* Reset TDM */
789 outb(0x0f, wc->ioaddr + WC_CNTL);
790 }
791
792 static void wcfxo_disable_interrupts(struct wcfxo *wc)
793 {
794 outb(0x00, wc->ioaddr + WC_MASK0);
795 outb(0x00, wc->ioaddr + WC_MASK1);
796 }
797
798 static void wcfxo_set_daa_mode(struct wcfxo *wc)
799 {
800 /* Set country specific parameters (OHS, ACT, DCT, RZ, RT, LIM, VOL) */
801 int reg16 = ((fxo_modes[opermode].ohs & 0x1) << 6) |
802 ((fxo_modes[opermode].act & 0x1) << 5) |
803 ((fxo_modes[opermode].dct & 0x3) << 2) |
804 ((fxo_modes[opermode].rz & 0x1) << 1) |
805 ((fxo_modes[opermode].rt & 0x1) << 0);
806 int reg17 = ((fxo_modes[opermode].lim & 0x3) << 3);
807 int reg18 = ((fxo_modes[opermode].vol & 0x3) << 3);
808
809 if (wc->flags & FLAG_NO_I18N_REGS) {
810 wcfxo_dbg(wc->span, "This card does not support international settings.\n");
811 return;
812 }
813
814 wcfxo_setreg(wc, WC_DAA_INT_CTL1, reg16);
815 wcfxo_setreg(wc, WC_DAA_INT_CTL2, reg17);
816 wcfxo_setreg(wc, WC_DAA_INT_CTL3, reg18);
817
818
819 /* Wait a couple of jiffies for our writes to finish */
820 set_current_state(TASK_INTERRUPTIBLE);
821 schedule_timeout(1 + (DAHDI_CHUNKSIZE * HZ) / 800);
822
823 printk(KERN_INFO "wcfxo: DAA mode is '%s'\n", fxo_modes[opermode].name);
824 }
825
826 static int wcfxo_init_daa(struct wcfxo *wc)
827 {
828 /* This must not be called in an interrupt */
829 /* We let things settle for a bit */
830 unsigned char reg15;
831 int chip_revb;
832 // set_current_state(TASK_INTERRUPTIBLE);
833 // schedule_timeout(10);
834
835 /* Soft-reset it */
836 wcfxo_setreg(wc, WC_DAA_CTL1, 0x80);
837
838 /* Let the reset go */
839 set_current_state(TASK_UNINTERRUPTIBLE);
840 schedule_timeout(1 + (DAHDI_CHUNKSIZE * HZ) / 800);
841
842 /* We have a clock at 18.432 Mhz, so N1=1, M1=2, CGM=0 */
843 wcfxo_setreg(wc, WC_DAA_PLL1_N1, 0x0); /* This value is N1 - 1 */
844 wcfxo_setreg(wc, WC_DAA_PLL1_M1, 0x1); /* This value is M1 - 1 */
845 /* We want to sample at 8khz, so N2 = 9, M2 = 10 (N2-1, M2-1) */
846 wcfxo_setreg(wc, WC_DAA_PLL2_N2_M2, 0x89);
847
848 /* Wait until the PLL's are locked. Time is between 100 uSec and 1 mSec */
849 set_current_state(TASK_INTERRUPTIBLE);
850 schedule_timeout(1 + HZ/1000 + (DAHDI_CHUNKSIZE * HZ) / 800);
851
852 /* No additional ration is applied to the PLL and faster lock times
853 * are possible */
854 wcfxo_setreg(wc, WC_DAA_PLL_CTL, 0x0);
855 /* Enable off hook pin */
856 wcfxo_setreg(wc, WC_DAA_DCTL1, 0x0a);
857 if (monitor) {
858 /* Enable ISOcap and external speaker and charge pump if present */
859 wcfxo_setreg(wc, WC_DAA_DCTL2, 0x80);
860 } else {
861 /* Enable ISOcap and charge pump if present (leave speaker disabled) */
862 wcfxo_setreg(wc, WC_DAA_DCTL2, 0xe0);
863 }
864
865 /* Wait a couple of jiffies for our writes to finish */
866 set_current_state(TASK_INTERRUPTIBLE);
867 schedule_timeout(1 + (DAHDI_CHUNKSIZE * HZ) / 800);
868 reg15 = 0x0;
869 /* Go ahead and attenuate transmit signal by 6 db */
870 if (quiet) {
871 printk(KERN_INFO "wcfxo: Attenuating transmit signal for quiet operation\n");
872 reg15 |= (quiet & 0x3) << 4;
873 }
874 if (boost) {
875 printk(KERN_INFO "wcfxo: Boosting receive signal\n");
876 reg15 |= (boost & 0x3);
877 }
878 wcfxo_setreg(wc, WC_DAA_TXRX_GCTL, reg15);
879
880 /* REVB: reg. 13, bits 5:2 */
881 chip_revb = (wc->readregs[WC_DAA_CHIPB_REV] >> 2) & 0xF;
882 wcfxo_dbg(wc->span, "DAA chip REVB is %x\n", chip_revb);
883 switch(chip_revb) {
884 case 1: case 2: case 3:
885 /* This is a si3034. Nothing to do */
886 break;
887 case 4: case 5: case 7:
888 /* This is 3035. Has no support for international registers */
889 wc->flags |= FLAG_NO_I18N_REGS;
890 break;
891 default:
892 wcfxo_notice(wc->span, "Unknown DAA chip revision: REVB=%d\n",
893 chip_revb);
894 }
895
896 /* Didn't get it right. Register 9 is still garbage */
897 if (wc->readregs[WC_DAA_PLL2_N2_M2] != 0x89)
898 return -1;
899 #if 0
900 { int x;
901 int y;
902 for (y=0;y<100;y++) {
903 printk(KERN_DEBUG " reg dump ====== %d ======\n", y);
904 for (x=0;x<sizeof(wecareregs) / sizeof(wecareregs[0]);x++) {
905 printk(KERN_DEBUG "daa: Reg %d: %02x\n", wecareregs[x], wc->readregs[wecareregs[x]]);
906 }
907 set_current_state(TASK_INTERRUPTIBLE);
908 schedule_timeout(100);
909 } }
910 #endif
911 return 0;
912 }
913
914 static int __devinit wcfxo_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
915 {
916 struct wcfxo *wc;
917 struct wcfxo_desc *d = (struct wcfxo_desc *)ent->driver_data;
918 int x;
919
920 for (x=0;x<WC_MAX_IFACES;x++)
921 if (!ifaces[x]) break;
922 if (x >= WC_MAX_IFACES) {
923 printk(KERN_ERR "Too many interfaces: Found %d, can only handle %d.\n",
924 x, WC_MAX_IFACES - 1);
925 return -EIO;
926 }
927
928 if (pci_enable_device(pdev))
929 return -EIO;
930
931 wc = kmalloc(sizeof(struct wcfxo), GFP_KERNEL);
932 if (!wc) {
933 printk(KERN_ERR "wcfxo: Failed initializinf card. Not enough memory.");
934 return -ENOMEM;
935 }
936
937 ifaces[x] = wc;
938 memset(wc, 0, sizeof(struct wcfxo));
939 wc->chan = &wc->_chan;
940 wc->ioaddr = pci_resource_start(pdev, 0);
941 wc->dev = pdev;
942 wc->pos = x;
943 wc->variety = d->name;
944 wc->flags = d->flags;
945 /* Keep track of whether we need to free the region */
946 if (request_region(wc->ioaddr, 0xff, "wcfxo"))
947 wc->freeregion = 1;
948
949 /* Allocate enough memory for two zt chunks, receive and transmit. Each sample uses
950 32 bits. Allocate an extra set just for control too */
951 wc->writechunk = (int *)pci_alloc_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 2 * 2 * 2 * 4, &wc->writedma);
952 if (!wc->writechunk) {
953 printk(KERN_NOTICE "wcfxo: Unable to allocate DMA-able memory\n");
954 if (wc->freeregion)
955 release_region(wc->ioaddr, 0xff);
956 return -ENOMEM;
957 }
958
959 wc->readchunk = wc->writechunk + DAHDI_MAX_CHUNKSIZE * 4; /* in doublewords */
960 wc->readdma = wc->writedma + DAHDI_MAX_CHUNKSIZE * 16; /* in bytes */
961
962 if (wcfxo_initialize(wc)) {
963 printk(KERN_NOTICE "wcfxo: Unable to intialize modem\n");
964 if (wc->freeregion)
965 release_region(wc->ioaddr, 0xff);
966 kfree(wc);
967 return -EIO;
968 }
969
970 /* Enable bus mastering */
971 pci_set_master(pdev);
972
973 /* Keep track of which device we are */
974 pci_set_drvdata(pdev, wc);
975
976 if (request_irq(pdev->irq, wcfxo_interrupt, IRQF_SHARED, "wcfxo", wc)) {
977 printk(KERN_NOTICE "wcfxo: Unable to request IRQ %d\n", pdev->irq);
978 if (wc->freeregion)
979 release_region(wc->ioaddr, 0xff);
980 kfree(wc);
981 return -EIO;
982 }
983
984
985 wcfxo_hardware_init(wc);
986 /* Enable interrupts */
987 wcfxo_enable_interrupts(wc);
988 /* Initialize Write/Buffers to all blank data */
989 memset((void *)wc->writechunk,0,DAHDI_MAX_CHUNKSIZE * 2 * 2 * 2 * 4);
990 /* Start DMA */
991 wcfxo_start_dma(wc);
992
993 /* Initialize DAA (after it's started) */
994 if (wcfxo_init_daa(wc)) {
995 printk(KERN_NOTICE "Failed to initailize DAA, giving up...\n");
996 wcfxo_stop_dma(wc);
997 wcfxo_disable_interrupts(wc);
998 dahdi_unregister_device(wc->ddev);
999 free_irq(pdev->irq, wc);
1000
1001 /* Reset PCI chip and registers */
1002 outb(0x0e, wc->ioaddr + WC_CNTL);
1003
1004 if (wc->freeregion)
1005 release_region(wc->ioaddr, 0xff);
1006 kfree(wc->ddev->location);
1007 dahdi_free_device(wc->ddev);
1008 kfree(wc);
1009 return -EIO;
1010 }
1011 wcfxo_set_daa_mode(wc);
1012 printk(KERN_INFO "Found a Wildcard FXO: %s\n", wc->variety);
1013
1014 return 0;
1015 }
1016
1017 static void wcfxo_release(struct wcfxo *wc)
1018 {
1019 dahdi_unregister_device(wc->ddev);
1020 if (wc->freeregion)
1021 release_region(wc->ioaddr, 0xff);
1022 kfree(wc->ddev->location);
1023 dahdi_free_device(wc->ddev);
1024 kfree(wc);
1025 printk(KERN_INFO "Freed a Wildcard\n");
1026 }
1027
1028 static void __devexit wcfxo_remove_one(struct pci_dev *pdev)
1029 {
1030 struct wcfxo *wc = pci_get_drvdata(pdev);
1031 if (wc) {
1032
1033 /* Stop any DMA */
1034 wcfxo_stop_dma(wc);
1035 wcfxo_reset_tdm(wc);
1036
1037 /* In case hardware is still there */
1038 wcfxo_disable_interrupts(wc);
1039
1040 /* Immediately free resources */
1041 pci_free_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 2 * 2 * 2 * 4, (void *)wc->writechunk, wc->writedma);
1042 free_irq(pdev->irq, wc);
1043
1044 /* Reset PCI chip and registers */
1045 outb(0x0e, wc->ioaddr + WC_CNTL);
1046
1047 /* Release span, possibly delayed */
1048 if (!wc->usecount)
1049 wcfxo_release(wc);
1050 else
1051 wc->dead = 1;
1052 }
1053 }
1054
1055 static DEFINE_PCI_DEVICE_TABLE(wcfxo_pci_tbl) = {
1056 { 0xe159, 0x0001, 0x8084, PCI_ANY_ID, 0, 0, (unsigned long) &generic },
1057 { 0xe159, 0x0001, 0x8085, PCI_ANY_ID, 0, 0, (unsigned long) &wcx101p },
1058 { 0xe159, 0x0001, 0x8086, PCI_ANY_ID, 0, 0, (unsigned long) &generic },
1059 { 0xe159, 0x0001, 0x8087, PCI_ANY_ID, 0, 0, (unsigned long) &generic },
1060 { 0x1057, 0x5608, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &wcx100p },
1061 { 0 }
1062 };
1063
1064 MODULE_DEVICE_TABLE (pci, wcfxo_pci_tbl);
1065
1066 static struct pci_driver wcfxo_driver = {
1067 .name = "wcfxo",
1068 .probe = wcfxo_init_one,
1069 .remove = __devexit_p(wcfxo_remove_one),
1070 .id_table = wcfxo_pci_tbl,
1071 };
1072
1073 static int __init wcfxo_init(void)
1074 {
1075 int res;
1076 int x;
1077 if ((opermode >= sizeof(fxo_modes) / sizeof(fxo_modes[0])) || (opermode < 0)) {
1078 printk(KERN_NOTICE "Invalid/unknown operating mode specified. Please choose one of:\n");
1079 for (x=0;x<sizeof(fxo_modes) / sizeof(fxo_modes[0]); x++)
1080 printk(KERN_INFO "%d: %s\n", x, fxo_modes[x].name);
1081 return -ENODEV;
1082 }
1083 res = dahdi_pci_module(&wcfxo_driver);
1084 if (res)
1085 return -ENODEV;
1086 return 0;
1087 }
1088
1089 static void __exit wcfxo_cleanup(void)
1090 {
1091 pci_unregister_driver(&wcfxo_driver);
1092 }
1093
1094 module_param(debug, int, 0644);
1095 module_param(quiet, int, 0444);
1096 module_param(boost, int, 0444);
1097 module_param(monitor, int, 0444);
1098 module_param(opermode, int, 0444);
1099
1100 MODULE_DESCRIPTION("Wildcard X100P Driver");
1101 MODULE_AUTHOR("Mark Spencer <[email protected]>");
1102 MODULE_LICENSE("GPL v2");
1103
1104 module_init(wcfxo_init);
1105 module_exit(wcfxo_cleanup);