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>
18 #include <linux/slab.h>
20 #define HW_HDR_LEN 2 /* Header size used to store ack info */
22 /* == Handling of I4L IO =====================================================*/
25 * called by LL to transmit data on an open channel
26 * inserts the buffer data into the send queue and starts the transmission
27 * Note that this operation must not sleep!
28 * When the buffer is processed completely, gigaset_skb_sent() should be called.
30 * driverID driver ID as assigned by LL
31 * channel channel number
32 * ack if != 0 LL wants to be notified on completion via
33 * statcallb(ISDN_STAT_BSENT)
34 * skb skb containing data to send
36 * number of accepted bytes
37 * 0 if temporarily unable to accept data (out of buffer space)
38 * <0 on error (eg. -EINVAL)
40 static int writebuf_from_LL(int driverID, int channel, int ack,
43 struct cardstate *cs = gigaset_get_cs_by_id(driverID);
45 unsigned char *ack_header;
49 pr_err("%s: invalid driver ID (%d)\n", __func__, driverID);
52 if (channel < 0 || channel >= cs->channels) {
53 dev_err(cs->dev, "%s: invalid channel ID (%d)\n",
57 bcs = &cs->bcs[channel];
59 /* can only handle linear sk_buffs */
60 if (skb_linearize(skb) < 0) {
61 dev_err(cs->dev, "%s: skb_linearize failed\n", __func__);
67 "Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
68 driverID, channel, ack, len);
72 dev_notice(cs->dev, "%s: not ACKing empty packet\n",
76 if (len > MAX_BUF_SIZE) {
77 dev_err(cs->dev, "%s: packet too large (%d bytes)\n",
82 /* set up acknowledgement header */
83 if (skb_headroom(skb) < HW_HDR_LEN) {
84 /* should never happen */
85 dev_err(cs->dev, "%s: insufficient skb headroom\n", __func__);
88 skb_set_mac_header(skb, -HW_HDR_LEN);
89 skb->mac_len = HW_HDR_LEN;
90 ack_header = skb_mac_header(skb);
92 ack_header[0] = len & 0xff;
93 ack_header[1] = len >> 8;
95 ack_header[0] = ack_header[1] = 0;
97 gig_dbg(DEBUG_MCMD, "skb: len=%u, ack=%d: %02x %02x",
98 len, ack, ack_header[0], ack_header[1]);
100 /* pass to device-specific module */
101 return cs->ops->send_skb(bcs, skb);
105 * gigaset_skb_sent() - acknowledge sending an skb
106 * @bcs: B channel descriptor structure.
109 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
110 * skb has been successfully sent, for signalling completion to the LL.
112 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
114 isdn_if *iif = bcs->cs->iif;
115 unsigned char *ack_header = skb_mac_header(skb);
122 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
125 len = ack_header[0] + ((unsigned) ack_header[1] << 8);
127 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
128 bcs->cs->myid, bcs->channel, len);
130 response.driver = bcs->cs->myid;
131 response.command = ISDN_STAT_BSENT;
132 response.arg = bcs->channel;
133 response.parm.length = len;
134 iif->statcallb(&response);
137 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
140 * gigaset_skb_rcvd() - pass received skb to LL
141 * @bcs: B channel descriptor structure.
142 * @skb: received data.
144 * Called by hardware module {bas,ser,usb}_gigaset when user data has
145 * been successfully received, for passing to the LL.
146 * Warning: skb must not be accessed anymore!
148 void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
150 isdn_if *iif = bcs->cs->iif;
152 iif->rcvcallb_skb(bcs->cs->myid, bcs->channel, skb);
155 EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
158 * gigaset_isdn_rcv_err() - signal receive error
159 * @bcs: B channel descriptor structure.
161 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
162 * has occurred, for signalling to the LL.
164 void gigaset_isdn_rcv_err(struct bc_state *bcs)
166 isdn_if *iif = bcs->cs->iif;
169 /* if currently ignoring packets, just count down */
175 /* update statistics */
179 gig_dbg(DEBUG_CMD, "sending L1ERR");
180 response.driver = bcs->cs->myid;
181 response.command = ISDN_STAT_L1ERR;
182 response.arg = bcs->channel;
183 response.parm.errcode = ISDN_STAT_L1ERR_RECV;
184 iif->statcallb(&response);
186 EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
188 /* This function will be called by LL to send commands
189 * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
190 * so don't put too much effort into it.
192 static int command_from_LL(isdn_ctrl *cntrl)
194 struct cardstate *cs;
195 struct bc_state *bcs;
202 gigaset_debugdrivers();
204 gig_dbg(DEBUG_CMD, "driver: %d, command: %d, arg: 0x%lx",
205 cntrl->driver, cntrl->command, cntrl->arg);
207 cs = gigaset_get_cs_by_id(cntrl->driver);
209 pr_err("%s: invalid driver ID (%d)\n", __func__, cntrl->driver);
212 ch = cntrl->arg & 0xff;
214 switch (cntrl->command) {
216 dev_warn(cs->dev, "ISDN_CMD_IOCTL not supported\n");
221 "ISDN_CMD_DIAL (phone: %s, msn: %s, si1: %d, si2: %d)",
222 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
223 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
225 if (ch >= cs->channels) {
227 "ISDN_CMD_DIAL: invalid channel (%d)\n", ch);
231 if (!gigaset_get_channel(bcs)) {
232 dev_err(cs->dev, "ISDN_CMD_DIAL: channel not free\n");
236 commands = kzalloc(AT_NUM*(sizeof *commands), GFP_ATOMIC);
238 gigaset_free_channel(bcs);
239 dev_err(cs->dev, "ISDN_CMD_DIAL: out of memory\n");
243 l = 3 + strlen(cntrl->parm.setup.phone);
244 commands[AT_DIAL] = kmalloc(l, GFP_ATOMIC);
245 if (!commands[AT_DIAL])
247 if (cntrl->parm.setup.phone[0] == '*' &&
248 cntrl->parm.setup.phone[1] == '*') {
249 /* internal call: translate ** prefix to CTP value */
250 commands[AT_TYPE] = kstrdup("^SCTP=0\r", GFP_ATOMIC);
251 if (!commands[AT_TYPE])
253 snprintf(commands[AT_DIAL], l,
254 "D%s\r", cntrl->parm.setup.phone+2);
256 commands[AT_TYPE] = kstrdup("^SCTP=1\r", GFP_ATOMIC);
257 if (!commands[AT_TYPE])
259 snprintf(commands[AT_DIAL], l,
260 "D%s\r", cntrl->parm.setup.phone);
263 l = strlen(cntrl->parm.setup.eazmsn);
266 commands[AT_MSN] = kmalloc(l, GFP_ATOMIC);
267 if (!commands[AT_MSN])
269 snprintf(commands[AT_MSN], l, "^SMSN=%s\r",
270 cntrl->parm.setup.eazmsn);
273 switch (cntrl->parm.setup.si1) {
275 /* BC = 9090A3: 3.1 kHz audio, A-law */
276 commands[AT_BC] = kstrdup("^SBC=9090A3\r", GFP_ATOMIC);
277 if (!commands[AT_BC])
281 default: /* hope the app knows what it is doing */
282 /* BC = 8890: unrestricted digital information */
283 commands[AT_BC] = kstrdup("^SBC=8890\r", GFP_ATOMIC);
284 if (!commands[AT_BC])
287 /* ToDo: other si1 values, inspect si2, set HLC/LLC */
289 commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
290 if (!commands[AT_PROTO])
292 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
294 commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
295 if (!commands[AT_ISO])
297 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
298 (unsigned) bcs->channel + 1);
300 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
301 bcs->at_state.seq_index, NULL)) {
302 for (i = 0; i < AT_NUM; ++i)
305 gigaset_free_channel(bcs);
308 gigaset_schedule_event(cs);
310 case ISDN_CMD_ACCEPTD:
311 gig_dbg(DEBUG_CMD, "ISDN_CMD_ACCEPTD");
312 if (ch >= cs->channels) {
314 "ISDN_CMD_ACCEPTD: invalid channel (%d)\n", ch);
318 if (!gigaset_add_event(cs, &bcs->at_state,
319 EV_ACCEPT, NULL, 0, NULL))
321 gigaset_schedule_event(cs);
324 case ISDN_CMD_HANGUP:
325 gig_dbg(DEBUG_CMD, "ISDN_CMD_HANGUP");
326 if (ch >= cs->channels) {
328 "ISDN_CMD_HANGUP: invalid channel (%d)\n", ch);
332 if (!gigaset_add_event(cs, &bcs->at_state,
333 EV_HUP, NULL, 0, NULL))
335 gigaset_schedule_event(cs);
338 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */
339 dev_info(cs->dev, "ignoring ISDN_CMD_CLREAZ\n");
341 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */
342 dev_info(cs->dev, "ignoring ISDN_CMD_SETEAZ (%s)\n",
345 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
346 if (ch >= cs->channels) {
348 "ISDN_CMD_SETL2: invalid channel (%d)\n", ch);
352 if (bcs->chstate & CHS_D_UP) {
354 "ISDN_CMD_SETL2: channel active (%d)\n", ch);
357 switch (cntrl->arg >> 8) {
358 case ISDN_PROTO_L2_HDLC:
359 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_HDLC");
360 bcs->proto2 = L2_HDLC;
362 case ISDN_PROTO_L2_TRANS:
363 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_VOICE");
364 bcs->proto2 = L2_VOICE;
368 "ISDN_CMD_SETL2: unsupported protocol (%lu)\n",
373 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
374 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL3");
375 if (ch >= cs->channels) {
377 "ISDN_CMD_SETL3: invalid channel (%d)\n", ch);
381 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
383 "ISDN_CMD_SETL3: unsupported protocol (%lu)\n",
391 gig_dbg(DEBUG_CMD, "unknown command %d from LL",
399 dev_err(bcs->cs->dev, "out of memory\n");
400 for (i = 0; i < AT_NUM; ++i)
405 static void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
407 isdn_if *iif = cs->iif;
410 command.driver = cs->myid;
411 command.command = cmd;
413 iif->statcallb(&command);
416 static void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
418 isdn_if *iif = bcs->cs->iif;
421 command.driver = bcs->cs->myid;
422 command.command = cmd;
423 command.arg = bcs->channel;
424 iif->statcallb(&command);
428 * gigaset_isdn_icall() - signal incoming call
429 * @at_state: connection state structure.
431 * Called by main module to notify the LL that an incoming call has been
432 * received. @at_state contains the parameters of the call.
434 * Return value: call disposition (ICALL_*)
436 int gigaset_isdn_icall(struct at_state_t *at_state)
438 struct cardstate *cs = at_state->cs;
439 struct bc_state *bcs = at_state->bcs;
440 isdn_if *iif = cs->iif;
444 /* fill ICALL structure */
445 response.parm.setup.si1 = 0; /* default: unknown */
446 response.parm.setup.si2 = 0;
447 response.parm.setup.screen = 0;
448 response.parm.setup.plan = 0;
449 if (!at_state->str_var[STR_ZBC]) {
450 /* no BC (internal call): assume speech, A-law */
451 response.parm.setup.si1 = 1;
452 } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
453 /* unrestricted digital information */
454 response.parm.setup.si1 = 7;
455 } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
457 response.parm.setup.si1 = 1;
458 } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
459 /* 3,1 kHz audio, A-law */
460 response.parm.setup.si1 = 1;
461 response.parm.setup.si2 = 2;
463 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
464 at_state->str_var[STR_ZBC]);
467 if (at_state->str_var[STR_NMBR]) {
468 strlcpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
469 sizeof response.parm.setup.phone);
471 response.parm.setup.phone[0] = 0;
472 if (at_state->str_var[STR_ZCPN]) {
473 strlcpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
474 sizeof response.parm.setup.eazmsn);
476 response.parm.setup.eazmsn[0] = 0;
479 dev_notice(cs->dev, "no channel for incoming call\n");
480 response.command = ISDN_STAT_ICALLW;
483 gig_dbg(DEBUG_CMD, "Sending ICALL");
484 response.command = ISDN_STAT_ICALL;
485 response.arg = bcs->channel;
487 response.driver = cs->myid;
488 retval = iif->statcallb(&response);
489 gig_dbg(DEBUG_CMD, "Response: %d", retval);
491 case 0: /* no takers */
493 case 1: /* alerting */
494 bcs->chstate |= CHS_NOTIFY_LL;
498 case 3: /* incomplete */
500 "LL requested unsupported feature: Incomplete Number\n");
502 case 4: /* proceeding */
503 /* Gigaset will send ALERTING anyway.
504 * There doesn't seem to be a way to avoid this.
507 case 5: /* deflect */
509 "LL requested unsupported feature: Call Deflection\n");
512 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
518 * gigaset_isdn_connD() - signal D channel connect
519 * @bcs: B channel descriptor structure.
521 * Called by main module to notify the LL that the D channel connection has
524 void gigaset_isdn_connD(struct bc_state *bcs)
526 gig_dbg(DEBUG_CMD, "sending DCONN");
527 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
531 * gigaset_isdn_hupD() - signal D channel hangup
532 * @bcs: B channel descriptor structure.
534 * Called by main module to notify the LL that the D channel connection has
537 void gigaset_isdn_hupD(struct bc_state *bcs)
539 gig_dbg(DEBUG_CMD, "sending DHUP");
540 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
544 * gigaset_isdn_connB() - signal B channel connect
545 * @bcs: B channel descriptor structure.
547 * Called by main module to notify the LL that the B channel connection has
550 void gigaset_isdn_connB(struct bc_state *bcs)
552 gig_dbg(DEBUG_CMD, "sending BCONN");
553 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
557 * gigaset_isdn_hupB() - signal B channel hangup
558 * @bcs: B channel descriptor structure.
560 * Called by main module to notify the LL that the B channel connection has
563 void gigaset_isdn_hupB(struct bc_state *bcs)
565 gig_dbg(DEBUG_CMD, "sending BHUP");
566 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
570 * gigaset_isdn_start() - signal device availability
571 * @cs: device descriptor structure.
573 * Called by main module to notify the LL that the device is available for
576 void gigaset_isdn_start(struct cardstate *cs)
578 gig_dbg(DEBUG_CMD, "sending RUN");
579 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
583 * gigaset_isdn_stop() - signal device unavailability
584 * @cs: device descriptor structure.
586 * Called by main module to notify the LL that the device is no longer
589 void gigaset_isdn_stop(struct cardstate *cs)
591 gig_dbg(DEBUG_CMD, "sending STOP");
592 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
596 * gigaset_isdn_regdev() - register to LL
597 * @cs: device descriptor structure.
598 * @isdnid: device name.
600 * Return value: 1 for success, 0 for failure
602 int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
606 pr_info("ISDN4Linux interface\n");
608 iif = kmalloc(sizeof *iif, GFP_KERNEL);
610 pr_err("out of memory\n");
614 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
616 pr_err("ID too long: %s\n", isdnid);
621 iif->owner = THIS_MODULE;
622 iif->channels = cs->channels;
623 iif->maxbufsize = MAX_BUF_SIZE;
624 iif->features = ISDN_FEATURE_L2_TRANS |
625 ISDN_FEATURE_L2_HDLC |
627 ISDN_FEATURE_L2_X75I |
629 ISDN_FEATURE_L3_TRANS |
631 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
632 iif->command = command_from_LL;
633 iif->writebuf_skb = writebuf_from_LL;
634 iif->writecmd = NULL; /* Don't support isdnctrl */
635 iif->readstat = NULL; /* Don't support isdnctrl */
636 iif->rcvcallb_skb = NULL; /* Will be set by LL */
637 iif->statcallb = NULL; /* Will be set by LL */
639 if (!register_isdn(iif)) {
640 pr_err("register_isdn failed\n");
646 cs->myid = iif->channels; /* Set my device id */
647 cs->hw_hdr_len = HW_HDR_LEN;
652 * gigaset_isdn_unregdev() - unregister device from LL
653 * @cs: device descriptor structure.
655 void gigaset_isdn_unregdev(struct cardstate *cs)
657 gig_dbg(DEBUG_CMD, "sending UNLOAD");
658 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
664 * gigaset_isdn_regdrv() - register driver to LL
666 void gigaset_isdn_regdrv(void)
672 * gigaset_isdn_unregdrv() - unregister driver from LL
674 void gigaset_isdn_unregdrv(void)