blob: 2d75329007f158e31975a1d745e147a75ef8e7e6 [file] [log] [blame]
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -08001/*
2 * Stuff used by all variants of the driver
3 *
Tilman Schmidt70440cf2006-04-10 22:55:14 -07004 * Copyright (c) 2001 by Stefan Eilers,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Tilman Schmidt <tilman@imap.cc>.
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -08007 *
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 * =====================================================================
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080014 */
15
16#include "gigaset.h"
Tilman Schmidt088ec0c2009-10-06 12:19:07 +000017#include <linux/isdnif.h>
Paul Gortmaker5d76fc22011-07-10 12:23:16 -040018#include <linux/export.h>
Tilman Schmidt088ec0c2009-10-06 12:19:07 +000019
Tilman Schmidte7752ee2010-06-21 13:54:19 +000020#define SBUFSIZE 4096 /* sk_buff payload size */
21#define TRANSBUFSIZE 768 /* bytes per skb for transparent receive */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +000022#define HW_HDR_LEN 2 /* Header size used to store ack info */
Tilman Schmidte7752ee2010-06-21 13:54:19 +000023#define MAX_BUF_SIZE (SBUFSIZE - HW_HDR_LEN) /* max data packet from LL */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080024
Tilman Schmidt784d5852006-04-10 22:55:04 -070025/* == Handling of I4L IO =====================================================*/
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080026
27/* writebuf_from_LL
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.
32 * parameters:
33 * driverID driver ID as assigned by LL
34 * channel channel number
Tilman Schmidt917f5082006-04-10 22:55:00 -070035 * ack if != 0 LL wants to be notified on completion via
36 * statcallb(ISDN_STAT_BSENT)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080037 * skb skb containing data to send
38 * return value:
39 * number of accepted bytes
40 * 0 if temporarily unable to accept data (out of buffer space)
41 * <0 on error (eg. -EINVAL)
42 */
Tilman Schmidt784d5852006-04-10 22:55:04 -070043static int writebuf_from_LL(int driverID, int channel, int ack,
44 struct sk_buff *skb)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080045{
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +000046 struct cardstate *cs = gigaset_get_cs_by_id(driverID);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080047 struct bc_state *bcs;
Tilman Schmidt4dd82302009-10-25 09:29:57 +000048 unsigned char *ack_header;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080049 unsigned len;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080050
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +000051 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -080052 pr_err("%s: invalid driver ID (%d)\n", __func__, driverID);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080053 return -ENODEV;
54 }
55 if (channel < 0 || channel >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -070056 dev_err(cs->dev, "%s: invalid channel ID (%d)\n",
57 __func__, channel);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080058 return -ENODEV;
59 }
60 bcs = &cs->bcs[channel];
Tilman Schmidtee239d92009-10-06 12:18:31 +000061
62 /* can only handle linear sk_buffs */
63 if (skb_linearize(skb) < 0) {
64 dev_err(cs->dev, "%s: skb_linearize failed\n", __func__);
65 return -ENOMEM;
66 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080067 len = skb->len;
68
Tilman Schmidt784d5852006-04-10 22:55:04 -070069 gig_dbg(DEBUG_LLDATA,
70 "Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
71 driverID, channel, ack, len);
72
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080073 if (!len) {
74 if (ack)
Tilman Schmidt50027792008-07-23 21:28:27 -070075 dev_notice(cs->dev, "%s: not ACKing empty packet\n",
76 __func__);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080077 return 0;
78 }
79 if (len > MAX_BUF_SIZE) {
Tilman Schmidt50027792008-07-23 21:28:27 -070080 dev_err(cs->dev, "%s: packet too large (%d bytes)\n",
81 __func__, len);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080082 return -EINVAL;
83 }
84
Tilman Schmidt4dd82302009-10-25 09:29:57 +000085 /* 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__);
89 return -ENOMEM;
90 }
91 skb_set_mac_header(skb, -HW_HDR_LEN);
92 skb->mac_len = HW_HDR_LEN;
93 ack_header = skb_mac_header(skb);
94 if (ack) {
95 ack_header[0] = len & 0xff;
96 ack_header[1] = len >> 8;
97 } else {
98 ack_header[0] = ack_header[1] = 0;
99 }
100 gig_dbg(DEBUG_MCMD, "skb: len=%u, ack=%d: %02x %02x",
101 len, ack, ack_header[0], ack_header[1]);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800102
103 /* pass to device-specific module */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700104 return cs->ops->send_skb(bcs, skb);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800105}
106
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000107/**
108 * gigaset_skb_sent() - acknowledge sending an skb
109 * @bcs: B channel descriptor structure.
110 * @skb: sent data.
111 *
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.
114 */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800115void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
116{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000117 isdn_if *iif = bcs->cs->iif;
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000118 unsigned char *ack_header = skb_mac_header(skb);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800119 unsigned len;
120 isdn_ctrl response;
121
122 ++bcs->trans_up;
123
124 if (skb->len)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700125 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
126 __func__, skb->len);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800127
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000128 len = ack_header[0] + ((unsigned) ack_header[1] << 8);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800129 if (len) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700130 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
131 bcs->cs->myid, bcs->channel, len);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800132
133 response.driver = bcs->cs->myid;
134 response.command = ISDN_STAT_BSENT;
135 response.arg = bcs->channel;
136 response.parm.length = len;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000137 iif->statcallb(&response);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800138 }
139}
140EXPORT_SYMBOL_GPL(gigaset_skb_sent);
141
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000142/**
143 * gigaset_skb_rcvd() - pass received skb to LL
144 * @bcs: B channel descriptor structure.
145 * @skb: received data.
146 *
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!
150 */
151void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
152{
153 isdn_if *iif = bcs->cs->iif;
154
155 iif->rcvcallb_skb(bcs->cs->myid, bcs->channel, skb);
156 bcs->trans_down++;
157}
158EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
159
160/**
161 * gigaset_isdn_rcv_err() - signal receive error
162 * @bcs: B channel descriptor structure.
163 *
164 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
165 * has occurred, for signalling to the LL.
166 */
167void gigaset_isdn_rcv_err(struct bc_state *bcs)
168{
169 isdn_if *iif = bcs->cs->iif;
170 isdn_ctrl response;
171
172 /* if currently ignoring packets, just count down */
173 if (bcs->ignore) {
174 bcs->ignore--;
175 return;
176 }
177
178 /* update statistics */
179 bcs->corrupted++;
180
181 /* error -> LL */
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);
188}
189EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
190
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800191/* 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.
194 */
195static int command_from_LL(isdn_ctrl *cntrl)
196{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000197 struct cardstate *cs;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800198 struct bc_state *bcs;
199 int retval = 0;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000200 char **commands;
201 int ch;
202 int i;
203 size_t l;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800204
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000205 gig_dbg(DEBUG_CMD, "driver: %d, command: %d, arg: 0x%lx",
206 cntrl->driver, cntrl->command, cntrl->arg);
207
208 cs = gigaset_get_cs_by_id(cntrl->driver);
209 if (cs == NULL) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800210 pr_err("%s: invalid driver ID (%d)\n", __func__, cntrl->driver);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800211 return -ENODEV;
212 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000213 ch = cntrl->arg & 0xff;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800214
215 switch (cntrl->command) {
216 case ISDN_CMD_IOCTL:
Tilman Schmidt50027792008-07-23 21:28:27 -0700217 dev_warn(cs->dev, "ISDN_CMD_IOCTL not supported\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800218 return -EINVAL;
219
220 case ISDN_CMD_DIAL:
Tilman Schmidt1528b182010-02-22 13:09:52 +0000221 gig_dbg(DEBUG_CMD,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000222 "ISDN_CMD_DIAL (phone: %s, msn: %s, si1: %d, si2: %d)",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700223 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
224 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800225
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000226 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700227 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000228 "ISDN_CMD_DIAL: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800229 return -EINVAL;
230 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000231 bcs = cs->bcs + ch;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000232 if (gigaset_get_channel(bcs) < 0) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700233 dev_err(cs->dev, "ISDN_CMD_DIAL: channel not free\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800234 return -EBUSY;
235 }
Tilman Schmidte7752ee2010-06-21 13:54:19 +0000236 switch (bcs->proto2) {
237 case L2_HDLC:
238 bcs->rx_bufsize = SBUFSIZE;
239 break;
240 default: /* assume transparent */
241 bcs->rx_bufsize = TRANSBUFSIZE;
242 }
243 dev_kfree_skb(bcs->rx_skb);
244 gigaset_new_rx_skb(bcs);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800245
Joe Perches475be4d2012-02-19 19:52:38 -0800246 commands = kzalloc(AT_NUM * (sizeof *commands), GFP_ATOMIC);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000247 if (!commands) {
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800248 gigaset_free_channel(bcs);
Tilman Schmidt50027792008-07-23 21:28:27 -0700249 dev_err(cs->dev, "ISDN_CMD_DIAL: out of memory\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800250 return -ENOMEM;
251 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800252
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000253 l = 3 + strlen(cntrl->parm.setup.phone);
254 commands[AT_DIAL] = kmalloc(l, GFP_ATOMIC);
255 if (!commands[AT_DIAL])
256 goto oom;
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])
262 goto oom;
263 snprintf(commands[AT_DIAL], l,
Joe Perches475be4d2012-02-19 19:52:38 -0800264 "D%s\r", cntrl->parm.setup.phone + 2);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000265 } else {
266 commands[AT_TYPE] = kstrdup("^SCTP=1\r", GFP_ATOMIC);
267 if (!commands[AT_TYPE])
268 goto oom;
269 snprintf(commands[AT_DIAL], l,
270 "D%s\r", cntrl->parm.setup.phone);
271 }
272
273 l = strlen(cntrl->parm.setup.eazmsn);
274 if (l) {
275 l += 8;
276 commands[AT_MSN] = kmalloc(l, GFP_ATOMIC);
277 if (!commands[AT_MSN])
278 goto oom;
279 snprintf(commands[AT_MSN], l, "^SMSN=%s\r",
280 cntrl->parm.setup.eazmsn);
281 }
282
283 switch (cntrl->parm.setup.si1) {
284 case 1: /* audio */
285 /* BC = 9090A3: 3.1 kHz audio, A-law */
286 commands[AT_BC] = kstrdup("^SBC=9090A3\r", GFP_ATOMIC);
287 if (!commands[AT_BC])
288 goto oom;
289 break;
290 case 7: /* data */
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])
295 goto oom;
296 }
297 /* ToDo: other si1 values, inspect si2, set HLC/LLC */
298
299 commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
300 if (!commands[AT_PROTO])
301 goto oom;
302 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
303
304 commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
305 if (!commands[AT_ISO])
306 goto oom;
307 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
308 (unsigned) bcs->channel + 1);
309
310 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
Tilman Schmidt4d1ff582007-10-16 01:27:50 -0700311 bcs->at_state.seq_index, NULL)) {
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000312 for (i = 0; i < AT_NUM; ++i)
313 kfree(commands[i]);
314 kfree(commands);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800315 gigaset_free_channel(bcs);
316 return -ENOMEM;
317 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800318 gigaset_schedule_event(cs);
319 break;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000320 case ISDN_CMD_ACCEPTD:
Tilman Schmidt1528b182010-02-22 13:09:52 +0000321 gig_dbg(DEBUG_CMD, "ISDN_CMD_ACCEPTD");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000322 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700323 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000324 "ISDN_CMD_ACCEPTD: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800325 return -EINVAL;
326 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000327 bcs = cs->bcs + ch;
Tilman Schmidte7752ee2010-06-21 13:54:19 +0000328 switch (bcs->proto2) {
329 case L2_HDLC:
330 bcs->rx_bufsize = SBUFSIZE;
331 break;
332 default: /* assume transparent */
333 bcs->rx_bufsize = TRANSBUFSIZE;
334 }
335 dev_kfree_skb(bcs->rx_skb);
336 gigaset_new_rx_skb(bcs);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000337 if (!gigaset_add_event(cs, &bcs->at_state,
338 EV_ACCEPT, NULL, 0, NULL))
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800339 return -ENOMEM;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800340 gigaset_schedule_event(cs);
341
342 break;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800343 case ISDN_CMD_HANGUP:
Tilman Schmidt1528b182010-02-22 13:09:52 +0000344 gig_dbg(DEBUG_CMD, "ISDN_CMD_HANGUP");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000345 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700346 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000347 "ISDN_CMD_HANGUP: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800348 return -EINVAL;
349 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000350 bcs = cs->bcs + ch;
351 if (!gigaset_add_event(cs, &bcs->at_state,
352 EV_HUP, NULL, 0, NULL))
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800353 return -ENOMEM;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800354 gigaset_schedule_event(cs);
355
356 break;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000357 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */
358 dev_info(cs->dev, "ignoring ISDN_CMD_CLREAZ\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800359 break;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000360 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */
361 dev_info(cs->dev, "ignoring ISDN_CMD_SETEAZ (%s)\n",
362 cntrl->parm.num);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800363 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700364 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000365 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700366 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000367 "ISDN_CMD_SETL2: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800368 return -EINVAL;
369 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000370 bcs = cs->bcs + ch;
371 if (bcs->chstate & CHS_D_UP) {
372 dev_err(cs->dev,
373 "ISDN_CMD_SETL2: channel active (%d)\n", ch);
374 return -EINVAL;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800375 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000376 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;
380 break;
381 case ISDN_PROTO_L2_TRANS:
382 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_VOICE");
383 bcs->proto2 = L2_VOICE;
384 break;
385 default:
386 dev_err(cs->dev,
387 "ISDN_CMD_SETL2: unsupported protocol (%lu)\n",
388 cntrl->arg >> 8);
389 return -EINVAL;
390 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800391 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700392 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
Tilman Schmidt1528b182010-02-22 13:09:52 +0000393 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL3");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000394 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700395 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000396 "ISDN_CMD_SETL3: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800397 return -EINVAL;
398 }
399
400 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700401 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000402 "ISDN_CMD_SETL3: unsupported protocol (%lu)\n",
Tilman Schmidt50027792008-07-23 21:28:27 -0700403 cntrl->arg >> 8);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800404 return -EINVAL;
405 }
406
407 break;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000408
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800409 default:
Tilman Schmidt1528b182010-02-22 13:09:52 +0000410 gig_dbg(DEBUG_CMD, "unknown command %d from LL",
Tilman Schmidt50027792008-07-23 21:28:27 -0700411 cntrl->command);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800412 return -EINVAL;
413 }
414
415 return retval;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000416
417oom:
418 dev_err(bcs->cs->dev, "out of memory\n");
419 for (i = 0; i < AT_NUM; ++i)
420 kfree(commands[i]);
Tilman Schmidt54438f92010-07-05 14:19:19 +0000421 kfree(commands);
422 gigaset_free_channel(bcs);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000423 return -ENOMEM;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800424}
425
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000426static void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800427{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000428 isdn_if *iif = cs->iif;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800429 isdn_ctrl command;
430
431 command.driver = cs->myid;
432 command.command = cmd;
433 command.arg = 0;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000434 iif->statcallb(&command);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800435}
436
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000437static void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800438{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000439 isdn_if *iif = bcs->cs->iif;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800440 isdn_ctrl command;
441
442 command.driver = bcs->cs->myid;
443 command.command = cmd;
444 command.arg = bcs->channel;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000445 iif->statcallb(&command);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800446}
447
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000448/**
449 * gigaset_isdn_icall() - signal incoming call
450 * @at_state: connection state structure.
451 *
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.
454 *
455 * Return value: call disposition (ICALL_*)
456 */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800457int gigaset_isdn_icall(struct at_state_t *at_state)
458{
459 struct cardstate *cs = at_state->cs;
460 struct bc_state *bcs = at_state->bcs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000461 isdn_if *iif = cs->iif;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800462 isdn_ctrl response;
463 int retval;
464
465 /* fill ICALL structure */
466 response.parm.setup.si1 = 0; /* default: unknown */
467 response.parm.setup.si2 = 0;
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000468 response.parm.setup.screen = 0;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800469 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")) {
477 /* speech, A-law */
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;
483 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700484 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800485 at_state->str_var[STR_ZBC]);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800486 return ICALL_IGNORE;
487 }
488 if (at_state->str_var[STR_NMBR]) {
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000489 strlcpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
490 sizeof response.parm.setup.phone);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800491 } else
492 response.parm.setup.phone[0] = 0;
493 if (at_state->str_var[STR_ZCPN]) {
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000494 strlcpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
495 sizeof response.parm.setup.eazmsn);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800496 } else
497 response.parm.setup.eazmsn[0] = 0;
498
499 if (!bcs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700500 dev_notice(cs->dev, "no channel for incoming call\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800501 response.command = ISDN_STAT_ICALLW;
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000502 response.arg = 0;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800503 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700504 gig_dbg(DEBUG_CMD, "Sending ICALL");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800505 response.command = ISDN_STAT_ICALL;
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000506 response.arg = bcs->channel;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800507 }
508 response.driver = cs->myid;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000509 retval = iif->statcallb(&response);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700510 gig_dbg(DEBUG_CMD, "Response: %d", retval);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800511 switch (retval) {
512 case 0: /* no takers */
513 return ICALL_IGNORE;
514 case 1: /* alerting */
515 bcs->chstate |= CHS_NOTIFY_LL;
516 return ICALL_ACCEPT;
517 case 2: /* reject */
518 return ICALL_REJECT;
519 case 3: /* incomplete */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700520 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800521 "LL requested unsupported feature: Incomplete Number\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800522 return ICALL_IGNORE;
523 case 4: /* proceeding */
524 /* Gigaset will send ALERTING anyway.
525 * There doesn't seem to be a way to avoid this.
526 */
527 return ICALL_ACCEPT;
528 case 5: /* deflect */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700529 dev_warn(cs->dev,
530 "LL requested unsupported feature: Call Deflection\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800531 return ICALL_IGNORE;
532 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700533 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800534 return ICALL_IGNORE;
535 }
536}
537
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000538/**
539 * gigaset_isdn_connD() - signal D channel connect
540 * @bcs: B channel descriptor structure.
541 *
542 * Called by main module to notify the LL that the D channel connection has
543 * been established.
544 */
545void gigaset_isdn_connD(struct bc_state *bcs)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800546{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000547 gig_dbg(DEBUG_CMD, "sending DCONN");
548 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
549}
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800550
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000551/**
552 * gigaset_isdn_hupD() - signal D channel hangup
553 * @bcs: B channel descriptor structure.
554 *
555 * Called by main module to notify the LL that the D channel connection has
556 * been shut down.
557 */
558void gigaset_isdn_hupD(struct bc_state *bcs)
559{
560 gig_dbg(DEBUG_CMD, "sending DHUP");
561 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
562}
563
564/**
565 * gigaset_isdn_connB() - signal B channel connect
566 * @bcs: B channel descriptor structure.
567 *
568 * Called by main module to notify the LL that the B channel connection has
569 * been established.
570 */
571void gigaset_isdn_connB(struct bc_state *bcs)
572{
573 gig_dbg(DEBUG_CMD, "sending BCONN");
574 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
575}
576
577/**
578 * gigaset_isdn_hupB() - signal B channel hangup
579 * @bcs: B channel descriptor structure.
580 *
581 * Called by main module to notify the LL that the B channel connection has
582 * been shut down.
583 */
584void gigaset_isdn_hupB(struct bc_state *bcs)
585{
586 gig_dbg(DEBUG_CMD, "sending BHUP");
587 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
588}
589
590/**
591 * gigaset_isdn_start() - signal device availability
592 * @cs: device descriptor structure.
593 *
594 * Called by main module to notify the LL that the device is available for
595 * use.
596 */
597void gigaset_isdn_start(struct cardstate *cs)
598{
599 gig_dbg(DEBUG_CMD, "sending RUN");
600 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
601}
602
603/**
604 * gigaset_isdn_stop() - signal device unavailability
605 * @cs: device descriptor structure.
606 *
607 * Called by main module to notify the LL that the device is no longer
608 * available for use.
609 */
610void gigaset_isdn_stop(struct cardstate *cs)
611{
612 gig_dbg(DEBUG_CMD, "sending STOP");
613 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
614}
615
616/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +0000617 * gigaset_isdn_regdev() - register to LL
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000618 * @cs: device descriptor structure.
619 * @isdnid: device name.
620 *
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000621 * Return value: 0 on success, error code < 0 on failure
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000622 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +0000623int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000624{
625 isdn_if *iif;
626
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000627 iif = kmalloc(sizeof *iif, GFP_KERNEL);
628 if (!iif) {
629 pr_err("out of memory\n");
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000630 return -ENOMEM;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000631 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800632
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800633 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000634 >= sizeof iif->id) {
635 pr_err("ID too long: %s\n", isdnid);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000636 kfree(iif);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000637 return -EINVAL;
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000638 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800639
640 iif->owner = THIS_MODULE;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700641 iif->channels = cs->channels;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800642 iif->maxbufsize = MAX_BUF_SIZE;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700643 iif->features = ISDN_FEATURE_L2_TRANS |
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800644 ISDN_FEATURE_L2_HDLC |
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800645 ISDN_FEATURE_L2_X75I |
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800646 ISDN_FEATURE_L3_TRANS |
647 ISDN_FEATURE_P_EURO;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700648 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800649 iif->command = command_from_LL;
650 iif->writebuf_skb = writebuf_from_LL;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700651 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 */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800655
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000656 if (!register_isdn(iif)) {
657 pr_err("register_isdn failed\n");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000658 kfree(iif);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000659 return -EINVAL;
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000660 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800661
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000662 cs->iif = iif;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700663 cs->myid = iif->channels; /* Set my device id */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000664 cs->hw_hdr_len = HW_HDR_LEN;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000665 return 0;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800666}
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000667
668/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +0000669 * gigaset_isdn_unregdev() - unregister device from LL
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000670 * @cs: device descriptor structure.
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000671 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +0000672void gigaset_isdn_unregdev(struct cardstate *cs)
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000673{
674 gig_dbg(DEBUG_CMD, "sending UNLOAD");
675 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
676 kfree(cs->iif);
677 cs->iif = NULL;
678}
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +0000679
680/**
681 * gigaset_isdn_regdrv() - register driver to LL
682 */
683void gigaset_isdn_regdrv(void)
684{
Tilman Schmidtfd4f8622011-11-27 07:39:22 +0000685 pr_info("ISDN4Linux interface\n");
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +0000686 /* nothing to do */
687}
688
689/**
690 * gigaset_isdn_unregdrv() - unregister driver from LL
691 */
692void gigaset_isdn_unregdrv(void)
693{
694 /* nothing to do */
695}