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

« back to all changes in this revision

Viewing changes to kernel/wcte12xp/base.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
 * Digium, Inc.  Wildcard TE12xP T1/E1 card Driver
 
3
 *
 
4
 * Written by Michael Spiceland <mspiceland@digium.com>
 
5
 *
 
6
 * Adapted from the wctdm24xxp and wcte11xp drivers originally
 
7
 * written by Mark Spencer <markster@digium.com>
 
8
 *            Matthew Fredrickson <creslin@digium.com>
 
9
 *            William Meadows <wmeadows@digium.com>
 
10
 *
 
11
 * Copyright (C) 2007-2008, Digium, Inc.
 
12
 *
 
13
 * All rights reserved.
 
14
 *
 
15
 * This program is free software; you can redistribute it and/or modify
 
16
 * it under the terms of the GNU General Public License as published by
 
17
 * the Free Software Foundation; either version 2 of the License, or
 
18
 * (at your option) any later version.
 
19
 * 
 
20
 * This program is distributed in the hope that it will be useful,
 
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 * GNU General Public License for more details.
 
24
 * 
 
25
 * You should have received a copy of the GNU General Public License
 
26
 * along with this program; if not, write to the Free Software
 
27
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 
28
 *
 
29
 */
 
30
 
 
31
#include <linux/kernel.h>
 
32
#include <linux/errno.h>
 
33
#include <linux/module.h>
 
34
#include <linux/init.h>
 
35
#include <linux/errno.h>
 
36
#include <linux/pci.h>
 
37
#include <linux/proc_fs.h>
 
38
 
 
39
#ifdef LINUX26
 
40
#include <linux/moduleparam.h>
 
41
#endif
 
42
 
 
43
#include "zaptel.h"
 
44
 
 
45
#include "../wct4xxp/wct4xxp.h" /* For certain definitions */
 
46
 
 
47
#include "../voicebus.h"
 
48
#include "wcte12xp.h"
 
49
 
 
50
#if defined(VPM_SUPPORT)
 
51
#include "vpmadt032.h"
 
52
#include "GpakApi.h"
 
53
#endif
 
54
 
 
55
struct pci_driver te12xp_driver;
 
56
 
 
57
static int chanmap_t1[] = 
 
58
{ 2,1,0,
 
59
  6,5,4,
 
60
  10,9,8,
 
61
  14,13,12,
 
62
  18,17,16,
 
63
  22,21,20,
 
64
  26,25,24,
 
65
  30,29,28 };
 
66
 
 
67
static int chanmap_e1[] = 
 
68
{ 2,1,0,
 
69
  7,6,5,4,
 
70
  11,10,9,8,
 
71
  15,14,13,12,
 
72
  19,18,17,16,
 
73
  23,22,21,20,
 
74
  27,26,25,24,
 
75
  31,30,29,28 };
 
76
 
 
77
static int chanmap_e1uc[] =
 
78
{ 3,2,1,0,
 
79
  7,6,5,4,
 
80
  11,10,9,8,
 
81
  15,14,13,12,
 
82
  19,18,17,16,
 
83
  23,22,21,20,
 
84
  27,26,25,24,
 
85
  31,30,29,28 };
 
86
 
 
87
int debug = 0;
 
88
static int j1mode = 0;
 
89
static int alarmdebounce = 0;
 
90
static int loopback = 0;
 
91
static int t1e1override = -1;
 
92
static int unchannelized = 0;
 
93
static int latency = VOICEBUS_DEFAULT_LATENCY;
 
94
#ifdef VPM_SUPPORT
 
95
int vpmsupport = 1;
 
96
int vpmdtmfsupport = 0;
 
97
int vpmtsisupport = 0;
 
98
int vpmnlptype = 1;
 
99
int vpmnlpthresh = 24;
 
100
int vpmnlpmaxsupp = 0;
 
101
#endif
 
102
 
 
103
struct t1 *ifaces[WC_MAX_IFACES];
 
104
spinlock_t ifacelock = SPIN_LOCK_UNLOCKED;
 
105
 
 
106
struct t1_desc {
 
107
        char *name;
 
108
        int flags;
 
109
};
 
110
 
 
111
static struct t1_desc te120p = { "Wildcard TE120P", 0 };
 
112
static struct t1_desc te122 = { "Wildcard TE122", 0 };
 
113
static struct t1_desc te121 = { "Wildcard TE121", 0 };
 
114
 
 
115
int schluffen(wait_queue_head_t *q)
 
116
{
 
117
        DECLARE_WAITQUEUE(wait, current);
 
118
        add_wait_queue(q, &wait);
 
119
        current->state = TASK_INTERRUPTIBLE;
 
120
        if (!signal_pending(current)) schedule();
 
121
        current->state = TASK_RUNNING;
 
122
        remove_wait_queue(q, &wait);
 
123
        if (signal_pending(current)) return -ERESTARTSYS;
 
124
        return(0);
 
125
}
 
126
 
 
127
static inline int empty_slot(struct t1 *wc)
 
128
{
 
129
        unsigned int x;
 
130
 
 
131
        for (x = 0; x < sizeof(wc->cmdq.cmds) / sizeof(wc->cmdq.cmds[0]); x++) {
 
132
                if (!wc->cmdq.cmds[x].flags && !wc->cmdq.cmds[x].address)
 
133
                        return x;
 
134
        }
 
135
        return -1;
 
136
}
 
137
 
 
138
static inline void cmd_dequeue(struct t1 *wc, volatile unsigned char *writechunk, int eframe, int slot)
 
139
{
 
140
        struct command *curcmd=NULL;
 
141
        unsigned int x;
 
142
 
 
143
        /* Skip audio */
 
144
        writechunk += 66;
 
145
        /* Search for something waiting to transmit */
 
146
        if ((slot < 6) && (eframe) && (eframe < ZT_CHUNKSIZE - 1)) { 
 
147
                /* only 6 useable cs slots per */
 
148
 
 
149
                /* framer */
 
150
                for (x = 0; x < sizeof(wc->cmdq.cmds) / sizeof(wc->cmdq.cmds[0]); x++) {
 
151
                        if ((wc->cmdq.cmds[x].flags & (__CMD_RD | __CMD_WR | __CMD_LEDS | __CMD_PINS)) && 
 
152
                            !(wc->cmdq.cmds[x].flags & (__CMD_TX | __CMD_FIN))) {
 
153
                                curcmd = &wc->cmdq.cmds[x];
 
154
                                wc->cmdq.cmds[x].flags |= __CMD_TX;
 
155
                                wc->cmdq.cmds[x].ident = wc->txident;
 
156
                                break;
 
157
                        }
 
158
                }
 
159
                if (!curcmd) {
 
160
                        curcmd = &wc->dummy;
 
161
                        /* If nothing else, use filler */
 
162
                        curcmd->address = 0x4a;
 
163
                        curcmd->data = 0x00;
 
164
                        curcmd->flags = __CMD_RD;
 
165
                }
 
166
                curcmd->cs_slot = slot; 
 
167
                if (curcmd->flags & __CMD_WR)
 
168
                        writechunk[CMD_BYTE(slot,0,0)] = 0x0c; /* 0c write command */
 
169
                else if (curcmd->flags & __CMD_LEDS)
 
170
                        writechunk[CMD_BYTE(slot,0,0)] = 0x10 | ((curcmd->address) & 0x0E); /* led set command */ 
 
171
                else if (curcmd->flags & __CMD_PINS)
 
172
                        writechunk[CMD_BYTE(slot,0,0)] = 0x30; /* CPLD2 pin state */
 
173
                else
 
174
                        writechunk[CMD_BYTE(slot,0,0)] = 0x0a; /* read command */ 
 
175
                writechunk[CMD_BYTE(slot,1,0)] = curcmd->address;
 
176
                writechunk[CMD_BYTE(slot,2,0)] = curcmd->data;
 
177
        } 
 
178
 
 
179
}
 
180
 
 
181
static inline void cmd_decipher(struct t1 *wc, volatile unsigned char *readchunk)
 
182
{
 
183
        unsigned char ident, cs_slot;
 
184
        unsigned int x;
 
185
        unsigned int is_vpm = 0;
 
186
 
 
187
        /* Skip audio */
 
188
        readchunk += 66;
 
189
        /* Search for any pending results */
 
190
        for (x = 0; x < sizeof(wc->cmdq.cmds) / sizeof(wc->cmdq.cmds[0]); x++) {
 
191
                if ((wc->cmdq.cmds[x].flags & (__CMD_RD | __CMD_WR | __CMD_LEDS | __CMD_PINS)) && 
 
192
                    (wc->cmdq.cmds[x].flags & (__CMD_TX)) && 
 
193
                    !(wc->cmdq.cmds[x].flags & (__CMD_FIN))) {
 
194
                        ident = wc->cmdq.cmds[x].ident;
 
195
                        cs_slot = wc->cmdq.cmds[x].cs_slot;
 
196
 
 
197
                        if (ident == wc->rxident) {
 
198
                                /* Store result */
 
199
                                wc->cmdq.cmds[x].data |= readchunk[CMD_BYTE(cs_slot,2,is_vpm)];
 
200
                                /*printk("answer in rxident=%d cs_slot=%d is %d CMD_BYTE=%d jiffies=%d\n", ident, cs_slot, last_read_command, CMD_BYTE(cs_slot, 2), jiffies); */
 
201
                                wc->cmdq.cmds[x].flags |= __CMD_FIN;
 
202
                                if (wc->cmdq.cmds[x].flags & (__CMD_WR | __CMD_LEDS))
 
203
                                        /* clear out writes (and leds) since they need no ack */
 
204
                                        memset(&wc->cmdq.cmds[x], 0, sizeof(wc->cmdq.cmds[x]));
 
205
                        }
 
206
                }
 
207
        }
 
208
}
 
209
 
 
210
static inline int t1_setreg_full(struct t1 *wc, int addr, int val, int inisr, int vpm_num)
 
211
{
 
212
        unsigned long flags;
 
213
        int hit;
 
214
        int ret;
 
215
 
 
216
 
 
217
        do {
 
218
                if (!inisr)
 
219
                        spin_lock_irqsave(&wc->reglock, flags);
 
220
                hit = empty_slot(wc);
 
221
                if (hit > -1) {
 
222
                        wc->cmdq.cmds[hit].address = addr;
 
223
                        wc->cmdq.cmds[hit].data = val;
 
224
                        wc->cmdq.cmds[hit].flags |= __CMD_WR;
 
225
                        if(vpm_num >= 0) {
 
226
                                wc->cmdq.cmds[hit].flags |= __CMD_VPM;
 
227
                                wc->cmdq.cmds[hit].vpm_num = vpm_num;
 
228
                        }
 
229
                }
 
230
                if (inisr)
 
231
                        break;
 
232
        
 
233
                spin_unlock_irqrestore(&wc->reglock, flags);
 
234
                if (hit < 0) {
 
235
                        if ((ret = schluffen(&wc->regq)))
 
236
                                return ret;
 
237
                }
 
238
        } while (hit < 0);
 
239
 
 
240
        return (hit > -1) ? 0 : -1;
 
241
}
 
