blob: aca72a06184ec198d0f328a91c00b7e269803f0e [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>
18
19#define HW_HDR_LEN 2 /* Header size used to store ack info */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080020
Tilman Schmidt784d5852006-04-10 22:55:04 -070021/* == Handling of I4L IO =====================================================*/
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080022
23/* writebuf_from_LL
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.
28 * parameters:
29 * driverID driver ID as assigned by LL
30 * channel channel number
Tilman Schmidt917f5082006-04-10 22:55:00 -070031 * ack if != 0 LL wants to be notified on completion via
32 * statcallb(ISDN_STAT_BSENT)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080033 * skb skb containing data to send
34 * return value:
35 * number of accepted bytes
36 * 0 if temporarily unable to accept data (out of buffer space)
37 * <0 on error (eg. -EINVAL)
38 */
Tilman Schmidt784d5852006-04-10 22:55:04 -070039static int writebuf_from_LL(int driverID, int channel, int ack,
40 struct sk_buff *skb)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080041{
42 struct cardstate *cs;
43 struct bc_state *bcs;
44 unsigned len;
45 unsigned skblen;
46
47 if (!(cs = gigaset_get_cs_by_id(driverID))) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -080048 pr_err("%s: invalid driver ID (%d)\n", __func__, driverID);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080049 return -ENODEV;
50 }
51 if (channel < 0 || channel >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -070052 dev_err(cs->dev, "%s: invalid channel ID (%d)\n",
53 __func__, channel);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080054 return -ENODEV;
55 }
56 bcs = &cs->bcs[channel];
Tilman Schmidtee239d92009-10-06 12:18:31 +000057
58 /* can only handle linear sk_buffs */
59 if (skb_linearize(skb) < 0) {
60 dev_err(cs->dev, "%s: skb_linearize failed\n", __func__);
61 return -ENOMEM;
62 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080063 len = skb->len;
64
Tilman Schmidt784d5852006-04-10 22:55:04 -070065 gig_dbg(DEBUG_LLDATA,
66 "Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
67 driverID, channel, ack, len);
68
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080069 if (!len) {
70 if (ack)
Tilman Schmidt50027792008-07-23 21:28:27 -070071 dev_notice(cs->dev, "%s: not ACKing empty packet\n",
72 __func__);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080073 return 0;
74 }
75 if (len > MAX_BUF_SIZE) {
Tilman Schmidt50027792008-07-23 21:28:27 -070076 dev_err(cs->dev, "%s: packet too large (%d bytes)\n",
77 __func__, len);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080078 return -EINVAL;
79 }
80
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080081 skblen = ack ? len : 0;
82 skb->head[0] = skblen & 0xff;
83 skb->head[1] = skblen >> 8;
Tilman Schmidt784d5852006-04-10 22:55:04 -070084 gig_dbg(DEBUG_MCMD, "skb: len=%u, skblen=%u: %02x %02x",
85 len, skblen, (unsigned) skb->head[0], (unsigned) skb->head[1]);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080086
87 /* pass to device-specific module */
Tilman Schmidt73a88812006-04-22 02:35:30 -070088 return cs->ops->send_skb(bcs, skb);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080089}
90
Tilman Schmidt1cec9722009-10-06 12:19:01 +000091/**
92 * gigaset_skb_sent() - acknowledge sending an skb
93 * @bcs: B channel descriptor structure.
94 * @skb: sent data.
95 *
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.
98 */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -080099void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
100{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000101 isdn_if *iif = bcs->cs->iif;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800102 unsigned len;
103 isdn_ctrl response;
104
105 ++bcs->trans_up;
106
107 if (skb->len)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700108 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
109 __func__, skb->len);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800110
111 len = (unsigned char) skb->head[0] |
112 (unsigned) (unsigned char) skb->head[1] << 8;
113 if (len) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700114 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
115 bcs->cs->myid, bcs->channel, len);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800116
117 response.driver = bcs->cs->myid;
118 response.command = ISDN_STAT_BSENT;
119 response.arg = bcs->channel;
120 response.parm.length = len;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000121 iif->statcallb(&response);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800122 }
123}
124EXPORT_SYMBOL_GPL(gigaset_skb_sent);
125
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000126/**
127 * gigaset_skb_rcvd() - pass received skb to LL
128 * @bcs: B channel descriptor structure.
129 * @skb: received data.
130 *
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!
134 */
135void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
136{
137 isdn_if *iif = bcs->cs->iif;
138
139 iif->rcvcallb_skb(bcs->cs->myid, bcs->channel, skb);
140 bcs->trans_down++;
141}
142EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
143
144/**
145 * gigaset_isdn_rcv_err() - signal receive error
146 * @bcs: B channel descriptor structure.
147 *
148 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
149 * has occurred, for signalling to the LL.
150 */
151void gigaset_isdn_rcv_err(struct bc_state *bcs)
152{
153 isdn_if *iif = bcs->cs->iif;
154 isdn_ctrl response;
155
156 /* if currently ignoring packets, just count down */
157 if (bcs->ignore) {
158 bcs->ignore--;
159 return;
160 }
161
162 /* update statistics */
163 bcs->corrupted++;
164
165 /* error -> LL */
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);
172}
173EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
174
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800175/* 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.
178 */
179static int command_from_LL(isdn_ctrl *cntrl)
180{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000181 struct cardstate *cs;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800182 struct bc_state *bcs;
183 int retval = 0;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000184 char **commands;
185 int ch;
186 int i;
187 size_t l;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800188
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800189 gigaset_debugdrivers();
190
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000191 gig_dbg(DEBUG_CMD, "driver: %d, command: %d, arg: 0x%lx",
192 cntrl->driver, cntrl->command, cntrl->arg);
193
194 cs = gigaset_get_cs_by_id(cntrl->driver);
195 if (cs == NULL) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800196 pr_err("%s: invalid driver ID (%d)\n", __func__, cntrl->driver);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800197 return -ENODEV;
198 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000199 ch = cntrl->arg & 0xff;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800200
201 switch (cntrl->command) {
202 case ISDN_CMD_IOCTL:
Tilman Schmidt50027792008-07-23 21:28:27 -0700203 dev_warn(cs->dev, "ISDN_CMD_IOCTL not supported\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800204 return -EINVAL;
205
206 case ISDN_CMD_DIAL:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700207 gig_dbg(DEBUG_ANY,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000208 "ISDN_CMD_DIAL (phone: %s, msn: %s, si1: %d, si2: %d)",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700209 cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
210 cntrl->parm.setup.si1, cntrl->parm.setup.si2);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800211
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000212 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700213 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000214 "ISDN_CMD_DIAL: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800215 return -EINVAL;
216 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000217 bcs = cs->bcs + ch;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800218 if (!gigaset_get_channel(bcs)) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700219 dev_err(cs->dev, "ISDN_CMD_DIAL: channel not free\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800220 return -EBUSY;
221 }
222
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000223 commands = kzalloc(AT_NUM*(sizeof *commands), GFP_ATOMIC);
224 if (!commands) {
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800225 gigaset_free_channel(bcs);
Tilman Schmidt50027792008-07-23 21:28:27 -0700226 dev_err(cs->dev, "ISDN_CMD_DIAL: out of memory\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800227 return -ENOMEM;
228 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800229
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000230 l = 3 + strlen(cntrl->parm.setup.phone);
231 commands[AT_DIAL] = kmalloc(l, GFP_ATOMIC);
232 if (!commands[AT_DIAL])
233 goto oom;
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])
239 goto oom;
240 snprintf(commands[AT_DIAL], l,
241 "D%s\r", cntrl->parm.setup.phone+2);
242 } else {
243 commands[AT_TYPE] = kstrdup("^SCTP=1\r", GFP_ATOMIC);
244 if (!commands[AT_TYPE])
245 goto oom;
246 snprintf(commands[AT_DIAL], l,
247 "D%s\r", cntrl->parm.setup.phone);
248 }
249
250 l = strlen(cntrl->parm.setup.eazmsn);
251 if (l) {
252 l += 8;
253 commands[AT_MSN] = kmalloc(l, GFP_ATOMIC);
254 if (!commands[AT_MSN])
255 goto oom;
256 snprintf(commands[AT_MSN], l, "^SMSN=%s\r",
257 cntrl->parm.setup.eazmsn);
258 }
259
260 switch (cntrl->parm.setup.si1) {
261 case 1: /* audio */
262 /* BC = 9090A3: 3.1 kHz audio, A-law */
263 commands[AT_BC] = kstrdup("^SBC=9090A3\r", GFP_ATOMIC);
264 if (!commands[AT_BC])
265 goto oom;
266 break;
267 case 7: /* data */
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])
272 goto oom;
273 }
274 /* ToDo: other si1 values, inspect si2, set HLC/LLC */
275
276 commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
277 if (!commands[AT_PROTO])
278 goto oom;
279 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
280
281 commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
282 if (!commands[AT_ISO])
283 goto oom;
284 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
285 (unsigned) bcs->channel + 1);
286
287 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
Tilman Schmidt4d1ff582007-10-16 01:27:50 -0700288 bcs->at_state.seq_index, NULL)) {
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000289 for (i = 0; i < AT_NUM; ++i)
290 kfree(commands[i]);
291 kfree(commands);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800292 gigaset_free_channel(bcs);
293 return -ENOMEM;
294 }
295
Tilman Schmidt784d5852006-04-10 22:55:04 -0700296 gig_dbg(DEBUG_CMD, "scheduling DIAL");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800297 gigaset_schedule_event(cs);
298 break;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000299 case ISDN_CMD_ACCEPTD:
300 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700301 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000302 "ISDN_CMD_ACCEPTD: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800303 return -EINVAL;
304 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000305 bcs = cs->bcs + ch;
306 if (!gigaset_add_event(cs, &bcs->at_state,
307 EV_ACCEPT, NULL, 0, NULL))
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800308 return -ENOMEM;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800309
Tilman Schmidt784d5852006-04-10 22:55:04 -0700310 gig_dbg(DEBUG_CMD, "scheduling ACCEPT");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800311 gigaset_schedule_event(cs);
312
313 break;
314 case ISDN_CMD_ACCEPTB:
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800315 break;
316 case ISDN_CMD_HANGUP:
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000317 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700318 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000319 "ISDN_CMD_HANGUP: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800320 return -EINVAL;
321 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000322 bcs = cs->bcs + ch;
323 if (!gigaset_add_event(cs, &bcs->at_state,
324 EV_HUP, NULL, 0, NULL))
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800325 return -ENOMEM;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800326
Tilman Schmidt784d5852006-04-10 22:55:04 -0700327 gig_dbg(DEBUG_CMD, "scheduling HUP");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800328 gigaset_schedule_event(cs);
329
330 break;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000331 case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */
332 dev_info(cs->dev, "ignoring ISDN_CMD_CLREAZ\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800333 break;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000334 case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */
335 dev_info(cs->dev, "ignoring ISDN_CMD_SETEAZ (%s)\n",
336 cntrl->parm.num);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800337 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700338 case ISDN_CMD_SETL2: /* Set L2 to given protocol */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000339 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700340 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000341 "ISDN_CMD_SETL2: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800342 return -EINVAL;
343 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000344 bcs = cs->bcs + ch;
345 if (bcs->chstate & CHS_D_UP) {
346 dev_err(cs->dev,
347 "ISDN_CMD_SETL2: channel active (%d)\n", ch);
348 return -EINVAL;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800349 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000350 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;
354 break;
355 case ISDN_PROTO_L2_TRANS:
356 gig_dbg(DEBUG_CMD, "ISDN_CMD_SETL2: setting L2_VOICE");
357 bcs->proto2 = L2_VOICE;
358 break;
359 default:
360 dev_err(cs->dev,
361 "ISDN_CMD_SETL2: unsupported protocol (%lu)\n",
362 cntrl->arg >> 8);
363 return -EINVAL;
364 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800365 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700366 case ISDN_CMD_SETL3: /* Set L3 to given protocol */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000367 if (ch >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700368 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000369 "ISDN_CMD_SETL3: invalid channel (%d)\n", ch);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800370 return -EINVAL;
371 }
372
373 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700374 dev_err(cs->dev,
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000375 "ISDN_CMD_SETL3: unsupported protocol (%lu)\n",
Tilman Schmidt50027792008-07-23 21:28:27 -0700376 cntrl->arg >> 8);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800377 return -EINVAL;
378 }
379
380 break;
381 case ISDN_CMD_PROCEED:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700382 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROCEED"); //FIXME
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800383 break;
384 case ISDN_CMD_ALERT:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700385 gig_dbg(DEBUG_ANY, "ISDN_CMD_ALERT"); //FIXME
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800386 if (cntrl->arg >= cs->channels) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700387 dev_err(cs->dev,
388 "ISDN_CMD_ALERT: invalid channel (%d)\n",
389 (int) cntrl->arg);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800390 return -EINVAL;
391 }
392 //bcs = cs->bcs + cntrl->arg;
393 //bcs->proto2 = -1;
394 // FIXME
395 break;
396 case ISDN_CMD_REDIR:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700397 gig_dbg(DEBUG_ANY, "ISDN_CMD_REDIR"); //FIXME
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800398 break;
399 case ISDN_CMD_PROT_IO:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700400 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROT_IO");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800401 break;
402 case ISDN_CMD_FAXCMD:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700403 gig_dbg(DEBUG_ANY, "ISDN_CMD_FAXCMD");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800404 break;
405 case ISDN_CMD_GETL2:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700406 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL2");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800407 break;
408 case ISDN_CMD_GETL3:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700409 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL3");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800410 break;
411 case ISDN_CMD_GETEAZ:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700412 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETEAZ");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800413 break;
414 case ISDN_CMD_SETSIL:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700415 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETSIL");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800416 break;
417 case ISDN_CMD_GETSIL:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700418 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETSIL");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800419 break;
420 default:
Tilman Schmidt50027792008-07-23 21:28:27 -0700421 dev_err(cs->dev, "unknown command %d from LL\n",
422 cntrl->command);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800423 return -EINVAL;
424 }
425
426 return retval;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000427
428oom:
429 dev_err(bcs->cs->dev, "out of memory\n");
430 for (i = 0; i < AT_NUM; ++i)
431 kfree(commands[i]);
432 return -ENOMEM;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800433}
434
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000435static void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800436{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000437 isdn_if *iif = cs->iif;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800438 isdn_ctrl command;
439
440 command.driver = cs->myid;
441 command.command = cmd;
442 command.arg = 0;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000443 iif->statcallb(&command);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800444}
445
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000446static void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800447{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000448 isdn_if *iif = bcs->cs->iif;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800449 isdn_ctrl command;
450
451 command.driver = bcs->cs->myid;
452 command.command = cmd;
453 command.arg = bcs->channel;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000454 iif->statcallb(&command);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800455}
456
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000457/**
458 * gigaset_isdn_icall() - signal incoming call
459 * @at_state: connection state structure.
460 *
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.
463 *
464 * Return value: call disposition (ICALL_*)
465 */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800466int gigaset_isdn_icall(struct at_state_t *at_state)
467{
468 struct cardstate *cs = at_state->cs;
469 struct bc_state *bcs = at_state->bcs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000470 isdn_if *iif = cs->iif;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800471 isdn_ctrl response;
472 int retval;
473
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")) {
486 /* speech, A-law */
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;
492 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700493 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800494 at_state->str_var[STR_ZBC]);
495 return ICALL_IGNORE;
496 }
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;
501 } else
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;
507 } else
508 response.parm.setup.eazmsn[0] = 0;
509
510 if (!bcs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700511 dev_notice(cs->dev, "no channel for incoming call\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800512 response.command = ISDN_STAT_ICALLW;
513 response.arg = 0; //FIXME
514 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700515 gig_dbg(DEBUG_CMD, "Sending ICALL");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800516 response.command = ISDN_STAT_ICALL;
517 response.arg = bcs->channel; //FIXME
518 }
519 response.driver = cs->myid;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000520 retval = iif->statcallb(&response);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700521 gig_dbg(DEBUG_CMD, "Response: %d", retval);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800522 switch (retval) {
523 case 0: /* no takers */
524 return ICALL_IGNORE;
525 case 1: /* alerting */
526 bcs->chstate |= CHS_NOTIFY_LL;
527 return ICALL_ACCEPT;
528 case 2: /* reject */
529 return ICALL_REJECT;
530 case 3: /* incomplete */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700531 dev_warn(cs->dev,
532 "LL requested unsupported feature: Incomplete Number\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800533 return ICALL_IGNORE;
534 case 4: /* proceeding */
535 /* Gigaset will send ALERTING anyway.
536 * There doesn't seem to be a way to avoid this.
537 */
538 return ICALL_ACCEPT;
539 case 5: /* deflect */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700540 dev_warn(cs->dev,
541 "LL requested unsupported feature: Call Deflection\n");
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800542 return ICALL_IGNORE;
543 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700544 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800545 return ICALL_IGNORE;
546 }
547}
548
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000549/**
550 * gigaset_isdn_connD() - signal D channel connect
551 * @bcs: B channel descriptor structure.
552 *
553 * Called by main module to notify the LL that the D channel connection has
554 * been established.
555 */
556void gigaset_isdn_connD(struct bc_state *bcs)
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800557{
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000558 gig_dbg(DEBUG_CMD, "sending DCONN");
559 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
560}
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800561
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000562/**
563 * gigaset_isdn_hupD() - signal D channel hangup
564 * @bcs: B channel descriptor structure.
565 *
566 * Called by main module to notify the LL that the D channel connection has
567 * been shut down.
568 */
569void gigaset_isdn_hupD(struct bc_state *bcs)
570{
571 gig_dbg(DEBUG_CMD, "sending DHUP");
572 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
573}
574
575/**
576 * gigaset_isdn_connB() - signal B channel connect
577 * @bcs: B channel descriptor structure.
578 *
579 * Called by main module to notify the LL that the B channel connection has
580 * been established.
581 */
582void gigaset_isdn_connB(struct bc_state *bcs)
583{
584 gig_dbg(DEBUG_CMD, "sending BCONN");
585 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
586}
587
588/**
589 * gigaset_isdn_hupB() - signal B channel hangup
590 * @bcs: B channel descriptor structure.
591 *
592 * Called by main module to notify the LL that the B channel connection has
593 * been shut down.
594 */
595void gigaset_isdn_hupB(struct bc_state *bcs)
596{
597 gig_dbg(DEBUG_CMD, "sending BHUP");
598 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
599}
600
601/**
602 * gigaset_isdn_start() - signal device availability
603 * @cs: device descriptor structure.
604 *
605 * Called by main module to notify the LL that the device is available for
606 * use.
607 */
608void gigaset_isdn_start(struct cardstate *cs)
609{
610 gig_dbg(DEBUG_CMD, "sending RUN");
611 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
612}
613
614/**
615 * gigaset_isdn_stop() - signal device unavailability
616 * @cs: device descriptor structure.
617 *
618 * Called by main module to notify the LL that the device is no longer
619 * available for use.
620 */
621void gigaset_isdn_stop(struct cardstate *cs)
622{
623 gig_dbg(DEBUG_CMD, "sending STOP");
624 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
625}
626
627/**
628 * gigaset_isdn_register() - register to LL
629 * @cs: device descriptor structure.
630 * @isdnid: device name.
631 *
632 * Called by main module to register the device with the LL.
633 *
634 * Return value: 1 for success, 0 for failure
635 */
636int gigaset_isdn_register(struct cardstate *cs, const char *isdnid)
637{
638 isdn_if *iif;
639
640 pr_info("ISDN4Linux interface\n");
641
642 iif = kmalloc(sizeof *iif, GFP_KERNEL);
643 if (!iif) {
644 pr_err("out of memory\n");
645 return 0;
646 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800647
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800648 if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000649 >= sizeof iif->id) {
650 pr_err("ID too long: %s\n", isdnid);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000651 kfree(iif);
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000652 return 0;
653 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800654
655 iif->owner = THIS_MODULE;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700656 iif->channels = cs->channels;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800657 iif->maxbufsize = MAX_BUF_SIZE;
Tilman Schmidt917f5082006-04-10 22:55:00 -0700658 iif->features = ISDN_FEATURE_L2_TRANS |
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800659 ISDN_FEATURE_L2_HDLC |
660#ifdef GIG_X75
661 ISDN_FEATURE_L2_X75I |
662#endif
663 ISDN_FEATURE_L3_TRANS |
664 ISDN_FEATURE_P_EURO;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700665 iif->hl_hdrlen = HW_HDR_LEN; /* Area for storing ack */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800666 iif->command = command_from_LL;
667 iif->writebuf_skb = writebuf_from_LL;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700668 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 */
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800672
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000673 if (!register_isdn(iif)) {
674 pr_err("register_isdn failed\n");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000675 kfree(iif);
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800676 return 0;
Tilman Schmidt7226d7c2009-05-13 12:44:17 +0000677 }
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800678
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000679 cs->iif = iif;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700680 cs->myid = iif->channels; /* Set my device id */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000681 cs->hw_hdr_len = HW_HDR_LEN;
Hansjoerg Lipp3c66a222006-03-26 01:38:31 -0800682 return 1;
683}
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000684
685/**
686 * gigaset_isdn_unregister() - unregister from LL
687 * @cs: device descriptor structure.
688 *
689 * Called by main module to unregister the device from the LL.
690 */
691void gigaset_isdn_unregister(struct cardstate *cs)
692{
693 gig_dbg(DEBUG_CMD, "sending UNLOAD");
694 gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
695 kfree(cs->iif);
696 cs->iif = NULL;
697}