~ubuntu-branches/ubuntu/quantal/zaptel/quantal

« back to all changes in this revision

Viewing changes to kernel/qozap/qozap.c

  • Committer: Bazaar Package Importer
  • Author(s): Tzafrir Cohen
  • Date: 2008-08-28 22:58:23 UTC
  • mfrom: (11.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080828225823-r8bdunirm8hmc76m
Tags: 1:1.4.11~dfsg-2
* Patch xpp_fxs_power: Fixed an issue with hook detection of the Astribank
  FXS module.
* Don't fail init.d script if fxotune fails. This may happen if running it
  when Asterisk is already running.
* Bump standards version to 3.8.0.0 .
* Ignore false lintian warning ("m-a a-i" has "a a").
* Patch xpp_fxo_cid_always: do always pass PCM if that's what the user
  asked.
* Patch vzaphfc_proc_root_dir: fix vzaphfc on 2.6.26.
* Patch wcte12xp_flags: Proper time for irq save flags.
* Patch headers_2627: Fix location of semaphore.h for 2.6.27 .
* Patch xpp_fxs_dtmf_leak: Don't play DTMFs to the wrong channel.
* Patch wctdm_fix_alarm: Fix sending channel alarms.
* Patch device_class_2626: Fix building 2.6.26 (Closes: #493397).
* Using dh_lintian for lintian overrides, hence requiring debhelper 6.0.7.
* Lintian: we know we have direct changes. Too bad we're half-upstream :-(
* Fix doc-base section names. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * qozap.c - Zaptel driver for the quadBRI PCI ISDN card
 
3
 * and the octoBRI PCI ISDN card!
 
4
 *
 
5
 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Junghanns.NET GmbH
 
6
 *
 
7
 * Klaus-Peter Junghanns <kpj@junghanns.net>
 
8
 *
 
9
 * This program is free software and may be modified and
 
10
 * distributed under the terms of the GNU Public License.
 
11
 *
 
12
 */
 
13
#include <linux/kernel.h>
 
14
#include <linux/module.h>
 
15
#include <linux/pci.h>
 
16
#include <linux/init.h>
 
17
#include <linux/interrupt.h>
 
18
#include <zaptel.h>
 
19
#include "qozap.h"
 
20
 
 
21
#ifdef LINUX26
 
22
#include <linux/moduleparam.h>
 
23
#endif
 
24
 
 
25
#if CONFIG_PCI
 
26
 
 
27
static int doubleclock=0;
 
28
static int ports=-1; /* autodetect */
 
29
static int pcmslave=0;
 
30
static int bloop=0;
 
31
static int debug=0;
 
32
static int dacs=1; /* 0 = no dacs, 1 = oncard dacs */
 
33
static struct qoz_card *qoz_dev_list = NULL;
 
34
static int qoz_dev_count = 0;
 
35
static int totalBRIs = 0;
 
36
static struct pci_dev *multi_qoz = NULL;
 
37
static spinlock_t registerlock = SPIN_LOCK_UNLOCKED;
 
38
static int sort=1;
 
39
 
 
40
static int ztqoz_shutdown(struct zt_span *span);
 
41
 
 
42
int qoz_waitbusy(struct qoz_card *qoztmp) {
 
43
    int x=1000;
 
44
    while (x-- && (qoz_inb(qoztmp,qoz_R_STATUS) & 1));
 
45
    if (x < 0) {
 
46
        return -1;
 
47
    } else {
 
48
        return 0;
 
49
    }
 
50
}
 
51
 
 
52
void qoz_shutdownCard(struct qoz_card *qoztmp) {
 
53
    int s=0;
 
54
    unsigned long flags;
 
55
    
 
56
    int stports=0;
 
57
    if (qoztmp == NULL) {
 
58
        printk(KERN_INFO "qozap: shutting down NULL card!\n");
 
59
        return;
 
60
    }
 
61
 
 
62
    if ((qoztmp->pci_io == NULL) || (qoztmp->ioport == 0)) {
 
63
        return;
 
64
    }
 
65
 
 
66
    if (debug)
 
67
        printk(KERN_INFO "qozap: shutting down card %d (cardID %d) at io port %#x.\n",qoztmp->cardno,qoztmp->cardID,(u_int) qoztmp->ioport);
 
68
 
 
69
 
 
70
    spin_lock_irqsave(&qoztmp->lock,flags);
 
71
 
 
72
    // turn off irqs
 
73
    qoz_outb(qoztmp,qoz_R_IRQMSK_MISC, 0); 
 
74
    qoz_outb(qoztmp,qoz_R_SCI_MSK, 0); 
 
75
    qoz_outb(qoztmp,qoz_R_IRQ_CTRL, 0); 
 
76
 
 
77
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
78
 
 
79
    stports = qoztmp->stports;
 
80
    for (s=0; s < stports; s++) {
 
81
        if(qoztmp->spans[s].flags & ZT_FLAG_REGISTERED) {
 
82
            zt_unregister(&(qoztmp->spans[s]));
 
83
            if (debug > 2)
 
84
                printk(KERN_INFO "qozap: unregistered card %d span %d.\n",qoztmp->cardno,s+1);
 
85
        }
 
86
    }
 
87
 
 
88
 
 
89
    release_region(qoztmp->ioport, 8);
 
90
    iounmap((void *) qoztmp->pci_io);
 
91
    release_mem_region((unsigned long) qoztmp->pci_io_phys, 256);
 
92
 
 
93
    spin_lock_irqsave(&qoztmp->lock,flags);
 
94
 
 
95
    qoztmp->pci_io = NULL;
 
96
    qoztmp->ioport = 0;
 
97
 
 
98
    if (qoztmp->pcidev != NULL) {
 
99
        pci_disable_device(qoztmp->pcidev);
 
100
    }
 
101
    pci_write_config_word(qoztmp->pcidev, PCI_COMMAND, 0);      
 
102
    
 
103
    free_irq(qoztmp->irq,qoztmp);
 
104
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
105
 
 
106
    
 
107
}
 
108
 
 
109
void qoz_doLEDs(struct qoz_card *qoztmp) {
 
110
    unsigned char leds = 0x0;
 
111
    unsigned long ledw;
 
112
    unsigned long flags;
 
113
    spin_lock_irqsave(&qoztmp->lock,flags);
 
114
 
 
115
    if ((qoztmp->type == 0xb520) && (qoztmp->stports == 4)){
 
116
            qoz_outb(qoztmp,qoz_R_GPIO_SEL,0x20 | 0x10);
 
117
            qoz_outb(qoztmp,qoz_R_GPIO_EN1,0xf);
 
118
            qoz_outb(qoztmp,qoz_R_GPIO_OUT1,(qoztmp->leds[0] | (qoztmp->leds[1] << 1) | (qoztmp->leds[2] << 2) | (qoztmp->leds[3] << 3)));
 
119
    } else if (((qoztmp->type == 0xb550)  || (qoztmp->type == 0xb556) || (qoztmp->type == 0xb752)) && (qoztmp->stports == 4)){
 
120
            qoz_outb(qoztmp,qoz_R_GPIO_SEL,0x80 | 0x40 | 0x20 | 0x10);
 
121
            qoz_outb(qoztmp,qoz_R_GPIO_EN1,0xff);
 
122
            leds = 0x10;
 
123
            if (qoztmp->leds[0] == 1) {
 
124
                leds |= 0x04;
 
125
            }
 
126
            if (qoztmp->leds[1] == 1) {
 
127
                leds |= 0x20;
 
128
            }
 
129
            if (qoztmp->leds[2] == 1) {
 
130
                leds |= 0x80;
 
131
            }
 
132
            if (qoztmp->leds[3] == 1) {
 
133
                leds |= 0x40;
 
134
            }
 
135
 
 
136
    /*
 
137
             03
 
138
             12
 
139
        0x80 rr
 
140
             rg
 
141
        0x40 rg
 
142
             rr
 
143
        0x20 rr
 
144
             gr
 
145
        0x04 gr
 
146
             rr
 
147
    */
 
148
            qoz_outb(qoztmp,qoz_R_GPIO_OUT1, leds);
 
149
 
 
150
    } else if (((qoztmp->type == 0xb556) || (qoztmp->type == 0xb751)) && (qoztmp->stports == 2)){
 
151
            qoz_outb(qoztmp,qoz_R_GPIO_SEL,0x80 | 0x40 | 0x20 | 0x10);
 
152
            qoz_outb(qoztmp,qoz_R_GPIO_EN1,0xff);
 
153
            leds = 0x0;
 
154
            if (qoztmp->leds[0] == 1) {
 
155
                leds |= 0x04;
 
156
            }
 
157
            if (qoztmp->leds[1] == 1) {
 
158
                leds |= 0x08;
 
159
            }
 
160
            qoz_outb(qoztmp,qoz_R_GPIO_OUT1, leds);
 
161
 
 
162
    } else if (qoztmp->type == 0xb558) {
 
163
            qoz_outb(qoztmp,qoz_R_GPIO_SEL,0x80 | 0x40 | 0x20);
 
164
            qoz_outb(qoztmp,qoz_R_GPIO_EN1,0x80 | 0x40 | 0x20 | 0x4);
 
165
            if (qoztmp->leds[0] == 1) {
 
166
                leds |= 0x04;
 
167
            }
 
168
            if (qoztmp->leds[1] == 1) {
 
169
                leds |= 0x20;
 
170
            }
 
171
            if (qoztmp->leds[2] == 1) {
 
172
                leds |= 0x40;
 
173
            }
 
174
            if (qoztmp->leds[3] == 1) {
 
175
                leds |= 0x80;
 
176
            }
 
177
            
 
178
            qoz_outb(qoztmp,qoz_R_GPIO_OUT1, leds);
 
179
        /*
 
180
            gpio_10 0
 
181
            gpio_13 1
 
182
            gpio_15 2
 
183
            gpio_14 3
 
184
        */          
 
185
    } else if (qoztmp->type == 0xb55b) {
 
186
        qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x21); 
 
187
    /* 0x01 g1 st1
 
188
       0x02 g2 st2
 
189
       0x04 g3 st3
 
190
       0x08 g4 st4
 
191
       0x10 r8 st5
 
192
       0x20 r7 st6
 
193
       0x40 r6 st7
 
194
       0x80 r5 st8
 
195
      */
 
196
        if (qoztmp->leds[0] == 0) {
 
197
                leds |= 0x01;
 
198
        }
 
199
        if (qoztmp->leds[1] == 0) {
 
200
                leds |= 0x02;
 
201
        }
 
202
        if (qoztmp->leds[2] == 0) {
 
203
                leds |= 0x04;
 
204
        }
 
205
        if (qoztmp->leds[3] == 0) {
 
206
                leds |= 0x08;
 
207
        }
 
208
        if (qoztmp->leds[4] == 0) {
 
209
                leds |= 0x10;
 
210
        }
 
211
        if (qoztmp->leds[5] == 0) {
 
212
                leds |= 0x20;
 
213
        }
 
214
        if (qoztmp->leds[6] == 0) {
 
215
                leds |= 0x40;
 
216
        }
 
217
        if (qoztmp->leds[7] == 0) {
 
218
                leds |= 0x80;
 
219
        }
 
220
 
 
221
        ledw = leds << 24 | leds << 16 | leds << 8 | leds;
 
222
        
 
223
        qoz_outdw_io(qoztmp,0x4000, ledw);
 
224
 
 
225
        qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
226
 
 
227
    }
 
228
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
229
}
 
230
 
 
231
void qoz_doWD(struct qoz_card *qoztmp) {
 
232
    unsigned long flags;
 
233
    if (!qoztmp->wdp) {
 
234
        return;
 
235
    }
 
236
    spin_lock_irqsave(&qoztmp->lock,flags);
 
237
    qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x21); 
 
238
        if (qoztmp->wdp == 1) {
 
239
            qoz_outdw_io(qoztmp,0x4000, qoz_WD_P2);
 
240
            qoztmp->wdp = 2;
 
241
        } else {
 
242
            qoz_outdw_io(qoztmp,0x4000, qoz_WD_P1);
 
243
            qoztmp->wdp = 1;
 
244
        }
 
245
        qoz_inb_io(qoztmp,qoz_R_CHIP_ID);    
 
246
    qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
247
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
248
}
 
249
 
 
250
void qoz_undoWD(struct qoz_card *qoztmp) {
 
251
    unsigned long flags;
 
252
 
 
253
    if (qoztmp->wdp) {
 
254
        printk(KERN_INFO "qozap: Stopping hardware watchdog.\n");
 
255
        spin_lock_irqsave(&qoztmp->lock,flags);
 
256
        qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x21); 
 
257
        qoz_outdw_io(qoztmp,0x4000, qoz_WD_P0);
 
258
        qoztmp->wdp = 0;
 
259
        qoz_inb_io(qoztmp,qoz_R_CHIP_ID);    
 
260
        qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
261
        spin_unlock_irqrestore(&qoztmp->lock,flags);
 
262
    }
 
263
}
 
264
 
 
265
void qoz_reset_pcm(struct qoz_card *qoztmp) {
 
266
    qoz_outb(qoztmp,qoz_R_CIRM,0x20);
 
267
    qoz_outb(qoztmp,qoz_R_CIRM,0x0); 
 
268
    qoz_waitbusy(qoztmp);
 
269
 
 
270
    if (pcmslave) {
 
271
        qoz_outb(qoztmp,qoz_R_PCM_MD0, 0x90);
 
272
        qoz_outb(qoztmp,qoz_R_PCM_MD1, 0x20);
 
273
        qoz_outb(qoztmp,qoz_R_PCM_MD0, 0xA0);
 
274
        qoz_outb(qoztmp,qoz_R_PCM_MD2, 0x4 | 0x8);
 
275
    } else {
 
276
        qoz_outb(qoztmp,qoz_R_PCM_MD0, 0x91);
 
277
        qoz_outb(qoztmp,qoz_R_PCM_MD1, 0x20);
 
278
    }
 
279
}
 
