Debian Codegraph @

drivers/dahdi/xpp/xbus-core.c

raw
1 /*
2 * Written by Oron Peled <[email protected]>
3 * Copyright (C) 2004-2006, Xorcom
4 *
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22 #include <linux/version.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/sched.h>
27 #include <linux/mutex.h>
28 #include <linux/proc_fs.h>
29 #include <linux/seq_file.h>
30 #include <linux/slab.h>
31 #ifdef PROTOCOL_DEBUG
32 #include <linux/ctype.h>
33 #endif
34 #include <linux/workqueue.h>
35 #include <linux/device.h>
36 #include <linux/delay.h> /* for msleep() to debug */
37 #include "xpd.h"
38 #include "xpp_dahdi.h"
39 #include "xbus-core.h"
40 #include "card_global.h"
41 #include "dahdi_debug.h"
42
43 static const char rcsid[] = "$Id$";
44
45 /* Defines */
46 #define INITIALIZATION_TIMEOUT (90*HZ) /* in jiffies */
47 #define PROC_XBUSES "xbuses"
48 #define PROC_XBUS_SUMMARY "summary"
49
50 #ifdef PROTOCOL_DEBUG
51 #ifdef CONFIG_PROC_FS
52 #define PROC_XBUS_COMMAND "command"
53 static const struct file_operations proc_xbus_command_ops;
54 #endif
55 #endif
56
57 /* Command line parameters */
58 extern int debug;
59 static DEF_PARM(uint, command_queue_length, 1500, 0444,
60 "Maximal command queue length");
61 static DEF_PARM(uint, poll_timeout, 1000, 0644,
62 "Timeout (in jiffies) waiting for units to reply");
63 static DEF_PARM_BOOL(rx_tasklet, 0, 0644, "Use receive tasklets");
64 static DEF_PARM_BOOL(dahdi_autoreg, 0, 0444,
65 "Register devices automatically (1) or not (0). UNUSED.");
66
67 #ifdef CONFIG_PROC_FS
68 static const struct file_operations xbus_read_proc_ops;
69 #endif
70 static void transport_init(xbus_t *xbus, struct xbus_ops *ops,
71 ushort max_send_size,
72 struct device *transport_device, void *priv);
73 static void transport_destroy(xbus_t *xbus);
74
75 /* Data structures */
76 static DEFINE_SPINLOCK(xbuses_lock);
77 #ifdef CONFIG_PROC_FS
78 static struct proc_dir_entry *proc_xbuses;
79 #endif
80
81 static struct xbus_desc {
82 xbus_t *xbus;
83 int shutting_down;
84 } xbuses_array[MAX_BUSES];
85
86 static int xbus_is_shutting_down(int num)
87 {
88 return xbuses_array[num].shutting_down;
89 }
90
91 static xbus_t *xbus_byhwid(const char *hwid)
92 {
93 int i;
94 xbus_t *xbus;
95
96 for (i = 0; i < ARRAY_SIZE(xbuses_array); i++) {
97 xbus = xbuses_array[i].xbus;
98 if (xbus && strcmp(hwid, xbus->label) == 0)
99 return xbus;
100 }
101 return NULL;
102 }
103
104 int xbus_check_unique(xbus_t *xbus)
105 {
106 if (!xbus)
107 return -ENOENT;
108 if (xbus->label && *(xbus->label)) {
109 xbus_t *xbus_old;
110
111 XBUS_DBG(DEVICES, xbus, "Checking LABEL='%s'\n", xbus->label);
112 xbus_old = xbus_byhwid(xbus->label);
113 if (xbus_old && xbus_old != xbus) {
114 XBUS_NOTICE(xbus_old,
115 "Duplicate LABEL='%s'. Leave %s unused. "
116 "refcount_xbus=%d\n",
117 xbus_old->label, xbus->busname,
118 refcount_xbus(xbus_old));
119 return -EBUSY;
120 }
121 } else {
122 XBUS_NOTICE(xbus, "Missing board label (old Astribank?)\n");
123 }
124 return 0;
125 }
126
127 const char *xbus_statename(enum xbus_state st)
128 {
129 switch (st) {
130 case XBUS_STATE_START:
131 return "START";
132 case XBUS_STATE_IDLE:
133 return "IDLE";
134 case XBUS_STATE_SENT_REQUEST:
135 return "SENT_REQUEST";
136 case XBUS_STATE_RECVD_DESC:
137 return "RECVD_DESC";
138 case XBUS_STATE_READY:
139 return "READY";
140 case XBUS_STATE_DEACTIVATING:
141 return "DEACTIVATING";
142 case XBUS_STATE_DEACTIVATED:
143 return "DEACTIVATED";
144 case XBUS_STATE_FAIL:
145 return "FAIL";
146 }
147 return NULL;
148 }
149 EXPORT_SYMBOL(xbus_statename);
150
151 static void init_xbus(uint num, xbus_t *xbus)
152 {
153 struct xbus_desc *desc;
154
155 BUG_ON(num >= ARRAY_SIZE(xbuses_array));
156 desc = &xbuses_array[num];
157 desc->xbus = xbus;
158 if (xbus)
159 desc->shutting_down = 0;
160 }
161
162 xbus_t *xbus_num(uint num)
163 {
164 struct xbus_desc *desc;
165
166 if (num >= ARRAY_SIZE(xbuses_array))
167 return NULL;
168 desc = &xbuses_array[num];
169 return desc->xbus;
170 }
171 EXPORT_SYMBOL(xbus_num);
172
173 static void initialize_xbuses_array(void)
174 {
175 int i;
176
177 for (i = 0; i < ARRAY_SIZE(xbuses_array); i++)
178 init_xbus(i, NULL);
179 }
180
181 static void finalize_xbuses_array(void)
182 {
183 int i;
184
185 for (i = 0; i < ARRAY_SIZE(xbuses_array); i++) {
186 if (xbuses_array[i].xbus != NULL) {
187 ERR("%s: xbus #%d is not NULL\n", __func__, i);
188 BUG();
189 }
190 }
191 }
192
193 /*
194 * Called by put_xbus() when XBUS has no more references.
195 */
196 static void xbus_destroy(struct kref *kref)
197 {
198 xbus_t *xbus;
199 int num;
200
201 xbus = kref_to_xbus(kref);
202 XBUS_DBG(DEVICES, xbus, "%s\n", __func__);
203 num = xbus->num;
204 xbuses_array[num].shutting_down = 1;
205 xbus_sysfs_remove(xbus);
206 }
207
208 xbus_t *get_xbus(const char *msg, uint num)
209 {
210 unsigned long flags;
211 xbus_t *xbus;
212
213 if (xbus_is_shutting_down(num)) {
214 DBG(DEVICES, "%s(%s): XBUS-%d: shutting down\n", __func__,
215 msg, num);
216 return NULL;
217 }
218 spin_lock_irqsave(&xbuses_lock, flags);
219 xbus = xbus_num(num);
220 if (xbus != NULL) {
221 kref_get(&xbus->kref);
222 XBUS_DBG(DEVICES, xbus, "%s: refcount_xbus=%d\n", msg,
223 refcount_xbus(xbus));
224 }
225 spin_unlock_irqrestore(&xbuses_lock, flags);
226 return xbus;
227 }
228
229 void put_xbus(const char *msg, xbus_t *xbus)
230 {
231 if (xbus_is_shutting_down(xbus->num)) {
232 if (!refcount_xbus(xbus)) {
233 DBG(DEVICES, "%s(%s): XBUS-%d: shutting down\n",
234 __func__, msg, xbus->num);
235 return;
236 }
237 }
238 XBUS_DBG(DEVICES, xbus, "%s: refcount_xbus=%d\n", msg,
239 refcount_xbus(xbus));
240 kref_put(&xbus->kref, xbus_destroy);
241 }
242
243 int refcount_xbus(xbus_t *xbus)
244 {
245 struct kref *kref = &xbus->kref;
246
247 return refcount_read(&kref->refcount);
248 }
249
250 /*------------------------- Frame Handling ------------------------*/
251
252 void xframe_init(xbus_t *xbus, xframe_t *xframe, void *buf, size_t maxsize,
253 void *priv)
254 {
255 memset(xframe, 0, sizeof(*xframe));
256 INIT_LIST_HEAD(&xframe->frame_list);
257 xframe->priv = priv;
258 xframe->xbus = xbus;
259 xframe->packets = xframe->first_free = buf;
260 xframe->frame_maxlen = maxsize;
261 atomic_set(&xframe->frame_len, 0);
262 do_gettimeofday(&xframe->tv_created);
263 xframe->xframe_magic = XFRAME_MAGIC;
264 }
265 EXPORT_SYMBOL(xframe_init);
266
267 /*
268 * Return pointer to next packet slot in the frame
269 * or NULL if the frame is full.
270 *
271 * FIXME: we do not use atomic_add_return() because kernel-2.6.8
272 * does not have it. This make this code a little racy,
273 * but we currently call xframe_next_packet() only in the
274 * PCM loop (xbus_tick() etc.)
275 */
276 xpacket_t *xframe_next_packet(xframe_t *frm, int len)
277 {
278 int newlen = XFRAME_LEN(frm);
279
280 newlen += len;
281 #if 0
282 DBG(GENERAL, "len=%d, newlen=%d, frm->frame_len=%d\n",
283 len, newlen, XFRAME_LEN(frm));
284 #endif
285 if (newlen > XFRAME_DATASIZE)
286 return NULL;
287 atomic_add(len, &frm->frame_len);
288 return (xpacket_t *)(frm->packets + newlen - len);
289 }
290 EXPORT_SYMBOL(xframe_next_packet);
291
292 static DEFINE_SPINLOCK(serialize_dump_xframe);
293
294 static void do_hexdump(const char msg[], __u8 *data, uint16_t len)
295 {
296 int i;
297 int debug = DBG_ANY; /* mask global debug */
298
299 for (i = 0; i < len; i++)
300 DBG(ANY, "%s: %3d> %02X\n", msg, i, data[i]);
301 }
302
303 void dump_xframe(const char msg[], const xbus_t *xbus, const xframe_t *xframe,
304 int debug)
305 {
306 const uint16_t frm_len = XFRAME_LEN(xframe);
307 xpacket_t *pack;
308 uint16_t pos = 0;
309 uint16_t nextpos;
310 int num = 1;
311 bool do_print;
312 unsigned long flags;
313
314 if (xframe->xframe_magic != XFRAME_MAGIC) {
315 XBUS_ERR(xbus, "%s: bad xframe_magic %lX\n", __func__,
316 xframe->xframe_magic);
317 return;
318 }
319 spin_lock_irqsave(&serialize_dump_xframe, flags);
320 do {
321 if (pos >= xbus->transport.max_send_size) {
322 if (printk_ratelimit()) {
323 XBUS_NOTICE(xbus,
324 "%s: xframe overflow (%d bytes)\n",
325 msg, frm_len);
326 do_hexdump(msg, xframe->packets, frm_len);
327 }
328 break;
329 }
330 if (pos > frm_len) {
331 if (printk_ratelimit()) {
332 XBUS_NOTICE(xbus,
333 "%s: packet overflow pos=%d "
334 "frame_len=%d\n",
335 msg, pos, frm_len);
336 do_hexdump(msg, xframe->packets, frm_len);
337 }
338 break;
339 }
340 pack = (xpacket_t *)&xframe->packets[pos];
341 if (XPACKET_LEN(pack) <= 0) {
342 if (printk_ratelimit()) {
343 XBUS_NOTICE(xbus,
344 "%s: xframe -- bad packet_len=%d "
345 "pos=%d frame_len=%d\n",
346 msg, XPACKET_LEN(pack), pos, frm_len);
347 do_hexdump(msg, xframe->packets, frm_len);
348 }
349 break;
350 }
351 nextpos = pos + XPACKET_LEN(pack);
352 if (nextpos > frm_len) {
353 if (printk_ratelimit()) {
354 XBUS_NOTICE(xbus,
355 "%s: packet overflow nextpos=%d "
356 "frame_len=%d\n",
357 msg, nextpos, frm_len);
358 do_hexdump(msg, xframe->packets, frm_len);
359 }
360 break;
361 }
362 do_print = 0;
363 if (debug == DBG_ANY)
364 do_print = 1;
365 else if (XPACKET_OP(pack) != XPROTO_NAME(GLOBAL, PCM_READ)
366 && XPACKET_OP(pack) != XPROTO_NAME(GLOBAL, PCM_WRITE))
367 do_print = 1;
368 else if (debug & DBG_PCM) {
369 static int rate_limit;
370
371 if ((rate_limit++ % 1003) == 0)
372 do_print = 1;
373 }
374 if (do_print) {
375 if (num == 1) {
376 XBUS_DBG(ANY, xbus, "%s: frame_len=%d. %s\n",
377 msg, frm_len, (XPACKET_IS_PCM(pack))
378 ? "(IS_PCM)" : "");
379 }
380 XBUS_DBG(ANY, xbus,
381 " %3d. DATALEN=%d pcm=%d slot=%d OP=0x%02X "
382 "XPD-%d%d (pos=%d)\n",
383 num, XPACKET_LEN(pack), XPACKET_IS_PCM(pack),
384 XPACKET_PCMSLOT(pack), XPACKET_OP(pack),
385 XPACKET_ADDR_UNIT(pack),
386 XPACKET_ADDR_SUBUNIT(pack), pos);
387 dump_packet(" ", pack, debug);
388 }
389 num++;
390 pos = nextpos;
391 if (pos >= frm_len)
392 break;
393 } while (1);
394 spin_unlock_irqrestore(&serialize_dump_xframe, flags);
395 }
396 EXPORT_SYMBOL(dump_xframe);
397
398 /**
399 *
400 * Frame is freed:
401 * - In case of error, by this function.
402 * - Otherwise, by the underlying sending mechanism
403 */
404 int send_pcm_frame(xbus_t *xbus, xframe_t *xframe)
405 {
406 struct xbus_ops *ops;
407 int ret = -ENODEV;
408
409 BUG_ON(!xframe);
410 if (!XBUS_IS(xbus, READY)) {
411 XBUS_ERR(xbus,
412 "Dropped a pcm frame -- hardware is not ready.\n");
413 ret = -ENODEV;
414 goto error;
415 }
416 ops = transportops_get(xbus);
417 BUG_ON(!ops);
418 ret = ops->xframe_send_pcm(xbus, xframe);
419 transportops_put(xbus);
420 if (ret)
421 XBUS_COUNTER(xbus, TX_BYTES) += XFRAME_LEN(xframe);
422 return ret;
423
424 error:
425 FREE_SEND_XFRAME(xbus, xframe);
426 return ret;
427 }
428 EXPORT_SYMBOL(send_pcm_frame);
429
430 static int really_send_cmd_frame(xbus_t *xbus, xframe_t *xframe)
431 {
432 struct xbus_ops *ops;
433 int ret;
434
435 BUG_ON(!xbus);
436 BUG_ON(!xframe);
437 BUG_ON(xframe->xframe_magic != XFRAME_MAGIC);
438 if (!XBUS_FLAGS(xbus, CONNECTED)) {
439 XBUS_ERR(xbus,
440 "Dropped command before sending -- "
441 "hardware deactivated.\n");
442 dump_xframe("Dropped", xbus, xframe, DBG_ANY);
443 FREE_SEND_XFRAME(xbus, xframe);
444 return -ENODEV;
445 }
446 ops = transportops_get(xbus);
447 BUG_ON(!ops);
448 if (debug & DBG_COMMANDS)
449 dump_xframe("TX-CMD", xbus, xframe, DBG_ANY);
450 ret = ops->xframe_send_cmd(xbus, xframe);
451 transportops_put(xbus);
452 if (ret == 0) {
453 XBUS_COUNTER(xbus, TX_CMD)++;
454 XBUS_COUNTER(xbus, TX_BYTES) += XFRAME_LEN(xframe);
455 }
456 return ret;
457 }
458
459 int xbus_command_queue_tick(xbus_t *xbus)
460 {
461 xframe_t *frm;
462 int ret = 0;
463 int packno;
464
465 xbus->command_tick_counter++;
466 xbus->usec_nosend -= 1000; /* That's our budget */
467 for (packno = 0; packno < 3; packno++) {
468 if (xbus->usec_nosend > 0)
469 break;
470 frm = xframe_dequeue(&xbus->command_queue);
471 if (!frm) {
472 wake_up(&xbus->command_queue_empty);
473 break;
474 }
475 BUG_ON(frm->xframe_magic != XFRAME_MAGIC);
476 xbus->usec_nosend += frm->usec_towait;
477 ret = really_send_cmd_frame(xbus, frm);
478 if (ret < 0) {
479 XBUS_ERR(xbus,
480 "Failed to send from command_queue (ret=%d)\n",
481 ret);
482 xbus_setstate(xbus, XBUS_STATE_FAIL);
483 }
484 }
485 if (xbus->usec_nosend < 0)
486 xbus->usec_nosend = 0;
487 return ret;
488 }
489 EXPORT_SYMBOL(xbus_command_queue_tick);
490
491 static void xbus_command_queue_clean(xbus_t *xbus)
492 {
493 xframe_t *frm;
494
495 XBUS_DBG(DEVICES, xbus, "count=%d\n", xbus->command_queue.count);
496 xframe_queue_disable(&xbus->command_queue, 1);
497 while ((frm = xframe_dequeue(&xbus->command_queue)) != NULL)
498 FREE_SEND_XFRAME(xbus, frm);
499 }
500
501 static int xbus_command_queue_waitempty(xbus_t *xbus)
502 {
503 int ret;
504
505 XBUS_DBG(DEVICES, xbus, "Waiting for command_queue to empty\n");
506 ret =
507 wait_event_interruptible(xbus->command_queue_empty,
508 xframe_queue_count(&xbus->command_queue) ==
509 0);
510 if (ret)
511 XBUS_ERR(xbus, "waiting for command_queue interrupted!!!\n");
512 return ret;
513 }
514
515 int send_cmd_frame(xbus_t *xbus, xframe_t *xframe)
516 {
517 static int rate_limit;
518 int ret = 0;
519
520 BUG_ON(xframe->xframe_magic != XFRAME_MAGIC);
521 if (!XBUS_FLAGS(xbus, CONNECTED)) {
522 XBUS_ERR(xbus,
523 "Dropped command before queueing -- "
524 "hardware deactivated.\n");
525 ret = -ENODEV;
526 goto err;
527 }
528 if (debug & DBG_COMMANDS)
529 dump_xframe(__func__, xbus, xframe, DBG_ANY);
530 if (!xframe_enqueue(&xbus->command_queue, xframe)) {
531 if ((rate_limit++ % 1003) == 0) {
532 XBUS_ERR(xbus,
533 "Dropped command xframe. Cannot enqueue (%d)\n",
534 rate_limit);
535 dump_xframe(__func__, xbus, xframe, DBG_ANY);
536 }
537 xbus_setstate(xbus, XBUS_STATE_FAIL);
538 ret = -E2BIG;
539 goto err;
540 }
541 return 0;
542 err:
543 FREE_SEND_XFRAME(xbus, xframe);
544 return ret;
545 }
546 EXPORT_SYMBOL(send_cmd_frame);
547
548 /*------------------------- Receive Tasklet Handling ---------------*/
549
550 static void xframe_enqueue_recv(xbus_t *xbus, xframe_t *xframe)
551 {
552 int cpu = smp_processor_id();
553
554 BUG_ON(!xbus);
555 xbus->cpu_rcv_intr[cpu]++;
556 if (!xframe_enqueue(&xbus->receive_queue, xframe)) {
557 static int rate_limit;
558
559 if ((rate_limit++ % 1003) == 0)
560 XBUS_ERR(xbus,
561 "Failed to enqueue for receive_tasklet (%d)\n",
562 rate_limit);
563 FREE_RECV_XFRAME(xbus, xframe); /* return to receive_pool */
564 return;
565 }
566 tasklet_schedule(&xbus->receive_tasklet);
567 }
568
569 /*
570 * process frames in the receive_queue in a tasklet
571 */
572 static void receive_tasklet_func(unsigned long data)
573 {
574 xbus_t *xbus = (xbus_t *)data;
575 xframe_t *xframe = NULL;
576 int cpu = smp_processor_id();
577
578 BUG_ON(!xbus);
579 xbus->cpu_rcv_tasklet[cpu]++;
580 while ((xframe = xframe_dequeue(&xbus->receive_queue)) != NULL)
581 xframe_receive(xbus, xframe);
582 }
583
584 void xbus_receive_xframe(xbus_t *xbus, xframe_t *xframe)
585 {
586 BUG_ON(!xbus);
587 if (xbus_is_shutting_down(xbus->num)) {
588 static int rate_limit;
589
590 if ((rate_limit++ % 1000) == 0)
591 XBUS_NOTICE(xbus, "%s: during shutdown (%d)\n",
592 __func__, rate_limit);
593 return;
594 }
595 if (rx_tasklet) {
596 xframe_enqueue_recv(xbus, xframe);
597 } else {
598 if (likely(XBUS_FLAGS(xbus, CONNECTED)))
599 xframe_receive(xbus, xframe);
600 else
601 /* return to receive_pool */
602 FREE_RECV_XFRAME(xbus, xframe);
603 }
604 }
605 EXPORT_SYMBOL(xbus_receive_xframe);
606
607 /*------------------------- Bus Management -------------------------*/
608 xpd_t *xpd_of(const xbus_t *xbus, int xpd_num)
609 {
610 if (!VALID_XPD_NUM(xpd_num))
611 return NULL;
612 return xbus->xpds[xpd_num];
613 }
614 EXPORT_SYMBOL(xpd_of);
615
616 xpd_t *xpd_byaddr(const xbus_t *xbus, uint unit, uint subunit)
617 {
618 if (unit > MAX_UNIT || subunit > MAX_SUBUNIT)
619 return NULL;
620 return xbus->xpds[XPD_IDX(unit, subunit)];
621 }
622 EXPORT_SYMBOL(xpd_byaddr);
623
624 int xbus_xpd_bind(xbus_t *xbus, xpd_t *xpd, int unit, int subunit)
625 {
626 unsigned int xpd_num;
627 unsigned long flags;
628
629 BUG_ON(!xbus);
630 xpd_num = XPD_IDX(unit, subunit);
631 XBUS_DBG(DEVICES, xbus, "XPD #%d\n", xpd_num);
632 spin_lock_irqsave(&xbus->lock, flags);
633 if (!VALID_XPD_NUM(xpd_num)) {
634 XBUS_ERR(xbus, "Bad xpd_num = %d\n", xpd_num);
635 BUG();
636 }
637 if (xbus->xpds[xpd_num] != NULL) {
638 xpd_t *other = xbus->xpds[xpd_num];
639
640 XBUS_ERR(xbus, "xpd_num=%d is occupied by %p (%s)\n", xpd_num,
641 other, other->xpdname);
642 BUG();
643 }
644 snprintf(xpd->xpdname, XPD_NAMELEN, "XPD-%1d%1d", unit, subunit);
645 MKADDR(&xpd->addr, unit, subunit);
646 xpd->xbus_idx = xpd_num;
647 xbus->xpds[xpd_num] = xpd;
648 xpd->xbus = xbus;
649 atomic_inc(&xbus->num_xpds);
650 spin_unlock_irqrestore(&xbus->lock, flags);
651 /* Must be done out of atomic context */
652 if (xpd_device_register(xbus, xpd) < 0) {
653 XPD_ERR(xpd, "%s: xpd_device_register() failed\n", __func__);
654 /* FIXME: What to do? */
655 }
656 return 0;
657 }
658
659 int xbus_xpd_unbind(xbus_t *xbus, xpd_t *xpd)
660 {
661 unsigned int xpd_num = xpd->xbus_idx;
662 unsigned long flags;
663
664 XBUS_DBG(DEVICES, xbus, "XPD #%d\n", xpd_num);
665 if (!VALID_XPD_NUM(xpd_num)) {
666 XBUS_ERR(xbus, "%s: Bad xpd_num = %d\n", __func__, xpd_num);
667 BUG();
668 }
669 if (xbus->xpds[xpd_num] == NULL) {
670 XBUS_ERR(xbus, "%s: slot xpd_num=%d is empty\n", __func__,
671 xpd_num);
672 BUG();
673 }
674 if (xbus->xpds[xpd_num] != xpd) {
675 xpd_t *other = xbus->xpds[xpd_num];
676
677 XBUS_ERR(xbus, "%s: slot xpd_num=%d is occupied by %p (%s)\n",
678 __func__, xpd_num, other, other->xpdname);
679 BUG();
680 }
681 spin_lock_irqsave(&xbus->lock, flags);
682 xpd->xbus = NULL;
683 xbus->xpds[xpd_num] = NULL;
684 if (atomic_dec_and_test(&xbus->num_xpds))
685 xbus_setstate(xbus, XBUS_STATE_IDLE);
686 spin_unlock_irqrestore(&xbus->lock, flags);
687 return 0;
688 }
689
690 static xpd_type_t xpd_hw2xpd_type(const struct unit_descriptor *unit_descriptor)
691 {
692 xpd_type_t xpd_type;
693
694 switch (unit_descriptor->type) {
695 case 1:
696 case 6:
697 xpd_type = XPD_TYPE_FXS;
698 break;
699 case 2:
700 xpd_type = XPD_TYPE_FXO;
701 break;
702 case 3:
703 xpd_type = XPD_TYPE_BRI;
704 break;
705 case 4:
706 xpd_type = XPD_TYPE_PRI;
707 break;
708 case 5:
709 xpd_type = XPD_TYPE_ECHO;
710 break;
711 case 7:
712 xpd_type = XPD_TYPE_NOMODULE;
713 break;
714 default:
715 NOTICE("WARNING: xpd hw type is: %d\n", unit_descriptor->type);
716 xpd_type = XPD_TYPE_NOMODULE;
717 break;
718 }
719 return xpd_type;
720 }
721
722 int subunits_of_xpd(const struct unit_descriptor* unit_descriptor,
723 const xproto_table_t *proto_table) {
724 int ports = unit_descriptor->ports_per_chip * unit_descriptor->numchips;
725
726 return
727 (ports + proto_table->ports_per_subunit - 1)
728 / proto_table->ports_per_subunit;
729 }
730
731 static int new_card(xbus_t *xbus, const struct unit_descriptor *unit_descriptor)
732 {
733 int unit = unit_descriptor->addr.unit;
734 xpd_type_t xpd_type;
735 __u8 hw_type;
736 __u8 numchips;
737 __u8 ports_per_chip;
738 __u8 ports;
739 __u8 port_dir;
740 const xproto_table_t *proto_table;
741 int i;
742 int subunits;
743 int ret = 0;
744 int remaining_ports;
745 const struct echoops *echoops;
746
747 /* Translate parameters from "unit_descriptor" */
748 hw_type = unit_descriptor->type;
749 numchips = unit_descriptor->numchips;
750 ports_per_chip = unit_descriptor->ports_per_chip;
751 port_dir = unit_descriptor->port_dir;
752 ports = unit_descriptor->ports_per_chip * unit_descriptor->numchips;
753 xpd_type = xpd_hw2xpd_type(unit_descriptor);
754 proto_table = xproto_get(xpd_type);
755 if (!proto_table) {
756 XBUS_NOTICE(xbus,
757 "CARD %d: missing protocol table for xpd_type %d. "
758 "Ignored.\n",
759 unit, xpd_type);
760 return -EINVAL;
761 }
762 echoops = proto_table->echoops;
763 if (echoops) {
764 XBUS_INFO(xbus, "Detected ECHO Canceler (%d)\n", unit);
765 if (ECHOOPS(xbus)) {
766 XBUS_NOTICE(xbus,
767 "CARD %d: tryies to define echoops (xpd_type %d) "
768 "but we already have one. Ignored.\n",
769 unit, xpd_type);
770 return -EINVAL;
771 }
772 xbus->echo_state.echoops = echoops;
773 xbus->echo_state.xpd_idx = XPD_IDX(unit, 0);
774 }
775 remaining_ports = ports;
776 subunits = subunits_of_xpd(unit_descriptor, proto_table);
777 XBUS_DBG(DEVICES, xbus,
778 "CARD %d xpd_type=%d/hw_type=%d ports=%d (%dx%d), "
779 "%d subunits, port-dir=0x%02X\n",
780 unit, xpd_type, hw_type, ports, numchips, ports_per_chip, subunits,
781 port_dir);
782 if (xpd_type == XPD_TYPE_PRI || xpd_type == XPD_TYPE_BRI)
783 xbus->quirks.has_digital_span = 1;
784 if (xpd_type == XPD_TYPE_FXO)
785 xbus->quirks.has_fxo = 1;
786 xbus->worker.num_units += subunits - 1;
787 for (i = 0; i < subunits; i++) {
788 int subunit_ports = proto_table->ports_per_subunit;
789
790 if (subunit_ports > remaining_ports)
791 subunit_ports = remaining_ports;
792 remaining_ports -= proto_table->ports_per_subunit;
793 if (subunit_ports <= 0) {
794 XBUS_NOTICE(xbus,
795 "Subunit XPD=%d%d without ports (%d of %d)\n",
796 unit, i, subunit_ports, ports);
797 ret = -ENODEV;
798 goto out;
799 }
800 if (!XBUS_IS(xbus, RECVD_DESC)) {
801 XBUS_NOTICE(xbus,
802 "Cannot create XPD=%d%d in state %s\n",
803 unit, i, xbus_statename(XBUS_STATE(xbus)));
804 ret = -ENODEV;
805 goto out;
806 }
807 XBUS_DBG(DEVICES, xbus,
808 "Creating XPD=%d%d xpd_type=%d.%d hw_type=%d (%d ports)\n", unit, i,
809 xpd_type, unit_descriptor->subtype, hw_type, subunit_ports);
810 ret =
811 create_xpd(xbus, proto_table, unit_descriptor, unit, i, xpd_type);
812 if (ret < 0) {
813 XBUS_ERR(xbus, "Creation of XPD=%d%d failed %d\n", unit,
814 i, ret);
815 goto out;
816 }
817 xbus->worker.num_units_initialized++;
818 }
819 out:
820 xproto_put(proto_table); /* ref count is inside the xpds now */
821 return ret;
822 }
823
824 static void xbus_release_xpds(xbus_t *xbus)
825 {
826 int i;
827
828 XBUS_DBG(DEVICES, xbus, "[%s] Release XPDS\n", xbus->label);
829 for (i = 0; i < MAX_XPDS; i++) {
830 xpd_t *xpd = xpd_of(xbus, i);
831
832 if (xpd)
833 put_xpd(__func__, xpd);
834 }
835 }
836
837 static int xbus_aquire_xpds(xbus_t *xbus)
838 {
839 unsigned long flags;
840 int i;
841 int ret = 0;
842 xpd_t *xpd;
843
844 XBUS_DBG(DEVICES, xbus, "[%s] Aquire XPDS\n", xbus->label);
845 spin_lock_irqsave(&xbus->lock, flags);
846 for (i = 0; i < MAX_XPDS; i++) {
847 xpd = xpd_of(xbus, i);
848 if (xpd) {
849 xpd = get_xpd(__func__, xpd);
850 if (!xpd)
851 goto err;
852 }
853 }
854 out:
855 spin_unlock_irqrestore(&xbus->lock, flags);
856 return ret;
857 err:
858 for (--i; i >= 0; i--) {
859 xpd = xpd_of(xbus, i);
860 if (xpd)
861 put_xpd(__func__, xpd);
862 }
863 ret = -EBUSY;
864 goto out;
865 }
866
867 static int xpd_initialize(xpd_t *xpd)
868 {
869 int ret = -ENODEV;
870
871 if (CALL_XMETHOD(card_init, xpd) < 0) {
872 XPD_ERR(xpd, "Card Initialization failed\n");
873 goto out;
874 }
875 xpd->card_present = 1;
876 if (IS_PHONEDEV(xpd)) {
877 /*
878 * Set echo canceler channels (off)
879 * Asterisk will tell us when/if it's needed.
880 */
881 CALL_PHONE_METHOD(echocancel_setmask, xpd, 0);
882 /* Turn on all channels */
883 CALL_PHONE_METHOD(card_state, xpd, 1);
884 }
885 if (!xpd_setstate(xpd, XPD_STATE_READY))
886 goto out;
887 XPD_INFO(xpd, "Initialized: %s\n", xpd->type_name);
888 ret = 0;
889 out:
890 return ret;
891 }
892
893 static int xbus_echocancel(xbus_t *xbus, int on)
894 {
895 int unit;
896 int subunit;
897 xpd_t *xpd;
898
899 if (!ECHOOPS(xbus))
900 return 0;
901 for (unit = 0; unit < MAX_UNIT; unit++) {
902 xpd = xpd_byaddr(xbus, unit, 0);
903 if (!xpd || !IS_PHONEDEV(xpd))
904 continue;
905 for (subunit = 0; subunit < MAX_SUBUNIT; subunit++) {
906 int ret;
907
908 xpd = xpd_byaddr(xbus, unit, subunit);
909 if (!xpd || !IS_PHONEDEV(xpd))
910 continue;
911 ret = echocancel_xpd(xpd, on);
912 if (ret < 0) {
913 XPD_ERR(xpd, "Fail in xbus_echocancel()\n");
914 return ret;
915 }
916 }
917 }
918 return 0;
919 }
920
921 static void xbus_deactivate_xpds(xbus_t *xbus)
922 {
923 unsigned long flags;
924 int unit;
925 int subunit;
926 xpd_t *xpd;
927
928 for (unit = 0; unit < MAX_UNIT; unit++) {
929 xpd = xpd_byaddr(xbus, unit, 0);
930 if (!xpd)
931 continue;
932 for (subunit = 0; subunit < MAX_SUBUNIT; subunit++) {
933 xpd = xpd_byaddr(xbus, unit, subunit);
934 if (!xpd)
935 continue;
936 spin_lock_irqsave(&xpd->lock, flags);
937 xpd->card_present = 0;
938 xpd_setstate(xpd, XPD_STATE_NOHW);
939 spin_unlock_irqrestore(&xpd->lock, flags);
940 }
941 }
942 }
943
944 static int xbus_initialize(xbus_t *xbus)
945 {
946 int unit;
947 int subunit;
948 xpd_t *xpd;
949 struct timeval time_start;
950 struct timeval time_end;
951 unsigned long timediff;
952 int res = 0;
953
954 do_gettimeofday(&time_start);
955 XBUS_DBG(DEVICES, xbus, "refcount_xbus=%d\n", refcount_xbus(xbus));
956 if (xbus_aquire_xpds(xbus) < 0) /* Until end of initialization */
957 return -EBUSY;
958 for (unit = 0; unit < MAX_UNIT; unit++) {
959 xpd = xpd_byaddr(xbus, unit, 0);
960 if (!xpd)
961 continue;
962 if (!XBUS_IS(xbus, RECVD_DESC)) {
963 XBUS_NOTICE(xbus,
964 "Cannot initialize UNIT=%d in state %s\n",
965 unit, xbus_statename(XBUS_STATE(xbus)));
966 goto err;
967 }
968 if (run_initialize_registers(xpd) < 0) {
969 XBUS_ERR(xbus,
970 "Register Initialization of card #%d failed\n",
971 unit);
972 goto err;
973 }
974 for (subunit = 0; subunit < MAX_SUBUNIT; subunit++) {
975 int ret;
976
977 xpd = xpd_byaddr(xbus, unit, subunit);
978 if (!xpd)
979 continue;
980 if (!XBUS_IS(xbus, RECVD_DESC)) {
981 XBUS_ERR(xbus,
982 "XPD-%d%d Not in 'RECVD_DESC' state\n",
983 unit, subunit);
984 goto err;
985 }
986 ret = xpd_initialize(xpd);
987 if (ret < 0)
988 goto err;
989 }
990 }
991 xbus_echocancel(xbus, 1);
992 do_gettimeofday(&time_end);
993 timediff = usec_diff(&time_end, &time_start);
994 timediff /= 1000 * 100;
995 XBUS_INFO(xbus, "Initialized in %ld.%1ld sec\n", timediff / 10,
996 timediff % 10);
997 out:
998 xbus_release_xpds(xbus); /* Initialization done/failed */
999 return res;
1000 err:
1001 xbus_setstate(xbus, XBUS_STATE_FAIL);
1002 res = -EINVAL;
1003 goto out;
1004 }
1005
1006 int xbus_is_registered(xbus_t *xbus)
1007 {
1008 return xbus->ddev && xbus->ddev->dev.parent;
1009 }
1010
1011 static void xbus_free_ddev(xbus_t *xbus)
1012 {
1013 if (!xbus->ddev)
1014 return;
1015 kfree(xbus->ddev->devicetype); /* NULL is safe */
1016 xbus->ddev->devicetype = NULL;
1017 xbus->ddev->location = NULL;
1018 xbus->ddev->hardware_id = NULL;
1019 dahdi_free_device(xbus->ddev);
1020 xbus->ddev = NULL;
1021 }
1022
1023 static DEFINE_MUTEX(dahdi_registration_mutex);
1024
1025 int xbus_register_dahdi_device(xbus_t *xbus)
1026 {
1027 int i;
1028 int offset = 0;
1029 int ret;
1030
1031 XBUS_DBG(DEVICES, xbus, "Entering %s\n", __func__);
1032 ret = mutex_lock_interruptible(&dahdi_registration_mutex);
1033 if (ret < 0) {
1034 XBUS_ERR(xbus, "dahdi_registration_mutex already taken\n");
1035 goto err;
1036 }
1037 if (xbus_is_registered(xbus)) {
1038 /*
1039 * Ignore duplicate registrations (from dahdi_registration)
1040 * Until we completely migrate to dahdi_autoreg=1 and
1041 * hotplug-based span-assignments
1042 */
1043 XBUS_DBG(DEVICES, xbus, "Already registered to DAHDI\n");
1044 ret = 0;
1045 goto out;
1046 }
1047 xbus->ddev = dahdi_create_device();
1048 if (!xbus->ddev) {
1049 ret = -ENOMEM;
1050 goto err;
1051 }
1052 /*
1053 * This actually describe the dahdi_spaninfo version 3
1054 * A bunch of unrelated data exported via a modified ioctl()
1055 * What a bummer...
1056 */
1057 xbus->ddev->manufacturer = "Xorcom Inc."; /* OK, that's obvious */
1058 /* span->spantype = "...."; set in card_dahdi_preregistration() */
1059 /*
1060 * Yes, this basically duplicates information available
1061 * from the description field. If some more is needed
1062 * why not add it there?
1063 * OK, let's add to the kernel more useless info.
1064 */
1065 xbus->ddev->devicetype = kasprintf(GFP_KERNEL, "Astribank2");
1066 if (!xbus->ddev->devicetype) {
1067 ret = -ENOMEM;
1068 goto err;
1069 }
1070
1071 /*
1072 * location is the only usefull new data item.
1073 * For our devices it was available for ages via:
1074 * - The legacy "/proc/xpp/XBUS-??/summary" (CONNECTOR=...)
1075 * - The same info in "/proc/xpp/xbuses"
1076 * - The modern "/sys/bus/astribanks/devices/xbus-??/connector"
1077 * attribute
1078 * So let's also export it via the newfangled "location" field.
1079 */
1080 xbus->ddev->location = xbus->connector;
1081 xbus->ddev->hardware_id = xbus->label;
1082
1083 /*
1084 * Prepare the span list
1085 */
1086 for (i = 0; i < MAX_XPDS; i++) {
1087 xpd_t *xpd = xpd_of(xbus, i);
1088 if (xpd && IS_PHONEDEV(xpd)) {
1089 XPD_DBG(DEVICES, xpd, "offset=%d\n", offset);
1090 xpd_dahdi_preregister(xpd, offset++);
1091 }
1092 }
1093 if (dahdi_register_device(xbus->ddev, &xbus->astribank)) {
1094 XBUS_ERR(xbus, "Failed to dahdi_register_device()\n");
1095 ret = -ENODEV;
1096 goto err;
1097 }
1098 for (i = 0; i < MAX_XPDS; i++) {
1099 xpd_t *xpd = xpd_of(xbus, i);
1100 if (xpd && IS_PHONEDEV(xpd)) {
1101 XPD_DBG(DEVICES, xpd, "\n");
1102 xpd_dahdi_postregister(xpd);
1103 }
1104 }
1105 ret = 0;
1106 out:
1107 mutex_unlock(&dahdi_registration_mutex);
1108 return ret;
1109 err:
1110 xbus_free_ddev(xbus);
1111 goto out;
1112 }
1113
1114 void xbus_unregister_dahdi_device(xbus_t *xbus)
1115 {
1116 int i;
1117 int ret;
1118
1119 XBUS_DBG(DEVICES, xbus, "%s\n", __func__);
1120 ret = mutex_lock_interruptible(&dahdi_registration_mutex);
1121 if (ret < 0) {
1122 XBUS_ERR(xbus, "dahdi_registration_mutex already taken\n");
1123 return;
1124 }
1125 if (!xbus_is_registered(xbus)) {
1126 /*
1127 * Ignore duplicate unregistrations
1128 */
1129 XBUS_DBG(DEVICES, xbus, "Already unregistered to DAHDI\n");
1130 goto err;
1131 }
1132 for (i = 0; i < MAX_XPDS; i++) {
1133 xpd_t *xpd = xpd_of(xbus, i);
1134 xpd_dahdi_preunregister(xpd);
1135 }
1136 if (xbus->ddev) {
1137 dahdi_unregister_device(xbus->ddev);
1138 XBUS_DBG(DEVICES, xbus,
1139 "%s: finished dahdi_unregister_device()\n", __func__);
1140 xbus_free_ddev(xbus);
1141 }
1142 for (i = 0; i < MAX_XPDS; i++) {
1143 xpd_t *xpd = xpd_of(xbus, i);
1144 xpd_dahdi_postunregister(xpd);
1145 }
1146 err:
1147 mutex_unlock(&dahdi_registration_mutex);
1148 }
1149
1150 /*
1151 * This must be called from synchronous (non-interrupt) context
1152 * it returns only when all XPD's on the bus are detected and
1153 * initialized.
1154 */
1155 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
1156 static void xbus_populate(struct work_struct *work)
1157 {
1158 struct xbus_workqueue *worker =
1159 container_of(work, struct xbus_workqueue, xpds_init_work);
1160 #else
1161 void xbus_populate(void *data)
1162 {
1163 struct xbus_workqueue *worker = data;
1164 #endif
1165 xbus_t *xbus;
1166 struct list_head *card;
1167 struct list_head *next_card;
1168 unsigned long flags;
1169 int ret = 0;
1170
1171 xbus = container_of(worker, xbus_t, worker);
1172 xbus = get_xbus(__func__, xbus->num); /* return in function end */
1173 XBUS_DBG(DEVICES, xbus, "Entering %s\n", __func__);
1174 spin_lock_irqsave(&worker->worker_lock, flags);
1175 list_for_each_safe(card, next_card, &worker->card_list) {
1176 struct card_desc_struct *card_desc =
1177 list_entry(card, struct card_desc_struct, card_list);
1178
1179 list_del(card);
1180 BUG_ON(card_desc->magic != CARD_DESC_MAGIC);
1181 /* Release/Reacquire locks around blocking calls */
1182 spin_unlock_irqrestore(&xbus->worker.worker_lock, flags);
1183 ret = new_card(xbus, &card_desc->unit_descriptor);
1184 spin_lock_irqsave(&xbus->worker.worker_lock, flags);
1185 KZFREE(card_desc);
1186 if (ret)
1187 break;
1188 }
1189 spin_unlock_irqrestore(&worker->worker_lock, flags);
1190 if (xbus_initialize(xbus) < 0) {
1191 XBUS_NOTICE(xbus,
1192 "Initialization failed. Leave unused. "
1193 "refcount_xbus=%d\n",
1194 refcount_xbus(xbus));
1195 goto failed;
1196 }
1197 if (!xbus_setstate(xbus, XBUS_STATE_READY)) {
1198 XBUS_NOTICE(xbus,
1199 "Illegal transition. Leave unused. refcount_xbus=%d\n",
1200 refcount_xbus(xbus));
1201 goto failed;
1202 }
1203 worker->xpds_init_done = 1;
1204 /*
1205 * Now request Astribank to start self_ticking.
1206 * This is the last initialization command. So
1207 * all others will reach the device before it.
1208 */
1209 xbus_request_sync(xbus, SYNC_MODE_PLL);
1210 elect_syncer("xbus_populate(end)"); /* FIXME: try to do it later */
1211 if (!dahdi_get_auto_assign_spans())
1212 xbus_register_dahdi_device(xbus);
1213 out:
1214 XBUS_DBG(DEVICES, xbus, "Leaving\n");
1215 wake_up_interruptible_all(&worker->wait_for_xpd_initialization);
1216 XBUS_DBG(DEVICES, xbus, "populate release\n");
1217 up(&worker->running_initialization);
1218 put_xbus(__func__, xbus); /* taken at function entry */
1219 return;
1220 failed:
1221 xbus_setstate(xbus, XBUS_STATE_FAIL);
1222 goto out;
1223 }
1224
1225 int xbus_process_worker(xbus_t *xbus)
1226 {
1227 struct xbus_workqueue *worker;
1228
1229 if (!xbus) {
1230 ERR("%s: xbus gone -- skip initialization\n", __func__);
1231 return 0;
1232 }
1233 worker = &xbus->worker;
1234 if (down_trylock(&worker->running_initialization)) {
1235 ERR("%s: xbus is disconnected -- skip initialization\n",
1236 __func__);
1237 return 0;
1238 }
1239 XBUS_DBG(DEVICES, xbus, "\n");
1240 /* Initialize the work. (adapt to kernel API changes). */
1241 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
1242 INIT_WORK(&worker->xpds_init_work, xbus_populate);
1243 #else
1244 INIT_WORK(&worker->xpds_init_work, xbus_populate, worker);
1245 #endif
1246 BUG_ON(!xbus);
1247 /* Now send it */
1248 if (!queue_work(worker->wq, &worker->xpds_init_work)) {
1249 XBUS_ERR(xbus, "Failed to queue xpd initialization work\n");
1250 up(&worker->running_initialization);
1251 return 0;
1252 }
1253 return 1;
1254 }
1255
1256 static void worker_reset(xbus_t *xbus)
1257 {
1258 struct xbus_workqueue *worker;
1259 struct list_head *card;
1260 struct list_head *next_card;
1261 unsigned long flags;
1262 char *name;
1263
1264 BUG_ON(!xbus);
1265 worker = &xbus->worker;
1266 name = (xbus) ? xbus->busname : "detached";
1267 DBG(DEVICES, "%s\n", name);
1268 if (!worker->xpds_init_done) {
1269 XBUS_NOTICE(xbus, "XPDS initialization was not finished\n");
1270 }
1271 spin_lock_irqsave(&worker->worker_lock, flags);
1272 list_for_each_safe(card, next_card, &worker->card_list) {
1273 struct card_desc_struct *card_desc =
1274 list_entry(card, struct card_desc_struct, card_list);
1275
1276 BUG_ON(card_desc->magic != CARD_DESC_MAGIC);
1277 list_del(card);
1278 KZFREE(card_desc);
1279 }
1280 worker->xpds_init_done = 0;
1281 worker->num_units = 0;
1282 worker->num_units_initialized = 0;
1283 wake_up_interruptible_all(&worker->wait_for_xpd_initialization);
1284 spin_unlock_irqrestore(&worker->worker_lock, flags);
1285 }
1286
1287 /*
1288 * Called only after worker_reset(xbus)
1289 */
1290 static void worker_destroy(xbus_t *xbus)
1291 {
1292 struct xbus_workqueue *worker;
1293
1294 BUG_ON(!xbus);
1295 worker = &xbus->worker;
1296 XBUS_DBG(DEVICES, xbus, "Waiting for worker to finish...\n");
1297 down(&worker->running_initialization);
1298 XBUS_DBG(DEVICES, xbus, "Waiting for worker to finish -- done\n");
1299 if (worker->wq) {
1300 XBUS_DBG(DEVICES, xbus, "destroying workqueue...\n");
1301 flush_workqueue(worker->wq);
1302 destroy_workqueue(worker->wq);
1303 worker->wq = NULL;
1304 XBUS_DBG(DEVICES, xbus, "destroying workqueue -- done\n");
1305 }
1306 XBUS_DBG(DEVICES, xbus, "detach worker\n");
1307 put_xbus(__func__, xbus); /* got from worker_run() */
1308 }
1309
1310 static void worker_init(xbus_t *xbus)
1311 {
1312 struct xbus_workqueue *worker;
1313
1314 BUG_ON(!xbus);
1315 XBUS_DBG(DEVICES, xbus, "\n");
1316 worker = &xbus->worker;
1317 /* poll related variables */
1318 spin_lock_init(&worker->worker_lock);
1319 INIT_LIST_HEAD(&worker->card_list);
1320 init_waitqueue_head(&worker->wait_for_xpd_initialization);
1321 worker->wq = NULL;
1322 sema_init(&xbus->worker.running_initialization, 1);
1323 }
1324
1325 /*
1326 * Allocate a worker for the xbus including the nessessary workqueue.
1327 * May call blocking operations, but only briefly (as we are called
1328 * from xbus_new() which is called from khubd.
1329 */
1330 static int worker_run(xbus_t *xbus)
1331 {
1332 struct xbus_workqueue *worker;
1333
1334 xbus = get_xbus(__func__, xbus->num); /* return in worker_destroy() */
1335 BUG_ON(!xbus);
1336 BUG_ON(xbus->busname[0] == '\0'); /* No name? */
1337 worker = &xbus->worker;
1338 BUG_ON(worker->wq); /* Hmmm... nested workers? */
1339 XBUS_DBG(DEVICES, xbus, "\n");
1340 /* poll related variables */
1341 worker->wq = create_singlethread_workqueue(xbus->busname);
1342 if (!worker->wq) {
1343 XBUS_ERR(xbus, "Failed to create worker workqueue.\n");
1344 goto err;
1345 }
1346 return 1;
1347 err:
1348 worker_reset(xbus);
1349 worker_destroy(xbus);
1350 return 0;
1351 }
1352
1353 bool xbus_setflags(xbus_t *xbus, int flagbit, bool on)
1354 {
1355 unsigned long flags;
1356
1357 spin_lock_irqsave(&xbus->transport.state_lock, flags);
1358 XBUS_DBG(DEVICES, xbus, "%s flag %d\n", (on) ? "Set" : "Clear",
1359 flagbit);
1360 if (on)
1361 set_bit(flagbit, &(xbus->transport.transport_flags));
1362 else
1363 clear_bit(flagbit, &(xbus->transport.transport_flags));
1364 spin_unlock_irqrestore(&xbus->transport.state_lock, flags);
1365 return 1;
1366 }
1367
1368 bool xbus_setstate(xbus_t *xbus, enum xbus_state newstate)
1369 {
1370 unsigned long flags;
1371 bool ret = 0;
1372 int state_flip = 0;
1373
1374 spin_lock_irqsave(&xbus->transport.state_lock, flags);
1375 if (newstate == XBUS_STATE(xbus)) {
1376 XBUS_DBG(DEVICES, xbus, "stay at %s\n",
1377 xbus_statename(newstate));
1378 goto out;
1379 }
1380 /* Sanity tests */
1381 switch (newstate) {
1382 case XBUS_STATE_START:
1383 goto bad_state;
1384 case XBUS_STATE_IDLE:
1385 if (!XBUS_IS(xbus, START) && !XBUS_IS(xbus, DEACTIVATED))
1386 goto bad_state;
1387 break;
1388 case XBUS_STATE_SENT_REQUEST:
1389 if (!XBUS_IS(xbus, IDLE) && !XBUS_IS(xbus, SENT_REQUEST))
1390 goto bad_state;
1391 break;
1392 case XBUS_STATE_RECVD_DESC:
1393 if (!XBUS_IS(xbus, SENT_REQUEST))
1394 goto bad_state;
1395 break;
1396 case XBUS_STATE_READY:
1397 if (!XBUS_IS(xbus, RECVD_DESC))
1398 goto bad_state;
1399 state_flip = 1; /* We are good */
1400 break;
1401 case XBUS_STATE_DEACTIVATING:
1402 if (XBUS_IS(xbus, DEACTIVATING))
1403 goto bad_state;
1404 if (XBUS_IS(xbus, DEACTIVATED))
1405 goto bad_state;
1406 break;
1407 case XBUS_STATE_DEACTIVATED:
1408 if (!XBUS_IS(xbus, DEACTIVATING))
1409 goto bad_state;
1410 break;
1411 case XBUS_STATE_FAIL:
1412 if (XBUS_IS(xbus, DEACTIVATING))
1413 goto bad_state;
1414 if (XBUS_IS(xbus, DEACTIVATED))
1415 goto bad_state;
1416 break;
1417 default:
1418 XBUS_NOTICE(xbus, "%s: unknown state %d\n", __func__, newstate);
1419 goto out;
1420 }
1421 /* All good */
1422 XBUS_DBG(DEVICES, xbus, "%s -> %s\n", xbus_statename(XBUS_STATE(xbus)),
1423 xbus_statename(newstate));
1424 if (xbus->transport.xbus_state == XBUS_STATE_READY
1425 && newstate != XBUS_STATE_READY)
1426 state_flip = -1; /* We became bad */
1427 xbus->transport.xbus_state = newstate;
1428 ret = 1;
1429 out:
1430 spin_unlock_irqrestore(&xbus->transport.state_lock, flags);
1431 /* Should be sent out of spinlocks */
1432 if (state_flip > 0)
1433 astribank_uevent_send(xbus, KOBJ_ONLINE);
1434 else if (state_flip < 0)
1435 astribank_uevent_send(xbus, KOBJ_OFFLINE);
1436 return ret;
1437 bad_state:
1438 XBUS_NOTICE(xbus, "Bad state transition %s -> %s ignored.\n",
1439 xbus_statename(XBUS_STATE(xbus)), xbus_statename(newstate));
1440 goto out;
1441 }
1442 EXPORT_SYMBOL(xbus_setstate);
1443
1444 int xbus_activate(xbus_t *xbus)
1445 {
1446 XBUS_INFO(xbus, "[%s] Activating\n", xbus->label);
1447 xpp_drift_init(xbus);
1448 xbus_set_command_timer(xbus, 1);
1449 xframe_queue_disable(&xbus->command_queue, 0);
1450 /* must be done after transport is valid */
1451 xbus_setstate(xbus, XBUS_STATE_IDLE);
1452 CALL_PROTO(GLOBAL, AB_REQUEST, xbus, NULL);
1453 /*
1454 * Make sure Astribank knows not to send us ticks.
1455 */
1456 xbus_request_sync(xbus, SYNC_MODE_NONE);
1457 return 0;
1458 }
1459 EXPORT_SYMBOL(xbus_activate);
1460
1461 int xbus_connect(xbus_t *xbus)
1462 {
1463 struct xbus_ops *ops;
1464
1465 BUG_ON(!xbus);
1466 XBUS_DBG(DEVICES, xbus, "\n");
1467 ops = transportops_get(xbus);
1468 BUG_ON(!ops);
1469 /* Sanity checks */
1470 BUG_ON(!ops->xframe_send_pcm);
1471 BUG_ON(!ops->xframe_send_cmd);
1472 BUG_ON(!ops->alloc_xframe);
1473 BUG_ON(!ops->free_xframe);
1474 xbus_setflags(xbus, XBUS_FLAG_CONNECTED, 1);
1475 xbus_activate(xbus);
1476 return 0;
1477 }
1478 EXPORT_SYMBOL(xbus_connect);
1479
1480 void xbus_deactivate(xbus_t *xbus)
1481 {
1482 BUG_ON(!xbus);
1483 XBUS_INFO(xbus, "[%s] Deactivating\n", xbus->label);
1484 if (!xbus_setstate(xbus, XBUS_STATE_DEACTIVATING))
1485 return;
1486 xbus_request_sync(xbus, SYNC_MODE_NONE); /* no more ticks */
1487 elect_syncer("deactivate");
1488 xbus_echocancel(xbus, 0);
1489 xbus_deactivate_xpds(xbus);
1490 XBUS_DBG(DEVICES, xbus, "[%s] Waiting for queues\n", xbus->label);
1491 xbus_command_queue_clean(xbus);
1492 xbus_command_queue_waitempty(xbus);
1493 xbus_setstate(xbus, XBUS_STATE_DEACTIVATED);
1494 worker_reset(xbus);
1495 xbus_unregister_dahdi_device(xbus);
1496 xbus_release_xpds(xbus); /* taken in xpd_alloc() [kref_init] */
1497 }
1498 EXPORT_SYMBOL(xbus_deactivate);
1499
1500 void xbus_disconnect(xbus_t *xbus)
1501 {
1502 BUG_ON(!xbus);
1503 XBUS_INFO(xbus, "[%s] Disconnecting\n", xbus->label);
1504 xbus_setflags(xbus, XBUS_FLAG_CONNECTED, 0);
1505 xbus_deactivate(xbus);
1506 xbus_command_queue_clean(xbus);
1507 xbus_command_queue_waitempty(xbus);
1508 tasklet_kill(&xbus->receive_tasklet);
1509 xframe_queue_clear(&xbus->receive_queue);
1510 xframe_queue_clear(&xbus->send_pool);
1511 xframe_queue_clear(&xbus->receive_pool);
1512 xframe_queue_clear(&xbus->pcm_tospan);
1513 del_timer_sync(&xbus->command_timer);
1514 transportops_put(xbus);
1515 transport_destroy(xbus);
1516 /* worker_reset(xbus) was called in xbus_deactivate(xbus) */
1517 worker_destroy(xbus);
1518 XBUS_DBG(DEVICES, xbus, "Deactivated refcount_xbus=%d\n",
1519 refcount_xbus(xbus));
1520 xbus_sysfs_transport_remove(xbus); /* Device-Model */
1521 put_xbus(__func__, xbus); /* from xbus_new() [kref_init()] */
1522 }
1523 EXPORT_SYMBOL(xbus_disconnect);
1524
1525 static xbus_t *xbus_alloc(void)
1526 {
1527 unsigned long flags;
1528 xbus_t *xbus;
1529 int i;
1530
1531 xbus = KZALLOC(sizeof(xbus_t), GFP_KERNEL);
1532 if (!xbus) {
1533 ERR("%s: out of memory\n", __func__);
1534 return NULL;
1535 }
1536 spin_lock_irqsave(&xbuses_lock, flags);
1537 for (i = 0; i < MAX_BUSES; i++)
1538 if (xbuses_array[i].xbus == NULL)
1539 break;
1540 if (i >= MAX_BUSES) {
1541 ERR("%s: No free slot for new bus. i=%d\n", __func__, i);
1542 KZFREE(xbus);
1543 xbus = NULL;
1544 goto out;
1545 }
1546 /* Found empty slot */
1547 xbus->num = i;
1548 init_xbus(i, xbus);
1549 out:
1550 spin_unlock_irqrestore(&xbuses_lock, flags);
1551 return xbus;
1552 }
1553
1554 void xbus_free(xbus_t *xbus)
1555 {
1556 unsigned long flags;
1557 uint num;
1558
1559 if (!xbus)
1560 return;
1561 XBUS_DBG(DEVICES, xbus, "Free\n");
1562 spin_lock_irqsave(&xbuses_lock, flags);
1563 num = xbus->num;
1564 BUG_ON(!xbuses_array[num].xbus);
1565 BUG_ON(xbus != xbuses_array[num].xbus);
1566 init_xbus(num, NULL);
1567 spin_unlock_irqrestore(&xbuses_lock, flags);
1568 #ifdef CONFIG_PROC_FS
1569 if (xbus->proc_xbus_dir) {
1570 if (xbus->proc_xbus_summary) {
1571 XBUS_DBG(PROC, xbus, "Removing proc '%s'\n",
1572 PROC_XBUS_SUMMARY);
1573 remove_proc_entry(PROC_XBUS_SUMMARY,
1574 xbus->proc_xbus_dir);
1575 xbus->proc_xbus_summary = NULL;
1576 }
1577 #ifdef PROTOCOL_DEBUG
1578 if (xbus->proc_xbus_command) {
1579 XBUS_DBG(PROC, xbus, "Removing proc '%s'\n",
1580 PROC_XBUS_COMMAND);
1581 remove_proc_entry(PROC_XBUS_COMMAND,
1582 xbus->proc_xbus_dir);
1583 xbus->proc_xbus_command = NULL;
1584 }
1585 #endif
1586 XBUS_DBG(PROC, xbus, "Removing proc directory\n");
1587 remove_proc_entry(xbus->busname, xpp_proc_toplevel);
1588 xbus->proc_xbus_dir = NULL;
1589 }
1590 #endif
1591 spin_lock_irqsave(&xbuses_lock, flags);
1592 XBUS_DBG(DEVICES, xbus, "Going to free...\n");
1593 spin_unlock_irqrestore(&xbuses_lock, flags);
1594 KZFREE(xbus);
1595 }
1596 EXPORT_SYMBOL(xbus_free);
1597
1598 xbus_t *xbus_new(struct xbus_ops *ops, ushort max_send_size,
1599 struct device *transport_device, void *priv)
1600 {
1601 int err;
1602 xbus_t *xbus = NULL;
1603
1604 BUG_ON(!ops);
1605 xbus = xbus_alloc();
1606 if (!xbus) {
1607 ERR("%s: Failed allocating new xbus\n", __func__);
1608 return NULL;
1609 }
1610 snprintf(xbus->busname, XBUS_NAMELEN, "XBUS-%02d", xbus->num);
1611 XBUS_DBG(DEVICES, xbus, "\n");
1612 transport_init(xbus, ops, max_send_size, transport_device, priv);
1613 spin_lock_init(&xbus->lock);
1614 init_waitqueue_head(&xbus->command_queue_empty);
1615 init_timer(&xbus->command_timer);
1616 atomic_set(&xbus->pcm_rx_counter, 0);
1617 xbus->min_tx_sync = INT_MAX;
1618 xbus->min_rx_sync = INT_MAX;
1619
1620 kref_init(&xbus->kref);
1621 worker_init(xbus);
1622 atomic_set(&xbus->num_xpds, 0);
1623 xbus->sync_mode = SYNC_MODE_NONE;
1624 xbus->sync_mode_default = SYNC_MODE_PLL;
1625 err = xbus_sysfs_create(xbus);
1626 if (err) {
1627 XBUS_ERR(xbus, "SYSFS creation failed: %d\n", err);
1628 goto nobus;
1629 }
1630 err = xbus_sysfs_transport_create(xbus);
1631 if (err) {
1632 XBUS_ERR(xbus, "SYSFS transport link creation failed: %d\n",
1633 err);
1634 goto nobus;
1635 }
1636 xbus_reset_counters(xbus);
1637 #ifdef CONFIG_PROC_FS
1638 XBUS_DBG(PROC, xbus, "Creating xbus proc directory\n");
1639 xbus->proc_xbus_dir = proc_mkdir(xbus->busname, xpp_proc_toplevel);
1640 if (!xbus->proc_xbus_dir) {
1641 XBUS_ERR(xbus, "Failed to create proc directory\n");
1642 err = -EIO;
1643 goto nobus;
1644 }
1645 xbus->proc_xbus_summary = proc_create_data(PROC_XBUS_SUMMARY, 0444,
1646 xbus->proc_xbus_dir,
1647 &xbus_read_proc_ops,
1648 (void *)((unsigned long)xbus->num));
1649 if (!xbus->proc_xbus_summary) {
1650 XBUS_ERR(xbus, "Failed to create proc file '%s'\n",
1651 PROC_XBUS_SUMMARY);
1652 err = -EIO;
1653 goto nobus;
1654 }
1655 #ifdef PROTOCOL_DEBUG
1656 xbus->proc_xbus_command = proc_create_data(PROC_XBUS_COMMAND, 0200,
1657 xbus->proc_xbus_dir,
1658 &proc_xbus_command_ops, xbus);
1659 if (!xbus->proc_xbus_command) {
1660 XBUS_ERR(xbus, "Failed to create proc file '%s'\n",
1661 PROC_XBUS_COMMAND);
1662 err = -EIO;
1663 goto nobus;
1664 }
1665 #endif
1666 #endif
1667 xframe_queue_init(&xbus->command_queue, 10, command_queue_length,
1668 "command_queue", xbus);
1669 xframe_queue_init(&xbus->receive_queue, 10, 50, "receive_queue", xbus);
1670 xframe_queue_init(&xbus->send_pool, 10, 100, "send_pool", xbus);
1671 xframe_queue_init(&xbus->receive_pool, 10, 50, "receive_pool", xbus);
1672 xframe_queue_init(&xbus->pcm_tospan, 5, 10, "pcm_tospan", xbus);
1673 tasklet_init(&xbus->receive_tasklet, receive_tasklet_func,
1674 (unsigned long)xbus);
1675 /*
1676 * Create worker after /proc/XBUS-?? so the directory exists
1677 * before /proc/XBUS-??/waitfor_xpds tries to get created.
1678 */
1679 if (!worker_run(xbus)) {
1680 ERR("Failed to allocate worker\n");
1681 goto nobus;
1682 }
1683 return xbus;
1684 nobus:
1685 xbus_free(xbus);
1686 return NULL;
1687 }
1688 EXPORT_SYMBOL(xbus_new);
1689
1690 /*------------------------- Proc handling --------------------------*/
1691
1692 void xbus_reset_counters(xbus_t *xbus)
1693 {
1694 int i;
1695
1696 XBUS_DBG(GENERAL, xbus, "Reseting counters\n");
1697 for (i = 0; i < XBUS_COUNTER_MAX; i++)
1698 xbus->counters[i] = 0;
1699 }
1700 EXPORT_SYMBOL(xbus_reset_counters);
1701
1702 static bool xpds_done(xbus_t *xbus)
1703 {
1704 if (XBUS_IS(xbus, FAIL))
1705 return 1; /* Nothing to wait for */
1706 if (xbus->worker.xpds_init_done)
1707 return 1; /* All good */
1708 /* Keep waiting */
1709 return 0;
1710 }
1711
1712 int waitfor_xpds(xbus_t *xbus, char *buf)
1713 {
1714 struct xbus_workqueue *worker;
1715 unsigned long flags;
1716 int ret;
1717 int len = 0;
1718
1719 /*
1720 * FIXME: worker is created before ?????
1721 * So by now it exists and initialized.
1722 */
1723 worker = &xbus->worker;
1724 BUG_ON(!worker);
1725 if (!worker->wq) {
1726 XBUS_ERR(xbus, "Missing worker thread. Skipping.\n");
1727 len = -ENODEV;
1728 goto out;
1729 }
1730 XBUS_DBG(DEVICES, xbus,
1731 "Waiting for card init of XPDs max %d seconds\n",
1732 INITIALIZATION_TIMEOUT / HZ);
1733 ret =
1734 wait_event_interruptible_timeout(worker->
1735 wait_for_xpd_initialization,
1736 xpds_done(xbus),
1737 INITIALIZATION_TIMEOUT);
1738 if (ret == 0) {
1739 XBUS_ERR(xbus, "Card Initialization Timeout\n");
1740 len = -ETIMEDOUT;
1741 goto out;
1742 } else if (ret < 0) {
1743 XBUS_ERR(xbus, "Card Initialization Interrupted %d\n", ret);
1744 len = ret;
1745 goto out;
1746 } else
1747 XBUS_DBG(DEVICES, xbus,
1748 "Finished initialization of %d XPD's in %d seconds.\n",
1749 worker->num_units_initialized,
1750 (INITIALIZATION_TIMEOUT - ret) / HZ);
1751 if (XBUS_IS(xbus, FAIL)) {
1752 len += sprintf(buf, "FAILED: %s\n", xbus->busname);
1753 } else {
1754 spin_lock_irqsave(&xbus->lock, flags);
1755 len +=
1756 sprintf(buf, "XPDS_READY: %s: %d/%d\n", xbus->busname,
1757 worker->num_units_initialized, worker->num_units);
1758 spin_unlock_irqrestore(&xbus->lock, flags);
1759 }
1760 out:
1761 return len;
1762 }
1763
1764 #ifdef CONFIG_PROC_FS
1765
1766 static void xbus_fill_proc_queue(struct seq_file *sfile, struct xframe_queue *q)
1767 {
1768 seq_printf(sfile,
1769 "%-15s: counts %3d, %3d, %3d worst %3d, overflows %3d "
1770 "worst_lag %02ld.%ld ms\n",
1771 q->name, q->steady_state_count, q->count, q->max_count,
1772 q->worst_count, q->overflows, q->worst_lag_usec / 1000,
1773 q->worst_lag_usec % 1000);
1774 xframe_queue_clearstats(q);
1775 }
1776
1777 static int xbus_proc_show(struct seq_file *sfile, void *data)
1778 {
1779 xbus_t *xbus;
1780 unsigned long flags;
1781 int len = 0;
1782 int i = (int)((unsigned long)sfile->private);
1783
1784 xbus = get_xbus(__func__, i); /* until end of xbus_proc_show() */
1785 if (!xbus)
1786 return -EINVAL;
1787 spin_lock_irqsave(&xbus->lock, flags);
1788
1789 seq_printf(sfile, "%s: CONNECTOR=%s LABEL=[%s] STATUS=%s\n",
1790 xbus->busname, xbus->connector, xbus->label,
1791 (XBUS_FLAGS(xbus, CONNECTED)) ? "connected" : "missing");
1792 xbus_fill_proc_queue(sfile, &xbus->send_pool);
1793 xbus_fill_proc_queue(sfile, &xbus->receive_pool);
1794 xbus_fill_proc_queue(sfile, &xbus->command_queue);
1795 xbus_fill_proc_queue(sfile, &xbus->receive_queue);
1796 xbus_fill_proc_queue(sfile, &xbus->pcm_tospan);
1797 if (rx_tasklet) {
1798 seq_printf(sfile, "\ncpu_rcv_intr: ");
1799 for_each_online_cpu(i)
1800 seq_printf(sfile, "%5d ", xbus->cpu_rcv_intr[i]);
1801 seq_printf(sfile, "\ncpu_rcv_tasklet: ");
1802 for_each_online_cpu(i)
1803 seq_printf(sfile, "%5d ", xbus->cpu_rcv_tasklet[i]);
1804 seq_printf(sfile, "\n");
1805 }
1806 seq_printf(sfile, "self_ticking: %d (last_tick at %ld)\n",
1807 xbus->self_ticking, xbus->ticker.last_sample.tv.tv_sec);
1808 seq_printf(sfile, "command_tick: %d\n",
1809 xbus->command_tick_counter);
1810 seq_printf(sfile, "usec_nosend: %d\n", xbus->usec_nosend);
1811 seq_printf(sfile, "xbus: pcm_rx_counter = %d, frag = %d\n",
1812 atomic_read(&xbus->pcm_rx_counter), xbus->xbus_frag_count);
1813 seq_printf(sfile, "max_rx_process = %2ld.%ld ms\n",
1814 xbus->max_rx_process / 1000, xbus->max_rx_process % 1000);
1815 xbus->max_rx_process = 0;
1816 seq_printf(sfile, "\nTRANSPORT: max_send_size=%d refcount=%d\n",
1817 MAX_SEND_SIZE(xbus),
1818 atomic_read(&xbus->transport.transport_refcount)
1819 );
1820 seq_printf(sfile, "PCM Metrices:\n");
1821 seq_printf(sfile, "\tPCM TX: min=%ld max=%ld\n",
1822 xbus->min_tx_sync, xbus->max_tx_sync);
1823 seq_printf(sfile, "\tPCM RX: min=%ld max=%ld\n",
1824 xbus->min_rx_sync, xbus->max_rx_sync);
1825 seq_printf(sfile, "COUNTERS:\n");
1826 for (i = 0; i < XBUS_COUNTER_MAX; i++) {
1827 seq_printf(sfile, "\t%-15s = %d\n", xbus_counters[i].name,
1828 xbus->counters[i]);
1829 }
1830 seq_printf(sfile, "<-- len=%d\n", len);
1831 spin_unlock_irqrestore(&xbus->lock, flags);
1832 put_xbus(__func__, xbus); /* from xbus_proc_show() */
1833 return 0;
1834
1835 }
1836
1837 static int xbus_read_proc_open(struct inode *inode, struct file *file)
1838 {
1839 return single_open(file, xbus_proc_show, PDE_DATA(inode));
1840 }
1841
1842 static const struct file_operations xbus_read_proc_ops = {
1843 .owner = THIS_MODULE,
1844 .open = xbus_read_proc_open,
1845 .read = seq_read,
1846 .llseek = seq_lseek,
1847 .release = single_release,
1848 };
1849
1850 #ifdef PROTOCOL_DEBUG
1851 static ssize_t proc_xbus_command_write(struct file *file,
1852 const char __user *buffer, size_t count, loff_t *offset)
1853 {
1854 char *buf;
1855 xbus_t *xbus = file->private_data;
1856 char *p;
1857 __u8 *pack_start;
1858 __u8 *q;
1859 xframe_t *xframe;
1860 size_t len;
1861 const size_t max_len = xbus->transport.max_send_size;
1862 const size_t max_text = max_len * 3 + 10;
1863
1864 if (count > max_text) {
1865 XBUS_ERR(xbus, "%s: line too long (%zd > %zd)\n", __func__,
1866 count, max_len);
1867 return -EFBIG;
1868 }
1869 /* 3 bytes per hex-digit and space */
1870 buf = kmalloc(max_text, GFP_KERNEL);
1871 if (!buf)
1872 return -ENOMEM;
1873 if (copy_from_user(buf, buffer, count)) {
1874 count = -EINVAL;
1875 goto out;
1876 }
1877 buf[count] = '\0';
1878 XBUS_DBG(GENERAL, xbus, "count=%zd\n", count);
1879 /*
1880 * We replace the content of buf[] from
1881 * ascii representation to packet content
1882 * as the binary representation is shorter
1883 */
1884 q = pack_start = buf;
1885 for (p = buf; *p;) {
1886 int val;
1887 char hexdigit[3];
1888
1889 /* skip whitespace */
1890 while (*p && isspace(*p))
1891 p++;
1892 if (!(*p))
1893 break;
1894 if (!isxdigit(*p)) {
1895 XBUS_ERR(xbus,
1896 "%s: bad hex value ASCII='0x%X' "
1897 "at position %ld\n",
1898 __func__, *p, (long)(p - buf));
1899 count = -EINVAL;
1900 goto out;
1901 }
1902 hexdigit[0] = *p++;
1903 hexdigit[1] = '\0';
1904 hexdigit[2] = '\0';
1905 if (isxdigit(*p))
1906 hexdigit[1] = *p++;
1907 if (sscanf(hexdigit, "%2X", &val) != 1) {
1908 XBUS_ERR(xbus,
1909 "%s: bad hex value '%s' at position %ld\n",
1910 __func__, hexdigit, (long)(p - buf));
1911 count = -EINVAL;
1912 goto out;
1913 }
1914 *q++ = val;
1915 XBUS_DBG(GENERAL, xbus, "%3zd> '%s' val=%d\n", q - pack_start,
1916 hexdigit, val);
1917 }
1918 len = q - pack_start;
1919 xframe = ALLOC_SEND_XFRAME(xbus);
1920 if (!xframe) {
1921 count = -ENOMEM;
1922 goto out;
1923 }
1924 if (len > max_len)
1925 len = max_len;
1926 atomic_set(&xframe->frame_len, len);
1927 memcpy(xframe->packets, pack_start, len); /* FIXME: checksum? */
1928 dump_xframe("COMMAND", xbus, xframe, debug);
1929 send_cmd_frame(xbus, xframe);
1930 out:
1931 kfree(buf);
1932 return count;
1933 }
1934
1935 static int proc_xbus_command_open(struct inode *inode, struct file *file)
1936 {
1937 file->private_data = PDE_DATA(inode);
1938 return 0;
1939 }
1940
1941 static const struct file_operations proc_xbus_command_ops = {
1942 .owner = THIS_MODULE,
1943 .open = proc_xbus_command_open,
1944 .write = proc_xbus_command_write,
1945 };
1946 #endif
1947
1948 static int xpp_proc_read_show(struct seq_file *sfile, void *data)
1949 {
1950 int i;
1951
1952 for (i = 0; i < MAX_BUSES; i++) {
1953 xbus_t *xbus = get_xbus(__func__, i);
1954
1955 if (xbus) {
1956 seq_printf(sfile,
1957 "%s: CONNECTOR=%s LABEL=[%s] STATUS=%s\n",
1958 xbus->busname, xbus->connector, xbus->label,
1959 (XBUS_FLAGS(xbus, CONNECTED)) ? "connected"
1960 : "missing");
1961 put_xbus(__func__, xbus);
1962 }
1963 }
1964 #if 0
1965 seq_printf(sfile, "<-- len=%d\n", len);
1966 #endif
1967 return 0;
1968 }
1969
1970 static int xpp_proc_read_open(struct inode *inode, struct file *file)
1971 {
1972 return single_open(file, xpp_proc_read_show, PDE_DATA(inode));
1973 }
1974
1975 static const struct file_operations xpp_proc_read_ops = {
1976 .owner = THIS_MODULE,
1977 .open = xpp_proc_read_open,
1978 .read = seq_read,
1979 .llseek = seq_lseek,
1980 .release = single_release,
1981 };
1982
1983 #endif
1984
1985 static void transport_init(xbus_t *xbus, struct xbus_ops *ops,
1986 ushort max_send_size,
1987 struct device *transport_device, void *priv)
1988 {
1989 BUG_ON(!xbus);
1990 BUG_ON(!ops);
1991 BUG_ON(!ops->xframe_send_pcm);
1992 BUG_ON(!ops->xframe_send_cmd);
1993 BUG_ON(!ops->alloc_xframe);
1994 BUG_ON(!ops->free_xframe);
1995 xbus->transport.ops = ops;
1996 xbus->transport.max_send_size = max_send_size;
1997 xbus->transport.transport_device = transport_device;
1998 xbus->transport.priv = priv;
1999 xbus->transport.xbus_state = XBUS_STATE_START;
2000 spin_lock_init(&xbus->transport.state_lock);
2001 spin_lock_init(&xbus->transport.lock);
2002 atomic_set(&xbus->transport.transport_refcount, 0);
2003 xbus_setflags(xbus, XBUS_FLAG_CONNECTED, 0);
2004 init_waitqueue_head(&xbus->transport.transport_unused);
2005 }
2006
2007 static void transport_destroy(xbus_t *xbus)
2008 {
2009 int ret;
2010
2011 BUG_ON(!xbus);
2012 XBUS_DBG(DEVICES, xbus, "Waiting... (transport_refcount=%d)\n",
2013 atomic_read(&xbus->transport.transport_refcount));
2014 ret =
2015 wait_event_interruptible(xbus->transport.transport_unused,
2016 atomic_read(&xbus->transport.
2017 transport_refcount) == 0);
2018 if (ret)
2019 XBUS_ERR(xbus,
2020 "Waiting for transport_refcount interrupted!!!\n");
2021 xbus->transport.ops = NULL;
2022 xbus->transport.priv = NULL;
2023 }
2024
2025 struct xbus_ops *transportops_get(xbus_t *xbus)
2026 {
2027 struct xbus_ops *ops;
2028
2029 BUG_ON(!xbus);
2030 atomic_inc(&xbus->transport.transport_refcount);
2031 ops = xbus->transport.ops;
2032 if (!ops)
2033 atomic_dec(&xbus->transport.transport_refcount);
2034 /* fall through */
2035 return ops;
2036 }
2037 EXPORT_SYMBOL(transportops_get);
2038
2039 void transportops_put(xbus_t *xbus)
2040 {
2041 struct xbus_ops *ops;
2042
2043 BUG_ON(!xbus);
2044 ops = xbus->transport.ops;
2045 BUG_ON(!ops);
2046 if (atomic_dec_and_test(&xbus->transport.transport_refcount))
2047 wake_up(&xbus->transport.transport_unused);
2048 }
2049 EXPORT_SYMBOL(transportops_put);
2050
2051 /*------------------------- Initialization -------------------------*/
2052 static void xbus_core_cleanup(void)
2053 {
2054 finalize_xbuses_array();
2055 #ifdef CONFIG_PROC_FS
2056 if (proc_xbuses) {
2057 DBG(PROC, "Removing " PROC_XBUSES " from proc\n");
2058 remove_proc_entry(PROC_XBUSES, xpp_proc_toplevel);
2059 proc_xbuses = NULL;
2060 }
2061 #endif
2062 }
2063
2064 int __init xbus_core_init(void)
2065 {
2066 int ret = 0;
2067
2068 if (dahdi_autoreg == 1) {
2069 NOTICE("WARNING: The dahdi_autoreg parameter is deprecated " \
2070 "-- just set dahdi.auto_assign_spans=0\n");
2071 }
2072 initialize_xbuses_array();
2073 #ifdef PROTOCOL_DEBUG
2074 INFO("FEATURE: with PROTOCOL_DEBUG\n");
2075 #endif
2076 #ifdef CONFIG_PROC_FS
2077 proc_xbuses = proc_create_data(PROC_XBUSES, 0444, xpp_proc_toplevel,
2078 &xpp_proc_read_ops, NULL);
2079 if (!proc_xbuses) {
2080 ERR("Failed to create proc file %s\n", PROC_XBUSES);
2081 ret = -EFAULT;
2082 goto err;
2083 }
2084 #endif
2085 if ((ret = xpp_driver_init()) < 0)
2086 goto err;
2087 return 0;
2088 err:
2089 xbus_core_cleanup();
2090 return ret;
2091 }
2092
2093 void xbus_core_shutdown(void)
2094 {
2095 xbus_core_cleanup();
2096 xpp_driver_exit();
2097 }