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/export.h>
20 #define SBUFSIZE 4096 /* sk_buff payload size */
21 #define TRANSBUFSIZE 768 /* bytes per skb for transparent receive */
22 #define HW_HDR_LEN 2 /* Header size used to store ack info */
23 #define MAX_BUF_SIZE (SBUFSIZE - HW_HDR_LEN) /* max data packet from LL */
25 /* == Handling of I4L IO =====================================================*/
28 * called by LL to transmit data on an open channel
29 * inserts the buffer data into the send queue and starts the transmission
30 * Note that this operation must not sleep!
31 * When the buffer is processed completely, gigaset_skb_sent() should be called.
33 * driverID driver ID as assigned by LL
34 * channel channel number
35 * ack if != 0 LL wants to be notified on completion via
36 * statcallb(ISDN_STAT_BSENT)
37 * skb skb containing data to send
39 * number of accepted bytes
40 * 0 if temporarily unable to accept data (out of buffer space)
41 * <0 on error (eg. -EINVAL)
43 static int writebuf_from_LL(int driverID, int channel, int ack,
46 struct cardstate *cs = gigaset_get_cs_by_id(driverID);
48 unsigned char *ack_header;
52 pr_err("%s: invalid driver ID (%d)\n", __func__, driverID);
55 if (channel < 0 || channel >= cs->channels) {
56 dev_err(cs->dev, "%s: invalid channel ID (%d)\n",
60 bcs = &cs->bcs[channel];
62 /* can only handle linear sk_buffs */
63 if (skb_linearize(skb) < 0) {
64 dev_err(cs->dev, "%s: skb_linearize failed\n", __func__);
70 "Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
71 driverID, channel, ack, len);
75 dev_notice(cs->dev, "%s: not ACKing empty packet\n",
79 if (len > MAX_BUF_SIZE) {
80 dev_err(cs->dev, "%s: packet too large (%d bytes)\n",
85 /* set up acknowledgement header */
86 if (skb_headroom(skb) < HW_HDR_LEN) {
87 /* should never happen */
88 dev_err(cs->dev, "%s: insufficient skb headroom\n", __func__);
91 skb_set_mac_header(skb, -HW_HDR_LEN);
92 skb->mac_len = HW_HDR_LEN;
93 ack_header = skb_mac_header(skb);
95 ack_header[0] = len & 0xff;
96 ack_header[1] = len >> 8;
98 ack_header[0] = ack_header[1] = 0;
100 gig_dbg(DEBUG_MCMD, "skb: len=%u, ack=%d: %02x %02x",
101 len, ack, ack_header[0], ack_header[1]);
103 /* pass to device-specific module */
104 return cs->ops->send_skb(bcs, skb);
108 * gigaset_skb_sent() - acknowledge sending an skb
109 * @bcs: B channel descriptor structure.
112 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
113 * skb has been successfully sent, for signalling completion to the LL.
115 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
117 isdn_if *iif = bcs->cs->iif;
118 unsigned char *ack_header = skb_mac_header(skb);
125 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
128 len = ack_header[0] + ((unsigned) ack_header[1] << 8);
130 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
131 bcs->cs->myid, bcs->channel, len);
133 response.driver = bcs->cs->myid;
134 response.command = ISDN_STAT_BSENT;
135 response.arg = bcs->channel;
136 response.parm.length = len;
137 iif->statcallb(&response);
140 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
143 * gigaset_skb_rcvd() - pass received skb to LL
144 * @bcs: B channel descriptor structure.
145 * @skb: received data.
147 * Called by hardware module {bas,ser,usb}_gigaset when user data has
148 * been successfully received, for passing to the LL.
149 * Warning: skb must not be accessed anymore!
151 void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
153 isdn_if *iif = bcs->cs->iif;
155 iif->rcvcallb_skb(bcs->cs->myid, bcs->channel, skb);
158 EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
161 * gigaset_isdn_rcv_err() - signal receive error
162 * @bcs: B channel descriptor structure.
164 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
165 * has occurred, for signalling to the LL.
167 void gigaset_isdn_rcv_err(struct bc_state *bcs)
169 isdn_if *iif = bcs->cs->iif;
172 /* if currently ignoring packets, just count down */
178 /* update statistics */
182 gig_dbg(DEBUG_CMD, "sending L1ERR");
183 response.driver = bcs->cs->myid;
184 response.command = ISDN_STAT_L1ERR;
185 response.arg = bcs->channel;
186 response.parm.errcode = ISDN_STAT_L1ERR_RECV;
187 iif->statcallb(&response);
189 EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
191 /* This function will be called by LL to send commands
192 * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
193 * so don't put too much effort into it.
195 static int command_from_LL(isdn_ctrl *cntrl)
197 struct cardstate *cs;
198 struct bc_state *bcs;
205 gig_dbg(DEBUG_CMD, "driver: %d, command: %d, arg: 0x%lx",
206 cntrl->driver, cntrl->command, cntrl->arg);
208 cs = gigaset_get_cs_by_id(cntrl->driver);
210 pr_err("%s: invalid driver ID (%d)\n", __func__, cntrl->driver);
213 ch = cntrl->arg & 0xff;
215 switch (cntrl->command) {
217 dev_warn(cs->dev, "ISDN_CMD_IOCTL not supported\n");
222 "ISDN_CMD_DIAL (phone: %s, msn: %s, si1: %d, si2: %d)",
223 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
224 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
226 if (ch >= cs->channels) {
228 "ISDN_CMD_DIAL: invalid channel (%d)\n", ch);
232 if (gigaset_get_channel(bcs) < 0) {
233 dev_err(cs->dev, "ISDN_CMD_DIAL: channel not free\n");
236 switch (bcs->proto2) {
238 bcs->rx_bufsize = SBUFSIZE;
240 default: /* assume transparent */
241 bcs->rx_bufsize = TRANSBUFSIZE;
243 dev_kfree_skb(bcs->rx_skb);
244 gigaset_new_rx_skb(bcs);
246 commands = kzalloc(AT_NUM * (sizeof *commands), GFP_ATOMIC);
248 gigaset_free_channel(bcs);
249 dev_err(cs->dev, "ISDN_CMD_DIAL: out of memory\n");
253 l = 3 + strlen(cntrl->parm.setup.phone);
254 commands[AT_DIAL] = kmalloc(l, GFP_ATOMIC);
255 if (!commands[AT_DIAL])
257 if (cntrl->parm.setup.phone[0] == '*' &&
258 cntrl->parm.setup.phone[1] == '*') {
259 /* internal call: translate ** prefix to CTP value */
260 commands[AT_TYPE] = kstrdup("^SCTP=0\r", GFP_ATOMIC);
261 if (!commands[AT_TYPE])
263 snprintf(commands[AT_DIAL], l,
264 "D%s\r", cntrl->parm.setup.phone + 2);
266 commands[AT_TYPE] = kstrdup("^SCTP=1\r", GFP_ATOMIC);
267 if (!commands[AT_TYPE])
269 snprintf(commands[AT_DIAL], l,
270 "D%s\r", cntrl->parm.setup.phone);
273 l = strlen(cntrl->parm.setup.eazmsn);
276 commands[AT_MSN] = kmalloc(l, GFP_ATOMIC);
277 if (!commands[AT_MSN])
279 snprintf(commands[AT_MSN], l, "^SMSN=%s\r",
280 cntrl->parm.setup.eazmsn);
283 switch (cntrl->parm.setup.si1) {
285 /* BC = 9090A3: 3.1 kHz audio, A-law */
286 commands[AT_BC] = kstrdup("^SBC=9090A3\r", GFP_ATOMIC);
287 if (!commands[AT_BC])
291 default: /* hope the app knows what it is doing */
292 /* BC = 8890: unrestricted digital information */
293 commands[AT_BC] = kstrdup("^SBC=8890\r", GFP_ATOMIC);
294 if (!commands[AT_BC])
297 /* ToDo: other si1 values, inspect si2, set HLC/LLC */
299 commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
300 if (!commands[AT_PROTO])
302 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
304 commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
305 if (!commands[AT_ISO])
307 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
308 (unsigned) bcs->channel + 1);
310 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
311 bcs->at_state.seq_index, NULL)) {
312 for (i = 0; i < AT_NUM; ++i)
315 gigaset_free_channel(bcs);
318 gigaset_schedule_event(cs);
320 case ISDN_CMD_ACCEPTD:
321 gig_dbg(DEBUG_CMD, "ISDN_CMD_ACCEPTD");
322 if (ch >= cs->channels) {
324 "ISDN_CMD_ACCEPTD: invalid channel (%d)\n", ch);
328 switch (bcs->proto2) {
330 bcs->rx_bufsize = SBUFSIZE;
332 default: /* assume transparent */
333 bcs->rx_bufsize = TRANSBUFSIZE;
335 dev_kfree_skb(bcs->rx_skb);
336 gigaset_new_rx_skb(bcs);
337 if (!gigaset_add_event(cs, &bcs->at_state,
338 EV_ACCEPT, NULL, 0, NULL))
340 gigaset_schedule_event(cs);
343 case ISDN_CMD_HANGUP:
344 gig_dbg(DEBUG_CMD, "ISDN_CMD_HANGUP");
345 if (ch >= cs->channels) {
347 "ISDN_CMD_HANGUP: invalid channel (%d)\n", ch);
351 if (!gigaset_add_event(cs, &bcs->at_state,
352 EV_HUP, NULL, 0, NULL))
354 gigaset_schedule_event(cs);
357 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */
358 dev_info(cs->dev, "ignoring ISDN_CMD_CLREAZ\n");
360 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */
361 dev_info(cs->dev, "ignoring ISDN_CMD_SETEAZ (%s)\n",
364 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
365 if (ch >= cs->channels) {
367 "ISDN_CMD_SETL2: invalid channel (%d)\n", ch);
371 if (bcs->chstate & CHS_D_UP) {
373 "ISDN_CMD_SETL2: channel active (%d)\n", ch);
376 switch (cntrl->arg >> 8) {
377 case ISDN_PROTO_L2_HDLC:
378 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_HDLC");
379 bcs->proto2 = L2_HDLC;
381 case ISDN_PROTO_L2_TRANS:
382 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_VOICE");
383 bcs->proto2 = L2_VOICE;
387 "ISDN_CMD_SETL2: unsupported protocol (%lu)\n",
392 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
393 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL3");
394 if (ch >= cs->channels) {
396 "ISDN_CMD_SETL3: invalid channel (%d)\n", ch);
400 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
402 "ISDN_CMD_SETL3: unsupported protocol (%lu)\n",
410 gig_dbg(DEBUG_CMD, "unknown command %d from LL",
418 dev_err(bcs->cs->dev, "out of memory\n");
419 for (i = 0; i < AT_NUM; ++i)
422 gigaset_free_channel(bcs);
426 static void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
428 isdn_if *iif = cs->iif;
431 command.driver = cs->myid;
432 command.command = cmd;
434 iif->statcallb(&command);
437 static void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
439 isdn_if *iif = bcs->cs->iif;
442 command.driver = bcs->cs->myid;
443 command.command = cmd;
444 command.arg = bcs->channel;
445 iif->statcallb(&command);
449 * gigaset_isdn_icall() - signal incoming call
450 * @at_state: connection state structure.
452 * Called by main module to notify the LL that an incoming call has been
453 * received. @at_state contains the parameters of the call.
455 * Return value: call disposition (ICALL_*)
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;
461 isdn_if *iif = cs->iif;
465 /* fill ICALL structure */
466 response.parm.setup.si1 = 0; /* default: unknown */
467 response.parm.setup.si2 = 0;
468 response.parm.setup.screen = 0;
469 response.parm.setup.plan = 0;
470 if (!at_state->str_var[STR_ZBC]) {
471 /* no BC (internal call): assume speech, A-law */
472 response.parm.setup.si1 = 1;
473 } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
474 /* unrestricted digital information */
475 response.parm.setup.si1 = 7;
476 } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
478 response.parm.setup.si1 = 1;
479 } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
480 /* 3,1 kHz audio, A-law */
481 response.parm.setup.si1 = 1;
482 response.parm.setup.si2 = 2;
484 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
485 at_state->str_var[STR_ZBC]);
488 if (at_state->str_var[STR_NMBR]) {
489 strlcpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
490 sizeof response.parm.setup.phone);
492 response.parm.setup.phone[0] = 0;
493 if (at_state->str_var[STR_ZCPN]) {
494 strlcpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
495 sizeof response.parm.setup.eazmsn);
497 response.parm.setup.eazmsn[0] = 0;
500 dev_notice(cs->dev, "no channel for incoming call\n");
501 response.command = ISDN_STAT_ICALLW;
504 gig_dbg(DEBUG_CMD, "Sending ICALL");
505 response.command = ISDN_STAT_ICALL;
506 response.arg = bcs->channel;
508 response.driver = cs->myid;
509 retval = iif->statcallb(&response);
510 gig_dbg(DEBUG_CMD, "Response: %d", retval);
512 case 0: /* no takers */
514 case 1: /* alerting */
515 bcs->chstate |= CHS_NOTIFY_LL;
519 case 3: /* incomplete */
521 "LL requested unsupported feature: Incomplete Number\n");
523 case 4: /* proceeding */
524 /* Gigaset will send ALERTING anyway.
525 * There doesn't seem to be a way to avoid this.
528 case 5: /* deflect */
530 "LL requested unsupported feature: Call Deflection\n");
533 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
539 * gigaset_isdn_connD() - signal D channel connect
540 * @bcs: B channel descriptor structure.
542 * Called by main module to notify the LL that the D channel connection has
545 void gigaset_isdn_connD(struct bc_state *bcs)
547 gig_dbg(DEBUG_CMD, "sending DCONN");
548 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
552 * gigaset_isdn_hupD() - signal D channel hangup
553 * @bcs: B channel descriptor structure.
555 * Called by main module to notify the LL that the D channel connection has
558 void gigaset_isdn_hupD(struct bc_state *bcs)
560 gig_dbg(DEBUG_CMD, "sending DHUP");
561 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
565 * gigaset_isdn_connB() - signal B channel connect
566 * @bcs: B channel descriptor structure.
568 * Called by main module to notify the LL that the B channel connection has
571 void gigaset_isdn_connB(struct bc_state *bcs)
573 gig_dbg(DEBUG_CMD, "sending BCONN");
574 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
578 * gigaset_isdn_hupB() - signal B channel hangup
579 * @bcs: B channel descriptor structure.
581 * Called by main module to notify the LL that the B channel connection has
584 void gigaset_isdn_hupB(struct bc_state *bcs)
586 gig_dbg(DEBUG_CMD, "sending BHUP");
587 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
591 * gigaset_isdn_start() - signal device availability
592 * @cs: device descriptor structure.
594 * Called by main module to notify the LL that the device is available for
597 void gigaset_isdn_start(struct cardstate *cs)
599 gig_dbg(DEBUG_CMD, "sending RUN");
600 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
604 * gigaset_isdn_stop() - signal device unavailability
605 * @cs: device descriptor structure.
607 * Called by main module to notify the LL that the device is no longer
610 void gigaset_isdn_stop(struct cardstate *cs)
612 gig_dbg(DEBUG_CMD, "sending STOP");
613 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
617 * gigaset_isdn_regdev() - register to LL
618 * @cs: device descriptor structure.
619 * @isdnid: device name.
621 * Return value: 0 on success, error code < 0 on failure
623 int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
627 iif = kmalloc(sizeof *iif, GFP_KERNEL);
629 pr_err("out of memory\n");
633 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
635 pr_err("ID too long: %s\n", isdnid);
640 iif->owner = THIS_MODULE;
641 iif->channels = cs->channels;
642 iif->maxbufsize = MAX_BUF_SIZE;
643 iif->features = ISDN_FEATURE_L2_TRANS |
644 ISDN_FEATURE_L2_HDLC |
645 ISDN_FEATURE_L2_X75I |
646 ISDN_FEATURE_L3_TRANS |
648 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
649 iif->command = command_from_LL;
650 iif->writebuf_skb = writebuf_from_LL;
651 iif->writecmd = NULL; /* Don't support isdnctrl */
652 iif->readstat = NULL; /* Don't support isdnctrl */
653 iif->rcvcallb_skb = NULL; /* Will be set by LL */
654 iif->statcallb = NULL; /* Will be set by LL */
656 if (!register_isdn(iif)) {
657 pr_err("register_isdn failed\n");
663 cs->myid = iif->channels; /* Set my device id */
664 cs->hw_hdr_len = HW_HDR_LEN;
669 * gigaset_isdn_unregdev() - unregister device from LL
670 * @cs: device descriptor structure.
672 void gigaset_isdn_unregdev(struct cardstate *cs)
674 gig_dbg(DEBUG_CMD, "sending UNLOAD");
675 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
681 * gigaset_isdn_regdrv() - register driver to LL
683 void gigaset_isdn_regdrv(void)
685 pr_info("ISDN4Linux interface\n");
690 * gigaset_isdn_unregdrv() - unregister driver from LL
692 void gigaset_isdn_unregdrv(void)