280
 
 
281
void qoz_resetCard(struct qoz_card *qoztmp) {
 
282
    unsigned long flags;
 
283
    spin_lock_irqsave(&(qoztmp->lock),flags);
 
284
    pci_write_config_word(qoztmp->pcidev, PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
 
285
    
 
286
    // soft reset
 
287
    qoz_outb(qoztmp,qoz_R_CIRM,0x8);
 
288
    qoz_outb(qoztmp,qoz_R_CIRM,0x0); 
 
289
    qoz_waitbusy(qoztmp);
 
290
 
 
291
    qoz_reset_pcm(qoztmp);
 
292
 
 
293
    // fifo reset
 
294
    qoz_outb(qoztmp,qoz_R_CIRM,0x10);
 
295
    qoz_outb(qoztmp,qoz_R_CIRM,0x0); 
 
296
    qoz_waitbusy(qoztmp);
 
297
 
 
298
    // s/t reset
 
299
    qoz_outb(qoztmp,qoz_R_CIRM,0x40);
 
300
    qoz_outb(qoztmp,qoz_R_CIRM,0x0); 
 
301
    qoz_waitbusy(qoztmp);
 
302
 
 
303
    /* set S0 amplitude */
 
304
    qoz_outb(qoztmp,qoz_R_PWM_MD,0xa0);
 
305
    if (qoztmp->type == 0xb552) {
 
306
        qoz_outb(qoztmp,qoz_R_PWM0,0x19);
 
307
    } else if (qoztmp->type == 0xb55b) {
 
308
        qoz_outb(qoztmp,qoz_R_PWM0,0x19);
 
309
    } else {
 
310
        qoz_outb(qoztmp,qoz_R_PWM0,0x1E);
 
311
    }
 
312
 
 
313
    /* set up the timer */
 
314
    qoz_outb(qoztmp,qoz_R_TI_WD, 0x2); 
 
315
    qoz_outb(qoztmp,qoz_R_IRQMSK_MISC, 0x2); 
 
316
 
 
317
 
 
318
    /* all state changes */
 
319
    qoz_outb(qoztmp,qoz_R_SCI_MSK, 0xff); 
 
320
 
 
321
    if (qoztmp->type == 0xb552) {
 
322
        qoz_outb(qoztmp,qoz_R_FIFO_MD,0x16);
 
323
    } else if (qoztmp->type == 0xb55b) {
 
324
        qoz_outb(qoztmp,qoz_R_FIFO_MD,0x16);
 
325
    } else {
 
326
        qoz_outb(qoztmp,qoz_R_FIFO_MD,0x26);
 
327
    }
 
328
 
 
329
    // double clock
 
330
    if (doubleclock == 1) {
 
331
        // hopefully you have set CLK_MODE correctly!
 
332
        qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
333
    } else {
 
334
        if (qoztmp->type == 0x08b4) {
 
335
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x0); 
 
336
        } else if (qoztmp->type == 0xb550) {
 
337
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
338
        } else if (qoztmp->type == 0xb556) {
 
339
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
340
        } else if (qoztmp->type == 0xb559) {
 
341
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
342
        } else if (qoztmp->type == 0xb558) {
 
343
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
344
        } else if (qoztmp->type == 0xb520) {
 
345
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
346
        } else if (qoztmp->type == 0xb752) {
 
347
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
348
        } else if (qoztmp->type == 0xb751) {
 
349
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
350
        } else {
 
351
            /* you are on your own here! */
 
352
            qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
353
        }
 
354
    }
 
355
    qoz_outb(qoztmp,qoz_R_CTRL,0x0); 
 
356
 
 
357
    /* R0 G1 */
 
358
    qoztmp->leds[0] = 0x0;
 
359
    qoztmp->leds[1] = 0x0;
 
360
    qoztmp->leds[2] = 0x0;
 
361
    qoztmp->leds[3] = 0x0;
 
362
    qoztmp->leds[4] = 0x0;
 
363
    qoztmp->leds[5] = 0x0;
 
364
    qoztmp->leds[6] = 0x0;
 
365
    qoztmp->leds[7] = 0x0;
 
366
 
 
367
    if (qoztmp->type == 0xb552) {
 
368
        qoztmp->stports = 8;
 
369
    } else if (qoztmp->type == 0xb55b) {
 
370
        qoztmp->stports = 8;
 
371
    } else if (qoztmp->type == 0xb556) {
 
372
        qoztmp->stports = 2;
 
373
    } else if (qoztmp->type == 0xb559) {
 
374
        qoztmp->stports = 2;
 
375
    } else if (qoztmp->type == 0xb751) {
 
376
        qoztmp->stports = 2;
 
377
    } else {
 
378
        qoztmp->stports = 4;
 
379
    }
 
380
    qoztmp->ticks = 0;
 
381
    qoztmp->clicks = 0;
 
382
    if ((qoztmp->type == 0xb550) || (qoztmp->type == 0xb556)) {
 
383
        printk(KERN_INFO "qozap: Starting hardware watchdog.\n");
 
384
        qoztmp->wdp = 2;
 
385
    } else {
 
386
        qoztmp->wdp = 0;
 
387
    }
 
388
 
 
389
    qoz_outb(qoztmp,qoz_R_ST_SYNC,0x0);
 
390
 
 
391
    /* IRQs off */
 
392
    qoz_outb(qoztmp,qoz_R_IRQ_CTRL, 0); 
 
393
    
 
394
    spin_unlock_irqrestore(&(qoztmp->lock),flags);
 
395
}
 
396
 
 
397
void qoz_registerCard(struct qoz_card *qozcard) {
 
398
    struct qoz_card *tmpcard = NULL;
 
399
    spin_lock(&registerlock);
 
400
 
 
401
    if (qozcard != NULL) {
 
402
        if (!sort) {
 
403
            tmpcard = qoz_dev_list;
 
404
            if (tmpcard) {
 
405
                while (tmpcard->next) {
 
406
                    tmpcard = tmpcard->next;
 
407
                }
 
408
                tmpcard->next = qozcard;
 
409
                qozcard->prev = tmpcard;
 
410
            } else {
 
411
                qoz_dev_list = qozcard;
 
412
            }
 
413
        } else {
 
414
            qozcard->prev = NULL;
 
415
            qozcard->next = qoz_dev_list;
 
416
            if (qoz_dev_list) {
 
417
                qoz_dev_list->prev = qozcard;
 
418
            }
 
419
            qoz_dev_list = qozcard;
 
420
        }
 
421
        qozcard->cardno = ++qoz_dev_count;
 
422
    } else {
 
423
        printk(KERN_INFO "qozap: trying to register NULL card.\n");
 
424
    }
 
425
    spin_unlock(&registerlock);
 
426
}
 
427
 
 
428
static int qoz_dfifo_tx(struct qoz_card *qoztmp, int stport) {
 
429
    int chan = 2;
 
430
    int x=0;
 
431
    char fifo = 0;
 
432
    char offset = 0;
 
433
    unsigned long flags;
 
434
 
 
435
 
 
436
    if (qoztmp->type == 0xb552) {
 
437
        offset = 24;
 
438
    } else if (qoztmp->type == 0xb55b) {
 
439
        offset = 24;
 
440
    } else {
 
441
        offset = 28;
 
442
    }
 
443
 
 
444
    fifo = stport + offset;
 
445
 
 
446
    if (qoztmp->chans[stport][chan].bytes2transmit < 1) {
 
447
        return 0;
 
448
    } else {
 
449
        spin_lock_irqsave(&qoztmp->lock,flags);
 
450
        /* select fifo */
 
451
        qoz_outb(qoztmp,qoz_R_FIFO,fifo << 1);    
 
452
        qoz_waitbusy(qoztmp);
 
453
    
 
454
        if (debug > 1)
 
455
            printk(KERN_INFO "qozap: card %d stport %d TX [ ", qoztmp->cardno, stport + 1);
 
456
        /* copy frame to fifo */
 
457
        for (x=0;x<qoztmp->chans[stport][chan].bytes2transmit;x++) {
 
458
            if (debug > 1)
 
459
                printk("%#x ",qoztmp->dtxbuf[stport][x]);
 
460
            qoz_outb(qoztmp,qoz_A_FIFO_DATA0,qoztmp->dtxbuf[stport][x]);
 
461
        }
 
462
        if (debug > 1)
 
463
            printk("] %d bytes\n",qoztmp->chans[stport][chan].bytes2transmit);
 
464
 
 
465
        if (qoztmp->chans[stport][chan].eoftx == 1) {
 
466
            /* transmit HDLC frame */
 
467
            qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x1);    
 
468
            qoz_waitbusy(qoztmp);
 
469
        }
 
470
        spin_unlock_irqrestore(&qoztmp->lock,flags);
 
471
    }
 
472
    return 0;
 
473
}
 
474
 
 
475
static int qoz_fifo_tx(struct qoz_card *qoztmp, char fifo) {
 
476
    int stport = fifo / 2;
 
477
    int chan = fifo % 2;
 
478
    unsigned long flags;
 
479
 
 
480
    spin_lock_irqsave(&qoztmp->lock,flags);
 
481
    /* select fifo */
 
482
    qoz_outb(qoztmp,qoz_R_FIFO,0x80 | (fifo << 1));    
 
483
    qoz_waitbusy(qoztmp);
 
484
    /* transmit 8 bytes of transparent data */
 
485
    qoz_outdw(qoztmp,qoz_A_FIFO_DATA0,*((unsigned int *) &qoztmp->txbuf[stport][chan][0]));
 
486
    qoz_outdw(qoztmp,qoz_A_FIFO_DATA0,*((unsigned int *) &qoztmp->txbuf[stport][chan][4]));
 
487
            
 
488
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
489
    return 0;
 
490
}
 
491
 
 
492
static int qoz_dfifo_rx(struct qoz_card *qoztmp, int stport) {
 
493
    unsigned char f1=1,f2=1,data,stat;
 
494
    unsigned char of1=0,of2=0;
 
495
    int len,i;
 
496
    unsigned short z1=1,z2=1;
 
497
    unsigned short oz1=0,oz2=0;
 
498
    char fifo = 0;
 
499
    char offset = 0;
 
500
    unsigned long flags;
 
501
 
 
502
    if (qoztmp->type == 0xb552) {
 
503
        offset = 24;
 
504
    } else if (qoztmp->type == 0xb55b) {
 
505
        offset = 24;
 
506
    } else {
 
507
        offset = 28;
 
508
    }
 
509
 
 
510
    fifo = stport + offset;
 
511
 
 
512
    spin_lock_irqsave(&qoztmp->lock,flags);
 
513
    // select rx fifo
 
514
    qoz_outb(qoztmp,qoz_R_FIFO,(fifo << 1) | 1);    
 
515
    qoz_waitbusy(qoztmp);
 
516
 
 
517
    while ((of1 != f1) && (of2 != f2)) {
 
518
        of1 = f1;
 
519
        of2 = f2;
 
520
        f1 = qoz_inb(qoztmp,qoz_A_F1) & 0xf;
 
521
        f2 = qoz_inb(qoztmp,qoz_A_F2) & 0xf;
 
522
    }
 
523
    
 
524
    if (f1 == f2) {
 
525
        /* no frame */
 
526
        qoztmp->st[stport].drx--;
 
527
        qoztmp->chans[stport][2].bytes2receive = 0;
 
528
        spin_unlock_irqrestore(&qoztmp->lock,flags);
 
529
        return 0;
 
530
    }
 
531
    
 
532
    while ((oz1 != z1) && (oz2 != z2)) {
 
533
        oz1 = z1;
 
534
        oz2 = z2;
 
535
        if ((qoztmp->type != 0xb552) && (qoztmp->type != 0xb55b)){
 
536
            z1 = qoz_inw(qoztmp,qoz_A_Z1) & 0x7ff;
 
537
            z2 = qoz_inw(qoztmp,qoz_A_Z2) & 0x7ff;
 
538
        } else {
 
539
            z1 = qoz_inw(qoztmp,qoz_A_Z1) & 0x3ff;
 
540
            z2 = qoz_inw(qoztmp,qoz_A_Z2) & 0x3ff;
 
541
        }
 
542
    }
 
543
    
 
544
    if ((qoztmp->type == 0xb552)|| (qoztmp->type == 0xb55b)) {
 
545
        len = z1 - z2;
 
546
        if (len < 0) {
 
547
            len += qoz_DFIFO_SIZE8;
 
548
        }
 
549
    } else {
 
550
        len = z1 - z2;
 
551
        if (len < 0) {
 
552
            len += qoz_DFIFO_SIZE4;
 
553
        }
 
554
    }
 
555
 
 
556
    if (len > qoz_DFIFO_SIZE4) {
 
557
        printk(KERN_INFO "\nqozap: buffer overflow in D channel RX!\n");
 
558
        qoztmp->chans[stport][2].bytes2receive = 0;
 
559
        qoztmp->chans[stport][2].eofrx = 0;
 
560
    } else {
 
561
        if (debug > 1) printk(KERN_INFO "qozap: card %d span %d RX [ ", qoztmp->cardno, stport + 1);
 
562
        for (i=0; i<len; i++) {
 
563
            data = qoz_inb(qoztmp,qoz_A_FIFO_DATA0);
 
564
            qoztmp->drxbuf[stport][i] = data;
 
565
            if (debug > 1) printk("%#x ",data);
 
566
        }
 
567
        if (debug > 1) printk("] %d bytes\n", len);
 
568
        qoztmp->chans[stport][2].bytes2receive = i;
 
569
        qoztmp->chans[stport][2].eofrx = 1;
 
570
    }
 
571
 
 
572
    stat = qoz_inb(qoztmp,qoz_A_FIFO_DATA0);
 
573
    if (stat != 0x0) {
 
574
        // bad CRC, skip it
 
575
        printk(KERN_INFO "qozap: CRC error for HDLC frame on card %d (cardID %d) S/T port %d\n",qoztmp->cardno, qoztmp->cardID, stport+1);
 
576
        qoztmp->chans[stport][2].bytes2receive = 0;
 
577
        qoztmp->chans[stport][2].eofrx = 0;
 
578
//        zt_qevent_nolock(&qoztmp->chans[stport][2], ZT_EVENT_BADFCS);
 
579
    }
 
580
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x1);    
 
