Debian Codegraph @

drivers/dahdi/xpp/xpp_dahdi.c

raw symbols SCIP
1 /*
2 * Written by Oron Peled <[email protected]>
3 * Copyright (C) 2004, Xorcom
4 *
5 * Derived from ztdummy
6 *
7 * Copyright (C) 2002, Hermes Softlab
8 * Copyright (C) 2004, Digium, Inc.
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 */
27 #include <linux/version.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/errno.h>
31 #include <linux/module.h>
32 #include <linux/device.h>
33 #include <linux/init.h>
34 #include <linux/delay.h> /* for udelay */
35 #include <linux/interrupt.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <dahdi/kernel.h>
39 #include "xbus-core.h"
40 #include "xproto.h"
41 #include "xpp_dahdi.h"
42 #include "parport_debug.h"
43
44 static const char rcsid[] = "$Id$";
45
46 #ifdef CONFIG_PROC_FS
47 struct proc_dir_entry *xpp_proc_toplevel = NULL;
48 #define PROC_DIR "xpp"
49 #define PROC_XPD_SUMMARY "summary"
50 #endif
51
52 #define MAX_QUEUE_LEN 10000
53 #define DELAY_UNTIL_DIALTONE 3000
54
55 DEF_PARM(int, debug, 0, 0644, "Print DBG statements");
56 EXPORT_SYMBOL(debug);
57 static DEF_PARM_BOOL(prefmaster, 0, 0644,
58 "Do we want to be dahdi preferred sync master");
59 // DEF_ARRAY(int, pcmtx, 4, 0, "Forced PCM values to transmit");
60
61 #include "dahdi_debug.h"
62
63 static void phonedev_cleanup(xpd_t *xpd);
64
65 #ifdef DEBUG_SYNC_PARPORT
66 /*
67 * Use parallel port to sample our PCM sync and diagnose quality and
68 * potential problems. A logic analizer or a scope should be connected
69 * to the data bits of the parallel port.
70 *
71 * Array parameter: Choose the two xbuses Id's to sample.
72 * This can be changed on runtime as well. Example:
73 * echo "3,5" > /sys/module/xpp/parameters/parport_xbuses
74 */
75 static int parport_xbuses[2] = { 0, 1 };
76
77 unsigned int parport_xbuses_num_values;
78 module_param_array(parport_xbuses, int, &parport_xbuses_num_values, 0577);
79 MODULE_PARM_DESC(parport_xbuses, "Id's of xbuses to sample (1-2)");
80
81 /*
82 * Flip a single bit in the parallel port:
83 * - The bit number is either bitnum0 or bitnum1
84 * - Bit is selected by xbus number from parport_xbuses[]
85 */
86 void xbus_flip_bit(xbus_t *xbus, unsigned int bitnum0, unsigned int bitnum1)
87 {
88 int num = xbus->num;
89
90 if (num == parport_xbuses[0])
91 flip_parport_bit(bitnum0);
92 if (num == parport_xbuses[1])
93 flip_parport_bit(bitnum1);
94 }
95 EXPORT_SYMBOL(xbus_flip_bit);
96 #endif
97
98 static atomic_t num_registered_spans = ATOMIC_INIT(0);
99
100 int total_registered_spans(void)
101 {
102 return atomic_read(&num_registered_spans);
103 }
104
105 #ifdef CONFIG_PROC_FS
106 static const struct file_operations xpd_read_proc_ops;
107 #endif
108
109 /*------------------------- XPD Management -------------------------*/
110
111 /*
112 * Called by put_xpd() when XPD has no more references.
113 */
114 static void xpd_destroy(struct kref *kref)
115 {
116 xpd_t *xpd;
117
118 xpd = kref_to_xpd(kref);
119 XPD_DBG(DEVICES, xpd, "%s\n", __func__);
120 xpd_device_unregister(xpd);
121 }
122
123 int refcount_xpd(xpd_t *xpd)
124 {
125 struct kref *kref = &xpd->kref;
126
127 return refcount_read(&kref->refcount);
128 }
129
130 xpd_t *get_xpd(const char *msg, xpd_t *xpd)
131 {
132 XPD_DBG(DEVICES, xpd, "%s: refcount_xpd=%d\n", msg, refcount_xpd(xpd));
133 kref_get(&xpd->kref);
134 return xpd;
135 }
136 EXPORT_SYMBOL(get_xpd);
137
138 void put_xpd(const char *msg, xpd_t *xpd)
139 {
140 XPD_DBG(DEVICES, xpd, "%s: refcount_xpd=%d\n", msg, refcount_xpd(xpd));
141 kref_put(&xpd->kref, xpd_destroy);
142 }
143 EXPORT_SYMBOL(put_xpd);
144
145 static void xpd_proc_remove(xbus_t *xbus, xpd_t *xpd)
146 {
147 #ifdef CONFIG_PROC_FS
148 if (xpd->proc_xpd_dir) {
149 if (xpd->proc_xpd_summary) {
150 XPD_DBG(PROC, xpd, "Removing proc '%s'\n",
151 PROC_XPD_SUMMARY);
152 remove_proc_entry(PROC_XPD_SUMMARY, xpd->proc_xpd_dir);
153 xpd->proc_xpd_summary = NULL;
154 }
155 XPD_DBG(PROC, xpd, "Removing %s/%s proc directory\n",
156 xbus->busname, xpd->xpdname);
157 remove_proc_entry(xpd->xpdname, xbus->proc_xbus_dir);
158 xpd->proc_xpd_dir = NULL;
159 }
160 #endif
161 }
162
163 static int xpd_proc_create(xbus_t *xbus, xpd_t *xpd)
164 {
165 #ifdef CONFIG_PROC_FS
166 XPD_DBG(PROC, xpd, "Creating proc directory\n");
167 xpd->proc_xpd_dir = proc_mkdir(xpd->xpdname, xbus->proc_xbus_dir);
168 if (!xpd->proc_xpd_dir) {
169 XPD_ERR(xpd, "Failed to create proc directory\n");
170 goto err;
171 }
172 xpd->proc_xpd_summary = proc_create_data(PROC_XPD_SUMMARY, 0444,
173 xpd->proc_xpd_dir,
174 &xpd_read_proc_ops, xpd);
175 if (!xpd->proc_xpd_summary) {
176 XPD_ERR(xpd, "Failed to create proc file '%s'\n",
177 PROC_XPD_SUMMARY);
178 goto err;
179 }
180 SET_PROC_DIRENTRY_OWNER(xpd->proc_xpd_summary);
181 #endif
182 return 0;
183 #ifdef CONFIG_PROC_FS
184 err:
185 xpd_proc_remove(xbus, xpd);
186 return -EFAULT;
187 #endif
188 }
189
190 void xpd_free(xpd_t *xpd)
191 {
192 xbus_t *xbus = NULL;
193
194 if (!xpd)
195 return;
196 if (xpd->xproto)
197 xproto_put(xpd->xproto); /* was taken in xpd_alloc() */
198 xpd->xproto = NULL;
199 xbus = xpd->xbus;
200 if (!xbus)
201 return;
202 XPD_DBG(DEVICES, xpd, "\n");
203 xpd_proc_remove(xbus, xpd);
204 xbus_xpd_unbind(xbus, xpd);
205 phonedev_cleanup(xpd);
206 KZFREE(xpd);
207 DBG(DEVICES, "refcount_xbus=%d\n", refcount_xbus(xbus));
208 /*
209 * This must be last, so the xbus cannot be released before the xpd
210 */
211 put_xbus(__func__, xbus); /* was taken in xpd_alloc() */
212 }
213 EXPORT_SYMBOL(xpd_free);
214
215 /*
216 * Synchronous part of XPD detection.
217 * Called from new_card()
218 */
219 int create_xpd(xbus_t *xbus, const xproto_table_t *proto_table,
220 const struct unit_descriptor *unit_descriptor,
221 int unit,
222 int subunit, __u8 type)
223 {
224 xpd_t *xpd = NULL;
225 bool to_phone;
226
227 BUG_ON(type == XPD_TYPE_NOMODULE);
228 to_phone = BIT(subunit) & unit_descriptor->port_dir;
229 BUG_ON(!xbus);
230 xpd = xpd_byaddr(xbus, unit, subunit);
231 if (xpd) {
232 XPD_NOTICE(xpd, "XPD at %d%d already exists\n", unit, subunit);
233 return 0;
234 }
235 INFO("%s: [%d.%d] type=%d subtype=%d numchips=%d ports_per_chip=%d ports_dir=%d\n",
236 __func__,
237 unit_descriptor->addr.unit,
238 unit_descriptor->addr.subunit,
239 unit_descriptor->type,
240 unit_descriptor->subtype,
241 unit_descriptor->numchips,
242 unit_descriptor->ports_per_chip,
243 unit_descriptor->port_dir);
244 if (unit_descriptor->ports_per_chip <= 0 || unit_descriptor->ports_per_chip > CHANNELS_PERXPD) {
245 XBUS_NOTICE(xbus, "Illegal number of ports %d for XPD %d%d\n",
246 unit_descriptor->ports_per_chip, unit, subunit);
247 return 0;
248 }
249 xpd =
250 proto_table->xops->card_new(xbus, unit, subunit, proto_table,
251 unit_descriptor,
252 to_phone);
253 if (!xpd) {
254 XBUS_NOTICE(xbus, "card_new(%d,%d,%d,%d) failed. Ignored.\n",
255 unit, subunit, proto_table->type,
256 to_phone);
257 return -EINVAL;
258 }
259 return 0;
260 }
261 EXPORT_SYMBOL(create_xpd);
262
263 #ifdef CONFIG_PROC_FS
264
265 /**
266 * Prints a general procfs entry for the bus, under xpp/BUSNAME/summary
267 */
268 static int xpd_read_proc_show(struct seq_file *sfile, void *data)
269 {
270 int len = 0;
271 xpd_t *xpd = sfile->private;
272 int i;
273
274 if (!xpd)
275 return -EINVAL;
276
277 seq_printf(sfile,
278 "%s (%s, card %s, span %d)\n" "timing_priority: %d\n"
279 "timer_count: %d span->mainttimer=%d\n", xpd->xpdname,
280 xpd->type_name, (xpd->card_present) ? "present" : "missing",
281 (SPAN_REGISTERED(xpd)) ? PHONEDEV(xpd).span.spanno : 0,
282 PHONEDEV(xpd).timing_priority, xpd->timer_count,
283 PHONEDEV(xpd).span.mainttimer);
284 seq_printf(sfile, "xpd_state: %s (%d)\n",
285 xpd_statename(xpd->xpd_state), xpd->xpd_state);
286 seq_printf(sfile, "open_counter=%d refcount=%d\n",
287 atomic_read(&PHONEDEV(xpd).open_counter),
288 refcount_xpd(xpd));
289 seq_printf(sfile, "Address: U=%d S=%d\n", xpd->addr.unit,
290 xpd->addr.subunit);
291 seq_printf(sfile, "Subunits: %d\n", xpd->subunits);
292 seq_printf(sfile, "Type: %d.%d\n", xpd->xpd_type, XPD_HW(xpd).subtype);
293 seq_printf(sfile, "Hardware type: %d.%d\nn", XPD_HW(xpd).type, XPD_HW(xpd).subtype);
294 seq_printf(sfile, "pcm_len=%d\n\n", PHONEDEV(xpd).pcm_len);
295 seq_printf(sfile, "wanted_pcm_mask=0x%04X\n\n",
296 PHONEDEV(xpd).wanted_pcm_mask);
297 seq_printf(sfile, "mute_dtmf=0x%04X\n\n",
298 PHONEDEV(xpd).mute_dtmf);
299 seq_printf(sfile, "STATES:");
300 seq_printf(sfile, "\n\t%-17s: ", "output_relays");
301 for_each_line(xpd, i) {
302 seq_printf(sfile, "%d ",
303 IS_SET(PHONEDEV(xpd).digital_outputs, i));
304 }
305 seq_printf(sfile, "\n\t%-17s: ", "input_relays");
306 for_each_line(xpd, i) {
307 seq_printf(sfile, "%d ",
308 IS_SET(PHONEDEV(xpd).digital_inputs, i));
309 }
310 seq_printf(sfile, "\n\t%-17s: ", "offhook");
311 for_each_line(xpd, i) {
312 seq_printf(sfile, "%d ", IS_OFFHOOK(xpd, i));
313 }
314 seq_printf(sfile, "\n\t%-17s: ", "oht_pcm_pass");
315 for_each_line(xpd, i) {
316 seq_printf(sfile, "%d ",
317 IS_SET(PHONEDEV(xpd).oht_pcm_pass, i));
318 }
319 seq_printf(sfile, "\n\t%-17s: ", "msg_waiting");
320 for_each_line(xpd, i) {
321 seq_printf(sfile, "%d ", PHONEDEV(xpd).msg_waiting[i]);
322 }
323 seq_printf(sfile, "\n\t%-17s: ", "ringing");
324 for_each_line(xpd, i) {
325 seq_printf(sfile, "%d ", PHONEDEV(xpd).ringing[i]);
326 }
327 seq_printf(sfile, "\n\t%-17s: ", "no_pcm");
328 for_each_line(xpd, i) {
329 seq_printf(sfile, "%d ", IS_SET(PHONEDEV(xpd).no_pcm, i));
330 }
331 #if 1
332 if (SPAN_REGISTERED(xpd)) {
333 seq_printf(sfile,
334 "\nPCM:\n |"
335 " [readchunk] |"
336 " [writechunk] | W D");
337 for_each_line(xpd, i) {
338 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
339 __u8 rchunk[DAHDI_CHUNKSIZE];
340 __u8 wchunk[DAHDI_CHUNKSIZE];
341 __u8 *rp;
342 __u8 *wp;
343 int j;
344
345 if (IS_SET(PHONEDEV(xpd).digital_outputs, i))
346 continue;
347 if (IS_SET(PHONEDEV(xpd).digital_inputs, i))
348 continue;
349 if (IS_SET(PHONEDEV(xpd).digital_signalling, i))
350 continue;
351 rp = chan->readchunk;
352 wp = chan->writechunk;
353 memcpy(rchunk, rp, DAHDI_CHUNKSIZE);
354 memcpy(wchunk, wp, DAHDI_CHUNKSIZE);
355 seq_printf(sfile, "\n port %2d> | ", i);
356 for (j = 0; j < DAHDI_CHUNKSIZE; j++)
357 seq_printf(sfile, "%02X ", rchunk[j]);
358 seq_printf(sfile, " | ");
359 for (j = 0; j < DAHDI_CHUNKSIZE; j++)
360 seq_printf(sfile, "%02X ", wchunk[j]);
361 seq_printf(sfile, " | %c",
362 (IS_SET(PHONEDEV(xpd).wanted_pcm_mask, i))
363 ? '+' : ' ');
364 seq_printf(sfile, " %c",
365 (IS_SET(PHONEDEV(xpd).mute_dtmf, i))
366 ? '-' : ' ');
367 }
368 }
369 #endif
370 #if 0
371 if (SPAN_REGISTERED(xpd)) {
372 seq_printf(sfile, "\nSignalling:\n");
373 for_each_line(xpd, i) {
374 struct dahdi_chan *chan = XPD_CHAN(xpd, i);
375 seq_printf(sfile,
376 "\t%2d> sigcap=0x%04X sig=0x%04X\n", i,
377 chan->sigcap, chan->sig);
378 }
379 }
380 #endif
381 seq_printf(sfile, "\nCOUNTERS:\n");
382 for (i = 0; i < XPD_COUNTER_MAX; i++) {
383 seq_printf(sfile, "\t\t%-20s = %d\n",
384 xpd_counters[i].name, xpd->counters[i]);
385 }
386 seq_printf(sfile, "<-- len=%d\n", len);
387 return 0;
388 }
389
390 static int xpd_read_proc_open(struct inode *inode, struct file *file)
391 {
392 return single_open(file, xpd_read_proc_show, PDE_DATA(inode));
393 }
394
395 static const struct file_operations xpd_read_proc_ops = {
396 .owner = THIS_MODULE,
397 .open = xpd_read_proc_open,
398 .read = seq_read,
399 .llseek = seq_lseek,
400 .release = single_release,
401 };
402
403 #endif
404
405 const char *xpd_statename(enum xpd_state st)
406 {
407 switch (st) {
408 case XPD_STATE_START:
409 return "START";
410 case XPD_STATE_INIT_REGS:
411 return "INIT_REGS";
412 case XPD_STATE_READY:
413 return "READY";
414 case XPD_STATE_NOHW:
415 return "NOHW";
416 }
417 return NULL;
418 }
419
420 bool xpd_setstate(xpd_t *xpd, enum xpd_state newstate)
421 {
422 BUG_ON(!xpd);
423 XPD_DBG(DEVICES, xpd, "%s: %s (%d) -> %s (%d)\n", __func__,
424 xpd_statename(xpd->xpd_state), xpd->xpd_state,
425 xpd_statename(newstate), newstate);
426 switch (newstate) {
427 case XPD_STATE_START:
428 goto badstate;
429 case XPD_STATE_INIT_REGS:
430 if (xpd->xpd_state != XPD_STATE_START)
431 goto badstate;
432 if (xpd->addr.subunit != 0) {
433 XPD_NOTICE(xpd,
434 "%s: Moving to %s allowed only for subunit 0\n",
435 __func__, xpd_statename(newstate));
436 goto badstate;
437 }
438 break;
439 case XPD_STATE_READY:
440 if (xpd->addr.subunit == 0) {
441 /* Unit 0 script initialize registers of all subunits */
442 if (xpd->xpd_state != XPD_STATE_INIT_REGS)
443 goto badstate;
444 } else {
445 if (xpd->xpd_state != XPD_STATE_START)
446 goto badstate;
447 }
448 break;
449 case XPD_STATE_NOHW:
450 break;
451 default:
452 XPD_ERR(xpd, "%s: Unknown newstate=%d\n", __func__, newstate);
453 }
454 xpd->xpd_state = newstate;
455 return 1;
456 badstate:
457 XPD_NOTICE(xpd, "%s: cannot transition: %s (%d) -> %s (%d)\n", __func__,
458 xpd_statename(xpd->xpd_state), xpd->xpd_state,
459 xpd_statename(newstate), newstate);
460 return 0;
461 }
462
463 /*
464 * Cleanup/initialize phonedev
465 */
466 static void phonedev_cleanup(xpd_t *xpd)
467 {
468 struct phonedev *phonedev = &PHONEDEV(xpd);
469 unsigned int x;
470
471 for (x = 0; x < phonedev->channels; x++) {
472 if (phonedev->chans[x]) {
473 KZFREE(phonedev->chans[x]);
474 phonedev->chans[x] = NULL;
475 }
476 if (phonedev->ec[x]) {
477 KZFREE(phonedev->ec[x]);
478 phonedev->ec[x] = NULL;
479 }
480 }
481 phonedev->channels = 0;
482 }
483
484 int phonedev_alloc_channels(xpd_t *xpd, int channels)
485 {
486 struct phonedev *phonedev = &PHONEDEV(xpd);
487 int old_channels = phonedev->channels;
488 unsigned int x;
489
490 XPD_DBG(DEVICES, xpd, "Reallocating channels: %d -> %d\n",
491 old_channels, channels);
492 phonedev_cleanup(xpd);
493 phonedev->channels = channels;
494 for (x = 0; x < phonedev->channels; x++) {
495 if (!
496 (phonedev->chans[x] =
497 KZALLOC(sizeof(*(phonedev->chans[x])), GFP_KERNEL))) {
498 ERR("%s: Unable to allocate channel %d\n", __func__, x);
499 goto err;
500 }
501 phonedev->ec[x] =
502 KZALLOC(sizeof(*(phonedev->ec[x])), GFP_KERNEL);
503 if (!phonedev->ec[x]) {
504 ERR("%s: Unable to allocate ec state %d\n", __func__,
505 x);
506 goto err;
507 }
508 }
509 return 0;
510 err:
511 phonedev_cleanup(xpd);
512 return -ENOMEM;
513 }
514 EXPORT_SYMBOL(phonedev_alloc_channels);
515
516 __must_check static int phonedev_init(xpd_t *xpd,
517 const xproto_table_t *proto_table,
518 int channels, xpp_line_t no_pcm)
519 {
520 struct phonedev *phonedev = &PHONEDEV(xpd);
521
522 spin_lock_init(&phonedev->lock_recompute_pcm);
523 phonedev->no_pcm = no_pcm;
524 phonedev->offhook_state = 0x0; /* ONHOOK */
525 phonedev->phoneops = proto_table->phoneops;
526 phonedev->digital_outputs = 0;
527 phonedev->digital_inputs = 0;
528 atomic_set(&phonedev->dahdi_registered, 0);
529 atomic_set(&phonedev->open_counter, 0);
530 if (phonedev_alloc_channels(xpd, channels) < 0)
531 goto err;
532 return 0;
533 err:
534 return -ENOMEM;
535 }
536
537
538 /*
539 * xpd_alloc - Allocator for new XPD's
540 *
541 */
542 __must_check xpd_t *xpd_alloc(xbus_t *xbus, int unit, int subunit,
543 size_t privsize,
544 const xproto_table_t *proto_table,
545 const struct unit_descriptor *unit_descriptor,
546 int channels)
547 {
548 xpd_t *xpd = NULL;
549 size_t alloc_size = sizeof(xpd_t) + privsize;
550 int type = proto_table->type;
551 xpp_line_t no_pcm = 0;
552
553 BUG_ON(!proto_table);
554 XBUS_DBG(DEVICES, xbus, "type=%d channels=%d (alloc_size=%zd)\n", type,
555 channels, alloc_size);
556 if (channels > CHANNELS_PERXPD) {
557 XBUS_ERR(xbus, "%s: type=%d: too many channels %d\n", __func__,
558 type, channels);
559 goto err;
560 }
561
562 if ((xpd = KZALLOC(alloc_size, GFP_KERNEL)) == NULL) {
563 XBUS_ERR(xbus, "%s: type=%d: Unable to allocate memory\n",
564 __func__, type);
565 goto err;
566 }
567 xpd->priv = (__u8 *)xpd + sizeof(xpd_t);
568 spin_lock_init(&xpd->lock);
569 xpd->card_present = 0;
570 xpd->xpd_type = proto_table->type;
571 xpd->xproto = proto_table;
572 xpd->unit_descriptor = *unit_descriptor;
573 xpd->xops = proto_table->xops;
574 xpd->xpd_state = XPD_STATE_START;
575 xpd->subunits = subunits_of_xpd(unit_descriptor, proto_table);
576 kref_init(&xpd->kref);
577
578 /* For USB-1 disable some channels */
579 if (MAX_SEND_SIZE(xbus) < RPACKET_SIZE(GLOBAL, PCM_WRITE)) {
580 no_pcm =
581 0x7F | PHONEDEV(xpd).digital_outputs | PHONEDEV(xpd).
582 digital_inputs;
583 XBUS_NOTICE(xbus,
584 "max xframe size = %d, disabling some PCM channels. "
585 "no_pcm=0x%04X\n",
586 MAX_SEND_SIZE(xbus), PHONEDEV(xpd).no_pcm);
587 }
588 if (phonedev_init(xpd, proto_table, channels, no_pcm) < 0)
589 goto err;
590 xbus_xpd_bind(xbus, xpd, unit, subunit);
591 if (xpd_proc_create(xbus, xpd) < 0)
592 goto err;
593 /*
594 * This makes sure the xbus cannot be removed before this xpd
595 * is removed in xpd_free()
596 */
597 xbus = get_xbus(__func__, xbus->num); /* returned in xpd_free() */
598 xproto_get(type); /* will be returned in xpd_free() */
599 return xpd;
600 err:
601 if (xpd) {
602 xpd_proc_remove(xbus, xpd);
603 phonedev_cleanup(xpd);
604 KZFREE(xpd);
605 }
606 return NULL;
607 }
608 EXPORT_SYMBOL(xpd_alloc);
609
610 /*
611 * The xpd isn't open by anyone, we can unregister it and free it
612 */
613 void xpd_remove(xpd_t *xpd)
614 {
615 BUG_ON(!xpd);
616 XPD_INFO(xpd, "Remove\n");
617 CALL_XMETHOD(card_remove, xpd);
618 xpd_free(xpd);
619 }
620
621 void update_xpd_status(xpd_t *xpd, int alarm_flag)
622 {
623 struct dahdi_span *span = &PHONEDEV(xpd).span;
624
625 if (!SPAN_REGISTERED(xpd)) {
626 #if 0
627 XPD_NOTICE(xpd,
628 "%s: XPD is not registered. Skipping.\n",
629 __func__);
630 #endif
631 return;
632 }
633 switch (alarm_flag) {
634 case DAHDI_ALARM_NONE:
635 xpd->last_response = jiffies;
636 break;
637 default:
638 // Nothing
639 break;
640 }
641 if (span->alarms == alarm_flag)
642 return;
643 XPD_DBG(GENERAL, xpd, "Update XPD alarms: %s -> %02X\n",
644 PHONEDEV(xpd).span.name, alarm_flag);
645 span->alarms = alarm_flag;
646 dahdi_alarm_notify(span);
647 }
648 EXPORT_SYMBOL(update_xpd_status);
649
650 /*
651 * Used to block/pass PCM during onhook-transfers. E.g:
652 * - Playing FSK after FXS ONHOOK for MWI (non-neon style)
653 * - Playing DTFM/FSK for FXO Caller-ID detection.
654 */
655 void oht_pcm(xpd_t *xpd, int pos, bool pass)
656 {
657 if (pass) {
658 LINE_DBG(SIGNAL, xpd, pos, "OHT PCM: pass\n");
659 BIT_SET(PHONEDEV(xpd).oht_pcm_pass, pos);
660 } else {
661 LINE_DBG(SIGNAL, xpd, pos, "OHT PCM: block\n");
662 BIT_CLR(PHONEDEV(xpd).oht_pcm_pass, pos);
663 }
664 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
665 }
666 EXPORT_SYMBOL(oht_pcm);
667
668 /*
669 * Update our hookstate -- for PCM block/pass
670 */
671 void mark_offhook(xpd_t *xpd, int pos, bool to_offhook)
672 {
673 if (to_offhook) {
674 LINE_DBG(SIGNAL, xpd, pos, "OFFHOOK\n");
675 BIT_SET(PHONEDEV(xpd).offhook_state, pos);
676 } else {
677 LINE_DBG(SIGNAL, xpd, pos, "ONHOOK\n");
678 BIT_CLR(PHONEDEV(xpd).offhook_state, pos);
679 }
680 CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
681 }
682 EXPORT_SYMBOL(mark_offhook);
683
684 /*
685 * Send a signalling notification to Asterisk
686 */
687 void notify_rxsig(xpd_t *xpd, int pos, enum dahdi_rxsig rxsig)
688 {
689 /*
690 * We should not spinlock before calling dahdi_hooksig() as
691 * it may call back into our xpp_hooksig() and cause
692 * a nested spinlock scenario
693 */
694 LINE_DBG(SIGNAL, xpd, pos, "rxsig=%s\n", rxsig2str(rxsig));
695 if (SPAN_REGISTERED(xpd))
696 dahdi_hooksig(XPD_CHAN(xpd, pos), rxsig);
697 }
698 EXPORT_SYMBOL(notify_rxsig);
699
700 /*
701 * Called when hardware state changed:
702 * - FXS -- the phone was picked up or hanged-up.
703 * - FXO -- we answered the phone or handed-up.
704 */
705 void hookstate_changed(xpd_t *xpd, int pos, bool to_offhook)
706 {
707 BUG_ON(!xpd);
708 mark_offhook(xpd, pos, to_offhook);
709 if (!to_offhook) {
710 oht_pcm(xpd, pos, 0);
711 /*
712 * To prevent latest PCM to stay in buffers
713 * indefinitely, mark this channel for a
714 * single silence transmittion.
715 *
716 * This bit will be cleared on the next tick.
717 */
718 BIT_SET(PHONEDEV(xpd).silence_pcm, pos);
719 }
720 notify_rxsig(xpd, pos,
721 (to_offhook) ? DAHDI_RXSIG_OFFHOOK : DAHDI_RXSIG_ONHOOK);
722 }
723 EXPORT_SYMBOL(hookstate_changed);
724
725 #define XPP_MAX_LEN 512
726
727 /*------------------------- Dahdi Interfaces -----------------------*/
728
729 /*
730 * Called with spinlock held on chan. Must not call back
731 * dahdi functions.
732 */
733 static int _xpp_open(struct dahdi_chan *chan)
734 {
735 xpd_t *xpd;
736 xbus_t *xbus;
737 int pos;
738 int open_counter;
739
740 if (!chan) {
741 NOTICE("open called on a null chan\n");
742 return -EINVAL;
743 }
744 xpd = chan->pvt;
745 if (!xpd) {
746 NOTICE("open called on a chan with no pvt (xpd)\n");
747 BUG();
748 }
749 xbus = xpd->xbus;
750 if (!xbus) {
751 NOTICE("open called on a chan with no xbus\n");
752 BUG();
753 }
754 pos = chan->chanpos - 1;
755 if (!xpd->card_present) {
756 LINE_NOTICE(xpd, pos, "Cannot open -- device not ready\n");
757 return -ENODEV;
758 }
759 open_counter = atomic_inc_return(&PHONEDEV(xpd).open_counter);
760 LINE_DBG(DEVICES, xpd, pos, "%s[%d]: open_counter=%d\n", current->comm,
761 current->pid, open_counter);
762 if (PHONE_METHOD(card_open, xpd))
763 CALL_PHONE_METHOD(card_open, xpd, pos);
764 return 0;
765 }
766
767 int xpp_open(struct dahdi_chan *chan)
768 {
769 unsigned long flags;
770 int res;
771 spin_lock_irqsave(&chan->lock, flags);
772 res = _xpp_open(chan);
773 spin_unlock_irqrestore(&chan->lock, flags);
774 return res;
775 }
776 EXPORT_SYMBOL(xpp_open);
777
778 int xpp_close(struct dahdi_chan *chan)
779 {
780 xpd_t *xpd = chan->pvt;
781 int pos = chan->chanpos - 1;
782 int open_counter;
783
784 if (PHONE_METHOD(card_close, xpd))
785 CALL_PHONE_METHOD(card_close, xpd, pos);
786 /* from xpp_open(): */
787 open_counter = atomic_dec_return(&PHONEDEV(xpd).open_counter);
788 LINE_DBG(DEVICES, xpd, pos, "%s[%d]: open_counter=%d\n", current->comm,
789 current->pid, open_counter);
790 return 0;
791 }
792 EXPORT_SYMBOL(xpp_close);
793
794 void report_bad_ioctl(const char *msg, xpd_t *xpd, int pos, unsigned int cmd)
795 {
796 char *extra_msg = "";
797
798 if (_IOC_TYPE(cmd) == 'J')
799 extra_msg = " (for old ZAPTEL)";
800 XPD_NOTICE(xpd, "%s: Bad ioctl%s\n", msg, extra_msg);
801 XPD_NOTICE(xpd, "ENOTTY: chan=%d cmd=0x%x\n", pos, cmd);
802 XPD_NOTICE(xpd, " IOC_TYPE=0x%02X\n", _IOC_TYPE(cmd));
803 XPD_NOTICE(xpd, " IOC_DIR=0x%02X\n", _IOC_DIR(cmd));
804 XPD_NOTICE(xpd, " IOC_NR=%d\n", _IOC_NR(cmd));
805 XPD_NOTICE(xpd, " IOC_SIZE=0x%02X\n", _IOC_SIZE(cmd));
806 }
807 EXPORT_SYMBOL(report_bad_ioctl);
808
809 int xpp_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long arg)
810 {
811 xpd_t *xpd = chan->pvt;
812 int pos = chan->chanpos - 1;
813
814 if (!xpd) {
815 ERR("%s: channel in pos %d, was already closed. Ignore.\n",
816 __func__, pos);
817 return -ENODEV;
818 }
819 switch (cmd) {
820 default:
821 /* Some span-specific commands before we give up: */
822 if (PHONE_METHOD(card_ioctl, xpd)) {
823 return CALL_PHONE_METHOD(card_ioctl, xpd, pos, cmd,
824 arg);
825 }
826 report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
827 return -ENOTTY;
828 }
829 return 0;
830 }
831 EXPORT_SYMBOL(xpp_ioctl);
832
833 int xpp_hooksig(struct dahdi_chan *chan, enum dahdi_txsig txsig)
834 {
835 xpd_t *xpd = chan->pvt;
836 xbus_t *xbus;
837 int pos = chan->chanpos - 1;
838
839 if (!xpd) {
840 ERR("%s: channel in pos %d, was already closed. Ignore.\n",
841 __func__, pos);
842 return -ENODEV;
843 }
844 if (!PHONE_METHOD(card_hooksig, xpd)) {
845 LINE_ERR(xpd, pos,
846 "%s: No hooksig method for this channel. Ignore.\n",
847 __func__);
848 return -ENODEV;
849 }
850 xbus = xpd->xbus;
851 BUG_ON(!xbus);
852 DBG(SIGNAL, "Setting %s to %s (%d)\n", chan->name, txsig2str(txsig),
853 txsig);
854 return CALL_PHONE_METHOD(card_hooksig, xpd, pos, txsig);
855 }
856 EXPORT_SYMBOL(xpp_hooksig);
857
858 /* Req: Set the requested chunk size. This is the unit in which you must
859 report results for conferencing, etc */
860 int xpp_setchunksize(struct dahdi_span *span, int chunksize);
861
862 /* Enable maintenance modes */
863 int xpp_maint(struct dahdi_span *span, int cmd)
864 {
865 struct phonedev *phonedev = container_of(span, struct phonedev, span);
866 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
867 int ret = 0;
868 #if 0
869 char loopback_data[] = "THE-QUICK-BROWN-FOX-JUMPED-OVER-THE-LAZY-DOG";
870 #endif
871
872 DBG(GENERAL, "span->mainttimer=%d\n", span->mainttimer);
873 switch (cmd) {
874 case DAHDI_MAINT_NONE:
875 INFO("XXX Turn off local and remote loops XXX\n");
876 break;
877 case DAHDI_MAINT_LOCALLOOP:
878 INFO("XXX Turn on local loopback XXX\n");
879 break;
880 case DAHDI_MAINT_REMOTELOOP:
881 INFO("XXX Turn on remote loopback XXX\n");
882 break;
883 case DAHDI_MAINT_LOOPUP:
884 INFO("XXX Send loopup code XXX\n");
885 break;
886 case DAHDI_MAINT_LOOPDOWN:
887 INFO("XXX Send loopdown code XXX\n");
888 break;
889 default:
890 ERR("XPP: Unknown maint command: %d\n", cmd);
891 ret = -EINVAL;
892 break;
893 }
894 if (span->mainttimer || span->maintstat)
895 update_xpd_status(xpd, DAHDI_ALARM_LOOPBACK);
896 return ret;
897 }
898 EXPORT_SYMBOL(xpp_maint);
899
900 #ifdef CONFIG_DAHDI_WATCHDOG
901 /*
902 * If the watchdog detects no received data, it will call the
903 * watchdog routine
904 */
905 int xpp_watchdog(struct dahdi_span *span, int cause)
906 {
907 static int rate_limit;
908
909 if ((rate_limit++ % 1000) == 0)
910 DBG(GENERAL, "\n");
911 return 0;
912 }
913 EXPORT_SYMBOL(xpp_watchdog);
914 #endif
915
916 /*
917 * Hardware Echo Canceller management
918 */
919 static void echocan_free(struct dahdi_chan *chan,
920 struct dahdi_echocan_state *ec)
921 {
922 xpd_t *xpd;
923 xbus_t *xbus;
924 int pos = chan->chanpos - 1;
925 const struct echoops *echoops;
926
927 xpd = chan->pvt;
928 xbus = xpd->xbus;
929 echoops = ECHOOPS(xbus);
930 if (!echoops)
931 return;
932 LINE_DBG(GENERAL, xpd, pos, "mode=0x%X\n", ec->status.mode);
933 CALL_EC_METHOD(ec_set, xbus, xpd, pos, 0);
934 CALL_EC_METHOD(ec_update, xbus, xbus);
935 put_xpd(__func__, xpd); /* aquired in xpp_echocan_create() */
936 }
937
938 static const struct dahdi_echocan_features xpp_ec_features = {
939 };
940
941 static const struct dahdi_echocan_ops xpp_ec_ops = {
942 .echocan_free = echocan_free,
943 };
944
945 const char *xpp_echocan_name(const struct dahdi_chan *chan)
946 {
947 xpd_t *xpd;
948 xbus_t *xbus;
949 int pos;
950
951 if (!chan) {
952 NOTICE("%s(NULL)\n", __func__);
953 return "XPP";
954 }
955 xpd = chan->pvt;
956 xbus = xpd->xbus;
957 pos = chan->chanpos - 1;
958 LINE_DBG(GENERAL, xpd, pos, "\n");
959 if (!ECHOOPS(xbus))
960 return NULL;
961 /*
962 * quirks and limitations
963 */
964 if (xbus->quirks.has_fxo) {
965 if (xbus->quirks.has_digital_span && xpd->xpd_type == XPD_TYPE_FXO) {
966 LINE_NOTICE(xpd, pos,
967 "quirk: give up HWEC on FXO: "
968 "AB has digital span\n");
969 return NULL;
970 } else if (xbus->sync_mode != SYNC_MODE_AB
971 && xpd->xpd_type == XPD_TYPE_FXS) {
972 LINE_NOTICE(xpd, pos,
973 "quirk: give up HWEC on FXS: "
974 "AB has FXO and is sync slave\n");
975 return NULL;
976 }
977 }
978 return "XPP";
979 }
980 EXPORT_SYMBOL(xpp_echocan_name);
981
982 int xpp_echocan_create(struct dahdi_chan *chan,
983 struct dahdi_echocanparams *ecp,
984 struct dahdi_echocanparam *p,
985 struct dahdi_echocan_state **ec)
986 {
987 xpd_t *xpd;
988 xbus_t *xbus;
989 int pos;
990 struct phonedev *phonedev;
991 const struct echoops *echoops;
992 int ret;
993
994 xpd = chan->pvt;
995 xbus = xpd->xbus;
996 pos = chan->chanpos - 1;
997 echoops = ECHOOPS(xbus);
998 if (!echoops)
999 return -ENODEV;
1000 phonedev = &PHONEDEV(xpd);
1001 *ec = phonedev->ec[pos];
1002 (*ec)->ops = &xpp_ec_ops;
1003 (*ec)->features = xpp_ec_features;
1004 xpd = get_xpd(__func__, xpd); /* Returned in echocan_free() */
1005 LINE_DBG(GENERAL, xpd, pos, "(tap=%d, param_count=%d)\n",
1006 ecp->tap_length, ecp->param_count);
1007 ret = CALL_EC_METHOD(ec_set, xbus, xpd, pos, 1);
1008 CALL_EC_METHOD(ec_update, xbus, xbus);
1009 return ret;
1010 }
1011 EXPORT_SYMBOL(xpp_echocan_create);
1012
1013 void xpp_span_assigned(struct dahdi_span *span)
1014 {
1015 struct phonedev *phonedev = container_of(span, struct phonedev, span);
1016 xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
1017
1018 XPD_INFO(xpd, "Span assigned: %d\n", span->spanno);
1019 if (xpd->card_present) {
1020 span->alarms &= ~DAHDI_ALARM_NOTOPEN;
1021 dahdi_alarm_notify(&phonedev->span);
1022 }
1023 if (PHONE_METHOD(span_assigned, xpd))
1024 CALL_PHONE_METHOD(span_assigned, xpd);
1025 }
1026 EXPORT_SYMBOL(xpp_span_assigned);
1027
1028 static const struct dahdi_span_ops xpp_span_ops = {
1029 .owner = THIS_MODULE,
1030 .open = xpp_open,
1031 .close = xpp_close,
1032 .ioctl = xpp_ioctl,
1033 .maint = xpp_maint,
1034 .echocan_create = xpp_echocan_create,
1035 .echocan_name = xpp_echocan_name,
1036 .assigned = xpp_span_assigned,
1037 };
1038
1039 static const struct dahdi_span_ops xpp_rbs_span_ops = {
1040 .owner = THIS_MODULE,
1041 .hooksig = xpp_hooksig,
1042 .open = xpp_open,
1043 .close = xpp_close,
1044 .ioctl = xpp_ioctl,
1045 .maint = xpp_maint,
1046 .echocan_create = xpp_echocan_create,
1047 .echocan_name = xpp_echocan_name,
1048 .assigned = xpp_span_assigned,
1049 };
1050
1051 void xpd_set_spanname(xpd_t *xpd)
1052 {
1053 struct dahdi_span *span = &PHONEDEV(xpd).span;
1054
1055 snprintf(span->name, MAX_SPANNAME, "%s/%s", xpd->xbus->busname,
1056 xpd->xpdname);
1057 /*
1058 * The "Xorcom XPD" is a prefix in one of the regexes we
1059 * use in our dahdi_genconf to match for PRI cards.
1060 * FIXME: After moving completely to sysfs, we can remove
1061 * this horseshit.
1062 */
1063 snprintf(span->desc, MAX_SPANDESC, "Xorcom XPD [%s].%d: %s",
1064 xpd->xbus->label, span->offset + 1, xpd->type_name);
1065 }
1066 EXPORT_SYMBOL(xpd_set_spanname);
1067
1068 static void xpd_init_span(xpd_t *xpd, unsigned offset, int cn)
1069 {
1070 struct dahdi_span *span;
1071
1072 memset(&PHONEDEV(xpd).span, 0, sizeof(struct dahdi_span));
1073 phonedev_alloc_channels(xpd, cn);
1074 span = &PHONEDEV(xpd).span;
1075 span->deflaw = DAHDI_LAW_MULAW; /* card_* drivers may override */
1076 span->channels = cn;
1077 span->chans = PHONEDEV(xpd).chans;
1078
1079 span->flags = DAHDI_FLAG_RBS;
1080 span->offset = offset;
1081 if (PHONEDEV(xpd).phoneops->card_hooksig)
1082 span->ops = &xpp_rbs_span_ops; /* Only with RBS bits */
1083 else
1084 span->ops = &xpp_span_ops;
1085 xpd_set_spanname(xpd);
1086 list_add_tail(&span->device_node, &xpd->xbus->ddev->spans);
1087 }
1088
1089 int xpd_dahdi_preregister(xpd_t *xpd, unsigned offset)
1090 {
1091 xbus_t *xbus;
1092 int cn;
1093 struct phonedev *phonedev;
1094
1095 BUG_ON(!xpd);
1096
1097 xbus = xpd->xbus;
1098
1099 if (!IS_PHONEDEV(xpd)) {
1100 XPD_ERR(xpd, "Not a telephony device\n");
1101 return -EBADF;
1102 }
1103
1104 phonedev = &PHONEDEV(xpd);
1105
1106 if (SPAN_REGISTERED(xpd)) {
1107 XPD_ERR(xpd, "Already registered\n");
1108 return -EEXIST;
1109 }
1110
1111 cn = PHONEDEV(xpd).channels;
1112 xpd_init_span(xpd, offset, cn);
1113 XPD_DBG(DEVICES, xpd, "Preregister local span %d: %d channels.\n",
1114 offset + 1, cn);
1115 CALL_PHONE_METHOD(card_dahdi_preregistration, xpd, 1);
1116 return 0;
1117 }
1118
1119 int xpd_dahdi_postregister(xpd_t *xpd)
1120 {
1121 int cn;
1122
1123 atomic_inc(&num_registered_spans);
1124 atomic_inc(&PHONEDEV(xpd).dahdi_registered);
1125 CALL_PHONE_METHOD(card_dahdi_postregistration, xpd, 1);
1126 /*
1127 * Update dahdi about our state:
1128 * - Since asterisk didn't open the channel yet,
1129 * the report is discarded anyway.
1130 * - Our FXS driver have another notification mechanism that
1131 * is triggered (indirectly) by the open() of the channe.
1132 * - The real fix should be in Asterisk (to get the correct state
1133 * after open).
1134 */
1135 for_each_line(xpd, cn) {
1136 if (IS_OFFHOOK(xpd, cn))
1137 notify_rxsig(xpd, cn, DAHDI_RXSIG_OFFHOOK);
1138 }
1139 return 0;
1140 }
1141
1142 /*
1143 * Try our best to make asterisk close all channels related to
1144 * this Astribank:
1145 * - Set span state to DAHDI_ALARM_NOTOPEN in all relevant spans.
1146 * - Notify dahdi afterwards about spans
1147 * (so it can see all changes at once).
1148 * - Also send DAHDI_EVENT_REMOVED on all channels.
1149 */
1150 void xpd_dahdi_preunregister(xpd_t *xpd)
1151 {
1152 if (!xpd || !IS_PHONEDEV(xpd))
1153 return;
1154 XPD_DBG(DEVICES, xpd, "\n");
1155 update_xpd_status(xpd, DAHDI_ALARM_NOTOPEN);
1156 if (xpd->card_present)
1157 CALL_PHONE_METHOD(card_dahdi_preregistration, xpd, 0);
1158 /* Now notify dahdi */
1159 if (SPAN_REGISTERED(xpd)) {
1160 int j;
1161
1162 dahdi_alarm_notify(&PHONEDEV(xpd).span);
1163 XPD_DBG(DEVICES, xpd,
1164 "Queuing DAHDI_EVENT_REMOVED on all channels "
1165 "to ask user to release them\n");
1166 for (j = 0; j < PHONEDEV(xpd).span.channels; j++) {
1167 dahdi_qevent_lock(XPD_CHAN(xpd, j),
1168 DAHDI_EVENT_REMOVED);
1169 }
1170 }
1171 }
1172
1173 void xpd_dahdi_postunregister(xpd_t *xpd)
1174 {
1175 if (!xpd || !IS_PHONEDEV(xpd))
1176 return;
1177 atomic_dec(&PHONEDEV(xpd).dahdi_registered);
1178 atomic_dec(&num_registered_spans);
1179 if (xpd->card_present)
1180 CALL_PHONE_METHOD(card_dahdi_postregistration, xpd, 0);
1181 }
1182
1183 /*------------------------- Initialization -------------------------*/
1184
1185 static void do_cleanup(void)
1186 {
1187 #ifdef CONFIG_PROC_FS
1188 if (xpp_proc_toplevel) {
1189 DBG(GENERAL, "Removing '%s' from proc\n", PROC_DIR);
1190 remove_proc_entry(PROC_DIR, NULL);
1191 xpp_proc_toplevel = NULL;
1192 }
1193 #endif
1194 }
1195
1196 static int __init xpp_dahdi_init(void)
1197 {
1198 int ret = 0;
1199 void *top = NULL;
1200
1201 INFO("MAX_XPDS=%d (%d*%d)\n", MAX_XPDS, MAX_UNIT, MAX_SUBUNIT);
1202 #ifdef CONFIG_PROC_FS
1203 xpp_proc_toplevel = proc_mkdir(PROC_DIR, NULL);
1204 if (!xpp_proc_toplevel) {
1205 ret = -EIO;
1206 goto err;
1207 }
1208 top = xpp_proc_toplevel;
1209 #endif
1210 ret = xbus_core_init();
1211 if (ret) {
1212 ERR("xbus_core_init failed (%d)\n", ret);
1213 goto err;
1214 }
1215 ret = xbus_pcm_init(top);
1216 if (ret) {
1217 ERR("xbus_pcm_init failed (%d)\n", ret);
1218 xbus_core_shutdown();
1219 goto err;
1220 }
1221 return 0;
1222 err:
1223 do_cleanup();
1224 return ret;
1225 }
1226
1227 static void __exit xpp_dahdi_cleanup(void)
1228 {
1229 xbus_pcm_shutdown();
1230 xbus_core_shutdown();
1231 do_cleanup();
1232 }
1233
1234 MODULE_DESCRIPTION("XPP Dahdi Driver");
1235 MODULE_AUTHOR("Oron Peled <[email protected]>");
1236 MODULE_LICENSE("GPL");
1237
1238 module_init(xpp_dahdi_init);
1239 module_exit(xpp_dahdi_cleanup);