242
 
 
243
static inline int t1_setreg(struct t1 *wc, int addr, int val)
 
244
{
 
245
        return t1_setreg_full(wc, addr, val, 0, NOT_VPM);
 
246
}
 
247
 
 
248
/***************************************************************************
 
249
 * clean_leftovers()
 
250
 *
 
251
 * Check for unconsumed isr register reads and clean them up.
 
252
 **************************************************************************/
 
253
static inline void clean_leftovers(struct t1 *wc)
 
254
{
 
255
        unsigned int x;
 
256
        int count = 0;
 
257
        
 
258
        /* find our requested command */
 
259
        for (x = 0; x < sizeof(wc->cmdq.cmds) / sizeof(wc->cmdq.cmds[0]); x++) {
 
260
                if ((wc->cmdq.cmds[x].flags & __CMD_RD) &&
 
261
                    (wc->cmdq.cmds[x].flags & __CMD_ISR) &&
 
262
                    !(wc->cmdq.cmds[x].flags & __CMD_FIN)) {
 
263
                        debug_printk(1,"leftover isr read! %d", count);
 
264
                        memset(&wc->cmdq.cmds[x], 0, sizeof(wc->cmdq.cmds[x]));
 
265
                }
 
266
        }
 
267
}
 
268
 
 
269
/********************************************************************
 
270
 * t1_getreg_isr()
 
271
 *
 
272
 * Called in interrupt context to retrieve a value already requested
 
273
 * by the normal t1_getreg().
 
274
 *******************************************************************/
 
275
static inline int t1_getreg_isr(struct t1 *wc, int addr)
 
276
{
 
277
        int hit=-1;
 
278
        int ret;
 
279
        unsigned int x;
 
280
        
 
281
        /* find our requested command */
 
282
        for (x = 0;x < sizeof(wc->cmdq.cmds) / sizeof(wc->cmdq.cmds[0]); x++) {
 
283
                if ((wc->cmdq.cmds[x].flags & __CMD_RD) &&
 
284
                    (wc->cmdq.cmds[x].address==addr)) 
 
285
                {
 
286
                        if (wc->cmdq.cmds[x].flags & __CMD_FIN) {
 
287
                                hit = x;
 
288
                                break;
 
289
                        }
 
290
                        else {
 
291
                                /* still in progress. */
 
292
                                return -1;
 
293
                        }
 
294
                }
 
295
        }
 
296
 
 
297
        if (hit < 0) {
 
298
                debug_printk(2, "t1_getreg_isr() no addr=%02x\n", addr);
 
299
                return -1; /* oops, couldn't find it */
 
300
        }
 
301
 
 
302
        ret = wc->cmdq.cmds[hit].data;
 
303
        memset(&wc->cmdq.cmds[hit], 0, sizeof(struct command));
 
304
 
 
305
        return ret;
 
306
}
 
307
 
 
308
static inline int t1_getreg_full(struct t1 *wc, int addr, int inisr, int vpm_num)
 
309
{
 
310
        unsigned long flags;
 
311
        int hit;
 
312
        int ret = 0;
 
313
 
 
314
        do {
 
315
                if (!inisr) {
 
316
                        spin_lock_irqsave(&wc->reglock, flags);
 
317
                }
 
318
                hit = empty_slot(wc);
 
319
                if (hit > -1) {
 
320
                        wc->cmdq.cmds[hit].address = addr;
 
321
                        wc->cmdq.cmds[hit].data = 0x00;
 
322
                        wc->cmdq.cmds[hit].flags |= __CMD_RD;
 
323
                        if(vpm_num >= 0) {
 
324
                                wc->cmdq.cmds[hit].flags |= __CMD_VPM;
 
325
                                wc->cmdq.cmds[hit].vpm_num = vpm_num;
 
326
                        }
 
327
                        if (inisr)
 
328
                                wc->cmdq.cmds[hit].flags |= __CMD_ISR;
 
329
                }
 
330
                if (inisr) /* must be requested in t1_getreg_isr() */
 
331
                        return (hit > -1) ? 0 : -1;
 
332
                else {
 
333
                        spin_unlock_irqrestore(&wc->reglock, flags);
 
334
                }
 
335
                if (hit < 0) {
 
336
                        if ((ret = schluffen(&wc->regq)))
 
337
                                return ret;
 
338
                }
 
339
        } while (hit < 0);
 
340
 
 
341
        do {
 
342
                spin_lock_irqsave(&wc->reglock, flags);
 
343
                if (wc->cmdq.cmds[hit].flags & __CMD_FIN) {
 
344
                        ret = wc->cmdq.cmds[hit].data;
 
345
                        memset(&wc->cmdq.cmds[hit], 0, sizeof(wc->cmdq.cmds[hit]));
 
346
                        hit = -1;
 
347
                }
 
348
                spin_unlock_irqrestore(&wc->reglock, flags);
 
349
                if (hit > -1) {
 
350
                        if ((ret = schluffen(&wc->regq)))
 
351
                                return ret;
 
352
                }
 
353
        } while (hit > -1);
 
354
 
 
355
        return ret;
 
356
}
 
357
 
 
358
static inline int t1_getreg(struct t1 *wc, int addr, int inisr)
 
359
{
 
360
        return t1_getreg_full(wc, addr, inisr, NOT_VPM);
 
361
}
 
362
 
 
363
static inline int t1_setleds(struct t1 *wc, int leds, int inisr)
 
364
{
 
365
        unsigned long flags;
 
366
        int hit;
 
367
        int ret = 0;
 
368
 
 
369
        leds = ~leds & 0x0E; /* invert the LED bits (3 downto 1)*/
 
370
 
 
371
        do {
 
372
                if (!inisr) {
 
373
                        spin_lock_irqsave(&wc->reglock, flags);
 
374
                }
 
375
                hit = empty_slot(wc);
 
376
                if (hit > -1) {
 
377
                        wc->cmdq.cmds[hit].flags |= __CMD_LEDS;
 
378
                        wc->cmdq.cmds[hit].address = leds;
 
379
                }
 
380
                if (inisr) {
 
381
                        break;
 
382
                } else {
 
383
                        spin_unlock_irqrestore(&wc->reglock, flags);
 
384
                }
 
385
                if (hit < 0) {
 
386
                        if ((ret = schluffen(&wc->regq)))
 
387
                                return ret;
 
388
                }
 
389
        } while (hit < 0);
 
390
 
 
391
        return (hit > -1) ? 0 : -1;
 
392
}
 
393
 
 
394
static inline int t1_getpins(struct t1 *wc, int inisr)
 
395
{
 
396
        unsigned long flags;
 
397
        int hit;
 
398
        int ret = 0;
 
399
 
 
400
        do {
 
401
                spin_lock_irqsave(&wc->reglock, flags);
 
402
                hit = empty_slot(wc);
 
403
                if (hit > -1) {
 
404
                        wc->cmdq.cmds[hit].address = 0x00;
 
405
                        wc->cmdq.cmds[hit].data = 0x00;
 
406
                        wc->cmdq.cmds[hit].flags |= __CMD_PINS;
 
407
                }
 
408
                spin_unlock_irqrestore(&wc->reglock, flags);
 
409
                if (inisr)
 
410
                        return (hit > -1) ? 0 : -1;
 
411
                if (hit < 0) {
 
412
                        if ((ret = schluffen(&wc->regq)))
 
413
                                return ret;
 
414
                }
 
415
        } while (hit < 0);
 
416
 
 
417
        do {
 
418
                spin_lock_irqsave(&wc->reglock, flags);
 
419
                if (wc->cmdq.cmds[hit].flags & __CMD_FIN) {
 
420
                        ret = wc->cmdq.cmds[hit].data;
 
421
                        memset(&wc->cmdq.cmds[hit], 0, sizeof(wc->cmdq.cmds[hit]));
 
422
                        hit = -1;
 
423
                }
 
424
                spin_unlock_irqrestore(&wc->reglock, flags);
 
425
                if (hit > -1) {
 
426
                        if ((ret = schluffen(&wc->regq)))
 
427
                                return ret;
 
428
                }
 
429
        } while (hit > -1);
 
430
 
 
431
        return ret;
 
432
}
 
433
 
 
434
static void __t1xxp_set_clear(struct t1 *wc, int channo)
 
435
{
 
436
        int i,j;
 
437
        int ret;
 
438
        unsigned short val=0;
 
439
        
 
440
        for (i = 0; i < 24; i++) {
 
441
                j = (i / 8);
 
442
                if (wc->span.chans[i].flags & ZT_FLAG_CLEAR) 
 
443
                        val |= 1 << (7 - (i % 8));
 
444
                if (((i % 8)==7) &&  /* write byte every 8 channels */
 
445
                    ((channo < 0) ||    /* channo=-1 means all channels */ 
 
446
                     (j == (channo-1)/8) )) { /* only the register for this channo */    
 
447
                        ret = t1_setreg_full(wc, 0x2f + j, val, 1, NOT_VPM);
 
448
                        if (ret < 0)
 
449
                                module_printk("set_clear failed for chan %d!\n",i); 
 
450
                        val = 0;
 
451
                }
 
452
        }
 
453
}
 
454
 
 
455
static void t1_release(struct t1 *wc)
 
456
{
 
457
        zt_unregister(&wc->span);
 
458
        kfree(wc);
 
459
        printk("Freed a Wildcard TE12xP\n");
 
460
}
 
461
 
 
462
static void t4_serial_setup(struct t1 *wc)
 