581
    qoz_waitbusy(qoztmp);
 
582
 
 
583
    /* frame recevived */
 
584
    if (qoztmp->st[stport].drx > 0) { 
 
585
        qoztmp->st[stport].drx--;
 
586
    } else {
 
587
        printk(KERN_INFO "qozap: trying to receive too much (card %d span %d drx %d)\n", qoztmp->cardno, stport+1, qoztmp->st[stport].drx);
 
588
        qoztmp->st[stport].drx = 0;
 
589
    }
 
590
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
591
    return 0;
 
592
}
 
593
 
 
594
 
 
595
static int qoz_fifo_rx(struct qoz_card *qoztmp, char fifo) {
 
596
    int stport = fifo / 2;
 
597
    int chan = fifo % 2;
 
598
    unsigned char data;
 
599
    int len,i;
 
600
    unsigned short z1=1,z2=1;
 
601
    unsigned short oz1=0,oz2=0;
 
602
    int mumbojumbo=0;
 
603
    unsigned long flags;
 
604
 
 
605
    spin_lock_irqsave(&qoztmp->lock,flags);
 
606
 
 
607
        /* select rx fifo */
 
608
        qoz_outb(qoztmp,qoz_R_FIFO,0x80 | (fifo << 1) | 1);    
 
609
        qoz_waitbusy(qoztmp);
 
610
    
 
611
        while ((oz1 != z1) && (oz2 != z2)) {
 
612
            oz1 = z1;
 
613
            oz2 = z2;
 
614
            z1 = qoz_inw(qoztmp,qoz_A_Z1) & 0x7f;
 
615
            z2 = qoz_inw(qoztmp,qoz_A_Z2) & 0x7f;
 
616
        }
 
617
        len = z1 - z2;
 
618
        if (len < 0) {
 
619
            len += qoz_FIFO_SIZE;
 
620
        }
 
621
        if (len > 2 * ZT_CHUNKSIZE) {
 
622
            mumbojumbo = len - (2 * ZT_CHUNKSIZE);
 
623
            len = ZT_CHUNKSIZE;
 
624
            for (i=0;i<mumbojumbo;i++) {
 
625
                data = qoz_inb(qoztmp,qoz_A_FIFO_DATA0);
 
626
            }
 
627
            qoztmp->clicks++;
 
628
            if (((qoztmp->clicks > 50) || (debug > 5)) && (mumbojumbo > 0)) {
 
629
                printk(KERN_CRIT "qozap: dropped audio card %d cardid %d bytes %d z1 %d z2 %d fifo %d\n", qoztmp->cardno, qoztmp->cardID, mumbojumbo, z1, z2, fifo);
 
630
                qoztmp->clicks = 0;
 
631
            }
 
632
        }
 
633
 
 
634
 
 
635
        if (len < ZT_CHUNKSIZE) {
 
636
//          printk(KERN_INFO "qozap: not enough to receive (%d bytes)\n",len);
 
637
            spin_unlock_irqrestore(&qoztmp->lock,flags);
 
638
            return 0;
 
639
        } else {
 
640
            if (bloop) {
 
641
                *((unsigned int *) &qoztmp->txbuf[stport][chan][0]) = qoz_indw(qoztmp,qoz_A_FIFO_DATA0);
 
642
                *((unsigned int *) &qoztmp->txbuf[stport][chan][4]) = qoz_indw(qoztmp,qoz_A_FIFO_DATA0);
 
643
            } else {
 
644
                *((unsigned int *) &qoztmp->rxbuf[stport][chan][0]) = qoz_indw(qoztmp,qoz_A_FIFO_DATA0);
 
645
                *((unsigned int *) &qoztmp->rxbuf[stport][chan][4]) = qoz_indw(qoztmp,qoz_A_FIFO_DATA0);
 
646
            }
 
647
        }
 
648
 
 
649
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
650
        if (bloop == 0)
 
651
            zt_ec_chunk(&qoztmp->spans[stport].chans[chan], qoztmp->spans[stport].chans[chan].readchunk, qoztmp->spans[stport].chans[chan].writechunk);
 
652
 
 
653
//    printk(KERN_INFO "s/t port %d, channel %d, dbufi=%d, f1=%d, f2=%d, z1=%d, z2=%d  => len = %d stat=%#x, hdlc=%d\n",stport,chan,qoztmp->st[stport].dbufi,f1,f2,z1,z2,len,stat,hdlc);    
 
654
    return 0;
 
655
}
 
656
 
 
657
static void qoz_assign(struct qoz_card *qoztmp, int src_span, int src_chan, int dst_span, int dst_chan, int timeslot, int use_pcm_bus) {
 
658
    unsigned long flags;
 
659
    int dst_fifo = dst_span * 2 + (dst_chan - 1); 
 
660
    int src_fifo = src_span * 2 + (src_chan - 1); 
 
661
    int src_hfc_chan = src_span * 4 + (src_chan - 1);
 
662
    int dst_hfc_chan = dst_span * 4 + (dst_chan - 1);
 
663
 
 
664
    spin_lock_irqsave(&qoztmp->lock, flags);
 
665
 
 
666
    qoz_outb(qoztmp,qoz_R_FIFO,(src_fifo << 1) | 1); 
 
667
    qoz_waitbusy(qoztmp);
 
668
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
669
    qoz_waitbusy(qoztmp);
 
670
    qoz_outb(qoztmp,qoz_A_CHANNEL,(src_hfc_chan << 1) | 1); 
 
671
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0xDE); // was c2
 
672
 
 
673
    qoz_outb(qoztmp,qoz_R_SLOT,timeslot << 1);
 
674
    qoz_outb(qoztmp,qoz_A_SL_CFG, (src_hfc_chan << 1) | 0 | 0x40);
 
675
 
 
676
    qoz_outb(qoztmp,qoz_R_FIFO, dst_fifo << 1);
 
677
    qoz_waitbusy(qoztmp);
 
678
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
679
    qoz_waitbusy(qoztmp);
 
680
    qoz_outb(qoztmp,qoz_A_CHANNEL,dst_hfc_chan << 1);
 
681
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0xDE); // was c2
 
682
    
 
683
    qoz_outb(qoztmp,qoz_R_SLOT,(timeslot << 1) | 1); 
 
684
    qoz_outb(qoztmp,qoz_A_SL_CFG, (dst_hfc_chan << 1) | 1 | 0x40);
 
685
 
 
686
    spin_unlock_irqrestore(&qoztmp->lock, flags);
 
687
}
 
688
 
 
689
static void qoz_unassign(struct qoz_card *qoztmp, int span, int chan, int timeslot) {
 
690
    unsigned long flags;
 
691
 
 
692
//    int timeslot = span * 2 + (chan - 1);
 
693
    int fifo = span * 2 + (chan - 1);
 
694
    int hfc_chan = span * 4 + (chan - 1);
 
695
 
 
696
    spin_lock_irqsave(&qoztmp->lock, flags);
 
697
 
 
698
    qoz_outb(qoztmp,qoz_R_FIFO,(fifo << 1));
 
699
    qoz_waitbusy(qoztmp);
 
700
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
701
    qoz_waitbusy(qoztmp);
 
702
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x02);
 
703
    qoz_outb(qoztmp,qoz_A_CHANNEL,(hfc_chan << 1)); 
 
704
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
705
 
 
706
    qoz_outb(qoztmp,qoz_R_FIFO,(fifo << 1) | 1);
 
707
    qoz_waitbusy(qoztmp);
 
708
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
709
    qoz_waitbusy(qoztmp);
 
710
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x02);
 
711
    qoz_outb(qoztmp,qoz_A_CHANNEL,(hfc_chan << 1) | 1); 
 
712
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
713
 
 
714
    qoz_outb(qoztmp,qoz_R_SLOT,(timeslot << 1) | 1);
 
715
    qoz_outb(qoztmp,qoz_A_SL_CFG, 0x0);
 
716
 
 
717
    qoz_outb(qoztmp,qoz_R_SLOT,timeslot << 1);
 
718
    qoz_outb(qoztmp,qoz_A_SL_CFG, 0x0);
 
719
 
 
720
    spin_unlock_irqrestore(&qoztmp->lock, flags);
 
721
}
 
722
 
 
723
 
 
724
static int ztqoz_dacs(struct zt_chan *dst, struct zt_chan *src)
 
725
{
 
726
        struct qoz_card *qoztmp = NULL;
 
727
        int use_pcm_bus = 0;
 
728
        int timeslot = 0;
 
729
        if (!dacs) return -1;
 
730
 
 
731
        if (src) {
 
732
            qoztmp = src->pvt;
 
733
 
 
734
            if (src->pvt != dst->pvt) {
 
735
                if (dacs == 2) {
 
736
                    use_pcm_bus = 1;
 
737
                    timeslot = src->channo;
 
738
                    if (debug)
 
739
                        printk("qozap: Assigning channel %d/%d -> %d/%d, timeslot %d, different cards!\n", src->span->offset, src->chanpos, dst->span->offset, dst->chanpos, timeslot);
 
740
                } else {
 
741
                    if (debug)
 
742
                        printk("qozap: Not Assigning %d/%d -> %d/%d, different cards!\n", src->span->offset, src->chanpos, dst->span->offset, dst->chanpos);
 
743
                    return -1;
 
744
                }
 
745
            } else {
 
746
                use_pcm_bus = 0;
 
747
//              timeslot = src->span->offset * 2 + (src->chanpos);
 
748
                timeslot = src->channo;
 
749
                if (debug)
 
750
                    printk("qozap: Assigning channel %d/%d -> %d/%d, timeslot %d, same card!\n", src->span->offset, src->chanpos, dst->span->offset, dst->chanpos, timeslot);
 
751
            }
 
752
 
 
753
            if ((src->chanpos == 3) || (dst->chanpos == 3)) {
 
754
                if (debug)
 
755
                        printk("qozap: Not Assigning D-channel %d/%d -> %d/%d!\n", src->span->offset, src->chanpos, dst->span->offset, dst->chanpos);
 
756
            } else {
 
757
                qoz_assign(qoztmp, src->span->offset, src->chanpos, dst->span->offset, dst->chanpos, timeslot, use_pcm_bus);
 
758
            }
 
759
        } else {
 
760
                qoztmp = dst->pvt;
 
761
                if (dst->chanpos == 3) {
 
762
                    if (debug)
 
763
                        printk("qozap: Not Unassigning D-channel %d/%d!\n", dst->span->offset, dst->chanpos);
 
764
                } else {
 
765
                    timeslot = dst->channo;
 
766
                    qoz_unassign(qoztmp, dst->span->offset, dst->chanpos, timeslot);
 
767
                    if (debug)
 
768
                        printk("qozap: Unassigning channel %d/%d, timeslot %d!\n", dst->span->offset, dst->chanpos, timeslot);
 
769
                }
 
770
        }
 
771
        return 0;
 
772
}
 
773
 
 
774
static void qoz_resync(struct qoz_card *qoztmp) {
 
775
    int i=0;
 
776
    int best=9999;
 
777
    int src=-1;
 
778
 
 
779
    if (pcmslave) return;
 
780
 
 
781
    for (i=0; i<qoztmp->stports; i++){
 
782
        if (qoztmp->st[i].l1up && (qoztmp->st_sync[i] > 0) && (qoztmp->st_sync[i] < best)) {
 
783
            best = qoztmp->st_sync[i];
 
784
            src = i;
 
785
        }
 
786
    }
 
787
    if (src < 0) {
 
788
        for (i=0; i<qoztmp->stports; i++){
 
789
            if ((qoztmp->st_sync[i] > 0) && (qoztmp->st_sync[i] < best)) {
 
790
                best = qoztmp->st_sync[i];
 
791
                src = i;
 
792
            }
 
793
        }
 
794
    }
 
795
    if (src == qoztmp->syncsrc)
 
796
        return;
 
797
    if (src >= 0) {
 
798
        qoztmp->syncsrc = src;
 
799
        qoz_outb(qoztmp, qoz_R_ST_SYNC, src | 0x08);
 
800
        if (debug > 2)
 
801
            printk(KERN_INFO "qozap: card %d Sync source changed to span %d\n", qoztmp->cardno, src + 1);
 
802
    }
 
803
}
 
