2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers (Eilers.Stefan@epost.de),
5 * Hansjoerg Lipp (hjlipp@web.de),
6 * Tilman Schmidt (tilman@imap.cc).
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 * =====================================================================
18 /* == Handling of I4L IO =====================================================*/
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.
26 * driverID driver ID as assigned by LL
27 * channel channel number
28 * ack if != 0 LL wants to be notified on completion via
29 * statcallb(ISDN_STAT_BSENT)
30 * skb skb containing data to send
32 * number of accepted bytes
33 * 0 if temporarily unable to accept data (out of buffer space)
34 * <0 on error (eg. -EINVAL)
36 static int writebuf_from_LL(int driverID, int channel, int ack,
44 if (!(cs = gigaset_get_cs_by_id(driverID))) {
45 err("%s: invalid driver ID (%d)", __func__, driverID);
48 if (channel < 0 || channel >= cs->channels) {
49 err("%s: invalid channel ID (%d)", __func__, channel);
52 bcs = &cs->bcs[channel];
56 "Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
57 driverID, channel, ack, len);
59 if (!atomic_read(&cs->connected)) {
60 err("%s: disconnected", __func__);
66 notice("%s: not ACKing empty packet", __func__);
69 if (len > MAX_BUF_SIZE) {
70 err("%s: packet too large (%d bytes)", __func__, len);
74 skblen = ack ? len : 0;
75 skb->head[0] = skblen & 0xff;
76 skb->head[1] = skblen >> 8;
77 gig_dbg(DEBUG_MCMD, "skb: len=%u, skblen=%u: %02x %02x",
78 len, skblen, (unsigned) skb->head[0], (unsigned) skb->head[1]);
80 /* pass to device-specific module */
81 return cs->ops->send_skb(bcs, skb);
84 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
92 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
95 len = (unsigned char) skb->head[0] |
96 (unsigned) (unsigned char) skb->head[1] << 8;
98 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
99 bcs->cs->myid, bcs->channel, len);
101 response.driver = bcs->cs->myid;
102 response.command = ISDN_STAT_BSENT;
103 response.arg = bcs->channel;
104 response.parm.length = len;
105 bcs->cs->iif.statcallb(&response);
108 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
110 /* This function will be called by LL to send commands
111 * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
112 * so don't put too much effort into it.
114 static int command_from_LL(isdn_ctrl *cntrl)
116 struct cardstate *cs = gigaset_get_cs_by_id(cntrl->driver);
117 //isdn_ctrl response;
118 //unsigned long flags;
119 struct bc_state *bcs;
121 struct setup_parm *sp;
123 gigaset_debugdrivers();
125 //FIXME "remove test for &connected"
126 if ((!cs || !atomic_read(&cs->connected))) {
127 warn("LL tried to access unknown device with nr. %d",
132 switch (cntrl->command) {
134 gig_dbg(DEBUG_ANY, "ISDN_CMD_IOCTL (driver: %d, arg: %ld)",
135 cntrl->driver, cntrl->arg);
137 warn("ISDN_CMD_IOCTL is not supported.");
142 "ISDN_CMD_DIAL (driver: %d, ch: %ld, "
143 "phone: %s, ownmsn: %s, si1: %d, si2: %d)",
144 cntrl->driver, cntrl->arg,
145 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
146 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
148 if (cntrl->arg >= cs->channels) {
149 err("ISDN_CMD_DIAL: invalid channel (%d)",
154 bcs = cs->bcs + cntrl->arg;
156 if (!gigaset_get_channel(bcs)) {
157 err("ISDN_CMD_DIAL: channel not free");
161 sp = kmalloc(sizeof *sp, GFP_ATOMIC);
163 gigaset_free_channel(bcs);
164 err("ISDN_CMD_DIAL: out of memory");
167 *sp = cntrl->parm.setup;
169 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, sp,
170 atomic_read(&bcs->at_state.seq_index),
172 //FIXME what should we do?
174 gigaset_free_channel(bcs);
178 gig_dbg(DEBUG_CMD, "scheduling DIAL");
179 gigaset_schedule_event(cs);
181 case ISDN_CMD_ACCEPTD: //FIXME
182 gig_dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTD");
184 if (cntrl->arg >= cs->channels) {
185 err("ISDN_CMD_ACCEPTD: invalid channel (%d)",
190 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
191 EV_ACCEPT, NULL, 0, NULL)) {
192 //FIXME what should we do?
196 gig_dbg(DEBUG_CMD, "scheduling ACCEPT");
197 gigaset_schedule_event(cs);
200 case ISDN_CMD_ACCEPTB:
201 gig_dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTB");
203 case ISDN_CMD_HANGUP:
204 gig_dbg(DEBUG_ANY, "ISDN_CMD_HANGUP (ch: %d)",
207 if (cntrl->arg >= cs->channels) {
208 err("ISDN_CMD_HANGUP: invalid channel (%u)",
209 (unsigned) cntrl->arg);
213 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
214 EV_HUP, NULL, 0, NULL)) {
215 //FIXME what should we do?
219 gig_dbg(DEBUG_CMD, "scheduling HUP");
220 gigaset_schedule_event(cs);
223 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */ //FIXME
224 gig_dbg(DEBUG_ANY, "ISDN_CMD_CLREAZ");
226 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */ //FIXME
228 "ISDN_CMD_SETEAZ (id: %d, ch: %ld, number: %s)",
229 cntrl->driver, cntrl->arg, cntrl->parm.num);
231 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
232 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETL2 (ch: %ld, proto: %lx)",
233 cntrl->arg & 0xff, (cntrl->arg >> 8));
235 if ((cntrl->arg & 0xff) >= cs->channels) {
236 err("ISDN_CMD_SETL2: invalid channel (%u)",
237 (unsigned) cntrl->arg & 0xff);
241 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg & 0xff].at_state,
242 EV_PROTO_L2, NULL, cntrl->arg >> 8,
244 //FIXME what should we do?
248 gig_dbg(DEBUG_CMD, "scheduling PROTO_L2");
249 gigaset_schedule_event(cs);
251 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
252 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETL3 (ch: %ld, proto: %lx)",
253 cntrl->arg & 0xff, (cntrl->arg >> 8));
255 if ((cntrl->arg & 0xff) >= cs->channels) {
256 err("ISDN_CMD_SETL3: invalid channel (%u)",
257 (unsigned) cntrl->arg & 0xff);
261 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
262 err("ISDN_CMD_SETL3: invalid protocol %lu",
268 case ISDN_CMD_PROCEED:
269 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROCEED"); //FIXME
272 gig_dbg(DEBUG_ANY, "ISDN_CMD_ALERT"); //FIXME
273 if (cntrl->arg >= cs->channels) {
274 err("ISDN_CMD_ALERT: invalid channel (%d)",
278 //bcs = cs->bcs + cntrl->arg;
283 gig_dbg(DEBUG_ANY, "ISDN_CMD_REDIR"); //FIXME
285 case ISDN_CMD_PROT_IO:
286 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROT_IO");
288 case ISDN_CMD_FAXCMD:
289 gig_dbg(DEBUG_ANY, "ISDN_CMD_FAXCMD");
292 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL2");
295 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL3");
297 case ISDN_CMD_GETEAZ:
298 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETEAZ");
300 case ISDN_CMD_SETSIL:
301 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETSIL");
303 case ISDN_CMD_GETSIL:
304 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETSIL");
307 err("unknown command %d from LL", cntrl->command);
314 void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
318 command.driver = cs->myid;
319 command.command = cmd;
321 cs->iif.statcallb(&command);
324 void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
328 command.driver = bcs->cs->myid;
329 command.command = cmd;
330 command.arg = bcs->channel;
331 bcs->cs->iif.statcallb(&command);
334 int gigaset_isdn_setup_dial(struct at_state_t *at_state, void *data)
336 struct bc_state *bcs = at_state->bcs;
339 size_t length[AT_NUM];
342 struct setup_parm *sp = data;
344 switch (bcs->proto2) {
345 case ISDN_PROTO_L2_HDLC:
346 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
348 case ISDN_PROTO_L2_TRANS:
349 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
352 dev_err(bcs->cs->dev, "%s: invalid L2 protocol: %u\n",
353 __func__, bcs->proto2);
359 bc = "9090A3"; /* 3.1 kHz audio, A-law */
362 default: /* hope the app knows what it is doing */
363 bc = "8890"; /* unrestricted digital information */
365 //FIXME add missing si1 values from 1TR6, inspect si2, set HLC/LLC
367 length[AT_DIAL ] = 1 + strlen(sp->phone) + 1 + 1;
368 l = strlen(sp->eazmsn);
369 length[AT_MSN ] = l ? 6 + l + 1 + 1 : 0;
370 length[AT_BC ] = 5 + strlen(bc) + 1 + 1;
371 length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
372 length[AT_ISO ] = 6 + 1 + 1 + 1; /* channel: 1 character */
373 length[AT_TYPE ] = 6 + 1 + 1 + 1; /* call type: 1 character */
376 for (i = 0; i < AT_NUM; ++i) {
377 kfree(bcs->commands[i]);
378 bcs->commands[i] = NULL;
380 !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
381 dev_err(bcs->cs->dev, "out of memory\n");
386 /* type = 1: extern, 0: intern, 2: recall, 3: door, 4: centrex */
387 if (sp->phone[0] == '*' && sp->phone[1] == '*') {
388 /* internal call: translate ** prefix to CTP value */
389 snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
390 "D%s\r", sp->phone+2);
391 strncpy(bcs->commands[AT_TYPE], "^SCTP=0\r", length[AT_TYPE]);
393 snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
395 strncpy(bcs->commands[AT_TYPE], "^SCTP=1\r", length[AT_TYPE]);
398 if (bcs->commands[AT_MSN])
399 snprintf(bcs->commands[AT_MSN], length[AT_MSN],
400 "^SMSN=%s\r", sp->eazmsn);
401 snprintf(bcs->commands[AT_BC ], length[AT_BC ],
403 snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
404 "^SBPR=%u\r", proto);
405 snprintf(bcs->commands[AT_ISO ], length[AT_ISO ],
406 "^SISO=%u\r", (unsigned)bcs->channel + 1);
411 int gigaset_isdn_setup_accept(struct at_state_t *at_state)
414 size_t length[AT_NUM];
416 struct bc_state *bcs = at_state->bcs;
418 switch (bcs->proto2) {
419 case ISDN_PROTO_L2_HDLC:
420 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
422 case ISDN_PROTO_L2_TRANS:
423 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
426 dev_err(at_state->cs->dev, "%s: invalid protocol: %u\n",
427 __func__, bcs->proto2);
431 length[AT_DIAL ] = 0;
434 length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
435 length[AT_ISO ] = 6 + 1 + 1 + 1; /* channel: 1 character */
436 length[AT_TYPE ] = 0;
439 for (i = 0; i < AT_NUM; ++i) {
440 kfree(bcs->commands[i]);
441 bcs->commands[i] = NULL;
443 !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
444 dev_err(at_state->cs->dev, "out of memory\n");
449 snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
450 "^SBPR=%u\r", proto);
451 snprintf(bcs->commands[AT_ISO ], length[AT_ISO ],
452 "^SISO=%u\r", (unsigned) bcs->channel + 1);
457 int gigaset_isdn_icall(struct at_state_t *at_state)
459 struct cardstate *cs = at_state->cs;
460 struct bc_state *bcs = at_state->bcs;
464 /* fill ICALL structure */
465 response.parm.setup.si1 = 0; /* default: unknown */
466 response.parm.setup.si2 = 0;
467 response.parm.setup.screen = 0; //FIXME how to set these?
468 response.parm.setup.plan = 0;
469 if (!at_state->str_var[STR_ZBC]) {
470 /* no BC (internal call): assume speech, A-law */
471 response.parm.setup.si1 = 1;
472 } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
473 /* unrestricted digital information */
474 response.parm.setup.si1 = 7;
475 } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
477 response.parm.setup.si1 = 1;
478 } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
479 /* 3,1 kHz audio, A-law */
480 response.parm.setup.si1 = 1;
481 response.parm.setup.si2 = 2;
483 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
484 at_state->str_var[STR_ZBC]);
487 if (at_state->str_var[STR_NMBR]) {
488 strncpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
489 sizeof response.parm.setup.phone - 1);
490 response.parm.setup.phone[sizeof response.parm.setup.phone - 1] = 0;
492 response.parm.setup.phone[0] = 0;
493 if (at_state->str_var[STR_ZCPN]) {
494 strncpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
495 sizeof response.parm.setup.eazmsn - 1);
496 response.parm.setup.eazmsn[sizeof response.parm.setup.eazmsn - 1] = 0;
498 response.parm.setup.eazmsn[0] = 0;
501 dev_notice(cs->dev, "no channel for incoming call\n");
502 response.command = ISDN_STAT_ICALLW;
503 response.arg = 0; //FIXME
505 gig_dbg(DEBUG_CMD, "Sending ICALL");
506 response.command = ISDN_STAT_ICALL;
507 response.arg = bcs->channel; //FIXME
509 response.driver = cs->myid;
510 retval = cs->iif.statcallb(&response);
511 gig_dbg(DEBUG_CMD, "Response: %d", retval);
513 case 0: /* no takers */
515 case 1: /* alerting */
516 bcs->chstate |= CHS_NOTIFY_LL;
520 case 3: /* incomplete */
522 "LL requested unsupported feature: Incomplete Number\n");
524 case 4: /* proceeding */
525 /* Gigaset will send ALERTING anyway.
526 * There doesn't seem to be a way to avoid this.
529 case 5: /* deflect */
531 "LL requested unsupported feature: Call Deflection\n");
534 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
539 /* Set Callback function pointer */
540 int gigaset_register_to_LL(struct cardstate *cs, const char *isdnid)
542 isdn_if *iif = &cs->iif;
544 gig_dbg(DEBUG_ANY, "Register driver capabilities to LL");
546 //iif->id[sizeof(iif->id) - 1]=0;
547 //strncpy(iif->id, isdnid, sizeof(iif->id) - 1);
548 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
550 return -ENOMEM; //FIXME EINVAL/...??
552 iif->owner = THIS_MODULE;
553 iif->channels = cs->channels;
554 iif->maxbufsize = MAX_BUF_SIZE;
555 iif->features = ISDN_FEATURE_L2_TRANS |
556 ISDN_FEATURE_L2_HDLC |
558 ISDN_FEATURE_L2_X75I |
560 ISDN_FEATURE_L3_TRANS |
562 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
563 iif->command = command_from_LL;
564 iif->writebuf_skb = writebuf_from_LL;
565 iif->writecmd = NULL; /* Don't support isdnctrl */
566 iif->readstat = NULL; /* Don't support isdnctrl */
567 iif->rcvcallb_skb = NULL; /* Will be set by LL */
568 iif->statcallb = NULL; /* Will be set by LL */
570 if (!register_isdn(iif))
573 cs->myid = iif->channels; /* Set my device id */