463
{
 
464
        module_printk("Setting up global serial parameters for %s\n", 
 
465
                      wc->spantype == TYPE_E1 ? (unchannelized ? "Unchannelized E1" : "E1") : "T1");
 
466
 
 
467
        t1_setreg(wc, 0x85, 0xe0);      /* GPC1: Multiplex mode enabled, FSC is output, active low, RCLK from channel 0 */
 
468
        t1_setreg(wc, 0x08, 0x05);      /* IPC: Interrupt push/pull active low */
 
469
 
 
470
        /* Global clocks (8.192 Mhz CLK) */
 
471
        t1_setreg(wc, 0x92, 0x00);      
 
472
        t1_setreg(wc, 0x93, 0x18);
 
473
        t1_setreg(wc, 0x94, 0xfb);
 
474
        t1_setreg(wc, 0x95, 0x0b);
 
475
        t1_setreg(wc, 0x96, 0x00);
 
476
        t1_setreg(wc, 0x97, 0x0b);
 
477
        t1_setreg(wc, 0x98, 0xdb);
 
478
        t1_setreg(wc, 0x99, 0xdf);
 
479
 
 
480
        /* Configure interrupts */      
 
481
        t1_setreg(wc, 0x46, 0xc0);      /* GCR: Interrupt on Activation/Deactivation of AIX, LOS */
 
482
 
 
483
        /* Configure system interface */
 
484
        t1_setreg(wc, 0x3e, 0x0a /* 0x02 */);   /* SIC1: 4.096 Mhz clock/bus, double buffer receive / transmit, byte interleaved */
 
485
        t1_setreg(wc, 0x3f, 0x00);      /* SIC2: No FFS, no center receive eliastic buffer, phase 0 */
 
486
        t1_setreg(wc, 0x40, 0x04);      /* SIC3: Edges for capture */
 
487
        t1_setreg(wc, 0x44, 0x30);      /* CMR1: RCLK is at 8.192 Mhz dejittered */
 
488
        t1_setreg(wc, 0x45, 0x00);      /* CMR2: We provide sync and clock for tx and rx. */
 
489
        t1_setreg(wc, 0x22, 0x00);      /* XC0: Normal operation of Sa-bits */
 
490
        t1_setreg(wc, 0x23, 0x04);      /* XC1: 0 offset */
 
491
        t1_setreg(wc, 0x24, 0x00);      /* RC0: Just shy of 255 */
 
492
        t1_setreg(wc, 0x25, 0x05);      /* RC1: The rest of RC0 */
 
493
        
 
494
        /* Configure ports */
 
495
        t1_setreg(wc, 0x80, 0x00);      /* PC1: SPYR/SPYX input on RPA/XPA */
 
496
        t1_setreg(wc, 0x81, 0x22);      /* PC2: RMFB/XSIG output/input on RPB/XPB */
 
497
        t1_setreg(wc, 0x82, 0x65);      /* PC3: Some unused stuff */
 
498
        t1_setreg(wc, 0x83, 0x35);      /* PC4: Some more unused stuff */
 
499
        t1_setreg(wc, 0x84, 0x31);      /* PC5: XMFS active low, SCLKR is input, RCLK is output */
 
500
        t1_setreg(wc, 0x86, 0x03);      /* PC6: CLK1 is Tx Clock output, CLK2 is 8.192 Mhz from DCO-R */
 
501
        t1_setreg(wc, 0x3b, 0x00);      /* Clear LCR1 */
 
502
}
 
503
 
 
504
static void t1_configure_t1(struct t1 *wc, int lineconfig, int txlevel)
 
505
{
 
506
        unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
 
507
        char *framing, *line;
 
508
        int mytxlevel;
 
509
 
 
510
        if ((txlevel > 7) || (txlevel < 4))
 
511
                mytxlevel = 0;
 
512
        else
 
513
                mytxlevel = txlevel - 4;
 
514
        fmr1 = 0x9e; /* FMR1: Mode 0, T1 mode, CRC on for ESF, 2.048 Mhz system data rate, no XAIS */
 
515
        fmr2 = 0x22; /* FMR2: no payload loopback, auto send yellow alarm */
 
516
        if (loopback)
 
517
                fmr2 |= 0x4;
 
518
 
 
519
        if (j1mode)
 
520
                fmr4 = 0x1c;
 
521
        else
 
522
                fmr4 = 0x0c; /* FMR4: Lose sync on 2 out of 5 framing bits, auto resync */
 
523
 
 
524
        lim2 = 0x21; /* LIM2: 50% peak is a "1", Advanced Loss recovery */
 
525
        lim2 |= (mytxlevel << 6);       /* LIM2: Add line buildout */
 
526
        t1_setreg(wc, 0x1d, fmr1);
 
527
        t1_setreg(wc, 0x1e, fmr2);
 
528
 
 
529
        /* Configure line interface */
 
530
        if (lineconfig & ZT_CONFIG_AMI) {
 
531
                line = "AMI";
 
532
                fmr0 = 0xa0;
 
533
        } else {
 
534
                line = "B8ZS";
 
535
                fmr0 = 0xf0;
 
536
        }
 
537
        if (lineconfig & ZT_CONFIG_D4) {
 
538
                framing = "D4";
 
539
        } else {
 
540
                framing = "ESF";
 
541
                fmr4 |= 0x2;
 
542
                fmr2 |= 0xc0;
 
543
        }
 
544
        t1_setreg(wc, 0x1c, fmr0);
 
545
 
 
546
        t1_setreg(wc, 0x20, fmr4);
 
547
        t1_setreg(wc, 0x21, 0x40);      /* FMR5: Enable RBS mode */
 
548
 
 
549
        t1_setreg(wc, 0x37, 0xf8);      /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
 
550
        t1_setreg(wc, 0x36, 0x08);      /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
 
551
 
 
552
        t1_setreg(wc, 0x02, 0x50);      /* CMDR: Reset the receiver and transmitter line interface */
 
553
        t1_setreg(wc, 0x02, 0x00);      /* CMDR: Reset the receiver and transmitter line interface */
 
554
 
 
555
        t1_setreg(wc, 0x3a, lim2);      /* LIM2: 50% peak amplitude is a "1" */
 
556
        t1_setreg(wc, 0x38, 0x0a);      /* PCD: LOS after 176 consecutive "zeros" */
 
557
        t1_setreg(wc, 0x39, 0x15);      /* PCR: 22 "ones" clear LOS */
 
558
 
 
559
        if (j1mode)
 
560
                t1_setreg(wc, 0x24, 0x80); /* J1 overide */
 
561
                
 
562
        /* Generate pulse mask for T1 */
 
563
        switch (mytxlevel) {
 
564
        case 3:
 
565
                t1_setreg(wc, 0x26, 0x07);      /* XPM0 */
 
566
                t1_setreg(wc, 0x27, 0x01);      /* XPM1 */
 
567
                t1_setreg(wc, 0x28, 0x00);      /* XPM2 */
 
568
                break;
 
569
        case 2:
 
570
                t1_setreg(wc, 0x26, 0x8c);      /* XPM0 */
 
571
                t1_setreg(wc, 0x27, 0x11);      /* XPM1 */
 
572
                t1_setreg(wc, 0x28, 0x01);      /* XPM2 */
 
573
                break;
 
574
        case 1:
 
575
                t1_setreg(wc, 0x26, 0x8c);      /* XPM0 */
 
576
                t1_setreg(wc, 0x27, 0x01);      /* XPM1 */
 
577
                t1_setreg(wc, 0x28, 0x00);      /* XPM2 */
 
578
                break;
 
579
        case 0:
 
580
        default:
 
581
                t1_setreg(wc, 0x26, 0xd7);      /* XPM0 */
 
582
                t1_setreg(wc, 0x27, 0x22);      /* XPM1 */
 
583
                t1_setreg(wc, 0x28, 0x01);      /* XPM2 */
 
584
                break;
 
585
        }
 
586
 
 
587
        module_printk("Span configured for %s/%s\n", framing, line);
 
588
}
 
589
 
 
590
static void t1_configure_e1(struct t1 *wc, int lineconfig)
 
591
{
 
592
        unsigned int fmr2, fmr1, fmr0;
 
593
        unsigned int cas = 0;
 
594
        char *crc4 = "";
 
595
        char *framing, *line;
 
596
 
 
597
        fmr1 = 0x46; /* FMR1: E1 mode, Automatic force resync, PCM30 mode, 8.192 Mhz backplane, no XAIS */
 
598
        fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of multiframe recovery, no payload loopback */
 
599
        if (unchannelized)
 
600
                fmr2 |= 0x30;
 
601
        if (loopback)
 
602
                fmr2 |= 0x4;
 
603
        if (lineconfig & ZT_CONFIG_CRC4) {
 
604
                fmr1 |= 0x08;   /* CRC4 transmit */
 
605
                fmr2 |= 0xc0;   /* CRC4 receive */
 
606
                crc4 = "/CRC4";
 
607
        }
 
608
        t1_setreg(wc, 0x1d, fmr1);
 
609
        t1_setreg(wc, 0x1e, fmr2);
 
610
 
 
611
        /* Configure line interface */
 
612
        if (lineconfig & ZT_CONFIG_AMI) {
 
613
                line = "AMI";
 
614
                fmr0 = 0xa0;
 
615
        } else {
 
616
                line = "HDB3";
 
617
                fmr0 = 0xf0;
 
618
        }
 
619
        if (lineconfig & ZT_CONFIG_CCS) {
 
620
                framing = "CCS";
 
621
        } else {
 
622
                framing = "CAS";
 
623
                cas = 0x40;
 
624
        }
 
625
        t1_setreg(wc, 0x1c, fmr0);
 
626
 
 
627
        if (unchannelized)
 
628
                t1_setreg(wc, 0x1f, 0x40);
 
629
 
 
630
        t1_setreg(wc, 0x37, 0xf0 /*| 0x6 */ );  /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
 
631
        t1_setreg(wc, 0x36, 0x08);      /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
 
632
 
 
633
        t1_setreg(wc, 0x02, 0x50);      /* CMDR: Reset the receiver and transmitter line interface */
 
634
        t1_setreg(wc, 0x02, 0x00);      /* CMDR: Reset the receiver and transmitter line interface */
 
635
 
 
636
        /* Condition receive line interface for E1 after reset */
 
637
        t1_setreg(wc, 0xbb, 0x17);
 
638
        t1_setreg(wc, 0xbc, 0x55);
 
639
        t1_setreg(wc, 0xbb, 0x97);
 
640
        t1_setreg(wc, 0xbb, 0x11);
 
641
        t1_setreg(wc, 0xbc, 0xaa);
 
642
        t1_setreg(wc, 0xbb, 0x91);
 
643
        t1_setreg(wc, 0xbb, 0x12);
 
644
        t1_setreg(wc, 0xbc, 0x55);
 
645
        t1_setreg(wc, 0xbb, 0x92);
 
646
        t1_setreg(wc, 0xbb, 0x0c);
 
647
        t1_setreg(wc, 0xbb, 0x00);
 
648
        t1_setreg(wc, 0xbb, 0x8c);
 
649
        
 
650
        t1_setreg(wc, 0x3a, 0x20);      /* LIM2: 50% peak amplitude is a "1" */
 
651
        t1_setreg(wc, 0x38, 0x0a);      /* PCD: LOS after 176 consecutive "zeros" */
 
652
        t1_setreg(wc, 0x39, 0x15);      /* PCR: 22 "ones" clear LOS */
 
653
        
 
654
        t1_setreg(wc, 0x20, 0x9f);      /* XSW: Spare bits all to 1 */
 
655
        if (unchannelized)
 
656
                t1_setreg(wc, 0x21, 0x3c);
 
657
        else
 
658
                t1_setreg(wc, 0x21, 0x1c|cas);  /* XSP: E-bit set when async. AXS auto, XSIF to 1 */
 
659
        
 
660
        
 
661
        /* Generate pulse mask for E1 */
 
662
        t1_setreg(wc, 0x26, 0x54);      /* XPM0 */
 
663
        t1_setreg(wc, 0x27, 0x02);      /* XPM1 */
 
664
        t1_setreg(wc, 0x28, 0x00);      /* XPM2 */
 
665
        module_printk("Span configured for %s/%s%s\n", framing, line, crc4);
 
666
}
 