804
 
 
805
static inline void qoz_run(struct qoz_card *qoztmp) {
 
806
    int s=0;
 
807
    unsigned long flags;
 
808
 
 
809
    for (s=0;s<qoztmp->stports;s++) {
 
810
        if (!bloop) {
 
811
            if (qoztmp->spans[s].flags & ZT_FLAG_RUNNING) {
 
812
                /* oh zaptel! tell us what to transmit... */
 
813
                zt_transmit(&qoztmp->spans[s]);
 
814
                /* B1 xmit */
 
815
                qoz_fifo_tx(qoztmp, s * 2);
 
816
                /* B2 xmit */
 
817
                qoz_fifo_tx(qoztmp, (s * 2) + 1);
 
818
 
 
819
                if ((qoztmp->st[s].layer1state != 7) && (qoztmp->chans[s][2].bytes2transmit > 0) && (qoztmp->st[s].nt_mode != 1)) {
 
820
                    if (qoztmp->st[s].t3 == -1) {
 
821
                        if (debug > 2)
 
822
                            printk(KERN_INFO "qozap: activating layer 1, span %d\n",s);
 
823
                        qoztmp->st[s].t3 = 0;
 
824
                        spin_lock_irqsave(&qoztmp->lock,flags);
 
825
                            qoz_outb(qoztmp,qoz_R_ST_SEL, s);
 
826
                            qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x60); 
 
827
                        spin_unlock_irqrestore(&qoztmp->lock, flags);
 
828
                    } else {
 
829
                    }
 
830
                }
 
831
                
 
832
                /* D xmit */
 
833
                if (qoztmp->spans[s].alarms != ZT_ALARM_RED) {
 
834
                    qoz_dfifo_tx(qoztmp, s);
 
835
                } else {
 
836
                    if ((qoztmp->st[s].t3 == -1) && (qoztmp->st[s].t4 == -1) && (qoztmp->st[s].layer1state == 3) && (qoztmp->st[s].nt_mode != 1)) {
 
837
                        /* clear alarms */
 
838
                        if (debug > 2)
 
839
                            printk(KERN_INFO "qozap: clearing alarms on span %d\n",s);
 
840
                        qoztmp->spans[s].alarms = ZT_ALARM_NONE;
 
841
                        zt_alarm_notify_no_master_change(&qoztmp->spans[s]);
 
842
                    }
 
843
                }
 
844
 
 
845
                qoztmp->chans[s][2].bytes2receive = 0;
 
846
                qoztmp->chans[s][2].bytes2transmit = 0;
 
847
                qoztmp->chans[s][2].eofrx = 0;
 
848
                qoztmp->chans[s][2].eoftx = 0;
 
849
 
 
850
            }
 
851
 
 
852
            /* B1 receive */
 
853
            qoz_fifo_rx(qoztmp,(s*2));
 
854
            /* B2 receive */
 
855
            qoz_fifo_rx(qoztmp,(s*2)+1);
 
856
            /* d-chan data */
 
857
            if (qoztmp->st[s].drx > 0) {
 
858
                if (debug > 2)
 
859
                    printk(KERN_CRIT "qozap: card %d st[%d].drx = %d\n", qoztmp->cardno, s, qoztmp->st[s].drx);
 
860
                qoz_dfifo_rx(qoztmp, s);
 
861
            }
 
862
            if (qoztmp->spans[s].flags & ZT_FLAG_RUNNING) {
 
863
                /* oh zaptel! thou shall receive! */
 
864
                zt_receive(&(qoztmp->spans[s]));
 
865
            }
 
866
        } else {
 
867
            // loop
 
868
            /* B1 receive */
 
869
            qoz_fifo_rx(qoztmp,(s*2));
 
870
            /* B2 receive */
 
871
            qoz_fifo_rx(qoztmp,(s*2)+1);
 
872
            /* d-chan data */
 
873
/*          if (qoztmp->st[s].drx > 0) {
 
874
                if (debug > 2)
 
875
                    printk(KERN_CRIT "qozap: card %d st[%d].drx = %d\n", qoztmp->cardno, s, qoztmp->st[s].drx);
 
876
                qoz_dfifo_rx(qoztmp, s);
 
877
            } 
 
878
            if (qoztmp->spans[s].flags & ZT_FLAG_RUNNING) {
 
879
                zt_receive(&(qoztmp->spans[s]));
 
880
            }
 
881
*/
 
882
            if (qoztmp->spans[s].flags & ZT_FLAG_RUNNING) {
 
883
                /* oh zaptel! tell us what to transmit... */
 
884
        //      zt_transmit(&qoztmp->spans[s]);
 
885
                /* B1 xmit */
 
886
                qoz_fifo_tx(qoztmp, s * 2);
 
887
                /* B2 xmit */
 
888
                qoz_fifo_tx(qoztmp, (s * 2) + 1);
 
889
                /* D xmit */
 
890
//              qoz_dfifo_tx(qoztmp, s);
 
891
 
 
892
                qoztmp->chans[s][2].bytes2receive = 0;
 
893
                qoztmp->chans[s][2].bytes2transmit = 0;
 
894
                qoztmp->chans[s][2].eofrx = 0;
 
895
                qoztmp->chans[s][2].eoftx = 0;
 
896
 
 
897
            }
 
898
        }
 
899
    } 
 
900
}
 
