Debian Codegraph @

drivers/dahdi/wcte12xp/base.c

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