667
 
 
668
static void t1xxp_framer_start(struct t1 *wc, struct zt_span *span)
 
669
{
 
670
        int alreadyrunning = wc->span.flags & ZT_FLAG_RUNNING;
 
671
        unsigned long flags;
 
672
 
 
673
        if (wc->spantype == TYPE_E1) { /* if this is an E1 card */
 
674
                t1_configure_e1(wc, span->lineconfig);
 
675
        } else { /* is a T1 card */
 
676
                t1_configure_t1(wc, span->lineconfig, span->txlevel);
 
677
                __t1xxp_set_clear(wc, -1);
 
678
        }
 
679
 
 
680
        spin_lock_irqsave(&wc->reglock, flags);
 
681
        if (!alreadyrunning) 
 
682
                wc->span.flags |= ZT_FLAG_RUNNING;
 
683
        spin_unlock_irqrestore(&wc->reglock, flags);
 
684
}
 
685
 
 
686
static int t1xxp_startup(struct zt_span *span)
 
687
{
 
688
        struct t1 *wc = span->pvt;
 
689
        int i;
 
690
 
 
691
        /* initialize the start value for the entire chunk of last ec buffer */
 
692
        for (i = 0; i < span->channels; i++) {
 
693
                memset(wc->ec_chunk1[i], ZT_LIN2X(0, &span->chans[i]), ZT_CHUNKSIZE);
 
694
                memset(wc->ec_chunk2[i], ZT_LIN2X(0, &span->chans[i]), ZT_CHUNKSIZE);
 
695
        }
 
696
 
 
697
        /* Reset framer with proper parameters and start */
 
698
        t1xxp_framer_start(wc, span);
 
699
        debug_printk(1, "Calling startup (flags is %d)\n", span->flags);
 
700
 
 
701
        return 0;
 
702
}
 
703
 
 
704
static int t1xxp_shutdown(struct zt_span *span)
 
705
{
 
706
        struct t1 *wc = span->pvt;
 
707
        unsigned long flags;
 
708
 
 
709
        t1_setreg(wc, 0x46, 0x41);      /* GCR: Interrupt on Activation/Deactivation of AIX, LOS */
 
710
        spin_lock_irqsave(&wc->reglock, flags);
 
711
        span->flags &= ~ZT_FLAG_RUNNING;
 
712
        spin_unlock_irqrestore(&wc->reglock, flags);
 
713
        return 0;
 
714
}
 
715
 
 
716
static int t1xxp_chanconfig(struct zt_chan *chan, int sigtype)
 
717
{
 
718
        struct t1 *wc = chan->pvt;
 
719
        int alreadyrunning = chan->span->flags & ZT_FLAG_RUNNING;
 
720
 
 
721
        if (alreadyrunning && (wc->spantype != TYPE_E1))
 
722
                __t1xxp_set_clear(wc, chan->channo);
 
723
 
 
724
        return 0;
 
725
}
 
726
 
 
727
static int t1xxp_spanconfig(struct zt_span *span, struct zt_lineconfig *lc)
 
728
{
 
729
        struct t1 *wc = span->pvt;
 
730
 
 
731
        /* Do we want to SYNC on receive or not */
 
732
        wc->sync = lc->sync;
 
733
        if (wc->sync)
 
734
                wc->ctlreg |= 0x80;
 
735
        else
 
736
                wc->ctlreg &= ~0x80;
 
737
 
 
738
        /* If already running, apply changes immediately */
 
739
        if (span->flags & ZT_FLAG_RUNNING)
 
740
                return t1xxp_startup(span);
 
741
 
 
742
        return 0;
 
743
}
 
744
 
 
745
static int t1xxp_rbsbits(struct zt_chan *chan, int bits)
 
746
{
 
747
        u_char m,c;
 
748
        int n,b;
 
749
        struct t1 *wc = chan->pvt;
 
750
        unsigned long flags;
 
751
        
 
752
        debug_printk(2, "Setting bits to %d on channel %s\n", bits, chan->name);
 
753
        if (wc->spantype == TYPE_E1) { /* do it E1 way */
 
754
                if (chan->chanpos == 16)
 
755
                        return 0;
 
756
 
 
757
                n = chan->chanpos - 1;
 
758
                if (chan->chanpos > 15) n--;
 
759
                b = (n % 15);
 
760
                spin_lock_irqsave(&wc->reglock, flags); 
 
761
                c = wc->txsigs[b];
 
762
                m = (n / 15) << 2; /* nibble selector */
 
763
                c &= (0xf << m); /* keep the other nibble */
 
764
                c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
 
765
                wc->txsigs[b] = c;
 
766
                spin_unlock_irqrestore(&wc->reglock, flags);
 
767
                  /* output them to the chip */
 
768
                t1_setreg_full(wc,0x71 + b,c,1,NOT_VPM); 
 
769
        } else if (wc->span.lineconfig & ZT_CONFIG_D4) {
 
770
                n = chan->chanpos - 1;
 
771
                b = (n / 4);
 
772
                spin_lock_irqsave(&wc->reglock, flags); 
 
773
                c = wc->txsigs[b];
 
774
                m = ((3 - (n % 4)) << 1); /* nibble selector */
 
775
                c &= ~(0x3 << m); /* keep the other nibble */
 
776
                c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
 
777
                wc->txsigs[b] = c;
 
778
                spin_unlock_irqrestore(&wc->reglock, flags);
 
779
                /* output them to the chip */
 
780
                t1_setreg_full(wc,0x70 + b,c,1,NOT_VPM); 
 
781
                t1_setreg_full(wc,0x70 + b + 6,c,1,NOT_VPM); 
 
782
        } else if (wc->span.lineconfig & ZT_CONFIG_ESF) {
 
783
                n = chan->chanpos - 1;
 
784
                b = (n / 2);
 
785
                spin_lock_irqsave(&wc->reglock, flags); 
 
786
                c = wc->txsigs[b];
 
787
                m = ((n % 2) << 2); /* nibble selector */
 
788
                c &= (0xf << m); /* keep the other nibble */
 
789
                c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
 
790
                wc->txsigs[b] = c;
 
791
                spin_unlock_irqrestore(&wc->reglock, flags);
 
792
                  /* output them to the chip */
 
793
                t1_setreg_full(wc,0x70 + b,c,1,NOT_VPM); 
 
794
        } 
 
795
        debug_printk(2,"Finished setting RBS bits\n");
 
796
 
 
797
        return 0;
 
798
}
 
799
 
 
800
static inline void __t1_check_sigbits_reads(struct t1 *wc)
 
801
{
 
802
        int i;
 
803
 
 
804
        if (!(wc->span.flags & ZT_FLAG_RUNNING))
 
805
                return;
 
806
        if (wc->spantype == TYPE_E1) {
 
807
                for (i = 0; i < 15; i++) {
 
808
                        if (t1_getreg(wc, 0x71 + i, 1))
 
809
                                wc->isrreaderrors++;
 
810
                }
 
811
        } else if (wc->span.lineconfig & ZT_CONFIG_D4) {
 
812
                for (i = 0; i < 24; i+=4) {
 
813
                        if (t1_getreg(wc, 0x70 + (i >> 2), 1))
 
814
                                wc->isrreaderrors++;
 
815
                }
 
816
        } else {
 
817
                for (i = 0; i < 24; i+=2) {
 
818
                        if (t1_getreg(wc, 0x70 + (i >> 1), 1))
 
819
                                wc->isrreaderrors++;
 
820
                }
 
821
        }
 
822
}
 
823
 
 
824
static inline void __t1_check_sigbits(struct t1 *wc)
 
825
{
 
826
        int a,i,rxs;
 
827
 
 
828
        if (!(wc->span.flags & ZT_FLAG_RUNNING))
 
829
                return;
 
830
        if (wc->spantype == TYPE_E1) {
 
831
                for (i = 0; i < 15; i++) {
 
832
                        a = t1_getreg_isr(wc, 0x71 + i);
 
833
                        if (a > -1) {
 
834
                                /* Get high channel in low bits */
 
835
                                rxs = (a & 0xf);
 
836
                                if (!(wc->span.chans[i+16].sig & ZT_SIG_CLEAR)) {
 
837
                                        if (wc->span.chans[i+16].rxsig != rxs) {
 
838
                                                spin_unlock(&wc->reglock);
 
839
                                                zt_rbsbits(&wc->span.chans[i+16], rxs);
 
840
                                                spin_lock(&wc->reglock);
 
841
                                        }
 
842
                                }
 
843
                                rxs = (a >> 4) & 0xf;
 
844
                                if (!(wc->span.chans[i].sig & ZT_SIG_CLEAR)) {
 
845
                                        if (wc->span.chans[i].rxsig != rxs) {
 
846
                                                spin_unlock(&wc->reglock);
 
847
                                                zt_rbsbits(&wc->span.chans[i], rxs);
 
848
                                                spin_lock(&wc->reglock);
 
849
                                        }
 
850
                                }
 
851
                        }
 
852
                }
 
853
        } else if (wc->span.lineconfig & ZT_CONFIG_D4) {
 
854
                for (i = 0; i < 24; i+=4) {
 
855
                        a = t1_getreg_isr(wc, 0x70 + (i>>2));
 
856
                        if (a > -1) {
 
857
                                /* Get high channel in low bits */
 
858
                                rxs = (a & 0x3) << 2;
 
859
                                if (!(wc->span.chans[i+3].sig & ZT_SIG_CLEAR)) {
 
860
                                        if (wc->span.chans[i+3].rxsig != rxs) {
 
861
                                                spin_unlock(&wc->reglock);
 
862
                                                zt_rbsbits(&wc->span.chans[i+3], rxs);
 
863
                                                spin_lock(&wc->reglock);
 
864
                                        }
 
865
                                }
 
866
                                rxs = (a & 0xc);
 
867
                                if (!(wc->span.chans[i+2].sig & ZT_SIG_CLEAR)) {
 
868
                                        if (wc->span.chans[i+2].rxsig != rxs) {
 
869
                                                spin_unlock(&wc->reglock);
 
870
                                                zt_rbsbits(&wc->span.chans[i+2], rxs);
 
871
                                                spin_lock(&wc->reglock);
 
872
                                        }
 
873
                                }
 
874
                                rxs = (a >> 2) & 0xc;
 
875
                                if (!(wc->span.chans[i+1].sig & ZT_SIG_CLEAR)) {
 
876
                                        if (wc->span.chans[i+1].rxsig != rxs) {
 
877
                                                spin_unlock(&wc->reglock);
 
878
                                                zt_rbsbits(&wc->span.chans[i+1], rxs);
 
879
                                                spin_lock(&wc->reglock);
 
880
                                        }
 
881
                                }
 
882
                                rxs = (a >> 4) & 0xc;
 
883
                                if (!(wc->span.chans[i].sig & ZT_SIG_CLEAR)) {
 
884
                                        if (wc->span.chans[i].rxsig != rxs) {
 
885
                                                spin_unlock(&wc->reglock);
 
886
                                                zt_rbsbits(&wc->span.chans[i], rxs);
 
887
                                                spin_lock(&wc->reglock);
 
888
                                        }       
 
889
                                }
 
890
                        }
 
891
                }
 
892
        } else {
 
893
                for (i = 0; i < 24; i+=2) {
 
894
                        a = t1_getreg_isr(wc, 0x70 + (i>>1));
 
895
                        if (a > -1) {
 
896
                                /* Get high channel in low bits */
 
897
                                rxs = (a & 0xf);
 
898
                                if (!(wc->span.chans[i+1].sig & ZT_SIG_CLEAR)) {
 
899
                                        if (wc->span.chans[i+1].rxsig != rxs) {
 
900
                                                spin_unlock(&wc->reglock);
 
901
                                                zt_rbsbits(&wc->span.chans[i+1], rxs);
 
902
                                                spin_lock(&wc->reglock);
 
903
                                        }
 
904
                                }
 
905
                                rxs = (a >> 4) & 0xf;
 
906
                                if (!(wc->span.chans[i].sig & ZT_SIG_CLEAR)) {
 
907
                                        if (wc->span.chans[i].rxsig != rxs) {
 
908
                                                spin_unlock(&wc->reglock);
 
909
                                                zt_rbsbits(&wc->span.chans[i], rxs);
 
910
                                                spin_lock(&wc->reglock);
 
911
                                        }
 
912
                                }
 
913
                        }
 
914
                }
 
915
        }
 
916
}
 