901
 
 
902
ZAP_IRQ_HANDLER(qoz_interrupt) {
 
903
    struct qoz_card *qoztmp = dev_id;
 
904
    unsigned long flags;
 
905
    unsigned char irq_misc,irq_sci,status,l1state,irq_foview,fi;
 
906
    int st=0,i=0,offset=0;
 
907
    int j=0;
 
908
 
 
909
    if (!qoztmp) {
 
910
#ifdef LINUX26
 
911
                return IRQ_NONE;
 
912
#else
 
913
                return;
 
914
#endif          
 
915
    }
 
916
 
 
917
    if ((!qoztmp->pci_io) || (!qoztmp->ioport)) {
 
918
            printk(KERN_CRIT "qozap: no pci mem/io\n");
 
919
#ifdef LINUX26
 
920
                return IRQ_NONE;
 
921
#else
 
922
                return;
 
923
#endif          
 
924
    }
 
925
    if (qoztmp->dead) {
 
926
#ifdef LINUX26
 
927
        return IRQ_RETVAL(1);
 
928
#else
 
929
        return;
 
930
#endif          
 
931
    }
 
932
 
 
933
    
 
934
    spin_lock_irqsave(&(qoztmp->lock), flags);
 
935
        status = qoz_inb(qoztmp,qoz_R_STATUS);
 
936
        irq_sci = qoz_inb(qoztmp,qoz_R_SCI);
 
937
    spin_unlock_irqrestore(&(qoztmp->lock), flags);
 
938
 
 
939
    if (!(status & 0x80) && !(status & 0x40) && (irq_sci == 0)) {
 
940
//      printk(KERN_CRIT "qozap: status %#x\n", status);
 
941
        // it's not us!
 
942
#ifdef LINUX26
 
943
                return IRQ_NONE;
 
944
#else
 
945
                return;
 
946
#endif          
 
947
    }
 
948
    /* state machine irq */
 
949
    if (irq_sci != 0) {
 
950
        if (debug > 1) {
 
951
            printk(KERN_INFO "qozap: card %d R_BERT_STA = %#x\n", qoztmp->cardno, qoz_inb(qoztmp, qoz_R_BERT_STA) & 7);
 
952
        }
 
953
        if (qoztmp->type == 0xb552) {
 
954
            offset = 24;
 
955
        } else if (qoztmp->type == 0xb55b) {
 
956
            offset = 24;
 
957
        } else {
 
958
            offset = 28;
 
959
        }
 
960
        spin_lock_irqsave(&(qoztmp->lock), flags);
 
961
        for (st=0;st<qoztmp->stports;st++) {
 
962
            if (irq_sci & (1 << st)) {
 
963
                qoz_outb(qoztmp,qoz_R_ST_SEL,st);
 
964
                l1state = qoz_inb(qoztmp,qoz_A_ST_RD_STA) & 0xf;
 
965
                if (debug > 1) {
 
966
                    printk(KERN_INFO "qozap: card %d span %d A_ST_RD_STA = %#x\n", qoztmp->cardno, st+1, qoz_inb(qoztmp, qoz_A_ST_RD_STA));
 
967
                }
 
968
                qoztmp->st[st].layer1state = l1state;
 
969
                if (qoztmp->st[st].nt_mode == 1) {
 
970
                    if (debug)
 
971
                        printk(KERN_INFO "card %d span %d state G%d (A_ST_RD_STA = %#x)\n",qoztmp->cardno,st+1,l1state,qoz_inb(qoztmp,qoz_A_ST_RD_STA));
 
972
                    // NT state machine
 
973
                    if (l1state == 3) {
 
974
                        qoztmp->st[st].l1up = 1;
 
975
                        // keep layer1 up!
 
976
                        if (qoztmp->stports == 8) {
 
977
                            sprintf(qoztmp->spans[st].desc,"octoBRI PCI ISDN Card %d Span %d [NT] Layer 1 ACTIVATED (G%d)",qoztmp->cardno ,st + 1, l1state);
 
978
                        } else if (qoztmp->stports == 2) {
 
979
                            sprintf(qoztmp->spans[st].desc,"duoBRI PCI ISDN Card %d Span %d [NT] (cardID %d) Layer 1 ACTIVATED (G%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
980
                        } else{
 
981
                            sprintf(qoztmp->spans[st].desc,"quadBRI PCI ISDN Card %d Span %d [NT] (cardID %d) Layer 1 ACTIVATED (G%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
982
                        }
 
983
                        qoz_outb(qoztmp,qoz_A_ST_WR_STA,3 | 0x10 );
 
984
                        qoztmp->leds[st] = 1;
 
985
                    } else {
 
986
                        qoztmp->st[st].l1up = 0;
 
987
                        if (qoztmp->stports == 8) {
 
988
                            sprintf(qoztmp->spans[st].desc,"octoBRI PCI ISDN Card %d Span %d [NT] Layer 1 DEACTIVATED (G%d)",qoztmp->cardno ,st + 1, l1state);
 
989
                        } else if (qoztmp->stports == 2) {
 
990
                            sprintf(qoztmp->spans[st].desc,"duoBRI PCI ISDN Card %d Span %d [NT] (cardID %d) Layer 1 DEACTIVATED (G%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
991
                        } else {
 
992
                            sprintf(qoztmp->spans[st].desc,"quadBRI PCI ISDN Card %d Span %d [NT] (cardID %d) Layer 1 DEACTIVATED (G%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
993
                        }
 
994
                        qoztmp->leds[st] = 0;
 
995
                    }
 
996
                } else {
 
997
                    if (debug)
 
998
                        printk(KERN_INFO "card %d span %d state F%d (A_ST_RD_STA = %#x)\n",qoztmp->cardno,st+1,l1state,qoz_inb(qoztmp,qoz_A_ST_RD_STA));
 
999
                    // TE state machine
 
1000
                    if (l1state == 3) {
 
1001
                        qoztmp->st[st].l1up = 0;
 
1002
                        if (qoztmp->st[st].t3 > -1)  {
 
1003
                            /* keep layer1 up, if the span is started. */
 
1004
                            if (qoztmp->spans[st].flags & ZT_FLAG_RUNNING) {
 
1005
                                if (debug > 2) 
 
1006
                                    printk("qozap: re-activating layer1 span %d\n", st);
 
1007
                                qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x60);
 
1008
                            }
 
1009
                        } else {
 
1010
                                if (debug > 2) 
 
1011
                                    printk("qozap: not re-activating layer1 span %d\n", st);
 
1012
                                qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x40);
 
1013
                            /* if we tried to activate layer 1 and it failed make this an alarm */
 
1014
//                          qoztmp->spans[st].alarms = ZT_ALARM_RED;
 
1015
//                          zt_alarm_notify(&qoztmp->spans[st]);
 
1016
                            /* if the network shuts us down in idle mode dont make this an alarm */
 
1017
                        }
 
1018
                        qoztmp->leds[st] = 0;
 
1019
                        if (qoztmp->stports == 8) {
 
1020
                            sprintf(qoztmp->spans[st].desc,"octoBRI PCI ISDN Card %d Span %d [TE] Layer 1 DEACTIVATED (F%d)",qoztmp->cardno ,st + 1, l1state);
 
1021
                        } else if (qoztmp->stports == 2) {
 
1022
                            sprintf(qoztmp->spans[st].desc,"duoBRI PCI ISDN Card %d Span %d [TE] (cardID %d) Layer 1 DEACTIVATED (F%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
1023
                        } else {
 
1024
                            sprintf(qoztmp->spans[st].desc,"quadBRI PCI ISDN Card %d Span %d [TE] (cardID %d) Layer 1 DEACTIVATED (F%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
1025
                        }
 
1026
                    } else if (l1state == 7) {
 
1027
                        qoztmp->st[st].l1up = 1;
 
1028
                        /* reset D RX fifo */
 
1029
                        qoz_outb(qoztmp,qoz_R_FIFO,((st + offset) << 1) | 1);
 
1030
                        qoz_waitbusy(qoztmp);
 
1031
                        qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
1032
                        qoz_waitbusy(qoztmp);
 
1033
                        /* activation complete, stop timer t3 */
 
1034
                        qoztmp->st[st].t3 = -1;
 
1035
                        qoztmp->spans[st].alarms = ZT_ALARM_NONE;
 
1036
                        zt_alarm_notify(&qoztmp->spans[st]);
 
1037
                        qoztmp->leds[st] = 1;
 
1038
                        if (qoztmp->stports == 8) {
 
1039
                            sprintf(qoztmp->spans[st].desc,"octoBRI PCI ISDN Card %d Span %d [TE] Layer 1 ACTIVATED (F%d)",qoztmp->cardno ,st + 1, l1state);
 
1040
                        } else if (qoztmp->stports == 2) {
 
1041
                            sprintf(qoztmp->spans[st].desc,"duoBRI PCI ISDN Card %d Span %d [TE] (cardID %d) Layer 1 ACTIVATED (F%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
1042
                        } else {
 
1043
                            sprintf(qoztmp->spans[st].desc,"quadBRI PCI ISDN Card %d Span %d [TE] (cardID %d) Layer 1 ACTIVATED (F%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
1044
                        }
 
1045
                    } else if (l1state == 8) {
 
1046
                        /* lost framing */
 
1047
                        if (debug > 2)
 
1048
                            printk(KERN_INFO "qozap: starting t4 for span %d\n", st);
 
1049
                        qoztmp->st[st].t4 = 0;
 
1050
                    } else {
 
1051
                        qoztmp->st[st].l1up = 0;
 
1052
                        qoztmp->leds[st] = 0;
 
1053
                        if (qoztmp->stports == 8) {
 
1054
                            sprintf(qoztmp->spans[st].desc,"octoBRI PCI ISDN Card %d Span %d [TE] Layer 1 DEACTIVATED (F%d)",qoztmp->cardno ,st + 1, l1state);
 
1055
                        } else if (qoztmp->stports == 2) {
 
1056
                            sprintf(qoztmp->spans[st].desc,"duoBRI PCI ISDN Card %d Span %d [TE] (cardID %d) Layer 1 DEACTIVATED (F%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
1057
                        } else {
 
1058
                            sprintf(qoztmp->spans[st].desc,"quadBRI PCI ISDN Card %d Span %d [TE] (cardID %d) Layer 1 DEACTIVATED (F%d)",qoztmp->cardno ,st + 1,qoztmp->cardID, l1state);
 
1059
                        }    
 
1060
                    }
 
1061
                }
 
1062
                
 
1063
            }
 
1064
        }
 
1065
        qoz_resync(qoztmp);
 
1066
        spin_unlock_irqrestore(&(qoztmp->lock), flags);
 
1067
    }
 
1068
 
 
1069
 
 
1070
    // misc irq
 
1071
    if (status & 0x40) {
 
1072
        spin_lock_irqsave(&(qoztmp->lock), flags);
 
1073
            irq_misc = qoz_inb(qoztmp,qoz_R_IRQ_MISC);
 
1074
        spin_unlock_irqrestore(&(qoztmp->lock), flags);
 
1075
        if (irq_misc & 0x2)  {
 
1076
            // qozap timer
 
1077
            qoztmp->ticks++;
 
1078
            qoz_run(qoztmp);
 
1079
            if (qoztmp->ticks % 100) {
 
1080
                qoz_doLEDs(qoztmp);
 
1081
            }
 
1082
            if (qoztmp->ticks % 40) { 
 
1083
                /* you thought that 42 was the answer.... */
 
1084
                qoz_doWD(qoztmp);
 
1085
            }
 
1086
            if (qoztmp->ticks > 1000) {
 
1087
                qoztmp->ticks = 0;
 
1088
                for (j=0;j<qoztmp->stports;j++) {
 
1089
                    /* t3 */
 
1090
                    if (qoztmp->st[j].t3 >= 0) {
 
1091
                        qoztmp->st[j].t3++;
 
1092
                    }
 
1093
                    if (qoztmp->st[j].nt_mode != 1) {
 
1094
                        if ((qoztmp->st[j].t3 > qoz_T3) && (qoztmp->st[j].layer1state != 7)) {
 
1095
                            /* deactivate layer 1 */
 
1096
                            if (debug > 2)
 
1097
                                printk(KERN_INFO "qozap: t3 timer expired for span %d\n", j);
 
1098
                            spin_lock_irqsave(&(qoztmp->lock), flags);
 
1099
                                qoz_outb(qoztmp,qoz_R_ST_SEL, j);
 
1100
                                qoz_outb(qoztmp,qoz_A_ST_WR_STA, 0x40 );
 
1101
                                qoz_waitbusy(qoztmp);
 
1102
                                qoztmp->st[j].t3 = -1;
 
1103
                                qoztmp->spans[j].alarms = ZT_ALARM_RED;
 
1104
                            spin_unlock_irqrestore(&(qoztmp->lock), flags);
 
1105
                            zt_alarm_notify_no_master_change(&qoztmp->spans[j]);
 
1106
                        }
 
1107
                    }
 
1108
                    /* t4 */
 
1109
                    if (qoztmp->st[j].t4 >= 0) {
 
1110
                        qoztmp->st[j].t4++;
 
1111
                    }
 
1112
                    if (qoztmp->st[j].nt_mode != 1) {
 
1113
                        if ((qoztmp->st[j].t4 > qoz_T4) && (qoztmp->st[j].layer1state != 7)) {
 
1114
                            /* deactivate layer 1 */
 
1115
                            if (debug > 2)
 
1116
                                printk(KERN_INFO "qozap: t4 timer expired for span %d\n", j);
 
1117
                            spin_lock_irqsave(&(qoztmp->lock), flags);
 
1118
                                qoz_outb(qoztmp,qoz_R_ST_SEL, j);
 
1119
                                qoz_outb(qoztmp,qoz_A_ST_WR_STA, 0x40 );
 
1120
                                qoztmp->st[j].t4 = -1;
 
1121
                                qoztmp->st[st].l1up = 0;
 
1122
                                qoztmp->spans[j].alarms = ZT_ALARM_RED;
 
1123
                                qoz_waitbusy(qoztmp);
 
1124
                            spin_unlock_irqrestore(&(qoztmp->lock), flags);
 
1125
                            zt_alarm_notify_no_master_change(&qoztmp->spans[j]);
 
1126
                        }
 
1127
                    }
 
1128
                }
 
1129
            }
 
1130
        }
 
1131
        if (irq_misc & 0x4) {
 
1132
        //    printk(KERN_INFO "qozap proc/nonproc irq\n");
 
1133
        }
 
1134
    }
 
1135
 
 
1136
    if (status & 0x80) {
 
1137
        /* fifo irq */
 
1138
        spin_lock_irqsave(&(qoztmp->lock), flags);
 
1139
        irq_foview = qoz_inb(qoztmp,qoz_R_IRQ_OVIEW);
 
1140
        if ((qoztmp->type == 0xb552) || (qoztmp->type == 0xb55b)) {
 
1141
            if (irq_foview & 0x60) {
 
1142
                offset = 0;
 
1143
                fi = qoz_inb(qoztmp,qoz_R_IRQ_FIFO_BL6);
 
1144
                for (i=0; i < 8; i++) {
 
1145
                    if (fi & (1 << i)) {
 
1146
                        st = offset + (i / 2);
 
1147
                        if (i % 2) {
 
1148
                            if (debug > 2) 
 
1149
                                printk(KERN_CRIT "qozap: HDLC RX irq fifo %d span %d\n", i, st+1);
 
1150
                                qoztmp->st[st].drx += 1;                        
 
1151
                        } else {
 
1152
                            if (debug > 2) 
 
1153
                                printk(KERN_CRIT "qozap: HDLC TX irq fifo %d span %d\n", i, st+1);
 
1154
                        }
 
1155
                    }
 
1156
                }
 
1157
            }
 
1158
            if (irq_foview & 0x80) {
 
1159
                offset = 4;
 
1160
                fi = qoz_inb(qoztmp,qoz_R_IRQ_FIFO_BL7);
 
1161
                for (i=0; i < 8; i++) {
 
1162
                    if (fi & (1 << i)) {
 
1163
                        st = offset + (i / 2);
 
1164
                        if (i % 2) {
 
1165
                            if (debug > 2) 
 
1166
                                printk(KERN_CRIT "qozap: HDLC RX irq fifo %d span %d\n", i, st+1);
 
1167
                                qoztmp->st[st].drx += 1;                        
 
1168
                        } else {
 
1169
                            if (debug > 2) 
 
1170
                                printk(KERN_CRIT "qozap: HDLC TX irq fifo %d span %d\n", i, st+1);
 
1171
                        }
 
1172
                    }
 
1173
                }
 
1174
            }
 
1175
        } else {
 
1176
            if (irq_foview & 0x80) {
 
1177
                fi = qoz_inb(qoztmp,qoz_R_IRQ_FIFO_BL7);
 
1178
                for (i=0; i < 8; i++) {
 
1179
                    if (fi & (1 << i)) {
 
1180
                        st = i / 2;
 
1181
                        if (i % 2) {
 
1182
                            if (debug > 2) 
 
1183
                                printk(KERN_CRIT "qozap: HDLC RX irq fifo %d span %d\n", i, st+1);
 
1184
                                qoztmp->st[st].drx += 1;                        
 
1185
                        } else {
 
1186
                            if (debug > 2) 
 
1187
                                printk(KERN_CRIT "qozap: HDLC TX irq fifo %d span %d\n", i, st+1);
 
1188
                        }
 
1189
                    }
 
1190
                }
 
1191
            }
 
1192
        }
 
1193
        spin_unlock_irqrestore(&(qoztmp->lock), flags);
 
1194
    }
 
1195
    
 
1196
#ifdef LINUX26
 
1197
        return IRQ_RETVAL(1);
 
1198
#endif          
 
1199
}
 
1200
 
 
1201
static int ztqoz_open(struct zt_chan *chan) {
 
1202
//    printk(KERN_INFO "qozap: channel %d opened.\n",chan->channo);
 
1203
#ifndef LINUX26
 
1204
    MOD_INC_USE_COUNT;
 
1205
#else
 
1206
    try_module_get(THIS_MODULE);
 
1207
#endif
 
1208
    return 0;
 
1209
}
 
1210
 
 
1211
static int ztqoz_close(struct zt_chan *chan) {
 
1212
//    printk(KERN_INFO "qozap: channel %d closed.\n",chan->channo);
 
1213
#ifndef LINUX26
 
1214
    MOD_DEC_USE_COUNT;
 
1215
#else
 
1216
    module_put(THIS_MODULE);
 
1217
#endif
 
1218
    return 0;
 
1219
}
 
1220
 
 
1221
static int ztqoz_rbsbits(struct zt_chan *chan, int bits) {
 
1222
    return 0;
 
1223
}
 
1224
 
 
1225
static int ztqoz_ioctl(struct zt_chan *chan, unsigned int cmd, unsigned long data) {
 
1226
        switch(cmd) {
 
1227
        default:
 
1228
                return -ENOTTY;
 
1229
        }
 
1230
        return 0;
 
1231
}
 
1232
 
 
1233
static int ztqoz_startup(struct zt_span *span) {
 
1234
    struct qoz_card *qoztmp = span->pvt;
 
1235
    unsigned long flags;
 
1236
    int alreadyrunning;
 
1237
    int i=0;
 
1238
    int offset = 0;
 
1239
    
 
1240
    if (qoztmp == NULL) {
 
1241
        printk(KERN_INFO "qozap: no card for span at startup!\n");
 
1242
    }
 
1243
    
 
1244
    alreadyrunning = span->flags & ZT_FLAG_RUNNING;
 
1245
//    printk(KERN_CRIT "already running %d flags %d\n", alreadyrunning, span->flags);
 
1246
 
 
1247
    if (!alreadyrunning) {
 
1248
        span->chans[2].flags &= ~ZT_FLAG_HDLC;
 
1249
        span->chans[2].flags |= ZT_FLAG_BRIDCHAN;
 
1250
    
 
1251
        /* setup B channel buffers (8 bytes each) */
 
1252
        for (i=0; i<2 ; i++) {
 
1253
            memset(qoztmp->rxbuf[span->offset][i],0x0,sizeof(qoztmp->rxbuf[span->offset][i]));
 
1254
            span->chans[i].readchunk = qoztmp->rxbuf[span->offset][i];
 
1255
            memset(qoztmp->txbuf[span->offset][i],0x0,sizeof(qoztmp->txbuf[span->offset][i]));
 
1256
            span->chans[i].writechunk = qoztmp->txbuf[span->offset][i];
 
1257
        }
 
1258
        /* setup D channel buffer */
 
1259
        memset(qoztmp->dtxbuf[span->offset],0x0,sizeof(qoztmp->dtxbuf[span->offset]));
 
1260
        span->chans[2].writechunk = qoztmp->dtxbuf[span->offset];
 
1261
        qoztmp->chans[span->offset][2].maxbytes2transmit = sizeof(qoztmp->dtxbuf[span->offset]);
 
1262
 
 
1263
        memset(qoztmp->drxbuf[span->offset],0x0,sizeof(qoztmp->drxbuf[span->offset]));
 
1264
        span->chans[2].readchunk = qoztmp->drxbuf[span->offset];
 
1265
 
 
1266
        span->flags |= ZT_FLAG_RUNNING;
 
1267
    } else {
 
1268
//      printk(KERN_CRIT "already running\n");
 
1269
        return 0;
 
1270
    }
 
1271
 
 
1272
    if (pcmslave && !span->offset) {
 
1273
        set_current_state(TASK_UNINTERRUPTIBLE);
 
1274
        schedule_timeout((100 * HZ) / 1000);
 
1275
    }
 
1276
 
 
1277
    spin_lock_irqsave(&qoztmp->lock,flags);
 
1278
    if (pcmslave && !span->offset) {
 
1279
        if (debug) 
 
1280
            printk(KERN_INFO "qozap: resetting PCM interface to slave mode\n");
 
1281
        qoz_reset_pcm(qoztmp);
 
1282
    }
 
1283
 
 
1284
    // irqs off
 
1285
    qoz_outb(qoztmp,qoz_R_IRQ_CTRL, 0); 
 
1286
 
 
1287
    if (qoztmp->type == 0xb552) {
 
1288
        offset = 24;
 
1289
    } else if (qoztmp->type == 0xb55b) {
 
1290
        offset = 24;
 
1291
    } else {
 
1292
        offset = 28;
 
1293
    }
 
1294
 
 
1295
    /* setup D-FIFO TX */
 
1296
    qoz_outb(qoztmp,qoz_R_FIFO,(span->offset + offset) << 1);
 
1297
    qoz_waitbusy(qoztmp);
 
1298
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
1299
    qoz_waitbusy(qoztmp);
 
1300
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0xD);
 
1301
    qoz_outb(qoztmp,qoz_A_SUBCH_CFG,0x2);
 
1302
    qoz_outb(qoztmp,qoz_A_CHANNEL,((span->offset * 4) + 2) << 1);
 
1303
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
1304
 
 
1305
    /* setup D-FIFO RX */
 
1306
    qoz_outb(qoztmp,qoz_R_FIFO,((span->offset + offset) << 1) | 1);
 
1307
    qoz_waitbusy(qoztmp);
 
1308
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
1309
    qoz_waitbusy(qoztmp);
 
1310
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0xD);
 
1311
    qoz_outb(qoztmp,qoz_A_SUBCH_CFG,0x2);
 
1312
    qoz_outb(qoztmp,qoz_A_CHANNEL,(((span->offset * 4) + 2) << 1) | 1);
 
1313
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
1314
 
 
1315
    /* setup B1-FIFO TX */
 
1316
    qoz_outb(qoztmp,qoz_R_FIFO,(span->offset * 2) << 1);
 
1317
    qoz_waitbusy(qoztmp);
 
1318
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
1319
    qoz_waitbusy(qoztmp);
 
1320
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1321
    qoz_outb(qoztmp,qoz_A_CHANNEL,(span->offset * 4) << 1);
 
1322
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
1323
 
 
1324
    /* setup B1-FIFO RX */
 
1325
    qoz_outb(qoztmp,qoz_R_FIFO,((span->offset * 2) << 1) | 1);
 
1326
    qoz_waitbusy(qoztmp);
 
1327
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
1328
    qoz_waitbusy(qoztmp);
 
1329
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1330
    qoz_outb(qoztmp,qoz_A_CHANNEL,((span->offset * 4) << 1) | 1);
 
1331
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
1332
 
 
1333
    /* setup B2-FIFO TX */
 
1334
    qoz_outb(qoztmp,qoz_R_FIFO,((span->offset * 2) + 1) << 1);
 
1335
    qoz_waitbusy(qoztmp);
 
1336
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
1337
    qoz_waitbusy(qoztmp);
 
1338
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1339
    qoz_outb(qoztmp,qoz_A_CHANNEL,((span->offset * 4) + 1) << 1);
 
1340
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
1341
 
 
1342
    /* setup B2-FIFO RX */
 
1343
    qoz_outb(qoztmp,qoz_R_FIFO,(((span->offset * 2) + 1) << 1) | 1);
 
1344
    qoz_waitbusy(qoztmp);
 
1345
    qoz_outb(qoztmp,qoz_R_INC_RES_FIFO,0x2);
 
1346
    qoz_waitbusy(qoztmp);
 
1347
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1348
    qoz_outb(qoztmp,qoz_A_CHANNEL,((((span->offset) * 4) + 1) << 1) | 1);
 
1349
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x1);
 
1350
 
 
1351
    if (debug)
 
1352
        printk(KERN_INFO "qozap: starting card %d span %d/%d.\n",qoztmp->cardno,span->spanno,span->offset);
 
1353
    
 
1354
    /* activate layer 1 */
 
1355
    qoz_outb(qoztmp,qoz_R_ST_SEL,span->offset);
 
1356
    if (qoztmp->st[span->offset].nt_mode == 1) {
 
1357
        // NT mode
 
1358
        qoz_outb(qoztmp,qoz_A_ST_CTRL0,0x7);
 
1359
        qoz_outb(qoztmp,qoz_A_ST_CTRL1,0x0);
 
1360
        qoz_outb(qoztmp,qoz_A_ST_CTRL2,0x3);
 
1361
        qoz_outb(qoztmp,qoz_A_ST_CLK_DLY,0x60 | CLKDEL_NT);
 
1362
    } else {
 
1363
        // TE mode
 
1364
        qoz_outb(qoztmp,qoz_A_ST_CTRL0,0x3);
 
1365
        qoz_outb(qoztmp,qoz_A_ST_CTRL1,0x0);
 
1366
        qoz_outb(qoztmp,qoz_A_ST_CTRL2,0x3);
 
1367
        if (qoztmp->type == 0xb550) {
 
1368
            qoz_outb(qoztmp,qoz_A_ST_CLK_DLY,CLKDEL_TE);
 
1369
        } else {
 
1370
            qoz_outb(qoztmp,qoz_A_ST_CLK_DLY,CLKDEL_TE + 1);
 
1371
        }
 
1372
    }
 
1373
    qoztmp->st[span->offset].t3 = 0;
 
1374
    qoztmp->st[span->offset].t4 = -1;
 
1375
    
 
1376
    qoz_outb(qoztmp,qoz_R_ST_SEL,span->offset);
 
1377
    if (qoztmp->st[span->offset].nt_mode == 1) {
 
1378
        qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x80); 
 
1379
    } else {
 
1380
        qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x0); 
 
1381
    }
 
1382
 
 
1383
 
 
1384
    qoz_outb(qoztmp,qoz_R_ST_SEL,span->offset);
 
1385
    if (qoztmp->st[span->offset].nt_mode == 1) {
 
1386
        qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x60 | 0x80); // ACT, G2->G3 EN
 
1387
    } else {
 
1388
        qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x60); // start Activation
 
1389
    }
 
1390
 
 
1391
    /* enable irqs */
 
1392
    qoz_outb(qoztmp,qoz_R_IRQ_CTRL, 8 | 1); 
 
1393
 
 
1394
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
1395
    return 0;
 
1396
}
 
