Debian Codegraph @

drivers/dahdi/xpp/xbus-core.c

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