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,
42 struct cardstate *cs = gigaset_get_cs_by_id(driverID);
44 unsigned char *ack_header;
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 /* set up acknowledgement header */
82 if (skb_headroom(skb) < HW_HDR_LEN) {
83 /* should never happen */
84 dev_err(cs->dev, "%s: insufficient skb headroom\n", __func__);
87 skb_set_mac_header(skb, -HW_HDR_LEN);
88 skb->mac_len = HW_HDR_LEN;
89 ack_header = skb_mac_header(skb);
91 ack_header[0] = len & 0xff;
92 ack_header[1] = len >> 8;
94 ack_header[0] = ack_header[1] = 0;
96 gig_dbg(DEBUG_MCMD, "skb: len=%u, ack=%d: %02x %02x",
97 len, ack, ack_header[0], ack_header[1]);
99 /* pass to device-specific module */
100 return cs->ops->send_skb(bcs, skb);
104 * gigaset_skb_sent() - acknowledge sending an skb
105 * @bcs: B channel descriptor structure.
108 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
109 * skb has been successfully sent, for signalling completion to the LL.
111 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
113 isdn_if *iif = bcs->cs->iif;
114 unsigned char *ack_header = skb_mac_header(skb);
121 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
124 len = ack_header[0] + ((unsigned) ack_header[1] << 8);
126 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
127 bcs->cs->myid, bcs->channel, len);
129 response.driver = bcs->cs->myid;
130 response.command = ISDN_STAT_BSENT;
131 response.arg = bcs->channel;
132 response.parm.length = len;
133 iif->statcallb(&response);
136 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
139 * gigaset_skb_rcvd() - pass received skb to LL
140 * @bcs: B channel descriptor structure.
141 * @skb: received data.
143 * Called by hardware module {bas,ser,usb}_gigaset when user data has
144 * been successfully received, for passing to the LL.
145 * Warning: skb must not be accessed anymore!
147 void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
149 isdn_if *iif = bcs->cs->iif;
151 iif->rcvcallb_skb(bcs->cs->myid, bcs->channel, skb);
154 EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
157 * gigaset_isdn_rcv_err() - signal receive error
158 * @bcs: B channel descriptor structure.
160 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
161 * has occurred, for signalling to the LL.
163 void gigaset_isdn_rcv_err(struct bc_state *bcs)
165 isdn_if *iif = bcs->cs->iif;
168 /* if currently ignoring packets, just count down */
174 /* update statistics */
178 gig_dbg(DEBUG_CMD, "sending L1ERR");
179 response.driver = bcs->cs->myid;
180 response.command = ISDN_STAT_L1ERR;
181 response.arg = bcs->channel;
182 response.parm.errcode = ISDN_STAT_L1ERR_RECV;
183 iif->statcallb(&response);
185 EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
187 /* This function will be called by LL to send commands
188 * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
189 * so don't put too much effort into it.
191 static int command_from_LL(isdn_ctrl *cntrl)
193 struct cardstate *cs;
194 struct bc_state *bcs;
201 gigaset_debugdrivers();
203 gig_dbg(DEBUG_CMD, "driver: %d, command: %d, arg: 0x%lx",
204 cntrl->driver, cntrl->command, cntrl->arg);
206 cs = gigaset_get_cs_by_id(cntrl->driver);
208 pr_err("%s: invalid driver ID (%d)\n", __func__, cntrl->driver);
211 ch = cntrl->arg & 0xff;
213 switch (cntrl->command) {
215 dev_warn(cs->dev, "ISDN_CMD_IOCTL not supported\n");
220 "ISDN_CMD_DIAL (phone: %s, msn: %s, si1: %d, si2: %d)",
221 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
222 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
224 if (ch >= cs->channels) {
226 "ISDN_CMD_DIAL: invalid channel (%d)\n", ch);
230 if (!gigaset_get_channel(bcs)) {
231 dev_err(cs->dev, "ISDN_CMD_DIAL: channel not free\n");
235 commands = kzalloc(AT_NUM*(sizeof *commands), GFP_ATOMIC);
237 gigaset_free_channel(bcs);
238 dev_err(cs->dev, "ISDN_CMD_DIAL: out of memory\n");
242 l = 3 + strlen(cntrl->parm.setup.phone);
243 commands[AT_DIAL] = kmalloc(l, GFP_ATOMIC);
244 if (!commands[AT_DIAL])
246 if (cntrl->parm.setup.phone[0] == '*' &&
247 cntrl->parm.setup.phone[1] == '*') {
248 /* internal call: translate ** prefix to CTP value */
249 commands[AT_TYPE] = kstrdup("^SCTP=0\r", GFP_ATOMIC);
250 if (!commands[AT_TYPE])
252 snprintf(commands[AT_DIAL], l,
253 "D%s\r", cntrl->parm.setup.phone+2);
255 commands[AT_TYPE] = kstrdup("^SCTP=1\r", GFP_ATOMIC);
256 if (!commands[AT_TYPE])
258 snprintf(commands[AT_DIAL], l,
259 "D%s\r", cntrl->parm.setup.phone);
262 l = strlen(cntrl->parm.setup.eazmsn);
265 commands[AT_MSN] = kmalloc(l, GFP_ATOMIC);
266 if (!commands[AT_MSN])
268 snprintf(commands[AT_MSN], l, "^SMSN=%s\r",
269 cntrl->parm.setup.eazmsn);
272 switch (cntrl->parm.setup.si1) {
274 /* BC = 9090A3: 3.1 kHz audio, A-law */
275 commands[AT_BC] = kstrdup("^SBC=9090A3\r", GFP_ATOMIC);
276 if (!commands[AT_BC])
280 default: /* hope the app knows what it is doing */
281 /* BC = 8890: unrestricted digital information */
282 commands[AT_BC] = kstrdup("^SBC=8890\r", GFP_ATOMIC);
283 if (!commands[AT_BC])
286 /* ToDo: other si1 values, inspect si2, set HLC/LLC */
288 commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
289 if (!commands[AT_PROTO])
291 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
293 commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
294 if (!commands[AT_ISO])
296 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
297 (unsigned) bcs->channel + 1);
299 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
300 bcs->at_state.seq_index, NULL)) {
301 for (i = 0; i < AT_NUM; ++i)
304 gigaset_free_channel(bcs);
307 gigaset_schedule_event(cs);
309 case ISDN_CMD_ACCEPTD:
310 gig_dbg(DEBUG_CMD, "ISDN_CMD_ACCEPTD");
311 if (ch >= cs->channels) {
313 "ISDN_CMD_ACCEPTD: invalid channel (%d)\n", ch);
317 if (!gigaset_add_event(cs, &bcs->at_state,
318 EV_ACCEPT, NULL, 0, NULL))
320 gigaset_schedule_event(cs);
323 case ISDN_CMD_HANGUP:
324 gig_dbg(DEBUG_CMD, "ISDN_CMD_HANGUP");
325 if (ch >= cs->channels) {
327 "ISDN_CMD_HANGUP: invalid channel (%d)\n", ch);
331 if (!gigaset_add_event(cs, &bcs->at_state,
332 EV_HUP, NULL, 0, NULL))
334 gigaset_schedule_event(cs);
337 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */
338 dev_info(cs->dev, "ignoring ISDN_CMD_CLREAZ\n");
340 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */
341 dev_info(cs->dev, "ignoring ISDN_CMD_SETEAZ (%s)\n",
344 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
345 if (ch >= cs->channels) {
347 "ISDN_CMD_SETL2: invalid channel (%d)\n", ch);
351 if (bcs->chstate & CHS_D_UP) {
353 "ISDN_CMD_SETL2: channel active (%d)\n", ch);
356 switch (cntrl->arg >> 8) {
357 case ISDN_PROTO_L2_HDLC:
358 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_HDLC");
359 bcs->proto2 = L2_HDLC;
361 case ISDN_PROTO_L2_TRANS:
362 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_VOICE");
363 bcs->proto2 = L2_VOICE;
367 "ISDN_CMD_SETL2: unsupported protocol (%lu)\n",
372 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
373 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL3");
374 if (ch >= cs->channels) {
376 "ISDN_CMD_SETL3: invalid channel (%d)\n", ch);
380 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
382 "ISDN_CMD_SETL3: unsupported protocol (%lu)\n",
390 gig_dbg(DEBUG_CMD, "unknown command %d from LL",
398 dev_err(bcs->cs->dev, "out of memory\n");
399 for (i = 0; i < AT_NUM; ++i)
404 static void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
406 isdn_if *iif = cs->iif;
409 command.driver = cs->myid;
410 command.command = cmd;
412 iif->statcallb(&command);
415 static void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
417 isdn_if *iif = bcs->cs->iif;
420 command.driver = bcs->cs->myid;
421 command.command = cmd;
422 command.arg = bcs->channel;
423 iif->statcallb(&command);
427 * gigaset_isdn_icall() - signal incoming call
428 * @at_state: connection state structure.
430 * Called by main module to notify the LL that an incoming call has been
431 * received. @at_state contains the parameters of the call.
433 * Return value: call disposition (ICALL_*)
435 int gigaset_isdn_icall(struct at_state_t *at_state)
437 struct cardstate *cs = at_state->cs;
438 struct bc_state *bcs = at_state->bcs;
439 isdn_if *iif = cs->iif;
443 /* fill ICALL structure */
444 response.parm.setup.si1 = 0; /* default: unknown */
445 response.parm.setup.si2 = 0;
446 response.parm.setup.screen = 0;
447 response.parm.setup.plan = 0;
448 if (!at_state->str_var[STR_ZBC]) {
449 /* no BC (internal call): assume speech, A-law */
450 response.parm.setup.si1 = 1;
451 } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
452 /* unrestricted digital information */
453 response.parm.setup.si1 = 7;
454 } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
456 response.parm.setup.si1 = 1;
457 } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
458 /* 3,1 kHz audio, A-law */
459 response.parm.setup.si1 = 1;
460 response.parm.setup.si2 = 2;
462 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
463 at_state->str_var[STR_ZBC]);
466 if (at_state->str_var[STR_NMBR]) {
467 strlcpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
468 sizeof response.parm.setup.phone);
470 response.parm.setup.phone[0] = 0;
471 if (at_state->str_var[STR_ZCPN]) {
472 strlcpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
473 sizeof response.parm.setup.eazmsn);
475 response.parm.setup.eazmsn[0] = 0;
478 dev_notice(cs->dev, "no channel for incoming call\n");
479 response.command = ISDN_STAT_ICALLW;
482 gig_dbg(DEBUG_CMD, "Sending ICALL");
483 response.command = ISDN_STAT_ICALL;
484 response.arg = bcs->channel;
486 response.driver = cs->myid;
487 retval = iif->statcallb(&response);
488 gig_dbg(DEBUG_CMD, "Response: %d", retval);
490 case 0: /* no takers */
492 case 1: /* alerting */
493 bcs->chstate |= CHS_NOTIFY_LL;
497 case 3: /* incomplete */
499 "LL requested unsupported feature: Incomplete Number\n");
501 case 4: /* proceeding */
502 /* Gigaset will send ALERTING anyway.
503 * There doesn't seem to be a way to avoid this.
506 case 5: /* deflect */
508 "LL requested unsupported feature: Call Deflection\n");
511 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
517 * gigaset_isdn_connD() - signal D channel connect
518 * @bcs: B channel descriptor structure.
520 * Called by main module to notify the LL that the D channel connection has
523 void gigaset_isdn_connD(struct bc_state *bcs)
525 gig_dbg(DEBUG_CMD, "sending DCONN");
526 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
530 * gigaset_isdn_hupD() - signal D channel hangup
531 * @bcs: B channel descriptor structure.
533 * Called by main module to notify the LL that the D channel connection has
536 void gigaset_isdn_hupD(struct bc_state *bcs)
538 gig_dbg(DEBUG_CMD, "sending DHUP");
539 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
543 * gigaset_isdn_connB() - signal B channel connect
544 * @bcs: B channel descriptor structure.
546 * Called by main module to notify the LL that the B channel connection has
549 void gigaset_isdn_connB(struct bc_state *bcs)
551 gig_dbg(DEBUG_CMD, "sending BCONN");
552 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
556 * gigaset_isdn_hupB() - signal B channel hangup
557 * @bcs: B channel descriptor structure.
559 * Called by main module to notify the LL that the B channel connection has
562 void gigaset_isdn_hupB(struct bc_state *bcs)
564 gig_dbg(DEBUG_CMD, "sending BHUP");
565 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
569 * gigaset_isdn_start() - signal device availability
570 * @cs: device descriptor structure.
572 * Called by main module to notify the LL that the device is available for
575 void gigaset_isdn_start(struct cardstate *cs)
577 gig_dbg(DEBUG_CMD, "sending RUN");
578 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
582 * gigaset_isdn_stop() - signal device unavailability
583 * @cs: device descriptor structure.
585 * Called by main module to notify the LL that the device is no longer
588 void gigaset_isdn_stop(struct cardstate *cs)
590 gig_dbg(DEBUG_CMD, "sending STOP");
591 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
595 * gigaset_isdn_regdev() - register to LL
596 * @cs: device descriptor structure.
597 * @isdnid: device name.
599 * Return value: 1 for success, 0 for failure
601 int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
605 pr_info("ISDN4Linux interface\n");
607 iif = kmalloc(sizeof *iif, GFP_KERNEL);
609 pr_err("out of memory\n");
613 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
615 pr_err("ID too long: %s\n", isdnid);
620 iif->owner = THIS_MODULE;
621 iif->channels = cs->channels;
622 iif->maxbufsize = MAX_BUF_SIZE;
623 iif->features = ISDN_FEATURE_L2_TRANS |
624 ISDN_FEATURE_L2_HDLC |
626 ISDN_FEATURE_L2_X75I |
628 ISDN_FEATURE_L3_TRANS |
630 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
631 iif->command = command_from_LL;
632 iif->writebuf_skb = writebuf_from_LL;
633 iif->writecmd = NULL; /* Don't support isdnctrl */
634 iif->readstat = NULL; /* Don't support isdnctrl */
635 iif->rcvcallb_skb = NULL; /* Will be set by LL */
636 iif->statcallb = NULL; /* Will be set by LL */
638 if (!register_isdn(iif)) {
639 pr_err("register_isdn failed\n");
645 cs->myid = iif->channels; /* Set my device id */
646 cs->hw_hdr_len = HW_HDR_LEN;
651 * gigaset_isdn_unregdev() - unregister device from LL
652 * @cs: device descriptor structure.
654 void gigaset_isdn_unregdev(struct cardstate *cs)
656 gig_dbg(DEBUG_CMD, "sending UNLOAD");
657 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
663 * gigaset_isdn_regdrv() - register driver to LL
665 void gigaset_isdn_regdrv(void)
671 * gigaset_isdn_unregdrv() - unregister driver from LL
673 void gigaset_isdn_unregdrv(void)