Debian Codegraph @

drivers/dahdi/wcte12xp/base.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Digium, Inc. Wildcard TE12xP T1/E1 card Driver
3 *
4 * Written by Michael Spiceland <[email protected]>
5 *
6 * Adapted from the wctdm24xxp and wcte11xp drivers originally
7 * written by Mark Spencer <[email protected]>
8 * Matthew Fredrickson <[email protected]>
9 * William Meadows <[email protected]>
10 *
11 * Copyright (C) 2007-2012, Digium, Inc.
12 *
13 * All rights reserved.
14 *
15 */
16
17 /*
18 * See http://www.asterisk.org for more information about
19 * the Asterisk project. Please do not directly contact
20 * any of the maintainers of this project for assistance;
21 * the project provides a web site, mailing lists and IRC
22 * channels for your use.
23 *
24 * This program is free software, distributed under the terms of
25 * the GNU General Public License Version 2 as published by the
26 * Free Software Foundation. See the LICENSE file included with
27 * this program for more details.
28 */
29
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/pci.h>
37 #include <linux/proc_fs.h>
38 #include <linux/moduleparam.h>
39 #include <linux/interrupt.h>
40 #include <linux/workqueue.h>
41 #include <linux/delay.h>
42 #include <linux/sched.h>
43
44 #include <linux/slab.h>
45
46 #include <dahdi/kernel.h>
47
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
49 #include <linux/sched/signal.h>
50 #endif /* 4.11.0 */
51
52 #include "wct4xxp/wct4xxp.h" /* For certain definitions */
53
54 #include "voicebus/voicebus.h"
55 #include "voicebus/vpmoct.h"
56 #include "wcte12xp.h"
57
58 #include "voicebus/GpakCust.h"
59 #include "voicebus/GpakApi.h"
60
61 #if VOICEBUS_SFRAME_SIZE != SFRAME_SIZE
62 #error VOICEBUS_SFRAME_SIZE != SFRAME_SIZE
63 #endif
64
65 static int debug;
66 static int j1mode = -1;
67 static int alarmdebounce = 2500; /* LOF/LFA def to 2.5s AT&T TR54016*/
68 static int losalarmdebounce = 2500; /* LOS def to 2.5s AT&T TR54016*/
69 static int aisalarmdebounce = 2500; /* AIS(blue) def to 2.5s AT&T TR54016*/
70 static int yelalarmdebounce = 500; /* RAI(yellow) def to 0.5s AT&T devguide */
71 static int t1e1override = -1; /* deprecated */
72 static char *default_linemode = "auto"; /* 'auto', 'e1', 't1', or 'j1' */
73 static int latency = VOICEBUS_DEFAULT_LATENCY;
74 static unsigned int max_latency = VOICEBUS_DEFAULT_MAXLATENCY;
75 static int vpmsupport = 1;
76 static int vpmtsisupport = 0;
77
78 static int vpmnlptype = DEFAULT_NLPTYPE;
79 static int vpmnlpthresh = DEFAULT_NLPTHRESH;
80 static int vpmnlpmaxsupp = DEFAULT_NLPMAXSUPP;
81
82 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec);
83 static int t1xxp_clear_maint(struct dahdi_span *span);
84
85 static const struct dahdi_echocan_features vpm150m_ec_features = {
86 .NLP_automatic = 1,
87 .CED_tx_detect = 1,
88 .CED_rx_detect = 1,
89 };
90
91 static const struct dahdi_echocan_ops vpm150m_ec_ops = {
92 .echocan_free = echocan_free,
93 };
94
95 static struct t1 *ifaces[WC_MAX_IFACES];
96
97 struct t1_desc {
98 const char *name;
99 };
100
101 static const struct t1_desc te120p = {"Wildcard TE120P"};
102 static const struct t1_desc te122 = {"Wildcard TE122"};
103 static const struct t1_desc te121 = {"Wildcard TE121"};
104
105 static inline bool is_pcie(const struct t1 *t1)
106 {
107 return (0 == strcmp(t1->variety, te121.name));
108 }
109
110 /* names of HWEC modules */
111 static const char *vpmadt032_name = "VPMADT032";
112 static const char *vpmoct_name = "VPMOCT032";
113
114 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
115 static kmem_cache_t *cmd_cache;
116 #else
117 static struct kmem_cache *cmd_cache;
118 #endif
119
120 static struct command *get_free_cmd(struct t1 *wc)
121 {
122 struct command *cmd;
123 cmd = kmem_cache_alloc(cmd_cache, GFP_ATOMIC);
124 if (cmd) {
125 memset(cmd, 0, sizeof(*cmd));
126 init_completion(&cmd->complete);
127 INIT_LIST_HEAD(&cmd->node);
128 }
129 return cmd;
130 }
131
132 static void free_cmd(struct t1 *wc, struct command *cmd)
133 {
134 kmem_cache_free(cmd_cache, cmd);
135 }
136
137 static struct command *_get_pending_cmd(struct t1 *wc)
138 {
139 struct command *cmd = NULL;
140 if (!list_empty(&wc->pending_cmds)) {
141 cmd = list_entry(wc->pending_cmds.next, struct command, node);
142 list_move_tail(&cmd->node, &wc->active_cmds);
143 }
144 return cmd;
145 }
146
147 static void submit_cmd(struct t1 *wc, struct command *cmd)
148 {
149 unsigned long flags;
150 spin_lock_irqsave(&wc->reglock, flags);
151 list_add_tail(&cmd->node, &wc->pending_cmds);
152 spin_unlock_irqrestore(&wc->reglock, flags);
153 }
154
155 static void _resend_cmds(struct t1 *wc)
156 {
157 list_splice_init(&wc->active_cmds, &wc->pending_cmds);
158 if (wc->vpmadt032)
159 vpmadt032_resend(wc->vpmadt032);
160 }
161
162 static inline void cmd_dequeue_vpmoct(struct t1 *wc, u8 *eframe)
163 {
164 struct vpmoct *vpm = wc->vpmoct;
165 struct vpmoct_cmd *cmd;
166 u8 i;
167
168 /* Pop a command off pending list */
169 spin_lock(&vpm->list_lock);
170 if (list_empty(&vpm->pending_list)) {
171 spin_unlock(&vpm->list_lock);
172 return;
173 }
174 cmd = list_entry(vpm->pending_list.next, struct vpmoct_cmd, node);
175 /* Push the command onto active list, if it's a syncronous cmd */
176 if (is_vpmoct_cmd_read(cmd))
177 list_move_tail(&cmd->node, &vpm->active_list);
178 else
179 list_del_init(&cmd->node);
180
181 /* Skip audio */
182 eframe += 66;
183
184 /* Save ident so we can match the return eframe */
185 cmd->txident = wc->txident;
186 /* We have four timeslots to work with for a regular spi packet */
187 /* TODO: Create debug flag for this in dev */
188 eframe[CMD_BYTE(0, 0, 1)] = 0x12;
189 eframe[CMD_BYTE(0, 1, 1)] = 0x34;
190 eframe[CMD_BYTE(0, 2, 1)] = 0x56;
191 eframe[CMD_BYTE(1, 0, 1)] = cmd->command;
192 eframe[CMD_BYTE(1, 1, 1)] = cmd->address;
193 eframe[CMD_BYTE(1, 2, 1)] = cmd->data[0];
194 for (i = 1; i < cmd->chunksize; i++) {
195 /* Every time slot is filled with chunk data
196 * ignoring command/address/data structure */
197 eframe[CMD_BYTE(1, 2, 1) + 2*i] = cmd->data[i];
198 }
199
200 /* Clean up fire-and-forget messages from memory */
201 if (list_empty(&cmd->node))
202 kfree(cmd);
203
204 spin_unlock(&vpm->list_lock);
205 #if 0
206 dev_info(&wc->vb.pdev->dev, "Wrote: ");
207 for (i = 0; i < 7; i++) {
208 dev_info(&wc->vb.pdev->dev, "|%x %x %x|",
209 eframe[CMD_BYTE(i, 0, 1)],
210 eframe[CMD_BYTE(i, 1, 1)],
211 eframe[CMD_BYTE(i, 2, 1)]);
212 }
213 #endif
214 }
215
216 static void cmd_dequeue(struct t1 *wc, unsigned char *eframe, int frame_num, int slot)
217 {
218 struct command *curcmd=NULL;
219 u16 address;
220 u8 data;
221 u32 flags;
222
223 /* Skip audio */
224 eframe += 66;
225 /* Search for something waiting to transmit */
226 if ((slot < 6) && (frame_num) && (frame_num < DAHDI_CHUNKSIZE - 1)) {
227 /* only 6 useable cs slots per */
228
229 /* framer */
230 curcmd = _get_pending_cmd(wc);
231 if (curcmd) {
232 curcmd->cs_slot = slot;
233 curcmd->ident = wc->txident;
234
235 address = curcmd->address;
236 data = curcmd->data;
237 flags = curcmd->flags;
238 } else {
239 /* If nothing else, use filler */
240 address = 0x4a;
241 data = 0;
242 flags = __CMD_RD;
243 }
244
245 if (flags & __CMD_WR)
246 eframe[CMD_BYTE(slot, 0, 0)] = 0x0c; /* 0c write command */
247 else if (flags & __CMD_LEDS)
248 eframe[CMD_BYTE(slot, 0, 0)] = 0x10 | ((address) & 0x0E); /* led set command */
249 else if (flags & __CMD_PINS)
250 eframe[CMD_BYTE(slot, 0, 0)] = 0x30; /* CPLD2 pin state */
251 else
252 eframe[CMD_BYTE(slot, 0, 0)] = 0x0a; /* read command */
253 eframe[CMD_BYTE(slot, 1, 0)] = address;
254 eframe[CMD_BYTE(slot, 2, 0)] = data;
255 }
256
257 }
258
259 static inline void cmd_decipher(struct t1 *wc, const u8 *eframe)
260 {
261 struct command *cmd = NULL;
262 const int IS_VPM = 0;
263
264 /* Skip audio */
265 eframe += 66;
266
267 while (!list_empty(&wc->active_cmds)) {
268 cmd = list_entry(wc->active_cmds.next, struct command, node);
269 if (cmd->ident != wc->rxident)
270 break;
271
272 if (cmd->flags & (__CMD_WR | __CMD_LEDS)) {
273 /* Nobody is waiting on writes...so let's just
274 * free them here. */
275 list_del_init(&cmd->node);
276 free_cmd(wc, cmd);
277 } else {
278 cmd->data |= eframe[CMD_BYTE(cmd->cs_slot, 2, IS_VPM)];
279 list_del_init(&cmd->node);
280 complete(&cmd->complete);
281 }
282 }
283 }
284
285 inline void cmd_decipher_vpmoct(struct t1 *wc, const u8 *eframe)
286 {
287 int i;
288 struct vpmoct *vpm = wc->vpmoct;
289 struct vpmoct_cmd *cmd;
290
291 /* Skip audio and first 6 timeslots */
292 eframe += 66;
293
294 spin_lock(&vpm->list_lock);
295 /* No command to handle, just exit */
296 if (list_empty(&vpm->active_list)) {
297 spin_unlock(&vpm->list_lock);
298 return;
299 }
300
301 cmd = list_entry(vpm->active_list.next, struct vpmoct_cmd, node);
302 if (wc->rxident == cmd->txident)
303 list_del_init(&cmd->node);
304 else
305 cmd = NULL;
306 spin_unlock(&vpm->list_lock);
307
308 if (!cmd)
309 return;
310
311 #if 0
312 /* Store result */
313 dev_info(&wc->vb.pdev->dev, "Read: ");
314 for (i = 0; i < 7; i++) {
315 dev_info(&wc->vb.pdev->dev, "|%x %x %x|",
316 eframe[CMD_BYTE(i, 0, 1)],
317 eframe[CMD_BYTE(i, 1, 1)],
318 eframe[CMD_BYTE(i, 2, 1)]);
319 }
320 dev_info(&wc->vb.pdev->dev, "\n");
321 #endif
322 cmd->command = eframe[CMD_BYTE(1, 0, 1)];
323 cmd->address = eframe[CMD_BYTE(1, 1, 1)];
324 for (i = 0; i < cmd->chunksize; ++i)
325 cmd->data[i] = eframe[CMD_BYTE(1, 2, 1) + 2*i];
326 complete(&cmd->complete);
327 }
328
329 inline void cmd_decipher_vpmadt032(struct t1 *wc, const u8 *eframe)
330 {
331 struct vpmadt032 *vpm = wc->vpmadt032;
332 struct vpmadt032_cmd *cmd;
333
334 BUG_ON(!vpm);
335
336 /* If the hardware is not processing any commands currently, then
337 * there is nothing for us to do here. */
338 if (list_empty(&vpm->active_cmds)) {
339 return;
340 }
341
342 spin_lock(&vpm->list_lock);
343 cmd = list_entry(vpm->active_cmds.next, struct vpmadt032_cmd, node);
344 if (wc->rxident == cmd->txident) {
345 list_del_init(&cmd->node);
346 } else {
347 cmd = NULL;
348 }
349 spin_unlock(&vpm->list_lock);
350
351 if (!cmd) {
352 return;
353 }
354
355 /* Skip audio */
356 eframe += 66;
357
358 /* Store result */
359 cmd->data = (0xff & eframe[CMD_BYTE(2, 1, 1)]) << 8;
360 cmd->data |= eframe[CMD_BYTE(2, 2, 1)];
361 if (cmd->desc & __VPM150M_WR) {
362 kfree(cmd);
363 } else {
364 cmd->desc |= __VPM150M_FIN;
365 complete(&cmd->complete);
366 }
367 }
368
369 static int config_vpmadt032(struct vpmadt032 *vpm, struct t1 *wc)
370 {
371 int res, channel;
372 GpakPortConfig_t portconfig = {0};
373 gpakConfigPortStatus_t configportstatus;
374 GPAK_PortConfigStat_t pstatus;
375 GpakChannelConfig_t chanconfig;
376 GPAK_ChannelConfigStat_t cstatus;
377 GPAK_AlgControlStat_t algstatus;
378
379 /* First Serial Port config */
380 portconfig.SlotsSelect1 = SlotCfgNone;
381 portconfig.FirstBlockNum1 = 0;
382 portconfig.FirstSlotMask1 = 0x0000;
383 portconfig.SecBlockNum1 = 1;
384 portconfig.SecSlotMask1 = 0x0000;
385 portconfig.SerialWordSize1 = SerWordSize8;
386 portconfig.CompandingMode1 = cmpNone;
387 portconfig.TxFrameSyncPolarity1 = FrameSyncActHigh;
388 portconfig.RxFrameSyncPolarity1 = FrameSyncActHigh;
389 portconfig.TxClockPolarity1 = SerClockActHigh;
390 portconfig.RxClockPolarity1 = SerClockActHigh;
391 portconfig.TxDataDelay1 = DataDelay0;
392 portconfig.RxDataDelay1 = DataDelay0;
393 portconfig.DxDelay1 = Disabled;
394 portconfig.ThirdSlotMask1 = 0x0000;
395 portconfig.FouthSlotMask1 = 0x0000;
396 portconfig.FifthSlotMask1 = 0x0000;
397 portconfig.SixthSlotMask1 = 0x0000;
398 portconfig.SevenSlotMask1 = 0x0000;
399 portconfig.EightSlotMask1 = 0x0000;
400
401 /* Second Serial Port config */
402 portconfig.SlotsSelect2 = SlotCfg8Groups;
403 portconfig.FirstBlockNum2 = 0;
404 portconfig.FirstSlotMask2 = 0x5554;
405 portconfig.SecBlockNum2 = 1;
406 portconfig.SecSlotMask2 = 0x5555;
407 portconfig.ThirdSlotMask2 = 0x5555;
408 portconfig.FouthSlotMask2 = 0x5555;
409 portconfig.SerialWordSize2 = SerWordSize8;
410 portconfig.CompandingMode2 = cmpNone;
411 portconfig.TxFrameSyncPolarity2 = FrameSyncActHigh;
412 portconfig.RxFrameSyncPolarity2 = FrameSyncActHigh;
413 portconfig.TxClockPolarity2 = SerClockActHigh;
414 portconfig.RxClockPolarity2 = SerClockActHigh;
415 portconfig.TxDataDelay2 = DataDelay0;
416 portconfig.RxDataDelay2 = DataDelay0;
417 portconfig.DxDelay2 = Disabled;
418 portconfig.FifthSlotMask2 = 0x0001;
419 portconfig.SixthSlotMask2 = 0x0000;
420 portconfig.SevenSlotMask2 = 0x0000;
421 portconfig.EightSlotMask2 = 0x0000;
422
423 /* Third Serial Port Config */
424 portconfig.SlotsSelect3 = SlotCfg8Groups;
425 portconfig.FirstBlockNum3 = 0;
426 portconfig.FirstSlotMask3 = 0x5554;
427 portconfig.SecBlockNum3 = 1;
428 portconfig.SecSlotMask3 = 0x5555;
429 portconfig.SerialWordSize3 = SerWordSize8;
430 portconfig.CompandingMode3 = cmpNone;
431 portconfig.TxFrameSyncPolarity3 = FrameSyncActHigh;
432 portconfig.RxFrameSyncPolarity3 = FrameSyncActHigh;
433 portconfig.TxClockPolarity3 = SerClockActHigh;
434 portconfig.RxClockPolarity3 = SerClockActLow;
435 portconfig.TxDataDelay3 = DataDelay0;
436 portconfig.RxDataDelay3 = DataDelay0;
437 portconfig.DxDelay3 = Disabled;
438 portconfig.ThirdSlotMask3 = 0x5555;
439 portconfig.FouthSlotMask3 = 0x5555;
440 portconfig.FifthSlotMask3 = 0x0001;
441 portconfig.SixthSlotMask3 = 0x0000;
442 portconfig.SevenSlotMask3 = 0x0000;
443 portconfig.EightSlotMask3 = 0x0000;
444
445 if ((configportstatus = gpakConfigurePorts(vpm->dspid, &portconfig, &pstatus))) {
446 t1_info(wc, "Configuration of ports failed (%d)!\n",
447 configportstatus);
448 return -1;
449 } else {
450 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN)
451 t1_info(wc, "Configured McBSP ports successfully\n");
452 }
453
454 if ((res = gpakPingDsp(vpm->dspid, &vpm->version))) {
455 t1_info(wc, "Error pinging DSP (%d)\n", res);
456 return -1;
457 }
458
459 for (channel = 0; channel < ARRAY_SIZE(vpm->curecstate); ++channel) {
460 vpm->curecstate[channel].tap_length = 0;
461 vpm->curecstate[channel].nlp_type = vpm->options.vpmnlptype;
462 vpm->curecstate[channel].nlp_threshold = vpm->options.vpmnlpthresh;
463 vpm->curecstate[channel].nlp_max_suppress = vpm->options.vpmnlpmaxsupp;
464
465 vpm->setchanconfig_from_state(vpm, channel, &chanconfig);
466 if ((res = gpakConfigureChannel(vpm->dspid, channel, tdmToTdm, &chanconfig, &cstatus))) {
467 t1_info(wc, "Unable to configure channel #%d (%d)",
468 channel, res);
469 if (res == 1) {
470 printk(KERN_CONT ", reason %d", cstatus);
471 }
472 printk(KERN_CONT "\n");
473 return -1;
474 }
475
476 if ((res = gpakAlgControl(vpm->dspid, channel, BypassEcanA, &algstatus))) {
477 t1_info(wc, "Unable to disable echo can on channel %d "
478 "(reason %d:%d)\n", channel + 1, res,
479 algstatus);
480 return -1;
481 }
482 }
483
484 if ((res = gpakPingDsp(vpm->dspid, &vpm->version))) {
485 t1_info(wc, "Error pinging DSP (%d)\n", res);
486 return -1;
487 }
488
489 set_bit(VPM150M_ACTIVE, &vpm->control);
490
491 return 0;
492 }
493
494 #define debug_printk(wc, lvl, fmt, args...) if (debug >= (lvl)) do { \
495 t1_info((wc), fmt , ## args); } while (0)
496
497 static void cmd_dequeue_vpmadt032(struct t1 *wc, unsigned char *eframe)
498 {
499 struct vpmadt032_cmd *cmd;
500 struct vpmadt032 *vpm = wc->vpmadt032;
501 int x;
502 unsigned char leds = ~((atomic_read(&wc->txints) / 1000) % 8) & 0x7;
503
504 /* Skip audio */
505 eframe += 66;
506
507 if (test_bit(VPM150M_HPIRESET, &vpm->control)) {
508 debug_printk(wc, 1, "HW Resetting VPMADT032 ...\n");
509 for (x = 0; x < 4; x++) {
510 if (!x) {
511 if (test_and_clear_bit(VPM150M_HPIRESET,
512 &vpm->control)) {
513 eframe[CMD_BYTE(x, 0, 1)] = 0x0b;
514 } else {
515 eframe[CMD_BYTE(x, 0, 1)] = leds;
516 }
517 } else {
518 eframe[CMD_BYTE(x, 0, 1)] = 0x00 | leds;
519 }
520 eframe[CMD_BYTE(x, 1, 1)] = 0;
521 eframe[CMD_BYTE(x, 2, 1)] = 0x00;
522 }
523 return;
524 }
525
526 if ((cmd = vpmadt032_get_ready_cmd(vpm))) {
527 cmd->txident = wc->txident;
528 #if 0
529 printk(KERN_DEBUG "Found command txident = %d, desc = 0x%x, addr = 0x%x, data = 0x%x\n", cmd->txident, cmd->desc, cmd->address, cmd->data);
530 #endif
531 if (cmd->desc & __VPM150M_RWPAGE) {
532 /* Set CTRL access to page*/
533 eframe[CMD_BYTE(0, 0, 1)] = (0x8 << 4);
534 eframe[CMD_BYTE(0, 1, 1)] = 0;
535 eframe[CMD_BYTE(0, 2, 1)] = 0x20;
536
537 /* Do a page write */
538 if (cmd->desc & __VPM150M_WR) {
539 eframe[CMD_BYTE(1, 0, 1)] = ((0x8 | 0x4) << 4);
540 } else {
541 eframe[CMD_BYTE(1, 0, 1)] = ((0x8 | 0x4 | 0x1) << 4);
542 }
543 eframe[CMD_BYTE(1, 1, 1)] = 0;
544 if (cmd->desc & __VPM150M_WR) {
545 eframe[CMD_BYTE(1, 2, 1)] = cmd->data & 0xf;
546 } else {
547 eframe[CMD_BYTE(1, 2, 1)] = 0;
548 }
549
550 if (cmd->desc & __VPM150M_WR) {
551 /* Fill in buffer to size */
552 eframe[CMD_BYTE(2, 0, 1)] = 0;
553 eframe[CMD_BYTE(2, 1, 1)] = 0;
554 eframe[CMD_BYTE(2, 2, 1)] = 0;
555 } else {
556 /* Do reads twice b/c of vpmadt032 bug */
557 eframe[CMD_BYTE(2, 0, 1)] = ((0x8 | 0x4 | 0x1) << 4);
558 eframe[CMD_BYTE(2, 1, 1)] = 0;
559 eframe[CMD_BYTE(2, 2, 1)] = 0;
560 }
561
562 /* Clear XADD */
563 eframe[CMD_BYTE(3, 0, 1)] = (0x8 << 4);
564 eframe[CMD_BYTE(3, 1, 1)] = 0;
565 eframe[CMD_BYTE(3, 2, 1)] = 0;
566
567 /* Fill in buffer to size */
568 eframe[CMD_BYTE(4, 0, 1)] = 0;
569 eframe[CMD_BYTE(4, 1, 1)] = 0;
570 eframe[CMD_BYTE(4, 2, 1)] = 0;
571
572 } else {
573 /* Set address */
574 eframe[CMD_BYTE(0, 0, 1)] = ((0x8 | 0x4) << 4);
575 eframe[CMD_BYTE(0, 1, 1)] = (cmd->address >> 8) & 0xff;
576 eframe[CMD_BYTE(0, 2, 1)] = cmd->address & 0xff;
577
578 /* Send/Get our data */
579 if (cmd->desc & __VPM150M_WR) {
580 eframe[CMD_BYTE(1, 0, 1)] = ((0x8 | (0x3 << 1)) << 4);
581 } else {
582 eframe[CMD_BYTE(1, 0, 1)] = ((0x8 | (0x3 << 1) | 0x1) << 4);
583 }
584 eframe[CMD_BYTE(1, 1, 1)] = (cmd->data >> 8) & 0xff;
585 eframe[CMD_BYTE(1, 2, 1)] = cmd->data & 0xff;
586
587 if (cmd->desc & __VPM150M_WR) {
588 /* Fill in */
589 eframe[CMD_BYTE(2, 0, 1)] = 0;
590 eframe[CMD_BYTE(2, 1, 1)] = 0;
591 eframe[CMD_BYTE(2, 2, 1)] = 0;
592 } else {
593 /* Do this again for reads b/c of the bug in vpmadt032 */
594 eframe[CMD_BYTE(2, 0, 1)] = ((0x8 | (0x3 << 1) | 0x1) << 4);
595 eframe[CMD_BYTE(2, 1, 1)] = (cmd->data >> 8) & 0xff;
596 eframe[CMD_BYTE(2, 2, 1)] = cmd->data & 0xff;
597 }
598
599 /* Fill in the rest */
600 eframe[CMD_BYTE(3, 0, 1)] = 0;
601 eframe[CMD_BYTE(3, 1, 1)] = 0;
602 eframe[CMD_BYTE(3, 2, 1)] = 0;
603
604 /* Fill in the rest */
605 eframe[CMD_BYTE(4, 0, 1)] = 0;
606 eframe[CMD_BYTE(4, 1, 1)] = 0;
607 eframe[CMD_BYTE(4, 2, 1)] = 0;
608 }
609 } else if (test_and_clear_bit(VPM150M_SWRESET, &vpm->control)) {
610 for (x = 0; x < 7; x++) {
611 if (0 == x) {
612 eframe[CMD_BYTE(x, 0, 1)] = (0x8 << 4);
613 } else {
614 eframe[CMD_BYTE(x, 0, 1)] = 0x00;
615 }
616 eframe[CMD_BYTE(x, 1, 1)] = 0;
617 if (0 == x) {
618 eframe[CMD_BYTE(x, 2, 1)] = 0x01;
619 } else {
620 eframe[CMD_BYTE(x, 2, 1)] = 0x00;
621 }
622 }
623 } else {
624 for (x = 0; x < 7; x++) {
625 eframe[CMD_BYTE(x, 0, 1)] = 0x00;
626 eframe[CMD_BYTE(x, 1, 1)] = 0x00;
627 eframe[CMD_BYTE(x, 2, 1)] = 0x00;
628 }
629 }
630
631 /* Add our leds in */
632 for (x = 0; x < 7; x++)
633 eframe[CMD_BYTE(x, 0, 1)] |= leds;
634 }
635
636 static inline int t1_setreg(struct t1 *wc, int addr, int val)
637 {
638 struct command *cmd;
639 cmd = get_free_cmd(wc);
640 if (!cmd) {
641 WARN_ON(1);
642 return -ENOMEM;
643 }
644 cmd->address = addr;
645 cmd->data = val;
646 cmd->flags |= __CMD_WR;
647 submit_cmd(wc, cmd);
648 return 0;
649 }
650
651 static void __t1_getreg(struct t1 *wc, int addr, struct command *cmd)
652 {
653 cmd->address = addr;
654 cmd->data = 0x00;
655 cmd->flags = __CMD_RD;
656 submit_cmd(wc, cmd);
657 }
658
659 static int __t1_getresult(struct t1 *wc, struct command *cmd)
660 {
661 int ret;
662 unsigned long flags;
663
664 might_sleep();
665
666 if (test_bit(IOERROR, &wc->bit_flags)) {
667 spin_lock_irqsave(&wc->reglock, flags);
668 list_del_init(&cmd->node);
669 spin_unlock_irqrestore(&wc->reglock, flags);
670 if (printk_ratelimit()) {
671 dev_warn(&wc->vb.pdev->dev,
672 "Timeout in %s\n", __func__);
673 }
674 return -EIO;
675 }
676
677 ret = wait_for_completion_interruptible_timeout(&cmd->complete, HZ*10);
678 if (unlikely(!ret)) {
679 spin_lock_irqsave(&wc->reglock, flags);
680 if (!list_empty(&cmd->node)) {
681 /* Since we've removed this command from the list, we
682 * can go ahead and free it right away. */
683 list_del_init(&cmd->node);
684 spin_unlock_irqrestore(&wc->reglock, flags);
685 if (-ERESTARTSYS != ret) {
686 if (printk_ratelimit()) {
687 dev_warn(&wc->vb.pdev->dev,
688 "Timeout in %s\n", __func__);
689 }
690 ret = -EIO;
691 set_bit(IOERROR, &wc->bit_flags);
692 }
693 return ret;
694 } else {
695 /* Looks like this command was removed from the list by
696 * someone else already. Let's wait for them to complete
697 * it so that we don't free up the memory. */
698 spin_unlock_irqrestore(&wc->reglock, flags);
699 ret = wait_for_completion_timeout(&cmd->complete, HZ*2);
700 WARN_ON(!ret);
701 ret = cmd->data;
702 return ret;
703 }
704 }
705 ret = cmd->data;
706 return ret;
707 }
708
709 static int t1_getreg(struct t1 *wc, int addr)
710 {
711 int res;
712 struct command *cmd = NULL;
713 cmd = get_free_cmd(wc);
714 if (!cmd)
715 return -ENOMEM;
716 __t1_getreg(wc, addr, cmd);
717 res = __t1_getresult(wc, cmd);
718 free_cmd(wc, cmd);
719 return res;
720 }
721
722 static void t1_setleds(struct t1 *wc, int leds)
723 {
724 struct command *cmd;
725
726 leds = (~leds) & 0x0E; /* invert the LED bits (3 downto 1)*/
727
728 cmd = get_free_cmd(wc);
729 if (!cmd)
730 return;
731 cmd->flags |= __CMD_LEDS;
732 cmd->address = leds;
733 submit_cmd(wc, cmd);
734 }
735
736 /**
737 * t1_getpins - Returns the value of the jumpers on the card.
738 * @wc: The card to read from.
739 * @pins: Pointer to u8 character to hold the pins value.
740 *
741 * Returns 0 on success, otherwise an error code.
742 *
743 */
744 static int t1_getpins(struct t1 *wc, u8 *pins)
745 {
746 struct command *cmd;
747 unsigned long flags;
748 unsigned long ret;
749
750 *pins = 0;
751
752 cmd = get_free_cmd(wc);
753 BUG_ON(!cmd);
754
755 cmd->address = 0x00;
756 cmd->data = 0x00;
757 cmd->flags = __CMD_PINS;
758 submit_cmd(wc, cmd);
759 ret = wait_for_completion_interruptible_timeout(&cmd->complete, HZ*2);
760 if (unlikely(!ret)) {
761 spin_lock_irqsave(&wc->reglock, flags);
762 list_del_init(&cmd->node);
763 spin_unlock_irqrestore(&wc->reglock, flags);
764 free_cmd(wc, cmd);
765 if (-ERESTARTSYS != ret) {
766 if (printk_ratelimit()) {
767 dev_warn(&wc->vb.pdev->dev,
768 "Timeout in %s\n", __func__);
769 }
770 ret = -EIO;
771 }
772 return ret;
773 }
774 *pins = cmd->data;
775 free_cmd(wc, cmd);
776 return 0;
777 }
778
779 static void __t1xxp_set_clear(struct t1 *wc)
780 {
781 int i, offset;
782 int ret;
783 unsigned short reg[3] = {0, 0, 0};
784
785 /* Calculate all states on all 24 channels using the channel
786 flags, then write all 3 clear channel registers at once */
787
788 for (i = 0; i < wc->span.channels; i++) {
789 offset = i/8;
790 if (wc->span.chans[i]->flags & DAHDI_FLAG_CLEAR)
791 reg[offset] |= 1 << (7 - (i % 8));
792 else
793 reg[offset] &= ~(1 << (7 - (i % 8)));
794 }
795
796 ret = t1_setreg(wc, CCB1, reg[0]);
797 if (ret < 0)
798 t1_info(wc, "Unable to set clear/rbs mode!\n");
799
800 ret = t1_setreg(wc, CCB2, reg[1]);
801 if (ret < 0)
802 t1_info(wc, "Unable to set clear/rbs mode!\n");
803
804 ret = t1_setreg(wc, CCB3, reg[2]);
805 if (ret < 0)
806 t1_info(wc, "Unable to set clear/rbs mode!\n");
807 }
808
809 /**
810 * _t1_free_channels - Free the memory allocated for the channels.
811 *
812 * Must be called with wc->reglock held.
813 *
814 */
815 static void _t1_free_channels(struct t1 *wc)
816 {
817 int x;
818 for (x = 0; x < ARRAY_SIZE(wc->chans); x++) {
819 kfree(wc->chans[x]);
820 kfree(wc->ec[x]);
821 wc->chans[x] = NULL;
822 wc->ec[x] = NULL;
823 }
824 }
825
826 static void free_wc(struct t1 *wc)
827 {
828 unsigned long flags;
829 struct command *cmd;
830 LIST_HEAD(list);
831
832 spin_lock_irqsave(&wc->reglock, flags);
833 _t1_free_channels(wc);
834 list_splice_init(&wc->active_cmds, &list);
835 list_splice_init(&wc->pending_cmds, &list);
836 spin_unlock_irqrestore(&wc->reglock, flags);
837 while (!list_empty(&list)) {
838 cmd = list_entry(list.next, struct command, node);
839 list_del_init(&cmd->node);
840 free_cmd(wc, cmd);
841 }
842
843 if (wc->wq)
844 destroy_workqueue(wc->wq);
845
846 #ifdef CONFIG_VOICEBUS_ECREFERENCE
847 for (x = 0; x < ARRAY_SIZE(wc->ec_reference); ++x) {
848 if (wc->ec_reference[x])
849 dahdi_fifo_free(wc->ec_reference[x]);
850 }
851 #endif
852
853 kfree(wc->ddev->location);
854 kfree(wc->ddev->devicetype);
855 dahdi_free_device(wc->ddev);
856 kfree(wc);
857 }
858
859 /**
860 * t1_reset_registers - Put register back to their default values
861 *
862 * Since the card does not have an ability to reset just the framer
863 * specifically, we need to write all the default values to the framer.
864 *
865 */
866 static void t1_reset_registers(struct t1 *wc)
867 {
868 int i;
869 struct t1_reg {
870 u8 address;
871 u8 value;
872 } __attribute__((packed));
873 struct t1_reg *reg;
874 static struct t1_reg DEFAULT_REGS[] = {
875 {0x00, 0x7d}, {0x01, 0x7d}, {0x02, 0x00}, {0x03, 0x00},
876 {0x04, 0xfd}, {0x05, 0xff}, {0x06, 0xff}, {0x07, 0xff},
877 {0x08, 0x05}, {0x09, 0x00}, {0x0a, 0x00}, {0x0b, 0x00},
878 {0x0c, 0x00}, {0x0d, 0x00}, {0x0e, 0x00}, {0x0f, 0x00},
879 {0x10, 0x00}, {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00},
880 {0x14, 0xff}, {0x15, 0xff}, {0x16, 0xff}, {0x17, 0xff},
881 {0x18, 0xff}, {0x19, 0xff}, {0x1a, 0x00}, {0x1b, 0x00},
882 {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00}, {0x1f, 0x00},
883 {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x04},
884 {0x24, 0x00}, {0x25, 0x05}, {0x26, 0x7b}, {0x27, 0x03},
885 {0x28, 0x40}, {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00},
886 {0x2c, 0x00}, {0x2d, 0x00}, {0x2e, 0x00}, {0x2f, 0x00},
887 {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00}, {0x33, 0x00},
888 {0x34, 0x00}, {0x35, 0x00}, {0x36, 0x00}, {0x37, 0x80},
889 {0x38, 0x00}, {0x39, 0x00}, {0x3a, 0x20}, {0x3b, 0x00},
890 {0x3c, 0x00}, {0x3d, 0x00}, {0x3e, 0x0a}, {0x3f, 0x00},
891 {0x40, 0x04}, {0x41, 0x00}, {0x42, 0x00}, {0x43, 0x00},
892 {0x44, 0x30}, {0x45, 0x00}, {0x46, 0xc0}, {0x47, 0xff},
893 {0x48, 0x00}, {0x49, 0x1c}, {0x4a, 0x05}, {0x4b, 0x03},
894 {0x4c, 0xa3}, {0x4d, 0x28}, {0x4e, 0x00}, {0x4f, 0xc0},
895 {0x50, 0x00}, {0x51, 0x00}, {0x52, 0x00}, {0x53, 0x00},
896 {0x54, 0x00}, {0x55, 0x00}, {0x56, 0x00}, {0x57, 0x00},
897 {0x58, 0x00}, {0x59, 0x00}, {0x5a, 0x00}, {0x5b, 0x00},
898 {0x5c, 0x00}, {0x5d, 0x00}, {0x5e, 0x00}, {0x5f, 0x00},
899 {0x60, 0x00}, {0x61, 0x20}, {0x62, 0x00}, {0x63, 0x00},
900 {0x64, 0x5a}, {0x65, 0x02}, {0x66, 0x00}, {0x67, 0x00},
901 {0x68, 0x10}, {0x69, 0x09}, {0x6a, 0x00}, {0x6b, 0x03},
902 {0x6c, 0x00}, {0x6d, 0xc0}, {0x6e, 0x40}, {0x6f, 0x00},
903 {0x70, 0x00}, {0x71, 0x00}, {0x72, 0x00}, {0x73, 0x00},
904 {0x74, 0x00}, {0x75, 0x00}, {0x76, 0x00}, {0x77, 0x00},
905 {0x78, 0x00}, {0x79, 0x00}, {0x7a, 0x00}, {0x7b, 0x00},
906 {0x7c, 0x00}, {0x7d, 0x00}, {0x7e, 0x00}, {0x7f, 0x00},
907 {0x80, 0x00}, {0x81, 0x22}, {0x82, 0x65}, {0x83, 0x35},
908 {0x84, 0x31}, {0x85, 0x60}, {0x86, 0x03}, {0x87, 0x00},
909 {0x88, 0x00}, {0x89, 0x00}, {0x8a, 0x00}, {0x8b, 0x00},
910 {0x8c, 0x00}, {0x8d, 0x00}, {0x8e, 0x00}, {0x8f, 0x00},
911 {0x90, 0x00}, {0x91, 0x00}, {0x92, 0x00}, {0x93, 0x18},
912 {0x94, 0xfb}, {0x95, 0x0b}, {0x96, 0x00}, {0x97, 0x0b},
913 {0x98, 0xdb}, {0x99, 0xdf}, {0x9a, 0x48}, {0x9b, 0x00},
914 {0x9c, 0x3f}, {0x9d, 0x3f}, {0x9e, 0x77}, {0x9f, 0x77},
915 {0xa0, 0x00}, {0xa1, 0xff}, {0xa2, 0xff}, {0xa3, 0xff},
916 {0xa4, 0x00}, {0xa5, 0x00}, {0xa6, 0x00}, {0xa7, 0x00},
917 {0xa8, 0x00}
918 };
919
920 for (i = 0; i < ARRAY_SIZE(DEFAULT_REGS); ++i) {
921 reg = &DEFAULT_REGS[i];
922 t1_setreg(wc, reg->address, reg->value);
923 }
924 /* Flush previous writes. */
925 t1_getreg(wc, 0x1d);
926 }
927
928 static void t4_serial_setup(struct t1 *wc)
929 {
930 t1_setreg(wc, 0x85, 0xe0); /* GPC1: Multiplex mode enabled, FSC is output, active low, RCLK from channel 0 */
931 t1_setreg(wc, 0x08, 0x05); /* IPC: Interrupt push/pull active low */
932
933 /* Global clocks (8.192 Mhz CLK) */
934 t1_setreg(wc, 0x92, 0x00);
935 t1_setreg(wc, 0x93, 0x18);
936 t1_setreg(wc, 0x94, 0xfb);
937 t1_setreg(wc, 0x95, 0x0b);
938 t1_setreg(wc, 0x96, 0x00);
939 t1_setreg(wc, 0x97, 0x0b);
940 t1_setreg(wc, 0x98, 0xdb);
941 t1_setreg(wc, 0x99, 0xdf);
942
943 /* Configure interrupts */
944 t1_setreg(wc, 0x46, 0xc0); /* GCR: Interrupt on Activation/Deactivation of AIX, LOS */
945
946 /* Configure system interface */
947 t1_setreg(wc, 0x3e, 0x0a /* 0x02 */); /* SIC1: 4.096 Mhz clock/bus, double buffer receive / transmit, byte interleaved */
948 t1_setreg(wc, 0x3f, 0x00); /* SIC2: No FFS, no center receive eliastic buffer, phase 0 */
949 t1_setreg(wc, 0x40, 0x04); /* SIC3: Edges for capture */
950 t1_setreg(wc, 0x44, 0x30); /* CMR1: RCLK is at 8.192 Mhz dejittered */
951 t1_setreg(wc, 0x45, 0x00); /* CMR2: We provide sync and clock for tx and rx. */
952 t1_setreg(wc, 0x22, 0x00); /* XC0: Normal operation of Sa-bits */
953 t1_setreg(wc, 0x23, 0x04); /* XC1: 0 offset */
954 t1_setreg(wc, 0x24, 0x00); /* RC0: Just shy of 255 */
955 t1_setreg(wc, 0x25, 0x05); /* RC1: The rest of RC0 */
956
957 /* Configure ports */
958 t1_setreg(wc, 0x80, 0x00); /* PC1: SPYR/SPYX input on RPA/XPA */
959 t1_setreg(wc, 0x81, 0x22); /* PC2: RMFB/XSIG output/input on RPB/XPB */
960 t1_setreg(wc, 0x82, 0x65); /* PC3: Some unused stuff */
961 t1_setreg(wc, 0x83, 0x35); /* PC4: Some more unused stuff */
962 t1_setreg(wc, 0x84, 0x31); /* PC5: XMFS active low, SCLKR is input, RCLK is output */
963 t1_setreg(wc, 0x86, 0x03); /* PC6: CLK1 is Tx Clock output, CLK2 is 8.192 Mhz from DCO-R */
964 t1_setreg(wc, 0x3b, 0x00); /* Clear LCR1 */
965 }
966
967 static void t1_configure_t1(struct t1 *wc, int lineconfig, int txlevel)
968 {
969 unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
970 char *framing, *line;
971 int mytxlevel;
972
973 if ((txlevel > 7) || (txlevel < 4))
974 mytxlevel = 0;
975 else
976 mytxlevel = txlevel - 4;
977 fmr1 = 0x9e; /* FMR1: Mode 0, T1 mode, CRC on for ESF, 2.048 Mhz system data rate, no XAIS */
978 fmr2 = 0x20; /* FMR2: no payload loopback, don't auto yellow alarm */
979
980
981 if (SPANTYPE_DIGITAL_J1 == wc->span.spantype)
982 fmr4 = 0x1c;
983 else
984 fmr4 = 0x0c; /* FMR4: Lose sync on 2 out of 5 framing bits, auto resync */
985
986 lim2 = 0x21; /* LIM2: 50% peak is a "1", Advanced Loss recovery */
987 lim2 |= (mytxlevel << 6); /* LIM2: Add line buildout */
988 t1_setreg(wc, 0x1d, fmr1);
989 t1_setreg(wc, 0x1e, fmr2);
990
991 /* Configure line interface */
992 if (lineconfig & DAHDI_CONFIG_AMI) {
993 line = "AMI";
994 fmr0 = 0xa0;
995 } else {
996 line = "B8ZS";
997 fmr0 = 0xf0;
998 }
999 if (lineconfig & DAHDI_CONFIG_D4) {
1000 framing = "D4";
1001 } else {
1002 framing = "ESF";
1003 fmr4 |= 0x2;
1004 fmr2 |= 0xc0;
1005 }
1006 t1_setreg(wc, 0x1c, fmr0);
1007
1008 t1_setreg(wc, 0x20, fmr4);
1009 t1_setreg(wc, 0x21, 0x40); /* FMR5: Enable RBS mode */
1010
1011 t1_setreg(wc, 0x37, 0xf8); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
1012 t1_setreg(wc, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
1013
1014 t1_setreg(wc, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
1015 t1_setreg(wc, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
1016
1017 t1_setreg(wc, 0x3a, lim2); /* LIM2: 50% peak amplitude is a "1" */
1018 t1_setreg(wc, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
1019 t1_setreg(wc, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
1020
1021 if (SPANTYPE_DIGITAL_J1 == wc->span.spantype)
1022 t1_setreg(wc, 0x24, 0x80); /* J1 overide */
1023
1024 /* Generate pulse mask for T1 */
1025 switch (mytxlevel) {
1026 case 3:
1027 t1_setreg(wc, 0x26, 0x07); /* XPM0 */
1028 t1_setreg(wc, 0x27, 0x01); /* XPM1 */
1029 t1_setreg(wc, 0x28, 0x00); /* XPM2 */
1030 break;
1031 case 2:
1032 t1_setreg(wc, 0x26, 0x8c); /* XPM0 */
1033 t1_setreg(wc, 0x27, 0x11); /* XPM1 */
1034 t1_setreg(wc, 0x28, 0x01); /* XPM2 */
1035 break;
1036 case 1:
1037 t1_setreg(wc, 0x26, 0x8c); /* XPM0 */
1038 t1_setreg(wc, 0x27, 0x01); /* XPM1 */
1039 t1_setreg(wc, 0x28, 0x00); /* XPM2 */
1040 break;
1041 case 0:
1042 default:
1043 t1_setreg(wc, 0x26, 0xd7); /* XPM0 */
1044 t1_setreg(wc, 0x27, 0x22); /* XPM1 */
1045 t1_setreg(wc, 0x28, 0x01); /* XPM2 */
1046 break;
1047 }
1048
1049 if (debug)
1050 t1_info(wc, "Span configured for %s/%s\n", framing, line);
1051 }
1052
1053 static void t1_configure_e1(struct t1 *wc, int lineconfig)
1054 {
1055 unsigned int fmr2, fmr1, fmr0;
1056 unsigned int cas = 0;
1057 char *crc4 = "";
1058 char *framing, *line;
1059
1060 fmr1 = 0x46; /* FMR1: E1 mode, Automatic force resync, PCM30 mode, 8.192 Mhz backplane, no XAIS */
1061 fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of multiframe recovery, no payload loopback */
1062
1063 if (lineconfig & DAHDI_CONFIG_CRC4) {
1064 fmr1 |= 0x08; /* CRC4 transmit */
1065 fmr2 |= 0xc0; /* CRC4 receive */
1066 crc4 = "/CRC4";
1067 }
1068 t1_setreg(wc, 0x1d, fmr1);
1069 t1_setreg(wc, 0x1e, fmr2);
1070
1071 /* Configure line interface */
1072 if (lineconfig & DAHDI_CONFIG_AMI) {
1073 line = "AMI";
1074 fmr0 = 0xa0;
1075 } else {
1076 line = "HDB3";
1077 fmr0 = 0xf0;
1078 }
1079 if (lineconfig & DAHDI_CONFIG_CCS) {
1080 framing = "CCS";
1081 } else {
1082 framing = "CAS";
1083 cas = 0x40;
1084 }
1085 t1_setreg(wc, 0x1c, fmr0);
1086
1087 t1_setreg(wc, 0x37, 0xf0 /*| 0x6 */ ); /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No remote loop, no DRS */
1088 t1_setreg(wc, 0x36, 0x08); /* LIM0: Enable auto long haul mode, no local loop (must be after LIM1) */
1089
1090 t1_setreg(wc, 0x02, 0x50); /* CMDR: Reset the receiver and transmitter line interface */
1091 t1_setreg(wc, 0x02, 0x00); /* CMDR: Reset the receiver and transmitter line interface */
1092
1093 /* Condition receive line interface for E1 after reset */
1094 t1_setreg(wc, 0xbb, 0x17);
1095 t1_setreg(wc, 0xbc, 0x55);
1096 t1_setreg(wc, 0xbb, 0x97);
1097 t1_setreg(wc, 0xbb, 0x11);
1098 t1_setreg(wc, 0xbc, 0xaa);
1099 t1_setreg(wc, 0xbb, 0x91);
1100 t1_setreg(wc, 0xbb, 0x12);
1101 t1_setreg(wc, 0xbc, 0x55);
1102 t1_setreg(wc, 0xbb, 0x92);
1103 t1_setreg(wc, 0xbb, 0x0c);
1104 t1_setreg(wc, 0xbb, 0x00);
1105 t1_setreg(wc, 0xbb, 0x8c);
1106
1107 t1_setreg(wc, 0x3a, 0x20); /* LIM2: 50% peak amplitude is a "1" */
1108 t1_setreg(wc, 0x38, 0x0a); /* PCD: LOS after 176 consecutive "zeros" */
1109 t1_setreg(wc, 0x39, 0x15); /* PCR: 22 "ones" clear LOS */
1110
1111 t1_setreg(wc, 0x20, 0x9f); /* XSW: Spare bits all to 1 */
1112 t1_setreg(wc, 0x21, 0x1c|cas); /* XSP: E-bit set when async. AXS
1113 auto, XSIF to 1 */
1114
1115
1116 /* Generate pulse mask for E1 */
1117 t1_setreg(wc, 0x26, 0x54); /* XPM0 */
1118 t1_setreg(wc, 0x27, 0x02); /* XPM1 */
1119 t1_setreg(wc, 0x28, 0x00); /* XPM2 */
1120
1121 t1_info(wc, "Span configured for %s/%s%s\n", framing, line, crc4);
1122 }
1123
1124 static void t1xxp_framer_start(struct t1 *wc)
1125 {
1126 if (dahdi_is_e1_span(&wc->span)) {
1127 t1_configure_e1(wc, wc->span.lineconfig);
1128 } else { /* is a T1 card */
1129 t1_configure_t1(wc, wc->span.lineconfig, wc->span.txlevel);
1130 __t1xxp_set_clear(wc);
1131 }
1132
1133 set_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags);
1134 }
1135
1136 static void set_span_devicetype(struct t1 *wc)
1137 {
1138 const char *olddevicetype;
1139 olddevicetype = wc->ddev->devicetype;
1140
1141 #if defined(VPM_SUPPORT)
1142 if (wc->vpmadt032) {
1143 wc->ddev->devicetype = kasprintf(GFP_KERNEL,
1144 "%s (VPMADT032)", wc->variety);
1145 } else if (wc->vpmoct) {
1146 wc->ddev->devicetype = kasprintf(GFP_KERNEL,
1147 "%s (VPMOCT032)", wc->variety);
1148 } else {
1149 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s", wc->variety);
1150 }
1151 #else
1152 wc->ddev->devicetype = kasprintf(GFP_KERNEL, "%s", wc->variety);
1153 #endif
1154
1155 /* On the off chance that we were able to allocate it previously. */
1156 if (!wc->ddev->devicetype)
1157 wc->ddev->devicetype = olddevicetype;
1158 else
1159 kfree(olddevicetype);
1160 }
1161
1162 static int t1xxp_startup(struct file *file, struct dahdi_span *span)
1163 {
1164 struct t1 *wc = container_of(span, struct t1, span);
1165 #ifndef CONFIG_VOICEBUS_ECREFERENCE
1166 unsigned int i;
1167 #endif
1168
1169 set_span_devicetype(wc);
1170
1171 #ifndef CONFIG_VOICEBUS_ECREFERENCE
1172 /* initialize the start value for the entire chunk of last ec buffer */
1173 for (i = 0; i < span->channels; i++) {
1174 memset(wc->ec_chunk1[i], DAHDI_LIN2X(0, span->chans[i]), DAHDI_CHUNKSIZE);
1175 memset(wc->ec_chunk2[i], DAHDI_LIN2X(0, span->chans[i]), DAHDI_CHUNKSIZE);
1176 }
1177 #endif
1178
1179 /* Reset framer with proper parameters and start */
1180 t1xxp_framer_start(wc);
1181 debug_printk(wc, 1, "Calling startup (flags is %lu)\n", span->flags);
1182
1183 return 0;
1184 }
1185
1186 static inline bool is_initialized(struct t1 *wc)
1187 {
1188 WARN_ON(wc->not_ready < 0);
1189 return (wc->not_ready == 0);
1190 }
1191
1192 /**
1193 * t1_wait_for_ready
1194 *
1195 * Check if the board has finished any setup and is ready to start processing
1196 * calls.
1197 */
1198 static int t1_wait_for_ready(struct t1 *wc)
1199 {
1200 while (!is_initialized(wc)) {
1201 if (fatal_signal_pending(current))
1202 return -EIO;
1203 msleep_interruptible(250);
1204 }
1205 return 0;
1206 }
1207
1208 static int t1xxp_chanconfig(struct file *file,
1209 struct dahdi_chan *chan, int sigtype)
1210 {
1211 struct t1 *wc = chan->pvt;
1212
1213 if (file->f_flags & O_NONBLOCK && !is_initialized(wc)) {
1214 return -EAGAIN;
1215 } else {
1216 t1_wait_for_ready(wc);
1217 }
1218
1219 if (test_bit(DAHDI_FLAGBIT_RUNNING, &chan->span->flags) &&
1220 dahdi_is_t1_span(&wc->span)) {
1221 __t1xxp_set_clear(wc);
1222 }
1223 return 0;
1224 }
1225
1226 static int t1xxp_rbsbits(struct dahdi_chan *chan, int bits)
1227 {
1228 u_char m,c;
1229 int n,b;
1230 struct t1 *wc = chan->pvt;
1231 unsigned long flags;
1232
1233 debug_printk(wc, 2, "Setting bits to %d on channel %s\n",
1234 bits, chan->name);
1235 if (dahdi_is_e1_span(&wc->span)) { /* do it E1 way */
1236 if (chan->chanpos == 16)
1237 return 0;
1238
1239 n = chan->chanpos - 1;
1240 if (chan->chanpos > 15) n--;
1241 b = (n % 15);
1242 spin_lock_irqsave(&wc->reglock, flags);
1243 c = wc->txsigs[b];
1244 m = (n / 15) << 2; /* nibble selector */
1245 c &= (0xf << m); /* keep the other nibble */
1246 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1247 wc->txsigs[b] = c;
1248 spin_unlock_irqrestore(&wc->reglock, flags);
1249 /* output them to the chip */
1250 t1_setreg(wc, 0x71 + b, c);
1251 } else if (wc->span.lineconfig & DAHDI_CONFIG_D4) {
1252 n = chan->chanpos - 1;
1253 b = (n / 4);
1254 spin_lock_irqsave(&wc->reglock, flags);
1255 c = wc->txsigs[b];
1256 m = ((3 - (n % 4)) << 1); /* nibble selector */
1257 c &= ~(0x3 << m); /* keep the other nibble */
1258 c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
1259 wc->txsigs[b] = c;
1260 spin_unlock_irqrestore(&wc->reglock, flags);
1261 /* output them to the chip */
1262 t1_setreg(wc, 0x70 + b, c);
1263 t1_setreg(wc, 0x70 + b + 6, c);
1264 } else if (wc->span.lineconfig & DAHDI_CONFIG_ESF) {
1265 n = chan->chanpos - 1;
1266 b = (n / 2);
1267 spin_lock_irqsave(&wc->reglock, flags);
1268 c = wc->txsigs[b];
1269 m = ((n % 2) << 2); /* nibble selector */
1270 c &= (0xf << m); /* keep the other nibble */
1271 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1272 wc->txsigs[b] = c;
1273 spin_unlock_irqrestore(&wc->reglock, flags);
1274 /* output them to the chip */
1275 t1_setreg(wc, 0x70 + b, c);
1276 }
1277 debug_printk(wc, 2, "Finished setting RBS bits\n");
1278
1279 return 0;
1280 }
1281
1282 static void t1_check_sigbits(struct t1 *wc)
1283 {
1284 struct command_container {
1285 struct command *cmd;
1286 struct list_head node;
1287 unsigned int index;
1288 };
1289 struct command_container *cont;
1290 LIST_HEAD(commands);
1291 int a,i,rxs;
1292
1293 if (!(test_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags)))
1294 return;
1295
1296 if (dahdi_is_e1_span(&wc->span)) {
1297 /* Send out all the commands first. */
1298 for (i = 0; i < 15; i++) {
1299 if (!(wc->span.chans[i+16]->sig & DAHDI_SIG_CLEAR) ||
1300 !(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
1301 cont = kzalloc(sizeof(*cont), GFP_KERNEL);
1302 if (!cont) {
1303 WARN_ON_ONCE(1);
1304 goto done;
1305 }
1306 cont->cmd = get_free_cmd(wc);
1307 if (!cont->cmd) {
1308 WARN_ON_ONCE(1);
1309 goto done;
1310 }
1311 cont->index = i;
1312 list_add_tail(&cont->node, &commands);
1313 __t1_getreg(wc, 0x71 + i, cont->cmd);
1314 }
1315 }
1316
1317 /* Now check the results */
1318 list_for_each_entry_reverse(cont, &commands, node) {
1319 i = cont->index;
1320 a = __t1_getresult(wc, cont->cmd);
1321 free_cmd(wc, cont->cmd);
1322 cont->cmd = NULL;
1323 if (a > -1) {
1324 /* Get high channel in low bits */
1325 rxs = (a & 0xf);
1326 if (!(wc->span.chans[i+16]->sig & DAHDI_SIG_CLEAR)) {
1327 if (wc->span.chans[i+16]->rxsig != rxs) {
1328 dahdi_rbsbits(wc->span.chans[i+16], rxs);
1329 }
1330 }
1331 rxs = (a >> 4) & 0xf;
1332 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
1333 if (wc->span.chans[i]->rxsig != rxs) {
1334 dahdi_rbsbits(wc->span.chans[i], rxs);
1335 }
1336 }
1337 }
1338 }
1339 } else if (wc->span.lineconfig & DAHDI_CONFIG_D4) {
1340 /* First we'll send out the commands */
1341 for (i = 0; i < 24; i += 4) {
1342 cont = kzalloc(sizeof(*cont), GFP_KERNEL);
1343 if (!cont) {
1344 WARN_ON_ONCE(1);
1345 goto done;
1346 }
1347 cont->cmd = get_free_cmd(wc);
1348 if (!cont->cmd) {
1349 WARN_ON_ONCE(1);
1350 goto done;
1351 }
1352 cont->index = i;
1353 list_add_tail(&cont->node, &commands);
1354 __t1_getreg(wc, 0x70 + (i>>2), cont->cmd);
1355 }
1356
1357 /* Now we'll check the results */
1358 list_for_each_entry_reverse(cont, &commands, node) {
1359 i = cont->index;
1360 a = __t1_getresult(wc, cont->cmd);
1361 free_cmd(wc, cont->cmd);
1362 cont->cmd = NULL;
1363 if (a > -1) {
1364 /* Get high channel in low bits */
1365 rxs = (a & 0x3) << 2;
1366 if (!(wc->span.chans[i+3]->sig & DAHDI_SIG_CLEAR)) {
1367 if (wc->span.chans[i+3]->rxsig != rxs) {
1368 dahdi_rbsbits(wc->span.chans[i+3], rxs);
1369 }
1370 }
1371 rxs = (a & 0xc);
1372 if (!(wc->span.chans[i+2]->sig & DAHDI_SIG_CLEAR)) {
1373 if (wc->span.chans[i+2]->rxsig != rxs) {
1374 dahdi_rbsbits(wc->span.chans[i+2], rxs);
1375 }
1376 }
1377 rxs = (a >> 2) & 0xc;
1378 if (!(wc->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
1379 if (wc->span.chans[i+1]->rxsig != rxs) {
1380 dahdi_rbsbits(wc->span.chans[i+1], rxs);
1381 }
1382 }
1383 rxs = (a >> 4) & 0xc;
1384 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
1385 if (wc->span.chans[i]->rxsig != rxs) {
1386 dahdi_rbsbits(wc->span.chans[i], rxs);
1387 }
1388 }
1389 }
1390 }
1391 } else {
1392 /* First send out the commands. */
1393 for (i = 0; i < 24; i += 2) {
1394 cont = kzalloc(sizeof(*cont), GFP_KERNEL);
1395 if (!cont) {
1396 WARN_ON_ONCE(1);
1397 goto done;
1398 }
1399 cont->cmd = get_free_cmd(wc);
1400 if (!cont->cmd) {
1401 WARN_ON_ONCE(1);
1402 goto done;
1403 }
1404 cont->index = i;
1405 list_add_tail(&cont->node, &commands);
1406 __t1_getreg(wc, 0x70 + (i>>1), cont->cmd);
1407 }
1408
1409 list_for_each_entry_reverse(cont, &commands, node) {
1410 i = cont->index;
1411 a = __t1_getresult(wc, cont->cmd);
1412 free_cmd(wc, cont->cmd);
1413 cont->cmd = NULL;
1414 if (a > -1) {
1415 /* Get high channel in low bits */
1416 rxs = (a & 0xf);
1417 if (!(wc->span.chans[i+1]->sig & DAHDI_SIG_CLEAR)) {
1418 if (wc->span.chans[i+1]->rxsig != rxs) {
1419 dahdi_rbsbits(wc->span.chans[i+1], rxs);
1420 }
1421 }
1422 rxs = (a >> 4) & 0xf;
1423 if (!(wc->span.chans[i]->sig & DAHDI_SIG_CLEAR)) {
1424 if (wc->span.chans[i]->rxsig != rxs) {
1425 dahdi_rbsbits(wc->span.chans[i], rxs);
1426 }
1427 }
1428 }
1429 }
1430 }
1431 done:
1432 while (!list_empty(&commands)) {
1433 cont = container_of(commands.next,
1434 struct command_container, node);
1435 if (unlikely(cont->cmd)) {
1436 /* We do not care about the result, let's just wait for
1437 * the rest of the system to finish with it. */
1438 __t1_getresult(wc, cont->cmd);
1439 free_cmd(wc, cont->cmd);
1440 cont->cmd = NULL;
1441 }
1442 list_del(&cont->node);
1443 kfree(cont);
1444 }
1445 return;
1446 }
1447
1448 struct maint_work_struct {
1449 struct work_struct work;
1450 struct t1 *wc;
1451 int cmd;
1452 struct dahdi_span *span;
1453 };
1454
1455 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1456 static void t1xxp_maint_work(void *data)
1457 {
1458 struct maint_work_struct *w = data;
1459 #else
1460 static void t1xxp_maint_work(struct work_struct *work)
1461 {
1462 struct maint_work_struct *w = container_of(work,
1463 struct maint_work_struct, work);
1464 #endif
1465
1466 struct t1 *wc = w->wc;
1467 struct dahdi_span *span = w->span;
1468 int reg = 0;
1469 int cmd = w->cmd;
1470
1471 if (dahdi_is_e1_span(&wc->span)) {
1472 switch (cmd) {
1473 case DAHDI_MAINT_NONE:
1474 t1_info(wc, "Clearing all maint modes\n");
1475 t1xxp_clear_maint(span);
1476 break;
1477 case DAHDI_MAINT_LOCALLOOP:
1478 t1xxp_clear_maint(span);
1479 reg = t1_getreg(wc, LIM0);
1480 if (reg < 0)
1481 goto cleanup;
1482 t1_setreg(wc, LIM0, reg | LIM0_LL);
1483 break;
1484 case DAHDI_MAINT_REMOTELOOP:
1485 case DAHDI_MAINT_LOOPUP:
1486 case DAHDI_MAINT_LOOPDOWN:
1487 t1_info(wc, "Only local loop supported in E1 mode\n");
1488 goto cleanup;
1489 default:
1490 t1_info(wc, "Unknown E1 maint command: %d\n", cmd);
1491 goto cleanup;
1492 }
1493 } else {
1494 switch (cmd) {
1495 case DAHDI_MAINT_NONE:
1496 t1xxp_clear_maint(span);
1497 break;
1498 case DAHDI_MAINT_LOCALLOOP:
1499 t1xxp_clear_maint(span);
1500 reg = t1_getreg(wc, LIM0);
1501 if (reg < 0)
1502 goto cleanup;
1503 t1_setreg(wc, LIM0, reg | LIM0_LL);
1504 break;
1505 case DAHDI_MAINT_NETWORKLINELOOP:
1506 t1xxp_clear_maint(span);
1507 reg = t1_getreg(wc, LIM1);
1508 if (reg < 0)
1509 goto cleanup;
1510 t1_setreg(wc, LIM1, reg | LIM1_RL);
1511 break;
1512 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
1513 t1xxp_clear_maint(span);
1514 reg = t1_getreg(wc, LIM1);
1515 if (reg < 0)
1516 goto cleanup;
1517 t1_setreg(wc, LIM1, reg | (LIM1_RL | LIM1_JATT));
1518 break;
1519 case DAHDI_MAINT_LOOPUP:
1520 t1xxp_clear_maint(span);
1521 t1_setreg(wc, 0x21, 0x50);
1522 break;
1523 case DAHDI_MAINT_LOOPDOWN:
1524 t1xxp_clear_maint(span);
1525 t1_setreg(wc, 0x21, 0x60);
1526 break;
1527 default:
1528 t1_info(wc, "Unknown T1 maint command: %d\n", cmd);
1529 return;
1530 }
1531 }
1532
1533 cleanup:
1534 kfree(w);
1535 return;
1536 }
1537
1538 static int t1xxp_maint(struct dahdi_span *span, int cmd)
1539 {
1540 struct maint_work_struct *work;
1541 struct t1 *wc = container_of(span, struct t1, span);
1542
1543 if (dahdi_is_e1_span(&wc->span)) {
1544 switch (cmd) {
1545 case DAHDI_MAINT_NONE:
1546 case DAHDI_MAINT_LOCALLOOP:
1547 break;
1548 case DAHDI_MAINT_REMOTELOOP:
1549 case DAHDI_MAINT_LOOPUP:
1550 case DAHDI_MAINT_LOOPDOWN:
1551 t1_info(wc, "Only local loop supported in E1 mode\n");
1552 return -ENOSYS;
1553 default:
1554 t1_info(wc, "Unknown E1 maint command: %d\n", cmd);
1555 return -ENOSYS;
1556 }
1557 } else {
1558 switch (cmd) {
1559 case DAHDI_MAINT_NONE:
1560 case DAHDI_MAINT_LOCALLOOP:
1561 case DAHDI_MAINT_NETWORKLINELOOP:
1562 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
1563 case DAHDI_MAINT_LOOPUP:
1564 case DAHDI_MAINT_LOOPDOWN:
1565 break;
1566 default:
1567 t1_info(wc, "Unknown T1 maint command: %d\n", cmd);
1568 return -ENOSYS;
1569 }
1570 }
1571
1572 work = kmalloc(sizeof(*work), GFP_ATOMIC);
1573 if (!work) {
1574 t1_info(wc, "Failed to allocate memory for workqueue\n");
1575 return -ENOMEM;
1576 }
1577
1578 work->span = span;
1579 work->wc = wc;
1580 work->cmd = cmd;
1581
1582 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1583 INIT_WORK(&work->work, t1xxp_maint_work, work);
1584 #else
1585 INIT_WORK(&work->work, t1xxp_maint_work);
1586 #endif
1587 queue_work(wc->wq, &work->work);
1588 return 0;
1589 }
1590
1591 static int t1xxp_clear_maint(struct dahdi_span *span)
1592 {
1593 struct t1 *wc = container_of(span, struct t1, span);
1594 int reg = 0;
1595
1596 /* Turn off local loop */
1597 reg = t1_getreg(wc, LIM0);
1598 if (reg < 0)
1599 return -EIO;
1600 t1_setreg(wc, LIM0, reg & ~LIM0_LL);
1601
1602 /* Turn off remote loop & jitter attenuator */
1603 reg = t1_getreg(wc, LIM1);
1604 if (reg < 0)
1605 return -EIO;
1606 t1_setreg(wc, LIM1, reg & ~(LIM1_RL | LIM1_JATT));
1607
1608 /* Clear loopup/loopdown signals on the line */
1609 t1_setreg(wc, 0x21, 0x40);
1610 return 0;
1611 }
1612
1613
1614 static int t1xxp_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
1615 {
1616 struct t4_regs regs;
1617 unsigned int x;
1618 struct t1 *wc;
1619
1620 switch (cmd) {
1621 case WCT4_GET_REGS:
1622 wc = chan->pvt;
1623 for (x = 0; x < sizeof(regs.regs) / sizeof(regs.regs[0]); x++)
1624 regs.regs[x] = t1_getreg(wc, x);
1625
1626 if (copy_to_user((void __user *) data, &regs, sizeof(regs)))
1627 return -EFAULT;
1628 break;
1629 default:
1630 return -ENOTTY;
1631 }
1632 return 0;
1633 }
1634
1635 static const char *t1xxp_echocan_name(const struct dahdi_chan *chan)
1636 {
1637 struct t1 *wc = chan->pvt;
1638 if (wc->vpmadt032)
1639 return vpmadt032_name;
1640 else if (wc->vpmoct)
1641 return vpmoct_name;
1642 return NULL;
1643 }
1644
1645 static int t1xxp_echocan_create(struct dahdi_chan *chan,
1646 struct dahdi_echocanparams *ecp,
1647 struct dahdi_echocanparam *p,
1648 struct dahdi_echocan_state **ec)
1649 {
1650 struct t1 *wc = chan->pvt;
1651 enum adt_companding comp;
1652
1653 if (!vpmsupport || !test_bit(VPM150M_ACTIVE, &wc->ctlreg))
1654 return -ENODEV;
1655
1656 if (wc->vpmadt032) {
1657 *ec = wc->ec[chan->chanpos - 1];
1658 (*ec)->ops = &vpm150m_ec_ops;
1659 (*ec)->features = vpm150m_ec_features;
1660
1661 comp = (DAHDI_LAW_ALAW == chan->span->deflaw) ?
1662 ADT_COMP_ALAW : ADT_COMP_ULAW;
1663
1664 return vpmadt032_echocan_create(wc->vpmadt032, chan->chanpos-1,
1665 comp, ecp, p);
1666 } else if (wc->vpmoct) {
1667 /* Hookup legacy callbacks */
1668 *ec = wc->ec[chan->chanpos - 1];
1669 (*ec)->ops = &vpm150m_ec_ops;
1670 (*ec)->features = vpm150m_ec_features;
1671
1672 return vpmoct_echocan_create(wc->vpmoct, chan->chanpos-1,
1673 chan->span->deflaw);
1674 } else {
1675 return -ENODEV;
1676 }
1677 }
1678
1679 static void echocan_free(struct dahdi_chan *chan, struct dahdi_echocan_state *ec)
1680 {
1681 struct t1 *wc = chan->pvt;
1682 if (wc->vpmadt032)
1683 vpmadt032_echocan_free(wc->vpmadt032, chan->chanpos - 1, ec);
1684 else if (wc->vpmoct)
1685 vpmoct_echocan_free(wc->vpmoct, chan->chanpos - 1);
1686 }
1687
1688 static void
1689 setchanconfig_from_state(struct vpmadt032 *vpm, int channel,
1690 GpakChannelConfig_t *chanconfig)
1691 {
1692 GpakEcanParms_t *p;
1693
1694 BUG_ON(!vpm);
1695
1696 chanconfig->PcmInPortA = 3;
1697 chanconfig->PcmInSlotA = (channel + 1) * 2;
1698 chanconfig->PcmOutPortA = SerialPortNull;
1699 chanconfig->PcmOutSlotA = (channel + 1) * 2;
1700 chanconfig->PcmInPortB = 2;
1701 chanconfig->PcmInSlotB = (channel + 1) * 2;
1702 chanconfig->PcmOutPortB = 3;
1703 chanconfig->PcmOutSlotB = (channel + 1) * 2;
1704 chanconfig->ToneTypesA = Null_tone;
1705 chanconfig->MuteToneA = Disabled;
1706 chanconfig->FaxCngDetA = Disabled;
1707 chanconfig->ToneTypesB = Null_tone;
1708 chanconfig->EcanEnableA = Enabled;
1709 chanconfig->EcanEnableB = Disabled;
1710 chanconfig->MuteToneB = Disabled;
1711 chanconfig->FaxCngDetB = Disabled;
1712
1713 chanconfig->SoftwareCompand = cmpNone;
1714
1715 chanconfig->FrameRate = rate10ms;
1716
1717 p = &chanconfig->EcanParametersA;
1718
1719 vpmadt032_get_default_parameters(p);
1720
1721 p->EcanNlpType = vpm->curecstate[channel].nlp_type;
1722 p->EcanNlpThreshold = vpm->curecstate[channel].nlp_threshold;
1723 p->EcanNlpMaxSuppress = vpm->curecstate[channel].nlp_max_suppress;
1724
1725 memcpy(&chanconfig->EcanParametersB,
1726 &chanconfig->EcanParametersA,
1727 sizeof(chanconfig->EcanParametersB));
1728 }
1729
1730 #ifdef VPM_SUPPORT
1731
1732 struct vpm_load_work {
1733 struct work_struct work;
1734 struct t1 *wc;
1735 };
1736
1737 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1738 static void vpm_load_func(void *data)
1739 {
1740 struct maint_work_struct *w = data;
1741 #else
1742 static void vpm_load_func(struct work_struct *work)
1743 {
1744 struct vpm_load_work *w = container_of(work,
1745 struct vpm_load_work, work);
1746 #endif
1747 struct t1 *wc = w->wc;
1748 int res;
1749
1750 res = vpmadt032_init(wc->vpmadt032);
1751 if (res) {
1752 /* There was some problem during initialization, but it passed
1753 * the address test, let's try again in a bit. */
1754 wc->vpm_check = jiffies + HZ/2;
1755 return;
1756 }
1757
1758 if (config_vpmadt032(wc->vpmadt032, wc)) {
1759 clear_bit(VPM150M_ACTIVE, &wc->ctlreg);
1760 wc->vpm_check = jiffies + HZ/2;
1761 return;
1762 }
1763
1764 /* turn on vpm (RX audio from vpm module) */
1765 set_bit(VPM150M_ACTIVE, &wc->ctlreg);
1766 wc->vpm_check = jiffies + HZ*5;
1767 if (vpmtsisupport) {
1768 debug_printk(wc, 1, "enabling VPM TSI pin\n");
1769 /* turn on vpm timeslot interchange pin */
1770 set_bit(0, &wc->ctlreg);
1771 }
1772
1773 wc->not_ready--;
1774 kfree(w);
1775 }
1776
1777 static int vpm_start_load(struct t1 *wc)
1778 {
1779 struct vpm_load_work *work;
1780
1781 work = kzalloc(sizeof(*work), GFP_KERNEL);
1782 if (!work)
1783 return -ENOMEM;
1784 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1785 INIT_WORK(&work->work, vpm_load_func, work);
1786 #else
1787 INIT_WORK(&work->work, vpm_load_func);
1788 #endif
1789 work->wc = wc;
1790
1791 queue_work(wc->wq, &work->work);
1792 wc->not_ready++;
1793 return 0;
1794 }
1795
1796 static void t1_vpm_load_complete(struct device *dev, bool operational)
1797 {
1798 unsigned long flags;
1799 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
1800 struct t1 *wc = pci_get_drvdata(pdev);
1801 struct vpmoct *vpm = NULL;
1802
1803 if (!wc || is_initialized(wc)) {
1804 WARN_ON(!wc);
1805 return;
1806 }
1807
1808 spin_lock_irqsave(&wc->reglock, flags);
1809 wc->not_ready--;
1810 if (operational) {
1811 set_bit(VPM150M_ACTIVE, &wc->ctlreg);
1812 } else {
1813 clear_bit(VPM150M_ACTIVE, &wc->ctlreg);
1814 vpm = wc->vpmoct;
1815 wc->vpmoct = NULL;
1816 }
1817 spin_unlock_irqrestore(&wc->reglock, flags);
1818
1819 if (vpm)
1820 vpmoct_free(vpm);
1821 }
1822
1823 static void check_and_load_vpm(struct t1 *wc)
1824 {
1825 unsigned long flags;
1826 struct vpmadt032_options options;
1827 struct vpmadt032 *vpmadt = NULL;
1828
1829 if (!vpmsupport) {
1830 t1_info(wc, "VPM Support Disabled via module parameter\n");
1831 return;
1832 }
1833
1834 memset(&options, 0, sizeof(options));
1835 options.debug = debug;
1836 options.vpmnlptype = vpmnlptype;
1837 options.vpmnlpthresh = vpmnlpthresh;
1838 options.vpmnlpmaxsupp = vpmnlpmaxsupp;
1839 options.channels = dahdi_is_t1_span(&wc->span) ? 24 : 32;
1840
1841 /* We do not want to check that the VPM is alive until after we're
1842 * done setting it up here, an hour should cover it... */
1843 wc->vpm_check = jiffies + HZ*3600;
1844
1845 /* If there was one already allocated, let's free it. */
1846 if (wc->vpmadt032) {
1847 vpmadt = wc->vpmadt032;
1848 clear_bit(VPM150M_ACTIVE, &vpmadt->control);
1849 flush_workqueue(vpmadt->wq);
1850 spin_lock_irqsave(&wc->reglock, flags);
1851 wc->vpmadt032 = NULL;
1852 spin_unlock_irqrestore(&wc->reglock, flags);
1853 vpmadt032_free(vpmadt);
1854 }
1855
1856 vpmadt = vpmadt032_alloc(&options);
1857 if (!vpmadt)
1858 return;
1859
1860 vpmadt->setchanconfig_from_state = setchanconfig_from_state;
1861
1862 spin_lock_irqsave(&wc->reglock, flags);
1863 wc->vpmadt032 = vpmadt;
1864 spin_unlock_irqrestore(&wc->reglock, flags);
1865
1866 /* Probe for and attempt to load a vpmadt032 module */
1867 if (vpmadt032_test(vpmadt, &wc->vb) || vpm_start_load(wc)) {
1868 /* There does not appear to be a VPMADT032 installed. */
1869 clear_bit(VPM150M_ACTIVE, &wc->ctlreg);
1870 spin_lock_irqsave(&wc->reglock, flags);
1871 wc->vpmadt032 = NULL;
1872 spin_unlock_irqrestore(&wc->reglock, flags);
1873 vpmadt032_free(vpmadt);
1874 }
1875
1876 /* Probe for and attempt to load a vpmoct032 module */
1877 if (NULL == wc->vpmadt032) {
1878 struct vpmoct *vpmoct;
1879
1880 /* Check for vpmoct */
1881 vpmoct = vpmoct_alloc();
1882 if (!vpmoct)
1883 return;
1884
1885 vpmoct->dev = &wc->vb.pdev->dev;
1886
1887 spin_lock_irqsave(&wc->reglock, flags);
1888 wc->vpmoct = vpmoct;
1889 wc->not_ready++;
1890 spin_unlock_irqrestore(&wc->reglock, flags);
1891
1892 vpmoct_init(vpmoct, t1_vpm_load_complete);
1893 }
1894
1895 set_span_devicetype(wc);
1896 }
1897 #else
1898 static inline void check_and_load_vpm(const struct t1 *wc)
1899 {
1900 return;
1901 }
1902 #endif
1903
1904 static void t1_chan_set_sigcap(struct dahdi_span *span, int x)
1905 {
1906 struct t1 *wc = container_of(span, struct t1, span);
1907 struct dahdi_chan *chan = wc->chans[x];
1908 chan->sigcap = DAHDI_SIG_CLEAR;
1909 /* E&M variant supported depends on span type */
1910 if (dahdi_is_e1_span(&wc->span)) {
1911 /* E1 sigcap setup */
1912 if (span->lineconfig & DAHDI_CONFIG_CCS) {
1913 /* CCS setup */
1914 chan->sigcap |= DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
1915 return;
1916 }
1917 /* clear out sig and sigcap for channel 16 on E1 CAS
1918 * lines, otherwise, set it correctly */
1919 if (x == 15) {
1920 /* CAS signaling channel setup */
1921 wc->chans[15]->sigcap = 0;
1922 wc->chans[15]->sig = 0;
1923 return;
1924 }
1925 /* normal CAS setup */
1926 chan->sigcap |= DAHDI_SIG_EM_E1 | DAHDI_SIG_FXSLS |
1927 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_SF |
1928 DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS |
1929 DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
1930 } else {
1931 /* T1 sigcap setup */
1932 chan->sigcap |= DAHDI_SIG_EM | DAHDI_SIG_FXSLS |
1933 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_MTP2 |
1934 DAHDI_SIG_SF | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
1935 DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
1936 }
1937 }
1938
1939 static int
1940 t1xxp_spanconfig(struct file *file, struct dahdi_span *span,
1941 struct dahdi_lineconfig *lc)
1942 {
1943 struct t1 *wc = container_of(span, struct t1, span);
1944 int i;
1945
1946 if (file->f_flags & O_NONBLOCK) {
1947 if (!is_initialized(wc))
1948 return -EAGAIN;
1949 } else {
1950 t1_wait_for_ready(wc);
1951 }
1952
1953 /* Do we want to SYNC on receive or not */
1954 if (lc->sync) {
1955 set_bit(7, &wc->ctlreg);
1956 span->syncsrc = span->spanno;
1957 } else {
1958 clear_bit(7, &wc->ctlreg);
1959 span->syncsrc = 0;
1960 }
1961
1962 /* make sure that sigcaps gets updated if necessary */
1963 for (i = 0; i < wc->span.channels; i++)
1964 t1_chan_set_sigcap(span, i);
1965
1966 /* If already running, apply changes immediately */
1967 if (test_bit(DAHDI_FLAGBIT_RUNNING, &span->flags))
1968 return t1xxp_startup(file, span);
1969
1970 return 0;
1971 }
1972
1973 static int t1xxp_enable_hw_preechocan(struct dahdi_chan *chan)
1974 {
1975 struct t1 *wc = chan->pvt;
1976
1977 if (!wc->vpmoct)
1978 return 0;
1979
1980 return vpmoct_preecho_enable(wc->vpmoct, chan->chanpos - 1);
1981 }
1982
1983 static void t1xxp_disable_hw_preechocan(struct dahdi_chan *chan)
1984 {
1985 struct t1 *wc = chan->pvt;
1986
1987 if (!wc->vpmoct)
1988 return;
1989
1990 vpmoct_preecho_disable(wc->vpmoct, chan->chanpos - 1);
1991 }
1992
1993 /**
1994 * t1_software_init - Initialize the board for the given type.
1995 * @wc: The board to initialize.
1996 * @type: The type of board we are, T1 / E1
1997 *
1998 * This function is called at startup and when the type of the span is changed
1999 * via the dahdi_device before the span is assigned a number.
2000 *
2001 */
2002 static int t1_software_init(struct t1 *wc, enum linemode type)
2003 {
2004 int x;
2005 struct dahdi_chan *chans[32] = {NULL,};
2006 struct dahdi_echocan_state *ec[32] = {NULL,};
2007 unsigned long flags;
2008 int res = 0;
2009
2010 /* We may already be setup properly. */
2011 if (wc->span.channels == ((E1 == type) ? 31 : 24))
2012 return 0;
2013
2014 for (x = 0; x < ((E1 == type) ? 31 : 24); x++) {
2015 chans[x] = kzalloc(sizeof(*chans[x]), GFP_KERNEL);
2016 ec[x] = kzalloc(sizeof(*ec[x]), GFP_KERNEL);
2017 if (!chans[x] || !ec[x])
2018 goto error_exit;
2019 }
2020
2021 set_span_devicetype(wc);
2022
2023 /* Because the interrupt handler is running, we need to atomically
2024 * swap the channel arrays. */
2025 spin_lock_irqsave(&wc->reglock, flags);
2026 _t1_free_channels(wc);
2027 memcpy(wc->chans, chans, sizeof(wc->chans));
2028 memcpy(wc->ec, ec, sizeof(wc->ec));
2029 memset(chans, 0, sizeof(chans));
2030 memset(ec, 0, sizeof(ec));
2031
2032 switch (type) {
2033 case E1:
2034 wc->span.channels = 31;
2035 wc->span.spantype = SPANTYPE_DIGITAL_E1;
2036 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3 |
2037 DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
2038 wc->span.deflaw = DAHDI_LAW_ALAW;
2039 break;
2040 case T1:
2041 wc->span.channels = 24;
2042 wc->span.spantype = SPANTYPE_DIGITAL_T1;
2043 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
2044 DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
2045 wc->span.deflaw = DAHDI_LAW_MULAW;
2046 break;
2047 case J1:
2048 wc->span.channels = 24;
2049 wc->span.spantype = SPANTYPE_DIGITAL_J1;
2050 wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
2051 DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
2052 wc->span.deflaw = DAHDI_LAW_MULAW;
2053 break;
2054 default:
2055 spin_unlock_irqrestore(&wc->reglock, flags);
2056 res = -EINVAL;
2057 goto error_exit;
2058 }
2059
2060 spin_unlock_irqrestore(&wc->reglock, flags);
2061
2062 if (!wc->ddev->location)
2063 return -ENOMEM;
2064
2065 t1_info(wc, "Setting up global serial parameters for %s\n",
2066 dahdi_spantype2str(wc->span.spantype));
2067
2068 t4_serial_setup(wc);
2069 set_bit(DAHDI_FLAGBIT_RBS, &wc->span.flags);
2070 for (x = 0; x < wc->span.channels; x++) {
2071 snprintf(wc->chans[x]->name, sizeof(wc->chans[x]->name),
2072 "%s/%d", wc->span.name, x + 1);
2073 t1_chan_set_sigcap(&wc->span, x);
2074 wc->chans[x]->pvt = wc;
2075 wc->chans[x]->chanpos = x + 1;
2076 }
2077
2078 check_and_load_vpm(wc);
2079 set_span_devicetype(wc);
2080
2081 return 0;
2082
2083 error_exit:
2084 for (x = 0; x < ARRAY_SIZE(chans); ++x) {
2085 kfree(chans[x]);
2086 kfree(ec[x]);
2087 }
2088 return res;
2089 }
2090
2091 /**
2092 * t1xx_set_linemode - Change the type of span before assignment.
2093 * @span: The span to change.
2094 * @linemode: Text string for the line mode.
2095 *
2096 * This function may be called after the dahdi_device is registered but
2097 * before the spans are assigned numbers (and are visible to the rest of
2098 * DAHDI).
2099 *
2100 */
2101 static int t1xxp_set_linemode(struct dahdi_span *span, enum spantypes linemode)
2102 {
2103 int res;
2104 struct t1 *wc = container_of(span, struct t1, span);
2105
2106 /* We may already be set to the requested type. */
2107 if (span->spantype == linemode)
2108 return 0;
2109
2110 res = t1_wait_for_ready(wc);
2111 if (res)
2112 return res;
2113
2114 /* Stop the processing of the channels since we're going to change
2115 * them. */
2116 clear_bit(INITIALIZED, &wc->bit_flags);
2117 synchronize_irq(wc->vb.pdev->irq);
2118 smp_mb__after_atomic();
•2119 timer_delete_sync(&wc->timer);
2120 flush_workqueue(wc->wq);
2121
2122 t1_reset_registers(wc);
2123
2124 switch (linemode) {
2125 case SPANTYPE_DIGITAL_T1:
2126 dev_info(&wc->vb.pdev->dev,
2127 "Changing from %s to T1 line mode.\n",
2128 dahdi_spantype2str(wc->span.spantype));
2129 res = t1_software_init(wc, T1);
2130 break;
2131 case SPANTYPE_DIGITAL_E1:
2132 dev_info(&wc->vb.pdev->dev,
2133 "Changing from %s to E1 line mode.\n",
2134 dahdi_spantype2str(wc->span.spantype));
2135 res = t1_software_init(wc, E1);
2136 break;
2137 case SPANTYPE_DIGITAL_J1:
2138 dev_info(&wc->vb.pdev->dev,
2139 "Changing from %s to E1 line mode.\n",
2140 dahdi_spantype2str(wc->span.spantype));
2141 res = t1_software_init(wc, J1);
2142 break;
2143 default:
2144 dev_err(&wc->vb.pdev->dev,
2145 "Got invalid linemode '%s' from dahdi\n",
2146 dahdi_spantype2str(linemode));
2147 res = -EINVAL;
2148 }
2149
2150 /* Since we probably reallocated the channels we need to make
2151 * sure they are configured before setting INITIALIZED again. */
2152 if (!res) {
2153 dahdi_init_span(span);
2154 set_bit(INITIALIZED, &wc->bit_flags);
2155 mod_timer(&wc->timer, jiffies + HZ/5);
2156 }
2157 return res;
2158 }
2159
2160 #if 0
2161
2162 #ifdef VPM_SUPPORT
2163 static inline unsigned char t1_vpm_in(struct t1 *wc, int unit, const unsigned int addr)
2164 {
2165 return t1_getreg_full(wc, addr, unit);
2166 }
2167
2168 static inline unsigned char t1_vpm_out(struct t1 *wc, int unit, const unsigned int addr, const unsigned char val)
2169 {
2170 return t1_setreg(wc, addr, val, unit);
2171 }
2172
2173 #endif
2174 #endif
2175
2176 static int t1_hardware_post_init(struct t1 *wc, enum linemode *type)
2177 {
2178 int res;
2179 int reg;
2180 int x;
2181
2182 /* T1 or E1 */
2183 if ((-1 != t1e1override) || (-1 != j1mode)) {
2184 *type = (t1e1override) ? E1 : (j1mode) ? J1 : T1;
2185 } else {
2186 if (!strcasecmp(default_linemode, "e1")) {
2187 *type = E1;
2188 } else if (!strcasecmp(default_linemode, "t1")) {
2189 *type = T1;
2190 } else if (!strcasecmp(default_linemode, "j1")) {
2191 *type = J1;
2192 } else {
2193 u8 pins;
2194 res = t1_getpins(wc, &pins);
2195 if (res)
2196 return res;
2197 *type = (pins & 0x01) ? T1 : E1;
2198 }
2199 }
2200 debug_printk(wc, 1, "linemode: %s\n", (*type == T1) ? "T1" :
2201 (J1 == *type) ? "J1" : "E1");
2202
2203 /* what version of the FALC are we using? */
2204 reg = t1_setreg(wc, 0x4a, 0xaa);
2205 reg = t1_getreg(wc, 0x4a);
2206 if (reg < 0) {
2207 t1_info(wc, "Failed to read FALC version (%d)\n", reg);
2208 return -EIO;
2209 }
2210 debug_printk(wc, 1, "FALC version: %08x\n", reg);
2211
2212 /* make sure reads and writes work */
2213 for (x = 0; x < 256; x++) {
2214 t1_setreg(wc, 0x14, x);
2215 reg = t1_getreg(wc, 0x14);
2216 if (reg < 0) {
2217 t1_info(wc, "Failed register read (%d)\n", reg);
2218 return -EIO;
2219 }
2220 if (reg != x) {
2221 t1_info(wc, "Register test failed. "
2222 "Wrote '%x' but read '%x'\n", x, reg);
2223 return -EIO;
2224 }
2225 }
2226
2227 t1_setleds(wc, wc->ledstate);
2228
2229 return 0;
2230 }
2231
2232 static void t1_check_alarms(struct t1 *wc)
2233 {
2234 unsigned char c,d;
2235 int alarms;
2236 int x,j;
2237 unsigned char fmr4; /* must read this always */
2238 struct command *cmds[3];
2239
2240 if (!(test_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags)))
2241 return;
2242
2243 for (x = 0; x < ARRAY_SIZE(cmds); ++x) {
2244 cmds[x] = get_free_cmd(wc);
2245 if (!cmds[x]) {
2246 WARN_ON(1);
2247 for (x = 0; x < ARRAY_SIZE(cmds); ++x)
2248 free_cmd(wc, cmds[x]);
2249 return;
2250 }
2251 }
2252
2253 /* Since this is voicebus, if we issue all the reads initially and then
2254 * check the results we can save ourselves some time. Otherwise, each
2255 * read will take a minimum of 3ms to go through the complete pipeline.
2256 */
2257 __t1_getreg(wc, 0x4c, cmds[0]);
2258 __t1_getreg(wc, 0x20, cmds[1]); /* must read this even if not used */
2259 __t1_getreg(wc, 0x4d, cmds[2]);
2260
2261 d = __t1_getresult(wc, cmds[2]);
2262 fmr4 = __t1_getresult(wc, cmds[1]);
2263 c = __t1_getresult(wc, cmds[0]);
2264
2265 for (x=0; x < ARRAY_SIZE(cmds); ++x)
2266 free_cmd(wc, cmds[x]);
2267
2268 /* Assume no alarms */
2269 alarms = 0;
2270
2271 /* And consider only carrier alarms */
2272 wc->span.alarms &= (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_NOTOPEN);
2273
2274 if (dahdi_is_e1_span(&wc->span)) {
2275 if (c & 0x04) {
2276 /* No multiframe found, force RAI high after 400ms only if
2277 we haven't found a multiframe since last loss
2278 of frame */
2279 if (!wc->flags.nmf) {
2280 t1_setreg(wc, 0x20, 0x9f | 0x20); /* LIM0: Force RAI High */
2281 wc->flags.nmf = 1;
2282 t1_info(wc, "NMF workaround on!\n");
2283 }
2284 t1_setreg(wc, 0x1e, 0xc3); /* Reset to CRC4 mode */
2285 t1_setreg(wc, 0x1c, 0xf2); /* Force Resync */
2286 t1_setreg(wc, 0x1c, 0xf0); /* Force Resync */
2287 } else if (!(c & 0x02)) {
2288 if (wc->flags.nmf) {
2289 t1_setreg(wc, 0x20, 0x9f); /* LIM0: Clear forced RAI */
2290 wc->flags.nmf = 0;
2291 t1_info(wc, "NMF workaround off!\n");
2292 }
2293 }
2294 } else {
2295 /* Detect loopup code if we're not sending one */
2296 if ((!wc->span.mainttimer) && (d & 0x08)) {
2297 /* Loop-up code detected */
2298 if ((++wc->loopupcnt > 80) &&
2299 (wc->span.maintstat != DAHDI_MAINT_REMOTELOOP)) {
2300 t1_notice(wc, "Loopup detected,"\
2301 " enabling remote loop\n");
2302 t1_setreg(wc, 0x36, 0x08); /* LIM0: Disable any local loop */
2303 t1_setreg(wc, 0x37, 0xf6); /* LIM1: Enable remote loop */
2304 wc->span.maintstat = DAHDI_MAINT_REMOTELOOP;
2305 }
2306 } else {
2307 wc->loopupcnt = 0;
2308 }
2309
2310 /* Same for loopdown code */
2311 if ((!wc->span.mainttimer) && (d & 0x10)) {
2312 /* Loop-down code detected */
2313 if ((++wc->loopdowncnt > 80) &&
2314 (wc->span.maintstat == DAHDI_MAINT_REMOTELOOP)) {
2315 t1_notice(wc, "Loopdown detected,"\
2316 " disabling remote loop\n");
2317 t1_setreg(wc, 0x36, 0x08); /* LIM0: Disable any local loop */
2318 t1_setreg(wc, 0x37, 0xf0); /* LIM1: Disable remote loop */
2319 wc->span.maintstat = DAHDI_MAINT_NONE;
2320 }
2321 } else {
2322 wc->loopdowncnt = 0;
2323 }
2324 }
2325
2326 if (wc->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
2327 for (x=0,j=0;x < wc->span.channels;x++)
2328 if ((wc->span.chans[x]->flags & DAHDI_FLAG_OPEN) ||
2329 dahdi_have_netdev(wc->span.chans[x]))
2330 j++;
2331 if (!j)
2332 alarms |= DAHDI_ALARM_NOTOPEN;
2333 }
2334
2335 if (c & 0x20) { /* LOF/LFA */
2336 if (wc->alarmcount >= (alarmdebounce/100))
2337 alarms |= DAHDI_ALARM_RED;
2338 else {
2339 if (unlikely(debug && !wc->alarmcount)) {
2340 /* starting to debounce LOF/LFA */
2341 t1_info(wc, "LOF/LFA detected but "
2342 "debouncing for %d ms\n",
2343 alarmdebounce);
2344 }
2345 wc->alarmcount++;
2346 }
2347 } else
2348 wc->alarmcount = 0;
2349
2350 if (c & 0x80) { /* LOS */
2351 if (wc->losalarmcount >= (losalarmdebounce/100))
2352 alarms |= DAHDI_ALARM_RED;
2353 else {
2354 if (unlikely(debug && !wc->losalarmcount)) {
2355 /* starting to debounce LOS */
2356 t1_info(wc, "LOS detected but debouncing "
2357 "for %d ms\n", losalarmdebounce);
2358 }
2359 wc->losalarmcount++;
2360 }
2361 } else
2362 wc->losalarmcount = 0;
2363
2364 if (c & 0x40) { /* AIS */
2365 if (wc->aisalarmcount >= (aisalarmdebounce/100))
2366 alarms |= DAHDI_ALARM_BLUE;
2367 else {
2368 if (unlikely(debug && !wc->aisalarmcount)) {
2369 /* starting to debounce AIS */
2370 t1_info(wc, "AIS detected but debouncing "
2371 "for %d ms\n", aisalarmdebounce);
2372 }
2373 wc->aisalarmcount++;
2374 }
2375 } else
2376 wc->aisalarmcount = 0;
2377
2378 /* Keep track of recovering */
2379 if ((!alarms) && wc->span.alarms)
2380 wc->alarmtimer = jiffies + 5*HZ;
2381 if (wc->alarmtimer)
2382 alarms |= DAHDI_ALARM_RECOVER;
2383
2384 /* If receiving alarms, go into Yellow alarm state */
2385 if (alarms && !wc->flags.sendingyellow) {
2386 t1_info(wc, "Setting yellow alarm\n");
2387
2388 /* We manually do yellow alarm to handle RECOVER and NOTOPEN, otherwise it's auto anyway */
2389 t1_setreg(wc, 0x20, fmr4 | 0x20);
2390 wc->flags.sendingyellow = 1;
2391 } else if (!alarms && wc->flags.sendingyellow) {
2392 t1_info(wc, "Clearing yellow alarm\n");
2393 /* We manually do yellow alarm to handle RECOVER */
2394 t1_setreg(wc, 0x20, fmr4 & ~0x20);
2395 wc->flags.sendingyellow = 0;
2396 }
2397 /*
2398 if ((c & 0x10))
2399 alarms |= DAHDI_ALARM_YELLOW;
2400 */
2401
2402 if (c & 0x10) { /* receiving yellow (RAI) */
2403 if (wc->yelalarmcount >= (yelalarmdebounce/100))
2404 alarms |= DAHDI_ALARM_YELLOW;
2405 else {
2406 if (unlikely(debug && !wc->yelalarmcount)) {
2407 /* starting to debounce AIS */
2408 t1_info(wc, "yelllow (RAI) detected but "
2409 "debouncing for %d ms\n",
2410 yelalarmdebounce);
2411 }
2412 wc->yelalarmcount++;
2413 }
2414 } else
2415 wc->yelalarmcount = 0;
2416
2417 if (wc->span.mainttimer || wc->span.maintstat)
2418 alarms |= DAHDI_ALARM_LOOPBACK;
2419 wc->span.alarms = alarms;
2420 dahdi_alarm_notify(&wc->span);
2421 }
2422
2423 static void handle_leds(struct t1 *wc)
2424 {
2425 unsigned char led;
2426 unsigned long flags;
2427
2428 led = wc->ledstate;
2429
2430 if ((wc->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE))
2431 || wc->losalarmcount) {
2432 /* When we're in red alarm, blink the led once a second. */
2433 if (time_after(jiffies, wc->blinktimer)) {
2434 led = (led & __LED_GREEN) ? SET_LED_RED(led) : UNSET_LED_REDGREEN(led);
2435 }
2436 } else if (wc->span.alarms & DAHDI_ALARM_YELLOW) {
2437 led = (led & __LED_RED) ? SET_LED_GREEN(led) : SET_LED_RED(led);
2438 } else {
2439 if (wc->span.maintstat != DAHDI_MAINT_NONE)
2440 led = SET_LED_ORANGE(led);
2441 else
2442 led = UNSET_LED_ORANGE(led);
2443
2444 if (test_bit(DAHDI_FLAGBIT_RUNNING, &wc->span.flags))
2445 led = SET_LED_GREEN(led);
2446 else
2447 led = UNSET_LED_REDGREEN(led);
2448 }
2449
2450 if (led != wc->ledstate) {
2451 struct command *cmd;
2452 cmd = get_free_cmd(wc);
2453 if (cmd) {
2454 wc->blinktimer = jiffies + HZ/2;
2455 cmd->flags |= __CMD_LEDS;
2456 cmd->address = ~led & 0x0E;
2457 submit_cmd(wc, cmd);
2458 spin_lock_irqsave(&wc->reglock, flags);
2459 wc->ledstate = led;
2460 spin_unlock_irqrestore(&wc->reglock, flags);
2461 }
2462 }
2463 }
2464
2465
2466 static void t1_do_counters(struct t1 *wc)
2467 {
2468 if (wc->alarmtimer && time_after(jiffies, wc->alarmtimer)) {
2469 wc->span.alarms &= ~(DAHDI_ALARM_RECOVER);
2470 wc->alarmtimer = 0;
2471 dahdi_alarm_notify(&wc->span);
2472 }
2473 }
2474
2475 static void insert_tdm_data(const struct t1 *wc, u8 *sframe)
2476 {
2477 int i;
2478 register u8 *chanchunk;
2479 const int channels = wc->span.channels;
2480
2481 for (i = 0; i < channels; ++i) {
2482 chanchunk = &wc->chans[i]->writechunk[0];
2483 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*0] = chanchunk[0];
2484 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*1] = chanchunk[1];
2485 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*2] = chanchunk[2];
2486 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*3] = chanchunk[3];
2487 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*4] = chanchunk[4];
2488 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*5] = chanchunk[5];
2489 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*6] = chanchunk[6];
2490 sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*7] = chanchunk[7];
2491 }
2492 }
2493
2494 static inline void t1_transmitprep(struct t1 *wc, u8 *sframe)
2495 {
2496 int x;
2497 int y;
2498 u8 *eframe = sframe;
2499
2500 /* Calculate Transmission */
2501 if (likely(test_bit(INITIALIZED, &wc->bit_flags)))
2502 _dahdi_transmit(&wc->span);
2503
2504 #ifdef CONFIG_VOICEBUS_ECREFERENCE
2505 for (chan = 0; chan < wc->span.channels; chan++) {
2506 __dahdi_fifo_put(wc->ec_reference[chan],
2507 wc->chans[chan]->writechunk, DAHDI_CHUNKSIZE);
2508 }
2509 #endif
2510
2511 if (likely(test_bit(INITIALIZED, &wc->bit_flags)))
2512 insert_tdm_data(wc, sframe);
2513
2514 spin_lock(&wc->reglock);
2515 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
2516 /* process the command queue */
2517 for (y = 0; y < 7; y++)
2518 cmd_dequeue(wc, eframe, x, y);
2519
2520 #ifdef VPM_SUPPORT
2521 if (wc->vpmadt032) {
2522 cmd_dequeue_vpmadt032(wc, eframe);
2523 } else if (wc->vpmoct) {
2524 cmd_dequeue_vpmoct(wc, eframe);
2525 }
2526 #endif
2527
2528 if (x < DAHDI_CHUNKSIZE - 1) {
2529 eframe[EFRAME_SIZE] = wc->ctlreg;
2530 eframe[EFRAME_SIZE + 1] = wc->txident++;
2531 }
2532 eframe += (EFRAME_SIZE + EFRAME_GAP);
2533 }
2534 spin_unlock(&wc->reglock);
2535 }
2536
2537 /**
2538 * is_good_frame() - Whether the SFRAME received was one sent.
2539 *
2540 */
2541 static inline bool is_good_frame(const u8 *sframe)
2542 {
2543 const u8 a = sframe[0*(EFRAME_SIZE+EFRAME_GAP) + (EFRAME_SIZE+1)];
2544 const u8 b = sframe[1*(EFRAME_SIZE+EFRAME_GAP) + (EFRAME_SIZE+1)];
2545 return a != b;
2546 }
2547
2548 /**
2549 * extract_tdm_data() - Move TDM data from sframe to channels.
2550 *
2551 */
2552 static void extract_tdm_data(struct t1 *wc, const u8 *const sframe)
2553 {
2554 int i;
2555 register u8 *chanchunk;
2556 const int channels = wc->span.channels;
2557
2558 for (i = 0; i < channels; ++i) {
2559 chanchunk = &wc->chans[i]->readchunk[0];
2560 chanchunk[0] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*0];
2561 chanchunk[1] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*1];
2562 chanchunk[2] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*2];
2563 chanchunk[3] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*3];
2564 chanchunk[4] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*4];
2565 chanchunk[5] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*5];
2566 chanchunk[6] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*6];
2567 chanchunk[7] = sframe[(i+1)*2 + (EFRAME_SIZE + EFRAME_GAP)*7];
2568 }
2569
2570 /* Pre-echo with the vpmoct overwrites the 24th timeslot with the
2571 * specified channel's pre-echo audio stream. This timeslot is unused
2572 * by the te12xp */
2573 if (wc->vpmoct && wc->vpmoct->preecho_enabled) {
2574 chanchunk = &wc->vpmoct->preecho_buf[0];
2575 chanchunk[0] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*0];
2576 chanchunk[1] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*1];
2577 chanchunk[2] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*2];
2578 chanchunk[3] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*3];
2579 chanchunk[4] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*4];
2580 chanchunk[5] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*5];
2581 chanchunk[6] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*6];
2582 chanchunk[7] = sframe[23 + (EFRAME_SIZE + EFRAME_GAP)*7];
2583 }
2584 }
2585
2586 static inline void t1_receiveprep(struct t1 *wc, const u8* sframe)
2587 {
2588 int x;
2589 unsigned char expected;
2590 const u8 *eframe = sframe;
2591
2592 if (!is_good_frame(sframe))
2593 return;
2594
2595 if (likely(test_bit(INITIALIZED, &wc->bit_flags)))
2596 extract_tdm_data(wc, sframe);
2597
2598 spin_lock(&wc->reglock);
2599 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
2600 if (x < DAHDI_CHUNKSIZE - 1) {
2601 expected = wc->rxident+1;
2602 wc->rxident = eframe[EFRAME_SIZE + 1];
2603 wc->statreg = eframe[EFRAME_SIZE + 2];
2604 if (wc->rxident != expected) {
2605 wc->ddev->irqmisses++;
2606 _resend_cmds(wc);
2607 if (unlikely(debug)) {
2608 t1_info(wc, "oops: rxident=%d "
2609 "expected=%d x=%d\n",
2610 wc->rxident, expected, x);
2611 }
2612 }
2613 }
2614 cmd_decipher(wc, eframe);
2615 #ifdef VPM_SUPPORT
2616 if (wc->vpmadt032)
2617 cmd_decipher_vpmadt032(wc, eframe);
2618 else if (wc->vpmoct)
2619 cmd_decipher_vpmoct(wc, eframe);
2620 #endif
2621 eframe += (EFRAME_SIZE + EFRAME_GAP);
2622 }
2623
2624 spin_unlock(&wc->reglock);
2625
2626 /* echo cancel */
2627 if (likely(test_bit(INITIALIZED, &wc->bit_flags))) {
2628 for (x = 0; x < wc->span.channels; x++) {
2629 struct dahdi_chan *const c = wc->chans[x];
2630 #ifdef CONFIG_VOICEBUS_ECREFERENCE
2631 unsigned char buffer[DAHDI_CHUNKSIZE];
2632 __dahdi_fifo_get(wc->ec_reference[x], buffer,
2633 ARRAY_SIZE(buffer));
2634 _dahdi_ec_chunk(c, c->readchunk,
2635 buffer);
2636 #else
2637 if ((wc->vpmoct) &&
2638 (c->chanpos-1 == wc->vpmoct->preecho_timeslot) &&
2639 (wc->vpmoct->preecho_enabled)) {
2640 __dahdi_ec_chunk(c, c->readchunk,
2641 wc->vpmoct->preecho_buf,
2642 c->writechunk);
2643 } else {
2644 _dahdi_ec_chunk(c, c->readchunk,
2645 wc->ec_chunk2[x]);
2646 memcpy(wc->ec_chunk2[x], wc->ec_chunk1[x],
2647 DAHDI_CHUNKSIZE);
2648 memcpy(wc->ec_chunk1[x], c->writechunk,
2649 DAHDI_CHUNKSIZE);
2650 }
2651 }
2652 #endif
2653 _dahdi_receive(&wc->span);
2654 }
2655 }
2656
2657 static void t1_handle_transmit(struct voicebus *vb, struct list_head *buffers)
2658 {
2659 struct t1 *wc = container_of(vb, struct t1, vb);
2660 struct vbb *vbb;
2661
2662 list_for_each_entry(vbb, buffers, entry) {
2663 memset(vbb->data, 0, sizeof(vbb->data));
2664 atomic_inc(&wc->txints);
2665 t1_transmitprep(wc, vbb->data);
2666 handle_leds(wc);
2667 }
2668 }
2669
2670 static void t1_handle_receive(struct voicebus *vb, struct list_head *buffers)
2671 {
2672 struct t1 *wc = container_of(vb, struct t1, vb);
2673 struct vbb *vbb;
2674 list_for_each_entry(vbb, buffers, entry)
2675 t1_receiveprep(wc, vbb->data);
2676 }
2677
2678 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2679 static void timer_work_func(void *param)
2680 {
2681 struct t1 *wc = param;
2682 #else
2683 static void timer_work_func(struct work_struct *work)
2684 {
2685 struct t1 *wc = container_of(work, struct t1, timer_work);
2686 #endif
2687 if (test_bit(INITIALIZED, &wc->bit_flags))
2688 mod_timer(&wc->timer, jiffies + HZ/30);
2689 t1_do_counters(wc);
2690 t1_check_alarms(wc);
2691 t1_check_sigbits(wc);
2692 }
2693
2694 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2695 static void vpm_check_func(void *data)
2696 {
2697 struct t1 *wc = data;
2698 #else
2699 static void vpm_check_func(struct work_struct *work)
2700 {
2701 struct t1 *wc = container_of(work, struct t1, vpm_check_work);
2702 #endif
2703 int res;
2704 u16 version;
2705 const int MAX_CHECKS = 5;
2706
2707 /* If there is a failed VPM module, do not block dahdi_cfg
2708 * indefinitely. */
2709 if (++wc->vpm_check_count > MAX_CHECKS) {
2710 wc->not_ready--;
2711 wc->vpm_check = MAX_JIFFY_OFFSET;
2712 t1_info(wc, "Disabling VPMADT032 Checking.\n");
2713 return;
2714 }
2715
2716 if (!test_bit(INITIALIZED, &wc->bit_flags))
2717 return;
2718
2719 if (test_bit(VPM150M_ACTIVE, &wc->ctlreg)) {
2720 res = gpakPingDsp(wc->vpmadt032->dspid, &version);
2721 if (!res) {
2722 set_bit(VPM150M_ACTIVE, &wc->ctlreg);
2723 wc->vpm_check = jiffies + HZ*5;
2724 wc->vpm_check_count = 0;
2725 return;
2726 }
2727
2728 clear_bit(VPM150M_ACTIVE, &wc->ctlreg);
2729 t1_info(wc, "VPMADT032 is non-responsive. Resetting.\n");
2730 }
2731
2732
2733 if (!test_bit(INITIALIZED, &wc->bit_flags))
2734 return;
2735
2736 res = vpmadt032_reset(wc->vpmadt032);
2737 if (res) {
2738 t1_info(wc, "Failed VPMADT032 reset. VPMADT032 is disabled.\n");
2739 wc->vpm_check = jiffies + HZ*5;
2740 return;
2741 }
2742
2743 if (!test_bit(INITIALIZED, &wc->bit_flags))
2744 return;
2745
2746 res = config_vpmadt032(wc->vpmadt032, wc);
2747 if (res) {
2748 /* We failed the configuration, let's try again. */
2749 t1_info(wc, "Failed to configure the ports. Retrying.\n");
2750
2751 if (!test_bit(INITIALIZED, &wc->bit_flags))
2752 return;
2753 queue_work(wc->vpmadt032->wq, &wc->vpm_check_work);
2754 return;
2755 }
2756
2757 if (!test_bit(INITIALIZED, &wc->bit_flags))
2758 return;
2759
2760 /* Looks like the reset went ok so we can put the VPM module back in
2761 * the TDM path. */
2762 set_bit(VPM150M_ACTIVE, &wc->ctlreg);
2763 t1_info(wc, "VPMADT032 is reenabled.\n");
2764 wc->vpm_check = jiffies + HZ*5;
2765 wc->not_ready--;
2766 return;
2767 }
2768
2769 static void te12xp_timer(TIMER_DATA_TYPE timer)
2770 {
2771 unsigned long flags;
•2772 struct t1 *wc = timer_container_of(wc, timer, timer);
2773
2774 if (unlikely(!test_bit(INITIALIZED, &wc->bit_flags)))
2775 return;
2776
2777 queue_work(wc->wq, &wc->timer_work);
2778
2779 spin_lock_irqsave(&wc->reglock, flags);
2780 if (!wc->vpmadt032)
2781 goto unlock_exit;
2782
2783 if (time_after(wc->vpm_check, jiffies))
2784 goto unlock_exit;
2785
2786 queue_work(wc->vpmadt032->wq, &wc->vpm_check_work);
2787
2788 unlock_exit:
2789 spin_unlock_irqrestore(&wc->reglock, flags);
2790 return;
2791 }
2792
2793 static void t1_handle_error(struct voicebus *vb)
2794 {
2795 unsigned long flags;
2796 struct t1 *wc = container_of(vb, struct t1, vb);
2797
2798 spin_lock_irqsave(&wc->reglock, flags);
2799 if (!wc->vpmadt032)
2800 goto unlock_exit;
2801 clear_bit(VPM150M_ACTIVE, &wc->ctlreg);
2802 queue_work(wc->vpmadt032->wq, &wc->vpm_check_work);
2803
2804 unlock_exit:
2805 spin_unlock_irqrestore(&wc->reglock, flags);
2806 }
2807
2808 static const struct voicebus_operations voicebus_operations = {
2809 .handle_receive = t1_handle_receive,
2810 .handle_transmit = t1_handle_transmit,
2811 .handle_error = t1_handle_error,
2812 };
2813
2814 #ifdef CONFIG_VOICEBUS_SYSFS
2815 static ssize_t voicebus_current_latency_show(struct device *dev,
2816 struct device_attribute *attr,
2817 char *buf)
2818 {
2819 unsigned long flags;
2820 struct t1 *wc = dev_get_drvdata(dev);
2821 unsigned int current_latency;
2822 spin_lock_irqsave(&wc->vb.lock, flags);
2823 current_latency = wc->vb.min_tx_buffer_count;
2824 spin_unlock_irqrestore(&wc->vb.lock, flags);
2825 return sprintf(buf, "%d\n", current_latency);
2826 }
2827
2828 static DEVICE_ATTR(voicebus_current_latency, 0400,
2829 voicebus_current_latency_show, NULL);
2830
2831
2832 static ssize_t vpm_firmware_version_show(struct device *dev,
2833 struct device_attribute *attr,
2834 char *buf)
2835 {
2836 int res;
2837 u16 version = 0;
2838 struct t1 *wc = dev_get_drvdata(dev);
2839
2840 if (wc->vpmadt032) {
2841 res = gpakPingDsp(wc->vpmadt032->dspid, &version);
2842 if (res) {
2843 t1_info(wc, "Failed gpakPingDsp %d\n", res);
2844 version = -1;
2845 }
2846 }
2847
2848 return sprintf(buf, "%x.%02x\n", (version & 0xff00) >> 8, (version & 0xff));
2849 }
2850
2851 static DEVICE_ATTR(vpm_firmware_version, 0400,
2852 vpm_firmware_version_show, NULL);
2853
2854 static void create_sysfs_files(struct t1 *wc)
2855 {
2856 int ret;
2857 ret = device_create_file(&wc->vb.pdev->dev,
2858 &dev_attr_voicebus_current_latency);
2859 if (ret) {
2860 dev_info(&wc->vb.pdev->dev,
2861 "Failed to create device attributes.\n");
2862 }
2863
2864 ret = device_create_file(&wc->vb.pdev->dev,
2865 &dev_attr_vpm_firmware_version);
2866 if (ret) {
2867 dev_info(&wc->vb.pdev->dev,
2868 "Failed to create device attributes.\n");
2869 }
2870 }
2871
2872 static void remove_sysfs_files(struct t1 *wc)
2873 {
2874 device_remove_file(&wc->vb.pdev->dev,
2875 &dev_attr_vpm_firmware_version);
2876
2877 device_remove_file(&wc->vb.pdev->dev,
2878 &dev_attr_voicebus_current_latency);
2879 }
2880
2881 #else
2882
2883 static inline void create_sysfs_files(struct t1 *wc) { return; }
2884 static inline void remove_sysfs_files(struct t1 *wc) { return; }
2885
2886 #endif /* CONFIG_VOICEBUS_SYSFS */
2887
2888 static const struct dahdi_span_ops t1_span_ops = {
2889 .owner = THIS_MODULE,
2890 .spanconfig = t1xxp_spanconfig,
2891 .chanconfig = t1xxp_chanconfig,
2892 .startup = t1xxp_startup,
2893 .rbsbits = t1xxp_rbsbits,
2894 .maint = t1xxp_maint,
2895 .ioctl = t1xxp_ioctl,
2896 .set_spantype = t1xxp_set_linemode,
2897 #ifdef VPM_SUPPORT
2898 .enable_hw_preechocan = t1xxp_enable_hw_preechocan,
2899 .disable_hw_preechocan = t1xxp_disable_hw_preechocan,
2900 .echocan_create = t1xxp_echocan_create,
2901 .echocan_name = t1xxp_echocan_name,
2902 #endif
2903 };
2904
2905 static int __devinit te12xp_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2906 {
2907 struct t1 *wc;
2908 const struct t1_desc *d = (struct t1_desc *) ent->driver_data;
2909 unsigned int x;
2910 int res;
2911 unsigned int index = -1;
2912 enum linemode type;
2913
2914 for (x = 0; x < ARRAY_SIZE(ifaces); x++) {
2915 if (!ifaces[x]) {
2916 index = x;
2917 break;
2918 }
2919 }
2920
2921 if (-1 == index) {
2922 printk(KERN_INFO "%s: Too many interfaces\n",
2923 THIS_MODULE->name);
2924 return -EIO;
2925 }
2926
2927 wc = kzalloc(sizeof(*wc), GFP_KERNEL);
2928 if (!wc)
2929 return -ENOMEM;
2930
2931 /* Set the performance counters to -1 since this card currently does
2932 * not support collecting them. */
2933 memset(&wc->span.count, -1, sizeof(wc->span.count));
2934
2935 ifaces[index] = wc;
2936
2937 sprintf(wc->span.name, "WCT1/%d", index);
2938 snprintf(wc->span.desc, sizeof(wc->span.desc) - 1, "%s Card %d",
2939 d->name, index);
2940 wc->not_ready = 1;
2941 wc->ledstate = -1;
2942 wc->variety = d->name;
2943 wc->txident = 1;
2944 spin_lock_init(&wc->reglock);
2945 INIT_LIST_HEAD(&wc->active_cmds);
2946 INIT_LIST_HEAD(&wc->pending_cmds);
2947 timer_setup(&wc->timer, te12xp_timer, 0);
2948
2949 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2950 INIT_WORK(&wc->timer_work, timer_work_func, wc);
2951 # else
2952 INIT_WORK(&wc->timer_work, timer_work_func);
2953 # endif
2954
2955 # if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
2956 INIT_WORK(&wc->vpm_check_work, vpm_check_func, wc);
2957 # else
2958 INIT_WORK(&wc->vpm_check_work, vpm_check_func);
2959 # endif
2960
2961 wc->ddev = dahdi_create_device();
2962 if (!wc->ddev) {
2963 ifaces[index] = NULL;
2964 kfree(wc);
2965 return -ENOMEM;
2966 }
2967 wc->ddev->manufacturer = "Digium";
2968 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
2969 pdev->bus->number,
2970 PCI_SLOT(pdev->devfn) + 1);
2971 if (!wc->ddev->location) {
2972 ifaces[index] = NULL;
2973 kfree(wc);
2974 return -ENOMEM;
2975 }
2976
2977 #ifdef CONFIG_VOICEBUS_ECREFERENCE
2978 for (x = 0; x < ARRAY_SIZE(wc->ec_reference); ++x) {
2979 /* 256 is used here since it is the largest power of two that
2980 * will contain 8 * VOICBUS_DEFAULT_LATENCY */
2981 wc->ec_reference[x] = dahdi_fifo_alloc(256, GFP_KERNEL);
2982
2983 if (IS_ERR(wc->ec_reference[x])) {
2984 res = PTR_ERR(wc->ec_reference[x]);
2985 wc->ec_reference[x] = NULL;
2986 free_wc(wc);
2987 return res;
2988 }
2989
2990 }
2991 #endif /* CONFIG_VOICEBUS_ECREFERENCE */
2992
2993 #ifdef CONFIG_VOICEBUS_DISABLE_ASPM
2994 if (is_pcie(wc)) {
2995 pci_disable_link_state(pdev->bus->self, PCIE_LINK_STATE_L0S |
2996 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
2997 };
2998 #endif
2999
3000 snprintf(wc->name, sizeof(wc->name)-1, "wcte12xp%d", index);
3001 pci_set_drvdata(pdev, wc);
3002 wc->vb.ops = &voicebus_operations;
3003 wc->vb.pdev = pdev;
3004 wc->vb.debug = &debug;
3005 res = voicebus_init(&wc->vb, wc->name);
3006 if (res) {
3007 free_wc(wc);
3008 ifaces[index] = NULL;
3009 return res;
3010 }
3011
3012 wc->wq = create_singlethread_workqueue(wc->name);
3013 if (!wc->wq) {
3014 kfree(wc);
3015 ifaces[index] = NULL;
3016 return -ENOMEM;
3017 }
3018
3019 voicebus_set_minlatency(&wc->vb, latency);
3020 voicebus_set_maxlatency(&wc->vb, max_latency);
3021 max_latency = wc->vb.max_latency;
3022
3023 create_sysfs_files(wc);
3024
3025 voicebus_lock_latency(&wc->vb);
3026 if (voicebus_start(&wc->vb)) {
3027 voicebus_release(&wc->vb);
3028 free_wc(wc);
3029 ifaces[index] = NULL;
3030 return -EIO;
3031 }
3032
3033 res = t1_hardware_post_init(wc, &type);
3034 if (res) {
3035 voicebus_release(&wc->vb);
3036 free_wc(wc);
3037 ifaces[index] = NULL;
3038 return res;
3039 }
3040
3041 wc->span.chans = wc->chans;
3042
3043 res = t1_software_init(wc, type);
3044 if (res) {
3045 voicebus_release(&wc->vb);
3046 free_wc(wc);
3047 ifaces[index] = NULL;
3048 return res;
3049 }
3050
3051 wc->span.ops = &t1_span_ops;
3052 list_add_tail(&wc->span.device_node, &wc->ddev->spans);
3053 res = dahdi_register_device(wc->ddev, &wc->vb.pdev->dev);
3054 if (res) {
3055 t1_info(wc, "Unable to register with DAHDI\n");
3056 return res;
3057 }
3058
3059 set_bit(INITIALIZED, &wc->bit_flags);
3060 mod_timer(&wc->timer, jiffies + HZ/5);
3061
3062 t1_info(wc, "Found a %s\n", wc->variety);
3063 voicebus_unlock_latency(&wc->vb);
3064
3065 wc->not_ready--;
3066 return 0;
3067 }
3068
3069 static void __devexit te12xp_remove_one(struct pci_dev *pdev)
3070 {
3071 struct t1 *wc = pci_get_drvdata(pdev);
3072 #ifdef VPM_SUPPORT
3073 unsigned long flags;
3074 struct vpmadt032 *vpmadt = wc->vpmadt032;
3075 struct vpmoct *vpmoct = wc->vpmoct;
3076 #endif
3077 if (!wc)
3078 return;
3079
3080 dahdi_unregister_device(wc->ddev);
3081
3082 remove_sysfs_files(wc);
3083
3084 clear_bit(INITIALIZED, &wc->bit_flags);
3085 smp_mb__after_atomic();
3086
•3087 timer_delete_sync(&wc->timer);
3088 flush_workqueue(wc->wq);
3089 #ifdef VPM_SUPPORT
3090 if (vpmadt) {
3091 clear_bit(VPM150M_ACTIVE, &vpmadt->control);
3092 flush_workqueue(vpmadt->wq);
3093 } else if (vpmoct) {
3094 while (t1_wait_for_ready(wc))
3095 schedule();
3096 }
3097 #endif
•3098 timer_delete_sync(&wc->timer);
3099
3100 voicebus_release(&wc->vb);
3101
3102 #ifdef VPM_SUPPORT
3103 if (vpmadt) {
3104 spin_lock_irqsave(&wc->reglock, flags);
3105 wc->vpmadt032 = NULL;
3106 spin_unlock_irqrestore(&wc->reglock, flags);
3107 vpmadt032_free(vpmadt);
3108 } else if (vpmoct) {
3109 spin_lock_irqsave(&wc->reglock, flags);
3110 wc->vpmoct = NULL;
3111 spin_unlock_irqrestore(&wc->reglock, flags);
3112 vpmoct_free(vpmoct);
3113 }
3114 #endif
3115
3116 t1_info(wc, "Freed a Wildcard TE12xP.\n");
3117 free_wc(wc);
3118 }
3119
3120 static DEFINE_PCI_DEVICE_TABLE(te12xp_pci_tbl) = {
3121 { 0xd161, 0x0120, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te120p},
3122 { 0xd161, 0x8000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te121},
3123 { 0xd161, 0x8001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) &te122},
3124 { 0 }
3125 };
3126
3127 static void te12xp_shutdown(struct pci_dev *pdev)
3128 {
3129 struct t1 *wc = pci_get_drvdata(pdev);
3130 voicebus_quiesce(&wc->vb);
3131 }
3132
3133 static int te12xp_suspend(struct pci_dev *pdev, pm_message_t state)
3134 {
3135 return -ENOSYS;
3136 }
3137
3138 MODULE_DEVICE_TABLE(pci, te12xp_pci_tbl);
3139
3140 static struct pci_driver te12xp_driver = {
3141 .name = "wcte12xp",
3142 .probe = te12xp_init_one,
3143 .remove = __devexit_p(te12xp_remove_one),
3144 .shutdown = te12xp_shutdown,
3145 .suspend = te12xp_suspend,
3146 .id_table = te12xp_pci_tbl,
3147 };
3148
3149 static int __init te12xp_init(void)
3150 {
3151 int res;
3152
3153 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
3154 cmd_cache = kmem_cache_create(THIS_MODULE->name, sizeof(struct command), 0,
3155 #if defined(CONFIG_SLUB) && (LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 22))
3156 SLAB_HWCACHE_ALIGN | SLAB_STORE_USER, NULL, NULL);
3157 #else
3158 SLAB_HWCACHE_ALIGN, NULL, NULL);
3159 #endif
3160 #else
3161 cmd_cache = kmem_cache_create(THIS_MODULE->name, sizeof(struct command), 0,
3162 SLAB_HWCACHE_ALIGN, NULL);
3163 #endif
3164 if (!cmd_cache)
3165 return -ENOMEM;
3166
3167 if ((-1 != t1e1override) || (-1 != j1mode)) {
3168 pr_info("'t1e1override' and 'j1mode' are deprecated. "
3169 "Please use 'default_linemode' instead.\n");
3170 /* If someone is setting j1mode, then, t1e1override most likely
3171 * needs to be forced to t1 mode */
3172 if (j1mode > 0)
3173 t1e1override = 0;
3174 } else if (strcasecmp(default_linemode, "auto") &&
3175 strcasecmp(default_linemode, "t1") &&
3176 strcasecmp(default_linemode, "j1") &&
3177 strcasecmp(default_linemode, "e1")) {
3178 pr_err("'%s' is an unknown span type.\n", default_linemode);
3179 default_linemode = "auto";
3180 kmem_cache_destroy(cmd_cache);
3181 return -EINVAL;
3182 }
3183 res = pci_register_driver(&te12xp_driver);
3184 if (res) {
3185 kmem_cache_destroy(cmd_cache);
3186 return -ENODEV;
3187 }
3188
3189 return 0;
3190 }
3191
3192
3193 static void __exit te12xp_cleanup(void)
3194 {
3195 pci_unregister_driver(&te12xp_driver);
3196 kmem_cache_destroy(cmd_cache);
3197 }
3198
3199 module_param(debug, int, S_IRUGO | S_IWUSR);
3200 module_param(t1e1override, int, S_IRUGO | S_IWUSR);
3201 module_param(default_linemode, charp, S_IRUGO);
3202 MODULE_PARM_DESC(default_linemode, "\"auto\"(default), \"e1\", or \"t1\". "
3203 "\"auto\" will use the value from the hardware jumpers.");
3204 module_param(j1mode, int, S_IRUGO | S_IWUSR);
3205 module_param(alarmdebounce, int, S_IRUGO | S_IWUSR);
3206 module_param(losalarmdebounce, int, S_IRUGO | S_IWUSR);
3207 module_param(aisalarmdebounce, int, S_IRUGO | S_IWUSR);
3208 module_param(yelalarmdebounce, int, S_IRUGO | S_IWUSR);
3209 module_param(latency, int, S_IRUGO);
3210 module_param(max_latency, int, S_IRUGO);
3211 #ifdef VPM_SUPPORT
3212 module_param(vpmsupport, int, S_IRUGO);
3213 module_param(vpmtsisupport, int, S_IRUGO);
3214 module_param(vpmnlptype, int, S_IRUGO);
3215 module_param(vpmnlpthresh, int, S_IRUGO);
3216 module_param(vpmnlpmaxsupp, int, S_IRUGO);
3217 #endif
3218
3219 MODULE_DESCRIPTION("Wildcard VoiceBus Digital Card Driver");
3220 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
3221 MODULE_LICENSE("GPL v2");
3222
3223 module_init(te12xp_init);
3224 module_exit(te12xp_cleanup);