Debian Codegraph @

drivers/dahdi/xpp/xproto.c

raw symbols SCIP
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
23 #include "xpd.h"
24 #include "xproto.h"
25 #include "xpp_dahdi.h"
26 #include "xbus-core.h"
27 #include "dahdi_debug.h"
28 #include <linux/module.h>
29 #include <linux/delay.h>
30
31 static const char rcsid[] = "$Id$";
32
33 extern int debug;
34
35 static const xproto_table_t *xprotocol_tables[XPD_TYPE_NOMODULE];
36
37 #if MAX_UNIT*MAX_SUBUNIT > MAX_XPDS
38 #error MAX_XPDS is too small
39 #endif
40
41 bool valid_xpd_addr(const struct xpd_addr *addr)
42 {
43 return ((addr->subunit & ~BITMASK(SUBUNIT_BITS)) == 0)
44 && ((addr->unit & ~BITMASK(UNIT_BITS)) == 0);
45 }
46 EXPORT_SYMBOL(valid_xpd_addr);
47
48 /*------ General Protocol Management ----------------------------*/
49
50 const xproto_entry_t *xproto_card_entry(const xproto_table_t *table,
51 __u8 opcode)
52 {
53 const xproto_entry_t *xe;
54
55 //DBG(GENERAL, "\n");
56 xe = &table->entries[opcode];
57 return (xe->handler != NULL) ? xe : NULL;
58 }
59 EXPORT_SYMBOL(xproto_card_entry);
60
61 const xproto_entry_t *xproto_global_entry(__u8 opcode)
62 {
63 const xproto_entry_t *xe;
64
65 xe = xproto_card_entry(&PROTO_TABLE(GLOBAL), opcode);
66 //DBG(GENERAL, "opcode=0x%X xe=%p\n", opcode, xe);
67 return xe;
68 }
69 EXPORT_SYMBOL(xproto_global_entry);
70
71 xproto_handler_t xproto_global_handler(__u8 opcode)
72 {
73 return xproto_card_handler(&PROTO_TABLE(GLOBAL), opcode);
74 }
75
76 static const xproto_table_t *xproto_table(xpd_type_t cardtype)
77 {
78 if (cardtype >= XPD_TYPE_NOMODULE)
79 return NULL;
80 return xprotocol_tables[cardtype];
81 }
82
83 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0) || \
84 LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
85 #define MODULE_REFCOUNT_FORMAT "%s refcount was %d\n"
86 #else
87 #define MODULE_REFCOUNT_FORMAT "%s refcount was %lu\n"
88 #endif
89
90 const xproto_table_t *xproto_get(xpd_type_t cardtype)
91 {
92 const xproto_table_t *xtable;
93
94 if (cardtype >= XPD_TYPE_NOMODULE)
95 return NULL;
96 xtable = xprotocol_tables[cardtype];
97 if (!xtable) { /* Try to load the relevant module */
98 int ret = request_module(XPD_TYPE_PREFIX "%d", cardtype);
99 if (ret != 0) {
100 NOTICE("%s: Failed to load module for type=%d. "
101 "exit status=%d.\n",
102 __func__, cardtype, ret);
103 /* Drop through: we may be lucky... */
104 }
105 xtable = xprotocol_tables[cardtype];
106 }
107 if (xtable) {
108 BUG_ON(!xtable->owner);
109 #ifdef CONFIG_MODULE_UNLOAD
110 DBG(GENERAL, MODULE_REFCOUNT_FORMAT, xtable->name,
111 module_refcount(xtable->owner));
112 #endif
113 if (!try_module_get(xtable->owner)) {
114 ERR("%s: try_module_get for %s failed.\n", __func__,
115 xtable->name);
116 return NULL;
117 }
118 }
119 return xtable;
120 }
121
122 void xproto_put(const xproto_table_t *xtable)
123 {
124 BUG_ON(!xtable);
125 #ifdef CONFIG_MODULE_UNLOAD
126 DBG(GENERAL, MODULE_REFCOUNT_FORMAT, xtable->name,
127 module_refcount(xtable->owner));
128 BUG_ON(module_refcount(xtable->owner) <= 0);
129 #endif
130 module_put(xtable->owner);
131 }
132
133 xproto_handler_t xproto_card_handler(const xproto_table_t *table,
134 __u8 opcode)
135 {
136 const xproto_entry_t *xe;
137
138 //DBG(GENERAL, "\n");
139 xe = xproto_card_entry(table, opcode);
140 return xe->handler;
141 }
142
143 void notify_bad_xpd(const char *funcname, xbus_t *xbus,
144 const struct xpd_addr addr, const char *msg)
145 {
146 XBUS_NOTICE(xbus, "%s: non-existing address (%1d%1d): %s\n", funcname,
147 addr.unit, addr.subunit, msg);
148 }
149 EXPORT_SYMBOL(notify_bad_xpd);
150
151 static int packet_process(xbus_t *xbus, xpacket_t *pack)
152 {
153 __u8 op;
154 const xproto_entry_t *xe;
155 xproto_handler_t handler;
156 xproto_table_t *table;
157 xpd_t *xpd;
158 int ret = -EPROTO;
159
160 BUG_ON(!pack);
161 if (!valid_xpd_addr(&XPACKET_ADDR(pack))) {
162 if (printk_ratelimit()) {
163 XBUS_NOTICE(xbus, "%s: from %d%d: bad address.\n",
164 __func__, XPACKET_ADDR_UNIT(pack),
165 XPACKET_ADDR_SUBUNIT(pack));
166 dump_packet("packet_process -- bad address", pack,
167 debug);
168 }
169 goto out;
170 }
171 op = XPACKET_OP(pack);
172 xpd =
173 xpd_byaddr(xbus, XPACKET_ADDR_UNIT(pack),
174 XPACKET_ADDR_SUBUNIT(pack));
175 /* XPD may be NULL (e.g: during bus polling */
176 xe = xproto_global_entry(op);
177 /*-------- Validations -----------*/
178 if (!xe) {
179 const xproto_table_t *xtable;
180
181 if (!xpd) {
182 if (printk_ratelimit()) {
183 XBUS_NOTICE(xbus,
184 "%s: from %d%d opcode=0x%02X: "
185 "no such global command.\n",
186 __func__, XPACKET_ADDR_UNIT(pack),
187 XPACKET_ADDR_SUBUNIT(pack), op);
188 dump_packet
189 ("packet_process -- no such global command",
190 pack, 1);
191 }
192 goto out;
193 }
194 xtable = xproto_table(xpd->xpd_type);
195 if (!xtable) {
196 if (printk_ratelimit())
197 XPD_ERR(xpd,
198 "%s: no protocol table (xpd_type=%d)\n",
199 __func__, xpd->xpd_type);
200 goto out;
201 }
202 xe = xproto_card_entry(xtable, op);
203 if (!xe) {
204 if (printk_ratelimit()) {
205 XPD_NOTICE(xpd,
206 "%s: bad command (xpd_type=%d,opcode=0x%x)\n",
207 __func__, xpd->xpd_type, op);
208 dump_packet("packet_process -- bad command",
209 pack, 1);
210 }
211 goto out;
212 }
213 }
214 table = xe->table;
215 BUG_ON(!table);
216 if (!table->packet_is_valid(pack)) {
217 if (printk_ratelimit()) {
218 ERR("xpp: %s: wrong size %d for opcode=0x%02X\n",
219 __func__, XPACKET_LEN(pack), op);
220 dump_packet("packet_process -- wrong size", pack,
221 debug);
222 }
223 goto out;
224 }
225 ret = 0; /* All well */
226 handler = xe->handler;
227 BUG_ON(!handler);
228 XBUS_COUNTER(xbus, RX_BYTES) += XPACKET_LEN(pack);
229 handler(xbus, xpd, xe, pack);
230 out:
231 return ret;
232 }
233
234 static int xframe_receive_cmd(xbus_t *xbus, xframe_t *xframe)
235 {
236 __u8 *xframe_end;
237 xpacket_t *pack;
238 __u8 *p;
239 int len;
240 int ret;
241
242 if (debug & DBG_COMMANDS)
243 dump_xframe("RX-CMD", xbus, xframe, DBG_ANY);
244 p = xframe->packets;
245 xframe_end = p + XFRAME_LEN(xframe);
246 do {
247 pack = (xpacket_t *)p;
248 len = XPACKET_LEN(pack);
249 /* Sanity checks */
250 if (unlikely(XPACKET_OP(pack) == XPROTO_NAME(GLOBAL, PCM_READ))) {
251 static int rate_limit;
252
253 if ((rate_limit++ % 1003) == 0) {
254 XBUS_DBG(GENERAL, xbus,
255 "A PCM packet within a Non-PCM xframe\n");
256 dump_xframe("In Non-PCM xframe",
257 xbus, xframe, debug);
258 }
259 ret = -EPROTO;
260 goto out;
261 }
262 p += len;
263 if (p > xframe_end || len < RPACKET_HEADERSIZE) {
264 static int rate_limit;
265
266 if ((rate_limit++ % 1003) == 0) {
267 XBUS_NOTICE(xbus, "Invalid packet length %d\n",
268 len);
269 dump_xframe("BAD LENGTH", xbus, xframe, debug);
270 }
271 ret = -EPROTO;
272 goto out;
273 }
274 ret = packet_process(xbus, pack);
275 if (unlikely(ret < 0))
276 break;
277 } while (p < xframe_end);
278 out:
279 FREE_RECV_XFRAME(xbus, xframe);
280 return ret;
281 }
282
283 int xframe_receive(xbus_t *xbus, xframe_t *xframe)
284 {
285 int ret = 0;
286 ktime_t kt_received;
287 s64 usec;
288
289 if (XFRAME_LEN(xframe) < RPACKET_HEADERSIZE) {
290 static int rate_limit;
291
292 if ((rate_limit++ % 1003) == 0) {
293 XBUS_NOTICE(xbus, "short xframe\n");
294 dump_xframe("short xframe", xbus, xframe, debug);
295 }
296 FREE_RECV_XFRAME(xbus, xframe);
297 return -EPROTO;
298 }
299 if (!XBUS_FLAGS(xbus, CONNECTED)) {
300 XBUS_DBG(GENERAL, xbus, "Dropped xframe. Is shutting down.\n");
301 return -ENODEV;
302 }
303 kt_received = xframe->kt_received;
304 /*
305 * We want to check that xframes do not mix PCM and other commands
306 */
307 if (XPACKET_IS_PCM((xpacket_t *)xframe->packets)) {
308 if (!XBUS_IS(xbus, READY))
309 FREE_RECV_XFRAME(xbus, xframe);
310 else
311 xframe_receive_pcm(xbus, xframe);
312 } else {
313 XBUS_COUNTER(xbus, RX_CMD)++;
314 ret = xframe_receive_cmd(xbus, xframe);
315 }
316 /* Calculate total processing time */
317 usec = ktime_us_delta(ktime_get(), kt_received);
318 if (usec > xbus->max_rx_process)
319 xbus->max_rx_process = usec;
320 return ret;
321 }
322 EXPORT_SYMBOL(xframe_receive);
323
324 #define VERBOSE_DEBUG 1
325 #define ERR_REPORT_LIMIT 20
326
327 void dump_packet(const char *msg, const xpacket_t *packet, bool debug)
328 {
329 __u8 op = XPACKET_OP(packet);
330 __u8 *addr = (__u8 *)&XPACKET_ADDR(packet);
331
332 if (!debug)
333 return;
334 printk(KERN_DEBUG "%s: XPD=%1X-%1X%c (0x%X) OP=0x%02X LEN=%d", msg,
335 XPACKET_ADDR_UNIT(packet), XPACKET_ADDR_SUBUNIT(packet),
336 (XPACKET_ADDR_SYNC(packet)) ? '+' : ' ', *addr, op,
337 XPACKET_LEN(packet));
338 #if VERBOSE_DEBUG
339 {
340 int i;
341 __u8 *p = (__u8 *)packet;
342
343 printk(" BYTES: ");
344 for (i = 0; i < XPACKET_LEN(packet); i++) {
345 static int limiter;
346
347 if (i >= sizeof(xpacket_t)) {
348 if (limiter < ERR_REPORT_LIMIT) {
349 ERR("%s: length overflow "
350 "i=%d > sizeof(xpacket_t)=%lu\n",
351 __func__, i + 1,
352 (long)sizeof(xpacket_t));
353 } else if (limiter == ERR_REPORT_LIMIT) {
354 ERR("%s: error packet #%d... "
355 "squelsh reports.\n",
356 __func__, limiter);
357 }
358 limiter++;
359 break;
360 }
361 if (debug)
362 printk("%02X ", p[i]);
363 }
364 }
365 #endif
366 printk("\n");
367 }
368 EXPORT_SYMBOL(dump_packet);
369
370 void dump_reg_cmd(const char msg[], bool writing, xbus_t *xbus,
371 __u8 unit, xportno_t port, const reg_cmd_t *regcmd)
372 {
373 char action;
374 char modifier;
375 char port_buf[MAX_PROC_WRITE];
376 char reg_buf[MAX_PROC_WRITE];
377 char data_buf[MAX_PROC_WRITE];
378
379 /* The size byte is not included */
380 if (regcmd->h.bytes > sizeof(*regcmd) - 1) {
381 PORT_NOTICE(xbus, unit, port,
382 "%s: %s: Too long: regcmd->bytes = %d\n", __func__,
383 msg, regcmd->h.bytes);
384 return;
385 }
386 if (regcmd->h.bytes == REG_CMD_SIZE(RAM)) {
387 snprintf(port_buf, MAX_PROC_WRITE, "%d%s", regcmd->h.portnum,
388 (REG_FIELD_RAM(regcmd, all_ports_broadcast)) ? "*" : "");
389 if (REG_FIELD_RAM(regcmd, read_request)) {
390 action = 'R';
391 } else {
392 action = 'W';
393 }
394 PORT_DBG(REGS, xbus, unit, port,
395 "%s: %s %cR %02X %02X %02X %02X %02X %02X\n",
396 msg, port_buf, action,
397 REG_FIELD_RAM(regcmd, addr_low),
398 REG_FIELD_RAM(regcmd, addr_high),
399 REG_FIELD_RAM(regcmd, data_0),
400 REG_FIELD_RAM(regcmd, data_1),
401 REG_FIELD_RAM(regcmd, data_2),
402 REG_FIELD_RAM(regcmd, data_3));
403 return;
404 }
405 if (regcmd->h.is_multibyte) {
406 char buf[MAX_PROC_WRITE + 1];
407 int i;
408 int n = 0;
409 size_t len = regcmd->h.bytes;
410 const __u8 *p = REG_XDATA(regcmd);
411
412 buf[0] = '\0';
413 for (i = 0; i < len && n < MAX_PROC_WRITE; i++)
414 n += snprintf(&buf[n], MAX_PROC_WRITE - n, "%02X ",
415 p[i]);
416 PORT_DBG(REGS, xbus, unit, port,
417 "UNIT-%d PORT-%d: Multibyte(eoframe=%d) "
418 "%s[0..%zd]: %s%s\n",
419 unit, port, regcmd->h.eoframe, msg, len - 1, buf,
420 (n >= MAX_PROC_WRITE) ? "..." : "");
421 return;
422 }
423 /* The size byte is not included */
424 if (regcmd->h.bytes != REG_CMD_SIZE(REG)) {
425 PORT_NOTICE(xbus, unit, port,
426 "%s: %s: Wrong size: regcmd->bytes = %d\n",
427 __func__, msg, regcmd->h.bytes);
428 return;
429 }
430 snprintf(port_buf, MAX_PROC_WRITE, "%d%s", regcmd->h.portnum,
431 (REG_FIELD(regcmd, all_ports_broadcast)) ? "*" : "");
432 action = (REG_FIELD(regcmd, read_request)) ? 'R' : 'W';
433 modifier = 'D';
434 if (REG_FIELD(regcmd, do_subreg)) {
435 snprintf(reg_buf, MAX_PROC_WRITE, "%02X %02X",
436 REG_FIELD(regcmd, regnum), REG_FIELD(regcmd, subreg));
437 modifier = 'S';
438 } else {
439 snprintf(reg_buf, MAX_PROC_WRITE, "%02X",
440 REG_FIELD(regcmd, regnum));
441 }
442 if (REG_FIELD(regcmd, read_request)) {
443 data_buf[0] = '\0';
444 } else if (REG_FIELD(regcmd, do_datah)) {
445 snprintf(data_buf, MAX_PROC_WRITE, "%02X %02X",
446 REG_FIELD(regcmd, data_low), REG_FIELD(regcmd,
447 data_high));
448 modifier = 'I';
449 } else {
450 snprintf(data_buf, MAX_PROC_WRITE, "%02X",
451 REG_FIELD(regcmd, data_low));
452 }
453 PORT_DBG(REGS, xbus, unit, port, "%s: %s %c%c %s %s\n", msg, port_buf,
454 action, modifier, reg_buf, data_buf);
455 }
456 EXPORT_SYMBOL(dump_reg_cmd);
457
458 const char *xproto_name(xpd_type_t xpd_type)
459 {
460 const xproto_table_t *proto_table;
461
462 BUG_ON(xpd_type >= XPD_TYPE_NOMODULE);
463 proto_table = xprotocol_tables[xpd_type];
464 if (!proto_table)
465 return NULL;
466 return proto_table->name;
467 }
468 EXPORT_SYMBOL(xproto_name);
469
470 #define CHECK_XOP(xops, f) \
471 if (!(xops)->f) { \
472 ERR("%s: missing xmethod %s [%s (%d)]\n", \
473 __func__, #f, name, type); \
474 return -EINVAL; \
475 }
476
477 #define CHECK_PHONEOP(phoneops, f) \
478 if (!(phoneops)->f) { \
479 ERR("%s: missing phone method %s [%s (%d)]\n", \
480 __func__, #f, name, type); \
481 return -EINVAL; \
482 }
483
484 int xproto_register(const xproto_table_t *proto_table)
485 {
486 int type;
487 const char *name;
488 const struct xops *xops;
489 const struct phoneops *phoneops;
490
491 BUG_ON(!proto_table);
492 type = proto_table->type;
493 name = proto_table->name;
494 if (type >= XPD_TYPE_NOMODULE) {
495 NOTICE("%s: Bad xproto type %d\n", __func__, type);
496 return -EINVAL;
497 }
498 DBG(GENERAL, "%s (%d)\n", name, type);
499 if (xprotocol_tables[type])
500 NOTICE("%s: overriding registration of %s (%d)\n", __func__,
501 name, type);
502 xops = proto_table->xops;
503 CHECK_XOP(xops, card_new);
504 CHECK_XOP(xops, card_init);
505 CHECK_XOP(xops, card_remove);
506 CHECK_XOP(xops, card_tick);
507 CHECK_XOP(xops, card_register_reply);
508
509 phoneops = proto_table->phoneops;
510 if (phoneops) {
511 CHECK_PHONEOP(phoneops, card_pcm_recompute);
512 CHECK_PHONEOP(phoneops, card_pcm_fromspan);
513 CHECK_PHONEOP(phoneops, card_pcm_tospan);
514 CHECK_PHONEOP(phoneops, echocancel_timeslot);
515 CHECK_PHONEOP(phoneops, echocancel_setmask);
516 CHECK_PHONEOP(phoneops, card_dahdi_preregistration);
517 CHECK_PHONEOP(phoneops, card_dahdi_postregistration);
518 /* optional method -- call after testing: */
519 /*CHECK_PHONEOP(phoneops, card_ioctl); */
520 }
521
522 xprotocol_tables[type] = proto_table;
523 return 0;
524 }
525 EXPORT_SYMBOL(xproto_register);
526
527 void xproto_unregister(const xproto_table_t *proto_table)
528 {
529 int type;
530 const char *name;
531
532 BUG_ON(!proto_table);
533 type = proto_table->type;
534 name = proto_table->name;
535 DBG(GENERAL, "%s (%d)\n", name, type);
536 if (type >= XPD_TYPE_NOMODULE) {
537 NOTICE("%s: Bad xproto type %s (%d)\n", __func__, name, type);
538 return;
539 }
540 if (!xprotocol_tables[type])
541 NOTICE("%s: xproto type %s (%d) is already unregistered\n",
542 __func__, name, type);
543 xprotocol_tables[type] = NULL;
544 }
545 EXPORT_SYMBOL(xproto_unregister);