1397
 
 
1398
static int ztqoz_shutdown(struct zt_span *span) {
 
1399
    unsigned long flags;
 
1400
    struct qoz_card *qoztmp = span->pvt;
 
1401
    int alreadyrunning;
 
1402
    int offset = 0;
 
1403
    
 
1404
    if (qoztmp == NULL) {
 
1405
        printk(KERN_CRIT "qozap: qoztmp == NULL!\n");
 
1406
        return 0;
 
1407
        
 
1408
    }
 
1409
 
 
1410
    alreadyrunning = span->flags & ZT_FLAG_RUNNING;
 
1411
    
 
1412
    if (!alreadyrunning) {
 
1413
        return 0;
 
1414
    }
 
1415
 
 
1416
//    printk(KERN_CRIT "qozap: stopping card %d port %d.\n",qoztmp->cardno, span->offset + 1);
 
1417
 
 
1418
    spin_lock_irqsave(&qoztmp->lock,flags);
 
1419
    // turn off irqs for all fifos
 
1420
    if (qoztmp->type == 0xb552) {
 
1421
        offset = 24;
 
1422
    } else if (qoztmp->type == 0xb55b) {
 
1423
        offset = 24;
 
1424
    } else {
 
1425
        offset = 28;
 
1426
    }
 
1427
 
 
1428
    /* disable D TX fifo */
 
1429
    qoz_outb(qoztmp,qoz_R_FIFO,(span->offset + offset) << 1);
 
1430
    qoz_waitbusy(qoztmp);
 
1431
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x1);
 
1432
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x0);
 
1433
 
 
1434
    /* disable D RX fifo */
 
1435
    qoz_outb(qoztmp,qoz_R_FIFO,((span->offset + offset) << 1) | 1);
 
1436
    qoz_waitbusy(qoztmp);
 
1437
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x1);
 
1438
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x0);
 
1439
 
 
1440
    /* disable B1 TX fifo */
 
1441
    qoz_outb(qoztmp,qoz_R_FIFO,(span->offset * 2) << 1);
 
1442
    qoz_waitbusy(qoztmp);
 
1443
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1444
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x0);
 
1445
 
 
1446
    /* disable B1 RX fifo */
 
1447
    qoz_outb(qoztmp,qoz_R_FIFO,((span->offset * 2) << 1) | 1);
 
1448
    qoz_waitbusy(qoztmp);
 
1449
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1450
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x0);
 
1451
 
 
1452
    /* disable B2 TX fifo */
 
1453
    qoz_outb(qoztmp,qoz_R_FIFO,(((span->offset) * 2) + 1) << 1);
 
1454
    qoz_waitbusy(qoztmp);
 
1455
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1456
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x0);
 
1457
 
 
1458
    /* disable B2 RX fifo */
 
1459
    qoz_outb(qoztmp,qoz_R_FIFO,((((span->offset) * 2) + 1) << 1) | 1);
 
1460
    qoz_waitbusy(qoztmp);
 
1461
    qoz_outb(qoztmp,qoz_A_CON_HDLC,0x2);
 
1462
    qoz_outb(qoztmp,qoz_A_IRQ_MSK,0x0); 
 
1463
 
 
1464
    span->flags &= ~ZT_FLAG_RUNNING;
 
1465
 
 
1466
    /* Deactivate Layer 1 */
 
1467
    qoz_outb(qoztmp,qoz_R_ST_SEL,span->offset);
 
1468
    if (qoztmp->st[span->offset].nt_mode == 1) {
 
1469
        qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x40); 
 
1470
    } else {
 
1471
        qoz_outb(qoztmp,qoz_A_ST_WR_STA,0x40);
 
1472
    }
 
1473
 
 
1474
 
 
1475
    spin_unlock_irqrestore(&qoztmp->lock,flags);
 
1476
//    printk(KERN_CRIT "qozap: card %d span %d/%d down.\n",qoztmp->cardno,span->spanno,span->offset);
 
1477
    return 0;
 
1478
}
 
1479
 
 
1480
static int ztqoz_maint(struct zt_span *span, int cmd) {
 
1481
    return 0;
 
1482
}
 
1483
 
 
1484
static int ztqoz_chanconfig(struct zt_chan *chan,int sigtype) {
 
1485
//    printk(KERN_INFO "chan_config sigtype=%d\n",sigtype);
 
1486
    return 0;
 
1487
}
 
1488
 
 
1489
static int ztqoz_spanconfig(struct zt_span *span,struct zt_lineconfig *lc) {
 
1490
    struct qoz_card *qoztmp = span->pvt;
 
1491
    span->lineconfig = lc->lineconfig;
 
1492
    qoztmp->st_sync[span->offset] = lc->sync;
 
1493
    qoztmp->syncsrc = -1;
 
1494
    return 0;
 
1495
}
 
1496
 
 
1497
static int ztqoz_initialize(struct qoz_card *qoztmp) {
 
1498
    int i=0,s=0;
 
1499
    
 
1500
    for (s=0; s < qoztmp->stports; s++) {
 
1501
        memset(&qoztmp->spans[s],0,sizeof(struct zt_span));
 
1502
        sprintf(qoztmp->spans[s].name,"ztqoz/%d/%d",qoztmp->cardno ,s + 1);
 
1503
        if (qoztmp->stports == 8) {
 
1504
            if (qoztmp->st[s].nt_mode == 1){
 
1505
                sprintf(qoztmp->spans[s].desc,"octoBRI PCI ISDN Card %d Span %d [NT]",qoztmp->cardno,s + 1);
 
1506
            } else {
 
1507
                sprintf(qoztmp->spans[s].desc,"octoBRI PCI ISDN Card %d Span %d [TE]",qoztmp->cardno,s + 1);
 
1508
            }
 
1509
        } else  if (qoztmp->stports == 2) {
 
1510
            if (qoztmp->st[s].nt_mode == 1){
 
1511
                sprintf(qoztmp->spans[s].desc,"duoBRI PCI ISDN Card %d Span %d [NT]",qoztmp->cardno,s + 1);
 
1512
            } else {
 
1513
                sprintf(qoztmp->spans[s].desc,"duoBRI PCI ISDN Card %d Span %d [TE]",qoztmp->cardno,s + 1);
 
1514
            }
 
1515
        } else {
 
1516
            if (qoztmp->cardID < 0xff) {
 
1517
                if (qoztmp->st[s].nt_mode == 1){
 
1518
                    sprintf(qoztmp->spans[s].desc,"quadBRI PCI ISDN Card %d Span %d [NT] (cardID %d)",qoztmp->cardno,s + 1,qoztmp->cardID);
 
1519
                } else {
 
1520
                    sprintf(qoztmp->spans[s].desc,"quadBRI PCI ISDN Card %d Span %d [TE] (cardID %d)",qoztmp->cardno,s + 1,qoztmp->cardID);
 
1521
                }
 
1522
            } else {
 
1523
                if (qoztmp->st[s].nt_mode == 1){
 
1524
                    sprintf(qoztmp->spans[s].desc,"quadBRI PCI ISDN Card %d Span %d [NT]",qoztmp->cardno,s + 1);
 
1525
                } else {
 
1526
                    sprintf(qoztmp->spans[s].desc,"quadBRI PCI ISDN Card %d Span %d [TE]",qoztmp->cardno,s + 1);
 
1527
                }
 
1528
            }
 
1529
        }
 
1530
 
 
1531
        qoztmp->spans[s].spanconfig = ztqoz_spanconfig;
 
1532
        qoztmp->spans[s].chanconfig = ztqoz_chanconfig;
 
1533
        qoztmp->spans[s].startup = ztqoz_startup;
 
1534
        qoztmp->spans[s].shutdown = ztqoz_shutdown;
 
1535
        qoztmp->spans[s].maint = ztqoz_maint;
 
1536
        qoztmp->spans[s].rbsbits = ztqoz_rbsbits;
 
1537
        qoztmp->spans[s].open = ztqoz_open;
 
1538
        qoztmp->spans[s].close = ztqoz_close;
 
1539
        qoztmp->spans[s].ioctl = ztqoz_ioctl;
 
1540
        qoztmp->spans[s].dacs = ztqoz_dacs;
 
1541
 
 
1542
        qoztmp->spans[s].chans = qoztmp->chans[s];
 
1543
        qoztmp->spans[s].channels = 3;
 
1544
        qoztmp->spans[s].deflaw = ZT_LAW_ALAW;
 
1545
        qoztmp->spans[s].linecompat = ZT_CONFIG_AMI | ZT_CONFIG_CCS;
 
1546
        init_waitqueue_head(&qoztmp->spans[s].maintq);
 
1547
        qoztmp->spans[s].pvt = qoztmp;
 
1548
        qoztmp->spans[s].offset = s;
 
1549
 
 
1550
        for (i=0; i < qoztmp->spans[s].channels; i++) {
 
1551
            memset(&(qoztmp->chans[s][i]),0x0,sizeof(struct zt_chan));
 
1552
            sprintf(qoztmp->chans[s][i].name,"ztqoz%d/%d/%d",qoztmp->cardno,s + 1,i + 1);
 
1553
            qoztmp->chans[s][i].pvt = qoztmp;
 
1554
            qoztmp->chans[s][i].sigcap =  ZT_SIG_CLEAR | ZT_SIG_DACS;
 
1555
            qoztmp->chans[s][i].chanpos = i + 1; 
 
1556
        }
 
1557
 
 
1558
        if (zt_register(&qoztmp->spans[s],0)) {
 
1559
            printk(KERN_INFO "qozap: unable to register zaptel span %d!\n",s+1);
 
1560
            return -1;
 
1561
        }
 
1562
//       printk(KERN_INFO "qozap: registered zaptel span %d.\n",s+1);
 
1563
    }
 
1564
 
 
1565
    return 0;
 
1566
}
 
