4 * Written by Oron Peled <oron@actcom.co.il>
5 * Copyright (C) 2004-2006, Xorcom
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/list.h>
29 #include <linux/proc_fs.h>
30 #include <dahdi/kernel.h>
33 * This must match the firmware protocol version
35 #define XPP_PROTOCOL_VERSION 30
37 struct unit_descriptor;
40 uint8_t subunit:SUBUNIT_BITS;
42 uint8_t unit:UNIT_BITS;
43 uint8_t sync_master:1;
46 #define MKADDR(p, u, s) do { \
49 (p)->sync_master = 0; \
52 struct xpacket_header {
53 uint16_t packet_len:10;
61 #define XPACKET_OP(p) ((p)->head.opcode)
62 #define XPACKET_LEN(p) ((p)->head.packet_len)
63 #define XPACKET_IS_PCM(p) ((p)->head.is_pcm)
64 #define XPACKET_PCMSLOT(p) ((p)->head.pcmslot)
65 #define XPACKET_RESERVED(p) ((p)->head.reserved)
66 #define XPACKET_ADDR(p) ((p)->head.addr)
67 #define XPACKET_ADDR_UNIT(p) (XPACKET_ADDR(p).unit)
68 #define XPACKET_ADDR_SUBUNIT(p) (XPACKET_ADDR(p).subunit)
69 #define XPACKET_ADDR_SYNC(p) (XPACKET_ADDR(p).sync_master)
70 #define XPACKET_ADDR_RESERVED(p) (XPACKET_ADDR(p).reserved)
72 #define PROTO_TABLE(n) n ## _protocol_table
75 * The LSB of the type number signifies:
79 #define XPD_TYPE_FXS 1 // TO_PHONE
80 #define XPD_TYPE_FXO 2 // TO_PSTN
81 #define XPD_TYPE_BRI 3 // TO_PSTN/TO_PHONE (from hardware)
82 #define XPD_TYPE_PRI 4 // TO_PSTN/TO_PHONE (runtime)
83 #define XPD_TYPE_ECHO 5 // Octasic echo canceller
84 #define XPD_TYPE_NOMODULE 7
86 typedef __u8 xpd_type_t;
88 #define XPD_TYPE_PREFIX "xpd-type-"
90 #define MODULE_ALIAS_XPD(type) \
91 MODULE_ALIAS(XPD_TYPE_PREFIX __stringify(type))
93 #define PCM_CHUNKSIZE (CHANNELS_PERXPD * 8) /* samples of 8 bytes */
95 bool valid_xpd_addr(const struct xpd_addr *addr);
97 #define XPROTO_NAME(card, op) card ## _ ## op
98 #define XPROTO_HANDLER(card, op) XPROTO_NAME(card, op ## _handler)
99 #define XPROTO_CALLER(card, op) XPROTO_NAME(card, op ## _send)
101 #define HANDLER_DEF(card, op) \
102 static int XPROTO_HANDLER(card, op) ( \
105 const xproto_entry_t *cmd, \
108 #define CALL_PROTO(card, op, ...) XPROTO_CALLER(card, op)(__VA_ARGS__)
110 #define DECLARE_CMD(card, op, ...) \
111 int CALL_PROTO(card, op, xbus_t *xbus, xpd_t *xpd, ## __VA_ARGS__)
113 #define HOSTCMD(card, op, ...) \
114 DECLARE_CMD(card, op, ## __VA_ARGS__)
116 #define RPACKET_NAME(card, op) XPROTO_NAME(RPACKET_ ## card, op)
117 #define RPACKET_TYPE(card, op) struct RPACKET_NAME(card, op)
119 #define DEF_RPACKET_DATA(card, op, ...) \
120 RPACKET_TYPE(card, op) { \
121 struct xpacket_header head; \
124 #define RPACKET_HEADERSIZE sizeof(struct xpacket_header)
125 #define RPACKET_FIELD(p, card, op, field) \
126 (((RPACKET_TYPE(card, op) *)(p))->field)
127 #define RPACKET_SIZE(card, op) sizeof(RPACKET_TYPE(card, op))
129 #define XENTRY(prototab, module, op) \
130 [ XPROTO_NAME(module, op) ] = { \
131 .handler = XPROTO_HANDLER(module, op), \
133 .table = &PROTO_TABLE(prototab) \
136 #define XPACKET_INIT(p, card, op, to, pcm, pcmslot) \
138 XPACKET_OP(p) = XPROTO_NAME(card, op); \
139 XPACKET_LEN(p) = RPACKET_SIZE(card, op); \
140 XPACKET_IS_PCM(p) = (pcm); \
141 XPACKET_PCMSLOT(p) = (pcmslot); \
142 XPACKET_RESERVED(p) = 0; \
143 XPACKET_ADDR_UNIT(p) = XBUS_UNIT(to); \
144 XPACKET_ADDR_SUBUNIT(p) = XBUS_SUBUNIT(to); \
145 XPACKET_ADDR_SYNC(p) = 0; \
146 XPACKET_ADDR_RESERVED(p) = 0; \
149 #define XFRAME_NEW_CMD(frm, p, xbus, card, op, to) \
151 int pack_len = RPACKET_SIZE(card, op); \
153 if (!XBUS_FLAGS(xbus, CONNECTED)) \
155 (frm) = ALLOC_SEND_XFRAME(xbus); \
158 (p) = xframe_next_packet(frm, pack_len); \
161 XPACKET_INIT(p, card, op, to, 0, 0); \
162 (frm)->usec_towait = 0; \
167 /*----------------- register handling --------------------------------*/
169 #define MULTIBYTE_MAX_LEN 5 /* FPGA firmware limitation */
171 struct reg_cmd_header {
172 __u8 bytes:3; /* Length (for Multibyte) */
173 __u8 eoframe:1; /* For BRI -- end of frame */
174 __u8 portnum:3; /* For port specific registers */
184 __u8 all_ports_broadcast:1;
196 __u8 all_ports_broadcast:1;
205 typedef struct reg_cmd {
206 struct reg_cmd_header h;
208 struct reg_cmd_REG r;
209 /* For Write-Multibyte commands in BRI */
211 __u8 xdata[MULTIBYTE_MAX_LEN];
213 struct reg_cmd_RAM m;
217 /* Shortcut access macros */
218 #define REG_CMD_SIZE(variant) (sizeof(struct reg_cmd_ ## variant))
219 #define REG_FIELD(regptr, member) ((regptr)->alt.r.member)
220 #define REG_XDATA(regptr) ((regptr)->alt.d.xdata)
221 #define REG_FIELD_RAM(regptr, member) ((regptr)->alt.m.member)
225 #define XFRAME_CMD_LEN(variant) \
227 sizeof(struct xpacket_header) + \
228 sizeof(struct reg_cmd_header) + \
229 sizeof(struct reg_cmd_ ## variant) \
232 #define XFRAME_NEW_REG_CMD(frm, p, xbus, card, variant, to) \
234 int pack_len = XFRAME_CMD_LEN(variant); \
236 if (!XBUS_FLAGS(xbus, CONNECTED)) \
238 (frm) = ALLOC_SEND_XFRAME(xbus); \
241 (p) = xframe_next_packet(frm, pack_len); \
244 XPACKET_INIT(p, card, REGISTER_REQUEST, to, 0, 0); \
245 XPACKET_LEN(p) = pack_len; \
246 (frm)->usec_towait = 0; \
249 /*----------------- protocol tables ----------------------------------*/
251 typedef struct xproto_entry xproto_entry_t;
252 typedef struct xproto_table xproto_table_t;
254 typedef int (*xproto_handler_t) (xbus_t *xbus, xpd_t *xpd,
255 const xproto_entry_t *cmd, xpacket_t *pack);
257 const xproto_table_t *xproto_get(xpd_type_t cardtype);
258 void xproto_put(const xproto_table_t *xtable);
259 const xproto_entry_t *xproto_card_entry(const xproto_table_t *table,
261 xproto_handler_t xproto_card_handler(const xproto_table_t *table,
264 const xproto_entry_t *xproto_global_entry(__u8 opcode);
265 xproto_handler_t xproto_global_handler(__u8 opcode);
268 * XMETHOD() resolve to method pointer (NULL for optional methods)
269 * CALL_XMETHOD() calls the method, passing mandatory arguments
271 #define XMETHOD(name, xpd) ((xpd)->xops->name)
272 #define CALL_XMETHOD(name, xpd, ...) \
273 (XMETHOD(name, (xpd))((xpd)->xbus, (xpd), ## __VA_ARGS__))
276 * PHONE_METHOD() resolve to method pointer (NULL for optional methods)
277 * CALL_PHONE_METHOD() calls the method, passing mandatory arguments
279 #define PHONE_METHOD(name, xpd) (PHONEDEV(xpd).phoneops->name)
280 #define CALL_PHONE_METHOD(name, xpd, ...) \
281 (PHONE_METHOD(name, (xpd))((xpd), ## __VA_ARGS__))
284 void (*card_pcm_recompute) (xpd_t *xpd, xpp_line_t pcm_mask);
285 void (*card_pcm_fromspan) (xpd_t *xpd, xpacket_t *pack);
286 void (*card_pcm_tospan) (xpd_t *xpd, xpacket_t *pack);
287 int (*echocancel_timeslot) (xpd_t *xpd, int pos);
288 int (*echocancel_setmask) (xpd_t *xpd, xpp_line_t ec_mask);
289 int (*card_timing_priority) (xpd_t *xpd);
290 int (*card_dahdi_preregistration) (xpd_t *xpd, bool on);
291 int (*card_dahdi_postregistration) (xpd_t *xpd, bool on);
292 int (*card_hooksig) (xpd_t *xpd, int pos, enum dahdi_txsig txsig);
293 int (*card_ioctl) (xpd_t *xpd, int pos, unsigned int cmd,
295 int (*card_open) (xpd_t *xpd, lineno_t pos);
296 int (*card_close) (xpd_t *xpd, lineno_t pos);
297 int (*card_state) (xpd_t *xpd, bool on);
298 int (*span_assigned) (xpd_t *xpd);
302 xpd_t *(*card_new) (xbus_t *xbus, int unit, int subunit,
303 const xproto_table_t *proto_table,
304 const struct unit_descriptor *unit_descriptor,
306 int (*card_init) (xbus_t *xbus, xpd_t *xpd);
307 int (*card_remove) (xbus_t *xbus, xpd_t *xpd);
308 int (*card_tick) (xbus_t *xbus, xpd_t *xpd);
309 int (*card_register_reply) (xbus_t *xbus, xpd_t *xpd, reg_cmd_t *reg);
312 struct xproto_entry {
313 xproto_handler_t handler;
315 xproto_table_t *table;
318 struct xproto_table {
319 struct module *owner;
320 xproto_entry_t entries[256]; /* Indexed by opcode */
321 const struct xops *xops; /* Card level operations */
322 const struct phoneops *phoneops; /* DAHDI operations */
323 const struct echoops *echoops; /* Echo Canceller operations */
325 __u8 ports_per_subunit;
327 bool (*packet_is_valid) (xpacket_t *pack);
328 void (*packet_dump) (const char *msg, xpacket_t *pack);
331 #include "card_global.h"
332 #include "card_fxs.h"
333 #include "card_fxo.h"
334 #include "card_bri.h"
335 #include "card_pri.h"
337 #define MEMBER(card, op) RPACKET_TYPE(card, op) RPACKET_NAME(card, op)
340 struct xpacket_header head;
342 MEMBER(GLOBAL, NULL_REPLY);
343 MEMBER(GLOBAL, PCM_WRITE);
344 MEMBER(GLOBAL, PCM_READ);
345 MEMBER(GLOBAL, SYNC_REPLY);
346 MEMBER(GLOBAL, ERROR_CODE);
348 MEMBER(FXS, SIG_CHANGED);
349 MEMBER(FXO, SIG_CHANGED);
353 /* Last byte is chksum */
356 void dump_packet(const char *msg, const xpacket_t *packet, bool debug);
357 void dump_reg_cmd(const char msg[], bool writing, xbus_t *xbus,
358 __u8 unit, xportno_t port, const reg_cmd_t *regcmd);
359 int xframe_receive(xbus_t *xbus, xframe_t *xframe);
360 void notify_bad_xpd(const char *funcname, xbus_t *xbus,
361 const struct xpd_addr addr, const char *msg);
362 int xproto_register(const xproto_table_t *proto_table);
363 void xproto_unregister(const xproto_table_t *proto_table);
364 const xproto_entry_t *xproto_global_entry(__u8 opcode);
365 const char *xproto_name(xpd_type_t xpd_type);
367 #endif /* __KERNEL__ */
369 #endif /* XPROTO_H */