blob: e30275d4e1482f3861faabba988e1f3b7e19d7f8 [file] [log] [blame]
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -08001/*
2 * Stuff used by all variants of the driver
3 *
4 * Copyright (c) 2001 by Stefan Eilers (Eilers.Stefan@epost.de),
5 * Hansjoerg Lipp (hjlipp@web.de),
6 * Tilman Schmidt (tilman@imap.cc).
7 *
8 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080014 */
15
16#include "gigaset.h"
17
18/* == Handling of I4L IO ============================================================================*/
19
20/* writebuf_from_LL
21 * called by LL to transmit data on an open channel
22 * inserts the buffer data into the send queue and starts the transmission
23 * Note that this operation must not sleep!
24 * When the buffer is processed completely, gigaset_skb_sent() should be called.
25 * parameters:
26 * driverID driver ID as assigned by LL
27 * channel channel number
Tilman Schmidt917f5082006-04-10 22:55:00 -070028 * ack if != 0 LL wants to be notified on completion via
29 * statcallb(ISDN_STAT_BSENT)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080030 * skb skb containing data to send
31 * return value:
32 * number of accepted bytes
33 * 0 if temporarily unable to accept data (out of buffer space)
34 * <0 on error (eg. -EINVAL)
35 */
36static int writebuf_from_LL(int driverID, int channel, int ack, struct sk_buff *skb)
37{
38 struct cardstate *cs;
39 struct bc_state *bcs;
40 unsigned len;
41 unsigned skblen;
42
43 if (!(cs = gigaset_get_cs_by_id(driverID))) {
44 err("%s: invalid driver ID (%d)", __func__, driverID);
45 return -ENODEV;
46 }
47 if (channel < 0 || channel >= cs->channels) {
48 err("%s: invalid channel ID (%d)", __func__, channel);
49 return -ENODEV;
50 }
51 bcs = &cs->bcs[channel];
52 len = skb->len;
53
54 dbg(DEBUG_LLDATA,
55 "Receiving data from LL (id: %d, channel: %d, ack: %d, size: %d)",
56 driverID, channel, ack, len);
57
58 if (!len) {
59 if (ack)
60 warn("not ACKing empty packet from LL");
61 return 0;
62 }
63 if (len > MAX_BUF_SIZE) {
64 err("%s: packet too large (%d bytes)", __func__, channel);
65 return -EINVAL;
66 }
67
68 if (!atomic_read(&cs->connected))
69 return -ENODEV;
70
71 skblen = ack ? len : 0;
72 skb->head[0] = skblen & 0xff;
73 skb->head[1] = skblen >> 8;
74 dbg(DEBUG_MCMD, "skb: len=%u, skblen=%u: %02x %02x", len, skblen,
75 (unsigned) skb->head[0], (unsigned) skb->head[1]);
76
77 /* pass to device-specific module */
78 return cs->ops->send_skb(bcs, skb);
79}
80
81void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
82{
83 unsigned len;
84 isdn_ctrl response;
85
86 ++bcs->trans_up;
87
88 if (skb->len)
89 warn("%s: skb->len==%d", __func__, skb->len);
90
91 len = (unsigned char) skb->head[0] |
92 (unsigned) (unsigned char) skb->head[1] << 8;
93 if (len) {
94 dbg(DEBUG_MCMD,
95 "Acknowledge sending to LL (id: %d, channel: %d size: %u)",
96 bcs->cs->myid, bcs->channel, len);
97
98 response.driver = bcs->cs->myid;
99 response.command = ISDN_STAT_BSENT;
100 response.arg = bcs->channel;
101 response.parm.length = len;
102 bcs->cs->iif.statcallb(&response);
103 }
104}
105EXPORT_SYMBOL_GPL(gigaset_skb_sent);
106
107/* This function will be called by LL to send commands
108 * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
109 * so don't put too much effort into it.
110 */
111static int command_from_LL(isdn_ctrl *cntrl)
112{
113 struct cardstate *cs = gigaset_get_cs_by_id(cntrl->driver);
114 //isdn_ctrl response;
115 //unsigned long flags;
116 struct bc_state *bcs;
117 int retval = 0;
118 struct setup_parm *sp;
119
120 //dbg(DEBUG_ANY, "Gigaset_HW: Receiving command");
121 gigaset_debugdrivers();
122
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800123 //FIXME "remove test for &connected"
124 if ((!cs || !atomic_read(&cs->connected))) {
125 warn("LL tried to access unknown device with nr. %d",
126 cntrl->driver);
127 return -ENODEV;
128 }
129
130 switch (cntrl->command) {
131 case ISDN_CMD_IOCTL:
132
133 dbg(DEBUG_ANY, "ISDN_CMD_IOCTL (driver:%d,arg: %ld)",
134 cntrl->driver, cntrl->arg);
135
136 warn("ISDN_CMD_IOCTL is not supported.");
137 return -EINVAL;
138
139 case ISDN_CMD_DIAL:
140 dbg(DEBUG_ANY, "ISDN_CMD_DIAL (driver: %d, channel: %ld, "
141 "phone: %s,ownmsn: %s, si1: %d, si2: %d)",
142 cntrl->driver, cntrl->arg,
143 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
144 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
145
146 if (cntrl->arg >= cs->channels) {
147 err("invalid channel (%d)", (int) cntrl->arg);
148 return -EINVAL;
149 }
150
151 bcs = cs->bcs + cntrl->arg;
152
153 if (!gigaset_get_channel(bcs)) {
154 err("channel not free");
155 return -EBUSY;
156 }
157
158 sp = kmalloc(sizeof *sp, GFP_ATOMIC);
159 if (!sp) {
160 gigaset_free_channel(bcs);
161 err("ISDN_CMD_DIAL: out of memory");
162 return -ENOMEM;
163 }
164 *sp = cntrl->parm.setup;
165
166 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, sp,
167 atomic_read(&bcs->at_state.seq_index),
168 NULL)) {
169 //FIXME what should we do?
170 kfree(sp);
171 gigaset_free_channel(bcs);
172 return -ENOMEM;
173 }
174
175 dbg(DEBUG_CMD, "scheduling DIAL");
176 gigaset_schedule_event(cs);
177 break;
178 case ISDN_CMD_ACCEPTD: //FIXME
179 dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTD");
180
181 if (cntrl->arg >= cs->channels) {
182 err("invalid channel (%d)", (int) cntrl->arg);
183 return -EINVAL;
184 }
185
186 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
187 EV_ACCEPT, NULL, 0, NULL)) {
188 //FIXME what should we do?
189 return -ENOMEM;
190 }
191
192 dbg(DEBUG_CMD, "scheduling ACCEPT");
193 gigaset_schedule_event(cs);
194
195 break;
196 case ISDN_CMD_ACCEPTB:
197 dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTB");
198 break;
199 case ISDN_CMD_HANGUP:
200 dbg(DEBUG_ANY,
201 "ISDN_CMD_HANGUP (channel: %d)", (int) cntrl->arg);
202
203 if (cntrl->arg >= cs->channels) {
204 err("ISDN_CMD_HANGUP: invalid channel (%u)",
205 (unsigned) cntrl->arg);
206 return -EINVAL;
207 }
208
209 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
210 EV_HUP, NULL, 0, NULL)) {
211 //FIXME what should we do?
212 return -ENOMEM;
213 }
214
215 dbg(DEBUG_CMD, "scheduling HUP");
216 gigaset_schedule_event(cs);
217
218 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700219 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */ //FIXME
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800220 dbg(DEBUG_ANY, "ISDN_CMD_CLREAZ");
221 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700222 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */ //FIXME
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800223 dbg(DEBUG_ANY,
224 "ISDN_CMD_SETEAZ (id:%d, channel: %ld, number: %s)",
225 cntrl->driver, cntrl->arg, cntrl->parm.num);
226 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700227 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800228 dbg(DEBUG_ANY, "ISDN_CMD_SETL2 (Channel: %ld, Proto: %lx)",
229 cntrl->arg & 0xff, (cntrl->arg >> 8));
230
231 if ((cntrl->arg & 0xff) >= cs->channels) {
232 err("invalid channel (%u)",
233 (unsigned) cntrl->arg & 0xff);
234 return -EINVAL;
235 }
236
237 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg & 0xff].at_state,
238 EV_PROTO_L2, NULL, cntrl->arg >> 8,
239 NULL)) {
240 //FIXME what should we do?
241 return -ENOMEM;
242 }
243
244 dbg(DEBUG_CMD, "scheduling PROTO_L2");
245 gigaset_schedule_event(cs);
246 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700247 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800248 dbg(DEBUG_ANY, "ISDN_CMD_SETL3 (Channel: %ld, Proto: %lx)",
249 cntrl->arg & 0xff, (cntrl->arg >> 8));
250
251 if ((cntrl->arg & 0xff) >= cs->channels) {
252 err("invalid channel (%u)",
253 (unsigned) cntrl->arg & 0xff);
254 return -EINVAL;
255 }
256
257 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
258 err("invalid protocol %lu", cntrl->arg >> 8);
259 return -EINVAL;
260 }
261
262 break;
263 case ISDN_CMD_PROCEED:
264 dbg(DEBUG_ANY, "ISDN_CMD_PROCEED"); //FIXME
265 break;
266 case ISDN_CMD_ALERT:
267 dbg(DEBUG_ANY, "ISDN_CMD_ALERT"); //FIXME
268 if (cntrl->arg >= cs->channels) {
269 err("invalid channel (%d)", (int) cntrl->arg);
270 return -EINVAL;
271 }
272 //bcs = cs->bcs + cntrl->arg;
273 //bcs->proto2 = -1;
274 // FIXME
275 break;
276 case ISDN_CMD_REDIR:
277 dbg(DEBUG_ANY, "ISDN_CMD_REDIR"); //FIXME
278 break;
279 case ISDN_CMD_PROT_IO:
280 dbg(DEBUG_ANY, "ISDN_CMD_PROT_IO");
281 break;
282 case ISDN_CMD_FAXCMD:
283 dbg(DEBUG_ANY, "ISDN_CMD_FAXCMD");
284 break;
285 case ISDN_CMD_GETL2:
286 dbg(DEBUG_ANY, "ISDN_CMD_GETL2");
287 break;
288 case ISDN_CMD_GETL3:
289 dbg(DEBUG_ANY, "ISDN_CMD_GETL3");
290 break;
291 case ISDN_CMD_GETEAZ:
292 dbg(DEBUG_ANY, "ISDN_CMD_GETEAZ");
293 break;
294 case ISDN_CMD_SETSIL:
295 dbg(DEBUG_ANY, "ISDN_CMD_SETSIL");
296 break;
297 case ISDN_CMD_GETSIL:
298 dbg(DEBUG_ANY, "ISDN_CMD_GETSIL");
299 break;
300 default:
301 err("unknown command %d from LL",
302 cntrl->command);
303 return -EINVAL;
304 }
305
306 return retval;
307}
308
309void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
310{
311 isdn_ctrl command;
312
313 command.driver = cs->myid;
314 command.command = cmd;
315 command.arg = 0;
316 cs->iif.statcallb(&command);
317}
318
319void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
320{
321 isdn_ctrl command;
322
323 command.driver = bcs->cs->myid;
324 command.command = cmd;
325 command.arg = bcs->channel;
326 bcs->cs->iif.statcallb(&command);
327}
328
329int gigaset_isdn_setup_dial(struct at_state_t *at_state, void *data)
330{
331 struct bc_state *bcs = at_state->bcs;
332 unsigned proto;
333 const char *bc;
334 size_t length[AT_NUM];
335 size_t l;
336 int i;
337 struct setup_parm *sp = data;
338
339 switch (bcs->proto2) {
340 case ISDN_PROTO_L2_HDLC:
341 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
342 break;
343 case ISDN_PROTO_L2_TRANS:
344 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
345 break;
346 default:
347 err("invalid protocol: %u", bcs->proto2);
348 return -EINVAL;
349 }
350
351 switch (sp->si1) {
352 case 1: /* audio */
353 bc = "9090A3"; /* 3.1 kHz audio, A-law */
354 break;
355 case 7: /* data */
356 default: /* hope the app knows what it is doing */
357 bc = "8890"; /* unrestricted digital information */
358 }
359 //FIXME add missing si1 values from 1TR6, inspect si2, set HLC/LLC
360
361 length[AT_DIAL ] = 1 + strlen(sp->phone) + 1 + 1;
362 l = strlen(sp->eazmsn);
363 length[AT_MSN ] = l ? 6 + l + 1 + 1 : 0;
364 length[AT_BC ] = 5 + strlen(bc) + 1 + 1;
365 length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
366 length[AT_ISO ] = 6 + 1 + 1 + 1; /* channel: 1 character */
367 length[AT_TYPE ] = 6 + 1 + 1 + 1; /* call type: 1 character */
368 length[AT_HLC ] = 0;
369
370 for (i = 0; i < AT_NUM; ++i) {
371 kfree(bcs->commands[i]);
372 bcs->commands[i] = NULL;
373 if (length[i] &&
374 !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
375 err("out of memory");
376 return -ENOMEM;
377 }
378 }
379
380 /* type = 1: extern, 0: intern, 2: recall, 3: door, 4: centrex */
381 if (sp->phone[0] == '*' && sp->phone[1] == '*') {
382 /* internal call: translate ** prefix to CTP value */
383 snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
384 "D%s\r", sp->phone+2);
385 strncpy(bcs->commands[AT_TYPE], "^SCTP=0\r", length[AT_TYPE]);
386 } else {
387 snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
388 "D%s\r", sp->phone);
389 strncpy(bcs->commands[AT_TYPE], "^SCTP=1\r", length[AT_TYPE]);
390 }
391
392 if (bcs->commands[AT_MSN])
Tilman Schmidt917f5082006-04-10 22:55:00 -0700393 snprintf(bcs->commands[AT_MSN], length[AT_MSN],
394 "^SMSN=%s\r", sp->eazmsn);
395 snprintf(bcs->commands[AT_BC ], length[AT_BC ],
396 "^SBC=%s\r", bc);
397 snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
398 "^SBPR=%u\r", proto);
399 snprintf(bcs->commands[AT_ISO ], length[AT_ISO ],
400 "^SISO=%u\r", (unsigned)bcs->channel + 1);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800401
402 return 0;
403}
404
405int gigaset_isdn_setup_accept(struct at_state_t *at_state)
406{
407 unsigned proto;
408 size_t length[AT_NUM];
409 int i;
410 struct bc_state *bcs = at_state->bcs;
411
412 switch (bcs->proto2) {
413 case ISDN_PROTO_L2_HDLC:
414 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
415 break;
416 case ISDN_PROTO_L2_TRANS:
417 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
418 break;
419 default:
420 err("invalid protocol: %u", bcs->proto2);
421 return -EINVAL;
422 }
423
424 length[AT_DIAL ] = 0;
425 length[AT_MSN ] = 0;
426 length[AT_BC ] = 0;
427 length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
428 length[AT_ISO ] = 6 + 1 + 1 + 1; /* channel: 1 character */
429 length[AT_TYPE ] = 0;
430 length[AT_HLC ] = 0;
431
432 for (i = 0; i < AT_NUM; ++i) {
433 kfree(bcs->commands[i]);
434 bcs->commands[i] = NULL;
435 if (length[i] &&
436 !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
437 err("out of memory");
438 return -ENOMEM;
439 }
440 }
441
Tilman Schmidt917f5082006-04-10 22:55:00 -0700442 snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
443 "^SBPR=%u\r", proto);
444 snprintf(bcs->commands[AT_ISO ], length[AT_ISO ],
445 "^SISO=%u\r", (unsigned) bcs->channel + 1);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800446
447 return 0;
448}
449
450int gigaset_isdn_icall(struct at_state_t *at_state)
451{
452 struct cardstate *cs = at_state->cs;
453 struct bc_state *bcs = at_state->bcs;
454 isdn_ctrl response;
455 int retval;
456
457 /* fill ICALL structure */
458 response.parm.setup.si1 = 0; /* default: unknown */
459 response.parm.setup.si2 = 0;
460 response.parm.setup.screen = 0; //FIXME how to set these?
461 response.parm.setup.plan = 0;
462 if (!at_state->str_var[STR_ZBC]) {
463 /* no BC (internal call): assume speech, A-law */
464 response.parm.setup.si1 = 1;
465 } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
466 /* unrestricted digital information */
467 response.parm.setup.si1 = 7;
468 } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
469 /* speech, A-law */
470 response.parm.setup.si1 = 1;
471 } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
472 /* 3,1 kHz audio, A-law */
473 response.parm.setup.si1 = 1;
474 response.parm.setup.si2 = 2;
475 } else {
476 warn("RING ignored - unsupported BC %s",
477 at_state->str_var[STR_ZBC]);
478 return ICALL_IGNORE;
479 }
480 if (at_state->str_var[STR_NMBR]) {
481 strncpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
482 sizeof response.parm.setup.phone - 1);
483 response.parm.setup.phone[sizeof response.parm.setup.phone - 1] = 0;
484 } else
485 response.parm.setup.phone[0] = 0;
486 if (at_state->str_var[STR_ZCPN]) {
487 strncpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
488 sizeof response.parm.setup.eazmsn - 1);
489 response.parm.setup.eazmsn[sizeof response.parm.setup.eazmsn - 1] = 0;
490 } else
491 response.parm.setup.eazmsn[0] = 0;
492
493 if (!bcs) {
494 notice("no channel for incoming call");
495 dbg(DEBUG_CMD, "Sending ICALLW");
496 response.command = ISDN_STAT_ICALLW;
497 response.arg = 0; //FIXME
498 } else {
499 dbg(DEBUG_CMD, "Sending ICALL");
500 response.command = ISDN_STAT_ICALL;
501 response.arg = bcs->channel; //FIXME
502 }
503 response.driver = cs->myid;
504 retval = cs->iif.statcallb(&response);
505 dbg(DEBUG_CMD, "Response: %d", retval);
506 switch (retval) {
507 case 0: /* no takers */
508 return ICALL_IGNORE;
509 case 1: /* alerting */
510 bcs->chstate |= CHS_NOTIFY_LL;
511 return ICALL_ACCEPT;
512 case 2: /* reject */
513 return ICALL_REJECT;
514 case 3: /* incomplete */
515 warn("LL requested unsupported feature: Incomplete Number");
516 return ICALL_IGNORE;
517 case 4: /* proceeding */
518 /* Gigaset will send ALERTING anyway.
519 * There doesn't seem to be a way to avoid this.
520 */
521 return ICALL_ACCEPT;
522 case 5: /* deflect */
523 warn("LL requested unsupported feature: Call Deflection");
524 return ICALL_IGNORE;
525 default:
526 err("LL error %d on ICALL", retval);
527 return ICALL_IGNORE;
528 }
529}
530
531/* Set Callback function pointer */
532int gigaset_register_to_LL(struct cardstate *cs, const char *isdnid)
533{
534 isdn_if *iif = &cs->iif;
535
536 dbg(DEBUG_ANY, "Register driver capabilities to LL");
537
538 //iif->id[sizeof(iif->id) - 1]=0;
539 //strncpy(iif->id, isdnid, sizeof(iif->id) - 1);
540 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
541 >= sizeof iif->id)
542 return -ENOMEM; //FIXME EINVAL/...??
543
544 iif->owner = THIS_MODULE;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700545 iif->channels = cs->channels;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800546 iif->maxbufsize = MAX_BUF_SIZE;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700547 iif->features = ISDN_FEATURE_L2_TRANS |
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800548 ISDN_FEATURE_L2_HDLC |
549#ifdef GIG_X75
550 ISDN_FEATURE_L2_X75I |
551#endif
552 ISDN_FEATURE_L3_TRANS |
553 ISDN_FEATURE_P_EURO;
554 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
555 iif->command = command_from_LL;
556 iif->writebuf_skb = writebuf_from_LL;
557 iif->writecmd = NULL; /* Don't support isdnctrl */
558 iif->readstat = NULL; /* Don't support isdnctrl */
559 iif->rcvcallb_skb = NULL; /* Will be set by LL */
560 iif->statcallb = NULL; /* Will be set by LL */
561
562 if (!register_isdn(iif))
563 return 0;
564
565 cs->myid = iif->channels; /* Set my device id */
566 return 1;
567}