1567
 
 
1568
int qoz_findCards(unsigned int pcidid) {
 
1569
    struct pci_dev *tmp;
 
1570
    struct qoz_card *qoztmp = NULL;
 
1571
    int i=0;
 
1572
    unsigned char dips=0;
 
1573
    int cid=0;
 
1574
    int modes=0;
 
1575
    tmp = pci_get_device(PCI_VENDOR_ID_CCD,pcidid,multi_qoz);
 
1576
    while (tmp != NULL) {
 
1577
        multi_qoz = tmp;        // skip this next time.
 
1578
 
 
1579
        if (pci_enable_device(tmp)) {
 
1580
            multi_qoz = NULL;
 
1581
            return -1;
 
1582
        }
 
1583
 
 
1584
        qoztmp = kmalloc(sizeof(struct qoz_card),GFP_KERNEL);
 
1585
        if (!qoztmp) {
 
1586
            printk(KERN_WARNING "qozap: unable to kmalloc!\n");
 
1587
            pci_disable_device(tmp);
 
1588
            multi_qoz = NULL;
 
1589
            return -ENOMEM;
 
1590
        }
 
1591
        memset(qoztmp, 0x0, sizeof(struct qoz_card));
 
1592
        
 
1593
        spin_lock_init(&qoztmp->lock);
 
1594
        qoztmp->pcidev = tmp;
 
1595
        qoztmp->pcibus = tmp->bus->number;
 
1596
        qoztmp->pcidevfn = tmp->devfn; 
 
1597
 
 
1598
        if (!tmp->irq) {
 
1599
            printk(KERN_WARNING "qozap: no irq!\n");
 
1600
        } else {
 
1601
            qoztmp->irq = tmp->irq;
 
1602
        }
 
1603
 
 
1604
        qoztmp->pci_io_phys = (char *) tmp->resource[1].start;
 
1605
        if (!qoztmp->pci_io_phys) {
 
1606
            printk(KERN_WARNING "qozap: no iomem!\n");
 
1607
            pci_disable_device(tmp);
 
1608
            multi_qoz = NULL;
 
1609
            return -EIO;
 
1610
        }
 
1611
 
 
1612
        qoztmp->ioport = tmp->resource[0].start;
 
1613
        if (!qoztmp->ioport) {
 
1614
            printk(KERN_WARNING "qozap: no ioport!\n");
 
1615
            pci_disable_device(tmp);
 
1616
            multi_qoz = NULL;
 
1617
            return -EIO;
 
1618
        }
 
1619
        if (!request_region(qoztmp->ioport, 8, "qozap")) {
 
1620
            printk(KERN_WARNING "qozap: couldnt request io range!\n");
 
1621
            pci_disable_device(tmp);
 
1622
            multi_qoz = NULL;
 
1623
            return -EIO;
 
1624
        }
 
1625
        if (!request_mem_region((unsigned long) qoztmp->pci_io_phys, 256, "qozap")) {
 
1626
            printk(KERN_WARNING "qozap: couldnt request io mem range!\n");
 
1627
            release_region(qoztmp->ioport, 8);
 
1628
            pci_disable_device(tmp);
 
1629
            multi_qoz = NULL;
 
1630
            return -EIO;
 
1631
        }
 
1632
        
 
1633
        if (request_irq(qoztmp->irq, qoz_interrupt, ZAP_IRQ_SHARED, "qozap", qoztmp)) {
 
1634
            printk(KERN_WARNING "qozap: unable to register irq\n");
 
1635
            kfree(qoztmp);
 
1636
            pci_disable_device(tmp);
 
1637
            multi_qoz = NULL;
 
1638
            return -EIO;
 
1639
        }
 
1640
 
 
1641
        qoztmp->pci_io = ioremap((ulong) qoztmp->pci_io_phys, 256);
 
1642
        
 
1643
        pci_write_config_word(qoztmp->pcidev, PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
 
1644
 
 
1645
        qoz_outb(qoztmp,qoz_R_IRQ_CTRL, 0); 
 
1646
 
 
1647
        qoztmp->type = tmp->subsystem_device;
 
1648
        qoz_resetCard(qoztmp);
 
1649
        
 
1650
 
 
1651
        if ((tmp->subsystem_device==0xb520) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1652
        //    printk(KERN_INFO "MODES = %#x.\n",modes);
 
1653
            qoz_outb(qoztmp,qoz_R_GPIO_SEL,0x80 | 0x40);
 
1654
            qoz_outb(qoztmp,qoz_R_GPIO_EN1,0x00);
 
1655
            dips = (qoz_inb(qoztmp,qoz_R_GPIO_IN1) >> 5);
 
1656
            cid = 7;
 
1657
            for (i=0;i<3;i++) {
 
1658
                if ((dips & (1 << i)) != 0) {
 
1659
                cid -= (1 << (2-i));
 
1660
                }
 
1661
            }
 
1662
        //      printk(KERN_INFO "DIPS = %#x CID= %#x\n",dips,cid);
 
1663
        } else if ((tmp->subsystem_device==0xb550) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1664
                /* quadBRI 2.0 */
 
1665
        //    printk(KERN_INFO "MODES = %#x.\n",modes);
 
1666
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xF0);
 
1667
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1668
                qoz_outb(qoztmp,qoz_R_GPIO_EN1,0x00);
 
1669
//              dips = qoz_inb(qoztmp,qoz_R_GPIO_IN1) >> 5;
 
1670
                dips = ((qoz_inb(qoztmp,qoz_R_GPI_IN3) & 80) >> 1) | ((qoz_inb(qoztmp,qoz_R_GPI_IN3) & 8) << 2) | qoz_inb(qoztmp,qoz_R_GPIO_IN1);
 
1671
 
 
1672
                cid = 0;
 
1673
                for (i=0;i<3;i++) {
 
1674
                    if ((dips & (1 << i)) == 0) {
 
1675
                        cid += (1 << i);
 
1676
                    }
 
1677
                }
 
1678
 
 
1679
        } else if ((tmp->subsystem_device==0xb556) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1680
                /* duoBRI */
 
1681
 
 
1682
                /* gpi27 1 gpi23 2 */
 
1683
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xf0);
 
1684
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1685
                qoz_outb(qoztmp,qoz_R_GPIO_EN1,0x00);
 
1686
 
 
1687
/*              dips = qoz_inb_io(qoztmp,0x4000);
 
1688
                dips = qoz_inb_io(qoztmp,0x4000);
 
1689
                dips = qoz_inb_io(qoztmp,0x4000);
 
1690
                dips = qoz_inb_io(qoztmp,0x4000);
 
1691
 
 
1692
                qoz_outw_io(qoztmp,0x0,0x0);  */
 
1693
 
 
1694
 
 
1695
        } else if ((tmp->subsystem_device==0xb558) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1696
                /* quadBRI minipCI */
 
1697
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xF0);
 
1698
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1699
                qoz_outb(qoztmp,qoz_R_GPIO_EN1,0x00);
 
1700
                dips = qoz_inb(qoztmp,qoz_R_GPIO_IN1) >> 5;
 
1701
/*          printk(KERN_INFO "gpio_in1 %#x\n", qoz_inb(qoztmp,qoz_R_GPIO_IN1));
 
1702
 
 
1703
            printk(KERN_INFO "gpi_in2 %#x\n", qoz_inb(qoztmp,qoz_R_GPI_IN2));
 
1704
            printk(KERN_INFO "dip1 %#x\n", (qoz_inb(qoztmp,qoz_R_GPI_IN3) & 80) >> 7);
 
1705
            printk(KERN_INFO "gpi_in3 %#x\n", qoz_inb(qoztmp,qoz_R_GPI_IN3));
 
1706
            printk(KERN_INFO "dip2 %#x\n", (qoz_inb(qoztmp,qoz_R_GPI_IN3) & 8) >> 3); >*/
 
1707
                cid = 0;
 
1708
                for (i=0;i<3;i++) {
 
1709
                    if ((dips & (1 << i)) != 0) {
 
1710
                        cid += (1 << i);
 
1711
                    }
 
1712
                }
 
1713
//          printk(KERN_INFO "DIPS = %#x CID= %#x\n",dips,cid);
 
1714
 
 
1715
        } else if ((tmp->subsystem_device==0xb55b) && (pcidid == PCI_DEVICE_ID_CCD_M)){
 
1716
                qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x21); 
 
1717
 
 
1718
                dips = ~qoz_inb_io(qoztmp,0x4000);
 
1719
                dips = ~qoz_inb_io(qoztmp,0x4000);
 
1720
                dips = ~qoz_inb_io(qoztmp,0x4000);
 
1721
                dips = ~qoz_inb_io(qoztmp,0x4000);
 
1722
 
 
1723
                dips &= 0x3F;
 
1724
                qoz_outw_io(qoztmp,0x0,0x0); 
 
1725
 
 
1726
                cid = 0;
 
1727
                for (i=0;i<6;i++) {
 
1728
                    if ((dips & (1 << i)) != 0) {
 
1729
                        cid += (1 << i);
 
1730
                    }
 
1731
                }
 
1732
                qoz_outb(qoztmp,qoz_R_BRG_PCM_CFG,0x20); 
 
1733
                printk(KERN_CRIT "dips = %#x cid = %d\n", dips, cid);
 
1734
        } else if (((tmp->subsystem_device==0xb752) || (tmp->subsystem_device==0xb751)) && (pcidid == PCI_DEVICE_ID_CCD_M4)){
 
1735
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xF0);
 
1736
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1737
                qoz_outb(qoztmp,qoz_R_GPIO_EN1,0x00);
 
1738
//              dips = qoz_inb(qoztmp,qoz_R_GPIO_IN1) >> 5;
 
1739
                dips = ((qoz_inb(qoztmp,qoz_R_GPI_IN3) & 80) >> 1) | ((qoz_inb(qoztmp,qoz_R_GPI_IN3) & 8) << 2) | qoz_inb(qoztmp,qoz_R_GPIO_IN1);
 
1740
 
 
1741
                cid = 0;
 
1742
                for (i=0;i<3;i++) {
 
1743
                    if ((dips & (1 << i)) == 0) {
 
1744
                        cid += (1 << i);
 
1745
                    }
 
1746
                }
 
1747
                printk(KERN_CRIT "dips = %#x cid = %d\n", dips, cid);
 
1748
        } else {
 
1749
            cid = 0xff;
 
1750
        }
 
