Debian Codegraph @

drivers/dahdi/wct1xxp.c

raw symbols SCIP
1 /*
2 * Digium, Inc. Wildcard T100P T1/PRI card Driver
3 *
4 * Written by Mark Spencer <[email protected]>
5 * Matthew Fredrickson <[email protected]>
6 * William Meadows <[email protected]>
7 *
8 * Copyright (C) 2001-2008, Digium, Inc.
9 *
10 * All rights reserved.
11 *
12 */
13
14 /*
15 * See http://www.asterisk.org for more information about
16 * the Asterisk project. Please do not directly contact
17 * any of the maintainers of this project for assistance;
18 * the project provides a web site, mailing lists and IRC
19 * channels for your use.
20 *
21 * This program is free software, distributed under the terms of
22 * the GNU General Public License Version 2 as published by the
23 * Free Software Foundation. See the LICENSE file included with
24 * this program for more details.
25 */
26
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/usb.h>
32 #include <linux/pci.h>
33 #include <linux/spinlock.h>
34 #include <linux/moduleparam.h>
35 #include <linux/slab.h>
36
37 #include <dahdi/kernel.h>
38
39 #define WC_MAX_CARDS 32
40
41 /*
42 #define TEST_REGS
43 */
44
45 /* Define to get more attention-grabbing but slightly more I/O using
46 alarm status */
47 #define FANCY_ALARM
48
49
50 #define DELAY 0x0 /* 30 = 15 cycles, 10 = 8 cycles, 0 = 3 cycles */
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 #define WC_CURPOS 0x24
67
68 #define WC_SERC 0x2d
69 #define WC_FSCDELAY 0x2f
70
71 #define WC_USERREG 0xc0
72
73 #define WC_CLOCK 0x0
74 #define WC_LEDTEST 0x1
75 #define WC_VERSION 0x2
76
77 /* Offset between transmit and receive */
78 #define WC_OFFSET 4
79
80 #define BIT_CS (1 << 7)
81 #define BIT_ADDR (0xf << 3)
82
83 #define BIT_LED0 (1 << 0)
84 #define BIT_LED1 (1 << 1)
85 #define BIT_TEST (1 << 2)
86
87 static char *chips[] =
88 {
89 "DS2152",
90 "DS21352",
91 "DS21552",
92 "Unknown Chip (3)",
93 "DS2154",
94 "DS21354",
95 "DS21554",
96 "Unknown Chip (7)",
97 };
98
99 static int chanmap_t1[] =
100 { 2,1,0,
101 6,5,4,
102 10,9,8,
103 14,13,12,
104 18,17,16,
105 22,21,20,
106 26,25,24,
107 30,29,28 };
108
109 static int chanmap_e1[] =
110 { 2,1,0,
111 7,6,5,4,
112 11,10,9,8,
113 15,14,13,12,
114 19,18,17,16,
115 23,22,21,20,
116 27,26,25,24,
117 31,30,29,28 };
118
119 #ifdef FANCY_ALARM
120 static int altab[] = {
121 0, 0, 0, 1, 2, 3, 4, 6, 8, 9, 11, 13, 16, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 31, 32, 31, 31, 30, 29, 28, 27, 25, 23, 22, 20, 18, 16, 13, 11, 9, 8, 6, 4, 3, 2, 1, 0, 0,
122 };
123 #endif
124
125 struct t1xxp {
126 struct pci_dev *dev;
127 spinlock_t lock;
128 int ise1;
129 int num;
130 /* Our offset for finding channel 1 */
131 int offset;
132 char *variety;
133 unsigned int intcount;
134 int usecount;
135 int clocktimeout;
136 int sync;
137 int dead;
138 int blinktimer;
139 int alarmtimer;
140 int loopupcnt;
141 int loopdowncnt;
142 int miss;
143 int misslast;
144 int *chanmap;
145 #ifdef FANCY_ALARM
146 int alarmpos;
147 #endif
148 unsigned char ledtestreg;
149 unsigned char outbyte;
150 unsigned long ioaddr;
151 unsigned short canary;
152 /* T1 signalling */
153 unsigned char txsiga[3];
154 unsigned char txsigb[3];
155 dma_addr_t readdma;
156 dma_addr_t writedma;
157 volatile unsigned char *writechunk; /* Double-word aligned write memory */
158 volatile unsigned char *readchunk; /* Double-word aligned read memory */
159 unsigned char ec_chunk1[31][DAHDI_CHUNKSIZE];
160 unsigned char ec_chunk2[31][DAHDI_CHUNKSIZE];
161 unsigned char tempo[32];
162 struct dahdi_span span; /* Span */
163 struct dahdi_device *ddev;
164 struct dahdi_chan *chans[31]; /* Channels */
165 };
166
167 #define CANARY 0xca1e
168
169 static int debug = 0; /* doesnt do anything */
170
171 static struct t1xxp *cards[WC_MAX_CARDS];
172
173 static inline void start_alarm(struct t1xxp *wc)
174 {
175 #ifdef FANCY_ALARM
176 wc->alarmpos = 0;
177 #endif
178 wc->blinktimer = 0;
179 }
180
181 static inline void stop_alarm(struct t1xxp *wc)
182 {
183 #ifdef FANCY_ALARM
184 wc->alarmpos = 0;
185 #endif
186 wc->blinktimer = 0;
187 }
188
189 static inline void __select_framer(struct t1xxp *wc, int reg)
190 {
191 /* Top four bits of address from AUX 6-3 */
192 wc->outbyte &= ~BIT_CS;
193 wc->outbyte &= ~BIT_ADDR;
194 wc->outbyte |= (reg & 0xf0) >> 1;
195 outb(wc->outbyte, wc->ioaddr + WC_AUXD);
196 }
197
198 static inline void __select_control(struct t1xxp *wc)
199 {
200 if (!(wc->outbyte & BIT_CS)) {
201 wc->outbyte |= BIT_CS;
202 outb(wc->outbyte, wc->ioaddr + WC_AUXD);
203 }
204 }
205
206 static int t1xxp_open(struct dahdi_chan *chan)
207 {
208 struct t1xxp *wc = chan->pvt;
209 if (wc->dead)
210 return -ENODEV;
211 wc->usecount++;
212 return 0;
213 }
214
215 static int __t1_get_reg(struct t1xxp *wc, int reg)
216 {
217 unsigned char res;
218 __select_framer(wc, reg);
219 /* Get value */
220 res = inb(wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
221 return res;
222 }
223
224 static int __t1_set_reg(struct t1xxp *wc, int reg, unsigned char val)
225 {
226 __select_framer(wc, reg);
227 /* Send address */
228 outb(val, wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
229 return 0;
230 }
231
232 static int __control_set_reg(struct t1xxp *wc, int reg, unsigned char val)
233 {
234 __select_control(wc);
235 outb(val, wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
236 return 0;
237 }
238
239 static int control_set_reg(struct t1xxp *wc, int reg, unsigned char val)
240 {
241 unsigned long flags;
242 int res;
243 spin_lock_irqsave(&wc->lock, flags);
244 res = __control_set_reg(wc, reg, val);
245 spin_unlock_irqrestore(&wc->lock, flags);
246 return res;
247 }
248
249 static int __control_get_reg(struct t1xxp *wc, int reg)
250 {
251 unsigned char res;
252 /* The following makes UTTERLY no sense, but what was happening
253 was that reads in some cases were not actually happening
254 on the physical bus. Why, we dunno. But in debugging, we found
255 that writing before reading (in this case to an unused position)
256 seems to get rid of the problem */
257 __control_set_reg(wc,3,0x69); /* do magic here */
258 /* now get the read byte from the Xilinx part */
259 res = inb(wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
260 return res;
261 }
262
263 static int control_get_reg(struct t1xxp *wc, int reg)
264 {
265 unsigned long flags;
266 int res;
267 spin_lock_irqsave(&wc->lock, flags);
268 res = __control_get_reg(wc, reg);
269 spin_unlock_irqrestore(&wc->lock, flags);
270 return res;
271 }
272
273 static void t1xxp_release(struct t1xxp *wc)
274 {
275 unsigned int x;
276
277 dahdi_unregister_device(wc->ddev);
278 for (x = 0; x < (wc->ise1 ? 31 : 24); x++) {
279 kfree(wc->chans[x]);
280 }
281 dahdi_free_device(wc->ddev);
282 kfree(wc);
283 printk(KERN_INFO "Freed a Wildcard\n");
284 }
285
286 static int t1xxp_close(struct dahdi_chan *chan)
287 {
288 struct t1xxp *wc = chan->pvt;
289 wc->usecount--;
290 /* If we're dead, release us now */
291 if (!wc->usecount && wc->dead)
292 t1xxp_release(wc);
293 return 0;
294 }
295
296 static void t1xxp_enable_interrupts(struct t1xxp *wc)
297 {
298 /* Clear interrupts */
299 outb(0xff, wc->ioaddr + WC_INTSTAT);
300 /* Enable interrupts (we care about all of them) */
301 outb(0x3c /* 0x3f */, wc->ioaddr + WC_MASK0);
302 /* No external interrupts */
303 outb(0x00, wc->ioaddr + WC_MASK1);
304 }
305
306 static void t1xxp_start_dma(struct t1xxp *wc)
307 {
308 /* Reset Master and TDM */
309 outb(DELAY | 0x0f, wc->ioaddr + WC_CNTL);
310 set_current_state(TASK_INTERRUPTIBLE);
311 schedule_timeout(1);
312 outb(DELAY | 0x01, wc->ioaddr + WC_CNTL);
313 outb(0x01, wc->ioaddr + WC_OPER);
314 if (debug) printk(KERN_DEBUG "Started DMA\n");
315 }
316
317 static void __t1xxp_stop_dma(struct t1xxp *wc)
318 {
319 outb(0x00, wc->ioaddr + WC_OPER);
320 }
321
322 static void __t1xxp_disable_interrupts(struct t1xxp *wc)
323 {
324 outb(0x00, wc->ioaddr + WC_MASK0);
325 outb(0x00, wc->ioaddr + WC_MASK1);
326 }
327
328 static void __t1xxp_set_clear(struct t1xxp *wc)
329 {
330 /* Setup registers */
331 int x,y;
332 unsigned char b;
333
334 /* No such thing under E1 */
335 if (wc->ise1) {
336 printk(KERN_NOTICE "Can't set clear mode on an E1!\n");
337 return;
338 }
339
340 for (x=0;x<3;x++) {
341 b = 0;
342 for (y=0;y<8;y++)
343 if (wc->chans[x * 8 + y]->sig & DAHDI_SIG_CLEAR)
344 b |= (1 << y);
345 __t1_set_reg(wc, 0x39 + x, b);
346 }
347 }
348
349 static void t1xxp_t1_framer_start(struct t1xxp *wc)
350 {
351 int i;
352 char *coding, *framing;
353 unsigned long endjiffies;
354 int alreadyrunning = wc->span.flags & DAHDI_FLAG_RUNNING;
355 unsigned long flags;
356
357 spin_lock_irqsave(&wc->lock, flags);
358
359 /* Build up config */
360 i = 0x20;
361 if (wc->span.lineconfig & DAHDI_CONFIG_ESF) {
362 coding = "ESF";
363 i = 0x88;
364 } else {
365 coding = "SF";
366 }
367 if (wc->span.lineconfig & DAHDI_CONFIG_B8ZS) {
368 framing = "B8ZS";
369 i |= 0x44;
370 } else {
371 framing = "AMI";
372 }
373 __t1_set_reg(wc, 0x38, i);
374 if (!(wc->span.lineconfig & DAHDI_CONFIG_ESF)) {
375 /* 1c in FDL bit */
376 __t1_set_reg(wc, 0x7e, 0x1c);
377 } else {
378 __t1_set_reg(wc, 0x7e, 0x00);
379 }
380
381 /* Set outgoing LBO */
382 __t1_set_reg(wc, 0x7c, wc->span.txlevel << 5);
383
384 printk(KERN_DEBUG "Using %s/%s coding/framing\n", coding, framing);
385 if (!alreadyrunning) {
386 /* Setup the clear channels */
387 __t1xxp_set_clear(wc);
388
389 /* Set LIRST bit to 1 */
390 __t1_set_reg(wc, 0x0a, 0x80);
391 spin_unlock_irqrestore(&wc->lock, flags);
392
393 /* Wait 100ms to give plenty of time for reset */
394 endjiffies = jiffies + 10;
395 while(endjiffies < jiffies);
396
397 spin_lock_irqsave(&wc->lock, flags);
398
399 /* Reset LIRST bit and reset elastic stores */
400 __t1_set_reg(wc, 0xa, 0x30);
401
402 wc->span.flags |= DAHDI_FLAG_RUNNING;
403 }
404 spin_unlock_irqrestore(&wc->lock, flags);
405 }
406
407 static void t1xxp_e1_framer_start(struct t1xxp *wc)
408 {
409 int i;
410 char *coding, *framing;
411 unsigned long endjiffies;
412 int alreadyrunning = wc->span.flags & DAHDI_FLAG_RUNNING;
413 unsigned long flags;
414 char *crcing = "";
415 unsigned char ccr1, tcr1, tcr2;
416
417 spin_lock_irqsave(&wc->lock, flags);
418
419 /* Build up config */
420 ccr1 = 0;
421 tcr1 = 8;
422 tcr2 = 0;
423 if (wc->span.lineconfig & DAHDI_CONFIG_CCS) {
424 coding = "CCS"; /* Receive CCS */
425 ccr1 |= 8;
426 } else {
427 tcr1 |= 0x20;
428 coding = "CAS";
429 }
430 if (wc->span.lineconfig & DAHDI_CONFIG_HDB3) {
431 ccr1 |= 0x44; /* TX/RX HDB3 */
432 framing = "HDB3";
433 } else {
434 framing = "AMI";
435 }
436 if (wc->span.lineconfig & DAHDI_CONFIG_CRC4) {
437 ccr1 |= 0x11;
438 tcr2 |= 0x02;
439 crcing = " with CRC4";
440 }
441 __t1_set_reg(wc, 0x12, tcr1);
442 __t1_set_reg(wc, 0x13, tcr2);
443 __t1_set_reg(wc, 0x14, ccr1);
444 __t1_set_reg(wc, 0x18, 0x20); /* 120 Ohm */
445
446
447 #if 0 /* XXX Does LBO Matter? XXX */
448 /* Set outgoing LBO */
449 __t1_set_reg(wc, 0x7c, wc->span.txlevel << 5);
450 #endif
451
452 printk(KERN_DEBUG "Using %s/%s coding/framing%s 120 Ohms\n", coding, framing,crcing);
453 if (!alreadyrunning) {
454
455 __t1_set_reg(wc,0x1b,0x8a); /* CCR3: LIRST & TSCLKM */
456 __t1_set_reg(wc,0x20,0x1b); /* TAFR */
457 __t1_set_reg(wc,0x21,0x5f); /* TNAFR */
458 __t1_set_reg(wc,0x40,0xb); /* TSR1 */
459 for(i = 0x41; i <= 0x4f; i++) __t1_set_reg(wc,i,0x55);
460 for(i = 0x22; i <= 0x25; i++) __t1_set_reg(wc,i,0xff);
461 spin_unlock_irqrestore(&wc->lock, flags);
462
463 /* Wait 100ms to give plenty of time for reset */
464 endjiffies = jiffies + 10;
465 while(endjiffies < jiffies);
466
467 spin_lock_irqsave(&wc->lock, flags);
468
469 __t1_set_reg(wc, 0x1b, 0x9a); /* Set ESR */
470 __t1_set_reg(wc, 0x1b, 0x82); /* TSCLKM only now */
471
472 /* Reset LIRST bit and reset elastic stores */
473
474 wc->span.flags |= DAHDI_FLAG_RUNNING;
475 }
476 spin_unlock_irqrestore(&wc->lock, flags);
477 }
478
479 static int t1xxp_framer_sanity_check(struct t1xxp *wc)
480 {
481 int res;
482 int chipid;
483 unsigned long flags;
484 int x;
485
486 /* Sanity check */
487 spin_lock_irqsave(&wc->lock, flags);
488 for (x=0x0;x<192;x++)
489 __t1_set_reg(wc, x, 0);
490 res = __t1_get_reg(wc, 0x0f);
491 res = __t1_get_reg(wc, 0x0f);
492 chipid = ((res & 0x80) >> 5) | ((res & 0x30) >> 4);
493 wc->ise1 = (res & 0x80) ? (1 << 4) : 0;
494 spin_unlock_irqrestore(&wc->lock, flags);
495
496 printk(KERN_DEBUG "Framer: %s, Revision: %d (%s)\n", chips[chipid], res & 0xf, wc->ise1 ? "E1" : "T1");
497 return 0;
498 }
499
500 static int t1xxp_framer_hard_reset(struct t1xxp *wc)
501 {
502 int x;
503 unsigned long flags;
504
505 spin_lock_irqsave(&wc->lock, flags);
506 /* Initialize all registers to 0 */
507 for (x=0x0;x<192;x++)
508 __t1_set_reg(wc, x, 0);
509
510 if (wc->ise1) {
511 /* Set LOTCMC (switch to RCLCK if TCLK fails) */
512 __t1_set_reg(wc, 0x1a, 0x04);
513
514 /* RSYNC is an input */
515 __t1_set_reg(wc, 0x10, 0x20);
516
517 /* Rx elastic store enabled, 2.048 Mhz (in theory) */
518 __t1_set_reg(wc, 0x11, 0x06);
519
520 /* TSYNC is an input, Tsis mode */
521 __t1_set_reg(wc, 0x12, 0x08);
522
523 /* Tx elastic store enabled, 2.048 Mhz (in theory) */
524 __t1_set_reg(wc, 0x1b, 0x82);
525
526
527
528 } else {
529 /* Full-on sync required for T1 */
530 __t1_set_reg(wc, 0x2b, 0x08);
531 /* RSYNC is an input */
532 __t1_set_reg(wc, 0x2c, 0x08);
533
534 /* Enable tx RBS bits */
535 __t1_set_reg(wc, 0x35, 0x10);
536
537 /* TSYNC is output */
538 __t1_set_reg(wc, 0x36, 0x04);
539
540 /* Tx and Rx elastic store enabled, 2.048 Mhz (in theory) */
541 __t1_set_reg(wc, 0x37, 0x9c);
542
543 /* Setup Loopup / Loopdown codes */
544 __t1_set_reg(wc, 0x12, 0x22);
545 __t1_set_reg(wc, 0x14, 0x80);
546 __t1_set_reg(wc, 0x15, 0x80);
547 }
548
549 spin_unlock_irqrestore(&wc->lock, flags);
550 return 0;
551 }
552
553 static int t1xxp_rbsbits(struct dahdi_chan *chan, int bits)
554 {
555 struct t1xxp *wc = chan->pvt;
556 unsigned long flags;
557 int b,o;
558 unsigned char mask;
559
560 /* Byte offset */
561 spin_lock_irqsave(&wc->lock, flags);
562 if (wc->ise1) {
563 if (chan->chanpos < 16) {
564 mask = ((bits << 4) | wc->chans[chan->chanpos - 1 + 16]->txsig);
565 __t1_set_reg(wc, 0x40 + chan->chanpos, mask);
566 }
567 else if (chan->chanpos > 16) {
568 mask = (bits | (wc->chans[chan->chanpos - 1 - 16]->txsig << 4));
569 __t1_set_reg(wc, 0x40 + chan->chanpos - 16, mask);
570 }
571 wc->chans[chan->chanpos - 1]->txsig = bits;
572 } else {
573 b = (chan->chanpos - 1) / 8;
574 o = (chan->chanpos - 1) % 8;
575
576 mask = (1 << o);
577
578 if (bits & DAHDI_ABIT) {
579 /* Set A-bit */
580 wc->txsiga[b] |= mask;
581 } else {
582 /* Clear A-bit */
583 wc->txsiga[b] &= ~mask;
584 }
585 if (bits & DAHDI_BBIT) {
586 /* Set B-bit */
587 wc->txsigb[b] |= mask;
588 } else {
589 wc->txsigb[b] &= ~mask;
590 }
591 /* Output new values */
592 __t1_set_reg(wc, 0x70 + b, wc->txsiga[b]);
593 __t1_set_reg(wc, 0x73 + b, wc->txsigb[b]);
594 __t1_set_reg(wc, 0x76 + b, wc->txsiga[b]);
595 __t1_set_reg(wc, 0x79 + b, wc->txsigb[b]);
596 }
597 spin_unlock_irqrestore(&wc->lock, flags);
598 return 0;
599 }
600
601 static int t1xxp_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
602 {
603 switch(cmd) {
604 default:
605 return -ENOTTY;
606 }
607 }
608
609 static inline struct t1xxp *t1xxp_from_span(struct dahdi_span *span)
610 {
611 return container_of(span, struct t1xxp, span);
612 }
613
614 static int t1xxp_startup(struct file *file, struct dahdi_span *span)
615 {
616 struct t1xxp *wc = t1xxp_from_span(span);
617
618 int i,alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
619
620 /* initialize the start value for the entire chunk of last ec buffer */
621 for(i = 0; i < span->channels; i++)
622 {
623 memset(wc->ec_chunk1[i],
624 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
625 memset(wc->ec_chunk2[i],
626 DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
627 }
628
629 /* Reset framer with proper parameters and start */
630 if (wc->ise1)
631 t1xxp_e1_framer_start(wc);
632 else
633 t1xxp_t1_framer_start(wc);
634 printk(KERN_INFO "Calling startup (flags is %lu)\n", span->flags);
635
636 if (!alreadyrunning) {
637 /* Only if we're not already going */
638 t1xxp_enable_interrupts(wc);
639 t1xxp_start_dma(wc);
640 span->flags |= DAHDI_FLAG_RUNNING;
641 }
642 return 0;
643 }
644
645 static int t1xxp_shutdown(struct dahdi_span *span)
646 {
647 struct t1xxp *wc = t1xxp_from_span(span);
648 unsigned long flags;
649
650 spin_lock_irqsave(&wc->lock, flags);
651 __t1xxp_stop_dma(wc);
652 __t1xxp_disable_interrupts(wc);
653 span->flags &= ~DAHDI_FLAG_RUNNING;
654 spin_unlock_irqrestore(&wc->lock, flags);
655
656 t1xxp_framer_hard_reset(wc);
657 return 0;
658 }
659
660 static int t1xxp_maint(struct dahdi_span *span, int cmd)
661 {
662 struct t1xxp *wc = t1xxp_from_span(span);
663 int res = 0;
664 unsigned long flags;
665 spin_lock_irqsave(&wc->lock, flags);
666 if (wc->ise1) {
667 switch(cmd) {
668 case DAHDI_MAINT_NONE:
669 __t1_set_reg(wc,0xa8,0); /* no loops */
670 break;
671 case DAHDI_MAINT_LOCALLOOP:
672 __t1_set_reg(wc,0xa8,0x40); /* local loop */
673 break;
674 case DAHDI_MAINT_REMOTELOOP:
675 __t1_set_reg(wc,0xa8,0x80); /* remote loop */
676 break;
677 case DAHDI_MAINT_LOOPUP:
678 case DAHDI_MAINT_LOOPDOWN:
679 res = -ENOSYS;
680 break;
681 default:
682 printk(KERN_NOTICE "wct1xxp/E1: Unknown maint command: %d\n", cmd);
683 res = -EINVAL;
684 break;
685 }
686 } else {
687 switch(cmd) {
688 case DAHDI_MAINT_NONE:
689 __t1_set_reg(wc,0x19,0); /* no local loop */
690 __t1_set_reg(wc,0x0a,0); /* no remote loop */
691 __t1_set_reg(wc, 0x30, 0); /* stop sending loopup code*/
692 break;
693 case DAHDI_MAINT_LOCALLOOP:
694 __t1_set_reg(wc,0x19,0x40); /* local loop */
695 __t1_set_reg(wc,0x0a,0); /* no remote loop */
696 break;
697 case DAHDI_MAINT_REMOTELOOP:
698 __t1_set_reg(wc,0x1e,0); /* no local loop */
699 __t1_set_reg(wc,0x0a,0x40); /* remote loop */
700 break;
701 case DAHDI_MAINT_LOOPUP:
702 __t1_set_reg(wc,0x30,2); /* send loopup code */
703 __t1_set_reg(wc,0x12,0x22); /* send loopup code */
704 __t1_set_reg(wc,0x13,0x80); /* send loopup code */
705 break;
706 case DAHDI_MAINT_LOOPDOWN:
707 __t1_set_reg(wc,0x30,2); /* send loopdown code */
708 __t1_set_reg(wc,0x12,0x62); /* send loopdown code */
709 __t1_set_reg(wc,0x13,0x90); /* send loopdown code */
710 break;
711 default:
712 printk(KERN_NOTICE "wct1xxp/T1: Unknown maint command: %d\n", cmd);
713 res = -EINVAL;
714 }
715 }
716 spin_unlock_irqrestore(&wc->lock, flags);
717 return res;
718 }
719
720 static int
721 t1xxp_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
722 {
723 struct t1xxp *wc = chan->pvt;
724 unsigned long flags;
725 int alreadyrunning = chan->span->flags & DAHDI_FLAG_RUNNING;
726
727 spin_lock_irqsave(&wc->lock, flags);
728
729 if (alreadyrunning && !wc->ise1)
730 __t1xxp_set_clear(wc);
731
732 spin_unlock_irqrestore(&wc->lock, flags);
733 return 0;
734 }
735
736 static int
737 t1xxp_spanconfig(struct file *file, struct dahdi_span *span,
738 struct dahdi_lineconfig *lc)
739 {
740 struct t1xxp *wc = t1xxp_from_span(span);
741
742 /* Do we want to SYNC on receive or not */
743 wc->sync = (lc->sync) ? 1 : 0;
744 /* If already running, apply changes immediately */
745 if (span->flags & DAHDI_FLAG_RUNNING)
746 return t1xxp_startup(file, span);
747
748 return 0;
749 }
750
751 static const struct dahdi_span_ops t1xxp_span_ops = {
752 .owner = THIS_MODULE,
753 .spanconfig = t1xxp_spanconfig,
754 .chanconfig = t1xxp_chanconfig,
755 .startup = t1xxp_startup,
756 .shutdown = t1xxp_shutdown,
757 .rbsbits = t1xxp_rbsbits,
758 .maint = t1xxp_maint,
759 .open = t1xxp_open,
760 .close = t1xxp_close,
761 .ioctl = t1xxp_ioctl,
762 };
763
764 static int t1xxp_software_init(struct t1xxp *wc)
765 {
766 int x;
767 /* Find position */
768 for (x=0;x<WC_MAX_CARDS;x++) {
769 if (!cards[x]) {
770 cards[x] = wc;
771 break;
772 }
773 }
774 if (x >= WC_MAX_CARDS)
775 return -1;
776
777 wc->ddev = dahdi_create_device();
778
779 wc->num = x;
780 sprintf(wc->span.name, "WCT1/%d", wc->num);
781 snprintf(wc->span.desc, sizeof(wc->span.desc) - 1, "%s Card %d", wc->variety, wc->num);
782 wc->ddev->manufacturer = "Digium";
783 wc->ddev->devicetype = wc->variety;
784 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
785 wc->dev->bus->number,
786 PCI_SLOT(wc->dev->devfn) + 1);
787 if (!wc->ddev->location)
788 return -ENOMEM;
789
790 wc->span.chans = wc->chans;
791 wc->span.flags = DAHDI_FLAG_RBS;
792 if (wc->ise1) {
793 wc->span.channels = 31;
794 wc->span.deflaw = DAHDI_LAW_ALAW;
795 wc->span.linecompat = DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
796 wc->span.spantype = SPANTYPE_DIGITAL_E1;
797 } else {
798 wc->span.channels = 24;
799 wc->span.deflaw = DAHDI_LAW_MULAW;
800 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
801 wc->span.spantype = SPANTYPE_DIGITAL_T1;
802 }
803 for (x=0;x<wc->span.channels;x++) {
804 sprintf(wc->chans[x]->name, "WCT1/%d/%d", wc->num, x + 1);
805 wc->chans[x]->sigcap = DAHDI_SIG_EM | DAHDI_SIG_CLEAR | DAHDI_SIG_EM_E1 |
806 DAHDI_SIG_FXSLS | DAHDI_SIG_FXSGS |
807 DAHDI_SIG_FXSKS | DAHDI_SIG_FXOLS | DAHDI_SIG_DACS_RBS |
808 DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_SF;
809 wc->chans[x]->pvt = wc;
810 wc->chans[x]->chanpos = x + 1;
811 }
812 wc->span.ops = &t1xxp_span_ops;
813 list_add_tail(&wc->span.device_node, &wc->ddev->spans);
814 if (dahdi_register_device(wc->ddev, &wc->dev->dev)) {
815 printk(KERN_NOTICE "Unable to register span with DAHDI\n");
816 return -1;
817 }
818 return 0;
819 }
820
821 static inline void __handle_leds(struct t1xxp *wc)
822 {
823 int oldreg;
824 wc->blinktimer++;
825
826 if (wc->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE)) {
827 /* Red/Blue alarm */
828 #ifdef FANCY_ALARM
829 if (wc->blinktimer == (altab[wc->alarmpos] >> 1)) {
830 wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
831 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
832 }
833 if (wc->blinktimer == 0xf) {
834 wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
835 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
836 wc->blinktimer = -1;
837 wc->alarmpos++;
838 if (wc->alarmpos >= (sizeof(altab) / sizeof(altab[0])))
839 wc->alarmpos = 0;
840 }
841 #else
842 if (wc->blinktimer == 160) {
843 wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
844 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
845 } else if (wc->blinktimer == 480) {
846 wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
847 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
848 wc->blinktimer = 0;
849 }
850 #endif
851 } else if (wc->span.alarms & DAHDI_ALARM_YELLOW) {
852 /* Yellow Alarm */
853 if (!(wc->blinktimer % 2))
854 wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
855 else
856 wc->ledtestreg = (wc->ledtestreg | BIT_LED0) & ~BIT_LED1;
857 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
858 } else {
859 /* No Alarm */
860 oldreg = wc->ledtestreg;
861 if (wc->span.maintstat != DAHDI_MAINT_NONE)
862 wc->ledtestreg |= BIT_TEST;
863 else
864 wc->ledtestreg &= ~BIT_TEST;
865 if (wc->span.flags & DAHDI_FLAG_RUNNING)
866 wc->ledtestreg = (wc->ledtestreg | BIT_LED0) & ~BIT_LED1;
867 else
868 wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
869 if (oldreg != wc->ledtestreg)
870 __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
871 }
872 }
873
874 static void t1xxp_transmitprep(struct t1xxp *wc, int ints)
875 {
876 volatile unsigned char *txbuf;
877 int x,y;
878 int pos;
879 if (ints & 0x04 /* 0x01 */) {
880 /* We just finished sending the first buffer, start filling it
881 now */
882 txbuf = wc->writechunk;
883 } else {
884 /* Just finished sending second buffer, fill it now */
885 txbuf = wc->writechunk + 32 * DAHDI_CHUNKSIZE;
886 }
887 dahdi_transmit(&wc->span);
888 for (x=0;x<wc->offset;x++)
889 txbuf[x] = wc->tempo[x];
890 for (y=0;y<DAHDI_CHUNKSIZE;y++) {
891 for (x=0;x<wc->span.channels;x++) {
892 pos = y * 32 + wc->chanmap[x] + wc->offset;
893 /* Put channel number as outgoing data */
894 if (pos < 32 * DAHDI_CHUNKSIZE)
895 txbuf[pos] = wc->chans[x]->writechunk[y];
896 else
897 wc->tempo[pos - 32 * DAHDI_CHUNKSIZE] = wc->chans[x]->writechunk[y];
898 }
899 }
900 }
901
902 static void t1xxp_receiveprep(struct t1xxp *wc, int ints)
903 {
904 volatile unsigned char *rxbuf;
905 volatile unsigned int *canary;
906 int x;
907 int y;
908 unsigned int oldcan;
909 if (ints & 0x04) {
910 /* Just received first buffer */
911 rxbuf = wc->readchunk;
912 canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 64 - 4);
913 } else {
914 rxbuf = wc->readchunk + DAHDI_CHUNKSIZE * 32;
915 canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 32 - 4);
916 }
917 oldcan = *canary;
918 if (((oldcan & 0xffff0000) >> 16) != CANARY) {
919 /* Check top part */
920 if (debug) printk(KERN_DEBUG "Expecting top %04x, got %04x\n", CANARY, (oldcan & 0xffff0000) >> 16);
921 wc->ddev->irqmisses++;
922 } else if ((oldcan & 0xffff) != ((wc->canary - 1) & 0xffff)) {
923 if (debug) printk(KERN_DEBUG "Expecting bottom %d, got %d\n", wc->canary - 1, oldcan & 0xffff);
924 wc->ddev->irqmisses++;
925 }
926 for (y=0;y<DAHDI_CHUNKSIZE;y++) {
927 for (x=0;x<wc->span.channels;x++) {
928 /* XXX Optimize, remove * and + XXX */
929 /* Must map received channels into appropriate data */
930 wc->chans[x]->readchunk[y] =
931 rxbuf[32 * y + ((wc->chanmap[x] + WC_OFFSET + wc->offset) & 0x1f)];
932 }
933 if (!wc->ise1) {
934 for (x=3;x<32;x+=4) {
935 if (rxbuf[32 * y + ((x + WC_OFFSET) & 0x1f)] == 0x7f) {
936 if (wc->offset != (x-3)) {
937 /* Resync */
938 control_set_reg(wc, WC_CLOCK, 0x02 | wc->sync | wc->ise1);
939 wc->clocktimeout = 100;
940 #if 1
941 if (debug) printk(KERN_DEBUG "T1: Lost our place, resyncing\n");
942 #endif
943 }
944 }
945 }
946 } else {
947 if (!wc->clocktimeout && !wc->span.alarms) {
948 if ((rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)] & 0x7f) != 0x1b) {
949 if (wc->miss) {
950 if (debug) printk(KERN_DEBUG "Double miss (%d, %d)...\n", wc->misslast, rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)]);
951 control_set_reg(wc, WC_CLOCK, 0x02 | wc->sync | wc->ise1);
952 wc->clocktimeout = 100;
953 } else {
954 wc->miss = 1;
955 wc->misslast = rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)];
956 }
957 } else {
958 wc->miss = 0;
959 }
960 } else {
961 wc->miss = 0;
962 }
963 }
964 }
965 /* Store the next canary */
966 canary = (unsigned int *)(rxbuf + DAHDI_CHUNKSIZE * 32 - 4);
967 *canary = (wc->canary++) | (CANARY << 16);
968 for (x=0;x<wc->span.channels;x++) {
969 dahdi_ec_chunk(wc->chans[x], wc->chans[x]->readchunk, wc->ec_chunk2[x]);
970 memcpy(wc->ec_chunk2[x],wc->ec_chunk1[x],DAHDI_CHUNKSIZE);
971 memcpy(wc->ec_chunk1[x],wc->chans[x]->writechunk,DAHDI_CHUNKSIZE);
972 }
973 dahdi_receive(&wc->span);
974 }
975
976 static void t1xxp_check_sigbits(struct t1xxp *wc, int x)
977 {
978 int a,b,i,y,rxs;
979 unsigned long flags;
980
981 spin_lock_irqsave(&wc->lock, flags);
982 if (wc->ise1) {
983 /* Read 5 registers at a time, loading 10 channels at a time */
984 for (i = (x * 5); i < (x * 5) + 5; i++) {
985 a = __t1_get_reg(wc, 0x31 + i);
986 /* Get high channel in low bits */
987 rxs = (a & 0xf);
988 if (!(wc->chans[i+16]->sig & DAHDI_SIG_CLEAR)) {
989 if (wc->chans[i+16]->rxsig != rxs) {
990 spin_unlock_irqrestore(&wc->lock, flags);
991 dahdi_rbsbits(wc->chans[i+16], rxs);
992 spin_lock_irqsave(&wc->lock, flags);
993 }
994 }
995 rxs = (a >> 4) & 0xf;
996 if (!(wc->chans[i]->sig & DAHDI_SIG_CLEAR)) {
997 if (wc->chans[i]->rxsig != rxs) {
998 spin_unlock_irqrestore(&wc->lock, flags);
999 dahdi_rbsbits(wc->chans[i], rxs);
1000 spin_lock_irqsave(&wc->lock, flags);
1001 }
1002 }
1003 }
1004 } else {
1005 a = __t1_get_reg(wc, 0x60 + x);
1006 b = __t1_get_reg(wc, 0x63 + x);
1007 for (y=0;y<8;y++) {
1008 i = x * 8 + y;
1009 rxs = 0;
1010 if (a & (1 << y))
1011 rxs |= DAHDI_ABIT;
1012 if (b & (1 << y))
1013 rxs |= DAHDI_BBIT;
1014 if (!(wc->chans[i]->sig & DAHDI_SIG_CLEAR)) {
1015 if (wc->chans[i]->rxsig != rxs) {
1016 spin_unlock_irqrestore(&wc->lock, flags);
1017 dahdi_rbsbits(wc->chans[i], rxs);
1018 spin_lock_irqsave(&wc->lock, flags);
1019 }
1020 }
1021 }
1022 }
1023 spin_unlock_irqrestore(&wc->lock, flags);
1024 }
1025
1026 static void t1xxp_check_alarms(struct t1xxp *wc)
1027 {
1028 unsigned char c,d;
1029 int alarms;
1030 int x,j;
1031 unsigned long flags;
1032
1033 spin_lock_irqsave(&wc->lock, flags);
1034
1035 if (wc->ise1) {
1036 __t1_set_reg(wc, 0x06, 0xff);
1037 c = __t1_get_reg(wc, 0x6);
1038 } else {
1039 /* Get RIR2 */
1040 c = __t1_get_reg(wc, 0x31);
1041 wc->span.rxlevel = c >> 6;
1042
1043 /* Get status register s*/
1044 __t1_set_reg(wc, 0x20, 0xff);
1045 c = __t1_get_reg(wc, 0x20);
1046 }
1047
1048 /* Assume no alarms */
1049 alarms = 0;
1050
1051 /* And consider only carrier alarms */
1052 wc->span.alarms &= (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_NOTOPEN);
1053
1054 if (wc->ise1) {
1055 /* XXX Implement me XXX */
1056 } else {
1057 /* Detect loopup code if we're not sending one */
1058 if ((!wc->span.mainttimer) && (c & 0x80)) {
1059 /* Loop-up code detected */
1060 if ((wc->loopupcnt++ > 80) && (wc->span.maintstat != DAHDI_MAINT_REMOTELOOP)) {
1061 __t1_set_reg(wc, 0x1e, 0); /* No local loop */
1062 __t1_set_reg(wc, 0x0a, 0x40); /* Remote Loop */
1063 wc->span.maintstat = DAHDI_MAINT_REMOTELOOP;
1064 }
1065 } else {
1066 wc->loopupcnt = 0;
1067 }
1068 /* Same for loopdown code */
1069 if ((!wc->span.mainttimer) && (c & 0x40)) {
1070 /* Loop-down code detected */
1071 if ((wc->loopdowncnt++ > 80) && (wc->span.maintstat == DAHDI_MAINT_REMOTELOOP)) {
1072 __t1_set_reg(wc, 0x1e, 0); /* No local loop */
1073 __t1_set_reg(wc, 0x0a, 0x0); /* No remote Loop */
1074 wc->span.maintstat = DAHDI_MAINT_NONE;
1075 }
1076 } else
1077 wc->loopdowncnt = 0;
1078 }
1079
1080 if (wc->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
1081 for (x=0,j=0;x < wc->span.channels;x++)
1082 if ((wc->chans[x]->flags & DAHDI_FLAG_OPEN) ||
1083 dahdi_have_netdev(wc->chans[x]))
1084 j++;
1085 if (!j)
1086 alarms |= DAHDI_ALARM_NOTOPEN;
1087 }
1088
1089 if (wc->ise1) {
1090 if (c & 0x9)
1091 alarms |= DAHDI_ALARM_RED;
1092 if (c & 0x2)
1093 alarms |= DAHDI_ALARM_BLUE;
1094 } else {
1095 /* Check actual alarm status */
1096 if (c & 0x3)
1097 alarms |= DAHDI_ALARM_RED;
1098 if (c & 0x8)
1099 alarms |= DAHDI_ALARM_BLUE;
1100 }
1101 /* Keep track of recovering */
1102 if ((!alarms) && wc->span.alarms)
1103 wc->alarmtimer = DAHDI_ALARMSETTLE_TIME;
1104
1105 /* If receiving alarms, go into Yellow alarm state */
1106 if (alarms && (!wc->span.alarms)) {
1107 #if 0
1108 printk(KERN_DEBUG "Going into yellow alarm\n");
1109 #endif
1110 if (wc->ise1)
1111 __t1_set_reg(wc, 0x21, 0x7f);
1112 else
1113 __t1_set_reg(wc, 0x35, 0x11);
1114 }
1115
1116 if (wc->span.alarms != alarms) {
1117 d = __control_get_reg(wc, WC_CLOCK);
1118 start_alarm(wc);
1119 if (!(alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_LOOPBACK)) &&
1120 wc->sync) {
1121 /* Use the receive signalling */
1122 wc->span.syncsrc = wc->span.spanno;
1123 d |= 1;
1124 } else {
1125 wc->span.syncsrc = 0;
1126 d &= ~1;
1127 }
1128 __control_set_reg(wc, WC_CLOCK, d);
1129 }
1130 if (wc->alarmtimer)
1131 alarms |= DAHDI_ALARM_RECOVER;
1132 if (c & 0x4)
1133 alarms |= DAHDI_ALARM_YELLOW;
1134
1135 wc->span.alarms = alarms;
1136
1137 spin_unlock_irqrestore(&wc->lock, flags);
1138
1139 dahdi_alarm_notify(&wc->span);
1140 }
1141
1142 static void t1xxp_do_counters(struct t1xxp *wc)
1143 {
1144 unsigned long flags;
1145
1146 spin_lock_irqsave(&wc->lock, flags);
1147 if (wc->alarmtimer) {
1148 if (!--wc->alarmtimer) {
1149 wc->span.alarms &= ~(DAHDI_ALARM_RECOVER);
1150 /* Clear yellow alarm */
1151 #if 0
1152 printk(KERN_DEBUG "Coming out of alarm\n");
1153 #endif
1154 if (wc->ise1)
1155 __t1_set_reg(wc, 0x21, 0x5f);
1156 else
1157 __t1_set_reg(wc, 0x35, 0x10);
1158 spin_unlock_irqrestore(&wc->lock, flags);
1159 dahdi_alarm_notify(&wc->span);
1160 spin_lock_irqsave(&wc->lock, flags);
1161 }
1162 }
1163 spin_unlock_irqrestore(&wc->lock, flags);
1164 }
1165
1166 DAHDI_IRQ_HANDLER(t1xxp_interrupt)
1167 {
1168 struct t1xxp *wc = dev_id;
1169 unsigned char ints;
1170 unsigned long flags;
1171 int x;
1172
1173 ints = inb(wc->ioaddr + WC_INTSTAT);
1174 if (!ints)
1175 return IRQ_NONE;
1176
1177 local_irq_save(flags);
1178
1179 outb(ints, wc->ioaddr + WC_INTSTAT);
1180
1181 if (!wc->intcount) {
1182 if (debug) printk(KERN_DEBUG "Got interrupt: 0x%04x\n", ints);
1183 }
1184 wc->intcount++;
1185
1186 if (wc->clocktimeout && !--wc->clocktimeout)
1187 control_set_reg(wc, WC_CLOCK, 0x00 | wc->sync | wc->ise1);
1188
1189 if (ints & 0x0f) {
1190 t1xxp_receiveprep(wc, ints);
1191 t1xxp_transmitprep(wc, ints);
1192 }
1193 spin_lock(&wc->lock);
1194
1195 #if 1
1196 __handle_leds(wc);
1197 #endif
1198
1199 spin_unlock(&wc->lock);
1200
1201 /* Count down timers */
1202 t1xxp_do_counters(wc);
1203
1204 /* Do some things that we don't have to do very often */
1205 x = wc->intcount & 15 /* 63 */;
1206 switch(x) {
1207 case 0:
1208 case 1:
1209 case 2:
1210 t1xxp_check_sigbits(wc, x);
1211 break;
1212 case 4:
1213 /* Check alarms 1/4 as frequently */
1214 if (!(wc->intcount & 0x30))
1215 t1xxp_check_alarms(wc);
1216 break;
1217 }
1218
1219 if (ints & 0x10)
1220 printk(KERN_INFO "PCI Master abort\n");
1221
1222 if (ints & 0x20)
1223 printk(KERN_INFO "PCI Target abort\n");
1224
1225 local_irq_restore(flags);
1226
1227 return IRQ_RETVAL(1);
1228 }
1229
1230 static int t1xxp_hardware_init(struct t1xxp *wc)
1231 {
1232 /* Hardware PCI stuff */
1233 /* Reset chip and registers */
1234 outb(DELAY | 0x0e, wc->ioaddr + WC_CNTL);
1235 /* Set all outputs to 0 */
1236 outb(0x00, wc->ioaddr + WC_AUXD);
1237 /* Set all to outputs except AUX1 (TDO). */
1238 outb(0xfd, wc->ioaddr + WC_AUXC);
1239 /* Configure the serial port: double clock, 20ns width, no inversion,
1240 MSB first */
1241 outb(0xc8, wc->ioaddr + WC_SERC);
1242
1243 /* Internally delay FSC by one */
1244 outb(0x01, wc->ioaddr + WC_FSCDELAY);
1245
1246 /* Back to normal, with automatic DMA wrap around */
1247 outb(DELAY | 0x01, wc->ioaddr + WC_CNTL);
1248
1249 /* Make sure serial port and DMA are out of reset */
1250 outb(inb(wc->ioaddr + WC_CNTL) & 0xf9, WC_CNTL);
1251
1252 /* Setup DMA Addresses */
1253 /* Start at writedma */
1254 outl(wc->writedma, wc->ioaddr + WC_DMAWS); /* Write start */
1255 /* First frame */
1256 outl(wc->writedma + DAHDI_CHUNKSIZE * 32 - 4, wc->ioaddr + WC_DMAWI); /* Middle (interrupt) */
1257 /* Second frame */
1258 outl(wc->writedma + DAHDI_CHUNKSIZE * 32 * 2 - 4, wc->ioaddr + WC_DMAWE); /* End */
1259
1260 outl(wc->readdma, wc->ioaddr + WC_DMARS); /* Read start */
1261 /* First frame */
1262 outl(wc->readdma + DAHDI_CHUNKSIZE * 32 - 4, wc->ioaddr + WC_DMARI); /* Middle (interrupt) */
1263 /* Second frame */
1264 outl(wc->readdma + DAHDI_CHUNKSIZE * 32 * 2 - 4, wc->ioaddr + WC_DMARE); /* End */
1265
1266 if (debug) printk(KERN_DEBUG "Setting up DMA (write/read = %08lx/%08lx)\n", (long)wc->writedma, (long)wc->readdma);
1267
1268 /* Check out the controller */
1269 if (debug) printk(KERN_DEBUG "Controller version: %02x\n", control_get_reg(wc, WC_VERSION));
1270
1271
1272 control_set_reg(wc, WC_LEDTEST, 0x00);
1273
1274 /* Sanity check also determines e1 or t1 */
1275 if (t1xxp_framer_sanity_check(wc))
1276 return -1;
1277 if (wc->ise1)
1278 wc->chanmap = chanmap_e1;
1279 else
1280 wc->chanmap = chanmap_t1;
1281 /* Setup clock appropriately */
1282 control_set_reg(wc, WC_CLOCK, 0x02 | wc->sync | wc->ise1);
1283 wc->clocktimeout = 100;
1284
1285 /* Reset the T1 and report */
1286 t1xxp_framer_hard_reset(wc);
1287 start_alarm(wc);
1288 return 0;
1289
1290 }
1291
1292 static int __devinit t1xxp_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1293 {
1294 struct t1xxp *wc;
1295 unsigned int *canary;
1296 unsigned int x;
1297
1298 if (pci_enable_device(pdev)) {
1299 printk(KERN_ERR "%s: pci_enable_device failed\n", __FUNCTION__);
1300 return -EIO;
1301 }
1302
1303 if (!(wc = kmalloc(sizeof(*wc), GFP_KERNEL))) {
1304 printk(KERN_ERR "%s: Failed allocation a wc\n", __FUNCTION__);
1305 return -ENOMEM;
1306 }
1307
1308 memset(wc, 0x0, sizeof(*wc));
1309 spin_lock_init(&wc->lock);
1310 wc->ioaddr = pci_resource_start(pdev, 0);
1311 wc->dev = pdev;
1312 wc->offset = 28; /* And you thought 42 was the answer */
1313
1314 wc->writechunk =
1315 /* 32 channels, Double-buffer, Read/Write */
1316 (unsigned char *)pci_alloc_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 32 * 2 * 2, &wc->writedma);
1317 if (!wc->writechunk) {
1318 printk(KERN_NOTICE "wct1xxp: Unable to allocate DMA-able memory\n");
1319 return -ENOMEM;
1320 }
1321
1322 /* Read is after the whole write piece (in bytes) */
1323 wc->readchunk = wc->writechunk + DAHDI_CHUNKSIZE * 32 * 2;
1324
1325 /* Same thing... */
1326 wc->readdma = wc->writedma + DAHDI_CHUNKSIZE * 32 * 2;
1327
1328 /* Initialize Write/Buffers to all blank data */
1329 memset((void *)wc->writechunk,0x00,DAHDI_MAX_CHUNKSIZE * 2 * 2 * 32);
1330 /* Initialize canary */
1331 canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 64 - 4);
1332 *canary = (CANARY << 16) | (0xffff);
1333
1334 /* Enable bus mastering */
1335 pci_set_master(pdev);
1336
1337 /* Keep track of which device we are */
1338 pci_set_drvdata(pdev, wc);
1339
1340 if (request_irq(pdev->irq, t1xxp_interrupt, IRQF_SHARED, "t1xxp", wc)) {
1341 printk(KERN_NOTICE "t1xxp: Unable to request IRQ %d\n", pdev->irq);
1342 kfree(wc);
1343 return -EIO;
1344 }
1345 /* Initialize hardware */
1346 t1xxp_hardware_init(wc);
1347
1348 /* We now know which version of card we have */
1349 if (wc->ise1) {
1350 wc->variety = "Digium Wildcard E100P E1/PRA";
1351 } else {
1352 wc->variety = "Digium Wildcard T100P T1/PRI";
1353 }
1354
1355 for (x = 0; x < (wc->ise1 ? 31 : 24); x++) {
1356 if (!(wc->chans[x] = kmalloc(sizeof(*wc->chans[x]), GFP_KERNEL))) {
1357 while (x) {
1358 kfree(wc->chans[--x]);
1359 }
1360
1361 kfree(wc);
1362 return -ENOMEM;
1363 }
1364 memset(wc->chans[x], 0, sizeof(*wc->chans[x]));
1365 }
1366
1367 /* Misc. software stuff */
1368 t1xxp_software_init(wc);
1369
1370 printk(KERN_INFO "Found a Wildcard: %s\n", wc->variety);
1371
1372 return 0;
1373 }
1374
1375
1376 static void t1xxp_stop_stuff(struct t1xxp *wc)
1377 {
1378 /* Kill clock */
1379 control_set_reg(wc, WC_CLOCK, 0);
1380
1381 /* Turn off LED's */
1382 control_set_reg(wc, WC_LEDTEST, 0);
1383
1384 /* Reset the T1 */
1385 t1xxp_framer_hard_reset(wc);
1386
1387 }
1388
1389 static void __devexit t1xxp_remove_one(struct pci_dev *pdev)
1390 {
1391 struct t1xxp *wc = pci_get_drvdata(pdev);
1392 if (wc) {
1393
1394 /* Stop any DMA */
1395 __t1xxp_stop_dma(wc);
1396
1397 /* In case hardware is still there */
1398 __t1xxp_disable_interrupts(wc);
1399
1400 t1xxp_stop_stuff(wc);
1401
1402 /* Immediately free resources */
1403 pci_free_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 2 * 2 * 32 * 4, (void *)wc->writechunk, wc->writedma);
1404 free_irq(pdev->irq, wc);
1405
1406 /* Reset PCI chip and registers */
1407 outb(DELAY | 0x0e, wc->ioaddr + WC_CNTL);
1408
1409 /* Release span, possibly delayed */
1410 if (!wc->usecount)
1411 t1xxp_release(wc);
1412 else
1413 wc->dead = 1;
1414 }
1415 }
1416
1417 static DEFINE_PCI_DEVICE_TABLE(t1xxp_pci_tbl) = {
1418 { 0xe159, 0x0001, 0x6159, PCI_ANY_ID, 0, 0, (unsigned long) "Digium Wildcard T100P T1/PRI or E100P E1/PRA Board" },
1419 { 0 }
1420 };
1421
1422 MODULE_DEVICE_TABLE(pci,t1xxp_pci_tbl);
1423
1424 static struct pci_driver t1xxp_driver = {
1425 .name = "t1xxp",
1426 .probe = t1xxp_init_one,
1427 .remove = __devexit_p(t1xxp_remove_one),
1428 .suspend = NULL,
1429 .resume = NULL,
1430 . id_table = t1xxp_pci_tbl,
1431 };
1432
1433 static int __init t1xxp_init(void)
1434 {
1435 int res;
1436 res = dahdi_pci_module(&t1xxp_driver);
1437 if (res)
1438 return -ENODEV;
1439 return 0;
1440 }
1441
1442 static void __exit t1xxp_cleanup(void)
1443 {
1444 pci_unregister_driver(&t1xxp_driver);
1445 }
1446
1447 module_param(debug, int, 0600);
1448
1449 MODULE_DESCRIPTION("Wildcard T100P/E100P Driver");
1450 MODULE_AUTHOR("Mark Spencer <[email protected]>");
1451 MODULE_LICENSE("GPL v2");
1452
1453 module_init(t1xxp_init);
1454 module_exit(t1xxp_cleanup);