Debian Codegraph @

drivers/dahdi/wcte43x-base.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Digium, Inc. Wildcard te43x T1/E1 card Driver
3 *
4 * Written by Russ Meyerriecks <[email protected]>
5 * Copyright (C) 2012 - 2013, Digium, Inc.
6 * All rights reserved.
7 *
8 */
9
10 /*
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
21 */
22
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/proc_fs.h>
31 #include <linux/moduleparam.h>
32 #include <linux/interrupt.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <linux/sched.h>
36 #include <linux/crc32.h>
37 #include <linux/slab.h>
38
39 #include <linux/vmalloc.h>
40 #include <linux/string.h>
41 #include <linux/time.h>
42 #include <linux/version.h>
43 #include <linux/firmware.h>
44 #include <oct612x.h>
45
46 #include <dahdi/kernel.h>
47
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
49 #include <linux/sched/signal.h>
50 #endif /* 4.11.0 */
51
52 #include "wct4xxp/wct4xxp.h" /* For certain definitions */
53 #include "wcxb.h"
54 #include "wcxb_spi.h"
55 #include "wcxb_flash.h"
56
57 static const char *TE435_FW_FILENAME = "dahdi-fw-te435.bin";
58 static const char *TE436_FW_FILENAME = "dahdi-fw-te436.bin";
59 static const u32 TE435_VERSION = 0x13001e;
60 static const u32 TE436_VERSION = 0x10017;
61
62 /* #define RPC_RCLK */
63
64 enum linemode {
65 T1 = 1,
66 E1,
67 J1,
68 };
69
70 #define STATUS_LED_GREEN (1 << 9)
71 #define STATUS_LED_RED (1 << 10)
72 #define STATUS_LED_YELLOW (STATUS_LED_RED | STATUS_LED_GREEN)
73 #define LED_MASK 0x7f8
74 #define FALC_CPU_RESET (1 << 11)
75
76 /* Interrupt definitions */
77 #define FALC_INT (1<<3)
78 #define SPI_INT (1<<4)
79 #define DMA_STOPPED (1<<5)
80
81 struct t43x;
82
83 struct t43x_span {
84 struct t43x *owner;
85 struct dahdi_span span;
86 #define NMF_FLAGBIT 1
87 #define SENDINGYELLOW_FLAGBIT 2
88 #define HAVE_OPEN_CHANNELS_FLAGBIT 3
89 unsigned long bit_flags;
90 unsigned char txsigs[16]; /* Copy of tx sig registers */
91 unsigned long lofalarmtimer;
92 unsigned long losalarmtimer;
93 unsigned long aisalarmtimer;
94 unsigned long yelalarmtimer;
95 unsigned long recoverytimer;
96 unsigned long loopuptimer;
97 unsigned long loopdntimer;
98
99 struct dahdi_chan *chans[32]; /* Channels */
100 struct dahdi_echocan_state *ec[32]; /* Echocan state for channels */
101
102 bool debounce;
103 int syncpos;
104 int sync;
105 u32 lineconfig_fingerprint;
106 };
107
108 struct t43x_clksrc_work {
109 struct delayed_work work;
110 spinlock_t lock;
111 enum wcxb_clock_sources clksrc;
112 bool is_timing_master;
113 };
114
115 /* One structure per card */
116 struct t43x {
117 spinlock_t reglock;
118
119 const struct t43x_desc *devtype;
120 unsigned long blinktimer;
121 bool blink;
122 struct dahdi_device *ddev;
123 struct t43x_span *tspans[4];
124 int numspans; /* Number of spans on the card */
125 int spansstarted; /* Number of spans started */
126
127 /* protected by t1.reglock */
128 struct timer_list timer;
129 struct work_struct timer_work;
130 struct workqueue_struct *wq;
131 struct t43x_clksrc_work clksrc_work;
132 unsigned int not_ready; /* 0 when entire card is ready to go */
133 struct vpm450m *vpm;
134 char *vpm_name;
135 struct mutex lock;
136 bool latency_locked;
137 int syncsrc;
138 int num; /* card index */
139 int intr_span; /* span to service next interrupt */
140
141 struct wcxb xb;
142 struct list_head card_node;
143 };
144
145 static void t43x_handle_transmit(struct wcxb *xb, void *vfp);
146 static void t43x_handle_receive(struct wcxb *xb, void *vfp);
147 static void t43x_handle_interrupt(struct wcxb *xb, u32 pending);
148
149 static void t43x_handle_error(struct wcxb *xb)
150 {
151 struct t43x *wc = container_of(xb, struct t43x, xb);
152 wc->ddev->irqmisses++;
153 }
154
155 static struct wcxb_operations xb_ops = {
156 .handle_receive = t43x_handle_receive,
157 .handle_transmit = t43x_handle_transmit,
158 .handle_interrupt = t43x_handle_interrupt,
159 .handle_error = t43x_handle_error,
160 };
161
162 /* Maintenance Mode Registers */
163 #define LIM0 0x36
164 #define LIM0_LL (1<<1)
165 #define LIM1 0x37
166 #define LIM1_RL (1<<1)
167 #define LIM1_JATT (1<<2)
168
169 /* Clear Channel Registers */
170 #define CCB1 0x2f
171 #define CCB2 0x30
172 #define CCB3 0x31
173
174 #define FECL_T 0x50 /* Framing Error Counter Lower Byte */
175 #define FECH_T 0x51 /* Framing Error Counter Higher Byte */
176 #define CVCL_T 0x52 /* Code Violation Counter Lower Byte */
177 #define CVCH_T 0x53 /* Code Violation Counter Higher Byte */
178 #define CEC1L_T 0x54 /* CRC Error Counter 1 Lower Byte */
179 #define CEC1H_T 0x55 /* CRC Error Counter 1 Higher Byte */
180 #define EBCL_T 0x56 /* E-Bit Error Counter Lower Byte */
181 #define EBCH_T 0x57 /* E-Bit Error Counter Higher Byte */
182 #define BECL_T 0x58 /* Bit Error Counter Lower Byte */
183 #define BECH_T 0x59 /* Bit Error Counter Higher Byte */
184 #define COEC_T 0x5A /* COFA Event Counter */
185 #define PRBSSTA_T 0xDA /* PRBS Status Register */
186 #define FRS1_T 0x4D /* Framer Receive Status Reg 1 */
187
188 #define ISR3_SEC (1 << 6) /* Internal one-second interrupt bit mask */
189 #define ISR3_ES (1 << 7) /* Errored Second interrupt bit mask */
190
191 #define IMR0 0x14
192
193 /* pci memory map offsets */
194 #define DMA1 0x00 /* dma addresses */
195 #define DMA2 0x04
196 #define DMA3 0x08
197 #define CNTL 0x0C /* fpga control register */
198 #define INT_MASK 0x10 /* interrupt vectors from oct and framer */
199 #define INT_STAT 0x14
200 #define FRAMER_BASE 0x00000800 /* framer's address space */
201
202 #define NOSYNC_ALARMS (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | \
203 DAHDI_ALARM_LOOPBACK)
204
205 static int debug;
206 static int timingcable;
207 static int force_firmware;
208 static int alarmdebounce = 2500; /* LOF/LFA def to 2.5s AT&T TR54016*/
209 static int losalarmdebounce = 2500; /* LOS def to 2.5s AT&T TR54016*/
210 static int aisalarmdebounce = 2500; /* AIS(blue) def to 2.5s AT&T TR54016*/
211 static int yelalarmdebounce = 500; /* RAI(yellow) def to 0.5s AT&T guide */
212 static char *default_linemode = "t1"; /* 'e1', 't1', or 'j1' */
213 static int latency = WCXB_DEFAULT_LATENCY;
214 static int max_latency = WCXB_DEFAULT_MAXLATENCY;
215
216 struct t43x_firm_header {
217 u8 header[6];
218 __le32 chksum;
219 u8 pad[18];
220 __le32 version;
221 } __packed;
222
223 static int t43x_check_for_interrupts(struct t43x *wc);
224 static void t43x_check_alarms(struct t43x *wc, int span_idx);
225 static void t43x_check_sigbits(struct t43x *wc, int span_idx);
226 static const struct dahdi_span_ops t43x_span_ops;
227 static void __t43x_set_timing_source_auto(struct t43x *wc);
228
229 static int vpmsupport = 1;
230
231 #include "oct6100api/oct6100_api.h"
232
233 #define ECHOCAN_NUM_CHANS 128
234
235 #define FLAGBIT_DTMF 1
236 #define FLAGBIT_MUTE 2
237 #define FLAGBIT_ECHO 3
238 #define FLAGBIT_ALAW 4
239
240 #define OCT_CHIP_ID 0
241 #define OCT_MAX_TDM_STREAMS 4
242 #define OCT_TONEEVENT_BUFFER_SIZE 128
243 #define SOUT_STREAM 1
244 #define RIN_STREAM 0
245 #define SIN_STREAM 2
246
247 static void t43x_vpm_init(struct t43x *wc);
248 static void echocan_free(struct dahdi_chan *chan,
249 struct dahdi_echocan_state *ec);
250 static const struct dahdi_echocan_features vpm_ec_features = {
251 .NLP_automatic = 1,
252 .CED_tx_detect = 1,
253 .CED_rx_detect = 1,
254 };
255 static const struct dahdi_echocan_ops vpm_ec_ops = {
256 .echocan_free = echocan_free,
257 };
258
259 struct vpm450m {
260 tPOCT6100_INSTANCE_API pApiInstance;
261 struct oct612x_context context;
262 UINT32 aulEchoChanHndl[ECHOCAN_NUM_CHANS];
263 int ecmode[ECHOCAN_NUM_CHANS];
264 unsigned long chanflags[ECHOCAN_NUM_CHANS];
265 };
266
267 static int t43x_oct612x_write(struct oct612x_context *context,
268 u32 address, u16 value)
269 {
270 struct t43x *wc = dev_get_drvdata(context->dev);
271 wcxb_set_echocan_reg(&wc->xb, address, value);
272 return 0;
273 }
274
275 static int t43x_oct612x_read(struct oct612x_context *context, u32 address,
276 u16 *value)
277 {
278 struct t43x *wc = dev_get_drvdata(context->dev);
279 *value = wcxb_get_echocan_reg(&wc->xb, address);
280 return 0;
281 }
282
283 static int t43x_oct612x_write_smear(struct oct612x_context *context,
284 u32 address, u16 value, size_t count)
285 {
286 unsigned int i;
287 struct t43x *wc = dev_get_drvdata(context->dev);
288 for (i = 0; i < count; ++i)
289 wcxb_set_echocan_reg(&wc->xb, address + (i << 1), value);
290 return 0;
291 }
292
293 static int t43x_oct612x_write_burst(struct oct612x_context *context,
294 u32 address, const u16 *buffer,
295 size_t count)
296 {
297 unsigned int i;
298 struct t43x *wc = dev_get_drvdata(context->dev);
299 for (i = 0; i < count; ++i)
300 wcxb_set_echocan_reg(&wc->xb, address + (i << 1), buffer[i]);
301 return 0;
302 }
303
304 static int t43x_oct612x_read_burst(struct oct612x_context *context,
305 u32 address, u16 *buffer, size_t count)
306 {
307 unsigned int i;
308 struct t43x *wc = dev_get_drvdata(context->dev);
309 for (i = 0; i < count; ++i)
310 buffer[i] = wcxb_get_echocan_reg(&wc->xb, address + (i << 1));
311 return 0;
312 }
313
314 static const struct oct612x_ops t43x_oct612x_ops = {
315 .write = t43x_oct612x_write,
316 .read = t43x_oct612x_read,
317 .write_smear = t43x_oct612x_write_smear,
318 .write_burst = t43x_oct612x_write_burst,
319 .read_burst = t43x_oct612x_read_burst,
320 };
321
322 static void vpm450m_setecmode(struct vpm450m *vpm450m, int channel, int mode)
323 {
324 tOCT6100_CHANNEL_MODIFY *modify;
325 UINT32 ulResult;
326
327 if (vpm450m->ecmode[channel] == mode)
328 return;
329 modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
330 if (!modify) {
331 pr_notice("Unable to allocate memory for setec!\n");
332 return;
333 }
334 Oct6100ChannelModifyDef(modify);
335 modify->ulEchoOperationMode = mode;
336 modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
337 ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
338 if (ulResult != GENERIC_OK) {
339 pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
340 vpm450m->aulEchoChanHndl[channel], channel, ulResult);
341 } else {
342 #ifdef OCTASIC_DEBUG
343 pr_debug("Echo can on channel %d set to %d\n", channel, mode);
344 #endif
345 vpm450m->ecmode[channel] = mode;
346 }
347 kfree(modify);
348 }
349
350 static void vpm450m_set_alaw_companding(struct vpm450m *vpm450m, int channel,
351 bool alaw)
352 {
353 tOCT6100_CHANNEL_MODIFY *modify;
354 UINT32 ulResult;
355 UINT32 law_to_use = (alaw) ? cOCT6100_PCM_A_LAW :
356 cOCT6100_PCM_U_LAW;
357
358 /* If we're already in this companding mode, no need to do anything. */
359 if (alaw == (test_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]) > 0))
360 return;
361
362 modify = kzalloc(sizeof(tOCT6100_CHANNEL_MODIFY), GFP_ATOMIC);
363 if (!modify) {
364 pr_notice("Unable to allocate memory for setec!\n");
365 return;
366 }
367
368 Oct6100ChannelModifyDef(modify);
369 modify->ulChannelHndl = vpm450m->aulEchoChanHndl[channel];
370 modify->fTdmConfigModified = TRUE;
371 modify->TdmConfig.ulSinPcmLaw = law_to_use;
372 modify->TdmConfig.ulRinPcmLaw = law_to_use;
373 modify->TdmConfig.ulSoutPcmLaw = law_to_use;
374 modify->TdmConfig.ulRoutPcmLaw = law_to_use;
375 ulResult = Oct6100ChannelModify(vpm450m->pApiInstance, modify);
376 if (ulResult != GENERIC_OK) {
377 pr_notice("Failed to apply echo can changes on channel %d %d %08x!\n",
378 vpm450m->aulEchoChanHndl[channel], channel, ulResult);
379 } else {
380 if (debug) {
381 pr_info("Changed companding on channel %d to %s.\n",
382 channel, (alaw) ? "alaw" : "ulaw");
383 }
384 if (alaw)
385 set_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]);
386 else
387 clear_bit(FLAGBIT_ALAW, &vpm450m->chanflags[channel]);
388 }
389 kfree(modify);
390 }
391
392 static void vpm450m_setec(struct vpm450m *vpm450m, int channel, int eclen)
393 {
394 if (eclen) {
395 set_bit(FLAGBIT_ECHO, &vpm450m->chanflags[channel]);
396 vpm450m_setecmode(vpm450m, channel,
397 cOCT6100_ECHO_OP_MODE_NORMAL);
398 } else {
399 unsigned long *flags = &vpm450m->chanflags[channel];
400 clear_bit(FLAGBIT_ECHO, &vpm450m->chanflags[channel]);
401 if (test_bit(FLAGBIT_DTMF, flags) ||
402 test_bit(FLAGBIT_MUTE, flags)) {
403 vpm450m_setecmode(vpm450m, channel,
404 cOCT6100_ECHO_OP_MODE_HT_RESET);
405 } else {
406 vpm450m_setecmode(vpm450m, channel,
407 cOCT6100_ECHO_OP_MODE_POWER_DOWN);
408 }
409 }
410 }
411
412 static UINT32 tdmmode_chan_to_slot_map(int channel)
413 {
414 /* Four phases on the tdm bus, skip three of them per channel */
415 /* Due to a bug in the octasic, we had to move the data onto phase 2 */
416 return 1 + ((channel % 32) * 4) + (channel / 32);
417 }
418
419 static int echocan_initialize_channel(
420 struct vpm450m *vpm, int channel, int mode)
421 {
422 tOCT6100_CHANNEL_OPEN ChannelOpen;
423 UINT32 law_to_use = (mode) ? cOCT6100_PCM_A_LAW :
424 cOCT6100_PCM_U_LAW;
425 UINT32 tdmslot_setting;
426 UINT32 ulResult;
427
428 tdmslot_setting = tdmmode_chan_to_slot_map(channel);
429
430 /* Fill Open channel structure with defaults */
431 Oct6100ChannelOpenDef(&ChannelOpen);
432
433 /* Assign the handle memory.*/
434 ChannelOpen.pulChannelHndl = &vpm->aulEchoChanHndl[channel];
435 ChannelOpen.ulUserChanId = channel;
436 /* Enable Tone disabling for Fax and Modems */
437 ChannelOpen.fEnableToneDisabler = TRUE;
438
439 /* Passthrough TDM data by default, no echocan */
440 ChannelOpen.ulEchoOperationMode = cOCT6100_ECHO_OP_MODE_POWER_DOWN;
441
442 /* Configure the TDM settings.*/
443 /* Input from the framer */
444 ChannelOpen.TdmConfig.ulSinStream = SIN_STREAM;
445 ChannelOpen.TdmConfig.ulSinTimeslot = tdmslot_setting;
446 ChannelOpen.TdmConfig.ulSinPcmLaw = law_to_use;
447
448 /* Input from the Host (pre-framer) */
449 ChannelOpen.TdmConfig.ulRinStream = RIN_STREAM;
450 ChannelOpen.TdmConfig.ulRinTimeslot = tdmslot_setting;
451 ChannelOpen.TdmConfig.ulRinPcmLaw = law_to_use;
452
453 /* Output to the Host */
454 ChannelOpen.TdmConfig.ulSoutStream = SOUT_STREAM;
455 ChannelOpen.TdmConfig.ulSoutTimeslot = tdmslot_setting;
456 ChannelOpen.TdmConfig.ulSoutPcmLaw = law_to_use;
457
458 /* From asterisk after echo-cancellation - goes nowhere */
459 ChannelOpen.TdmConfig.ulRoutStream = cOCT6100_UNASSIGNED;
460 ChannelOpen.TdmConfig.ulRoutTimeslot = cOCT6100_UNASSIGNED;
461 ChannelOpen.TdmConfig.ulRoutPcmLaw = law_to_use;
462
463 /* Set the desired VQE features.*/
464 ChannelOpen.VqeConfig.fEnableNlp = TRUE;
465 ChannelOpen.VqeConfig.fRinDcOffsetRemoval = TRUE;
466 ChannelOpen.VqeConfig.fSinDcOffsetRemoval = TRUE;
467 ChannelOpen.VqeConfig.ulComfortNoiseMode =
468 cOCT6100_COMFORT_NOISE_NORMAL;
469
470 /* Open the channel.*/
471 ulResult = Oct6100ChannelOpen(vpm->pApiInstance, &ChannelOpen);
472
473 return ulResult;
474 }
475
476 static struct vpm450m *init_vpm450m(struct t43x *wc, int *laws, int numspans,
477 const struct firmware *firmware)
478 {
479 tOCT6100_CHIP_OPEN *ChipOpen;
480 tOCT6100_GET_INSTANCE_SIZE InstanceSize;
481 tOCT6100_CHANNEL_OPEN *ChannelOpen;
482 UINT32 ulResult;
483 struct vpm450m *vpm450m;
484 int x, i;
485
486 vpm450m = kzalloc(sizeof(struct vpm450m), GFP_KERNEL);
487 if (!vpm450m) {
488 dev_info(&wc->xb.pdev->dev,
489 "Unable to allocate vpm450m struct\n");
490 return NULL;
491 }
492
493 vpm450m->context.dev = &wc->xb.pdev->dev;
494 vpm450m->context.ops = &t43x_oct612x_ops;
495
496 ChipOpen = kzalloc(sizeof(tOCT6100_CHIP_OPEN), GFP_KERNEL);
497 if (!ChipOpen) {
498 dev_info(&wc->xb.pdev->dev, "Unable to allocate ChipOpen\n");
499 kfree(vpm450m);
500 return NULL;
501 }
502
503 ChannelOpen = kzalloc(sizeof(tOCT6100_CHANNEL_OPEN), GFP_KERNEL);
504 if (!ChannelOpen) {
505 dev_info(&wc->xb.pdev->dev, "Unable to allocate ChannelOpen\n");
506 kfree(vpm450m);
507 kfree(ChipOpen);
508 return NULL;
509 }
510
511 for (x = 0; x < ARRAY_SIZE(vpm450m->ecmode); x++)
512 vpm450m->ecmode[x] = -1;
513
514 dev_info(&wc->xb.pdev->dev, "Echo cancellation for %d channels\n",
515 wc->numspans * 32);
516
517 Oct6100ChipOpenDef(ChipOpen);
518 ChipOpen->pProcessContext = &vpm450m->context;
519
520 /* Change default parameters as needed */
521 /* upclk oscillator is at 33.33 Mhz */
522 ChipOpen->ulUpclkFreq = cOCT6100_UPCLK_FREQ_33_33_MHZ;
523
524 /* mclk will be generated by internal PLL at 133 Mhz */
525 ChipOpen->fEnableMemClkOut = TRUE;
526 ChipOpen->ulMemClkFreq = cOCT6100_MCLK_FREQ_133_MHZ;
527
528 /* User defined Chip ID.*/
529 ChipOpen->ulUserChipId = OCT_CHIP_ID;
530
531 /* Set the maximums that the chip needs to support */
532 ChipOpen->ulMaxChannels = wc->numspans * 32;
533 ChipOpen->ulMaxTdmStreams = OCT_MAX_TDM_STREAMS;
534
535 /* External Memory Settings */
536 /* Use DDR memory.*/
537 ChipOpen->ulMemoryType = cOCT6100_MEM_TYPE_DDR;
538 ChipOpen->ulNumMemoryChips = 1;
539 ChipOpen->ulMemoryChipSize = cOCT6100_MEMORY_CHIP_SIZE_32MB;
540
541 ChipOpen->pbyImageFile = (PUINT8) firmware->data;
542 ChipOpen->ulImageSize = firmware->size;
543
544 /* Set TDM data stream frequency */
545 for (i = 0; i < ChipOpen->ulMaxTdmStreams; i++)
546 ChipOpen->aulTdmStreamFreqs[i] = cOCT6100_TDM_STREAM_FREQ_8MHZ;
547
548 /* Configure TDM sampling */
549 ChipOpen->ulTdmSampling = cOCT6100_TDM_SAMPLE_AT_FALLING_EDGE;
550 /* Disable to save RAM footprint space */
551 ChipOpen->fEnableChannelRecording = false;
552
553 /* In this example we will maintain the API using polling so
554 interrupts must be disabled */
555 ChipOpen->InterruptConfig.ulErrorH100Config =
556 cOCT6100_INTERRUPT_DISABLE;
557 ChipOpen->InterruptConfig.ulErrorMemoryConfig =
558 cOCT6100_INTERRUPT_DISABLE;
559 ChipOpen->InterruptConfig.ulFatalGeneralConfig =
560 cOCT6100_INTERRUPT_DISABLE;
561 ChipOpen->InterruptConfig.ulFatalMemoryConfig =
562 cOCT6100_INTERRUPT_DISABLE;
563
564 ChipOpen->ulSoftToneEventsBufSize = OCT_TONEEVENT_BUFFER_SIZE;
565
566 /* Inserting default values into tOCT6100_GET_INSTANCE_SIZE
567 structure parameters. */
568 Oct6100GetInstanceSizeDef(&InstanceSize);
569
570 /* Reset octasic device */
571 wcxb_reset_echocan(&wc->xb);
572
573 /* Get the size of the OCT6100 instance structure. */
574 ulResult = Oct6100GetInstanceSize(ChipOpen, &InstanceSize);
575 if (ulResult != cOCT6100_ERR_OK) {
576 dev_info(&wc->xb.pdev->dev, "Unable to get instance size: %x\n",
577 ulResult);
578 return NULL;
579 }
580
581 vpm450m->pApiInstance = vmalloc(InstanceSize.ulApiInstanceSize);
582 if (!vpm450m->pApiInstance) {
583 dev_info(&wc->xb.pdev->dev,
584 "Out of memory (can't allocate %d bytes)!\n",
585 InstanceSize.ulApiInstanceSize);
586 return NULL;
587 }
588
589 /* Perform the actual configuration of the chip. */
590 wcxb_enable_echocan_dram(&wc->xb);
591 ulResult = Oct6100ChipOpen(vpm450m->pApiInstance, ChipOpen);
592 if (ulResult != cOCT6100_ERR_OK) {
593 dev_info(&wc->xb.pdev->dev, "Unable to Oct6100ChipOpen: %x\n",
594 ulResult);
595 return NULL;
596 }
597
598 /* OCT6100 is now booted and channels can be opened */
599 /* Open 31 channels/span since we're skipping the first on the VPM */
600 for (x = 0; x < numspans; x++) {
601 for (i = 0; i < 31; i++) {
602 ulResult = echocan_initialize_channel(vpm450m,
603 (x*32)+i,
604 laws[x]);
605 if (0 != ulResult) {
606 dev_info(&wc->xb.pdev->dev,
607 "Unable to echocan_initialize_channel: %d %x\n",
608 (x*32)+i, ulResult);
609 return NULL;
610 } else if (laws[x]) {
611 set_bit(FLAGBIT_ALAW,
612 &vpm450m->chanflags[(x*32)+i]);
613 }
614 }
615 }
616
617 if (vpmsupport != 0)
618 wcxb_enable_echocan(&wc->xb);
619
620 kfree(ChipOpen);
621 kfree(ChannelOpen);
622 return vpm450m;
623 }
624
625 static void release_vpm450m(struct vpm450m *vpm450m)
626 {
627 UINT32 ulResult;
628 tOCT6100_CHIP_CLOSE ChipClose;
629
630 Oct6100ChipCloseDef(&ChipClose);
631 ulResult = Oct6100ChipClose(vpm450m->pApiInstance, &ChipClose);
632 if (ulResult != cOCT6100_ERR_OK)
633 pr_notice("Failed to close chip, code %08x!\n", ulResult);
634 vfree(vpm450m->pApiInstance);
635 kfree(vpm450m);
636 }
637
638 static const char *__t43x_echocan_name(struct t43x *wc)
639 {
640 if (wc->vpm)
641 return wc->vpm_name;
642 else
643 return NULL;
644 }
645
646 static const char *t43x_echocan_name(const struct dahdi_chan *chan)
647 {
648 struct t43x *wc = chan->pvt;
649 return __t43x_echocan_name(wc);
650 }
651
652 static int t43x_echocan_create(struct dahdi_chan *chan,
653 struct dahdi_echocanparams *ecp,
654 struct dahdi_echocanparam *p,
655 struct dahdi_echocan_state **ec)
656 {
657 struct t43x *wc = chan->pvt;
658 struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
659 int channel = chan->chanpos - 1;
660 const bool alaw = (chan->span->deflaw == 2);
661
662 if (!vpmsupport || !wc->vpm)
663 return -ENODEV;
664
665 if (ecp->param_count > 0) {
666 dev_warn(&wc->xb.pdev->dev,
667 "%s echo canceller does not support parameters; failing request\n",
668 chan->ec_factory->get_name(chan));
669 return -EINVAL;
670 }
671
672 *ec = ts->ec[channel];
673 (*ec)->ops = &vpm_ec_ops;
674 (*ec)->features = vpm_ec_features;
675
676 channel += (32*chan->span->offset);
677 vpm450m_set_alaw_companding(wc->vpm, channel, alaw);
678 vpm450m_setec(wc->vpm, channel, ecp->tap_length);
679 return 0;
680 }
681
682 static void echocan_free(struct dahdi_chan *chan,
683 struct dahdi_echocan_state *ec)
684 {
685 struct t43x *wc = chan->pvt;
686 int channel = chan->chanpos - 1;
687 if (!wc->vpm)
688 return;
689 memset(ec, 0, sizeof(*ec));
690 channel += (32*chan->span->offset);
691 vpm450m_setec(wc->vpm, channel, 0);
692 }
693
694 static void t43x_vpm_init(struct t43x *wc)
695 {
696 int laws[8] = {0, };
697 int x;
698
699 struct firmware embedded_firmware;
700 const struct firmware *firmware = &embedded_firmware;
701 #if !defined(HOTPLUG_FIRMWARE)
702 extern void _binary_dahdi_fw_oct6114_064_bin_size;
703 extern u8 _binary_dahdi_fw_oct6114_064_bin_start[];
704 extern void _binary_dahdi_fw_oct6114_128_bin_size;
705 extern u8 _binary_dahdi_fw_oct6114_128_bin_start[];
706 #else
707 static const char oct064_firmware[] = "dahdi-fw-oct6114-064.bin";
708 static const char oct128_firmware[] = "dahdi-fw-oct6114-128.bin";
709 const char *oct_firmware;
710 #endif
711
712 if (!vpmsupport) {
713 dev_info(&wc->xb.pdev->dev, "VPM450: Support Disabled\n");
714 return;
715 }
716
717 #if defined(HOTPLUG_FIRMWARE)
718 if (wc->numspans == 2) {
719 wc->vpm_name = "VPMOCT064";
720 oct_firmware = oct064_firmware;
721 } else {
722 wc->vpm_name = "VPMOCT128";
723 oct_firmware = oct128_firmware;
724 }
725 if ((request_firmware(&firmware, oct_firmware, &wc->xb.pdev->dev) != 0)
726 || !firmware) {
727 dev_notice(&wc->xb.pdev->dev,
728 "VPM450: firmware %s not available from userspace\n",
729 oct_firmware);
730 return;
731 }
732 #else
733 /* Yes... this is weird. objcopy gives us a symbol containing
734 the size of the firmware, not a pointer a variable containing
735 the size. The only way we can get the value of the symbol
736 is to take its address, so we define it as a pointer and
737 then cast that value to the proper type.
738 */
739 if (wc->numspans == 2) {
740 embedded_firmware.data = _binary_dahdi_fw_oct6114_064_bin_start;
741 embedded_firmware.size =
742 (size_t)&_binary_dahdi_fw_oct6114_064_bin_size;
743 } else {
744 embedded_firmware.size =
745 (size_t)&_binary_dahdi_fw_oct6114_128_bin_size;
746 embedded_firmware.data = _binary_dahdi_fw_oct6114_128_bin_start;
747 }
748 #endif
749
750 /* Setup alaw vs ulaw rules */
751 for (x = 0; x < wc->numspans; x++) {
752 if (wc->tspans[x]->span.channels > 24)
753 laws[x] = 1;
754 }
755
756 wc->vpm = init_vpm450m(wc, laws, wc->numspans, firmware);
757
758 if (!wc->vpm) {
759 dev_notice(&wc->xb.pdev->dev, "VPM450: Failed to initialize\n");
760 if (firmware != &embedded_firmware)
761 release_firmware(firmware);
762 return;
763 }
764
765 if (firmware != &embedded_firmware)
766 release_firmware(firmware);
767
768 dev_info(&wc->xb.pdev->dev,
769 "VPM450: Present and operational servicing %d span(s)\n",
770 wc->numspans);
771
772 }
773
774 static int t43x_clear_maint(struct dahdi_span *span);
775
776 static DEFINE_MUTEX(card_list_lock);
777 static LIST_HEAD(card_list);
778
779 struct t43x_desc {
780 const char *name;
781 };
782
783 static const struct t43x_desc te435 = {"Wildcard TE435"}; /* pci express quad */
784 static const struct t43x_desc te235 = {"Wildcard TE235"}; /* pci express dual */
785 static const struct t43x_desc te436 = {"Wildcard TE436"}; /* pci quad */
786 static const struct t43x_desc te236 = {"Wildcard TE236"}; /* pci dual */
787
788 static int __t43x_pci_get(struct t43x *wc, unsigned int addr)
789 {
790 unsigned int res = ioread8(wc->xb.membase + addr);
791 return res;
792 }
793
794 static inline int __t43x_pci_set(struct t43x *wc, unsigned int addr, int val)
795 {
796 iowrite8(val, wc->xb.membase + addr);
797 __t43x_pci_get(wc, 0);
798 return 0;
799 }
800
801 static inline int t43x_pci_get(struct t43x *wc, int addr)
802 {
803 unsigned int ret;
804 unsigned long flags;
805
806 spin_lock_irqsave(&wc->reglock, flags);
807 ret = __t43x_pci_get(wc, addr);
808 spin_unlock_irqrestore(&wc->reglock, flags);
809 return ret;
810 }
811
812 static inline int t43x_pci_set(struct t43x *wc, int addr, int val)
813 {
814 unsigned long flags;
815 unsigned int ret;
816 spin_lock_irqsave(&wc->reglock, flags);
817 ret = __t43x_pci_set(wc, addr, val);
818 spin_unlock_irqrestore(&wc->reglock, flags);
819 return ret;
820 }
821
822 static inline int
823 __t43x_framer_set(struct t43x *wc, int unit, int addr, int val)
824 {
825 return __t43x_pci_set(wc, FRAMER_BASE + (unit << 8) + addr, val);
826 }
827
828 static inline int t43x_framer_set(struct t43x *wc, int unit, int addr, int val)
829 {
830 return t43x_pci_set(wc, FRAMER_BASE + (unit << 8) + addr, val);
831 }
832
833 static inline int __t43x_framer_get(struct t43x *wc, int unit, int addr)
834 {
835 return __t43x_pci_get(wc, FRAMER_BASE + (unit << 8) + addr);
836 }
837
838 static inline int t43x_framer_get(struct t43x *wc, int unit, int addr)
839 {
840 return t43x_pci_get(wc, FRAMER_BASE + (unit << 8) + addr);
841 }
842
843 static void t43x_framer_reset(struct t43x *wc)
844 {
845 /*
846 * When the framer is reset, RCLK will stop. The FPGA must be switched
847 * to it's internal clock when this happens, but it's only safe to
848 * switch the clock source on the FPGA when the DMA engine is stopped.
849 *
850 */
851 wcxb_stop_dma(&wc->xb);
852 wcxb_wait_for_stop(&wc->xb, 50);
853 wcxb_set_clksrc(&wc->xb, WCXB_CLOCK_SELF);
854 wcxb_gpio_clear(&wc->xb, FALC_CPU_RESET);
855 msleep_interruptible(100);
856 wcxb_gpio_set(&wc->xb, FALC_CPU_RESET);
857 }
858
859 static void t43x_setleds(struct t43x *wc, u32 leds)
860 {
861 wcxb_gpio_set(&wc->xb, leds & LED_MASK);
862 wcxb_gpio_clear(&wc->xb, ~leds & LED_MASK);
863 }
864
865 static void t43x_set_cas_mode(struct t43x *wc, int span_idx)
866 {
867 struct t43x_span *ts = wc->tspans[span_idx];
868 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
869 int i, offset;
870 int reg;
871 unsigned long flags;
872 bool span_has_cas_channel = false;
873
874 if (debug)
875 dev_info(&wc->xb.pdev->dev, "%s span: %d\n", __func__,
876 span_idx);
877
878 if (dahdi_is_e1_span(&ts->span)) {
879 span_has_cas_channel = !(ts->span.lineconfig&DAHDI_CONFIG_CCS);
880 } else {
881 unsigned char ccb[3] = {0, 0, 0};
882 /* Sort out channels that use CAS signalling */
883 for (i = 0; i < ts->span.channels; i++) {
884 offset = i/8;
885 if (offset >= ARRAY_SIZE(ccb)) {
886 WARN_ON(1);
887 break;
888 }
889 if (ts->span.chans[i]->flags & DAHDI_FLAG_CLEAR)
890 ccb[offset] |= 1 << (7 - (i % 8));
891 else
892 ccb[offset] &= ~(1 << (7 - (i % 8)));
893 }
894
895 spin_lock_irqsave(&wc->reglock, flags);
896 __t43x_framer_set(wc, fidx, CCB1, ccb[0]);
897 __t43x_framer_set(wc, fidx, CCB2, ccb[1]);
898 __t43x_framer_set(wc, fidx, CCB3, ccb[2]);
899 spin_unlock_irqrestore(&wc->reglock, flags);
900
901 if ((~ccb[0]) | (~ccb[1]) | (~ccb[2]))
902 span_has_cas_channel = true;
903 }
904
905 /* Unmask CAS RX interrupt if any single channel is in CAS mode */
906 /* This interrupt is called RSC in T1 and CASC in E1 */
907 spin_lock_irqsave(&wc->reglock, flags);
908 reg = __t43x_framer_get(wc, fidx, IMR0);
909 if (span_has_cas_channel)
910 __t43x_framer_set(wc, fidx, IMR0, reg & ~0x08);
911 else
912 __t43x_framer_set(wc, fidx, IMR0, reg | 0x08);
913 spin_unlock_irqrestore(&wc->reglock, flags);
914 }
915
916 /**
917 * _t43x_free_channels - Free the memory allocated for the channels.
918 *
919 * Must be called with wc->reglock held.
920 *
921 */
922 static void _t43x_free_channels(struct t43x *wc)
923 {
924 unsigned int x, y;
925
926 for (x = 0; x < ARRAY_SIZE(wc->tspans); x++) {
927 if (!wc->tspans[x])
928 continue;
929 for (y = 0; y < ARRAY_SIZE(wc->tspans[x]->chans); y++) {
930 kfree(wc->tspans[x]->chans[y]);
931 kfree(wc->tspans[x]->ec[y]);
932 }
933 kfree(wc->tspans[x]);
934 }
935 }
936
937 static void free_wc(struct t43x *wc)
938 {
939 unsigned long flags;
940 LIST_HEAD(list);
941
942 mutex_lock(&card_list_lock);
943 list_del(&wc->card_node);
944 mutex_unlock(&card_list_lock);
945
946 spin_lock_irqsave(&wc->reglock, flags);
947 _t43x_free_channels(wc);
948 spin_unlock_irqrestore(&wc->reglock, flags);
949
950 if (wc->wq)
951 destroy_workqueue(wc->wq);
952 kfree(wc->ddev->location);
953 kfree(wc->ddev->devicetype);
954 kfree(wc->ddev->hardware_id);
955 if (wc->ddev)
956 dahdi_free_device(wc->ddev);
957 kfree(wc);
958 }
959
960 static void t43x_serial_setup(struct t43x *wc)
961 {
962 unsigned long flags;
963 int slot, fidx;
964
965 dev_info(&wc->xb.pdev->dev,
966 "Setting up global serial parameters for card %d\n", wc->num);
967
968 t43x_framer_reset(wc);
969
970 spin_lock_irqsave(&wc->reglock, flags);
971 /* GPC1: Multiplex mode enabled, FSC is output, active low, RCLK from
972 * channel 0 */
973 __t43x_framer_set(wc, 0, 0x85, 0xe0);
974 /* GPC3: Enable Multi Purpose Switches */
975 __t43x_framer_set(wc, 0, 0xd3, 0xa1);
976 /* GPC4: Enable Multi Purpose Switches */
977 __t43x_framer_set(wc, 0, 0xd4, 0x83);
978 /* GPC5: Enable Multi Purpose Switches */
979 __t43x_framer_set(wc, 0, 0xd5, 0xe5);
980 /* GPC4: Enable Multi Purpose Switches */
981 __t43x_framer_set(wc, 0, 0xd6, 0x87);
982 /* IPC: Interrupt push/pull active low, 8KHz ref clk */
983 __t43x_framer_set(wc, 0, 0x08, 0x05);
984
985 /* Global clocks (8.192 Mhz CLK) */
986 __t43x_framer_set(wc, 0, 0x92, 0x00);
987 __t43x_framer_set(wc, 0, 0x93, 0x18);
988 __t43x_framer_set(wc, 0, 0x94, 0xfb);
989 __t43x_framer_set(wc, 0, 0x95, 0x0b);
990 __t43x_framer_set(wc, 0, 0x96, 0x00);
991 __t43x_framer_set(wc, 0, 0x97, 0x0b);
992 __t43x_framer_set(wc, 0, 0x98, 0xdb);
993 __t43x_framer_set(wc, 0, 0x99, 0xdf);
994
995 spin_unlock_irqrestore(&wc->reglock, flags);
996
997 for (fidx = 0; fidx < 4; fidx++) {
998 /* For dual-span, put the second and third framers
999 * onto the first and second timeslots */
1000 if (wc->numspans == 2)
1001 slot = (fidx - 1) & 3;
1002 else
1003 slot = fidx;
1004 spin_lock_irqsave(&wc->reglock, flags);
1005
1006 /* Configure interrupts */
1007
1008 /* GCR: Interrupt on Activation/Deactivation of AIX, LOS */
1009 __t43x_framer_set(wc, fidx, 0x46, 0xc0);
1010
1011 /* Configure system interface */
1012
1013 /* SIC1: 8.192 Mhz clock/bus, double buffer receive /
1014 * transmit, byte interleaved */
1015 __t43x_framer_set(wc, fidx, 0x3e, 0xc2);
1016 /* SIC2: No FFS, no CRB, SSC2 = 0, phase = unit */
1017 __t43x_framer_set(wc, fidx, 0x3f, (slot << 1));
1018
1019 /* SIC3: Edges for capture - original rx rising edge */
1020 __t43x_framer_set(wc, fidx, 0x40, 0x04);
1021 /* SIC4: CES high, SYPR opposite edge */
1022 __t43x_framer_set(wc, fidx, 0x2a, 0x06);
1023
1024 #ifndef RPC_RCLK
1025 /* CMR1: RCLK is at 8.192 Mhz dejittered */
1026 __t43x_framer_set(wc, fidx, 0x44, 0x30);
1027 #else
1028 /* CMR1: RCLK is at 8.192 Mhz dejittered, ref clock is this
1029 * channel */
1030 __t43x_framer_set(wc, fidx, 0x44, 0x38 | (fidx << 6));
1031 #endif
1032
1033 /* CMR2: We provide sync and clock for tx and rx. */
1034 __t43x_framer_set(wc, fidx, 0x45, 0x00);
1035
1036 /* XC0: Normal operation of Sa-bits */
1037 __t43x_framer_set(wc, fidx, 0x22, 0x07);
1038 __t43x_framer_set(wc, fidx, 0x23, 0xfa); /* XC1 */
1039
1040 __t43x_framer_set(wc, fidx, 0x24, 0x07); /* RC0 */
1041 __t43x_framer_set(wc, fidx, 0x25, 0xfa); /* RC1 */
1042
1043 /* Configure ports */
1044 /* PC1: SPYR/SPYX input on RPA/XPA */
1045 __t43x_framer_set(wc, fidx, 0x80, 0x00);
1046 /* PC2: Unused stuff */
1047 __t43x_framer_set(wc, fidx, 0x81, 0xBB);
1048 #ifndef RPC_RCLK
1049 /* PC3: Unused stuff */
1050 __t43x_framer_set(wc, fidx, 0x82, 0xBB);
1051 #else
1052 /* PC3: RPC is RCLK, XPC is low output */
1053 __t43x_framer_set(wc, fidx, 0x82, 0xFB);
1054 #endif
1055 /* PC4: Unused stuff */
1056 __t43x_framer_set(wc, fidx, 0x83, 0xBB);
1057 /* PC5: XMFS active low, SCLKR is input, RCLK is output */
1058 __t43x_framer_set(wc, fidx, 0x84, 0x01);
1059 __t43x_framer_set(wc, fidx, 0x3b, 0x00); /* Clear LCR1 */
1060
1061 /* Make sure unused ports are set to T1 configuration as a
1062 * default. This is required in order to make clock recovery
1063 * works on spans that are actually configured. */
1064
1065 __t43x_framer_set(wc, fidx, 0x1c, 0xf0);
1066 __t43x_framer_set(wc, fidx, 0x1d, 0x9c);
1067 __t43x_framer_set(wc, fidx, 0x1e, 0x20);
1068 __t43x_framer_set(wc, fidx, 0x20, 0x0c);
1069 /* FMR5: Enable RBS mode */
1070 __t43x_framer_set(wc, fidx, 0x21, 0x40);
1071
1072 __t43x_framer_set(wc, fidx, 0x36, 0x08);
1073 __t43x_framer_set(wc, fidx, 0x37, 0xf0);
1074 __t43x_framer_set(wc, fidx, 0x3a, 0x21);
1075
1076 __t43x_framer_set(wc, fidx, 0x02, 0x50);
1077 __t43x_framer_set(wc, fidx, 0x02, 0x00);
1078
1079 __t43x_framer_set(wc, fidx, 0x38, 0x0a);
1080 __t43x_framer_set(wc, fidx, 0x39, 0x15);
1081 /* Tri-state the TX output for the port */
1082 __t43x_framer_set(wc, fidx, 0x28, 0x40);
1083
1084 spin_unlock_irqrestore(&wc->reglock, flags);
1085 }
1086 }
1087
1088 /**
1089 * t43x_span_assigned - Called when the span is assigned by DAHDI.
1090 * @span: Span that has been assigned.
1091 *
1092 * When this function is called, the span has a valid spanno and all the
1093 * channels on the span have valid channel numbers assigned.
1094 *
1095 * This function is necessary because a device may be registered, and
1096 * then user space may then later decide to assign span numbers and the
1097 * channel numbers.
1098 *
1099 */
1100 static void t43x_span_assigned(struct dahdi_span *span)
1101 {
1102 struct t43x_span *tspan = container_of(span, struct t43x_span, span);
1103 struct t43x *wc = tspan->owner;
1104 struct dahdi_span *pos;
1105 unsigned int unassigned_spans = 0;
1106
1107 if (debug)
1108 dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
1109
1110 span->alarms = DAHDI_ALARM_NONE;
1111
1112 /* We use this to make sure all the spans are assigned before
1113 * running the serial setup. */
1114 list_for_each_entry(pos, &wc->ddev->spans, device_node) {
1115 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &pos->flags))
1116 ++unassigned_spans;
1117 }
1118
1119 if (0 == unassigned_spans) {
1120 /* Now all the spans are assigned so we can go ahead and start
1121 * things up. */
1122 t43x_serial_setup(wc);
1123 }
1124 }
1125
1126 static int syncsrc;
1127 static int syncnum;
1128 static int syncspan;
1129 static DEFINE_SPINLOCK(synclock);
1130
1131 static void __t43x_set_rclk_src(struct t43x *wc, int span)
1132 {
1133 #ifndef RPC_RCLK
1134 int cmr1 = 0x38; /* Clock Mode: RCLK sourced by DCO-R1
1135 by default, Disable Clock-Switching */
1136 int fidx;
1137
1138 if (2 == wc->numspans) {
1139 u8 reg;
1140
1141 /* Since the clock always comes from the first span (which is
1142 * not connected to a physical port on the dual span) we can
1143 * always set the framer mode register to match whatever span
1144 * we're currently sourcing the timing from. This ensures that
1145 * the DCO-R is always expecting the right clock speed from the
1146 * line. */
1147
1148 fidx = span+1;
1149
1150 reg = __t43x_framer_get(wc, fidx, 0x1d);
1151 __t43x_framer_set(wc, 0, 0x1d, reg);
1152
1153 reg = __t43x_framer_get(wc, fidx, 0x1e);
1154 __t43x_framer_set(wc, 0, 0x1e, reg);
1155
1156 } else {
1157 fidx = span;
1158 }
1159
1160 cmr1 |= (fidx) << 6;
1161 __t43x_framer_set(wc, 0, 0x44, cmr1);
1162 #else
1163 int fidx = (2 == wc->numspans) ? span+1 : span;
1164 int gpc1 = __t43x_framer_get(wc, 0, 0x85);
1165
1166 gpc1 &= ~3;
1167 gpc1 |= fidx & 3;
1168
1169 t43x_framer_set(wc, 0, 0x85, gpc1);
1170 #endif
1171 dev_info(&wc->xb.pdev->dev, "RCLK source set to span %d\n", span+1);
1172 }
1173
1174 /* This is called from the workqueue to wait for the TDM engine stop */
1175
1176 static void t43x_clksrc_work_fn(struct work_struct *data)
1177 {
1178 struct t43x_clksrc_work *work = container_of(data,
1179 struct t43x_clksrc_work, work.work);
1180 struct t43x *wc = container_of(work, struct t43x, clksrc_work);
1181
1182 if (debug) {
1183 dev_info(&wc->xb.pdev->dev,
1184 "t43x_clksrc_work() called from queue\n");
1185 }
1186
1187 if (wcxb_is_stopped(&wc->xb)) {
1188
1189 /* Set new clock select */
1190 if (work->is_timing_master)
1191 wcxb_enable_timing_header_driver(&wc->xb);
1192 else
1193 wcxb_disable_timing_header_driver(&wc->xb);
1194 wcxb_set_clksrc(&wc->xb, work->clksrc);
1195
1196 /* Restart DMA processing */
1197 wcxb_start(&wc->xb);
1198 } else {
1199 /* Stop DMA again in case DMA underrun int restarted it */
1200 wcxb_stop_dma(&wc->xb);
1201 queue_delayed_work(wc->wq, &work->work, msecs_to_jiffies(10));
1202 }
1203 }
1204
1205 /**
1206 * __t43x_set_sclk_src - Change the current source of the source clock.
1207 * @wc: The board to change the clock source on.
1208 * @mode: The clock mode that we would like to move to.
1209 * @master: If true, drive the clock on the timing header.
1210 *
1211 * The clock source cannot be changed while DMA is active, so this function
1212 * will stop the DMA, then queue a delayed work item in order to come back and
1213 * check that DMA was actually stopped before changing the source of the clock.
1214 *
1215 */
1216 static void
1217 __t43x_set_sclk_src(struct t43x *wc, enum wcxb_clock_sources mode, bool master)
1218 {
1219 struct t43x_clksrc_work *const work = &wc->clksrc_work;
1220 unsigned long flags;
1221 bool changed = false;
1222
1223 /* Cannot drive the clock on the header while also slaving from it. */
1224 WARN_ON(master && (mode == WCXB_CLOCK_SLAVE));
1225
1226 spin_lock_irqsave(&work->lock, flags);
1227 if (!delayed_work_pending(&work->work)) {
1228 /* We want to check the actual settings. */
1229 changed = (wcxb_get_clksrc(&wc->xb) != mode) ||
1230 (wcxb_is_timing_header_driver_enabled(&wc->xb) != master);
1231 } else {
1232 /* Otherwise, we'll check if delayed work is going to set it to
1233 * the same value. */
1234
1235 changed = (work->clksrc != mode) ||
1236 (work->is_timing_master != master);
1237 }
1238 if (changed) {
1239 work->clksrc = mode;
1240 work->is_timing_master = master;
1241 }
1242 spin_unlock_irqrestore(&work->lock, flags);
1243
1244 if (!changed) {
1245 if (debug)
1246 dev_info(&wc->xb.pdev->dev, "Clock source is unchanged\n");
1247 return;
1248 }
1249
1250 wcxb_stop_dma(&wc->xb);
1251
1252 dev_dbg(&wc->xb.pdev->dev,
1253 "Queueing delayed work for clock source change\n");
1254
1255 queue_delayed_work(wc->wq, &work->work, msecs_to_jiffies(10));
1256 }
1257
1258 static ssize_t t43x_timing_master_show(struct device *dev,
1259 struct device_attribute *attr,
1260 char *buf)
1261 {
1262 struct t43x *wc = dev_get_drvdata(dev);
1263
1264 if (wcxb_is_timing_header_driver_enabled(&wc->xb))
1265 return sprintf(buf, "%d\n", wc->syncsrc);
1266 else
1267 return sprintf(buf, "%d\n", -1);
1268 }
1269
1270 static DEVICE_ATTR(timing_master, 0400, t43x_timing_master_show, NULL);
1271
1272 static void create_sysfs_files(struct t43x *wc)
1273 {
1274 int ret;
1275 ret = device_create_file(&wc->xb.pdev->dev,
1276 &dev_attr_timing_master);
1277 if (ret) {
1278 dev_info(&wc->xb.pdev->dev,
1279 "Failed to create device attributes.\n");
1280 }
1281 }
1282
1283 static void remove_sysfs_files(struct t43x *wc)
1284 {
1285 device_remove_file(&wc->xb.pdev->dev, &dev_attr_timing_master);
1286 }
1287
1288 static inline void __t43x_update_timing(struct t43x *wc)
1289 {
1290 int i;
1291
1292 /* update sync src info */
1293 if (wc->syncsrc != syncsrc) {
1294 dev_info(&wc->xb.pdev->dev, "Swapping card %d from %d to %d\n",
1295 wc->num, wc->syncsrc, syncsrc);
1296 wc->syncsrc = syncsrc;
1297 /* Update sync sources */
1298 for (i = 0; i < wc->numspans; i++)
1299 wc->tspans[i]->span.syncsrc = wc->syncsrc;
1300 if (syncnum == wc->num) {
1301 __t43x_set_rclk_src(wc, syncspan-1);
1302 __t43x_set_sclk_src(wc, WCXB_CLOCK_RECOVER, 1);
1303 if (debug) {
1304 dev_notice(&wc->xb.pdev->dev,
1305 "Card %d, using sync span %d, master\n",
1306 wc->num, syncspan);
1307 }
1308 } else {
1309 __t43x_set_sclk_src(wc, WCXB_CLOCK_SLAVE, 0);
1310 if (debug) {
1311 dev_notice(&wc->xb.pdev->dev,
1312 "Card %d, using Timing Bus, NOT master\n",
1313 wc->num);
1314 }
1315 }
1316 }
1317 }
1318
1319 static int __t43x_findsync(struct t43x *wc)
1320 {
1321 int i;
1322 unsigned long flags;
1323 int p;
1324 int nonzero;
1325 int newsyncsrc = 0; /* DAHDI span number */
1326 int newsyncnum = 0; /* wct4xxp card number */
1327 int newsyncspan = 0; /* span on given wct4xxp card */
1328 struct t43x *cur;
1329
1330 spin_lock_irqsave(&synclock, flags);
1331 if (!wc->num) {
1332 /* If we're the first card, go through all the motions, up to 8
1333 * levels of sync source */
1334 p = 1;
1335 while (p < 8) {
1336 nonzero = 0;
1337 list_for_each_entry(cur, &card_list, card_node) {
1338 for (i = 0; i < cur->numspans; i++) {
1339 struct t43x_span *const ts =
1340 cur->tspans[i];
1341 struct dahdi_span *const s =
1342 &cur->tspans[i]->span;
1343 if (!ts->syncpos)
1344 continue;
1345 nonzero = 1;
1346 if ((ts->syncpos == p) &&
1347 !(s->alarms & NOSYNC_ALARMS) &&
1348 (s->flags & DAHDI_FLAG_RUNNING)) {
1349 /* This makes a good sync
1350 * source */
1351 newsyncsrc = s->spanno;
1352 newsyncnum = cur->num;
1353 newsyncspan = i + 1;
1354 /* Jump out */
1355 goto found;
1356 }
1357 }
1358 }
1359 if (nonzero)
1360 p++;
1361 else
1362 break;
1363 }
1364 found:
1365 if ((syncnum != newsyncnum) ||
1366 (syncsrc != newsyncsrc) ||
1367 (newsyncspan != syncspan)) {
1368 if (debug) {
1369 dev_notice(&wc->xb.pdev->dev,
1370 "New syncnum: %d (was %d), syncsrc: %d (was %d), syncspan: %d (was %d)\n",
1371 newsyncnum, syncnum, newsyncsrc,
1372 syncsrc, newsyncspan, syncspan);
1373 }
1374 syncnum = newsyncnum;
1375 syncsrc = newsyncsrc;
1376 syncspan = newsyncspan;
1377 nonzero = 0;
1378 list_for_each_entry(cur, &card_list, card_node)
1379 __t43x_update_timing(cur);
1380 }
1381 }
1382 __t43x_update_timing(wc);
1383 spin_unlock_irqrestore(&synclock, flags);
1384 return 0;
1385 }
1386
1387 static void __t43x_set_timing_source_auto(struct t43x *wc)
1388 {
1389 int x, i;
1390 int firstprio, secondprio;
1391 firstprio = secondprio = 4;
1392
1393 if (debug)
1394 dev_info(&wc->xb.pdev->dev, "timing source auto\n");
1395
1396 if (timingcable) {
1397 __t43x_findsync(wc);
1398 } else {
1399 if (debug)
1400 dev_info(&wc->xb.pdev->dev,
1401 "Evaluating spans for timing source\n");
1402 for (x = 0; x < wc->numspans; x++) {
1403 if ((wc->tspans[x]->span.flags & DAHDI_FLAG_RUNNING) &&
1404 !(wc->tspans[x]->span.alarms & (DAHDI_ALARM_RED |
1405 DAHDI_ALARM_BLUE))) {
1406 if (debug) {
1407 dev_info(&wc->xb.pdev->dev,
1408 "span %d is green : syncpos %d\n",
1409 x+1, wc->tspans[x]->syncpos);
1410 }
1411 if (wc->tspans[x]->syncpos) {
1412 /* Valid rsync source in recovered
1413 timing mode */
1414 if (firstprio == 4)
1415 firstprio = x;
1416 else if (wc->tspans[x]->syncpos <
1417 wc->tspans[firstprio]->syncpos)
1418 firstprio = x;
1419 } else {
1420 /* Valid rsync source in system timing
1421 mode */
1422 if (secondprio == 4)
1423 secondprio = x;
1424 }
1425 }
1426 }
1427 if (firstprio != 4) {
1428 wc->syncsrc = firstprio;
1429 __t43x_set_rclk_src(wc, firstprio);
1430 __t43x_set_sclk_src(wc, WCXB_CLOCK_RECOVER, 0);
1431 dev_info(&wc->xb.pdev->dev,
1432 "Recovered timing mode, RCLK set to span %d\n",
1433 firstprio+1);
1434 } else if (secondprio != 4) {
1435 wc->syncsrc = -1;
1436 __t43x_set_rclk_src(wc, secondprio);
1437 __t43x_set_sclk_src(wc, WCXB_CLOCK_SELF, 0);
1438 dev_info(&wc->xb.pdev->dev,
1439 "System timing mode, RCLK set to span %d\n",
1440 secondprio+1);
1441 } else {
1442 wc->syncsrc = -1;
1443 dev_info(&wc->xb.pdev->dev,
1444 "All spans in alarm : No valid span to source RCLK from\n");
1445 /* Default rclk to lock with span 1 */
1446 __t43x_set_rclk_src(wc, 0);
1447 __t43x_set_sclk_src(wc, WCXB_CLOCK_SELF, 0);
1448 }
1449
1450 /* Propagate sync selection to dahdi_span struct
1451 * this is read by dahdi_tool to display the span's
1452 * master/slave sync information */
1453 for (i = 0; i < wc->numspans; i++)
1454 wc->tspans[i]->span.syncsrc = wc->syncsrc + 1;
1455 }
1456 }
1457
1458 static void
1459 t43x_configure_t1(struct t43x *wc, int span_idx, int lineconfig, int txlevel)
1460 {
1461 struct t43x_span *ts = wc->tspans[span_idx];
1462 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
1463 unsigned int fmr4, fmr2, fmr1, fmr0, lim2;
1464 char *framing, *line;
1465 int mytxlevel, reg;
1466 unsigned long flags;
1467
1468 if ((txlevel > 7) || (txlevel < 4))
1469 mytxlevel = 0;
1470 else
1471 mytxlevel = txlevel - 4;
1472
1473 /* FMR1: Mode 0, T1 mode, CRC on for ESF, 8.192 Mhz system data rate,
1474 * no XAIS */
1475 fmr1 = 0x9c;
1476 /* FMR2: no payload loopback, don't auto yellow alarm */
1477 fmr2 = 0x20;
1478
1479 if (SPANTYPE_DIGITAL_J1 == ts->span.spantype) {
1480 fmr4 = 0x1c;
1481 } else {
1482 /* FMR4: Lose sync on 2 out of 5 framing bits, auto resync */
1483 fmr4 = 0x0c;
1484 }
1485
1486 /* LIM2: 50% peak is a "1", Advanced Loss Recovery, Multi Purpose
1487 * Analog Switch enabled */
1488 lim2 = 0x23;
1489 /* LIM2: Add line buildout */
1490 lim2 |= (mytxlevel << 6);
1491
1492 spin_lock_irqsave(&wc->reglock, flags);
1493
1494 __t43x_framer_set(wc, fidx, 0x1d, fmr1);
1495 __t43x_framer_set(wc, fidx, 0x1e, fmr2);
1496
1497 /* Configure line interface */
1498 if (lineconfig & DAHDI_CONFIG_AMI) {
1499 line = "AMI";
1500 /* WCT4XX has workaround for errata fmr0 = 0xb0 */
1501 /* was fmr0 = 0xa0; */
1502 fmr0 = 0xb0;
1503 } else {
1504 line = "B8ZS";
1505 fmr0 = 0xf0;
1506 }
1507 if (lineconfig & DAHDI_CONFIG_D4) {
1508 framing = "D4";
1509 } else {
1510 framing = "ESF";
1511 fmr4 |= 0x2;
1512 fmr2 |= 0xc0;
1513 }
1514
1515 /* Suppress RSC interrupt for cleared channels */
1516 __t43x_framer_set(wc, fidx, 0x09, 0x80);
1517 __t43x_framer_set(wc, fidx, 0x1c, fmr0);
1518
1519 __t43x_framer_set(wc, fidx, 0x20, fmr4);
1520 /* FMR5: Enable RBS mode */
1521 __t43x_framer_set(wc, fidx, 0x21, 0x40);
1522
1523 /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No
1524 * remote loop, no DRS */
1525 __t43x_framer_set(wc, fidx, 0x37, 0xf0);
1526 /* LIM0: Enable auto long haul mode, no local loop (set after LIM1) */
1527 __t43x_framer_set(wc, fidx, 0x36, 0x08);
1528
1529 /* CMDR: Reset the receiver and transmitter line interface */
1530 __t43x_framer_set(wc, fidx, 0x02, 0x50);
1531 /* CMDR: Reset the receiver and transmitter line interface */
1532 __t43x_framer_set(wc, fidx, 0x02, 0x00);
1533 if (debug) {
1534 dev_info(&wc->xb.pdev->dev,
1535 "card %d span %d: setting Rtx to 0ohm for T1\n",
1536 wc->num, span_idx);
1537 }
1538 /* PC6: set Rtx to 0ohm for T1 */
1539 __t43x_framer_set(wc, fidx, 0x86, 0x00);
1540
1541 /* Bugfix register for errata #3 */
1542 __t43x_framer_set(wc, fidx, 0xbd, 0x05);
1543
1544 /* LIM2: 50% peak amplitude is a "1" */
1545 __t43x_framer_set(wc, fidx, 0x3a, lim2);
1546 /* PCD: LOS after 176 consecutive "zeros" */
1547 __t43x_framer_set(wc, fidx, 0x38, 0x0a);
1548 /* PCR: 22 "ones" clear LOS */
1549 __t43x_framer_set(wc, fidx, 0x39, 0x15);
1550
1551 reg = __t43x_framer_get(wc, fidx, 0x24);
1552 if (SPANTYPE_DIGITAL_J1 == ts->span.spantype) {
1553 /* set J1 overide */
1554 __t43x_framer_set(wc, fidx, 0x24, reg | 0x80);
1555 } else {
1556 /* clear J1 overide */
1557 __t43x_framer_set(wc, fidx, 0x24, reg & ~0x80);
1558 }
1559
1560 /* Generate pulse mask for T1 */
1561 switch (mytxlevel) {
1562 case 3:
1563 __t43x_framer_set(wc, fidx, 0x26, 0x07); /* XPM0 */
1564 __t43x_framer_set(wc, fidx, 0x27, 0x01); /* XPM1 */
1565 __t43x_framer_set(wc, fidx, 0x28, 0x00); /* XPM2 */
1566 break;
1567 case 2:
1568 __t43x_framer_set(wc, fidx, 0x26, 0x8c); /* XPM0 */
1569 __t43x_framer_set(wc, fidx, 0x27, 0x11); /* XPM1 */
1570 __t43x_framer_set(wc, fidx, 0x28, 0x01); /* XPM2 */
1571 break;
1572 case 1:
1573 __t43x_framer_set(wc, fidx, 0x26, 0x8c); /* XPM0 */
1574 __t43x_framer_set(wc, fidx, 0x27, 0x01); /* XPM1 */
1575 __t43x_framer_set(wc, fidx, 0x28, 0x00); /* XPM2 */
1576 break;
1577 case 0:
1578 default:
1579 __t43x_framer_set(wc, fidx, 0x26, 0x1a); /* XPM0 */
1580 __t43x_framer_set(wc, fidx, 0x27, 0x27); /* XPM1 */
1581 __t43x_framer_set(wc, fidx, 0x28, 0x01); /* XPM2 */
1582 break;
1583 }
1584
1585 __t43x_framer_set(wc, fidx, 0x14, 0xff); /* IMR0 */
1586 __t43x_framer_set(wc, fidx, 0x15, 0xff); /* IMR1 */
1587
1588 /* IMR2: All the alarms */
1589 __t43x_framer_set(wc, fidx, 0x16, 0x00);
1590 /* IMR3: ES, SEC, LLBSC, rx slips */
1591 __t43x_framer_set(wc, fidx, 0x17, 0x34);
1592 /* IMR4: Slips on transmit */
1593 __t43x_framer_set(wc, fidx, 0x18, 0x3f);
1594
1595 spin_unlock_irqrestore(&wc->reglock, flags);
1596 dev_info(&wc->xb.pdev->dev, "Span %d configured for %s/%s\n",
1597 span_idx, framing, line);
1598 }
1599
1600 static void t43x_configure_e1(struct t43x *wc, int span_idx, int lineconfig)
1601 {
1602 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
1603 unsigned int fmr2, fmr1, fmr0;
1604 unsigned int cas = 0;
1605 unsigned int imr3extra = 0;
1606 char *crc4 = "";
1607 char *framing, *line;
1608 unsigned long flags;
1609
1610 fmr1 = 0x44; /* FMR1: E1 mode, Automatic force resync, PCM30 mode,
1611 8.192 Mhz backplane, no XAIS */
1612 fmr2 = 0x03; /* FMR2: Auto transmit remote alarm, auto loss of
1613 multiframe recovery, no payload loopback */
1614
1615 if (lineconfig & DAHDI_CONFIG_CRC4) {
1616 fmr1 |= 0x08; /* CRC4 transmit */
1617 fmr2 |= 0xc0; /* CRC4 receive */
1618 crc4 = "/CRC4";
1619 }
1620
1621 spin_lock_irqsave(&wc->reglock, flags);
1622
1623 __t43x_framer_set(wc, fidx, 0x1d, fmr1);
1624 __t43x_framer_set(wc, fidx, 0x1e, fmr2);
1625
1626 /* Configure line interface */
1627 if (lineconfig & DAHDI_CONFIG_AMI) {
1628 line = "AMI";
1629 /* workaround for errata #2 in ES v3 09-10-16 */
1630 fmr0 = 0xb0;
1631 } else {
1632 line = "HDB3";
1633 fmr0 = 0xf0;
1634 }
1635
1636 if (lineconfig & DAHDI_CONFIG_CCS) {
1637 framing = "CCS";
1638 imr3extra = 0x28;
1639 } else {
1640 framing = "CAS";
1641 cas = 0x40;
1642 }
1643
1644 __t43x_framer_set(wc, fidx, 0x1c, fmr0);
1645
1646 /* LIM1: Clear data in case of LOS, Set receiver threshold (0.5V), No
1647 * remote loop, no DRS */
1648 __t43x_framer_set(wc, fidx, 0x37, 0xf0);
1649 /* LIM0: Enable auto long haul mode, no local loop (must be after
1650 * LIM1) */
1651 __t43x_framer_set(wc, fidx, 0x36, 0x08);
1652
1653 /* CMDR: Reset the receiver and transmitter line interface */
1654 __t43x_framer_set(wc, fidx, 0x02, 0x50);
1655 /* CMDR: Reset the receiver and transmitter line interface */
1656 __t43x_framer_set(wc, fidx, 0x02, 0x00);
1657 if (debug)
1658 dev_info(&wc->xb.pdev->dev,
1659 "setting Rtx to 7.5ohm for E1\n");
1660 /* PC6: turn on 7.5ohm Rtx for E1 */
1661 __t43x_framer_set(wc, fidx, 0x86, 0x40);
1662
1663 /* Condition receive line interface for E1 after reset */
1664 __t43x_framer_set(wc, fidx, 0xbb, 0x17);
1665 __t43x_framer_set(wc, fidx, 0xbc, 0x55);
1666 __t43x_framer_set(wc, fidx, 0xbb, 0x97);
1667 __t43x_framer_set(wc, fidx, 0xbb, 0x11);
1668 __t43x_framer_set(wc, fidx, 0xbc, 0xaa);
1669 __t43x_framer_set(wc, fidx, 0xbb, 0x91);
1670 __t43x_framer_set(wc, fidx, 0xbb, 0x12);
1671 __t43x_framer_set(wc, fidx, 0xbc, 0x55);
1672 __t43x_framer_set(wc, fidx, 0xbb, 0x92);
1673 __t43x_framer_set(wc, fidx, 0xbb, 0x0c);
1674 __t43x_framer_set(wc, fidx, 0xbb, 0x00);
1675 __t43x_framer_set(wc, fidx, 0xbb, 0x8c);
1676
1677 /* LIM2: 50% peak amplitude isa "1", Multi Purpose Analog Switch
1678 * enabled */
1679 __t43x_framer_set(wc, fidx, 0x3a, 0x22);
1680
1681 /* PCD: LOS after 176 consecutive "zeros" */
1682 __t43x_framer_set(wc, fidx, 0x38, 0x0a);
1683 /* PCR: 22 "ones" clear LOS */
1684 __t43x_framer_set(wc, fidx, 0x39, 0x15);
1685
1686 /* XSW: Spare bits all to 1 */
1687 __t43x_framer_set(wc, fidx, 0x20, 0x9f);
1688 /* XSP: E-bit set when async. AXS auto, XSIF to 1 */
1689 __t43x_framer_set(wc, fidx, 0x21, 0x1c|cas);
1690
1691 /* Generate pulse mask for E1 */
1692 __t43x_framer_set(wc, fidx, 0x26, 0xb5); /* XPM0 */
1693 __t43x_framer_set(wc, fidx, 0x27, 0x02); /* XPM1 */
1694 __t43x_framer_set(wc, fidx, 0x28, 0x00); /* XPM2 */
1695
1696 __t43x_framer_set(wc, fidx, 0x14, 0xff); /* IMR0 */
1697 __t43x_framer_set(wc, fidx, 0x15, 0xff); /* IMR1 */
1698
1699 __t43x_framer_set(wc, fidx, 0x16, 0x00); /* IMR2: the alarm stuff! */
1700 __t43x_framer_set(wc, fidx, 0x17, 0x04 | imr3extra); /* IMR3: AIS */
1701 __t43x_framer_set(wc, fidx, 0x18, 0x3f); /* IMR4: slips on transmit */
1702
1703 spin_unlock_irqrestore(&wc->reglock, flags);
1704 dev_info(&wc->xb.pdev->dev, "Span configured for %s/%s%s\n",
1705 framing, line, crc4);
1706 }
1707
1708 static bool have_open_channels(const struct t43x_span *ts)
1709 {
1710 int x, j;
1711 for (x = 0, j = 0; x < ts->span.channels; x++) {
1712 const struct dahdi_chan *chan = ts->span.chans[x];
1713 if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags) ||
1714 dahdi_have_netdev(chan))
1715 return true;
1716 }
1717 return false;
1718 }
1719
1720 static void t43x_framer_start(struct t43x *wc)
1721 {
1722 int unit;
1723 unsigned long flags;
1724 int res;
1725
1726 if (debug)
1727 dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
1728
1729 /* Disable fpga hardware interrupts */
1730 wcxb_disable_interrupts(&wc->xb);
1731
1732 /* Disable DMA */
1733 wcxb_stop_dma(&wc->xb);
1734 res = wcxb_wait_for_stop(&wc->xb, 50);
1735 if (res)
1736 dev_warn(&wc->xb.pdev->dev, "DMA engine did not stop.\n");
1737
1738 for (unit = 0; unit < wc->numspans; unit++) {
1739 struct t43x_span *ts = wc->tspans[unit];
1740 if (dahdi_is_e1_span(&ts->span)) {
1741 t43x_configure_e1(wc, unit, ts->span.lineconfig);
1742 } else { /* is a T1 card */
1743 t43x_configure_t1(wc, unit, ts->span.lineconfig,
1744 ts->span.txlevel);
1745 }
1746 t43x_set_cas_mode(wc, unit);
1747
1748 set_bit(DAHDI_FLAGBIT_RUNNING, &ts->span.flags);
1749
1750 /* Reset span alarm state to RED to prevent false
1751 * temporary GREEN state on span bringup */
1752 ts->span.alarms |= DAHDI_ALARM_RED;
1753 }
1754
1755 for (unit = 0; unit < wc->numspans; unit++) {
1756 /* Get this party started */
1757 struct t43x_span *ts = wc->tspans[unit];
1758
1759 /* Check for "open channels" here in case some channels have
1760 * netdev. */
1761 if (have_open_channels(ts))
1762 clear_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags);
1763 else
1764 set_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags);
1765
1766 local_irq_save(flags);
1767 t43x_check_alarms(wc, unit);
1768 t43x_check_sigbits(wc, unit);
1769 local_irq_restore(flags);
1770 }
1771
1772 dev_info(&wc->xb.pdev->dev, "Enabling DMA controller and interrupts\n");
1773
1774 /* start interrupts and DMA processing */
1775 wcxb_start(&wc->xb);
1776
1777 t43x_check_for_interrupts(wc);
1778
1779 /* force re-evaluation of timing source */
1780 wc->syncsrc = -1;
1781 spin_lock_irqsave(&wc->reglock, flags);
1782 __t43x_set_timing_source_auto(wc);
1783 spin_unlock_irqrestore(&wc->reglock, flags);
1784
1785 /* Clear all counters */
1786 for (unit = 0; unit < wc->numspans; unit++) {
1787 struct t43x_span *ts = wc->tspans[unit];
1788 memset(&ts->span.count, 0, sizeof(ts->span.count));
1789 }
1790
1791 /* Invoke timer function to set leds */
1792 mod_timer(&wc->timer, jiffies);
1793 }
1794
1795 #ifndef RPC_RCLK
1796 /**
1797 * t43x_check_spanconfig - Return 0 if the span configuration is valid.
1798 * @wc - The card to check.
1799 *
1800 * The TE435 cannot sync timing from a span in a different line mode than the
1801 * first span. This function should be called after the spans are configured to
1802 * ensure that the are not configured in this mode.
1803 *
1804 */
1805 static int t43x_check_spanconfig(const struct t43x *wc)
1806 {
1807 unsigned int i;
1808 bool span_1_is_e1;
1809
1810 if (&te435 != wc->devtype)
1811 return 0;
1812
1813 span_1_is_e1 = dahdi_is_e1_span(&wc->tspans[0]->span);
1814
1815 for (i = 1; i < wc->numspans; ++i) {
1816 struct t43x_span *ts = wc->tspans[i];
1817
1818 /* We only need to check spans that we could be a sync source */
1819 if (!ts->syncpos)
1820 continue;
1821
1822 if ((bool)dahdi_is_e1_span(&ts->span) == span_1_is_e1)
1823 continue;
1824
1825 dev_warn(&wc->xb.pdev->dev,
1826 "Local span %d is configured as a sync source but the line mode does not match local span 1.\n",
1827 ts->span.offset + 1);
1828 dev_warn(&wc->xb.pdev->dev,
1829 "Please configure local span 1 as a sync src and ensure all other local sync sources match the line config of span 1.\n");
1830
1831 return -EINVAL;
1832 }
1833
1834 return 0;
1835 }
1836 #endif
1837
1838 static int t43x_startup(struct file *file, struct dahdi_span *span)
1839 {
1840 struct t43x_span *ts = container_of(span, struct t43x_span, span);
1841 struct t43x *wc = ts->owner;
1842
1843 if (debug)
1844 dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
1845
1846 #ifndef RPC_RCLK
1847 if (t43x_check_spanconfig(wc))
1848 return -EINVAL;
1849 #endif
1850
1851 /* Reset framer with proper parameters and start */
1852 dev_info(&wc->xb.pdev->dev,
1853 "Calling startup (flags is %lu)\n", span->flags);
1854 t43x_framer_start(wc);
1855
1856 return 0;
1857 }
1858
1859 static inline bool is_initialized(struct t43x *wc)
1860 {
1861 WARN_ON(wc->not_ready < 0);
1862 return (wc->not_ready == 0);
1863 }
1864
1865 /**
1866 * t43x_wait_for_ready
1867 *
1868 * Check if the board has finished any setup and is ready to start processing
1869 * calls.
1870 */
1871 static int t43x_wait_for_ready(struct t43x *wc)
1872 {
1873 while (!is_initialized(wc)) {
1874 if (fatal_signal_pending(current))
1875 return -EIO;
1876 msleep_interruptible(250);
1877 }
1878 return 0;
1879 }
1880
1881 static int t43x_chanconfig(struct file *file,
1882 struct dahdi_chan *chan, int sigtype)
1883 {
1884 struct t43x *wc = chan->pvt;
1885
1886 if (file->f_flags & O_NONBLOCK && !is_initialized(wc))
1887 return -EAGAIN;
1888 else
1889 t43x_wait_for_ready(wc);
1890
1891 if (test_bit(DAHDI_FLAGBIT_RUNNING, &chan->span->flags))
1892 t43x_set_cas_mode(wc, chan->span->offset);
1893 return 0;
1894 }
1895
1896 static int t43x_rbsbits(struct dahdi_chan *chan, int bits)
1897 {
1898 u_char m, c;
1899 int n, b;
1900 struct t43x *wc = chan->pvt;
1901 struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
1902 int fidx = (2 == wc->numspans) ?
1903 chan->span->offset+1 : chan->span->offset;
1904 unsigned long flags;
1905
1906 if (dahdi_is_e1_span(&ts->span)) { /* do it E1 way */
1907 if (chan->chanpos == 16)
1908 return 0;
1909
1910 n = chan->chanpos - 1;
1911 if (chan->chanpos > 15)
1912 n--;
1913 b = (n % 15);
1914 spin_lock_irqsave(&wc->reglock, flags);
1915 c = ts->txsigs[b];
1916 m = (n / 15) << 2; /* nibble selector */
1917 c &= (0xf << m); /* keep the other nibble */
1918 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1919 ts->txsigs[b] = c;
1920 /* output them to the chip */
1921 __t43x_framer_set(wc, fidx, 0x71 + b, c);
1922 spin_unlock_irqrestore(&wc->reglock, flags);
1923 } else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
1924 n = chan->chanpos - 1;
1925 b = (n / 4);
1926 spin_lock_irqsave(&wc->reglock, flags);
1927 c = ts->txsigs[b];
1928 m = ((3 - (n % 4)) << 1); /* nibble selector */
1929 c &= ~(0x3 << m); /* keep the other nibble */
1930 c |= ((bits >> 2) & 0x3) << m; /* put our new nibble here */
1931 ts->txsigs[b] = c;
1932 /* output them to the chip */
1933 __t43x_framer_set(wc, fidx, 0x70 + b, c);
1934 __t43x_framer_set(wc, fidx, 0x70 + b + 6, c);
1935 spin_unlock_irqrestore(&wc->reglock, flags);
1936 } else if (ts->span.lineconfig & DAHDI_CONFIG_ESF) {
1937 n = chan->chanpos - 1;
1938 b = (n / 2);
1939 spin_lock_irqsave(&wc->reglock, flags);
1940 c = ts->txsigs[b];
1941 m = ((n % 2) << 2); /* nibble selector */
1942 c &= (0xf << m); /* keep the other nibble */
1943 c |= (bits & 0xf) << (4 - m); /* put our new nibble here */
1944 ts->txsigs[b] = c;
1945 /* output them to the chip */
1946 __t43x_framer_set(wc, fidx, 0x70 + b, c);
1947 spin_unlock_irqrestore(&wc->reglock, flags);
1948 }
1949
1950 return 0;
1951 }
1952
1953 static inline void t43x_dahdi_rbsbits(struct dahdi_chan *c, const int rxs)
1954 {
1955 if (!(c->sig & DAHDI_SIG_CLEAR) && (c->rxsig != rxs))
1956 dahdi_rbsbits(c, rxs);
1957 }
1958
1959 static void t43x_check_sigbits(struct t43x *wc, int span_idx)
1960 {
1961 int a, i, rxs;
1962 struct t43x_span *ts = wc->tspans[span_idx];
1963 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
1964
1965 if (dahdi_is_e1_span(&ts->span)) {
1966 for (i = 0; i < 15; i++) {
1967 a = t43x_framer_get(wc, fidx, 0x71 + i);
1968 rxs = (a & 0xf);
1969 t43x_dahdi_rbsbits(ts->span.chans[i+16], rxs);
1970
1971 rxs = (a >> 4) & 0xf;
1972 t43x_dahdi_rbsbits(ts->span.chans[i], rxs);
1973 }
1974 } else if (ts->span.lineconfig & DAHDI_CONFIG_D4) {
1975 for (i = 0; i < 24; i += 4) {
1976 a = t43x_framer_get(wc, fidx, 0x70 + (i>>2));
1977 rxs = (a & 0x3) << 2;
1978 t43x_dahdi_rbsbits(ts->span.chans[i+3], rxs);
1979
1980 rxs = (a & 0xc);
1981 t43x_dahdi_rbsbits(ts->span.chans[i+2], rxs);
1982
1983 rxs = (a >> 2) & 0xc;
1984 t43x_dahdi_rbsbits(ts->span.chans[i+1], rxs);
1985
1986 rxs = (a >> 4) & 0xc;
1987 t43x_dahdi_rbsbits(ts->span.chans[i], rxs);
1988 }
1989 } else {
1990 for (i = 0; i < 24; i += 2) {
1991 a = t43x_framer_get(wc, fidx, 0x70 + (i>>1));
1992 rxs = (a & 0xf);
1993 t43x_dahdi_rbsbits(ts->span.chans[i+1], rxs);
1994
1995 rxs = (a >> 4) & 0xf;
1996 t43x_dahdi_rbsbits(ts->span.chans[i], rxs);
1997 }
1998 }
1999 }
2000
2001 struct maint_work_struct {
2002 struct work_struct work;
2003 struct t43x *wc;
2004 int cmd;
2005 struct dahdi_span *span;
2006 };
2007
2008 static void t43x_maint_work(struct work_struct *work)
2009 {
2010 struct maint_work_struct *w = container_of(work,
2011 struct maint_work_struct, work);
2012
2013 struct t43x *wc = w->wc;
2014 struct dahdi_span *span = w->span;
2015 int reg = 0;
2016 int cmd = w->cmd;
2017 unsigned long flags;
2018 int fidx = (wc->numspans == 2) ? span->offset+1 : span->offset;
2019
2020 if (dahdi_is_e1_span(span)) {
2021 switch (cmd) {
2022 case DAHDI_MAINT_NONE:
2023 dev_info(&wc->xb.pdev->dev,
2024 "Clearing all maint modes\n");
2025 t43x_clear_maint(span);
2026 break;
2027 case DAHDI_MAINT_LOCALLOOP:
2028 dev_info(&wc->xb.pdev->dev,
2029 "Turning on local loopback\n");
2030 t43x_clear_maint(span);
2031 spin_lock_irqsave(&wc->reglock, flags);
2032 reg = __t43x_framer_get(wc, fidx, LIM0);
2033 if (reg < 0) {
2034 spin_unlock_irqrestore(&wc->reglock, flags);
2035 goto cleanup;
2036 }
2037 __t43x_framer_set(wc, fidx, LIM0, reg | LIM0_LL);
2038 spin_unlock_irqrestore(&wc->reglock, flags);
2039 break;
2040 case DAHDI_MAINT_NETWORKLINELOOP:
2041 dev_info(&wc->xb.pdev->dev,
2042 "Turning on network line loopback\n");
2043 t43x_clear_maint(span);
2044 spin_lock_irqsave(&wc->reglock, flags);
2045 reg = __t43x_framer_get(wc, fidx, LIM1);
2046 if (reg < 0) {
2047 spin_unlock_irqrestore(&wc->reglock, flags);
2048 goto cleanup;
2049 }
2050 __t43x_framer_set(wc, fidx, LIM1, reg | LIM1_RL);
2051 spin_unlock_irqrestore(&wc->reglock, flags);
2052 break;
2053 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
2054 dev_info(&wc->xb.pdev->dev,
2055 "Turning on network payload loopback\n");
2056 t43x_clear_maint(span);
2057 spin_lock_irqsave(&wc->reglock, flags);
2058 reg = __t43x_framer_get(wc, fidx, LIM1);
2059 if (reg < 0) {
2060 spin_unlock_irqrestore(&wc->reglock, flags);
2061 goto cleanup;
2062 }
2063 __t43x_framer_set(wc, fidx, LIM1,
2064 reg | (LIM1_RL | LIM1_JATT));
2065 spin_unlock_irqrestore(&wc->reglock, flags);
2066 break;
2067 case DAHDI_MAINT_FAS_DEFECT:
2068 t43x_framer_set(wc, fidx, 0x1b, (1 << 5));
2069 break;
2070 case DAHDI_MAINT_MULTI_DEFECT:
2071 t43x_framer_set(wc, fidx, 0x1b, (1 << 4));
2072 break;
2073 case DAHDI_MAINT_CRC_DEFECT:
2074 t43x_framer_set(wc, fidx, 0x1b, (1 << 3));
2075 break;
2076 case DAHDI_MAINT_CAS_DEFECT:
2077 t43x_framer_set(wc, fidx, 0x1b, (1 << 2));
2078 break;
2079 case DAHDI_MAINT_PRBS_DEFECT:
2080 t43x_framer_set(wc, fidx, 0x1b, (1 << 1));
2081 break;
2082 case DAHDI_MAINT_BIPOLAR_DEFECT:
2083 t43x_framer_set(wc, fidx, 0x1b, (1 << 0));
2084 break;
2085 default:
2086 dev_info(&wc->xb.pdev->dev,
2087 "Unknown E1 maint command: %d\n", cmd);
2088 goto cleanup;
2089 }
2090 } else {
2091 switch (cmd) {
2092 case DAHDI_MAINT_NONE:
2093 dev_info(&wc->xb.pdev->dev,
2094 "Clearing all maint modes\n");
2095 t43x_clear_maint(span);
2096 break;
2097 case DAHDI_MAINT_LOCALLOOP:
2098 dev_info(&wc->xb.pdev->dev,
2099 "Turning on local loopback\n");
2100 t43x_clear_maint(span);
2101 spin_lock_irqsave(&wc->reglock, flags);
2102 reg = __t43x_framer_get(wc, fidx, LIM0);
2103 if (reg < 0) {
2104 spin_unlock_irqrestore(&wc->reglock, flags);
2105 goto cleanup;
2106 }
2107 __t43x_framer_set(wc, fidx, LIM0, reg | LIM0_LL);
2108 spin_unlock_irqrestore(&wc->reglock, flags);
2109 break;
2110 case DAHDI_MAINT_NETWORKLINELOOP:
2111 dev_info(&wc->xb.pdev->dev,
2112 "Turning on network line loopback\n");
2113 t43x_clear_maint(span);
2114 spin_lock_irqsave(&wc->reglock, flags);
2115 reg = __t43x_framer_get(wc, fidx, LIM1);
2116 if (reg < 0) {
2117 spin_unlock_irqrestore(&wc->reglock, flags);
2118 goto cleanup;
2119 }
2120 __t43x_framer_set(wc, fidx, LIM1, reg | LIM1_RL);
2121 spin_unlock_irqrestore(&wc->reglock, flags);
2122 break;
2123 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
2124 dev_info(&wc->xb.pdev->dev,
2125 "Turning on network payload loopback\n");
2126 t43x_clear_maint(span);
2127 spin_lock_irqsave(&wc->reglock, flags);
2128 reg = __t43x_framer_get(wc, fidx, LIM1);
2129 if (reg < 0) {
2130 spin_unlock_irqrestore(&wc->reglock, flags);
2131 goto cleanup;
2132 }
2133 __t43x_framer_set(wc, fidx, LIM1,
2134 reg | (LIM1_RL | LIM1_JATT));
2135 spin_unlock_irqrestore(&wc->reglock, flags);
2136 break;
2137 case DAHDI_MAINT_LOOPUP:
2138 dev_info(&wc->xb.pdev->dev,
2139 "Transmitting loopup code\n");
2140 t43x_clear_maint(span);
2141 t43x_framer_set(wc, fidx, 0x21, 0x50);
2142 break;
2143 case DAHDI_MAINT_LOOPDOWN:
2144 dev_info(&wc->xb.pdev->dev,
2145 "Transmitting loopdown code\n");
2146 t43x_clear_maint(span);
2147 t43x_framer_set(wc, fidx, 0x21, 0x60);
2148 break;
2149 case DAHDI_MAINT_FAS_DEFECT:
2150 t43x_framer_set(wc, fidx, 0x1b, (1 << 5));
2151 break;
2152 case DAHDI_MAINT_MULTI_DEFECT:
2153 t43x_framer_set(wc, fidx, 0x1b, (1 << 4));
2154 break;
2155 case DAHDI_MAINT_CRC_DEFECT:
2156 t43x_framer_set(wc, fidx, 0x1b, (1 << 3));
2157 break;
2158 case DAHDI_MAINT_CAS_DEFECT:
2159 t43x_framer_set(wc, fidx, 0x1b, (1 << 2));
2160 break;
2161 case DAHDI_MAINT_PRBS_DEFECT:
2162 t43x_framer_set(wc, fidx, 0x1b, (1 << 1));
2163 break;
2164 case DAHDI_MAINT_BIPOLAR_DEFECT:
2165 t43x_framer_set(wc, fidx, 0x1b, (1 << 0));
2166 break;
2167 default:
2168 dev_info(&wc->xb.pdev->dev,
2169 "Unknown T1 maint command: %d\n", cmd);
2170 return;
2171 }
2172 }
2173
2174 /* update DAHDI_ALARM_LOOPBACK status bit and check timing source */
2175 spin_lock_irqsave(&wc->reglock, flags);
2176 if (!span->maintstat)
2177 span->alarms &= ~DAHDI_ALARM_LOOPBACK;
2178 dahdi_alarm_notify(span);
2179 __t43x_set_timing_source_auto(wc);
2180 spin_unlock_irqrestore(&wc->reglock, flags);
2181
2182 cleanup:
2183 kfree(w);
2184 return;
2185 }
2186
2187 static int t43x_reset_counters(struct dahdi_span *span)
2188 {
2189 struct t43x_span *ts = container_of(span, struct t43x_span, span);
2190 memset(&ts->span.count, 0, sizeof(ts->span.count));
2191 return 0;
2192 }
2193
2194 static int t43x_maint(struct dahdi_span *span, int cmd)
2195 {
2196 struct maint_work_struct *work;
2197 struct t43x_span *ts = container_of(span, struct t43x_span, span);
2198 struct t43x *wc = ts->owner;
2199
2200 if (dahdi_is_e1_span(span)) {
2201 switch (cmd) {
2202 case DAHDI_MAINT_NONE:
2203 case DAHDI_MAINT_LOCALLOOP:
2204 case DAHDI_MAINT_NETWORKLINELOOP:
2205 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
2206 case DAHDI_MAINT_FAS_DEFECT:
2207 case DAHDI_MAINT_MULTI_DEFECT:
2208 case DAHDI_MAINT_CRC_DEFECT:
2209 case DAHDI_MAINT_CAS_DEFECT:
2210 case DAHDI_MAINT_PRBS_DEFECT:
2211 case DAHDI_MAINT_BIPOLAR_DEFECT:
2212 break;
2213 case DAHDI_MAINT_LOOPUP:
2214 case DAHDI_MAINT_LOOPDOWN:
2215 dev_info(&wc->xb.pdev->dev,
2216 "Only local loop supported in E1 mode\n");
2217 return -ENOSYS;
2218 case DAHDI_RESET_COUNTERS:
2219 t43x_reset_counters(span);
2220 return 0;
2221 default:
2222 dev_info(&wc->xb.pdev->dev,
2223 "Unknown E1 maint command: %d\n", cmd);
2224 return -ENOSYS;
2225 }
2226 } else {
2227 switch (cmd) {
2228 case DAHDI_MAINT_NONE:
2229 case DAHDI_MAINT_LOCALLOOP:
2230 case DAHDI_MAINT_NETWORKLINELOOP:
2231 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
2232 case DAHDI_MAINT_LOOPUP:
2233 case DAHDI_MAINT_LOOPDOWN:
2234 case DAHDI_MAINT_FAS_DEFECT:
2235 case DAHDI_MAINT_MULTI_DEFECT:
2236 case DAHDI_MAINT_CRC_DEFECT:
2237 case DAHDI_MAINT_CAS_DEFECT:
2238 case DAHDI_MAINT_PRBS_DEFECT:
2239 case DAHDI_MAINT_BIPOLAR_DEFECT:
2240 break;
2241 case DAHDI_RESET_COUNTERS:
2242 t43x_reset_counters(span);
2243 return 0;
2244 default:
2245 dev_info(&wc->xb.pdev->dev,
2246 "Unknown T1 maint command: %d\n", cmd);
2247 return -ENOSYS;
2248 }
2249 }
2250
2251 work = kzalloc(sizeof(*work), GFP_ATOMIC);
2252 if (!work) {
2253 dev_info(&wc->xb.pdev->dev,
2254 "Failed to allocate memory for workqueue\n");
2255 return -ENOMEM;
2256 }
2257
2258 work->span = span;
2259 work->wc = wc;
2260 work->cmd = cmd;
2261
2262 INIT_WORK(&work->work, t43x_maint_work);
2263 queue_work(wc->wq, &work->work);
2264 return 0;
2265 }
2266
2267 static int t43x_clear_maint(struct dahdi_span *span)
2268 {
2269 struct t43x_span *ts = container_of(span, struct t43x_span, span);
2270 struct t43x *wc = ts->owner;
2271 int reg = 0;
2272 unsigned long flags;
2273 int fidx = (wc->numspans == 2) ? span->offset+1 : span->offset;
2274
2275 /* Turn off local loop */
2276 spin_lock_irqsave(&wc->reglock, flags);
2277 reg = __t43x_framer_get(wc, fidx, LIM0);
2278 if (reg < 0) {
2279 spin_unlock_irqrestore(&wc->reglock, flags);
2280 return -EIO;
2281 }
2282 __t43x_framer_set(wc, fidx, LIM0, reg & ~LIM0_LL);
2283
2284 /* Turn off remote loop & jitter attenuator */
2285 reg = __t43x_framer_get(wc, fidx, LIM1);
2286 if (reg < 0) {
2287 spin_unlock_irqrestore(&wc->reglock, flags);
2288 return -EIO;
2289 }
2290 __t43x_framer_set(wc, fidx, LIM1, reg & ~(LIM1_RL | LIM1_JATT));
2291
2292 /* Clear loopup/loopdown signals on the line */
2293 __t43x_framer_set(wc, fidx, 0x21, 0x40);
2294 spin_unlock_irqrestore(&wc->reglock, flags);
2295 return 0;
2296 }
2297
2298 static int t43x_ioctl(struct dahdi_chan *chan, unsigned int cmd,
2299 unsigned long data)
2300 {
2301 struct t4_regs regs;
2302 unsigned int x;
2303 struct t43x *wc;
2304 int fidx;
2305
2306 switch (cmd) {
2307 case WCT4_GET_REGS:
2308 wc = chan->pvt;
2309 fidx = (wc->numspans == 2) ? chan->span->offset+1 :
2310 chan->span->offset;
2311 for (x = 0; x < sizeof(regs.regs) / sizeof(regs.regs[0]); x++)
2312 regs.regs[x] = t43x_framer_get(wc, fidx, x);
2313
2314 if (copy_to_user((void __user *) data, &regs, sizeof(regs)))
2315 return -EFAULT;
2316 break;
2317 default:
2318 return -ENOTTY;
2319 }
2320 return 0;
2321 }
2322
2323 static void t43x_chan_set_sigcap(struct dahdi_span *span, int x)
2324 {
2325 struct t43x_span *ts = container_of(span, struct t43x_span, span);
2326 struct dahdi_chan *chan = ts->chans[x];
2327
2328 chan->sigcap = DAHDI_SIG_CLEAR;
2329 /* E&M variant supported depends on span type */
2330 if (dahdi_is_e1_span(span)) {
2331 /* E1 sigcap setup */
2332 if (span->lineconfig & DAHDI_CONFIG_CCS) {
2333 /* CCS setup */
2334 chan->sigcap |= DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
2335 return;
2336 }
2337 /* clear out sig and sigcap for channel 16 on E1 CAS
2338 * lines, otherwise, set it correctly */
2339 if (x == 15) {
2340 /* CAS signaling channel setup */
2341 ts->chans[15]->sigcap = 0;
2342 ts->chans[15]->sig = 0;
2343 return;
2344 }
2345 /* normal CAS setup */
2346 chan->sigcap |= DAHDI_SIG_EM_E1 | DAHDI_SIG_FXSLS |
2347 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_SF |
2348 DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS |
2349 DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
2350 } else {
2351 /* T1 sigcap setup */
2352 chan->sigcap |= DAHDI_SIG_EM | DAHDI_SIG_FXSLS |
2353 DAHDI_SIG_FXSGS | DAHDI_SIG_FXSKS | DAHDI_SIG_MTP2 |
2354 DAHDI_SIG_SF | DAHDI_SIG_FXOLS | DAHDI_SIG_FXOGS |
2355 DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_DACS_RBS;
2356 }
2357 }
2358
2359 static bool t43x_lineconfig_changed(struct t43x_span *ts,
2360 const struct dahdi_lineconfig *lc)
2361 {
2362 unsigned long flags;
2363 bool result;
2364 u32 crc = crc32(~0, lc, sizeof(*lc));
2365
2366 spin_lock_irqsave(&ts->owner->reglock, flags);
2367 result = (crc != ts->lineconfig_fingerprint);
2368 spin_unlock_irqrestore(&ts->owner->reglock, flags);
2369 return result;
2370 }
2371
2372 static void t43x_save_lineconfig(struct t43x_span *ts,
2373 const struct dahdi_lineconfig *lc)
2374 {
2375 unsigned long flags;
2376 u32 crc = crc32(~0, lc, sizeof(*lc));
2377
2378 spin_lock_irqsave(&ts->owner->reglock, flags);
2379 ts->lineconfig_fingerprint = crc;
2380 spin_unlock_irqrestore(&ts->owner->reglock, flags);
2381 }
2382
2383 static int
2384 t43x_spanconfig(struct file *file, struct dahdi_span *span,
2385 struct dahdi_lineconfig *lc)
2386 {
2387 struct t43x_span *ts = container_of(span, struct t43x_span, span);
2388 struct t43x *wc = ts->owner;
2389 int i;
2390 int res = 0;
2391
2392 if (debug)
2393 dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
2394
2395 if (file->f_flags & O_NONBLOCK) {
2396 if (!is_initialized(wc))
2397 return -EAGAIN;
2398 } else {
2399 t43x_wait_for_ready(wc);
2400 }
2401
2402 if (lc->sync < 0)
2403 lc->sync = 0;
2404 if (lc->sync > wc->numspans) {
2405 dev_warn(&wc->xb.pdev->dev,
2406 "WARNING: Cannot set priority on span %d to %d. Please set to a number between 1 and %d\n",
2407 span->spanno, lc->sync, wc->numspans);
2408 lc->sync = 0;
2409 }
2410
2411 /* remove this span number from the current sync sources, if there */
2412 for (i = 0; i < wc->numspans; i++) {
2413 if (wc->tspans[i]->sync == span->spanno)
2414 wc->tspans[i]->sync = 0;
2415 }
2416
2417 wc->tspans[span->offset]->syncpos = lc->sync;
2418 /* if a sync src, put it in proper place */
2419 if (lc->sync)
2420 wc->tspans[lc->sync - 1]->sync = span->spanno;
2421
2422 /* make sure that sigcaps gets updated if necessary */
2423 for (i = 0; i < span->channels; i++)
2424 t43x_chan_set_sigcap(span, i);
2425
2426 /* If already running, apply changes immediately */
2427 if (test_bit(DAHDI_FLAGBIT_RUNNING, &span->flags) &&
2428 t43x_lineconfig_changed(ts, lc)) {
2429 res = t43x_startup(file, span);
2430 if (!res)
2431 t43x_save_lineconfig(ts, lc);
2432 }
2433 return res;
2434 }
2435
2436 /*
2437 * Initialize a span
2438 *
2439 */
2440 static int
2441 t43x_init_one_span(struct t43x *wc, struct t43x_span *ts, enum linemode type)
2442 {
2443 int x;
2444 struct dahdi_chan *chans[32] = {NULL,};
2445 struct dahdi_echocan_state *ec[32] = {NULL,};
2446 unsigned long flags;
2447 int res = 0;
2448
2449 if (debug)
2450 dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
2451
2452 for (x = 0; x < ((E1 == type) ? 31 : 24); x++) {
2453 chans[x] = kzalloc(sizeof(*chans[x]), GFP_KERNEL);
2454 ec[x] = kzalloc(sizeof(*ec[x]), GFP_KERNEL);
2455 if (!chans[x] || !ec[x])
2456 goto error_exit;
2457 }
2458
2459 /* Stop the interrupt handler so that we may swap the channel array. */
2460 disable_irq(wc->xb.pdev->irq);
2461
2462 spin_lock_irqsave(&wc->reglock, flags);
2463 for (x = 0; x < ARRAY_SIZE(ts->chans); x++) {
2464 kfree(ts->chans[x]);
2465 kfree(ts->ec[x]);
2466 ts->chans[x] = NULL;
2467 ts->ec[x] = NULL;
2468 }
2469 memcpy(ts->chans, chans, sizeof(ts->chans));
2470 memcpy(ts->ec, ec, sizeof(ts->ec));
2471
2472 switch (type) {
2473 case E1:
2474 ts->span.channels = 31;
2475 ts->span.spantype = SPANTYPE_DIGITAL_E1;
2476 ts->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3 |
2477 DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
2478 ts->span.deflaw = DAHDI_LAW_ALAW;
2479 break;
2480 case T1:
2481 ts->span.channels = 24;
2482 ts->span.spantype = SPANTYPE_DIGITAL_T1;
2483 ts->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
2484 DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
2485 ts->span.deflaw = DAHDI_LAW_MULAW;
2486 break;
2487 case J1:
2488 ts->span.channels = 24;
2489 ts->span.spantype = SPANTYPE_DIGITAL_J1;
2490 ts->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS |
2491 DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
2492 ts->span.deflaw = DAHDI_LAW_MULAW;
2493 break;
2494 default:
2495 spin_unlock_irqrestore(&wc->reglock, flags);
2496 res = -EINVAL;
2497 enable_irq(wc->xb.pdev->irq);
2498 goto error_exit;
2499 }
2500
2501 spin_unlock_irqrestore(&wc->reglock, flags);
2502
2503 set_bit(DAHDI_FLAGBIT_RBS, &ts->span.flags);
2504 for (x = 0; x < ts->span.channels; x++) {
2505 snprintf(ts->chans[x]->name, sizeof(ts->chans[x]->name),
2506 "%s/%d", ts->span.name, x + 1);
2507 t43x_chan_set_sigcap(&ts->span, x);
2508 ts->chans[x]->pvt = wc;
2509 ts->chans[x]->chanpos = x + 1;
2510 }
2511
2512 t43x_reset_counters(&ts->span);
2513
2514 /* Span is in red alarm by default ? */
2515 ts->span.alarms = DAHDI_ALARM_NONE;
2516
2517 enable_irq(wc->xb.pdev->irq);
2518 return 0;
2519
2520 error_exit:
2521
2522 for (x = 0; x < ARRAY_SIZE(chans); ++x) {
2523 kfree(chans[x]);
2524 kfree(ec[x]);
2525 }
2526 return res;
2527 }
2528
2529 /*
2530 * Initialize all spans (one time)
2531 */
2532
2533 static void t43x_init_spans(struct t43x *wc, enum linemode type)
2534 {
2535 struct t43x_span *ts;
2536 int x;
2537
2538 if (debug)
2539 dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
2540
2541 for (x = 0; x < wc->numspans; x++) {
2542 ts = wc->tspans[x];
2543 sprintf(ts->span.name,
2544 "WCTE%d/%d/%d", wc->numspans, wc->num, x + 1);
2545 snprintf(ts->span.desc, sizeof(ts->span.desc) - 1,
2546 "WCTE%d3X (PCI) Card %d Span %d",
2547 wc->numspans, wc->num, x + 1);
2548
2549 ts->span.chans = ts->chans;
2550 ts->span.flags = DAHDI_FLAG_RBS;
2551 ts->span.ops = &t43x_span_ops;
2552 ts->span.offset = x;
2553
2554 ts->owner = wc;
2555
2556 t43x_init_one_span(wc, ts, type);
2557
2558 list_add_tail(&ts->span.device_node, &wc->ddev->spans);
2559 }
2560 }
2561
2562 /**
2563 * t1xx_set_linemode - Change the type of span before assignment.
2564 * @span: The span to change.
2565 * @linemode: Text string for the line mode.
2566 *
2567 * This function may be called after the dahdi_device is registered but
2568 * before the spans are assigned numbers (and are visible to the rest of
2569 * DAHDI).
2570 *
2571 */
2572 static int t43x_set_linemode(struct dahdi_span *span, enum spantypes linemode)
2573 {
2574 int res;
2575 struct t43x_span *ts = container_of(span, struct t43x_span, span);
2576 struct t43x *wc = ts->owner;
2577
2578 if (debug)
2579 dev_info(&wc->xb.pdev->dev, "%s\n", __func__);
2580
2581 /* We may already be set to the requested type. */
2582 if (span->spantype == linemode) {
2583 ts->span.alarms = DAHDI_ALARM_NONE;
2584 return 0;
2585 }
2586
2587 res = t43x_wait_for_ready(wc);
2588 if (res)
2589 return res;
2590
2591 mutex_lock(&wc->lock);
2592
2593 switch (linemode) {
2594 case SPANTYPE_DIGITAL_T1:
2595 dev_info(&wc->xb.pdev->dev,
2596 "Changing from %s to T1 line mode.\n",
2597 dahdi_spantype2str(span->spantype));
2598 res = t43x_init_one_span(wc, ts, T1);
2599 break;
2600 case SPANTYPE_DIGITAL_E1:
2601 dev_info(&wc->xb.pdev->dev,
2602 "Changing from %s to E1 line mode.\n",
2603 dahdi_spantype2str(span->spantype));
2604 res = t43x_init_one_span(wc, ts, E1);
2605 break;
2606 case SPANTYPE_DIGITAL_J1:
2607 dev_info(&wc->xb.pdev->dev,
2608 "Changing from %s to J1 line mode.\n",
2609 dahdi_spantype2str(span->spantype));
2610 res = t43x_init_one_span(wc, ts, J1);
2611 break;
2612 default:
2613 dev_err(&wc->xb.pdev->dev,
2614 "Got invalid linemode '%s' from dahdi\n",
2615 dahdi_spantype2str(linemode));
2616 res = -EINVAL;
2617 }
2618
2619 /* Since we probably reallocated the channels we need to make
2620 * sure they are configured before setting INITIALIZED again. */
2621 if (!res)
2622 dahdi_init_span(span);
2623
2624 mutex_unlock(&wc->lock);
2625 return res;
2626 }
2627
2628 static int t43x_hardware_post_init(struct t43x *wc, enum linemode *type)
2629 {
2630 int reg;
2631 int x;
2632
2633 if (!strcasecmp(default_linemode, "e1")) {
2634 *type = E1;
2635 } else if (!strcasecmp(default_linemode, "t1")) {
2636 *type = T1;
2637 } else if (!strcasecmp(default_linemode, "j1")) {
2638 *type = J1;
2639 } else {
2640 dev_warn(&wc->xb.pdev->dev,
2641 "'%s' is an unknown linemode. Defaulting to 't1'\n",
2642 default_linemode);
2643 *type = T1;
2644 }
2645
2646 if (debug) {
2647 dev_info(&wc->xb.pdev->dev, "linemode: %s\n",
2648 (*type == T1) ? "T1" : ((J1 == *type) ? "J1" : "E1"));
2649 }
2650
2651 /* what version of the FALC are we using? */
2652 wcxb_gpio_set(&wc->xb, FALC_CPU_RESET);
2653 reg = t43x_framer_get(wc, 0, 0x4a);
2654 if (reg < 0) {
2655 dev_info(&wc->xb.pdev->dev,
2656 "Failed to read FALC version (%x)\n", reg);
2657 return -EIO;
2658 }
2659 dev_info(&wc->xb.pdev->dev, "FALC version: %1x\n", reg);
2660
2661 /* make sure reads and writes work */
2662 for (x = 0; x < 256; x++) {
2663 t43x_framer_set(wc, 0, 0x14, x);
2664 reg = t43x_framer_get(wc, 0, 0x14);
2665 if (reg < 0) {
2666 dev_info(&wc->xb.pdev->dev,
2667 "Failed register read (%d)\n", reg);
2668 return -EIO;
2669 }
2670 if (reg != x) {
2671 dev_info(&wc->xb.pdev->dev,
2672 "Register test failed. Wrote '%x' but read '%x'\n",
2673 x, reg);
2674 return -EIO;
2675 }
2676 }
2677
2678 /* Enable all the GPIO outputs. */
2679 t43x_setleds(wc, -1);
2680
2681 return 0;
2682 }
2683
2684 static void t43x_check_alarms(struct t43x *wc, int span_idx)
2685 {
2686 struct t43x_span *ts = wc->tspans[span_idx];
2687 unsigned char c, d;
2688 int alarms;
2689 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
2690
2691 if (!(test_bit(DAHDI_FLAGBIT_RUNNING, &ts->span.flags)))
2692 return;
2693
2694 spin_lock(&wc->reglock);
2695
2696 c = __t43x_framer_get(wc, fidx, 0x4c);
2697 d = __t43x_framer_get(wc, fidx, 0x4d);
2698
2699 /* start with existing span alarms */
2700 alarms = ts->span.alarms;
2701
2702 if (dahdi_is_e1_span(&ts->span)) {
2703 if (c & 0x04) {
2704 /* No multiframe found, force RAI high after 400ms only
2705 * if we haven't found a multiframe since last loss of
2706 * frame */
2707 if (!test_and_set_bit(NMF_FLAGBIT, &ts->bit_flags)) {
2708 /* LIM0: Force RAI High */
2709 __t43x_framer_set(wc, fidx, 0x20, 0x9f | 0x20);
2710 dev_info(&wc->xb.pdev->dev,
2711 "NMF workaround on!\n");
2712 }
2713 /* Reset to CRC4 mode */
2714 __t43x_framer_set(wc, fidx, 0x1e, 0xc3);
2715 /* Force Resync */
2716 __t43x_framer_set(wc, fidx, 0x1c, 0xf2);
2717 /* Force Resync */
2718 __t43x_framer_set(wc, fidx, 0x1c, 0xf0);
2719 } else if (!(c & 0x02)) {
2720 if (test_and_clear_bit(NMF_FLAGBIT, &ts->bit_flags)) {
2721 /* LIM0: Clear forced RAI */
2722 __t43x_framer_set(wc, fidx, 0x20, 0x9f);
2723 dev_info(&wc->xb.pdev->dev,
2724 "NMF workaround off!\n");
2725 }
2726 }
2727 }
2728
2729 if (ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
2730 if (!test_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags))
2731 alarms |= DAHDI_ALARM_NOTOPEN;
2732 else
2733 alarms &= ~DAHDI_ALARM_NOTOPEN;
2734 }
2735
2736 if (c & 0x20) { /* LOF/LFA */
2737 if (!(alarms & DAHDI_ALARM_RED) && (0 == ts->lofalarmtimer)) {
2738 ts->lofalarmtimer = (jiffies +
2739 msecs_to_jiffies(alarmdebounce)) ?: 1;
2740 }
2741 } else {
2742 ts->lofalarmtimer = 0;
2743 }
2744
2745 if (c & 0x80) { /* LOS */
2746 if (!(alarms & DAHDI_ALARM_RED) && (0 == ts->losalarmtimer)) {
2747 ts->losalarmtimer = (jiffies +
2748 msecs_to_jiffies(losalarmdebounce)) ?: 1;
2749 }
2750 } else {
2751 ts->losalarmtimer = 0;
2752 }
2753
2754 if (!(c & (0x80|0x20)))
2755 alarms &= ~DAHDI_ALARM_RED;
2756
2757 if (c & 0x40) { /* AIS */
2758 if (!(alarms & DAHDI_ALARM_BLUE) && (0 == ts->aisalarmtimer))
2759 ts->aisalarmtimer = (jiffies +
2760 msecs_to_jiffies(aisalarmdebounce)) ?: 1;
2761 } else {
2762 ts->aisalarmtimer = 0;
2763 alarms &= ~DAHDI_ALARM_BLUE;
2764 }
2765
2766 /* Keep track of recovering */
2767 if (alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE|DAHDI_ALARM_NOTOPEN)) {
2768 ts->recoverytimer = 0;
2769 alarms &= ~DAHDI_ALARM_RECOVER;
2770 } else if (ts->span.alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE)) {
2771 if (0 == ts->recoverytimer) {
2772 ts->recoverytimer = (jiffies + 5*HZ) ?: 1;
2773 alarms |= DAHDI_ALARM_RECOVER;
2774 }
2775 }
2776
2777 if (c & 0x10) { /* receiving yellow (RAI) */
2778 if (!(alarms & DAHDI_ALARM_YELLOW) && !ts->yelalarmtimer) {
2779 ts->yelalarmtimer = (jiffies +
2780 msecs_to_jiffies(yelalarmdebounce)) ?: 1;
2781 }
2782 } else {
2783 ts->yelalarmtimer = 0;
2784 alarms &= ~DAHDI_ALARM_YELLOW;
2785 }
2786
2787 if (ts->span.alarms != alarms) {
2788 /* re-evaluate timing source if alarm state is changed */
2789 if ((alarms ^ ts->span.alarms) & NOSYNC_ALARMS) {
2790 ts->span.alarms = alarms;
2791 __t43x_set_timing_source_auto(wc);
2792 } else {
2793 ts->span.alarms = alarms;
2794 }
2795
2796 spin_unlock(&wc->reglock);
2797
2798 dahdi_alarm_notify(&ts->span);
2799 } else {
2800 spin_unlock(&wc->reglock);
2801 }
2802 }
2803
2804 static void t43x_check_loopcodes(struct t43x *wc, int span_idx)
2805 {
2806 struct t43x_span *ts = wc->tspans[span_idx];
2807 unsigned char frs1;
2808 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
2809
2810 frs1 = t43x_framer_get(wc, fidx, 0x4d);
2811
2812 /* Detect loopup code if we're not sending one */
2813 if ((ts->span.maintstat != DAHDI_MAINT_LOOPUP) && (frs1 & 0x08)) {
2814 /* Loop-up code detected */
2815 if ((ts->span.maintstat != DAHDI_MAINT_REMOTELOOP) &&
2816 (0 == ts->loopuptimer))
2817 ts->loopuptimer = (jiffies + msecs_to_jiffies(800)) | 1;
2818 } else {
2819 ts->loopuptimer = 0;
2820 }
2821
2822 /* Same for loopdown code */
2823 if ((ts->span.maintstat != DAHDI_MAINT_LOOPDOWN) && (frs1 & 0x10)) {
2824 /* Loop-down code detected */
2825 if ((ts->span.maintstat == DAHDI_MAINT_REMOTELOOP) &&
2826 (0 == ts->loopdntimer))
2827 ts->loopdntimer = (jiffies + msecs_to_jiffies(800)) | 1;
2828 } else {
2829 ts->loopdntimer = 0;
2830 }
2831 }
2832
2833 static void t43x_debounce_alarms(struct t43x *wc, int span_idx)
2834 {
2835 struct t43x_span *ts = wc->tspans[span_idx];
2836 int alarms;
2837 unsigned long flags;
2838 unsigned int fmr4;
2839 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
2840
2841 spin_lock_irqsave(&wc->reglock, flags);
2842 alarms = ts->span.alarms;
2843
2844 if (ts->lofalarmtimer && time_after(jiffies, ts->lofalarmtimer)) {
2845 alarms |= DAHDI_ALARM_RED;
2846 ts->lofalarmtimer = 0;
2847 dev_info(&wc->xb.pdev->dev, "LOF alarm detected\n");
2848 }
2849
2850 if (ts->losalarmtimer && time_after(jiffies, ts->losalarmtimer)) {
2851 alarms |= DAHDI_ALARM_RED;
2852 ts->losalarmtimer = 0;
2853 dev_info(&wc->xb.pdev->dev, "LOS alarm detected\n");
2854 }
2855
2856 if (ts->aisalarmtimer && time_after(jiffies, ts->aisalarmtimer)) {
2857 alarms |= DAHDI_ALARM_BLUE;
2858 ts->aisalarmtimer = 0;
2859 dev_info(&wc->xb.pdev->dev, "AIS alarm detected\n");
2860 }
2861
2862 if (ts->yelalarmtimer && time_after(jiffies, ts->yelalarmtimer)) {
2863 alarms |= DAHDI_ALARM_YELLOW;
2864 ts->yelalarmtimer = 0;
2865 dev_info(&wc->xb.pdev->dev, "YEL alarm detected\n");
2866 }
2867
2868 if (ts->recoverytimer && time_after(jiffies, ts->recoverytimer)) {
2869 alarms &= ~(DAHDI_ALARM_RECOVER);
2870 ts->recoverytimer = 0;
2871 memset(&ts->span.count, 0, sizeof(ts->span.count));
2872 dev_info(&wc->xb.pdev->dev, "Alarms cleared\n");
2873 }
2874
2875 if (alarms != ts->span.alarms) {
2876 /* re-evaluate timing source if alarm state is changed */
2877 if ((alarms ^ ts->span.alarms) & NOSYNC_ALARMS) {
2878 ts->span.alarms = alarms;
2879 __t43x_set_timing_source_auto(wc);
2880 } else {
2881 ts->span.alarms = alarms;
2882 }
2883
2884 spin_unlock_irqrestore(&wc->reglock, flags);
2885 dahdi_alarm_notify(&ts->span);
2886 spin_lock_irqsave(&wc->reglock, flags);
2887 }
2888
2889 /* If receiving alarms (except Yellow), go into Yellow alarm state */
2890 if (alarms & (DAHDI_ALARM_RED|DAHDI_ALARM_BLUE|
2891 DAHDI_ALARM_NOTOPEN|DAHDI_ALARM_RECOVER)) {
2892 if (!test_and_set_bit(SENDINGYELLOW_FLAGBIT, &ts->bit_flags)) {
2893 dev_info(&wc->xb.pdev->dev, "Setting yellow alarm\n");
2894 /* We manually do yellow alarm to handle RECOVER
2895 * and NOTOPEN, otherwise it's auto anyway */
2896 fmr4 = __t43x_framer_get(wc, fidx, 0x20);
2897 __t43x_framer_set(wc, fidx, 0x20, fmr4 | 0x20);
2898 }
2899 } else {
2900 if (test_and_clear_bit(SENDINGYELLOW_FLAGBIT, &ts->bit_flags)) {
2901 dev_info(&wc->xb.pdev->dev, "Clearing yellow alarm\n");
2902 /* We manually do yellow alarm to handle RECOVER */
2903 fmr4 = __t43x_framer_get(wc, fidx, 0x20);
2904 __t43x_framer_set(wc, fidx, 0x20, fmr4 & ~0x20);
2905 }
2906 }
2907
2908 spin_unlock_irqrestore(&wc->reglock, flags);
2909 }
2910
2911 static void t43x_debounce_loopcodes(struct t43x *wc, int span_idx)
2912 {
2913 struct t43x_span *ts = wc->tspans[span_idx];
2914 unsigned long flags;
2915 int fidx = (wc->numspans == 2) ? span_idx+1 : span_idx;
2916
2917 spin_lock_irqsave(&wc->reglock, flags);
2918 if (ts->loopuptimer && time_after(jiffies, ts->loopuptimer)) {
2919 /* Loop-up code debounced */
2920 dev_info(&wc->xb.pdev->dev, "Loopup detected, enabling remote loop\n");
2921 __t43x_framer_set(wc, fidx, 0x36, 0x08); /* LIM0: Disable
2922 any local loop */
2923 __t43x_framer_set(wc, fidx, 0x37, 0xf6); /* LIM1: Enable
2924 remote loop */
2925 ts->span.maintstat = DAHDI_MAINT_REMOTELOOP;
2926 ts->loopuptimer = 0;
2927 dahdi_alarm_notify(&ts->span);
2928 __t43x_set_timing_source_auto(wc);
2929 }
2930
2931 if (ts->loopdntimer && time_after(jiffies, ts->loopdntimer)) {
2932 /* Loop-down code debounced */
2933 dev_info(&wc->xb.pdev->dev, "Loopdown detected, disabling remote loop\n");
2934 __t43x_framer_set(wc, fidx, 0x36, 0x08); /* LIM0: Disable
2935 any local loop */
2936 __t43x_framer_set(wc, fidx, 0x37, 0xf0); /* LIM1: Disable
2937 remote loop */
2938 ts->span.maintstat = DAHDI_MAINT_NONE;
2939 ts->loopdntimer = 0;
2940 dahdi_alarm_notify(&ts->span);
2941 __t43x_set_timing_source_auto(wc);
2942 }
2943 spin_unlock_irqrestore(&wc->reglock, flags);
2944 }
2945
2946 static void handle_leds(struct t43x *wc)
2947 {
2948 u32 led = 0;
2949 struct t43x_span *ts;
2950 int span_idx;
2951
2952 if (time_after(jiffies, wc->blinktimer)) {
2953 wc->blink = !wc->blink;
2954 wc->blinktimer = jiffies + HZ/2;
2955 }
2956
2957 for (span_idx = wc->numspans-1; span_idx >= 0; span_idx--) {
2958 led >>= 2;
2959 ts = wc->tspans[span_idx];
2960 if ((ts->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE)) ||
2961 ts->losalarmtimer) {
2962 /* When we're in red alarm, blink the led once a
2963 * second. */
2964 if (wc->blink)
2965 led |= STATUS_LED_RED;
2966 } else if (ts->span.alarms & DAHDI_ALARM_YELLOW) {
2967 led |= STATUS_LED_YELLOW;
2968 } else {
2969 if (test_bit(DAHDI_FLAGBIT_RUNNING, &ts->span.flags))
2970 led |= STATUS_LED_GREEN;
2971 }
2972 }
2973
2974 if (wc->numspans == 2)
2975 led >>= 2;
2976
2977 if (led != (ioread32be(wc->xb.membase) & LED_MASK))
2978 t43x_setleds(wc, led);
2979 }
2980
2981 static void t43x_handle_receive(struct wcxb *xb, void *vfp)
2982 {
2983 int i, j, s;
2984 u_char *frame = (u_char *) vfp;
2985 struct t43x *wc = container_of(xb, struct t43x, xb);
2986 struct t43x_span *ts;
2987
2988 for (s = 0; s < wc->numspans; s++) {
2989 ts = wc->tspans[s];
2990 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &ts->span.flags))
2991 continue;
2992
2993 for (j = 0; j < DAHDI_CHUNKSIZE; j++) {
2994 for (i = 0; i < ts->span.channels; i++) {
2995 ts->chans[i]->readchunk[j] =
2996 frame[j*WCXB_DMA_CHAN_SIZE+(s+1+i*4)];
2997 }
2998 }
2999
3000 if (0 == vpmsupport) {
3001 for (i = 0; i < ts->span.channels; i++) {
3002 struct dahdi_chan *const c = ts->span.chans[i];
3003 __dahdi_ec_chunk(c, c->readchunk, c->readchunk,
3004 c->writechunk);
3005 }
3006 }
3007
3008 _dahdi_receive(&ts->span);
3009 }
3010 }
3011
3012 static void t43x_handle_transmit(struct wcxb *xb, void *vfp)
3013 {
3014 int i, j, s;
3015 u_char *frame = (u_char *) vfp;
3016 struct t43x *wc = container_of(xb, struct t43x, xb);
3017 struct t43x_span *ts;
3018
3019 for (s = 0; s < wc->numspans; s++) {
3020 ts = wc->tspans[s];
3021 if (!test_bit(DAHDI_FLAGBIT_REGISTERED,
3022 &ts->span.flags)) {
3023 continue;
3024 }
3025
3026 _dahdi_transmit(&ts->span);
3027
3028 for (j = 0; j < DAHDI_CHUNKSIZE; j++)
3029 for (i = 0; i < ts->span.channels; i++)
3030 frame[j*WCXB_DMA_CHAN_SIZE+(s+1+i*4)] =
3031 ts->chans[i]->writechunk[j];
3032 }
3033 }
3034
3035 #define SPAN_DEBOUNCE \
3036 (ts->lofalarmtimer || ts->losalarmtimer || \
3037 ts->aisalarmtimer || ts->yelalarmtimer || \
3038 ts->recoverytimer || \
3039 ts->loopuptimer || ts->loopdntimer)
3040
3041 #define SPAN_ALARMS \
3042 (ts->span.alarms & ~DAHDI_ALARM_NOTOPEN)
3043
3044 static void timer_work_func(struct work_struct *work)
3045 {
3046 struct t43x *wc = container_of(work, struct t43x, timer_work);
3047 struct t43x_span *ts;
3048 int x;
3049 bool start_timer = false;
3050
3051 for (x = 0; x < wc->numspans; x++) {
3052 ts = wc->tspans[x];
3053 if (ts->debounce) {
3054 /* Clear it now in case the interrupt needs to set it
3055 * again */
3056 ts->debounce = false;
3057 t43x_debounce_alarms(wc, x);
3058 if (!dahdi_is_e1_span(&ts->span))
3059 t43x_debounce_loopcodes(wc, x);
3060 if (SPAN_DEBOUNCE || SPAN_ALARMS)
3061 ts->debounce = true;
3062 if (ts->debounce)
3063 start_timer = true;
3064 }
3065 }
3066
3067 handle_leds(wc);
3068 if (start_timer)
3069 mod_timer(&wc->timer, jiffies + HZ/10);
3070 }
3071
3072 static void handle_falc_int(struct t43x *wc, int unit)
3073 {
3074 struct t43x_span *ts = wc->tspans[unit];
3075 unsigned char gis, isr0, isr1, isr2, isr3, isr4;
3076 static int intcount;
3077 bool start_timer;
3078 bool recheck_sigbits = false;
3079 int fidx = (wc->numspans == 2) ? unit+1 : unit;
3080
3081 intcount++;
3082 start_timer = false;
3083
3084 spin_lock(&wc->reglock);
3085 gis = __t43x_framer_get(wc, fidx, FRMR_GIS);
3086 isr0 = (gis&FRMR_GIS_ISR0) ? __t43x_framer_get(wc, fidx, FRMR_ISR0) : 0;
3087 isr1 = (gis&FRMR_GIS_ISR1) ? __t43x_framer_get(wc, fidx, FRMR_ISR1) : 0;
3088 isr2 = (gis&FRMR_GIS_ISR2) ? __t43x_framer_get(wc, fidx, FRMR_ISR2) : 0;
3089 isr3 = (gis&FRMR_GIS_ISR3) ? __t43x_framer_get(wc, fidx, FRMR_ISR3) : 0;
3090 isr4 = (gis&FRMR_GIS_ISR4) ? __t43x_framer_get(wc, fidx, FRMR_ISR4) : 0;
3091
3092 if ((debug) && !(isr3 & ISR3_SEC)) {
3093 dev_info(&wc->xb.pdev->dev,
3094 "span: %d gis: %02x, isr0: %02x, isr1: %02x, isr2: %02x, isr3: %02x, isr4: %02x, intcount=%u\n",
3095 unit, gis, isr0, isr1, isr2, isr3, isr4, intcount);
3096 }
3097
3098 /* Collect performance counters once per second */
3099 if (isr3 & ISR3_SEC) {
3100 ts->span.count.fe += __t43x_framer_get(wc, fidx, FECL_T);
3101 ts->span.count.crc4 += __t43x_framer_get(wc, fidx, CEC1L_T);
3102 ts->span.count.cv += __t43x_framer_get(wc, fidx, CVCL_T);
3103 ts->span.count.ebit += __t43x_framer_get(wc, fidx, EBCL_T);
3104 ts->span.count.be += __t43x_framer_get(wc, fidx, BECL_T);
3105 ts->span.count.prbs = __t43x_framer_get(wc, fidx, FRS1_T);
3106
3107 if (DAHDI_RXSIG_INITIAL == ts->span.chans[0]->rxhooksig)
3108 recheck_sigbits = true;
3109 }
3110 spin_unlock(&wc->reglock);
3111
3112 /* Collect errored second counter once per second */
3113 if (isr3 & ISR3_ES)
3114 ts->span.count.errsec += 1;
3115
3116 /* RSC/CASC: Received Signaling Information Changed */
3117 /* This interrupt is enabled by set_cas_mode() for CAS channels */
3118 if (isr0 & 0x08 || recheck_sigbits)
3119 t43x_check_sigbits(wc, unit);
3120
3121 if (dahdi_is_e1_span(&ts->span)) {
3122 /* E1 checks */
3123 if ((isr3 & 0x68) || isr2 || (isr1 & 0x7f)) {
3124 t43x_check_alarms(wc, unit);
3125 start_timer = true;
3126 }
3127 } else {
3128 /* T1 checks */
3129 if (isr2) {
3130 t43x_check_alarms(wc, unit);
3131 start_timer = true;
3132 }
3133 if (isr3 & 0x08) { /* T1 LLBSC */
3134 t43x_check_loopcodes(wc, unit);
3135 start_timer = true;
3136 }
3137 }
3138
3139 if (!ts->span.alarms) {
3140 if ((isr3 & 0x3) || (isr4 & 0xc0))
3141 ts->span.count.timingslips++;
3142 }
3143
3144 if (start_timer) {
3145 ts->debounce = true;
3146 if (!timer_pending(&wc->timer))
3147 mod_timer(&wc->timer, jiffies + HZ/10);
3148 }
3149 }
3150
3151 static void t43x_handle_interrupt(struct wcxb *xb, u32 pending)
3152 {
3153 u32 status;
3154 struct t43x *wc = container_of(xb, struct t43x, xb);
3155
3156 if (!(pending & FALC_INT))
3157 return;
3158
3159 /* Service at most one framer per interrupt cycle */
3160 status = t43x_framer_get(wc, 0, FRMR_CIS);
3161 if (status & (1 << (wc->intr_span + ((wc->numspans == 2) ? 1 : 0))))
3162 handle_falc_int(wc, wc->intr_span);
3163 if (++wc->intr_span >= wc->numspans)
3164 wc->intr_span = 0;
3165 }
3166
3167 static void t43x_timer(TIMER_DATA_TYPE timer)
3168 {
•3169 struct t43x *wc = timer_container_of(wc, timer, timer);
3170
3171 if (!is_initialized(wc))
3172 return;
3173
3174 queue_work(wc->wq, &wc->timer_work);
3175 return;
3176 }
3177
3178 static int t43x_open(struct dahdi_chan *chan)
3179 {
3180 struct t43x *wc = chan->pvt;
3181 struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
3182 unsigned long flags;
3183
3184 if (!(ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN))
3185 return 0;
3186
3187 if (!test_and_set_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags)) {
3188 local_irq_save(flags);
3189 t43x_check_alarms(wc, ts->span.offset);
3190 local_irq_restore(flags);
3191 }
3192
3193 return 0;
3194 }
3195
3196 static int t43x_close(struct dahdi_chan *chan)
3197 {
3198 struct t43x *wc = chan->pvt;
3199 struct t43x_span *ts = container_of(chan->span, struct t43x_span, span);
3200 unsigned long flags;
3201
3202 if (!(ts->span.lineconfig & DAHDI_CONFIG_NOTOPEN))
3203 return 0;
3204
3205 if (have_open_channels(ts))
3206 return 0;
3207
3208 if (!test_and_set_bit(HAVE_OPEN_CHANNELS_FLAGBIT, &ts->bit_flags)) {
3209 local_irq_save(flags);
3210 t43x_check_alarms(wc, ts->span.offset);
3211 local_irq_restore(flags);
3212 }
3213
3214 return 0;
3215 }
3216
3217 static const struct dahdi_span_ops t43x_span_ops = {
3218 .owner = THIS_MODULE,
3219 .spanconfig = t43x_spanconfig,
3220 .chanconfig = t43x_chanconfig,
3221 .startup = t43x_startup,
3222 .rbsbits = t43x_rbsbits,
3223 .maint = t43x_maint,
3224 .ioctl = t43x_ioctl,
3225 .assigned = t43x_span_assigned,
3226 .set_spantype = t43x_set_linemode,
3227 .echocan_create = t43x_echocan_create,
3228 .echocan_name = t43x_echocan_name,
3229 .open = t43x_open,
3230 .close = t43x_close,
3231 };
3232
3233 /**
3234 * t43x_check_for_interrupts - Return 0 if the card is generating interrupts.
3235 * @wc: The card to check.
3236 *
3237 * If the card is not generating interrupts, this function will also place all
3238 * the spans on the card into red alarm.
3239 *
3240 */
3241 static int t43x_check_for_interrupts(struct t43x *wc)
3242 {
3243 unsigned int starting_framecount = wc->xb.framecount;
3244 unsigned long stop_time = jiffies + HZ*2;
3245 unsigned long flags;
3246 int x;
3247
3248 msleep(20);
3249 spin_lock_irqsave(&wc->reglock, flags);
3250 while (starting_framecount == wc->xb.framecount) {
3251 spin_unlock_irqrestore(&wc->reglock, flags);
3252 if (time_after(jiffies, stop_time)) {
3253 for (x = 0; x < wc->numspans; x++)
3254 wc->tspans[x]->span.alarms = DAHDI_ALARM_RED;
3255 dev_err(&wc->xb.pdev->dev, "Interrupts not detected.\n");
3256 return -EIO;
3257 }
3258 msleep(100);
3259 spin_lock_irqsave(&wc->reglock, flags);
3260 }
3261 spin_unlock_irqrestore(&wc->reglock, flags);
3262
3263 return 0;
3264 }
3265
3266 /**
3267 * t43x_read_serial - Returns the serial number of the board.
3268 * @wc: The board whos serial number we are reading.
3269 *
3270 * The buffer returned is dynamically allocated and must be kfree'd by the
3271 * caller. If memory could not be allocated, NULL is returned.
3272 *
3273 * Must be called in process context.
3274 *
3275 */
3276 static char *t43x_read_serial(struct t43x *wc)
3277 {
3278 int i;
3279 static const int MAX_SERIAL = 20*5;
3280 const unsigned int SERIAL_ADDRESS = 0x1f0000;
3281 unsigned char *serial = kzalloc(MAX_SERIAL + 1, GFP_KERNEL);
3282 struct wcxb const *xb = &wc->xb;
3283 struct wcxb_spi_master *flash_spi_master = NULL;
3284 struct wcxb_spi_device *flash_spi_device = NULL;
3285 const unsigned int FLASH_SPI_BASE = 0x200;
3286
3287 if (!serial)
3288 return NULL;
3289
3290 flash_spi_master = wcxb_spi_master_create(&xb->pdev->dev,
3291 xb->membase + FLASH_SPI_BASE,
3292 false);
3293 if (!flash_spi_master)
3294 return NULL;
3295
3296 flash_spi_device = wcxb_spi_device_create(flash_spi_master, 0);
3297 if (!flash_spi_device)
3298 goto error_exit;
3299
3300 wcxb_flash_read(flash_spi_device, SERIAL_ADDRESS,
3301 serial, MAX_SERIAL);
3302
3303 for (i = 0; i < MAX_SERIAL; ++i) {
3304 if ((serial[i] < 0x20) || (serial[i] > 0x7e)) {
3305 serial[i] = '\0';
3306 break;
3307 }
3308 }
3309
3310 if (!i) {
3311 kfree(serial);
3312 serial = NULL;
3313 } else {
3314 /* Limit the size of the buffer to just what is needed to
3315 * actually hold the serial number. */
3316 unsigned char *new_serial;
3317 new_serial = kasprintf(GFP_KERNEL, "%s", serial);
3318 kfree(serial);
3319 serial = new_serial;
3320 }
3321
3322 error_exit:
3323 wcxb_spi_device_destroy(flash_spi_device);
3324 wcxb_spi_master_destroy(flash_spi_master);
3325 return serial;
3326 }
3327
3328
3329 /**
3330 * t43x_assign_num - Assign wc->num a unique value and place on card_list
3331 *
3332 */
3333 static void t43x_assign_num(struct t43x *wc)
3334 {
3335 mutex_lock(&card_list_lock);
3336 if (list_empty(&card_list)) {
3337 wc->num = 0;
3338 list_add(&wc->card_node, &card_list);
3339 } else {
3340 struct t43x *cur;
3341 struct list_head *insert_pos;
3342 int new_num = 0;
3343
3344 insert_pos = &card_list;
3345 list_for_each_entry(cur, &card_list, card_node) {
3346 if (new_num != cur->num)
3347 break;
3348 new_num++;
3349 insert_pos = &cur->card_node;
3350 }
3351
3352 wc->num = new_num;
3353 list_add_tail(&wc->card_node, insert_pos);
3354 }
3355 mutex_unlock(&card_list_lock);
3356 }
3357
3358 /*
3359 * Initialize the card (one time)
3360 *
3361 */
3362 static int __devinit t43x_init_one(struct pci_dev *pdev,
3363 const struct pci_device_id *ent)
3364 {
3365 struct t43x *wc;
3366 struct t43x_span *ts;
3367 unsigned int x;
3368 int res;
3369 enum linemode type;
3370
3371 wc = kzalloc(sizeof(*wc), GFP_KERNEL);
3372 if (!wc) {
3373 pci_disable_device(pdev);
3374 return -ENOMEM;
3375 }
3376
3377 t43x_assign_num(wc);
3378
3379 wc->blinktimer = jiffies;
3380 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3381 wc->not_ready = 1;
3382
3383 spin_lock_init(&wc->reglock);
3384
3385 pci_set_drvdata(pdev, wc);
3386 wc->xb.pdev = pdev;
3387 wc->xb.ops = &xb_ops;
3388 wc->xb.debug = &debug;
3389
3390 res = wcxb_init(&wc->xb, KBUILD_MODNAME, 0);
3391 if (res)
3392 goto fail_exit;
3393
3394 mutex_init(&wc->lock);
3395 timer_setup(&wc->timer, t43x_timer, 0);
3396
3397 INIT_WORK(&wc->timer_work, timer_work_func);
3398 INIT_DELAYED_WORK(&wc->clksrc_work.work, t43x_clksrc_work_fn);
3399 spin_lock_init(&wc->clksrc_work.lock);
3400
3401 wc->ddev = dahdi_create_device();
3402 if (!wc->ddev) {
3403 res = -ENOMEM;
3404 goto fail_exit;
3405 }
3406 wc->ddev->manufacturer = "Digium";
3407 wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
3408 pdev->bus->number,
3409 PCI_SLOT(pdev->devfn) + 1);
3410 if (!wc->ddev->location) {
3411 res = -ENOMEM;
3412 goto fail_exit;
3413 }
3414
3415 wc->ddev->hardware_id = t43x_read_serial(wc);
3416
3417 if (wc->ddev->hardware_id == NULL) {
3418 dev_info(&wc->xb.pdev->dev, "Unable to read valid serial number\n");
3419 res = -EIO;
3420 goto fail_exit;
3421 }
3422
3423 if (strncmp(wc->ddev->hardware_id, "1TE435F", 7) == 0) {
3424 /* Quad-span PCIE */
3425 wc->numspans = 4;
3426 wc->devtype = &te435;
3427 } else if (strncmp(wc->ddev->hardware_id, "1TE235F", 7) == 0) {
3428 /* Dual-span PCIE */
3429 wc->numspans = 2;
3430 wc->devtype = &te235;
3431 } else if (strncmp(wc->ddev->hardware_id, "1TE436F", 7) == 0) {
3432 /* Quad-span PCI */
3433 wc->numspans = 4;
3434 wc->devtype = &te436;
3435 } else if (strncmp(wc->ddev->hardware_id, "1TE236F", 7) == 0) {
3436 /* Dual-span PCI */
3437 wc->numspans = 2;
3438 wc->devtype = &te236;
3439 } else {
3440 dev_info(&wc->xb.pdev->dev,
3441 "Unable to identify board type from serial number %s\n",
3442 wc->ddev->hardware_id);
3443 res = -EIO;
3444 goto fail_exit;
3445 }
3446
3447 /* Check for field updatable firmware */
3448 if ((wc->devtype == &te435) || (wc->devtype == &te235)) {
3449 res = wcxb_check_firmware(&wc->xb, TE435_VERSION,
3450 TE435_FW_FILENAME, force_firmware, WCXB_RESET_NOW);
3451 } else if ((wc->devtype == &te436) || (wc->devtype == &te236)) {
3452 res = wcxb_check_firmware(&wc->xb, TE436_VERSION,
3453 TE436_FW_FILENAME, force_firmware, WCXB_RESET_NOW);
3454 } else {
3455 res = -EIO;
3456 }
3457
3458 if (res)
3459 goto fail_exit;
3460
3461 for (x = 0; x < wc->numspans; x++) {
3462 ts = kzalloc(sizeof(*wc->tspans[x]), GFP_KERNEL);
3463 if (!ts) {
3464 res = -ENOMEM;
3465 goto fail_exit;
3466 }
3467 wc->tspans[x] = ts;
3468 }
3469
3470 wc->wq = create_singlethread_workqueue(KBUILD_MODNAME);
3471 if (!wc->wq) {
3472 res = -ENOMEM;
3473 goto fail_exit;
3474 }
3475
3476 wcxb_set_minlatency(&wc->xb, latency);
3477 wcxb_set_maxlatency(&wc->xb, max_latency);
3478
3479 create_sysfs_files(wc);
3480
3481 res = t43x_hardware_post_init(wc, &type);
3482 if (res)
3483 goto fail_exit;
3484
3485 t43x_init_spans(wc, type);
3486
3487 if (!wc->vpm)
3488 t43x_vpm_init(wc);
3489
3490 if (wc->vpm) {
3491 wc->ddev->devicetype = kasprintf(GFP_KERNEL,
3492 "%s (%s)", wc->devtype->name,
3493 wc->vpm_name);
3494 } else {
3495 wc->ddev->devicetype = kasprintf(GFP_KERNEL,
3496 "%s", wc->devtype->name);
3497 }
3498
3499 res = dahdi_register_device(wc->ddev, &wc->xb.pdev->dev);
3500 if (res) {
3501 dev_info(&wc->xb.pdev->dev, "Unable to register with DAHDI\n");
3502 goto fail_exit;
3503 }
3504
3505 if (wc->ddev->hardware_id) {
3506 dev_info(&wc->xb.pdev->dev, "Found a %s (SN: %s)\n",
3507 wc->devtype->name, wc->ddev->hardware_id);
3508 } else {
3509 dev_info(&wc->xb.pdev->dev, "Found a %s\n",
3510 wc->devtype->name);
3511 }
3512
3513 wc->not_ready = 0;
3514 return 0;
3515
3516 fail_exit:
3517 wcxb_release(&wc->xb);
3518
3519 if (debug)
3520 dev_info(&wc->xb.pdev->dev, "***At fail_exit in init_one***\n");
3521
3522 for (x = 0; x < wc->numspans; x++)
3523 kfree(wc->tspans[x]);
3524
3525 free_wc(wc);
3526 return res;
3527 }
3528
3529 static void __devexit t43x_remove_one(struct pci_dev *pdev)
3530 {
3531 struct t43x *wc = pci_get_drvdata(pdev);
3532 dev_info(&wc->xb.pdev->dev, "Removing a Wildcard TE43x.\n");
3533
3534 if (!wc)
3535 return;
3536
3537 wc->not_ready = 1;
3538 smp_mb__after_atomic();
3539
3540 /* Stop everything */
3541 wcxb_stop(&wc->xb);
3542
3543 /* Leave framer in reset so it no longer transmits */
3544 wcxb_gpio_clear(&wc->xb, FALC_CPU_RESET);
3545
3546 /* Turn off status LEDs */
3547 t43x_setleds(wc, 0);
3548
3549 if (wc->vpm)
3550 release_vpm450m(wc->vpm);
3551 wc->vpm = NULL;
3552
3553 cancel_delayed_work_sync(&wc->clksrc_work.work);
3554
•3555 timer_delete_sync(&wc->timer);
3556 flush_workqueue(wc->wq);
•3557 timer_delete_sync(&wc->timer);
3558
3559 dahdi_unregister_device(wc->ddev);
3560
3561 remove_sysfs_files(wc);
3562
3563 wcxb_release(&wc->xb);
3564 free_wc(wc);
3565 }
3566
3567 static DEFINE_PCI_DEVICE_TABLE(t43x_pci_tbl) = {
3568 { 0xd161, 0x800e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3569 { 0xd161, 0x8013, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
3570 { 0 }
3571 };
3572
3573 static void t43x_shutdown(struct pci_dev *pdev)
3574 {
3575 struct t43x *wc = pci_get_drvdata(pdev);
3576 dev_info(&wc->xb.pdev->dev, "Quiescing a Wildcard TE43x.\n");
3577 if (!wc)
3578 return;
3579
3580 /* Stop everything */
3581 wcxb_stop(&wc->xb);
3582
3583 }
3584
3585 static int t43x_suspend(struct pci_dev *pdev, pm_message_t state)
3586 {
3587 return -ENOSYS;
3588 }
3589
3590 MODULE_DEVICE_TABLE(pci, t43x_pci_tbl);
3591
3592 static struct pci_driver t43x_driver = {
3593 .name = "wcte43x",
3594 .probe = t43x_init_one,
3595 .remove = __devexit_p(t43x_remove_one),
3596 .shutdown = t43x_shutdown,
3597 .suspend = t43x_suspend,
3598 .id_table = t43x_pci_tbl,
3599 };
3600
3601 static int __init t43x_init(void)
3602 {
3603 int res;
3604
3605 if (strcasecmp(default_linemode, "t1") &&
3606 strcasecmp(default_linemode, "j1") &&
3607 strcasecmp(default_linemode, "e1")) {
3608 pr_err("'%s' is an unknown span type.\n", default_linemode);
3609 default_linemode = "t1";
3610 return -EINVAL;
3611 }
3612
3613 res = pci_register_driver(&t43x_driver);
3614 if (res)
3615 return -ENODEV;
3616
3617 return 0;
3618 }
3619
3620
3621 static void __exit t43x_cleanup(void)
3622 {
3623 pci_unregister_driver(&t43x_driver);
3624 }
3625
3626 module_param(debug, int, S_IRUGO | S_IWUSR);
3627 module_param(timingcable, int, 0600);
3628 module_param(default_linemode, charp, S_IRUGO);
3629 MODULE_PARM_DESC(default_linemode, "\"t1\"(default), \"e1\", or \"j1\".");
3630 module_param(alarmdebounce, int, S_IRUGO | S_IWUSR);
3631 module_param(losalarmdebounce, int, S_IRUGO | S_IWUSR);
3632 module_param(aisalarmdebounce, int, S_IRUGO | S_IWUSR);
3633 module_param(yelalarmdebounce, int, S_IRUGO | S_IWUSR);
3634 module_param(vpmsupport, int, 0600);
3635 module_param(force_firmware, int, S_IRUGO);
3636 module_param(latency, int, S_IRUGO);
3637 MODULE_PARM_DESC(latency, "How many milliseconds of audio to buffer between card and host (3ms default). This number will increase during runtime, dynamically, if dahdi detects that it is too small. This is commonly refered to as a \"latency bump\"");
3638 module_param(max_latency, int, 0600);
3639 MODULE_PARM_DESC(max_latency, "The maximum amount of latency that the driver will permit.");
3640
3641 MODULE_DESCRIPTION("Wildcard Digital Card Driver");
3642 MODULE_AUTHOR("Digium Incorporated <[email protected]>");
3643 MODULE_LICENSE("GPL v2");
3644
3645 module_init(t43x_init);
3646 module_exit(t43x_cleanup);