Debian Codegraph @

drivers/dahdi/xpp/xproto.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
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 struct timeval now;
287 struct timeval tv_received;
288 int usec;
289
290 if (XFRAME_LEN(xframe) < RPACKET_HEADERSIZE) {
291 static int rate_limit;
292
293 if ((rate_limit++ % 1003) == 0) {
294 XBUS_NOTICE(xbus, "short xframe\n");
295 dump_xframe("short xframe", xbus, xframe, debug);
296 }
297 FREE_RECV_XFRAME(xbus, xframe);
298 return -EPROTO;
299 }
300 if (!XBUS_FLAGS(xbus, CONNECTED)) {
301 XBUS_DBG(GENERAL, xbus, "Dropped xframe. Is shutting down.\n");
302 return -ENODEV;
303 }
304 tv_received = xframe->tv_received;
305 /*
306 * We want to check that xframes do not mix PCM and other commands
307 */
308 if (XPACKET_IS_PCM((xpacket_t *)xframe->packets)) {
309 if (!XBUS_IS(xbus, READY))
310 FREE_RECV_XFRAME(xbus, xframe);
311 else
312 xframe_receive_pcm(xbus, xframe);
313 } else {
314 XBUS_COUNTER(xbus, RX_CMD)++;
315 ret = xframe_receive_cmd(xbus, xframe);
316 }
317 /* Calculate total processing time */
318 do_gettimeofday(&now);
319 usec =
320 (now.tv_sec - tv_received.tv_sec) * 1000000 + now.tv_usec -
321 tv_received.tv_usec;
322 if (usec > xbus->max_rx_process)
323 xbus->max_rx_process = usec;
324 return ret;
325 }
326 EXPORT_SYMBOL(xframe_receive);
327
328 #define VERBOSE_DEBUG 1
329 #define ERR_REPORT_LIMIT 20
330
331 void dump_packet(const char *msg, const xpacket_t *packet, bool debug)
332 {
333 __u8 op = XPACKET_OP(packet);
334 __u8 *addr = (__u8 *)&XPACKET_ADDR(packet);
335
336 if (!debug)
337 return;
338 printk(KERN_DEBUG "%s: XPD=%1X-%1X%c (0x%X) OP=0x%02X LEN=%d", msg,
339 XPACKET_ADDR_UNIT(packet), XPACKET_ADDR_SUBUNIT(packet),
340 (XPACKET_ADDR_SYNC(packet)) ? '+' : ' ', *addr, op,
341 XPACKET_LEN(packet));
342 #if VERBOSE_DEBUG
343 {
344 int i;
345 __u8 *p = (__u8 *)packet;
346
347 printk(" BYTES: ");
348 for (i = 0; i < XPACKET_LEN(packet); i++) {
349 static int limiter;
350
351 if (i >= sizeof(xpacket_t)) {
352 if (limiter < ERR_REPORT_LIMIT) {
353 ERR("%s: length overflow "
354 "i=%d > sizeof(xpacket_t)=%lu\n",
355 __func__, i + 1,
356 (long)sizeof(xpacket_t));
357 } else if (limiter == ERR_REPORT_LIMIT) {
358 ERR("%s: error packet #%d... "
359 "squelsh reports.\n",
360 __func__, limiter);
361 }
362 limiter++;
363 break;
364 }
365 if (debug)
366 printk("%02X ", p[i]);
367 }
368 }
369 #endif
370 printk("\n");
371 }
372 EXPORT_SYMBOL(dump_packet);
373
374 void dump_reg_cmd(const char msg[], bool writing, xbus_t *xbus,
375 __u8 unit, xportno_t port, const reg_cmd_t *regcmd)
376 {
377 char action;
378 char modifier;
379 char port_buf[MAX_PROC_WRITE];
380 char reg_buf[MAX_PROC_WRITE];
381 char data_buf[MAX_PROC_WRITE];
382
383 /* The size byte is not included */
384 if (regcmd->h.bytes > sizeof(*regcmd) - 1) {
385 PORT_NOTICE(xbus, unit, port,
386 "%s: %s: Too long: regcmd->bytes = %d\n", __func__,
387 msg, regcmd->h.bytes);
388 return;
389 }
390 if (regcmd->h.bytes == REG_CMD_SIZE(RAM)) {
391 snprintf(port_buf, MAX_PROC_WRITE, "%d%s", regcmd->h.portnum,
392 (REG_FIELD_RAM(regcmd, all_ports_broadcast)) ? "*" : "");
393 if (REG_FIELD_RAM(regcmd, read_request)) {
394 action = 'R';
395 } else {
396 action = 'W';
397 }
398 PORT_DBG(REGS, xbus, unit, port,
399 "%s: %s %cR %02X %02X %02X %02X %02X %02X\n",
400 msg, port_buf, action,
401 REG_FIELD_RAM(regcmd, addr_low),
402 REG_FIELD_RAM(regcmd, addr_high),
403 REG_FIELD_RAM(regcmd, data_0),
404 REG_FIELD_RAM(regcmd, data_1),
405 REG_FIELD_RAM(regcmd, data_2),
406 REG_FIELD_RAM(regcmd, data_3));
407 return;
408 }
409 if (regcmd->h.is_multibyte) {
410 char buf[MAX_PROC_WRITE + 1];
411 int i;
412 int n = 0;
413 size_t len = regcmd->h.bytes;
414 const __u8 *p = REG_XDATA(regcmd);
415
416 buf[0] = '\0';
417 for (i = 0; i < len && n < MAX_PROC_WRITE; i++)
418 n += snprintf(&buf[n], MAX_PROC_WRITE - n, "%02X ",
419 p[i]);
420 PORT_DBG(REGS, xbus, unit, port,
421 "UNIT-%d PORT-%d: Multibyte(eoframe=%d) "
422 "%s[0..%zd]: %s%s\n",
423 unit, port, regcmd->h.eoframe, msg, len - 1, buf,
424 (n >= MAX_PROC_WRITE) ? "..." : "");
425 return;
426 }
427 /* The size byte is not included */
428 if (regcmd->h.bytes != REG_CMD_SIZE(REG)) {
429 PORT_NOTICE(xbus, unit, port,
430 "%s: %s: Wrong size: regcmd->bytes = %d\n",
431 __func__, msg, regcmd->h.bytes);
432 return;
433 }
434 snprintf(port_buf, MAX_PROC_WRITE, "%d%s", regcmd->h.portnum,
435 (REG_FIELD(regcmd, all_ports_broadcast)) ? "*" : "");
436 action = (REG_FIELD(regcmd, read_request)) ? 'R' : 'W';
437 modifier = 'D';
438 if (REG_FIELD(regcmd, do_subreg)) {
439 snprintf(reg_buf, MAX_PROC_WRITE, "%02X %02X",
440 REG_FIELD(regcmd, regnum), REG_FIELD(regcmd, subreg));
441 modifier = 'S';
442 } else {
443 snprintf(reg_buf, MAX_PROC_WRITE, "%02X",
444 REG_FIELD(regcmd, regnum));
445 }
446 if (REG_FIELD(regcmd, read_request)) {
447 data_buf[0] = '\0';
448 } else if (REG_FIELD(regcmd, do_datah)) {
449 snprintf(data_buf, MAX_PROC_WRITE, "%02X %02X",
450 REG_FIELD(regcmd, data_low), REG_FIELD(regcmd,
451 data_high));
452 modifier = 'I';
453 } else {
454 snprintf(data_buf, MAX_PROC_WRITE, "%02X",
455 REG_FIELD(regcmd, data_low));
456 }
457 PORT_DBG(REGS, xbus, unit, port, "%s: %s %c%c %s %s\n", msg, port_buf,
458 action, modifier, reg_buf, data_buf);
459 }
460 EXPORT_SYMBOL(dump_reg_cmd);
461
462 const char *xproto_name(xpd_type_t xpd_type)
463 {
464 const xproto_table_t *proto_table;
465
466 BUG_ON(xpd_type >= XPD_TYPE_NOMODULE);
467 proto_table = xprotocol_tables[xpd_type];
468 if (!proto_table)
469 return NULL;
470 return proto_table->name;
471 }
472 EXPORT_SYMBOL(xproto_name);
473
474 #define CHECK_XOP(xops, f) \
475 if (!(xops)->f) { \
476 ERR("%s: missing xmethod %s [%s (%d)]\n", \
477 __func__, #f, name, type); \
478 return -EINVAL; \
479 }
480
481 #define CHECK_PHONEOP(phoneops, f) \
482 if (!(phoneops)->f) { \
483 ERR("%s: missing phone method %s [%s (%d)]\n", \
484 __func__, #f, name, type); \
485 return -EINVAL; \
486 }
487
488 int xproto_register(const xproto_table_t *proto_table)
489 {
490 int type;
491 const char *name;
492 const struct xops *xops;
493 const struct phoneops *phoneops;
494
495 BUG_ON(!proto_table);
496 type = proto_table->type;
497 name = proto_table->name;
498 if (type >= XPD_TYPE_NOMODULE) {
499 NOTICE("%s: Bad xproto type %d\n", __func__, type);
500 return -EINVAL;
501 }
502 DBG(GENERAL, "%s (%d)\n", name, type);
503 if (xprotocol_tables[type])
504 NOTICE("%s: overriding registration of %s (%d)\n", __func__,
505 name, type);
506 xops = proto_table->xops;
507 CHECK_XOP(xops, card_new);
508 CHECK_XOP(xops, card_init);
509 CHECK_XOP(xops, card_remove);
510 CHECK_XOP(xops, card_tick);
511 CHECK_XOP(xops, card_register_reply);
512
513 phoneops = proto_table->phoneops;
514 if (phoneops) {
515 CHECK_PHONEOP(phoneops, card_pcm_recompute);
516 CHECK_PHONEOP(phoneops, card_pcm_fromspan);
517 CHECK_PHONEOP(phoneops, card_pcm_tospan);
518 CHECK_PHONEOP(phoneops, echocancel_timeslot);
519 CHECK_PHONEOP(phoneops, echocancel_setmask);
520 CHECK_PHONEOP(phoneops, card_dahdi_preregistration);
521 CHECK_PHONEOP(phoneops, card_dahdi_postregistration);
522 /* optional method -- call after testing: */
523 /*CHECK_PHONEOP(phoneops, card_ioctl); */
524 }
525
526 xprotocol_tables[type] = proto_table;
527 return 0;
528 }
529 EXPORT_SYMBOL(xproto_register);
530
531 void xproto_unregister(const xproto_table_t *proto_table)
532 {
533 int type;
534 const char *name;
535
536 BUG_ON(!proto_table);
537 type = proto_table->type;
538 name = proto_table->name;
539 DBG(GENERAL, "%s (%d)\n", name, type);
540 if (type >= XPD_TYPE_NOMODULE) {
541 NOTICE("%s: Bad xproto type %s (%d)\n", __func__, name, type);
542 return;
543 }
544 if (!xprotocol_tables[type])
545 NOTICE("%s: xproto type %s (%d) is already unregistered\n",
546 __func__, name, type);
547 xprotocol_tables[type] = NULL;
548 }
549 EXPORT_SYMBOL(xproto_unregister);