Debian Codegraph @

drivers/dahdi/wcte43x-base.c

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