917
 
 
918
static int t1xxp_maint(struct zt_span *span, int cmd)
 
919
{
 
920
        struct t1 *wc = span->pvt;
 
921
 
 
922
        if (wc->spantype == TYPE_E1) {
 
923
                switch (cmd) {
 
924
                case ZT_MAINT_NONE:
 
925
                        module_printk("XXX Turn off local and remote loops E1 XXX\n");
 
926
                        break;
 
927
                case ZT_MAINT_LOCALLOOP:
 
928
                        module_printk("XXX Turn on local loopback E1 XXX\n");
 
929
                        break;
 
930
                case ZT_MAINT_REMOTELOOP:
 
931
                        module_printk("XXX Turn on remote loopback E1 XXX\n");
 
932
                        break;
 
933
                case ZT_MAINT_LOOPUP:
 
934
                        module_printk("XXX Send loopup code E1 XXX\n");
 
935
                        break;
 
936
                case ZT_MAINT_LOOPDOWN:
 
937
                        module_printk("XXX Send loopdown code E1 XXX\n");
 
938
                        break;
 
939
                case ZT_MAINT_LOOPSTOP:
 
940
                        module_printk("XXX Stop sending loop codes E1 XXX\n");
 
941
                        break;
 
942
                default:
 
943
                        module_printk("Unknown E1 maint command: %d\n", cmd);
 
944
                        break;
 
945
                }
 
946
        } else {
 
947
                switch (cmd) {
 
948
                case ZT_MAINT_NONE:
 
949
                        module_printk("XXX Turn off local and remote loops T1 XXX\n");
 
950
                        break;
 
951
                case ZT_MAINT_LOCALLOOP:
 
952
                        module_printk("XXX Turn on local loop and no remote loop XXX\n");
 
953
                        break;
 
954
                case ZT_MAINT_REMOTELOOP:
 
955
                        module_printk("XXX Turn on remote loopup XXX\n");
 
956
                        break;
 
957
                case ZT_MAINT_LOOPUP:
 
958
                        t1_setreg(wc, 0x21, 0x50);      /* FMR5: Nothing but RBS mode */
 
959
                        break;
 
960
                case ZT_MAINT_LOOPDOWN:
 
961
                        t1_setreg(wc, 0x21, 0x60);      /* FMR5: Nothing but RBS mode */
 
962
                        break;
 
963
                case ZT_MAINT_LOOPSTOP:
 
964
                        t1_setreg(wc, 0x21, 0x40);      /* FMR5: Nothing but RBS mode */
 
965
                        break;
 
966
                default:
 
967
                        module_printk("Unknown T1 maint command: %d\n", cmd);
 
968
                        break;
 
969
                }
 
970
        }
 
971
 
 
972
        return 0;
 
973
}
 
974
 
 
975
static int t1xxp_open(struct zt_chan *chan)
 
976
{
 
977
        struct t1 *wc = chan->pvt;
 
978
 
 
979
        if (wc->dead)
 
980
                return -ENODEV;
 
981
        wc->usecount++;
 
982
 
 
983
#ifndef LINUX26 
 
984
        MOD_INC_USE_COUNT;
 
985
#else
 
986
        try_module_get(THIS_MODULE);
 
987
#endif  
 
988
 
 
989
        return 0;
 
990
}
 
991
 
 
992
static int t1xxp_close(struct zt_chan *chan)
 
993
{
 
994
        struct t1 *wc = chan->pvt;
 
995
 
 
996
        wc->usecount--;
 
997
 
 
998
#ifndef LINUX26 
 
999
        MOD_DEC_USE_COUNT;
 
1000
#else
 
1001
        module_put(THIS_MODULE);
 
1002
#endif
 
1003
 
 
1004
        /* If we're dead, release us now */
 
1005
        if (!wc->usecount && wc->dead) 
 
1006
                t1_release(wc);
 
1007
 
 
1008
        return 0;
 
1009
}
 
1010
 
 
1011
static int t1xxp_ioctl(struct zt_chan *chan, unsigned int cmd, unsigned long data)
 
1012
{
 
1013
        unsigned int x;
 
1014
        struct t1 *wc = chan->pvt;
 
1015
 
 
1016
        switch (cmd) {
 
1017
        case WCT4_GET_REGS:
 
1018
                /* Since all register access was moved into the voicebus
 
1019
                 * module....this was removed.  Although...why does the client
 
1020
                 * library need access to the registers (debugging)? \todo ..
 
1021
                 */
 
1022
                WARN_ON(1);
 
1023
                return -ENOSYS;
 
1024
                break;
 
1025
#ifdef VPM_SUPPORT
 
1026
        case ZT_TONEDETECT:
 
1027
                if (get_user(x, (int *) data))
 
1028
                        return -EFAULT;
 
1029
                if (!wc->vpm150m)
 
1030
                        return -ENOSYS;
 
1031
                if (wc->vpm150m && (x && !vpmdtmfsupport))
 
1032
                        return -ENOSYS;
 
1033
                if (x & ZT_TONEDETECT_ON) {
 
1034
                        set_bit(chan->chanpos - 1, &wc->dtmfmask);
 
1035
                        module_printk("turning on tone detection\n");
 
1036
                } else {
 
1037
                        clear_bit(chan->chanpos - 1, &wc->dtmfmask);
 
1038
                        module_printk("turning off tone detection\n");
 
1039
                }
 
1040
                if (x & ZT_TONEDETECT_MUTE) {
 
1041
                        if(wc->vpm150m)
 
1042
                                set_bit(chan->chanpos - 1, &wc->vpm150m->desireddtmfmutestate);
 
1043
                } else {
 
1044
                        if(wc->vpm150m)
 
1045
                                clear_bit(chan->chanpos - 1, &wc->vpm150m->desireddtmfmutestate);
 
1046
                }
 
1047
                return 0;
 
1048
#endif
 
1049
        default:
 
1050
                return -ENOTTY;
 
1051
        }
 
1052
        return 0;
 
1053
}
 
1054
 
 
1055
#ifdef VPM_SUPPORT
 
1056
 
 
1057
#include "adt_lec.c"
 
1058
 
 
1059
static int t1xxp_echocan_with_params(struct zt_chan *chan, struct zt_echocanparams *ecp, struct zt_echocanparam *p)
 
1060
{
 
1061
        struct adt_lec_params params;
 
1062
        struct t1 *wc = chan->pvt;
 
1063
        struct vpm150m *vpm150m = wc->vpm150m;
 
1064
        unsigned int flags;
 
1065
        struct vpm150m_workentry *work;
 
1066
        unsigned int ret;
 
1067
 
 
1068
        if (!wc->vpm150m)
 
1069
                return -ENODEV;
 
1070
 
 
1071
        adt_lec_init_defaults(&params, 32);
 
1072
 
 
1073
        if ((ret = adt_lec_parse_params(&params, ecp, p)))
 
1074
                return ret;
 
1075
 
 
1076
        /* we can't really control the tap length, but the value is used
 
1077
           to control whether the ec is on or off, so translate it */
 
1078
        params.tap_length = ecp->tap_length ? 1 : 0;
 
1079
 
 
1080
        if (!(work = kmalloc(sizeof(*work), GFP_KERNEL)))
 
1081
                return -ENOMEM;
 
1082
 
 
1083
        work->params = params;
 
1084
        work->wc = wc;
 
1085
        work->chan = chan; 
 
1086
        spin_lock_irqsave(&vpm150m->lock, flags);
 
1087
        list_add_tail(&work->list, &vpm150m->worklist);
 
1088
        spin_unlock_irqrestore(&vpm150m->lock, flags);
 
1089
        
 
1090
        /* we must do this later since we cannot sleep in the echocan function */
 
1091
        if (test_bit(VPM150M_ACTIVE, &vpm150m->control))
 
1092
                queue_work(vpm150m->wq, &vpm150m->work_echocan);
 
1093
 
 
1094
        return 0; /* how do I return the status since it is done later by the workqueue? */
 
1095
}
 
1096
#endif
 
1097
 
 
1098
static int t1_software_init(struct t1 *wc)
 
