2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers,
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 * =====================================================================
17 #include <linux/isdnif.h>
19 #define HW_HDR_LEN 2 /* Header size used to store ack info */
21 /* == Handling of I4L IO =====================================================*/
24 * called by LL to transmit data on an open channel
25 * inserts the buffer data into the send queue and starts the transmission
26 * Note that this operation must not sleep!
27 * When the buffer is processed completely, gigaset_skb_sent() should be called.
29 * driverID driver ID as assigned by LL
30 * channel channel number
31 * ack if != 0 LL wants to be notified on completion via
32 * statcallb(ISDN_STAT_BSENT)
33 * skb skb containing data to send
35 * number of accepted bytes
36 * 0 if temporarily unable to accept data (out of buffer space)
37 * <0 on error (eg. -EINVAL)
39 static int writebuf_from_LL(int driverID, int channel, int ack,
47 if (!(cs = gigaset_get_cs_by_id(driverID))) {
48 pr_err("%s: invalid driver ID (%d)\n", __func__, driverID);
51 if (channel < 0 || channel >= cs->channels) {
52 dev_err(cs->dev, "%s: invalid channel ID (%d)\n",
56 bcs = &cs->bcs[channel];
58 /* can only handle linear sk_buffs */
59 if (skb_linearize(skb) < 0) {
60 dev_err(cs->dev, "%s: skb_linearize failed\n", __func__);
66 "Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
67 driverID, channel, ack, len);
71 dev_notice(cs->dev, "%s: not ACKing empty packet\n",
75 if (len > MAX_BUF_SIZE) {
76 dev_err(cs->dev, "%s: packet too large (%d bytes)\n",
81 skblen = ack ? len : 0;
82 skb->head[0] = skblen & 0xff;
83 skb->head[1] = skblen >> 8;
84 gig_dbg(DEBUG_MCMD, "skb: len=%u, skblen=%u: %02x %02x",
85 len, skblen, (unsigned) skb->head[0], (unsigned) skb->head[1]);
87 /* pass to device-specific module */
88 return cs->ops->send_skb(bcs, skb);
92 * gigaset_skb_sent() - acknowledge sending an skb
93 * @bcs: B channel descriptor structure.
96 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
97 * skb has been successfully sent, for signalling completion to the LL.
99 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
101 isdn_if *iif = bcs->cs->iif;
108 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
111 len = (unsigned char) skb->head[0] |
112 (unsigned) (unsigned char) skb->head[1] << 8;
114 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
115 bcs->cs->myid, bcs->channel, len);
117 response.driver = bcs->cs->myid;
118 response.command = ISDN_STAT_BSENT;
119 response.arg = bcs->channel;
120 response.parm.length = len;
121 iif->statcallb(&response);
124 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
127 * gigaset_skb_rcvd() - pass received skb to LL
128 * @bcs: B channel descriptor structure.
129 * @skb: received data.
131 * Called by hardware module {bas,ser,usb}_gigaset when user data has
132 * been successfully received, for passing to the LL.
133 * Warning: skb must not be accessed anymore!
135 void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
137 isdn_if *iif = bcs->cs->iif;
139 iif->rcvcallb_skb(bcs->cs->myid, bcs->channel, skb);
142 EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
145 * gigaset_isdn_rcv_err() - signal receive error
146 * @bcs: B channel descriptor structure.
148 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
149 * has occurred, for signalling to the LL.
151 void gigaset_isdn_rcv_err(struct bc_state *bcs)
153 isdn_if *iif = bcs->cs->iif;
156 /* if currently ignoring packets, just count down */
162 /* update statistics */
166 gig_dbg(DEBUG_CMD, "sending L1ERR");
167 response.driver = bcs->cs->myid;
168 response.command = ISDN_STAT_L1ERR;
169 response.arg = bcs->channel;
170 response.parm.errcode = ISDN_STAT_L1ERR_RECV;
171 iif->statcallb(&response);
173 EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
175 /* This function will be called by LL to send commands
176 * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
177 * so don't put too much effort into it.
179 static int command_from_LL(isdn_ctrl *cntrl)
181 struct cardstate *cs;
182 struct bc_state *bcs;
189 gigaset_debugdrivers();
191 gig_dbg(DEBUG_CMD, "driver: %d, command: %d, arg: 0x%lx",
192 cntrl->driver, cntrl->command, cntrl->arg);
194 cs = gigaset_get_cs_by_id(cntrl->driver);
196 pr_err("%s: invalid driver ID (%d)\n", __func__, cntrl->driver);
199 ch = cntrl->arg & 0xff;
201 switch (cntrl->command) {
203 dev_warn(cs->dev, "ISDN_CMD_IOCTL not supported\n");
208 "ISDN_CMD_DIAL (phone: %s, msn: %s, si1: %d, si2: %d)",
209 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
210 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
212 if (ch >= cs->channels) {
214 "ISDN_CMD_DIAL: invalid channel (%d)\n", ch);
218 if (!gigaset_get_channel(bcs)) {
219 dev_err(cs->dev, "ISDN_CMD_DIAL: channel not free\n");
223 commands = kzalloc(AT_NUM*(sizeof *commands), GFP_ATOMIC);
225 gigaset_free_channel(bcs);
226 dev_err(cs->dev, "ISDN_CMD_DIAL: out of memory\n");
230 l = 3 + strlen(cntrl->parm.setup.phone);
231 commands[AT_DIAL] = kmalloc(l, GFP_ATOMIC);
232 if (!commands[AT_DIAL])
234 if (cntrl->parm.setup.phone[0] == '*' &&
235 cntrl->parm.setup.phone[1] == '*') {
236 /* internal call: translate ** prefix to CTP value */
237 commands[AT_TYPE] = kstrdup("^SCTP=0\r", GFP_ATOMIC);
238 if (!commands[AT_TYPE])
240 snprintf(commands[AT_DIAL], l,
241 "D%s\r", cntrl->parm.setup.phone+2);
243 commands[AT_TYPE] = kstrdup("^SCTP=1\r", GFP_ATOMIC);
244 if (!commands[AT_TYPE])
246 snprintf(commands[AT_DIAL], l,
247 "D%s\r", cntrl->parm.setup.phone);
250 l = strlen(cntrl->parm.setup.eazmsn);
253 commands[AT_MSN] = kmalloc(l, GFP_ATOMIC);
254 if (!commands[AT_MSN])
256 snprintf(commands[AT_MSN], l, "^SMSN=%s\r",
257 cntrl->parm.setup.eazmsn);
260 switch (cntrl->parm.setup.si1) {
262 /* BC = 9090A3: 3.1 kHz audio, A-law */
263 commands[AT_BC] = kstrdup("^SBC=9090A3\r", GFP_ATOMIC);
264 if (!commands[AT_BC])
268 default: /* hope the app knows what it is doing */
269 /* BC = 8890: unrestricted digital information */
270 commands[AT_BC] = kstrdup("^SBC=8890\r", GFP_ATOMIC);
271 if (!commands[AT_BC])
274 /* ToDo: other si1 values, inspect si2, set HLC/LLC */
276 commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
277 if (!commands[AT_PROTO])
279 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
281 commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
282 if (!commands[AT_ISO])
284 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
285 (unsigned) bcs->channel + 1);
287 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
288 bcs->at_state.seq_index, NULL)) {
289 for (i = 0; i < AT_NUM; ++i)
292 gigaset_free_channel(bcs);
296 gig_dbg(DEBUG_CMD, "scheduling DIAL");
297 gigaset_schedule_event(cs);
299 case ISDN_CMD_ACCEPTD:
300 if (ch >= cs->channels) {
302 "ISDN_CMD_ACCEPTD: invalid channel (%d)\n", ch);
306 if (!gigaset_add_event(cs, &bcs->at_state,
307 EV_ACCEPT, NULL, 0, NULL))
310 gig_dbg(DEBUG_CMD, "scheduling ACCEPT");
311 gigaset_schedule_event(cs);
314 case ISDN_CMD_ACCEPTB:
316 case ISDN_CMD_HANGUP:
317 if (ch >= cs->channels) {
319 "ISDN_CMD_HANGUP: invalid channel (%d)\n", ch);
323 if (!gigaset_add_event(cs, &bcs->at_state,
324 EV_HUP, NULL, 0, NULL))
327 gig_dbg(DEBUG_CMD, "scheduling HUP");
328 gigaset_schedule_event(cs);
331 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */
332 dev_info(cs->dev, "ignoring ISDN_CMD_CLREAZ\n");
334 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */
335 dev_info(cs->dev, "ignoring ISDN_CMD_SETEAZ (%s)\n",
338 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
339 if (ch >= cs->channels) {
341 "ISDN_CMD_SETL2: invalid channel (%d)\n", ch);
345 if (bcs->chstate & CHS_D_UP) {
347 "ISDN_CMD_SETL2: channel active (%d)\n", ch);
350 switch (cntrl->arg >> 8) {
351 case ISDN_PROTO_L2_HDLC:
352 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_HDLC");
353 bcs->proto2 = L2_HDLC;
355 case ISDN_PROTO_L2_TRANS:
356 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_VOICE");
357 bcs->proto2 = L2_VOICE;
361 "ISDN_CMD_SETL2: unsupported protocol (%lu)\n",
366 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
367 if (ch >= cs->channels) {
369 "ISDN_CMD_SETL3: invalid channel (%d)\n", ch);
373 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
375 "ISDN_CMD_SETL3: unsupported protocol (%lu)\n",
381 case ISDN_CMD_PROCEED:
382 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROCEED"); //FIXME
385 gig_dbg(DEBUG_ANY, "ISDN_CMD_ALERT"); //FIXME
386 if (cntrl->arg >= cs->channels) {
388 "ISDN_CMD_ALERT: invalid channel (%d)\n",
392 //bcs = cs->bcs + cntrl->arg;
397 gig_dbg(DEBUG_ANY, "ISDN_CMD_REDIR"); //FIXME
399 case ISDN_CMD_PROT_IO:
400 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROT_IO");
402 case ISDN_CMD_FAXCMD:
403 gig_dbg(DEBUG_ANY, "ISDN_CMD_FAXCMD");
406 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL2");
409 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL3");
411 case ISDN_CMD_GETEAZ:
412 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETEAZ");
414 case ISDN_CMD_SETSIL:
415 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETSIL");
417 case ISDN_CMD_GETSIL:
418 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETSIL");
421 dev_err(cs->dev, "unknown command %d from LL\n",
429 dev_err(bcs->cs->dev, "out of memory\n");
430 for (i = 0; i < AT_NUM; ++i)
435 static void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
437 isdn_if *iif = cs->iif;
440 command.driver = cs->myid;
441 command.command = cmd;
443 iif->statcallb(&command);
446 static void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
448 isdn_if *iif = bcs->cs->iif;
451 command.driver = bcs->cs->myid;
452 command.command = cmd;
453 command.arg = bcs->channel;
454 iif->statcallb(&command);
458 * gigaset_isdn_icall() - signal incoming call
459 * @at_state: connection state structure.
461 * Called by main module to notify the LL that an incoming call has been
462 * received. @at_state contains the parameters of the call.
464 * Return value: call disposition (ICALL_*)
466 int gigaset_isdn_icall(struct at_state_t *at_state)
468 struct cardstate *cs = at_state->cs;
469 struct bc_state *bcs = at_state->bcs;
470 isdn_if *iif = cs->iif;
474 /* fill ICALL structure */
475 response.parm.setup.si1 = 0; /* default: unknown */
476 response.parm.setup.si2 = 0;
477 response.parm.setup.screen = 0; //FIXME how to set these?
478 response.parm.setup.plan = 0;
479 if (!at_state->str_var[STR_ZBC]) {
480 /* no BC (internal call): assume speech, A-law */
481 response.parm.setup.si1 = 1;
482 } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
483 /* unrestricted digital information */
484 response.parm.setup.si1 = 7;
485 } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
487 response.parm.setup.si1 = 1;
488 } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
489 /* 3,1 kHz audio, A-law */
490 response.parm.setup.si1 = 1;
491 response.parm.setup.si2 = 2;
493 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
494 at_state->str_var[STR_ZBC]);
497 if (at_state->str_var[STR_NMBR]) {
498 strncpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
499 sizeof response.parm.setup.phone - 1);
500 response.parm.setup.phone[sizeof response.parm.setup.phone - 1] = 0;
502 response.parm.setup.phone[0] = 0;
503 if (at_state->str_var[STR_ZCPN]) {
504 strncpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
505 sizeof response.parm.setup.eazmsn - 1);
506 response.parm.setup.eazmsn[sizeof response.parm.setup.eazmsn - 1] = 0;
508 response.parm.setup.eazmsn[0] = 0;
511 dev_notice(cs->dev, "no channel for incoming call\n");
512 response.command = ISDN_STAT_ICALLW;
513 response.arg = 0; //FIXME
515 gig_dbg(DEBUG_CMD, "Sending ICALL");
516 response.command = ISDN_STAT_ICALL;
517 response.arg = bcs->channel; //FIXME
519 response.driver = cs->myid;
520 retval = iif->statcallb(&response);
521 gig_dbg(DEBUG_CMD, "Response: %d", retval);
523 case 0: /* no takers */
525 case 1: /* alerting */
526 bcs->chstate |= CHS_NOTIFY_LL;
530 case 3: /* incomplete */
532 "LL requested unsupported feature: Incomplete Number\n");
534 case 4: /* proceeding */
535 /* Gigaset will send ALERTING anyway.
536 * There doesn't seem to be a way to avoid this.
539 case 5: /* deflect */
541 "LL requested unsupported feature: Call Deflection\n");
544 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
550 * gigaset_isdn_connD() - signal D channel connect
551 * @bcs: B channel descriptor structure.
553 * Called by main module to notify the LL that the D channel connection has
556 void gigaset_isdn_connD(struct bc_state *bcs)
558 gig_dbg(DEBUG_CMD, "sending DCONN");
559 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
563 * gigaset_isdn_hupD() - signal D channel hangup
564 * @bcs: B channel descriptor structure.
566 * Called by main module to notify the LL that the D channel connection has
569 void gigaset_isdn_hupD(struct bc_state *bcs)
571 gig_dbg(DEBUG_CMD, "sending DHUP");
572 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
576 * gigaset_isdn_connB() - signal B channel connect
577 * @bcs: B channel descriptor structure.
579 * Called by main module to notify the LL that the B channel connection has
582 void gigaset_isdn_connB(struct bc_state *bcs)
584 gig_dbg(DEBUG_CMD, "sending BCONN");
585 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
589 * gigaset_isdn_hupB() - signal B channel hangup
590 * @bcs: B channel descriptor structure.
592 * Called by main module to notify the LL that the B channel connection has
595 void gigaset_isdn_hupB(struct bc_state *bcs)
597 gig_dbg(DEBUG_CMD, "sending BHUP");
598 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
602 * gigaset_isdn_start() - signal device availability
603 * @cs: device descriptor structure.
605 * Called by main module to notify the LL that the device is available for
608 void gigaset_isdn_start(struct cardstate *cs)
610 gig_dbg(DEBUG_CMD, "sending RUN");
611 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
615 * gigaset_isdn_stop() - signal device unavailability
616 * @cs: device descriptor structure.
618 * Called by main module to notify the LL that the device is no longer
621 void gigaset_isdn_stop(struct cardstate *cs)
623 gig_dbg(DEBUG_CMD, "sending STOP");
624 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
628 * gigaset_isdn_register() - register to LL
629 * @cs: device descriptor structure.
630 * @isdnid: device name.
632 * Called by main module to register the device with the LL.
634 * Return value: 1 for success, 0 for failure
636 int gigaset_isdn_register(struct cardstate *cs, const char *isdnid)
640 pr_info("ISDN4Linux interface\n");
642 iif = kmalloc(sizeof *iif, GFP_KERNEL);
644 pr_err("out of memory\n");
648 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
650 pr_err("ID too long: %s\n", isdnid);
655 iif->owner = THIS_MODULE;
656 iif->channels = cs->channels;
657 iif->maxbufsize = MAX_BUF_SIZE;
658 iif->features = ISDN_FEATURE_L2_TRANS |
659 ISDN_FEATURE_L2_HDLC |
661 ISDN_FEATURE_L2_X75I |
663 ISDN_FEATURE_L3_TRANS |
665 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
666 iif->command = command_from_LL;
667 iif->writebuf_skb = writebuf_from_LL;
668 iif->writecmd = NULL; /* Don't support isdnctrl */
669 iif->readstat = NULL; /* Don't support isdnctrl */
670 iif->rcvcallb_skb = NULL; /* Will be set by LL */
671 iif->statcallb = NULL; /* Will be set by LL */
673 if (!register_isdn(iif)) {
674 pr_err("register_isdn failed\n");
680 cs->myid = iif->channels; /* Set my device id */
681 cs->hw_hdr_len = HW_HDR_LEN;
686 * gigaset_isdn_unregister() - unregister from LL
687 * @cs: device descriptor structure.
689 * Called by main module to unregister the device from the LL.
691 void gigaset_isdn_unregister(struct cardstate *cs)
693 gig_dbg(DEBUG_CMD, "sending UNLOAD");
694 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);