Debian Codegraph @

drivers/dahdi/zaphfc/base.c

raw symbols SCIP
1 /*
2 * zaphfc.c - Dahdi driver for HFC-S PCI A based ISDN BRI cards
3 *
4 * Dahdi rewrite in hardhdlc mode
5 * Jose A. Deniz <[email protected]>
6 *
7 * Copyright (C) 2009, Jose A. Deniz
8 * Copyright (C) 2006, headiisue GmbH; Jens Wilke
9 * Copyright (C) 2004 Daniele Orlandi
10 * Copyright (C) 2002, 2003, 2004, Junghanns.NET GmbH
11 *
12 * Jens Wilke <[email protected]>
13 *
14 * Original author of this code is
15 * Daniele "Vihai" Orlandi <[email protected]>
16 *
17 * Major rewrite of the driver made by
18 * Klaus-Peter Junghanns <[email protected]>
19 *
20 * This program is free software and may be modified and
21 * distributed under the terms of the GNU General Public License.
22 *
23 * Please read the README file for important infos.
24 */
25
26 #include <linux/spinlock.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
29 #include <linux/interrupt.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/version.h>
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/sched.h>
36 #include <linux/proc_fs.h>
37 #include <linux/if_arp.h>
38
39 #include <dahdi/kernel.h>
40
41 #include "zaphfc.h"
42 #include "fifo.h"
43
44 #if CONFIG_PCI
45
46 #define DAHDI_B1 0
47 #define DAHDI_B2 1
48 #define DAHDI_D 2
49
50 #define D 0
51 #define B1 1
52 #define B2 2
53
54 /*
55 * Mode Te for all
56 */
57 static int modes;
58 static int nt_modes[hfc_MAX_BOARDS];
59 static int nt_modes_count;
60 static int force_l1_up;
61
62 #ifdef DEBUG
63 int debug_level;
64 #endif
65
66 #ifndef FALSE
67 #define FALSE 0
68 #endif
69 #ifndef TRUE
70 #define TRUE (!FALSE)
71 #endif
72
73 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
74 #define SET_PROC_DIRENTRY_OWNER(p) do { (p)->owner = THIS_MODULE; } while (0);
75 #else
76 #define SET_PROC_DIRENTRY_OWNER(p) do { } while (0);
77 #endif
78
79 static struct pci_device_id hfc_pci_ids[] = {
80 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_2BD0,
81 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
82 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B000,
83 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
84 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B006,
85 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
86 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B007,
87 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
88 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B008,
89 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
90 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B009,
91 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
92 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00A,
93 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
94 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00B,
95 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
96 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B00C,
97 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
98 {PCI_VENDOR_ID_CCD, PCI_DEVICE_ID_CCD_B100,
99 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
100 {PCI_VENDOR_ID_ABOCOM, PCI_DEVICE_ID_ABOCOM_2BD1,
101 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
102 {PCI_VENDOR_ID_ASUSTEK, PCI_DEVICE_ID_ASUSTEK_0675,
103 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
104 {PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_T_CONCEPT,
105 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
106 {PCI_VENDOR_ID_BERKOM, PCI_DEVICE_ID_BERKOM_A1T,
107 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
108 {PCI_VENDOR_ID_ANIGMA, PCI_DEVICE_ID_ANIGMA_MC145575,
109 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
110 {PCI_VENDOR_ID_ZOLTRIX, PCI_DEVICE_ID_ZOLTRIX_2BD0,
111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
112 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_E,
113 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
114 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_E,
115 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
116 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_IOM2_A,
117 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
118 {PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_DF_M_A,
119 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
120 {PCI_VENDOR_ID_SITECOM, PCI_DEVICE_ID_SITECOM_3069,
121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
122 {0,}
123 };
124
125 MODULE_DEVICE_TABLE(pci, hfc_pci_ids);
126
127 static int __devinit hfc_probe(struct pci_dev *dev
128 , const struct pci_device_id *ent);
129 static void __devexit hfc_remove(struct pci_dev *dev);
130
131 static struct pci_driver hfc_driver = {
132 .name = hfc_DRIVER_NAME,
133 .id_table = hfc_pci_ids,
134 .probe = hfc_probe,
135 .remove = hfc_remove,
136 };
137
138 /******************************************
139 * HW routines
140 ******************************************/
141
142 static void hfc_softreset(struct hfc_card *card)
143 {
144 printk(KERN_INFO hfc_DRIVER_PREFIX
145 "card %d: "
146 "resetting\n",
147 card->cardnum);
148
149 /*
150 * Softreset procedure. Put it on, wait and off again
151 */
152 hfc_outb(card, hfc_CIRM, hfc_CIRM_RESET);
153 udelay(6);
154 hfc_outb(card, hfc_CIRM, 0);
155
156 set_current_state(TASK_UNINTERRUPTIBLE);
157 schedule_timeout((hfc_RESET_DELAY * HZ) / 1000);
158 }
159
160 static void hfc_resetCard(struct hfc_card *card)
161 {
162 card->regs.m1 = 0;
163 hfc_outb(card, hfc_INT_M1, card->regs.m1);
164
165 card->regs.m2 = 0;
166 hfc_outb(card, hfc_INT_M2, card->regs.m2);
167
168 hfc_softreset(card);
169
170 card->regs.trm = 0;
171 hfc_outb(card, hfc_TRM, card->regs.trm);
172
173 /*
174 * Select the non-capacitive line mode for the S/T interface
175 */
176 card->regs.sctrl = hfc_SCTRL_NONE_CAP;
177
178 if (card->nt_mode) {
179 /*
180 * ST-Bit delay for NT-Mode
181 */
182 hfc_outb(card, hfc_CLKDEL, hfc_CLKDEL_NT);
183
184 card->regs.sctrl |= hfc_SCTRL_MODE_NT;
185 } else {
186 /*
187 * ST-Bit delay for TE-Mode
188 */
189 hfc_outb(card, hfc_CLKDEL, hfc_CLKDEL_TE);
190
191 card->regs.sctrl |= hfc_SCTRL_MODE_TE;
192 }
193
194 hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
195
196 /*
197 * S/T Auto awake
198 */
199 card->regs.sctrl_e = hfc_SCTRL_E_AUTO_AWAKE;
200 hfc_outb(card, hfc_SCTRL_E, card->regs.sctrl_e);
201
202 /*
203 * No B-channel enabled at startup
204 */
205 card->regs.sctrl_r = 0;
206 hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
207
208 /*
209 * HFC Master Mode
210 */
211 hfc_outb(card, hfc_MST_MODE, hfc_MST_MODE_MASTER);
212
213 /*
214 * Connect internal blocks
215 */
216 card->regs.connect =
217 hfc_CONNECT_B1_HFC_from_ST |
218 hfc_CONNECT_B1_ST_from_HFC |
219 hfc_CONNECT_B1_GCI_from_HFC |
220 hfc_CONNECT_B2_HFC_from_ST |
221 hfc_CONNECT_B2_ST_from_HFC |
222 hfc_CONNECT_B2_GCI_from_HFC;
223 hfc_outb(card, hfc_CONNECT, card->regs.connect);
224
225 /*
226 * All bchans are HDLC by default, not useful, actually
227 * since mode is set during open()
228 */
229 hfc_outb(card, hfc_CTMT, 0);
230
231 /*
232 * bit order
233 */
234 hfc_outb(card, hfc_CIRM, 0);
235
236 /*
237 * Enable D-rx FIFO. At least one FIFO must be enabled (by specs)
238 */
239 card->regs.fifo_en = hfc_FIFOEN_DRX;
240 hfc_outb(card, hfc_FIFO_EN, card->regs.fifo_en);
241
242 card->late_irqs = 0;
243
244 /*
245 * Clear already pending ints
246 */
247 hfc_inb(card, hfc_INT_S1);
248 hfc_inb(card, hfc_INT_S2);
249
250 /*
251 * Enable IRQ output
252 */
253 card->regs.m1 = hfc_INTS_DREC | hfc_INTS_L1STATE | hfc_INTS_TIMER;
254 hfc_outb(card, hfc_INT_M1, card->regs.m1);
255
256 card->regs.m2 = hfc_M2_IRQ_ENABLE;
257 hfc_outb(card, hfc_INT_M2, card->regs.m2);
258
259 /*
260 * Unlocks the states machine
261 */
262 hfc_outb(card, hfc_STATES, 0);
263
264 /*
265 * There's no need to explicitly activate L1 now.
266 * Activation is managed inside the interrupt routine.
267 */
268 }
269
270 static void hfc_update_fifo_state(struct hfc_card *card)
271 {
272 /*
273 * I'm not sure if irqsave is needed but there could be a race
274 * condition since hfc_update_fifo_state could be called from
275 * both the IRQ handler and the *_(open|close) functions
276 */
277
278 unsigned long flags;
279 spin_lock_irqsave(&card->chans[B1].lock, flags);
280 if (!card->fifo_suspended &&
281 (card->chans[B1].status == open_framed ||
282 card->chans[B1].status == open_voice)) {
283
284 if (!(card->regs.fifo_en & hfc_FIFOEN_B1RX)) {
285 card->regs.fifo_en |= hfc_FIFOEN_B1RX;
286 hfc_clear_fifo_rx(&card->chans[B1].rx);
287 }
288
289 if (!(card->regs.fifo_en & hfc_FIFOEN_B1TX)) {
290 card->regs.fifo_en |= hfc_FIFOEN_B1TX;
291 hfc_clear_fifo_tx(&card->chans[B1].tx);
292 }
293 } else {
294 if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
295 card->regs.fifo_en &= ~hfc_FIFOEN_B1RX;
296 if (card->regs.fifo_en & hfc_FIFOEN_B1TX)
297 card->regs.fifo_en &= ~hfc_FIFOEN_B1TX;
298 }
299 spin_unlock_irqrestore(&card->chans[B1].lock, flags);
300
301 spin_lock_irqsave(&card->chans[B2].lock, flags);
302 if (!card->fifo_suspended &&
303 (card->chans[B2].status == open_framed ||
304 card->chans[B2].status == open_voice ||
305 card->chans[B2].status == sniff_aux)) {
306
307 if (!(card->regs.fifo_en & hfc_FIFOEN_B2RX)) {
308 card->regs.fifo_en |= hfc_FIFOEN_B2RX;
309 hfc_clear_fifo_rx(&card->chans[B2].rx);
310 }
311
312 if (!(card->regs.fifo_en & hfc_FIFOEN_B2TX)) {
313 card->regs.fifo_en |= hfc_FIFOEN_B2TX;
314 hfc_clear_fifo_tx(&card->chans[B2].tx);
315 }
316 } else {
317 if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
318 card->regs.fifo_en &= ~hfc_FIFOEN_B2RX;
319 if (card->regs.fifo_en & hfc_FIFOEN_B2TX)
320 card->regs.fifo_en &= ~hfc_FIFOEN_B2TX;
321 }
322 spin_unlock_irqrestore(&card->chans[B2].lock, flags);
323
324 spin_lock_irqsave(&card->chans[D].lock, flags);
325 if (!card->fifo_suspended &&
326 card->chans[D].status == open_framed) {
327
328 if (!(card->regs.fifo_en & hfc_FIFOEN_DTX)) {
329 card->regs.fifo_en |= hfc_FIFOEN_DTX;
330
331 card->chans[D].tx.ugly_framebuf_size = 0;
332 card->chans[D].tx.ugly_framebuf_off = 0;
333 }
334 } else {
335 if (card->regs.fifo_en & hfc_FIFOEN_DTX)
336 card->regs.fifo_en &= ~hfc_FIFOEN_DTX;
337 }
338 spin_unlock_irqrestore(&card->chans[D].lock, flags);
339
340 hfc_outb(card, hfc_FIFO_EN, card->regs.fifo_en);
341 }
342
343 static inline void hfc_suspend_fifo(struct hfc_card *card)
344 {
345 card->fifo_suspended = TRUE;
346
347 hfc_update_fifo_state(card);
348
349 /*
350 * When L1 goes down D rx receives garbage; it is nice to
351 * clear it to avoid a CRC error on reactivation
352 * udelay is needed because the FIFO deactivation happens
353 * in 250us
354 */
355 udelay(250);
356 hfc_clear_fifo_rx(&card->chans[D].rx);
357
358 #ifdef DEBUG
359 if (debug_level >= 3) {
360 printk(KERN_DEBUG hfc_DRIVER_PREFIX
361 "card %d: "
362 "FIFOs suspended\n",
363 card->cardnum);
364 }
365 #endif
366 }
367
368 static inline void hfc_resume_fifo(struct hfc_card *card)
369 {
370 card->fifo_suspended = FALSE;
371
372 hfc_update_fifo_state(card);
373
374 #ifdef DEBUG
375 if (debug_level >= 3) {
376 printk(KERN_DEBUG hfc_DRIVER_PREFIX
377 "card %d: "
378 "FIFOs resumed\n",
379 card->cardnum);
380 }
381 #endif
382 }
383
384 static void hfc_check_l1_up(struct hfc_card *card)
385 {
386 if ((!card->nt_mode && card->l1_state != 7)
387 || (card->nt_mode && card->l1_state != 3)) {
388
389 hfc_outb(card, hfc_STATES, hfc_STATES_DO_ACTION |
390 hfc_STATES_ACTIVATE|
391 hfc_STATES_NT_G2_G3);
392
393 /*
394 * 0 because this is quite verbose when an inferface is unconnected, jaw
395 */
396 #if 0
397 if (debug_level >= 1) {
398 printk(KERN_DEBUG hfc_DRIVER_PREFIX
399 "card %d: "
400 "L1 is down, bringing up L1.\n",
401 card->cardnum);
402 }
403 #endif
404 }
405 }
406
407
408 /*******************
409 * Dahdi interface *
410 *******************/
411
412 static int hfc_zap_open(struct dahdi_chan *zaptel_chan)
413 {
414 struct hfc_chan_duplex *chan = zaptel_chan->pvt;
415 struct hfc_card *card = chan->card;
416
417 spin_lock(&chan->lock);
418
419 switch (chan->number) {
420 case D:
421 if (chan->status != free &&
422 chan->status != open_framed) {
423 spin_unlock(&chan->lock);
424 return -EBUSY;
425 }
426 chan->status = open_framed;
427 break;
428
429 case B1:
430 case B2:
431 if (chan->status != free) {
432 spin_unlock(&chan->lock);
433 return -EBUSY;
434 }
435 chan->status = open_voice;
436 break;
437 }
438
439 chan->open_by_zaptel = TRUE;
440 try_module_get(THIS_MODULE);
441 spin_unlock(&chan->lock);
442
443 switch (chan->number) {
444 case D:
445 break;
446
447 case B1:
448 card->regs.m2 |= hfc_M2_PROC_TRANS;
449 /*
450 * Enable transparent mode
451 */
452 card->regs.ctmt |= hfc_CTMT_TRANSB1;
453 /*
454 * Reversed bit order
455 */
456 card->regs.cirm |= hfc_CIRM_B1_REV;
457 /*
458 * Enable transmission
459 */
460 card->regs.sctrl |= hfc_SCTRL_B1_ENA;
461 /*
462 * Enable reception
463 */
464 card->regs.sctrl_r |= hfc_SCTRL_R_B1_ENA;
465 break;
466
467 case B2:
468 card->regs.m2 |= hfc_M2_PROC_TRANS;
469 card->regs.ctmt |= hfc_CTMT_TRANSB2;
470 card->regs.cirm |= hfc_CIRM_B2_REV;
471 card->regs.sctrl |= hfc_SCTRL_B2_ENA;
472 card->regs.sctrl_r |= hfc_SCTRL_R_B2_ENA;
473 break;
474
475 }
476
477 /*
478 * If not already enabled, enable processing transition (8KHz)
479 * interrupt
480 */
481 hfc_outb(card, hfc_INT_M2, card->regs.m2);
482 hfc_outb(card, hfc_CTMT, card->regs.ctmt);
483 hfc_outb(card, hfc_CIRM, card->regs.cirm);
484 hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
485 hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
486
487 hfc_update_fifo_state(card);
488
489 printk(KERN_INFO hfc_DRIVER_PREFIX
490 "card %d: "
491 "chan %s opened as %s.\n",
492 card->cardnum,
493 chan->name,
494 zaptel_chan->name);
495
496 return 0;
497 }
498
499 static int hfc_zap_close(struct dahdi_chan *zaptel_chan)
500 {
501 struct hfc_chan_duplex *chan = zaptel_chan->pvt;
502 struct hfc_card *card = chan->card;
503
504 if (!card) {
505 printk(KERN_CRIT hfc_DRIVER_PREFIX
506 "hfc_zap_close called with NULL card\n");
507 return -1;
508 }
509
510 spin_lock(&chan->lock);
511
512 if (chan->status == free) {
513 spin_unlock(&chan->lock);
514 return -EINVAL;
515 }
516
517 chan->status = free;
518 chan->open_by_zaptel = FALSE;
519
520 spin_unlock(&chan->lock);
521
522 switch (chan->number) {
523 case D:
524 break;
525
526 case B1:
527 card->regs.ctmt &= ~hfc_CTMT_TRANSB1;
528 card->regs.cirm &= ~hfc_CIRM_B1_REV;
529 card->regs.sctrl &= ~hfc_SCTRL_B1_ENA;
530 card->regs.sctrl_r &= ~hfc_SCTRL_R_B1_ENA;
531 break;
532
533 case B2:
534 card->regs.ctmt &= ~hfc_CTMT_TRANSB2;
535 card->regs.cirm &= ~hfc_CIRM_B2_REV;
536 card->regs.sctrl &= ~hfc_SCTRL_B2_ENA;
537 card->regs.sctrl_r &= ~hfc_SCTRL_R_B2_ENA;
538 break;
539 }
540
541 if (card->chans[B1].status == free &&
542 card->chans[B2].status == free)
543 card->regs.m2 &= ~hfc_M2_PROC_TRANS;
544
545 hfc_outb(card, hfc_INT_M2, card->regs.m2);
546 hfc_outb(card, hfc_CTMT, card->regs.ctmt);
547 hfc_outb(card, hfc_CIRM, card->regs.cirm);
548 hfc_outb(card, hfc_SCTRL, card->regs.sctrl);
549 hfc_outb(card, hfc_SCTRL_R, card->regs.sctrl_r);
550
551 hfc_update_fifo_state(card);
552
553 module_put(THIS_MODULE);
554
555 printk(KERN_INFO hfc_DRIVER_PREFIX
556 "card %d: "
557 "chan %s closed as %s.\n",
558 card->cardnum,
559 chan->name,
560 zaptel_chan->name);
561
562 return 0;
563 }
564
565 static int hfc_zap_rbsbits(struct dahdi_chan *chan, int bits)
566 {
567 return 0;
568 }
569
570 static int hfc_zap_ioctl(struct dahdi_chan *chan,
571 unsigned int cmd, unsigned long data)
572 {
573 switch (cmd) {
574
575 default:
576 return -ENOTTY;
577 }
578
579 return 0;
580 }
581
582 static void hfc_hdlc_hard_xmit(struct dahdi_chan *d_chan)
583 {
584 struct hfc_chan_duplex *chan = d_chan->pvt;
585 struct hfc_card *card = chan->card;
586 struct dahdi_hfc *hfccard = card->ztdev;
587
588 atomic_inc(&hfccard->hdlc_pending);
589
590 }
591
592 static int hfc_zap_startup(struct file *file, struct dahdi_span *span)
593 {
594 struct dahdi_hfc *zthfc = dahdi_hfc_from_span(span);
595 struct hfc_card *hfctmp = zthfc->card;
596 int alreadyrunning;
597
598 if (!hfctmp) {
599 printk(KERN_INFO hfc_DRIVER_PREFIX
600 "card %d: "
601 "no card for span at startup!\n",
602 hfctmp->cardnum);
603 }
604
605 alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
606
607 if (!alreadyrunning)
608 span->flags |= DAHDI_FLAG_RUNNING;
609
610 return 0;
611 }
612
613 static int hfc_zap_shutdown(struct dahdi_span *span)
614 {
615 return 0;
616 }
617
618 static int hfc_zap_maint(struct dahdi_span *span, int cmd)
619 {
620 return 0;
621 }
622
623 static int hfc_zap_chanconfig(struct file *file, struct dahdi_chan *d_chan,
624 int sigtype)
625 {
626 struct hfc_chan_duplex *chan = d_chan->pvt;
627 struct hfc_card *card = chan->card;
628 struct dahdi_hfc *hfccard = card->ztdev;
629
630 if ((sigtype == DAHDI_SIG_HARDHDLC) && (hfccard->sigchan == d_chan)) {
631 hfccard->sigactive = 0;
632 atomic_set(&hfccard->hdlc_pending, 0);
633 }
634
635 return 0;
636 }
637
638 static int hfc_zap_spanconfig(struct file *file, struct dahdi_span *span,
639 struct dahdi_lineconfig *lc)
640 {
641 span->lineconfig = lc->lineconfig;
642
643 return 0;
644 }
645
646 static const struct dahdi_span_ops hfc_zap_span_ops = {
647 .owner = THIS_MODULE,
648 .chanconfig = hfc_zap_chanconfig,
649 .spanconfig = hfc_zap_spanconfig,
650 .startup = hfc_zap_startup,
651 .shutdown = hfc_zap_shutdown,
652 .maint = hfc_zap_maint,
653 .rbsbits = hfc_zap_rbsbits,
654 .open = hfc_zap_open,
655 .close = hfc_zap_close,
656 .ioctl = hfc_zap_ioctl,
657 .hdlc_hard_xmit = hfc_hdlc_hard_xmit
658 };
659
660 static int hfc_zap_initialize(struct dahdi_hfc *hfccard)
661 {
662 struct hfc_card *hfctmp = hfccard->card;
663 int i;
664
665 hfccard->ddev = dahdi_create_device();
666 hfccard->ddev->manufacturer = "Cologne Chips";
667 hfccard->ddev->devicetype = "HFC-S PCI-A ISDN";
668 hfccard->ddev->location = kasprintf(GFP_KERNEL,
669 "PCI Bus %02d Slot %02d",
670 hfctmp->pcidev->bus->number,
671 PCI_SLOT(hfctmp->pcidev->devfn) + 1);
672 memset(&hfccard->span, 0x0, sizeof(struct dahdi_span));
673 sprintf(hfccard->span.name, "ZTHFC%d", hfctmp->cardnum + 1);
674 sprintf(hfccard->span.desc,
675 "HFC-S PCI A ISDN card %d [%s] ",
676 hfctmp->cardnum,
677 hfctmp->nt_mode ? "NT" : "TE");
678 hfccard->span.spantype = hfctmp->nt_mode ? SPANTYPE_DIGITAL_BRI_NT :
679 SPANTYPE_DIGITAL_BRI_TE;
680 hfccard->span.flags = 0;
681 hfccard->span.ops = &hfc_zap_span_ops;
682 hfccard->span.chans = hfccard->_chans;
683 hfccard->span.channels = 3;
684 for (i = 0; i < hfccard->span.channels; i++)
685 hfccard->_chans[i] = &hfccard->chans[i];
686 hfccard->span.deflaw = DAHDI_LAW_ALAW;
687 hfccard->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_CCS;
688 hfccard->span.offset = 0;
689
690 for (i = 0; i < hfccard->span.channels; i++) {
691 memset(&hfccard->chans[i], 0x0, sizeof(struct dahdi_chan));
692
693 sprintf(hfccard->chans[i].name,
694 "ZTHFC%d/%d/%d",
695 hfctmp->cardnum + 1, 0, i + 1);
696
697 printk(KERN_INFO hfc_DRIVER_PREFIX
698 "card %d: "
699 "registered %s\n",
700 hfctmp->cardnum,
701 hfccard->chans[i].name);
702
703 if (i == hfccard->span.channels - 1) {
704 hfccard->chans[i].sigcap = DAHDI_SIG_HARDHDLC;
705 hfccard->sigchan = &hfccard->chans[DAHDI_D];
706 hfccard->sigactive = 0;
707 atomic_set(&hfccard->hdlc_pending, 0);
708 } else {
709 hfccard->chans[i].sigcap =
710 DAHDI_SIG_CLEAR | DAHDI_SIG_DACS;
711 }
712
713 hfccard->chans[i].chanpos = i + 1;
714 }
715
716 hfccard->chans[DAHDI_D].readchunk =
717 hfctmp->chans[D].rx.zaptel_buffer;
718
719 hfccard->chans[DAHDI_D].writechunk =
720 hfctmp->chans[D].tx.zaptel_buffer;
721
722 hfccard->chans[DAHDI_D].pvt = &hfctmp->chans[D];
723
724 hfccard->chans[DAHDI_B1].readchunk =
725 hfctmp->chans[B1].rx.zaptel_buffer;
726
727 hfccard->chans[DAHDI_B1].writechunk =
728 hfctmp->chans[B1].tx.zaptel_buffer;
729
730 hfccard->chans[DAHDI_B1].pvt = &hfctmp->chans[B1];
731
732 hfccard->chans[DAHDI_B2].readchunk =
733 hfctmp->chans[B2].rx.zaptel_buffer;
734
735 hfccard->chans[DAHDI_B2].writechunk =
736 hfctmp->chans[B2].tx.zaptel_buffer;
737
738 hfccard->chans[DAHDI_B2].pvt = &hfctmp->chans[B2];
739
740 list_add_tail(&hfccard->span.device_node, &hfccard->ddev->spans);
741 if (dahdi_register_device(hfccard->ddev, &hfctmp->pcidev->dev)) {
742 printk(KERN_ERR "unable to register DAHDI device %s!\n",
743 hfccard->span.name);
744 return -1; /* FIXME: release resources? */
745 }
746
747 return 0;
748 }
749
750 static void hfc_dahdi_free(struct dahdi_hfc *hfccard)
751 {
752 kfree(hfccard->ddev->location);
753 dahdi_free_device(hfccard->ddev);
754 }
755
756 static void hfc_zap_transmit(struct hfc_chan_simplex *chan)
757 {
758 hfc_fifo_put(chan, chan->zaptel_buffer, DAHDI_CHUNKSIZE);
759 }
760
761 static void hfc_zap_receive(struct hfc_chan_simplex *chan)
762 {
763 hfc_fifo_get(chan, chan->zaptel_buffer, DAHDI_CHUNKSIZE);
764 }
765
766 /******************************************
767 * Interrupt Handler
768 ******************************************/
769
770 static void hfc_handle_timer_interrupt(struct hfc_card *card);
771 static void hfc_handle_state_interrupt(struct hfc_card *card);
772 static void hfc_handle_processing_interrupt(struct hfc_card *card);
773 static void hfc_frame_arrived(struct hfc_chan_duplex *chan);
774 static void hfc_handle_voice(struct hfc_card *card);
775
776 #if (KERNEL_VERSION(2, 6, 24) < LINUX_VERSION_CODE)
777 static irqreturn_t hfc_interrupt(int irq, void *dev_id)
778 #else
779 static irqreturn_t hfc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
780 #endif
781 {
782 struct hfc_card *card = dev_id;
783 unsigned long flags;
784 u8 status, s1, s2;
785
786 if (!card) {
787 printk(KERN_CRIT hfc_DRIVER_PREFIX
788 "spurious interrupt (IRQ %d)\n",
789 irq);
790 return IRQ_NONE;
791 }
792
793 spin_lock_irqsave(&card->lock, flags);
794 status = hfc_inb(card, hfc_STATUS);
795 if (!(status & hfc_STATUS_ANYINT)) {
796 /*
797 * maybe we are sharing the irq
798 */
799 spin_unlock_irqrestore(&card->lock, flags);
800 return IRQ_NONE;
801 }
802
803 /* We used to ingore the IRQ when the card was in processing
804 * state but apparently there is no restriction to access the
805 * card in such state:
806 *
807 * Joerg Ciesielski wrote:
808 * > There is no restriction for the IRQ handler to access
809 * > HFC-S PCI during processing phase. A IRQ latency of 375 us
810 * > is also no problem since there are no interrupt sources in
811 * > HFC-S PCI which must be handled very fast.
812 * > Due to its deep fifos the IRQ latency can be several ms with
813 * > out the risk of loosing data. Even the S/T state interrupts
814 * > must not be handled with a latency less than <5ms.
815 * >
816 * > The processing phase only indicates that HFC-S PCI is
817 * > processing the Fifos as PCI master so that data is read and
818 * > written in the 32k memory window. But there is no restriction
819 * > to access data in the memory window during this time.
820 *
821 * // if (status & hfc_STATUS_PCI_PROC) {
822 * // return IRQ_HANDLED;
823 * // }
824 */
825
826 s1 = hfc_inb(card, hfc_INT_S1);
827 s2 = hfc_inb(card, hfc_INT_S2);
828
829 if (s1 != 0) {
830 if (s1 & hfc_INTS_TIMER) {
831 /*
832 * timer (bit 7)
833 */
834 hfc_handle_timer_interrupt(card);
835 }
836
837 if (s1 & hfc_INTS_L1STATE) {
838 /*
839 * state machine (bit 6)
840 */
841 hfc_handle_state_interrupt(card);
842 }
843
844 if (s1 & hfc_INTS_DREC) {
845 /*
846 * D chan RX (bit 5)
847 */
848 hfc_frame_arrived(&card->chans[D]);
849 }
850
851 if (s1 & hfc_INTS_B1REC) {
852 /*
853 * B1 chan RX (bit 3)
854 */
855 hfc_frame_arrived(&card->chans[B1]);
856 }
857
858 if (s1 & hfc_INTS_B2REC) {
859 /*
860 * B2 chan RX (bit 4)
861 */
862 hfc_frame_arrived(&card->chans[B2]);
863 }
864
865 if (s1 & hfc_INTS_DTRANS) {
866 /*
867 * D chan TX (bit 2)
868 */
869 }
870
871 if (s1 & hfc_INTS_B1TRANS) {
872 /*
873 * B1 chan TX (bit 0)
874 */
875 }
876
877 if (s1 & hfc_INTS_B2TRANS) {
878 /*
879 * B2 chan TX (bit 1)
880 */
881 }
882
883 }
884
885 if (s2 != 0) {
886 if (s2 & hfc_M2_PMESEL) {
887 /*
888 * kaboom irq (bit 7)
889 *
890 * CologneChip says:
891 *
892 * the meaning of this fatal error bit is that HFC-S
893 * PCI as PCI master could not access the PCI bus
894 * within 125us to finish its data processing. If this
895 * happens only very seldom it does not cause big
896 * problems but of course some B-channel or D-channel
897 * data will be corrupted due to this event.
898 *
899 * Unfortunately this bit is only set once after the
900 * problem occurs and can only be reseted by a
901 * software reset. That means it is not easily
902 * possible to check how often this fatal error
903 * happens.
904 *
905 */
906
907 if (!card->sync_loss_reported) {
908 printk(KERN_CRIT hfc_DRIVER_PREFIX
909 "card %d: "
910 "sync lost, pci performance too low!\n",
911 card->cardnum);
912
913 card->sync_loss_reported = TRUE;
914 }
915 }
916
917 if (s2 & hfc_M2_GCI_MON_REC) {
918 /*
919 * RxR monitor channel (bit 2)
920 */
921 }
922
923 if (s2 & hfc_M2_GCI_I_CHG) {
924 /*
925 * GCI I-change (bit 1)
926 */
927 }
928
929 if (s2 & hfc_M2_PROC_TRANS) {
930 /*
931 * processing/non-processing transition (bit 0)
932 */
933 hfc_handle_processing_interrupt(card);
934 }
935
936 }
937
938 spin_unlock_irqrestore(&card->lock, flags);
939
940 return IRQ_HANDLED;
941 }
942
943 static void hfc_handle_timer_interrupt(struct hfc_card *card)
944 {
945 if (card->ignore_first_timer_interrupt) {
946 card->ignore_first_timer_interrupt = FALSE;
947 return;
948 }
949
950 if ((card->nt_mode && card->l1_state == 3) ||
951 (!card->nt_mode && card->l1_state == 7)) {
952
953 card->regs.ctmt &= ~hfc_CTMT_TIMER_MASK;
954 hfc_outb(card, hfc_CTMT, card->regs.ctmt);
955
956 hfc_resume_fifo(card);
957 }
958 }
959
960 static void hfc_handle_state_interrupt(struct hfc_card *card)
961 {
962 u8 new_state = hfc_inb(card, hfc_STATES) & hfc_STATES_STATE_MASK;
963
964 #ifdef DEBUG
965 if (debug_level >= 1) {
966 printk(KERN_DEBUG hfc_DRIVER_PREFIX
967 "card %d: "
968 "layer 1 state = %c%d\n",
969 card->cardnum,
970 card->nt_mode ? 'G' : 'F',
971 new_state);
972 }
973 #endif
974
975 if (card->nt_mode) {
976 /*
977 * NT mode
978 */
979
980 if (new_state == 3) {
981 /*
982 * fix to G3 state (see specs)
983 */
984 hfc_outb(card, hfc_STATES, hfc_STATES_LOAD_STATE | 3);
985 }
986
987 if (new_state == 3 && card->l1_state != 3)
988 hfc_resume_fifo(card);
989
990 if (new_state != 3 && card->l1_state == 3)
991 hfc_suspend_fifo(card);
992
993 } else {
994 if (new_state == 3) {
995 /*
996 * Keep L1 up... zaptel & libpri expects
997 * a always up L1...
998 * Enable only when using an unpatched libpri
999 */
1000
1001 if (force_l1_up) {
1002 hfc_outb(card, hfc_STATES,
1003 hfc_STATES_DO_ACTION |
1004 hfc_STATES_ACTIVATE|
1005 hfc_STATES_NT_G2_G3);
1006 }
1007 }
1008
1009 if (new_state == 7 && card->l1_state != 7) {
1010 /*
1011 * TE is now active, schedule FIFO activation after
1012 * some time, otherwise the first frames are lost
1013 */
1014
1015 card->regs.ctmt |= hfc_CTMT_TIMER_50 |
1016 hfc_CTMT_TIMER_CLEAR;
1017 hfc_outb(card, hfc_CTMT, card->regs.ctmt);
1018
1019 /*
1020 * Activating the timer firest an
1021 * interrupt immediately, we
1022 * obviously need to ignore it
1023 */
1024 card->ignore_first_timer_interrupt = TRUE;
1025 }
1026
1027 if (new_state != 7 && card->l1_state == 7) {
1028 /*
1029 * TE has become inactive, disable FIFO
1030 */
1031 hfc_suspend_fifo(card);
1032 }
1033 }
1034
1035 card->l1_state = new_state;
1036 }
1037
1038 static void hfc_handle_processing_interrupt(struct hfc_card *card)
1039 {
1040 int available_bytes = 0;
1041
1042 /*
1043 * Synchronize with the first enabled channel
1044 */
1045 if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
1046 available_bytes = hfc_fifo_used_rx(&card->chans[B1].rx);
1047 if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
1048 available_bytes = hfc_fifo_used_rx(&card->chans[B2].rx);
1049 else
1050 available_bytes = -1;
1051
1052 if ((available_bytes == -1 && card->ticks == 8) ||
1053 available_bytes >= DAHDI_CHUNKSIZE + hfc_RX_FIFO_PRELOAD) {
1054 card->ticks = 0;
1055
1056 if (available_bytes > DAHDI_CHUNKSIZE*2 + hfc_RX_FIFO_PRELOAD) {
1057 card->late_irqs++;
1058 /*
1059 * we are out of sync, clear fifos, jaw
1060 */
1061 hfc_clear_fifo_rx(&card->chans[B1].rx);
1062 hfc_clear_fifo_tx(&card->chans[B1].tx);
1063 hfc_clear_fifo_rx(&card->chans[B2].rx);
1064 hfc_clear_fifo_tx(&card->chans[B2].tx);
1065
1066 #ifdef DEBUG
1067 if (debug_level >= 4) {
1068 printk(KERN_DEBUG hfc_DRIVER_PREFIX
1069 "card %d: "
1070 "late IRQ, %d bytes late\n",
1071 card->cardnum,
1072 available_bytes -
1073 (DAHDI_CHUNKSIZE +
1074 hfc_RX_FIFO_PRELOAD));
1075 }
1076 #endif
1077 } else {
1078 hfc_handle_voice(card);
1079 }
1080 }
1081
1082 card->ticks++;
1083 }
1084
1085
1086 static void hfc_handle_voice(struct hfc_card *card)
1087 {
1088 struct dahdi_hfc *hfccard = card->ztdev;
1089 int frame_left, res;
1090 unsigned char buf[hfc_HDLC_BUF_LEN];
1091 unsigned int size = sizeof(buf) / sizeof(buf[0]);
1092
1093
1094 if (card->chans[B1].status != open_voice &&
1095 card->chans[B2].status != open_voice)
1096 return;
1097
1098 dahdi_transmit(&hfccard->span);
1099
1100 if (card->regs.fifo_en & hfc_FIFOEN_B1TX)
1101 hfc_zap_transmit(&card->chans[B1].tx);
1102 if (card->regs.fifo_en & hfc_FIFOEN_B2TX)
1103 hfc_zap_transmit(&card->chans[B2].tx);
1104
1105 /*
1106 * dahdi hdlc frame tx
1107 */
1108
1109 if (atomic_read(&hfccard->hdlc_pending)) {
1110 hfc_check_l1_up(card);
1111 res = dahdi_hdlc_getbuf(hfccard->sigchan, buf, &size);
1112 if (size > 0) {
1113 hfccard->sigactive = 1;
1114 memcpy(card->chans[D].tx.ugly_framebuf +
1115 card->chans[D].tx.ugly_framebuf_size,
1116 buf, size);
1117 card->chans[D].tx.ugly_framebuf_size += size;
1118 if (res != 0) {
1119 hfc_fifo_put_frame(&card->chans[D].tx,
1120 card->chans[D].tx.ugly_framebuf,
1121 card->chans[D].tx.ugly_framebuf_size);
1122 ++hfccard->frames_out;
1123 hfccard->sigactive = 0;
1124 card->chans[D].tx.ugly_framebuf_size
1125 = 0;
1126 atomic_dec(&hfccard->hdlc_pending);
1127 }
1128 }
1129 }
1130 /*
1131 * dahdi hdlc frame tx done
1132 */
1133
1134 if (card->regs.fifo_en & hfc_FIFOEN_B1RX)
1135 hfc_zap_receive(&card->chans[B1].rx);
1136 else
1137 memset(&card->chans[B1].rx.zaptel_buffer, 0x7f,
1138 sizeof(card->chans[B1].rx.zaptel_buffer));
1139
1140 if (card->regs.fifo_en & hfc_FIFOEN_B2RX)
1141 hfc_zap_receive(&card->chans[B2].rx);
1142 else
1143 memset(&card->chans[B2].rx.zaptel_buffer, 0x7f,
1144 sizeof(card->chans[B1].rx.zaptel_buffer));
1145
1146 /*
1147 * Echo cancellation
1148 */
1149 dahdi_ec_chunk(&hfccard->chans[DAHDI_B1],
1150 card->chans[B1].rx.zaptel_buffer,
1151 card->chans[B1].tx.zaptel_buffer);
1152 dahdi_ec_chunk(&hfccard->chans[DAHDI_B2],
1153 card->chans[B2].rx.zaptel_buffer,
1154 card->chans[B2].tx.zaptel_buffer);
1155
1156 /*
1157 * dahdi hdlc frame rx
1158 */
1159 if (hfc_fifo_has_frames(&card->chans[D].rx))
1160 hfc_frame_arrived(&card->chans[D]);
1161
1162 if (card->chans[D].rx.ugly_framebuf_size) {
1163 frame_left = card->chans[D].rx.ugly_framebuf_size -
1164 card->chans[D].rx.ugly_framebuf_off ;
1165 if (frame_left > hfc_HDLC_BUF_LEN) {
1166 dahdi_hdlc_putbuf(hfccard->sigchan,
1167 card->chans[D].rx.ugly_framebuf +
1168 card->chans[D].rx.ugly_framebuf_off,
1169 hfc_HDLC_BUF_LEN);
1170 card->chans[D].rx.ugly_framebuf_off +=
1171 hfc_HDLC_BUF_LEN;
1172 } else {
1173 dahdi_hdlc_putbuf(hfccard->sigchan,
1174 card->chans[D].rx.ugly_framebuf +
1175 card->chans[D].rx.ugly_framebuf_off,
1176 frame_left);
1177 dahdi_hdlc_finish(hfccard->sigchan);
1178 card->chans[D].rx.ugly_framebuf_size = 0;
1179 card->chans[D].rx.ugly_framebuf_off = 0;
1180 }
1181 }
1182 /*
1183 * dahdi hdlc frame rx done
1184 */
1185
1186 if (hfccard->span.flags & DAHDI_FLAG_RUNNING)
1187 dahdi_receive(&hfccard->span);
1188
1189 }
1190
1191 static void hfc_frame_arrived(struct hfc_chan_duplex *chan)
1192 {
1193 struct hfc_card *card = chan->card;
1194 int antiloop = 16;
1195
1196 while (hfc_fifo_has_frames(&chan->rx) && --antiloop) {
1197 int frame_size = hfc_fifo_get_frame_size(&chan->rx);
1198
1199 if (frame_size < 3) {
1200 #ifdef DEBUG
1201 if (debug_level >= 2)
1202 printk(KERN_DEBUG hfc_DRIVER_PREFIX
1203 "card %d: "
1204 "chan %s: "
1205 "invalid frame received, "
1206 "just %d bytes\n",
1207 card->cardnum,
1208 chan->name,
1209 frame_size);
1210 #endif
1211
1212 hfc_fifo_drop_frame(&chan->rx);
1213
1214
1215 continue;
1216 } else if (frame_size == 3) {
1217 #ifdef DEBUG
1218 if (debug_level >= 2)
1219 printk(KERN_DEBUG hfc_DRIVER_PREFIX
1220 "card %d: "
1221 "chan %s: "
1222 "empty frame received\n",
1223 card->cardnum,
1224 chan->name);
1225 #endif
1226
1227 hfc_fifo_drop_frame(&chan->rx);
1228
1229
1230 continue;
1231 }
1232
1233 if (chan->open_by_zaptel) {
1234 if (card->chans[D].rx.ugly_framebuf_size) {
1235 /*
1236 * We have to wait for Dahdi to transmit the
1237 * frame... wait for next time
1238 */
1239
1240 break;
1241 } else {
1242 card->chans[D].rx.ugly_framebuf_size =
1243 frame_size - 1;
1244
1245 if (hfc_fifo_get_frame(&card->chans[D].rx,
1246 card->chans[D].rx.ugly_framebuf,
1247 frame_size - 1) == -1) {
1248 continue;
1249 }
1250 }
1251 } else {
1252 hfc_fifo_drop_frame(&chan->rx);
1253 }
1254 }
1255
1256 if (!antiloop)
1257 printk(KERN_CRIT hfc_DRIVER_PREFIX
1258 "card %d: "
1259 "Infinite loop detected\n",
1260 card->cardnum);
1261 }
1262
1263 /******************************************
1264 * Module initialization and cleanup
1265 ******************************************/
1266
1267 static int __devinit hfc_probe(struct pci_dev *pci_dev,
1268 const struct pci_device_id *ent)
1269 {
1270 static int cardnum;
1271 int err;
1272 int i;
1273
1274 struct hfc_card *card = NULL;
1275 struct dahdi_hfc *zthfc = NULL;
1276 card = kmalloc(sizeof(struct hfc_card), GFP_KERNEL);
1277 if (!card) {
1278 printk(KERN_CRIT hfc_DRIVER_PREFIX
1279 "unable to kmalloc!\n");
1280 err = -ENOMEM;
1281 goto err_alloc_hfccard;
1282 }
1283
1284 memset(card, 0x00, sizeof(struct hfc_card));
1285 card->cardnum = cardnum;
1286 card->pcidev = pci_dev;
1287 spin_lock_init(&card->lock);
1288
1289 pci_set_drvdata(pci_dev, card);
1290
1291 err = pci_enable_device(pci_dev);
1292 if (err)
1293 goto err_pci_enable_device;
1294
1295 err = pci_set_dma_mask(pci_dev, PCI_DMA_32BIT);
1296 if (err) {
1297 printk(KERN_ERR hfc_DRIVER_PREFIX
1298 "card %d: "
1299 "No suitable DMA configuration available.\n",
1300 card->cardnum);
1301 goto err_pci_set_dma_mask;
1302 }
1303
1304 pci_write_config_word(pci_dev, PCI_COMMAND, PCI_COMMAND_MEMORY);
1305 err = pci_request_regions(pci_dev, hfc_DRIVER_NAME);
1306 if (err) {
1307 printk(KERN_CRIT hfc_DRIVER_PREFIX
1308 "card %d: "
1309 "cannot request I/O memory region\n",
1310 card->cardnum);
1311 goto err_pci_request_regions;
1312 }
1313
1314 pci_set_master(pci_dev);
1315
1316 if (!pci_dev->irq) {
1317 printk(KERN_CRIT hfc_DRIVER_PREFIX
1318 "card %d: "
1319 "no irq!\n",
1320 card->cardnum);
1321 err = -ENODEV;
1322 goto err_noirq;
1323 }
1324
1325 card->io_bus_mem = pci_resource_start(pci_dev, 1);
1326 if (!card->io_bus_mem) {
1327 printk(KERN_CRIT hfc_DRIVER_PREFIX
1328 "card %d: "
1329 "no iomem!\n",
1330 card->cardnum);
1331 err = -ENODEV;
1332 goto err_noiobase;
1333 }
1334
1335 card->io_mem = ioremap(card->io_bus_mem, hfc_PCI_MEM_SIZE);
1336 if (!(card->io_mem)) {
1337 printk(KERN_CRIT hfc_DRIVER_PREFIX
1338 "card %d: "
1339 "cannot ioremap I/O memory\n",
1340 card->cardnum);
1341 err = -ENODEV;
1342 goto err_ioremap;
1343 }
1344
1345 /*
1346 * pci_alloc_consistent guarantees alignment
1347 * (Documentation/DMA-mapping.txt)
1348 */
1349 card->fifo_mem = pci_alloc_consistent(pci_dev,
1350 hfc_FIFO_SIZE, &card->fifo_bus_mem);
1351 if (!card->fifo_mem) {
1352 printk(KERN_CRIT hfc_DRIVER_PREFIX
1353 "card %d: "
1354 "unable to allocate FIFO DMA memory!\n",
1355 card->cardnum);
1356 err = -ENOMEM;
1357 goto err_alloc_fifo;
1358 }
1359
1360 memset(card->fifo_mem, 0x00, hfc_FIFO_SIZE);
1361
1362 card->fifos = card->fifo_mem;
1363
1364 pci_write_config_dword(card->pcidev, hfc_PCI_MWBA, card->fifo_bus_mem);
1365
1366 err = request_irq(card->pcidev->irq, &hfc_interrupt,
1367
1368 #if (KERNEL_VERSION(2, 6, 23) < LINUX_VERSION_CODE)
1369 IRQF_SHARED, hfc_DRIVER_NAME, card);
1370 #else
1371 SA_SHIRQ, hfc_DRIVER_NAME, card);
1372 #endif
1373
1374 if (err) {
1375 printk(KERN_CRIT hfc_DRIVER_PREFIX
1376 "card %d: "
1377 "unable to register irq\n",
1378 card->cardnum);
1379 goto err_request_irq;
1380 }
1381
1382 card->nt_mode = FALSE;
1383
1384 if (modes & (1 << card->cardnum))
1385 card->nt_mode = TRUE;
1386
1387 for (i = 0; i < nt_modes_count; i++) {
1388 if (nt_modes[i] == card->cardnum)
1389 card->nt_mode = TRUE;
1390 }
1391
1392 /*
1393 * D Channel
1394 */
1395 card->chans[D].card = card;
1396 card->chans[D].name = "D";
1397 card->chans[D].status = free;
1398 card->chans[D].number = D;
1399 spin_lock_init(&card->chans[D].lock);
1400
1401 card->chans[D].rx.chan = &card->chans[D];
1402 card->chans[D].rx.fifo_base = card->fifos + 0x4000;
1403 card->chans[D].rx.z_base = card->fifos + 0x4000;
1404 card->chans[D].rx.z1_base = card->fifos + 0x6080;
1405 card->chans[D].rx.z2_base = card->fifos + 0x6082;
1406 card->chans[D].rx.z_min = 0x0000;
1407 card->chans[D].rx.z_max = 0x01FF;
1408 card->chans[D].rx.f_min = 0x10;
1409 card->chans[D].rx.f_max = 0x1F;
1410 card->chans[D].rx.f1 = card->fifos + 0x60a0;
1411 card->chans[D].rx.f2 = card->fifos + 0x60a1;
1412 card->chans[D].rx.fifo_size = card->chans[D].rx.z_max
1413 - card->chans[D].rx.z_min + 1;
1414 card->chans[D].rx.f_num = card->chans[D].rx.f_max
1415 - card->chans[D].rx.f_min + 1;
1416
1417 card->chans[D].tx.chan = &card->chans[D];
1418 card->chans[D].tx.fifo_base = card->fifos + 0x0000;
1419 card->chans[D].tx.z_base = card->fifos + 0x0000;
1420 card->chans[D].tx.z1_base = card->fifos + 0x2080;
1421 card->chans[D].tx.z2_base = card->fifos + 0x2082;
1422 card->chans[D].tx.z_min = 0x0000;
1423 card->chans[D].tx.z_max = 0x01FF;
1424 card->chans[D].tx.f_min = 0x10;
1425 card->chans[D].tx.f_max = 0x1F;
1426 card->chans[D].tx.f1 = card->fifos + 0x20a0;
1427 card->chans[D].tx.f2 = card->fifos + 0x20a1;
1428 card->chans[D].tx.fifo_size = card->chans[D].tx.z_max -
1429 card->chans[D].tx.z_min + 1;
1430 card->chans[D].tx.f_num = card->chans[D].tx.f_max -
1431 card->chans[D].tx.f_min + 1;
1432
1433 /*
1434 * B1 Channel
1435 */
1436 card->chans[B1].card = card;
1437 card->chans[B1].name = "B1";
1438 card->chans[B1].status = free;
1439 card->chans[B1].number = B1;
1440 card->chans[B1].protocol = 0;
1441 spin_lock_init(&card->chans[B1].lock);
1442
1443 card->chans[B1].rx.chan = &card->chans[B1];
1444 card->chans[B1].rx.fifo_base = card->fifos + 0x4200;
1445 card->chans[B1].rx.z_base = card->fifos + 0x4000;
1446 card->chans[B1].rx.z1_base = card->fifos + 0x6000;
1447 card->chans[B1].rx.z2_base = card->fifos + 0x6002;
1448 card->chans[B1].rx.z_min = 0x0200;
1449 card->chans[B1].rx.z_max = 0x1FFF;
1450 card->chans[B1].rx.f_min = 0x00;
1451 card->chans[B1].rx.f_max = 0x1F;
1452 card->chans[B1].rx.f1 = card->fifos + 0x6080;
1453 card->chans[B1].rx.f2 = card->fifos + 0x6081;
1454 card->chans[B1].rx.fifo_size = card->chans[B1].rx.z_max -
1455 card->chans[B1].rx.z_min + 1;
1456 card->chans[B1].rx.f_num = card->chans[B1].rx.f_max -
1457 card->chans[B1].rx.f_min + 1;
1458
1459 card->chans[B1].tx.chan = &card->chans[B1];
1460 card->chans[B1].tx.fifo_base = card->fifos + 0x0200;
1461 card->chans[B1].tx.z_base = card->fifos + 0x0000;
1462 card->chans[B1].tx.z1_base = card->fifos + 0x2000;
1463 card->chans[B1].tx.z2_base = card->fifos + 0x2002;
1464 card->chans[B1].tx.z_min = 0x0200;
1465 card->chans[B1].tx.z_max = 0x1FFF;
1466 card->chans[B1].tx.f_min = 0x00;
1467 card->chans[B1].tx.f_max = 0x1F;
1468 card->chans[B1].tx.f1 = card->fifos + 0x2080;
1469 card->chans[B1].tx.f2 = card->fifos + 0x2081;
1470 card->chans[B1].tx.fifo_size = card->chans[B1].tx.z_max -
1471 card->chans[B1].tx.z_min + 1;
1472 card->chans[B1].tx.f_num = card->chans[B1].tx.f_max -
1473 card->chans[B1].tx.f_min + 1;
1474
1475 /*
1476 * B2 Channel
1477 */
1478 card->chans[B2].card = card;
1479 card->chans[B2].name = "B2";
1480 card->chans[B2].status = free;
1481 card->chans[B2].number = B2;
1482 card->chans[B2].protocol = 0;
1483 spin_lock_init(&card->chans[B2].lock);
1484
1485 card->chans[B2].rx.chan = &card->chans[B2];
1486 card->chans[B2].rx.fifo_base = card->fifos + 0x6200,
1487 card->chans[B2].rx.z_base = card->fifos + 0x6000;
1488 card->chans[B2].rx.z1_base = card->fifos + 0x6100;
1489 card->chans[B2].rx.z2_base = card->fifos + 0x6102;
1490 card->chans[B2].rx.z_min = 0x0200;
1491 card->chans[B2].rx.z_max = 0x1FFF;
1492 card->chans[B2].rx.f_min = 0x00;
1493 card->chans[B2].rx.f_max = 0x1F;
1494 card->chans[B2].rx.f1 = card->fifos + 0x6180;
1495 card->chans[B2].rx.f2 = card->fifos + 0x6181;
1496 card->chans[B2].rx.fifo_size = card->chans[B2].rx.z_max -
1497 card->chans[B2].rx.z_min + 1;
1498 card->chans[B2].rx.f_num = card->chans[B2].rx.f_max -
1499 card->chans[B2].rx.f_min + 1;
1500
1501 card->chans[B2].tx.chan = &card->chans[B2];
1502 card->chans[B2].tx.fifo_base = card->fifos + 0x2200;
1503 card->chans[B2].tx.z_base = card->fifos + 0x2000;
1504 card->chans[B2].tx.z1_base = card->fifos + 0x2100;
1505 card->chans[B2].tx.z2_base = card->fifos + 0x2102;
1506 card->chans[B2].tx.z_min = 0x0200;
1507 card->chans[B2].tx.z_max = 0x1FFF;
1508 card->chans[B2].tx.f_min = 0x00;
1509 card->chans[B2].tx.f_max = 0x1F;
1510 card->chans[B2].tx.f1 = card->fifos + 0x2180;
1511 card->chans[B2].tx.f2 = card->fifos + 0x2181;
1512 card->chans[B2].tx.fifo_size = card->chans[B2].tx.z_max -
1513 card->chans[B2].tx.z_min + 1;
1514 card->chans[B2].tx.f_num = card->chans[B2].tx.f_max -
1515 card->chans[B2].tx.f_min + 1;
1516
1517 /*
1518 * All done
1519 */
1520
1521 zthfc = kmalloc(sizeof(struct dahdi_hfc), GFP_KERNEL);
1522 if (!zthfc) {
1523 printk(KERN_CRIT hfc_DRIVER_PREFIX
1524 "unable to kmalloc!\n");
1525 goto err_request_irq;
1526 }
1527 memset(zthfc, 0x0, sizeof(struct dahdi_hfc));
1528
1529 zthfc->card = card;
1530 hfc_zap_initialize(zthfc);
1531 card->ztdev = zthfc;
1532
1533 hfc_resetCard(card);
1534
1535 printk(KERN_INFO hfc_DRIVER_PREFIX
1536 "card %d configured for %s mode at mem %#lx (0x%p) IRQ %u\n",
1537 card->cardnum,
1538 card->nt_mode ? "NT" : "TE",
1539 card->io_bus_mem,
1540 card->io_mem,
1541 card->pcidev->irq);
1542
1543 cardnum++;
1544
1545 return 0;
1546
1547 err_request_irq:
1548 pci_free_consistent(pci_dev, hfc_FIFO_SIZE,
1549 card->fifo_mem, card->fifo_bus_mem);
1550 err_alloc_fifo:
1551 iounmap(card->io_mem);
1552 err_ioremap:
1553 err_noiobase:
1554 err_noirq:
1555 pci_release_regions(pci_dev);
1556 err_pci_request_regions:
1557 err_pci_set_dma_mask:
1558 err_pci_enable_device:
1559 kfree(card);
1560 err_alloc_hfccard:
1561 return err;
1562 }
1563
1564 static void __devexit hfc_remove(struct pci_dev *pci_dev)
1565 {
1566 struct hfc_card *card = pci_get_drvdata(pci_dev);
1567
1568
1569 printk(KERN_INFO hfc_DRIVER_PREFIX
1570 "card %d: "
1571 "shutting down card at %p.\n",
1572 card->cardnum,
1573 card->io_mem);
1574
1575 hfc_softreset(card);
1576
1577 dahdi_unregister_device(card->ztdev->ddev);
1578 hfc_dahdi_free(card->ztdev);
1579
1580
1581 /*
1582 * disable memio and busmaster
1583 */
1584 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
1585
1586 free_irq(pci_dev->irq, card);
1587
1588 pci_free_consistent(pci_dev, hfc_FIFO_SIZE,
1589 card->fifo_mem, card->fifo_bus_mem);
1590
1591 iounmap(card->io_mem);
1592
1593 pci_release_regions(pci_dev);
1594
1595 pci_disable_device(pci_dev);
1596
1597 kfree(card);
1598 }
1599
1600 /******************************************
1601 * Module stuff
1602 ******************************************/
1603
1604 static int __init hfc_init_module(void)
1605 {
1606 int ret;
1607
1608 printk(KERN_INFO hfc_DRIVER_PREFIX
1609 hfc_DRIVER_STRING " loading\n");
1610
1611 ret = pci_register_driver(&hfc_driver);
1612 return ret;
1613 }
1614
1615 module_init(hfc_init_module);
1616
1617 static void __exit hfc_module_exit(void)
1618 {
1619 pci_unregister_driver(&hfc_driver);
1620 printk(KERN_INFO hfc_DRIVER_PREFIX
1621 hfc_DRIVER_STRING " unloaded\n");
1622 }
1623
1624 module_exit(hfc_module_exit);
1625
1626 #endif
1627
1628 MODULE_DESCRIPTION(hfc_DRIVER_DESCR);
1629 MODULE_AUTHOR("Jens Wilke <[email protected]>, "
1630 "Daniele (Vihai) Orlandi <[email protected]>, "
1631 "Jose A. Deniz <[email protected]>");
1632 MODULE_ALIAS("vzaphfc");
1633 #ifdef MODULE_LICENSE
1634 MODULE_LICENSE("GPL");
1635 #endif
1636
1637
1638 module_param(modes, int, 0444);
1639
1640 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
1641 module_param_array(nt_modes, int, &nt_modes_count, 0444);
1642 #else
1643 module_param_array(nt_modes, int, nt_modes_count, 0444);
1644 #endif
1645
1646 module_param(force_l1_up, int, 0444);
1647 #ifdef DEBUG
1648 module_param(debug_level, int, 0444);
1649 #endif
1650
1651 MODULE_PARM_DESC(modes, "[Deprecated] bit-mask to configure NT mode");
1652 MODULE_PARM_DESC(nt_modes,
1653 "Comma-separated list of card IDs to configure in NT mode");
1654 MODULE_PARM_DESC(force_l1_up, "Don't allow L1 to go down");
1655 #ifdef DEBUG
1656 MODULE_PARM_DESC(debug_level, "Debug verbosity level");
1657 #endif