1099
{
 
1100
        int x;
 
1101
        struct pci_dev* dev;
 
1102
 
 
1103
        dev = voicebus_get_pci_dev(wc->vb);
 
1104
 
 
1105
        /* Find position */
 
1106
        for (x = 0; x < sizeof(ifaces) / sizeof(ifaces[0]); x++) {
 
1107
                if (ifaces[x] == wc) {
 
1108
                        debug_printk(1, "software init for card %d\n",x);
 
1109
                        break;
 
1110
                }
 
1111
        }
 
1112
 
 
1113
        if (x == sizeof(ifaces) / sizeof(ifaces[0]))
 
1114
                return -1;
 
1115
 
 
1116
        t4_serial_setup(wc);
 
1117
 
 
1118
        wc->num = x;
 
1119
        sprintf(wc->span.name, "WCT1/%d", wc->num);
 
1120
        snprintf(wc->span.desc, sizeof(wc->span.desc) - 1, "%s Card %d", wc->variety, wc->num);
 
1121
        wc->span.manufacturer = "Digium";
 
1122
        strncpy(wc->span.devicetype, wc->variety, sizeof(wc->span.devicetype) - 1);
 
1123
 
 
1124
#if defined(VPM_SUPPORT)
 
1125
        if (wc->vpm150m)
 
1126
                strncat(wc->span.devicetype, " with VPMADT032", sizeof(wc->span.devicetype) - 1);
 
1127
#endif
 
1128
 
 
1129
        snprintf(wc->span.location, sizeof(wc->span.location) - 1,
 
1130
                "PCI Bus %02d Slot %02d", dev->bus->number, PCI_SLOT(dev->devfn) + 1);
 
1131
 
 
1132
        wc->span.spanconfig = t1xxp_spanconfig;
 
1133
        wc->span.chanconfig = t1xxp_chanconfig;
 
1134
        wc->span.irq = dev->irq;
 
1135
        wc->span.startup = t1xxp_startup;
 
1136
        wc->span.shutdown = t1xxp_shutdown;
 
1137
        wc->span.rbsbits = t1xxp_rbsbits;
 
1138
        wc->span.maint = t1xxp_maint;
 
1139
        wc->span.open = t1xxp_open;
 
1140
        wc->span.close = t1xxp_close;
 
1141
        wc->span.ioctl = t1xxp_ioctl;
 
1142
#ifdef VPM_SUPPORT
 
1143
        wc->span.echocan_with_params = t1xxp_echocan_with_params;
 
1144
#endif
 
1145
 
 
1146
        if (wc->spantype == TYPE_E1) {
 
1147
                if (unchannelized)
 
1148
                        wc->span.channels = 32;
 
1149
                else
 
1150
                        wc->span.channels = 31;
 
1151
                wc->span.spantype = "E1";
 
1152
                wc->span.linecompat = ZT_CONFIG_HDB3 | ZT_CONFIG_CCS | ZT_CONFIG_CRC4;
 
1153
                wc->span.deflaw = ZT_LAW_ALAW;
 
1154
        } else {
 
1155
                wc->span.channels = 24;
 
1156
                wc->span.spantype = "T1";
 
1157
                wc->span.linecompat = ZT_CONFIG_AMI | ZT_CONFIG_B8ZS | ZT_CONFIG_D4 | ZT_CONFIG_ESF;
 
1158
                wc->span.deflaw = ZT_LAW_MULAW;
 
1159
        }
 
1160
        wc->span.chans = wc->chans;
 
1161
        wc->span.flags = ZT_FLAG_RBS;
 
1162
        wc->span.pvt = wc;
 
1163
        init_waitqueue_head(&wc->span.maintq);
 
1164
        for (x = 0; x < wc->span.channels; x++) {
 
1165
                sprintf(wc->chans[x].name, "WCT1/%d/%d", wc->num, x + 1);
 
1166
                wc->chans[x].sigcap = ZT_SIG_EM | ZT_SIG_CLEAR | ZT_SIG_EM_E1 | 
 
1167
                                      ZT_SIG_FXSLS | ZT_SIG_FXSGS | ZT_SIG_MTP2 |
 
1168
                                      ZT_SIG_FXSKS | ZT_SIG_FXOLS | ZT_SIG_DACS_RBS |
 
1169
                                      ZT_SIG_FXOGS | ZT_SIG_FXOKS | ZT_SIG_CAS | ZT_SIG_SF;
 
1170
                wc->chans[x].pvt = wc;
 
1171
                wc->chans[x].chanpos = x + 1;
 
1172
        }
 
1173
        if (zt_register(&wc->span, 0)) {
 
1174
                module_printk("Unable to register span with Zaptel\n");
 
1175
                return -1;
 
1176
        }
 
1177
        wc->initialized = 1;
 
1178
 
 
1179
        return 0;
 
1180
}
 
1181
 
 
1182
#ifdef VPM_SUPPORT
 
1183
static inline unsigned char t1_vpm_in(struct t1 *wc, int unit, const unsigned int addr) 
 
1184
{
 
1185
                return t1_getreg_full(wc, addr, 0, unit);
 
1186
}
 
1187
 
 
1188
static inline unsigned char t1_vpm_out(struct t1 *wc, int unit, const unsigned int addr, const unsigned char val) 
 
1189
{
 
1190
                return t1_setreg_full(wc, addr, val, 0, unit);
 
1191
}
 
1192
 
 
1193
#endif
 
1194
 
 
1195
static int t1_hardware_post_init(struct t1 *wc)
 
1196
{
 
1197
        unsigned int reg;
 
1198
        int x;
 
1199
 
 
1200
        /* T1 or E1 */
 
1201
        if (t1e1override > -1) {
 
1202
                if (t1e1override)
 
1203
                        wc->spantype = TYPE_E1;
 
1204
                else
 
1205
                        wc->spantype = TYPE_T1;
 
1206
        } else {
 
1207
                if (t1_getpins(wc,0) & 0x01) /* returns 1 for T1 mode */
 
1208
                        wc->spantype = TYPE_T1;
 
1209
                else    
 
1210
                        wc->spantype = TYPE_E1;
 
1211
        }
 
1212
        debug_printk(1, "spantype: %s\n", wc->spantype==1 ? "T1" : "E1");
 
1213
        
 
1214
        if (wc->spantype == TYPE_E1) {
 
1215
                if (unchannelized)
 
1216
                        wc->chanmap = chanmap_e1uc;
 
1217
                else
 
1218
                        wc->chanmap = chanmap_e1;
 
1219
        } else
 
1220
                wc->chanmap = chanmap_t1;
 
1221
        /* what version of the FALC are we using? */
 
1222
        reg = t1_setreg(wc, 0x4a, 0xaa);
 
1223
        reg = t1_getreg(wc, 0x4a, 0);
 
1224
        debug_printk(1, "FALC version: %08x\n", reg);
 
1225
 
 
1226
        /* make sure reads and writes work */
 
1227
        for (x = 0; x < 256; x++) {
 
1228
                t1_setreg(wc, 0x14, x);
 
1229
                if ((reg = t1_getreg(wc, 0x14, 0)) != x)
 
1230
                        module_printk("Wrote '%x' but read '%x'\n", x, reg);
 
1231
        }
 
1232
 
 
1233
        /* all LED's blank */
 
1234
        wc->ledtestreg = UNSET_LED_ORANGE(wc->ledtestreg);
 
1235
        wc->ledtestreg = UNSET_LED_REDGREEN(wc->ledtestreg);
 
1236
        t1_setleds(wc, wc->ledtestreg, 0);
 
1237
 
 
1238
#ifdef VPM_SUPPORT
 
1239
        t1_vpm150m_init(wc);
 
1240
        if (wc->vpm150m) {
 
1241
                module_printk("VPM present and operational (Firmware version %x)\n", wc->vpm150m->version);
 
1242
                wc->ctlreg |= 0x10; /* turn on vpm (RX audio from vpm module) */
 
1243
                if (vpmtsisupport) {
 
1244
                        debug_printk(1, "enabling VPM TSI pin\n");
 
1245
                        wc->ctlreg |= 0x01; /* turn on vpm timeslot interchange pin */
 
1246
                }
 
1247
        }
 
1248
#endif
 
1249
 
 
1250
        return 0;
 
1251
}
 
1252
 
 
1253
static inline void __t1_check_alarms_reads(struct t1 *wc)
 
1254
{
 
1255
        if (!(wc->span.flags & ZT_FLAG_RUNNING))
 
1256
                return;
 
1257
 
 
1258
        if (t1_getreg(wc, 0x4c, 1))
 
1259
                wc->isrreaderrors++;
 
1260
        if (t1_getreg(wc, 0x20, 1))
 
1261
                wc->isrreaderrors++;
 
1262
        if (t1_getreg(wc, 0x4d, 1))
 
1263
                wc->isrreaderrors++;
 
1264
}
 
1265
 
 
1266
static inline void __t1_check_alarms(struct t1 *wc)
 
