Debian Codegraph @

drivers/dahdi/xpp/xproto.h

raw symbols SCIP
1 #ifndef XPROTO_H
2 #define XPROTO_H
3 /*
4 * Written by Oron Peled <[email protected]>
5 * Copyright (C) 2004-2006, Xorcom
6 *
7 * All rights reserved.
8 *
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.
13 *
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.
18 *
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.
22 *
23 */
24
25 #include "xdefs.h"
26
27 #ifdef __KERNEL__
28 #include <linux/list.h>
29 #include <linux/proc_fs.h>
30 #include <dahdi/kernel.h>
31
32 /*
33 * This must match the firmware protocol version
34 */
35 #define XPP_PROTOCOL_VERSION 30
36
37 struct unit_descriptor;
38
39 struct xpd_addr {
40 uint8_t subunit:SUBUNIT_BITS;
41 uint8_t reserved:1;
42 uint8_t unit:UNIT_BITS;
43 uint8_t sync_master:1;
44 } PACKED;
45
46 #define MKADDR(p, u, s) do { \
47 (p)->unit = (u); \
48 (p)->subunit = (s); \
49 (p)->sync_master = 0; \
50 } while (0)
51
52 struct xpacket_header {
53 uint16_t packet_len:10;
54 uint16_t reserved:1;
55 uint16_t is_pcm:1;
56 uint16_t pcmslot:4;
57 uint8_t opcode;
58 struct xpd_addr addr;
59 } PACKED;
60
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)
71
72 #define PROTO_TABLE(n) n ## _protocol_table
73
74 /*
75 * The LSB of the type number signifies:
76 * 0 - TO_PSTN
77 * 1 - TO_PHONE
78 */
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
85
86 typedef __u8 xpd_type_t;
87
88 #define XPD_TYPE_PREFIX "xpd-type-"
89
90 #define MODULE_ALIAS_XPD(type) \
91 MODULE_ALIAS(XPD_TYPE_PREFIX __stringify(type))
92
93 #define PCM_CHUNKSIZE (CHANNELS_PERXPD * 8) /* samples of 8 bytes */
94
95 bool valid_xpd_addr(const struct xpd_addr *addr);
96
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)
100
101 #define HANDLER_DEF(card, op) \
102 static int XPROTO_HANDLER(card, op) ( \
103 xbus_t *xbus, \
104 xpd_t *xpd, \
105 const xproto_entry_t *cmd, \
106 xpacket_t *pack)
107
108 #define CALL_PROTO(card, op, ...) XPROTO_CALLER(card, op)(__VA_ARGS__)
109
110 #define DECLARE_CMD(card, op, ...) \
111 int CALL_PROTO(card, op, xbus_t *xbus, xpd_t *xpd, ## __VA_ARGS__)
112
113 #define HOSTCMD(card, op, ...) \
114 DECLARE_CMD(card, op, ## __VA_ARGS__)
115
116 #define RPACKET_NAME(card, op) XPROTO_NAME(RPACKET_ ## card, op)
117 #define RPACKET_TYPE(card, op) struct RPACKET_NAME(card, op)
118
119 #define DEF_RPACKET_DATA(card, op, ...) \
120 RPACKET_TYPE(card, op) { \
121 struct xpacket_header head; \
122 __VA_ARGS__ \
123 } PACKED
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))
128
129 #define XENTRY(prototab, module, op) \
130 [ XPROTO_NAME(module, op) ] = { \
131 .handler = XPROTO_HANDLER(module, op), \
132 .name = #op, \
133 .table = &PROTO_TABLE(prototab) \
134 }
135
136 #define XPACKET_INIT(p, card, op, to, pcm, pcmslot) \
137 do { \
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; \
147 } while (0)
148
149 #define XFRAME_NEW_CMD(frm, p, xbus, card, op, to) \
150 do { \
151 int pack_len = RPACKET_SIZE(card, op); \
152 \
153 if (!XBUS_FLAGS(xbus, CONNECTED)) \
154 return -ENODEV; \
155 (frm) = ALLOC_SEND_XFRAME(xbus); \
156 if (!(frm)) \
157 return -ENOMEM; \
158 (p) = xframe_next_packet(frm, pack_len); \
159 if (!(p)) \
160 return -ENOMEM; \
161 XPACKET_INIT(p, card, op, to, 0, 0); \
162 (frm)->usec_towait = 0; \
163 } while (0)
164
165 #endif
166
167 /*----------------- register handling --------------------------------*/
168
169 #define MULTIBYTE_MAX_LEN 5 /* FPGA firmware limitation */
170
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 */
175 __u8 is_multibyte:1;
176 } PACKED;
177
178 struct reg_cmd_REG {
179 __u8 reserved:3;
180 __u8 do_expander:1;
181 __u8 do_datah:1;
182 __u8 do_subreg:1;
183 __u8 read_request:1;
184 __u8 all_ports_broadcast:1;
185 __u8 regnum;
186 __u8 subreg;
187 __u8 data_low;
188 __u8 data_high;
189 } PACKED;
190
191 struct reg_cmd_RAM {
192 __u8 reserved:4;
193 __u8 do_datah:1;
194 __u8 do_subreg:1;
195 __u8 read_request:1;
196 __u8 all_ports_broadcast:1;
197 __u8 addr_low;
198 __u8 addr_high;
199 __u8 data_0;
200 __u8 data_1;
201 __u8 data_2;
202 __u8 data_3;
203 } PACKED;
204
205 typedef struct reg_cmd {
206 struct reg_cmd_header h;
207 union {
208 struct reg_cmd_REG r;
209 /* For Write-Multibyte commands in BRI */
210 struct {
211 __u8 xdata[MULTIBYTE_MAX_LEN];
212 } PACKED d;
213 struct reg_cmd_RAM m;
214 } PACKED alt;
215 } PACKED reg_cmd_t;
216
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)
222
223 #ifdef __KERNEL__
224
225 #define XFRAME_CMD_LEN(variant) \
226 ( \
227 sizeof(struct xpacket_header) + \
228 sizeof(struct reg_cmd_header) + \
229 sizeof(struct reg_cmd_ ## variant) \
230 )
231
232 #define XFRAME_NEW_REG_CMD(frm, p, xbus, card, variant, to) \
233 do { \
234 int pack_len = XFRAME_CMD_LEN(variant); \
235 \
236 if (!XBUS_FLAGS(xbus, CONNECTED)) \
237 return -ENODEV; \
238 (frm) = ALLOC_SEND_XFRAME(xbus); \
239 if (!(frm)) \
240 return -ENOMEM; \
241 (p) = xframe_next_packet(frm, pack_len); \
242 if (!(p)) \
243 return -ENOMEM; \
244 XPACKET_INIT(p, card, REGISTER_REQUEST, to, 0, 0); \
245 XPACKET_LEN(p) = pack_len; \
246 (frm)->usec_towait = 0; \
247 } while (0)
248
249 /*----------------- protocol tables ----------------------------------*/
250
251 typedef struct xproto_entry xproto_entry_t;
252 typedef struct xproto_table xproto_table_t;
253
254 typedef int (*xproto_handler_t) (xbus_t *xbus, xpd_t *xpd,
255 const xproto_entry_t *cmd, xpacket_t *pack);
256
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,
260 __u8 opcode);
261 xproto_handler_t xproto_card_handler(const xproto_table_t *table,
262 __u8 opcode);
263
264 const xproto_entry_t *xproto_global_entry(__u8 opcode);
265 xproto_handler_t xproto_global_handler(__u8 opcode);
266
267 /*
268 * XMETHOD() resolve to method pointer (NULL for optional methods)
269 * CALL_XMETHOD() calls the method, passing mandatory arguments
270 */
271 #define XMETHOD(name, xpd) ((xpd)->xops->name)
272 #define CALL_XMETHOD(name, xpd, ...) \
273 (XMETHOD(name, (xpd))((xpd)->xbus, (xpd), ## __VA_ARGS__))
274
275 /*
276 * PHONE_METHOD() resolve to method pointer (NULL for optional methods)
277 * CALL_PHONE_METHOD() calls the method, passing mandatory arguments
278 */
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__))
282
283 struct phoneops {
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,
294 unsigned long arg);
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);
299 };
300
301 struct xops {
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,
305 bool to_phone);
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);
310 };
311
312 struct xproto_entry {
313 xproto_handler_t handler;
314 const char *name;
315 xproto_table_t *table;
316 };
317
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 */
324 xpd_type_t type;
325 __u8 ports_per_subunit;
326 const char *name;
327 bool (*packet_is_valid) (xpacket_t *pack);
328 void (*packet_dump) (const char *msg, xpacket_t *pack);
329 };
330
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"
336
337 #define MEMBER(card, op) RPACKET_TYPE(card, op) RPACKET_NAME(card, op)
338
339 struct xpacket {
340 struct xpacket_header head;
341 union {
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);
347
348 MEMBER(FXS, SIG_CHANGED);
349 MEMBER(FXO, SIG_CHANGED);
350
351 __u8 data[0];
352 };
353 /* Last byte is chksum */
354 } PACKED;
355
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);
366
367 #endif /* __KERNEL__ */
368
369 #endif /* XPROTO_H */