1751
 
 
1752
        if (ports == -1) {
 
1753
            if ((tmp->subsystem_device==0xb520) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1754
                modes = qoz_inb(qoztmp,qoz_R_GPI_IN3) >> 4;
 
1755
            } else if ((tmp->subsystem_device==0xb550) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1756
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xf0);
 
1757
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1758
                if (debug) {
 
1759
                    printk(KERN_CRIT "gpio_in0 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN0));
 
1760
                    printk(KERN_CRIT "gpio_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN1));
 
1761
                    printk(KERN_CRIT "gpi_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN1));
 
1762
                    printk(KERN_CRIT "gpi_in2 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN2));
 
1763
                    printk(KERN_CRIT "gpi_in3 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN3));
 
1764
                }
 
1765
                modes = qoz_inb(qoztmp,qoz_R_GPI_IN3) >> 4;         
 
1766
            } else if ((tmp->subsystem_device==0xb558) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1767
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xf0);
 
1768
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1769
                if (debug) {
 
1770
                    printk(KERN_CRIT "gpio_in0 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN0));
 
1771
                    printk(KERN_CRIT "gpio_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN1));
 
1772
                    printk(KERN_CRIT "gpi_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN1));
 
1773
                    printk(KERN_CRIT "gpi_in2 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN2));
 
1774
                    printk(KERN_CRIT "gpi_in3 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN3));
 
1775
                }
 
1776
                modes = qoz_inb(qoztmp,qoz_R_GPI_IN3) >> 4;         
 
1777
            } else if ((tmp->subsystem_device==0xb556) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1778
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xf0);
 
1779
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1780
                if (debug) {
 
1781
                    printk(KERN_CRIT "gpio_in0 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN0));
 
1782
                    printk(KERN_CRIT "gpio_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN1));
 
1783
                    printk(KERN_CRIT "gpi_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN1));
 
1784
                    printk(KERN_CRIT "gpi_in2 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN2));
 
1785
                    printk(KERN_CRIT "gpi_in3 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN3));
 
1786
                }
 
1787
                modes = qoz_inb(qoztmp,qoz_R_GPI_IN3) >> 4;
 
1788
            } else if ((tmp->subsystem_device==0xb751) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1789
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xf0);
 
1790
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1791
                if (debug) {
 
1792
                    printk(KERN_CRIT "gpio_in0 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN0));
 
1793
                    printk(KERN_CRIT "gpio_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN1));
 
1794
                    printk(KERN_CRIT "gpi_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN1));
 
1795
                    printk(KERN_CRIT "gpi_in2 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN2));
 
1796
                    printk(KERN_CRIT "gpi_in3 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN3));
 
1797
                }
 
1798
                modes = qoz_inb(qoztmp,qoz_R_GPI_IN3) >> 4;         
 
1799
            } else if ((tmp->subsystem_device==0xb752) && (pcidid == PCI_DEVICE_ID_CCD_M4)) {
 
1800
                qoz_outb(qoztmp,qoz_R_GPIO_SEL,0xf0);
 
1801
                qoz_outb(qoztmp,qoz_R_GPIO_EN0,0x00);
 
1802
                if (debug) {
 
1803
                    printk(KERN_CRIT "gpio_in0 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN0));
 
1804
                    printk(KERN_CRIT "gpio_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPIO_IN1));
 
1805
                    printk(KERN_CRIT "gpi_in1 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN1));
 
1806
                    printk(KERN_CRIT "gpi_in2 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN2));
 
1807
                    printk(KERN_CRIT "gpi_in3 %#x \n", qoz_inb(qoztmp,qoz_R_GPI_IN3));
 
1808
                }
 
1809
                modes = qoz_inb(qoztmp,qoz_R_GPI_IN3) >> 4;         
 
1810
            } else {
 
1811
                modes = 0; // assume TE mode
 
1812
            }
 
1813
        } else {
 
1814
            modes = ports >> totalBRIs;
 
1815
        }
 
1816
 
 
1817
        if (pcidid == PCI_DEVICE_ID_CCD_M4) {
 
1818
            switch (tmp->subsystem_device) {
 
1819
                case 0x08b4:
 
1820
                        if (ports == -1) ports = 0; /* assume TE mode if no ports param */
 
1821
                        printk(KERN_INFO
 
1822
                        "qozap: CologneChip HFC-4S evaluation board configured at io port %#x IRQ %d HZ %d\n",
 
1823
                          (u_int) qoztmp->ioport,
 
1824
                        qoztmp->irq, HZ);
 
1825
                        totalBRIs += 4;
 
1826
                    break;
 
1827
                case 0xb520:
 
1828
                        printk(KERN_INFO
 
1829
                        "qozap: Junghanns.NET quadBRI card configured at io port %#x IRQ %d HZ %d CardID %d\n",
 
1830
                          (u_int) qoztmp->ioport,
 
1831
                        qoztmp->irq, HZ, cid);
 
1832
                        totalBRIs += 4;
 
1833
                    break;
 
1834
                case 0xb550:
 
1835
                        printk(KERN_INFO
 
1836
                        "qozap: Junghanns.NET quadBRI (Version 2.0) card configured at io port %#x IRQ %d HZ %d CardID %d\n",
 
1837
                          (u_int) qoztmp->ioport,
 
1838
                        qoztmp->irq, HZ, cid);
 
1839
                        totalBRIs += 4;
 
1840
                    break;
 
1841
                case 0xb556:
 
1842
                        printk(KERN_INFO
 
1843
                        "qozap: Junghanns.NET duoBRI card configured at io port %#x IRQ %d HZ %d CardID %d\n",
 
1844
                          (u_int) qoztmp->ioport,
 
1845
                        qoztmp->irq, HZ, cid);
 
1846
                        totalBRIs += 2;
 
1847
                    break;
 
1848
                case 0xb559:
 
1849
                        printk(KERN_INFO
 
1850
                        "qozap: Junghanns.NET duoBRI miniPCI card configured at io port %#x IRQ %d HZ %d CardID %d\n",
 
1851
                          (u_int) qoztmp->ioport,
 
1852
                        qoztmp->irq, HZ, cid);
 
1853
                        totalBRIs += 2;
 
1854
                    break;
 
1855
                case 0xb751:
 
1856
                        printk(KERN_INFO
 
1857
                        "qozap: Junghanns.NET duoBRI PCIe card configured at io port %#x IRQ %d HZ %d CardID %d\n",
 
1858
                          (u_int) qoztmp->ioport,
 
1859
                        qoztmp->irq, HZ, cid);
 
1860
                        totalBRIs += 2;
 
1861
                    break;
 
1862
                case 0xb558:
 
1863
                        printk(KERN_INFO
 
1864
                        "qozap: Junghanns.NET quadBRI miniPCI card configured at io port %#x IRQ %d HZ %d CardID %d\n",
 
1865
                          (u_int) qoztmp->ioport,
 
1866
                        qoztmp->irq, HZ, cid);
 
1867
                        totalBRIs += 4;
 
1868
                    break;
 
1869
                case 0xb752:
 
1870
                        printk(KERN_INFO
 
1871
                        "qozap: Junghanns.NET quadBRI PCIe card configured at io port %#x IRQ %d HZ %d CardID %d\n",
 
1872
                          (u_int) qoztmp->ioport,
 
1873
                        qoztmp->irq, HZ, cid);
 
1874
                        totalBRIs += 4;
 
1875
                    break;
 
1876
            } 
 
1877
        } else {
 
1878
            switch (tmp->subsystem_device) {
 
1879
                case 0xb552:
 
1880
                    printk(KERN_INFO
 
1881
                       "qozap: Junghanns.NET octoBRI card configured at io port %#x IRQ %d HZ %d\n",
 
1882
                       (u_int) qoztmp->ioport,
 
1883
                       qoztmp->irq, HZ);
 
1884
                break;
 
1885
                case 0xb55b:
 
1886
                    printk(KERN_INFO
 
1887
                       "qozap: Junghanns.NET octoBRI (Version 2.0) card configured at io port %#x IRQ %d HZ %d\n",
 
1888
                       (u_int) qoztmp->ioport,
 
1889
                       qoztmp->irq, HZ);
 
1890
                break;
 
1891
                default:
 
1892
                    printk(KERN_INFO
 
1893
                       "qozap: wtf\n");
 
1894
                    if (qoztmp->pcidev != NULL) {
 
1895
                        pci_disable_device(qoztmp->pcidev);
 
1896
                    }
 
1897
                    pci_write_config_word(qoztmp->pcidev, PCI_COMMAND, 0);      
 
1898
                    free_irq(qoztmp->irq,qoztmp);
 
1899
                    kfree(qoztmp);
 
1900
                    qoztmp = NULL;
 
1901
                    tmp = pci_get_device(PCI_VENDOR_ID_CCD,pcidid,multi_qoz);
 
1902
                    continue;
 
1903
                break;          
 
1904
            } 
 
1905
            totalBRIs += 8;
 
1906
        }
 
1907
 
 
1908
        qoztmp->cardID = cid;
 
1909
        qoztmp->type = tmp->subsystem_device;
 
1910
 
 
1911
        printk(KERN_INFO "qozap: S/T ports: %d [",qoztmp->stports);
 
1912
        for (i=0;i<qoztmp->stports;i++) {
 
1913
            qoztmp->st_sync[i] = 0;
 
1914
            if ((modes & (1 << i)) != 0) {
 
1915
                qoztmp->st[i].nt_mode = 1;
 
1916
                printk(" NT");
 
1917
            } else {
 
1918
                qoztmp->st[i].nt_mode = 0;
 
1919
                printk(" TE");
 
1920
            }
 
1921
        }
 
1922
        printk(" ]\n");
 
1923
        
 
1924
        qoz_registerCard(qoztmp);
 
1925
        tmp = pci_get_device(PCI_VENDOR_ID_CCD,pcidid,multi_qoz);
 
1926
    }
 
1927
    return 0;
 
1928
}
 
1929
 
 
1930
 
 
1931
int qoz_sortCards(void) {
 
1932
    int changed=0,tmpcardno;
 
1933
    struct qoz_card *tmpcard,*tmpcard2;
 
1934
    spin_lock(&registerlock);
 
1935
    do {
 
1936
        changed = 0;
 
1937
        tmpcard = qoz_dev_list;
 
1938
        while (tmpcard != NULL) {
 
1939
            if (tmpcard->prev) {
 
1940
                if (tmpcard->prev->cardID > tmpcard->cardID) {
 
1941
                    tmpcardno = tmpcard->prev->cardno;
 
1942
                    tmpcard->prev->cardno = tmpcard->cardno; 
 
1943
                    tmpcard->cardno = tmpcardno;
 
1944
                
 
1945
                    tmpcard2 = tmpcard->prev;
 
1946
                    if (tmpcard2->prev) {
 
1947
                        tmpcard2->prev->next = tmpcard;
 
1948
                    } else {
 
1949
                        qoz_dev_list = tmpcard;
 
1950
                    }
 
1951
                    if (tmpcard->next) {
 
1952
                        tmpcard->next->prev = tmpcard2;
 
1953
                    } 
 
1954
                    tmpcard2->next = tmpcard->next;
 
1955
                    tmpcard->prev = tmpcard2->prev;
 
1956
                    tmpcard->next = tmpcard2;
 
1957
                    tmpcard2->prev = tmpcard;
 
1958
                    changed = 1;
 
1959
                    tmpcard = tmpcard2;
 
1960
                }
 
1961
            }
 
1962
            tmpcard = tmpcard->next;
 
1963
        }
 
1964
    } while (changed == 1);
 
1965
    spin_unlock(&registerlock);
 
1966
    return 0;
 
1967
}
 
1968
 
 
1969
int qoz_zapCards(void) {
 
1970
    struct qoz_card *tmpcard;
 
1971
    tmpcard = qoz_dev_list;
 
1972
    while (tmpcard != NULL) {
 
1973
        ztqoz_initialize(tmpcard);
 
1974
        tmpcard = tmpcard->next;
 
1975
    }
 
1976
    return 0;
 
1977
}
 
1978
 
 
1979
 
 
1980
int init_module(void) {
 
1981
    multi_qoz = NULL;
 
1982
    qoz_findCards(PCI_DEVICE_ID_CCD_M4);
 
1983
    multi_qoz = NULL;
 
1984
    qoz_findCards(PCI_DEVICE_ID_CCD_M);
 
1985
    if (sort) {
 
1986
        qoz_sortCards();
 
1987
    }
 
1988
    qoz_zapCards();
 
1989
    if (qoz_dev_count == 0) {
 
1990
        printk(KERN_INFO "qozap: no multiBRI cards found.\n");
 
1991
    } else {
 
1992
        printk(KERN_INFO "qozap: %d multiBRI card(s) in this box, %d BRI ports total, bloop %d, pcmslave %d, dacs %d.\n",qoz_dev_count, totalBRIs, bloop, pcmslave, dacs);
 
1993
    }
 
1994
    return 0;
 
1995
}
 
1996
 
 
1997
void cleanup_module(void) {
 
1998
    struct qoz_card *tmpcard,*tmplist;
 
1999
    int i=0;
 
2000
    tmplist = qoz_dev_list;
 
2001
    while (tmplist != NULL) {
 
2002
        tmplist->dead = 1;
 
2003
        qoz_resetCard(tmplist);
 
2004
        qoz_undoWD(tmplist);
 
2005
        qoz_shutdownCard(tmplist);
 
2006
        tmplist = tmplist->next;
 
2007
    }
 
2008
    tmplist = qoz_dev_list;
 
2009
    spin_lock(&registerlock);
 
2010
    while (tmplist != NULL) {
 
2011
        tmpcard = tmplist->next;
 
2012
        kfree(tmplist);
 
2013
        i++;
 
2014
        tmplist = tmpcard;
 
2015
    }
 
2016
    spin_unlock(&registerlock);
 
2017
    printk(KERN_INFO "qozap: shutdown %d multiBRI cards.\n", i);
 
2018
}
 
2019
#endif
 
2020
 
 
2021
#ifdef LINUX26
 
2022
module_param(doubleclock, int, 0600);
 
2023
module_param(ports, int, 0600);
 
2024
module_param(pcmslave, int, 0600);
 
2025
module_param(bloop, int, 0600);
 
2026
module_param(debug, int, 0600);
 
2027
module_param(dacs, int, 0600);
 
2028
module_param(sort, int, 0600);
 
2029
#else
 
2030
MODULE_PARM(doubleclock,"i");
 
2031
MODULE_PARM(ports,"i");
 
2032
MODULE_PARM(pcmslave,"i");
 
2033
MODULE_PARM(bloop,"i");
 
2034
MODULE_PARM(debug,"i");
 
2035
MODULE_PARM(dacs,"i");
 
2036
MODULE_PARM(sort,"i");
 
2037
#endif
 
2038
 
 
2039
 
 
2040
MODULE_DESCRIPTION("quad/octo BRI zaptel driver");
 
2041
MODULE_AUTHOR("Klaus-Peter Junghanns <kpj@junghanns.net>");
 
2042
#ifdef MODULE_LICENSE
 
2043
MODULE_LICENSE("GPL");
 
2044
#endif