1267
{
 
1268
        unsigned char c,d;
 
1269
        int alarms;
 
1270
        int x,j;
 
1271
        unsigned char fmr4; /* must read this always */
 
1272
 
 
1273
        if (!(wc->span.flags & ZT_FLAG_RUNNING))
 
1274
                return;
 
1275
 
 
1276
        c = t1_getreg_isr(wc, 0x4c);
 
1277
        fmr4 = t1_getreg_isr(wc, 0x20); /* must read this even if we don't use it */
 
1278
        d = t1_getreg_isr(wc, 0x4d);
 
1279
 
 
1280
        /* Assume no alarms */
 
1281
        alarms = 0;
 
1282
 
 
1283
        /* And consider only carrier alarms */
 
1284
        wc->span.alarms &= (ZT_ALARM_RED | ZT_ALARM_BLUE | ZT_ALARM_NOTOPEN);
 
1285
 
 
1286
        if (wc->spantype == TYPE_E1) {
 
1287
                if (c & 0x04) {
 
1288
                        /* No multiframe found, force RAI high after 400ms only if
 
1289
                           we haven't found a multiframe since last loss
 
1290
                           of frame */
 
1291
                        if (!wc->flags.nmf) {
 
1292
                                t1_setreg_full(wc, 0x20, 0x9f | 0x20, 1, NOT_VPM);      /* LIM0: Force RAI High */
 
1293
                                wc->flags.nmf = 1;
 
1294
                                module_printk("NMF workaround on!\n");
 
1295
                        }
 
1296
                        t1_setreg_full(wc, 0x1e, 0xc3, 1, NOT_VPM);     /* Reset to CRC4 mode */
 
1297
                        t1_setreg_full(wc, 0x1c, 0xf2, 1, NOT_VPM);     /* Force Resync */
 
1298
                        t1_setreg_full(wc, 0x1c, 0xf0, 1, NOT_VPM);     /* Force Resync */
 
1299
                } else if (!(c & 0x02)) {
 
1300
                        if (wc->flags.nmf) {
 
1301
                                t1_setreg_full(wc, 0x20, 0x9f, 1, NOT_VPM);     /* LIM0: Clear forced RAI */
 
1302
                                wc->flags.nmf = 0;
 
1303
                                module_printk("NMF workaround off!\n");
 
1304
                        }
 
1305
                }
 
1306
        } else {
 
1307
                /* Detect loopup code if we're not sending one */
 
1308
                if ((!wc->span.mainttimer) && (d & 0x08)) {
 
1309
                        /* Loop-up code detected */
 
1310
                        if ((wc->loopupcnt++ > 80)  && (wc->span.maintstat != ZT_MAINT_REMOTELOOP)) {
 
1311
                                t1_setreg_full(wc, 0x36, 0x08, 1, NOT_VPM);     /* LIM0: Disable any local loop */
 
1312
                                t1_setreg_full(wc, 0x37, 0xf6, 1, NOT_VPM);     /* LIM1: Enable remote loop */
 
1313
                                wc->span.maintstat = ZT_MAINT_REMOTELOOP;
 
1314
                        }
 
1315
                } else
 
1316
                        wc->loopupcnt = 0;
 
1317
                /* Same for loopdown code */
 
1318
                if ((!wc->span.mainttimer) && (d & 0x10)) {
 
1319
                        /* Loop-down code detected */
 
1320
                        if ((wc->loopdowncnt++ > 80)  && (wc->span.maintstat == ZT_MAINT_REMOTELOOP)) {
 
1321
                                t1_setreg_full(wc, 0x36, 0x08, 1, NOT_VPM);     /* LIM0: Disable any local loop */
 
1322
                                t1_setreg_full(wc, 0x37, 0xf0, 1, NOT_VPM);     /* LIM1: Disable remote loop */
 
1323
                                wc->span.maintstat = ZT_MAINT_NONE;
 
1324
                        }
 
1325
                } else
 
1326
                        wc->loopdowncnt = 0;
 
1327
        }
 
1328
 
 
1329
        if (wc->span.lineconfig & ZT_CONFIG_NOTOPEN) {
 
1330
                for (x=0,j=0;x < wc->span.channels;x++)
 
1331
                        if ((wc->span.chans[x].flags & ZT_FLAG_OPEN) ||
 
1332
                            (wc->span.chans[x].flags & ZT_FLAG_NETDEV))
 
1333
                                j++;
 
1334
                if (!j)
 
1335
                        alarms |= ZT_ALARM_NOTOPEN;
 
1336
        }
 
1337
 
 
1338
        if (c & 0xa0) {
 
1339
                if (wc->alarmcount >= alarmdebounce) {
 
1340
                        if (!unchannelized)
 
1341
                                alarms |= ZT_ALARM_RED;
 
1342
                } else
 
1343
                        wc->alarmcount++;
 
1344
        } else
 
1345
                wc->alarmcount = 0;
 
1346
        if (c & 0x4)
 
1347
                alarms |= ZT_ALARM_BLUE;
 
1348
 
 
1349
        /* Keep track of recovering */
 
1350
        if ((!alarms) && wc->span.alarms) 
 
1351
                wc->alarmtimer = ZT_ALARMSETTLE_TIME;
 
1352
        if (wc->alarmtimer)
 
1353
                alarms |= ZT_ALARM_RECOVER;
 
1354
 
 
1355
        /* If receiving alarms, go into Yellow alarm state */
 
1356
        if (alarms && !wc->flags.sendingyellow) {
 
1357
                module_printk("Setting yellow alarm\n");
 
1358
 
 
1359
                /* We manually do yellow alarm to handle RECOVER and NOTOPEN, otherwise it's auto anyway */
 
1360
                t1_setreg_full(wc, 0x20, fmr4 | 0x20, 1, NOT_VPM);
 
1361
                wc->flags.sendingyellow = 1;
 
1362
        } else if (!alarms && wc->flags.sendingyellow) {
 
1363
                module_printk("Clearing yellow alarm\n");
 
1364
                /* We manually do yellow alarm to handle RECOVER  */
 
1365
                t1_setreg_full(wc, 0x20, fmr4 & ~0x20, 1, NOT_VPM);
 
1366
                wc->flags.sendingyellow = 0;
 
1367
        }
 
1368
        
 
1369
        if ((c & 0x10) && !unchannelized)
 
1370
                alarms |= ZT_ALARM_YELLOW;
 
1371
        if (wc->span.mainttimer || wc->span.maintstat) 
 
1372
                alarms |= ZT_ALARM_LOOPBACK;
 
1373
        wc->span.alarms = alarms;
 
1374
        spin_unlock(&wc->reglock);
 
1375
        zt_alarm_notify(&wc->span);
 
1376
        spin_lock(&wc->reglock);
 
1377
}
 
1378
 
 
1379
static inline void __handle_leds(struct t1 *wc)
 
1380
{
 
1381
        if (wc->span.alarms & (ZT_ALARM_RED | ZT_ALARM_BLUE)) {
 
1382
                wc->blinktimer++;
 
1383
                if (wc->blinktimer == 160)
 
1384
                        wc->ledtestreg = SET_LED_RED(wc->ledtestreg); 
 
1385
                if (wc->blinktimer == 480) {
 
1386
                        wc->ledtestreg = UNSET_LED_REDGREEN(wc->ledtestreg); 
 
1387
                        wc->blinktimer = 0;
 
1388
                }
 
1389
        } else if (wc->span.alarms & ZT_ALARM_YELLOW) {
 
1390
                wc->yellowtimer++;
 
1391
                if (!(wc->yellowtimer % 2))
 
1392
                        wc->ledtestreg = SET_LED_RED(wc->ledtestreg); 
 
1393
                else
 
1394
                        wc->ledtestreg = SET_LED_GREEN(wc->ledtestreg); 
 
1395
        } else {
 
1396
                if (wc->span.maintstat != ZT_MAINT_NONE)
 
1397
                        wc->ledtestreg = SET_LED_ORANGE(wc->ledtestreg);
 
1398
                else
 
1399
                        wc->ledtestreg = UNSET_LED_ORANGE(wc->ledtestreg);
 
1400
                if (wc->span.flags & ZT_FLAG_RUNNING)
 
1401
                        wc->ledtestreg = SET_LED_GREEN(wc->ledtestreg); 
 
1402
                else
 
1403
                        wc->ledtestreg = UNSET_LED_REDGREEN(wc->ledtestreg); 
 
1404
        }
 
1405
 
 
1406
        if (wc->ledtestreg != wc->ledlastvalue) {
 
1407
                t1_setleds(wc, wc->ledtestreg, 1);
 
1408
                wc->ledlastvalue = wc->ledtestreg;
 
1409
        }
 
1410
}
 
1411
 
 
1412
 
 
1413
static void __t1_do_counters(struct t1 *wc)
 
1414
{
 
1415
        if (wc->alarmtimer) {
 
1416
                if (!--wc->alarmtimer) {
 
1417
                        wc->span.alarms &= ~(ZT_ALARM_RECOVER);
 
1418
                        zt_alarm_notify(&wc->span);
 
1419
                }
 
1420
        }
 
1421
}
 
1422
 
 
1423
static inline void t1_isr_misc(struct t1 *wc)
 
1424
{
 
1425
        const unsigned int x = wc->intcount & 0x3f;
 
1426
        int buffer_count = voicebus_current_latency(wc->vb);
 
1427
 
 
1428
        if (unlikely(!wc->initialized)) return;
 
1429
        
 
1430
        __handle_leds(wc);
 
1431
 
 
1432
        __t1_do_counters(wc);
 
1433
 
 
1434
        if ( 0 == x ) {
 
1435
                __t1_check_sigbits_reads(wc);
 
1436
        }
 
1437
        else if ( 1 == x ) {
 
1438
                if (!(wc->intcount & 0x30)) {
 
1439
                        __t1_check_alarms_reads(wc);
 
1440
                        wc->alarms_read=1;
 
1441
                }
 
1442
        }
 
1443
        else if ( x == buffer_count*2) {
 
1444
                __t1_check_sigbits(wc);
 
1445
        }
 
1446
        else if ( x == (buffer_count*2)+1 ) {
 
1447
                if (wc->alarms_read) {
 
1448
                        __t1_check_alarms(wc);
 
1449
                        wc->alarms_read=0;
 
1450
                }
 
1451
        }
 
1452
        else if ( x == (buffer_count*2)+2) {
 
1453
                clean_leftovers(wc);
 
1454
        }
 
1455
}
 
1456
 
 
1457
static inline void t1_transmitprep(struct t1 *wc, unsigned char* writechunk)
 
1458
{
 
1459
        int x;
 
1460
        int y;
 
1461
        int chan;
 
1462
 
 
1463
        /* Calculate Transmission */
 
1464
        if (likely(wc->initialized)) {
 
1465
                spin_unlock(&wc->reglock);
 
1466
                zt_transmit(&wc->span);
 
1467
                spin_lock(&wc->reglock);
 
1468
        }
 
1469
 
 
1470
        for (x = 0; x < ZT_CHUNKSIZE; x++) {
 
1471
                if (likely(wc->initialized)) {
 
1472
                        for (chan = 0; chan < wc->span.channels; chan++)
 
1473
                                writechunk[(chan+1)*2] = wc->chans[chan].writechunk[x]; 
 
1474
                }
 
1475
 
 
1476
                /* process the command queue */
 
1477
                for (y = 0; y < 7; y++) {
 
1478
                        cmd_dequeue(wc, writechunk, x, y);
 
1479
                }
 
1480
#ifdef VPM_SUPPORT
 
1481
                if(likely(wc->vpm150m)) {
 
1482
                        vpm150m_cmd_dequeue(wc, writechunk, x);
 
1483
                }
 
1484
#endif
 
1485
 
 
1486
                if (x < ZT_CHUNKSIZE - 1) {
 
1487
                        writechunk[EFRAME_SIZE] = wc->ctlreg;
 
1488
                        writechunk[EFRAME_SIZE + 1] = wc->txident++;
 
1489
                }
 
1490
                writechunk += (EFRAME_SIZE + EFRAME_GAP);
 
1491
        }
 
1492
}
 
1493
 
 
1494
static inline void cmd_retransmit(struct t1 *wc)
 
1495
{
 
1496
        unsigned int x;
 
1497
 
 
1498
        for (x = 0; x < sizeof(wc->cmdq.cmds) / sizeof(wc->cmdq.cmds[0]); x++) {
 
1499
                if (!(wc->cmdq.cmds[x].flags &  __CMD_FIN)) {
 
1500
                        wc->cmdq.cmds[x].flags &= ~(__CMD_TX) ; /* clear __CMD_TX */
 
1501
                        wc->cmdq.cmds[x].ident = 0;
 
1502
                }
 
1503
        }
 
1504
}
 
1505
 
 
1506
static inline void t1_receiveprep(struct t1 *wc, unsigned char* readchunk)
 
1507
{
 
1508
        int x,chan;
 
1509
        unsigned char expected;
 
1510
 
 
1511
        for (x = 0; x < ZT_CHUNKSIZE; x++) {
 
1512
                if (likely(wc->initialized)) {
 
1513
                        for (chan = 0; chan < wc->span.channels; chan++) {
 
1514
                                wc->chans[chan].readchunk[x]= readchunk[(chan+1)*2];
 
1515
                        }
 
1516
                }
 
1517
                if (x < ZT_CHUNKSIZE - 1) {
 
1518
                        expected = wc->rxident+1;
 
1519
                        wc->rxident = readchunk[EFRAME_SIZE + 1];
 
1520
                        wc->statreg = readchunk[EFRAME_SIZE + 2];
 
1521
                        if (wc->rxident != expected) {
 
1522
                                wc->span.irqmisses++;
 
1523
                                cmd_retransmit(wc);
 
1524
                                if (unlikely(debug && wc->initialized))
 
1525
                                        module_printk("oops: rxident=%d expected=%d x=%d\n", wc->rxident, expected, x);
 
1526
                        }
 
1527
                }
 
1528
                cmd_decipher(wc, readchunk);
 
1529
#ifdef VPM_SUPPORT
 
1530
                if(wc->vpm150m)
 
1531
                        vpm150m_cmd_decipher(wc, readchunk);
 
1532
#endif
 
1533
                readchunk += (EFRAME_SIZE + EFRAME_GAP);
 
1534
        }
 
1535
        
 
1536
        /* echo cancel */
 
1537
        if (likely(wc->initialized)) {
 
1538
                spin_unlock(&wc->reglock);
 
1539
                for (x = 0; x < wc->span.channels; x++) {
 
1540
                        zt_ec_chunk(&wc->chans[x], wc->chans[x].readchunk, wc->ec_chunk2[x]);
 
1541
                        memcpy(wc->ec_chunk2[x],wc->ec_chunk1[x],ZT_CHUNKSIZE);
 
1542
                        memcpy(wc->ec_chunk1[x],wc->chans[x].writechunk,ZT_CHUNKSIZE);
 
1543
                }
 
1544
                zt_receive(&wc->span);
 
1545
                spin_lock(&wc->reglock);
 
1546
        }
 
1547
 
 
1548
        /* Wake up anyone sleeping to read/write a new register */
 
1549
        wake_up_interruptible(&wc->regq);
 
1550
}
 
1551
 
 
1552
static void
 
1553
t1_handle_transmit(void* vbb, void* context)
 
1554
{
 
1555
        struct t1* wc = context;
 
1556
        /* Either this function is called from within interrupt context, or
 
1557
         * the reglock will never be acquired from interrupt context, so it's
 
1558
         * safe to grab it without locking interrupt.
 
1559
         */
 
1560
        memset(vbb, 0, SFRAME_SIZE);
 
1561
        spin_lock(&wc->reglock);
 
1562
        wc->txints++;
 
1563
        t1_transmitprep(wc, vbb);
 
1564
        wc->intcount++;
 
1565
        t1_isr_misc(wc);
 
1566
        spin_unlock(&wc->reglock);
 
1567
        voicebus_transmit(wc->vb, vbb);
 
1568
}
 
1569
 
 
1570
static void
 
1571
t1_handle_receive(void* vbb, void* context)
 
1572
{
 
1573
        struct t1* wc = context;
 
1574
        wc->rxints++;
 
1575
        /* Either this function is called from within interrupt context, or
 
1576
         * the reglock will never be acquired from interrupt context, so it's
 
1577
         * safe to grab it without locking interrupt.
 
1578
         */
 
1579
        spin_lock(&wc->reglock);
 
1580
        t1_receiveprep(wc, vbb);
 
1581
        spin_unlock(&wc->reglock);
 
1582
}
 
1583
 
 
1584
static int __devinit te12xp_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 
1585
{
 
1586
        struct t1 *wc;
 
1587
        struct t1_desc *d = (struct t1_desc *) ent->driver_data;
 
1588
        unsigned int x;
 
1589
        int res;
 
1590
        int startinglatency;
 
1591
 
 
1592
        for (x = 0; x < sizeof(ifaces) / sizeof(ifaces[0]); x++)
 
1593
                if (!ifaces[x]) break;
 
1594
 
 
1595
        if (x >= sizeof(ifaces) / sizeof(ifaces[0])) {
 
1596
                module_printk("Too many interfaces\n");
 
1597
                return -EIO;
 
1598
        }
 
1599
        
 
1600
retry:
 
1601
        wc = kmalloc(sizeof(*wc), GFP_KERNEL);
 
1602
        if (!wc)
 
1603
                return -ENOMEM;
 
1604
 
 
1605
        ifaces[x] = wc;
 
1606
        memset(wc, 0, sizeof(*wc));
 
1607
        spin_lock_init(&wc->reglock);
 
1608
        wc->variety = d->name;
 
1609
        wc->txident = 1;
 
1610
 
 
1611
        init_waitqueue_head(&wc->regq);
 
1612
        snprintf(wc->name, sizeof(wc->name)-1, "wcte12xp%d", x);
 
1613
        if ((res=voicebus_init(pdev, SFRAME_SIZE, wc->name,
 
1614
                t1_handle_receive, t1_handle_transmit, wc, &wc->vb))) 
 
1615
        {
 
1616
                WARN_ON(1);
 
1617
                kfree(wc);
 
1618
                return res;
 
1619
        }
 
1620
        
 
1621
        /* Keep track of which device we are */
 
1622
        pci_set_drvdata(pdev, wc);
 
1623
        if (VOICEBUS_DEFAULT_LATENCY != latency) {
 
1624
                voicebus_set_minlatency(wc->vb, latency);
 
1625
        }
 
1626
        voicebus_start(wc->vb);
 
1627
        startinglatency = voicebus_current_latency(wc->vb);
 
1628
        t1_hardware_post_init(wc);
 
1629
        t1_software_init(wc);
 
1630
        if (voicebus_current_latency(wc->vb) > startinglatency) {
 
1631
                /* The voicebus library increased the latency during
 
1632
                 * initialization because the host wasn't able to service the
 
1633
                 * interrupts from the adapter quickly enough.  In this case,
 
1634
                 * we'll increase our latency and restart the initialization.
 
1635
                 */
 
1636
                printk(KERN_NOTICE "%s: Restarting board initialization " \
 
1637
                 "after increasing latency.\n", wc->name);
 
1638
                latency = voicebus_current_latency(wc->vb);
 
1639
                zt_unregister(&wc->span);
 
1640
                voicebus_release(wc->vb);
 
1641
                wc->vb = NULL;
 
1642
                kfree(wc);
 
1643
                wc = NULL;
 
1644
                goto retry;
 
1645
        }
 
1646
        module_printk("Found a %s\n", wc->variety);
 
1647
 
 
1648
        return 0;
 
1649
}
 
1650
 
 
1651
static void __devexit te12xp_remove_one(struct pci_dev *pdev)
 
1652
{
 
1653
        struct t1 *wc = pci_get_drvdata(pdev);
 
1654
#ifdef VPM_SUPPORT
 
1655
        unsigned long flags;
 
1656
        struct vpm150m *vpm150m = wc->vpm150m;
 
1657
#endif
 
1658
        if (!wc)
 
1659
                return;
 
1660
 
 
1661
#ifdef VPM_SUPPORT
 
1662
        if(vpm150m) {
 
1663
                clear_bit(VPM150M_DTMFDETECT, &vpm150m->control);
 
1664
                clear_bit(VPM150M_ACTIVE, &vpm150m->control);
 
1665
                flush_workqueue(vpm150m->wq);
 
1666
                destroy_workqueue(vpm150m->wq);
 
1667
        }
 
1668
#endif
 
1669
 
 
1670
        BUG_ON(!wc->vb);
 
1671
        voicebus_release(wc->vb);
 
1672
        wc->vb = NULL;
 
1673
 
 
1674
        if (debug && wc->isrreaderrors)
 
1675
                debug_printk(1, "isrreaderrors=%d\n", wc->isrreaderrors);
 
1676
 
 
1677
#ifdef VPM_SUPPORT
 
1678
        if(vpm150m) {
 
1679
                spin_lock_irqsave(&wc->reglock, flags);
 
1680
                wc->vpm150m = NULL;
 
1681
                vpm150m->wc = NULL;
 
1682
                spin_unlock_irqrestore(&wc->reglock, flags);
 
1683
                kfree(wc->vpm150m);
 
1684
        }
 
1685
#endif
 
1686
        /* Release span, possibly delayed */
 
1687
        if (!wc->usecount)
 
1688
                t1_release(wc);
 
1689
        else
 
1690
                wc->dead = 1;
 
1691
}
 
1692
 
 
1693
static struct pci_device_id te12xp_pci_tbl[] = {
 
1694
        { 0xd161, 0x0120, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te120p},
 
1695
        { 0xd161, 0x8000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te121},
 
1696
        { 0xd161, 0x8001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te122},
 
1697
        { 0 }
 
1698
};
 
1699
 
 
1700
MODULE_DEVICE_TABLE(pci, te12xp_pci_tbl);
 
1701
 
 
1702
struct pci_driver te12xp_driver = {
 
1703
        name:   "wcte12xp",
 
1704
        probe:  te12xp_init_one,
 
1705
#ifdef LINUX26
 
1706
        remove: __devexit_p(te12xp_remove_one),
 
1707
#else
 
1708
        remove: te12xp_remove_one,
 
1709
#endif
 
1710
        suspend: NULL,
 
1711
        resume: NULL,
 
1712
        id_table: te12xp_pci_tbl,
 
1713
};
 
1714
 
 
1715
static int __init te12xp_init(void)
 
1716
{
 
1717
        int res;
 
1718
 
 
1719
        res = zap_pci_module(&te12xp_driver);
 
1720
 
 
1721
        return res ? -ENODEV : 0;
 
1722
}
 
1723
 
 
1724
 
 
1725
static void __exit te12xp_cleanup(void)
 
1726
{
 
1727
        pci_unregister_driver(&te12xp_driver);
 
1728
}
 
1729
 
 
1730
#ifdef LINUX26
 
1731
module_param(debug, int, S_IRUGO | S_IWUSR);
 
1732
module_param(loopback, int, S_IRUGO | S_IWUSR);
 
1733
module_param(t1e1override, int, S_IRUGO | S_IWUSR);
 
1734
module_param(j1mode, int, S_IRUGO | S_IWUSR);
 
1735
module_param(alarmdebounce, int, S_IRUGO | S_IWUSR);
 
1736
module_param(latency, int, S_IRUGO | S_IWUSR);
 
1737
#ifdef VPM_SUPPORT
 
1738
module_param(vpmsupport, int, S_IRUGO | S_IWUSR);
 
1739
module_param(vpmdtmfsupport, int, S_IRUGO | S_IWUSR);
 
1740
module_param(vpmtsisupport, int, S_IRUGO | S_IWUSR);
 
1741
#endif
 
1742
#else
 
1743
MODULE_PARM(debug, "i");
 
1744
MODULE_PARM(loopback, "i");
 
1745
MODULE_PARM(t1e1override, "i");
 
1746
MODULE_PARM(j1mode, "i");
 
1747
MODULE_PARM(alarmdebounce, "i");
 
1748
#ifdef VPM_SUPPORT
 
1749
MODULE_PARM(vpmsupport, "i");
 
1750
MODULE_PARM(vpmdtmfsupport, "i");
 
1751
MODULE_PARM(vpmtsisupport, "i");
 
1752
MODULE_PARM(vpmnlptype, "i");
 
1753
MODULE_PARM(vpmnlpthresh, "i");
 
1754
MODULE_PARM(vpmnlpmaxsupp, "i");
 
1755
#endif
 
1756
#endif
 
1757
 
 
1758
#ifdef MODULE_LICENSE
 
1759
MODULE_LICENSE("GPL");
 
1760
#endif
 
1761
 
 
1762
module_init(te12xp_init);
 
1763
module_exit(te12xp_cleanup);