]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/isdn/gigaset/capi.c
665673f4d66745965e282747d12cf7a821b76f16
[net-next-2.6.git] / drivers / isdn / gigaset / capi.c
1 /*
2  * Kernel CAPI interface for the Gigaset driver
3  *
4  * Copyright (c) 2009 by Tilman Schmidt <tilman@imap.cc>.
5  *
6  * =====================================================================
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License as
9  *      published by the Free Software Foundation; either version 2 of
10  *      the License, or (at your option) any later version.
11  * =====================================================================
12  */
13
14 #include "gigaset.h"
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/isdn/capilli.h>
18 #include <linux/isdn/capicmd.h>
19 #include <linux/isdn/capiutil.h>
20
21 /* missing from kernelcapi.h */
22 #define CapiNcpiNotSupportedByProtocol  0x0001
23 #define CapiFlagsNotSupportedByProtocol 0x0002
24 #define CapiAlertAlreadySent            0x0003
25 #define CapiFacilitySpecificFunctionNotSupported        0x3011
26
27 /* missing from capicmd.h */
28 #define CAPI_CONNECT_IND_BASELEN        (CAPI_MSG_BASELEN+4+2+8*1)
29 #define CAPI_CONNECT_ACTIVE_IND_BASELEN (CAPI_MSG_BASELEN+4+3*1)
30 #define CAPI_CONNECT_B3_IND_BASELEN     (CAPI_MSG_BASELEN+4+1)
31 #define CAPI_CONNECT_B3_ACTIVE_IND_BASELEN      (CAPI_MSG_BASELEN+4+1)
32 #define CAPI_DATA_B3_REQ_LEN64          (CAPI_MSG_BASELEN+4+4+2+2+2+8)
33 #define CAPI_DATA_B3_CONF_LEN           (CAPI_MSG_BASELEN+4+2+2)
34 #define CAPI_DISCONNECT_IND_LEN         (CAPI_MSG_BASELEN+4+2)
35 #define CAPI_DISCONNECT_B3_IND_BASELEN  (CAPI_MSG_BASELEN+4+2+1)
36 #define CAPI_FACILITY_CONF_BASELEN      (CAPI_MSG_BASELEN+4+2+2+1)
37 /* most _CONF messages contain only Controller/PLCI/NCCI and Info parameters */
38 #define CAPI_STDCONF_LEN                (CAPI_MSG_BASELEN+4+2)
39
40 #define CAPI_FACILITY_HANDSET   0x0000
41 #define CAPI_FACILITY_DTMF      0x0001
42 #define CAPI_FACILITY_V42BIS    0x0002
43 #define CAPI_FACILITY_SUPPSVC   0x0003
44 #define CAPI_FACILITY_WAKEUP    0x0004
45 #define CAPI_FACILITY_LI        0x0005
46
47 #define CAPI_SUPPSVC_GETSUPPORTED       0x0000
48
49 /* missing from capiutil.h */
50 #define CAPIMSG_PLCI_PART(m)    CAPIMSG_U8(m, 9)
51 #define CAPIMSG_NCCI_PART(m)    CAPIMSG_U16(m, 10)
52 #define CAPIMSG_HANDLE_REQ(m)   CAPIMSG_U16(m, 18) /* DATA_B3_REQ/_IND only! */
53 #define CAPIMSG_FLAGS(m)        CAPIMSG_U16(m, 20)
54 #define CAPIMSG_SETCONTROLLER(m, contr) capimsg_setu8(m, 8, contr)
55 #define CAPIMSG_SETPLCI_PART(m, plci)   capimsg_setu8(m, 9, plci)
56 #define CAPIMSG_SETNCCI_PART(m, ncci)   capimsg_setu16(m, 10, ncci)
57 #define CAPIMSG_SETFLAGS(m, flags)      capimsg_setu16(m, 20, flags)
58
59 /* parameters with differing location in DATA_B3_CONF/_RESP: */
60 #define CAPIMSG_SETHANDLE_CONF(m, handle)       capimsg_setu16(m, 12, handle)
61 #define CAPIMSG_SETINFO_CONF(m, info)           capimsg_setu16(m, 14, info)
62
63 /* Flags (DATA_B3_REQ/_IND) */
64 #define CAPI_FLAGS_DELIVERY_CONFIRMATION        0x04
65 #define CAPI_FLAGS_RESERVED                     (~0x1f)
66
67 /* buffer sizes */
68 #define MAX_BC_OCTETS 11
69 #define MAX_HLC_OCTETS 3
70 #define MAX_NUMBER_DIGITS 20
71 #define MAX_FMT_IE_LEN 20
72
73 /* values for gigaset_capi_appl.connected */
74 #define APCONN_NONE     0       /* inactive/listening */
75 #define APCONN_SETUP    1       /* connecting */
76 #define APCONN_ACTIVE   2       /* B channel up */
77
78 /* registered application data structure */
79 struct gigaset_capi_appl {
80         struct list_head ctrlist;
81         struct gigaset_capi_appl *bcnext;
82         u16 id;
83         struct capi_register_params rp;
84         u16 nextMessageNumber;
85         u32 listenInfoMask;
86         u32 listenCIPmask;
87         int connected;
88 };
89
90 /* CAPI specific controller data structure */
91 struct gigaset_capi_ctr {
92         struct capi_ctr ctr;
93         struct list_head appls;
94         struct sk_buff_head sendqueue;
95         atomic_t sendqlen;
96         /* two _cmsg structures possibly used concurrently: */
97         _cmsg hcmsg;    /* for message composition triggered from hardware */
98         _cmsg acmsg;    /* for dissection of messages sent from application */
99         u8 bc_buf[MAX_BC_OCTETS+1];
100         u8 hlc_buf[MAX_HLC_OCTETS+1];
101         u8 cgpty_buf[MAX_NUMBER_DIGITS+3];
102         u8 cdpty_buf[MAX_NUMBER_DIGITS+2];
103 };
104
105 /* CIP Value table (from CAPI 2.0 standard, ch. 6.1) */
106 static struct {
107         u8 *bc;
108         u8 *hlc;
109 } cip2bchlc[] = {
110         [1] = { "8090A3", NULL },
111                 /* Speech (A-law) */
112         [2] = { "8890", NULL },
113                 /* Unrestricted digital information */
114         [3] = { "8990", NULL },
115                 /* Restricted digital information */
116         [4] = { "9090A3", NULL },
117                 /* 3,1 kHz audio (A-law) */
118         [5] = { "9190", NULL },
119                 /* 7 kHz audio */
120         [6] = { "9890", NULL },
121                 /* Video */
122         [7] = { "88C0C6E6", NULL },
123                 /* Packet mode */
124         [8] = { "8890218F", NULL },
125                 /* 56 kbit/s rate adaptation */
126         [9] = { "9190A5", NULL },
127                 /* Unrestricted digital information with tones/announcements */
128         [16] = { "8090A3", "9181" },
129                 /* Telephony */
130         [17] = { "9090A3", "9184" },
131                 /* Group 2/3 facsimile */
132         [18] = { "8890", "91A1" },
133                 /* Group 4 facsimile Class 1 */
134         [19] = { "8890", "91A4" },
135                 /* Teletex service basic and mixed mode
136                    and Group 4 facsimile service Classes II and III */
137         [20] = { "8890", "91A8" },
138                 /* Teletex service basic and processable mode */
139         [21] = { "8890", "91B1" },
140                 /* Teletex service basic mode */
141         [22] = { "8890", "91B2" },
142                 /* International interworking for Videotex */
143         [23] = { "8890", "91B5" },
144                 /* Telex */
145         [24] = { "8890", "91B8" },
146                 /* Message Handling Systems in accordance with X.400 */
147         [25] = { "8890", "91C1" },
148                 /* OSI application in accordance with X.200 */
149         [26] = { "9190A5", "9181" },
150                 /* 7 kHz telephony */
151         [27] = { "9190A5", "916001" },
152                 /* Video telephony, first connection */
153         [28] = { "8890", "916002" },
154                 /* Video telephony, second connection */
155 };
156
157 /*
158  * helper functions
159  * ================
160  */
161
162 /*
163  * emit unsupported parameter warning
164  */
165 static inline void ignore_cstruct_param(struct cardstate *cs, _cstruct param,
166                                        char *msgname, char *paramname)
167 {
168         if (param && *param)
169                 dev_warn(cs->dev, "%s: ignoring unsupported parameter: %s\n",
170                          msgname, paramname);
171 }
172
173 /*
174  * convert an IE from Gigaset hex string to ETSI binary representation
175  * including length byte
176  * return value: result length, -1 on error
177  */
178 static int encode_ie(char *in, u8 *out, int maxlen)
179 {
180         int l = 0;
181         while (*in) {
182                 if (!isxdigit(in[0]) || !isxdigit(in[1]) || l >= maxlen)
183                         return -1;
184                 out[++l] = (hex_to_bin(in[0]) << 4) + hex_to_bin(in[1]);
185                 in += 2;
186         }
187         out[0] = l;
188         return l;
189 }
190
191 /*
192  * convert an IE from ETSI binary representation including length byte
193  * to Gigaset hex string
194  */
195 static void decode_ie(u8 *in, char *out)
196 {
197         int i = *in;
198         while (i-- > 0) {
199                 /* ToDo: conversion to upper case necessary? */
200                 *out++ = toupper(hex_asc_hi(*++in));
201                 *out++ = toupper(hex_asc_lo(*in));
202         }
203 }
204
205 /*
206  * retrieve application data structure for an application ID
207  */
208 static inline struct gigaset_capi_appl *
209 get_appl(struct gigaset_capi_ctr *iif, u16 appl)
210 {
211         struct gigaset_capi_appl *ap;
212
213         list_for_each_entry(ap, &iif->appls, ctrlist)
214                 if (ap->id == appl)
215                         return ap;
216         return NULL;
217 }
218
219 /*
220  * dump CAPI message to kernel messages for debugging
221  */
222 static inline void dump_cmsg(enum debuglevel level, const char *tag, _cmsg *p)
223 {
224 #ifdef CONFIG_GIGASET_DEBUG
225         _cdebbuf *cdb;
226
227         if (!(gigaset_debuglevel & level))
228                 return;
229
230         cdb = capi_cmsg2str(p);
231         if (cdb) {
232                 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId, cdb->buf);
233                 cdebbuf_free(cdb);
234         } else {
235                 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId,
236                         capi_cmd2str(p->Command, p->Subcommand));
237         }
238 #endif
239 }
240
241 static inline void dump_rawmsg(enum debuglevel level, const char *tag,
242                                unsigned char *data)
243 {
244 #ifdef CONFIG_GIGASET_DEBUG
245         char *dbgline;
246         int i, l;
247
248         if (!(gigaset_debuglevel & level))
249                 return;
250
251         l = CAPIMSG_LEN(data);
252         if (l < 12) {
253                 gig_dbg(level, "%s: ??? LEN=%04d", tag, l);
254                 return;
255         }
256         gig_dbg(level, "%s: 0x%02x:0x%02x: ID=%03d #0x%04x LEN=%04d NCCI=0x%x",
257                 tag, CAPIMSG_COMMAND(data), CAPIMSG_SUBCOMMAND(data),
258                 CAPIMSG_APPID(data), CAPIMSG_MSGID(data), l,
259                 CAPIMSG_CONTROL(data));
260         l -= 12;
261         dbgline = kmalloc(3*l, GFP_ATOMIC);
262         if (!dbgline)
263                 return;
264         for (i = 0; i < l; i++) {
265                 dbgline[3*i] = hex_asc_hi(data[12+i]);
266                 dbgline[3*i+1] = hex_asc_lo(data[12+i]);
267                 dbgline[3*i+2] = ' ';
268         }
269         dbgline[3*l-1] = '\0';
270         gig_dbg(level, "  %s", dbgline);
271         kfree(dbgline);
272         if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
273             (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
274              CAPIMSG_SUBCOMMAND(data) == CAPI_IND) &&
275             CAPIMSG_DATALEN(data) > 0) {
276                 l = CAPIMSG_DATALEN(data);
277                 dbgline = kmalloc(3*l, GFP_ATOMIC);
278                 if (!dbgline)
279                         return;
280                 data += CAPIMSG_LEN(data);
281                 for (i = 0; i < l; i++) {
282                         dbgline[3*i] = hex_asc_hi(data[i]);
283                         dbgline[3*i+1] = hex_asc_lo(data[i]);
284                         dbgline[3*i+2] = ' ';
285                 }
286                 dbgline[3*l-1] = '\0';
287                 gig_dbg(level, "  %s", dbgline);
288                 kfree(dbgline);
289         }
290 #endif
291 }
292
293 /*
294  * format CAPI IE as string
295  */
296
297 static const char *format_ie(const char *ie)
298 {
299         static char result[3*MAX_FMT_IE_LEN];
300         int len, count;
301         char *pout = result;
302
303         if (!ie)
304                 return "NULL";
305
306         count = len = ie[0];
307         if (count > MAX_FMT_IE_LEN)
308                 count = MAX_FMT_IE_LEN-1;
309         while (count--) {
310                 *pout++ = hex_asc_hi(*++ie);
311                 *pout++ = hex_asc_lo(*ie);
312                 *pout++ = ' ';
313         }
314         if (len > MAX_FMT_IE_LEN) {
315                 *pout++ = '.';
316                 *pout++ = '.';
317                 *pout++ = '.';
318         }
319         *--pout = 0;
320         return result;
321 }
322
323 /*
324  * emit DATA_B3_CONF message
325  */
326 static void send_data_b3_conf(struct cardstate *cs, struct capi_ctr *ctr,
327                               u16 appl, u16 msgid, int channel,
328                               u16 handle, u16 info)
329 {
330         struct sk_buff *cskb;
331         u8 *msg;
332
333         cskb = alloc_skb(CAPI_DATA_B3_CONF_LEN, GFP_ATOMIC);
334         if (!cskb) {
335                 dev_err(cs->dev, "%s: out of memory\n", __func__);
336                 return;
337         }
338         /* frequent message, avoid _cmsg overhead */
339         msg = __skb_put(cskb, CAPI_DATA_B3_CONF_LEN);
340         CAPIMSG_SETLEN(msg, CAPI_DATA_B3_CONF_LEN);
341         CAPIMSG_SETAPPID(msg, appl);
342         CAPIMSG_SETCOMMAND(msg, CAPI_DATA_B3);
343         CAPIMSG_SETSUBCOMMAND(msg,  CAPI_CONF);
344         CAPIMSG_SETMSGID(msg, msgid);
345         CAPIMSG_SETCONTROLLER(msg, ctr->cnr);
346         CAPIMSG_SETPLCI_PART(msg, channel);
347         CAPIMSG_SETNCCI_PART(msg, 1);
348         CAPIMSG_SETHANDLE_CONF(msg, handle);
349         CAPIMSG_SETINFO_CONF(msg, info);
350
351         /* emit message */
352         dump_rawmsg(DEBUG_MCMD, __func__, msg);
353         capi_ctr_handle_message(ctr, appl, cskb);
354 }
355
356
357 /*
358  * driver interface functions
359  * ==========================
360  */
361
362 /**
363  * gigaset_skb_sent() - acknowledge transmission of outgoing skb
364  * @bcs:        B channel descriptor structure.
365  * @skb:        sent data.
366  *
367  * Called by hardware module {bas,ser,usb}_gigaset when the data in a
368  * skb has been successfully sent, for signalling completion to the LL.
369  */
370 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *dskb)
371 {
372         struct cardstate *cs = bcs->cs;
373         struct gigaset_capi_ctr *iif = cs->iif;
374         struct gigaset_capi_appl *ap = bcs->ap;
375         unsigned char *req = skb_mac_header(dskb);
376         u16 flags;
377
378         /* update statistics */
379         ++bcs->trans_up;
380
381         if (!ap) {
382                 dev_err(cs->dev, "%s: no application\n", __func__);
383                 return;
384         }
385
386         /* don't send further B3 messages if disconnected */
387         if (ap->connected < APCONN_ACTIVE) {
388                 gig_dbg(DEBUG_LLDATA, "disconnected, discarding ack");
389                 return;
390         }
391
392         /*
393          * send DATA_B3_CONF if "delivery confirmation" bit was set in request;
394          * otherwise it has already been sent by do_data_b3_req()
395          */
396         flags = CAPIMSG_FLAGS(req);
397         if (flags & CAPI_FLAGS_DELIVERY_CONFIRMATION)
398                 send_data_b3_conf(cs, &iif->ctr, ap->id, CAPIMSG_MSGID(req),
399                                   bcs->channel + 1, CAPIMSG_HANDLE_REQ(req),
400                                   (flags & ~CAPI_FLAGS_DELIVERY_CONFIRMATION) ?
401                                         CapiFlagsNotSupportedByProtocol :
402                                         CAPI_NOERROR);
403 }
404 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
405
406 /**
407  * gigaset_skb_rcvd() - pass received skb to LL
408  * @bcs:        B channel descriptor structure.
409  * @skb:        received data.
410  *
411  * Called by hardware module {bas,ser,usb}_gigaset when user data has
412  * been successfully received, for passing to the LL.
413  * Warning: skb must not be accessed anymore!
414  */
415 void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
416 {
417         struct cardstate *cs = bcs->cs;
418         struct gigaset_capi_ctr *iif = cs->iif;
419         struct gigaset_capi_appl *ap = bcs->ap;
420         int len = skb->len;
421
422         /* update statistics */
423         bcs->trans_down++;
424
425         if (!ap) {
426                 dev_err(cs->dev, "%s: no application\n", __func__);
427                 return;
428         }
429
430         /* don't send further B3 messages if disconnected */
431         if (ap->connected < APCONN_ACTIVE) {
432                 gig_dbg(DEBUG_LLDATA, "disconnected, discarding data");
433                 dev_kfree_skb_any(skb);
434                 return;
435         }
436
437         /*
438          * prepend DATA_B3_IND message to payload
439          * Parameters: NCCI = 1, all others 0/unused
440          * frequent message, avoid _cmsg overhead
441          */
442         skb_push(skb, CAPI_DATA_B3_REQ_LEN);
443         CAPIMSG_SETLEN(skb->data, CAPI_DATA_B3_REQ_LEN);
444         CAPIMSG_SETAPPID(skb->data, ap->id);
445         CAPIMSG_SETCOMMAND(skb->data, CAPI_DATA_B3);
446         CAPIMSG_SETSUBCOMMAND(skb->data,  CAPI_IND);
447         CAPIMSG_SETMSGID(skb->data, ap->nextMessageNumber++);
448         CAPIMSG_SETCONTROLLER(skb->data, iif->ctr.cnr);
449         CAPIMSG_SETPLCI_PART(skb->data, bcs->channel + 1);
450         CAPIMSG_SETNCCI_PART(skb->data, 1);
451         /* Data parameter not used */
452         CAPIMSG_SETDATALEN(skb->data, len);
453         /* Data handle parameter not used */
454         CAPIMSG_SETFLAGS(skb->data, 0);
455         /* Data64 parameter not present */
456
457         /* emit message */
458         dump_rawmsg(DEBUG_LLDATA, "DATA_B3_IND", skb->data);
459         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
460 }
461 EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
462
463 /**
464  * gigaset_isdn_rcv_err() - signal receive error
465  * @bcs:        B channel descriptor structure.
466  *
467  * Called by hardware module {bas,ser,usb}_gigaset when a receive error
468  * has occurred, for signalling to the LL.
469  */
470 void gigaset_isdn_rcv_err(struct bc_state *bcs)
471 {
472         /* if currently ignoring packets, just count down */
473         if (bcs->ignore) {
474                 bcs->ignore--;
475                 return;
476         }
477
478         /* update statistics */
479         bcs->corrupted++;
480
481         /* ToDo: signal error -> LL */
482 }
483 EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
484
485 /**
486  * gigaset_isdn_icall() - signal incoming call
487  * @at_state:   connection state structure.
488  *
489  * Called by main module at tasklet level to notify the LL that an incoming
490  * call has been received. @at_state contains the parameters of the call.
491  *
492  * Return value: call disposition (ICALL_*)
493  */
494 int gigaset_isdn_icall(struct at_state_t *at_state)
495 {
496         struct cardstate *cs = at_state->cs;
497         struct bc_state *bcs = at_state->bcs;
498         struct gigaset_capi_ctr *iif = cs->iif;
499         struct gigaset_capi_appl *ap;
500         u32 actCIPmask;
501         struct sk_buff *skb;
502         unsigned int msgsize;
503         int i;
504
505         /*
506          * ToDo: signal calls without a free B channel, too
507          * (requires a u8 handle for the at_state structure that can
508          * be stored in the PLCI and used in the CONNECT_RESP message
509          * handler to retrieve it)
510          */
511         if (!bcs)
512                 return ICALL_IGNORE;
513
514         /* prepare CONNECT_IND message, using B channel number as PLCI */
515         capi_cmsg_header(&iif->hcmsg, 0, CAPI_CONNECT, CAPI_IND, 0,
516                          iif->ctr.cnr | ((bcs->channel + 1) << 8));
517
518         /* minimum size, all structs empty */
519         msgsize = CAPI_CONNECT_IND_BASELEN;
520
521         /* Bearer Capability (mandatory) */
522         if (at_state->str_var[STR_ZBC]) {
523                 /* pass on BC from Gigaset */
524                 if (encode_ie(at_state->str_var[STR_ZBC], iif->bc_buf,
525                               MAX_BC_OCTETS) < 0) {
526                         dev_warn(cs->dev, "RING ignored - bad BC %s\n",
527                                  at_state->str_var[STR_ZBC]);
528                         return ICALL_IGNORE;
529                 }
530
531                 /* look up corresponding CIP value */
532                 iif->hcmsg.CIPValue = 0;        /* default if nothing found */
533                 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
534                         if (cip2bchlc[i].bc != NULL &&
535                             cip2bchlc[i].hlc == NULL &&
536                             !strcmp(cip2bchlc[i].bc,
537                                     at_state->str_var[STR_ZBC])) {
538                                 iif->hcmsg.CIPValue = i;
539                                 break;
540                         }
541         } else {
542                 /* no BC (internal call): assume CIP 1 (speech, A-law) */
543                 iif->hcmsg.CIPValue = 1;
544                 encode_ie(cip2bchlc[1].bc, iif->bc_buf, MAX_BC_OCTETS);
545         }
546         iif->hcmsg.BC = iif->bc_buf;
547         msgsize += iif->hcmsg.BC[0];
548
549         /* High Layer Compatibility (optional) */
550         if (at_state->str_var[STR_ZHLC]) {
551                 /* pass on HLC from Gigaset */
552                 if (encode_ie(at_state->str_var[STR_ZHLC], iif->hlc_buf,
553                               MAX_HLC_OCTETS) < 0) {
554                         dev_warn(cs->dev, "RING ignored - bad HLC %s\n",
555                                  at_state->str_var[STR_ZHLC]);
556                         return ICALL_IGNORE;
557                 }
558                 iif->hcmsg.HLC = iif->hlc_buf;
559                 msgsize += iif->hcmsg.HLC[0];
560
561                 /* look up corresponding CIP value */
562                 /* keep BC based CIP value if none found */
563                 if (at_state->str_var[STR_ZBC])
564                         for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
565                                 if (cip2bchlc[i].hlc != NULL &&
566                                     !strcmp(cip2bchlc[i].hlc,
567                                             at_state->str_var[STR_ZHLC]) &&
568                                     !strcmp(cip2bchlc[i].bc,
569                                             at_state->str_var[STR_ZBC])) {
570                                         iif->hcmsg.CIPValue = i;
571                                         break;
572                                 }
573         }
574
575         /* Called Party Number (optional) */
576         if (at_state->str_var[STR_ZCPN]) {
577                 i = strlen(at_state->str_var[STR_ZCPN]);
578                 if (i > MAX_NUMBER_DIGITS) {
579                         dev_warn(cs->dev, "RING ignored - bad number %s\n",
580                                  at_state->str_var[STR_ZBC]);
581                         return ICALL_IGNORE;
582                 }
583                 iif->cdpty_buf[0] = i + 1;
584                 iif->cdpty_buf[1] = 0x80; /* type / numbering plan unknown */
585                 memcpy(iif->cdpty_buf+2, at_state->str_var[STR_ZCPN], i);
586                 iif->hcmsg.CalledPartyNumber = iif->cdpty_buf;
587                 msgsize += iif->hcmsg.CalledPartyNumber[0];
588         }
589
590         /* Calling Party Number (optional) */
591         if (at_state->str_var[STR_NMBR]) {
592                 i = strlen(at_state->str_var[STR_NMBR]);
593                 if (i > MAX_NUMBER_DIGITS) {
594                         dev_warn(cs->dev, "RING ignored - bad number %s\n",
595                                  at_state->str_var[STR_ZBC]);
596                         return ICALL_IGNORE;
597                 }
598                 iif->cgpty_buf[0] = i + 2;
599                 iif->cgpty_buf[1] = 0x00; /* type / numbering plan unknown */
600                 iif->cgpty_buf[2] = 0x80; /* pres. allowed, not screened */
601                 memcpy(iif->cgpty_buf+3, at_state->str_var[STR_NMBR], i);
602                 iif->hcmsg.CallingPartyNumber = iif->cgpty_buf;
603                 msgsize += iif->hcmsg.CallingPartyNumber[0];
604         }
605
606         /* remaining parameters (not supported, always left NULL):
607          * - CalledPartySubaddress
608          * - CallingPartySubaddress
609          * - AdditionalInfo
610          *   - BChannelinformation
611          *   - Keypadfacility
612          *   - Useruserdata
613          *   - Facilitydataarray
614          */
615
616         gig_dbg(DEBUG_CMD, "icall: PLCI %x CIP %d BC %s",
617                 iif->hcmsg.adr.adrPLCI, iif->hcmsg.CIPValue,
618                 format_ie(iif->hcmsg.BC));
619         gig_dbg(DEBUG_CMD, "icall: HLC %s",
620                 format_ie(iif->hcmsg.HLC));
621         gig_dbg(DEBUG_CMD, "icall: CgPty %s",
622                 format_ie(iif->hcmsg.CallingPartyNumber));
623         gig_dbg(DEBUG_CMD, "icall: CdPty %s",
624                 format_ie(iif->hcmsg.CalledPartyNumber));
625
626         /* scan application list for matching listeners */
627         bcs->ap = NULL;
628         actCIPmask = 1 | (1 << iif->hcmsg.CIPValue);
629         list_for_each_entry(ap, &iif->appls, ctrlist)
630                 if (actCIPmask & ap->listenCIPmask) {
631                         /* build CONNECT_IND message for this application */
632                         iif->hcmsg.ApplId = ap->id;
633                         iif->hcmsg.Messagenumber = ap->nextMessageNumber++;
634
635                         skb = alloc_skb(msgsize, GFP_ATOMIC);
636                         if (!skb) {
637                                 dev_err(cs->dev, "%s: out of memory\n",
638                                         __func__);
639                                 break;
640                         }
641                         capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
642                         dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
643
644                         /* add to listeners on this B channel, update state */
645                         ap->bcnext = bcs->ap;
646                         bcs->ap = ap;
647                         bcs->chstate |= CHS_NOTIFY_LL;
648                         ap->connected = APCONN_SETUP;
649
650                         /* emit message */
651                         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
652                 }
653
654         /*
655          * Return "accept" if any listeners.
656          * Gigaset will send ALERTING.
657          * There doesn't seem to be a way to avoid this.
658          */
659         return bcs->ap ? ICALL_ACCEPT : ICALL_IGNORE;
660 }
661
662 /*
663  * send a DISCONNECT_IND message to an application
664  * does not sleep, clobbers the controller's hcmsg structure
665  */
666 static void send_disconnect_ind(struct bc_state *bcs,
667                                 struct gigaset_capi_appl *ap, u16 reason)
668 {
669         struct cardstate *cs = bcs->cs;
670         struct gigaset_capi_ctr *iif = cs->iif;
671         struct sk_buff *skb;
672
673         if (ap->connected == APCONN_NONE)
674                 return;
675
676         capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT, CAPI_IND,
677                          ap->nextMessageNumber++,
678                          iif->ctr.cnr | ((bcs->channel + 1) << 8));
679         iif->hcmsg.Reason = reason;
680         skb = alloc_skb(CAPI_DISCONNECT_IND_LEN, GFP_ATOMIC);
681         if (!skb) {
682                 dev_err(cs->dev, "%s: out of memory\n", __func__);
683                 return;
684         }
685         capi_cmsg2message(&iif->hcmsg, __skb_put(skb, CAPI_DISCONNECT_IND_LEN));
686         dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
687         ap->connected = APCONN_NONE;
688         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
689 }
690
691 /*
692  * send a DISCONNECT_B3_IND message to an application
693  * Parameters: NCCI = 1, NCPI empty, Reason_B3 = 0
694  * does not sleep, clobbers the controller's hcmsg structure
695  */
696 static void send_disconnect_b3_ind(struct bc_state *bcs,
697                                    struct gigaset_capi_appl *ap)
698 {
699         struct cardstate *cs = bcs->cs;
700         struct gigaset_capi_ctr *iif = cs->iif;
701         struct sk_buff *skb;
702
703         /* nothing to do if no logical connection active */
704         if (ap->connected < APCONN_ACTIVE)
705                 return;
706         ap->connected = APCONN_SETUP;
707
708         capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
709                          ap->nextMessageNumber++,
710                          iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
711         skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_ATOMIC);
712         if (!skb) {
713                 dev_err(cs->dev, "%s: out of memory\n", __func__);
714                 return;
715         }
716         capi_cmsg2message(&iif->hcmsg,
717                           __skb_put(skb, CAPI_DISCONNECT_B3_IND_BASELEN));
718         dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
719         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
720 }
721
722 /**
723  * gigaset_isdn_connD() - signal D channel connect
724  * @bcs:        B channel descriptor structure.
725  *
726  * Called by main module at tasklet level to notify the LL that the D channel
727  * connection has been established.
728  */
729 void gigaset_isdn_connD(struct bc_state *bcs)
730 {
731         struct cardstate *cs = bcs->cs;
732         struct gigaset_capi_ctr *iif = cs->iif;
733         struct gigaset_capi_appl *ap = bcs->ap;
734         struct sk_buff *skb;
735         unsigned int msgsize;
736
737         if (!ap) {
738                 dev_err(cs->dev, "%s: no application\n", __func__);
739                 return;
740         }
741         while (ap->bcnext) {
742                 /* this should never happen */
743                 dev_warn(cs->dev, "%s: dropping extra application %u\n",
744                          __func__, ap->bcnext->id);
745                 send_disconnect_ind(bcs, ap->bcnext,
746                                     CapiCallGivenToOtherApplication);
747                 ap->bcnext = ap->bcnext->bcnext;
748         }
749         if (ap->connected == APCONN_NONE) {
750                 dev_warn(cs->dev, "%s: application %u not connected\n",
751                          __func__, ap->id);
752                 return;
753         }
754
755         /* prepare CONNECT_ACTIVE_IND message
756          * Note: LLC not supported by device
757          */
758         capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_CONNECT_ACTIVE, CAPI_IND,
759                          ap->nextMessageNumber++,
760                          iif->ctr.cnr | ((bcs->channel + 1) << 8));
761
762         /* minimum size, all structs empty */
763         msgsize = CAPI_CONNECT_ACTIVE_IND_BASELEN;
764
765         /* ToDo: set parameter: Connected number
766          * (requires ev-layer state machine extension to collect
767          * ZCON device reply)
768          */
769
770         /* build and emit CONNECT_ACTIVE_IND message */
771         skb = alloc_skb(msgsize, GFP_ATOMIC);
772         if (!skb) {
773                 dev_err(cs->dev, "%s: out of memory\n", __func__);
774                 return;
775         }
776         capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
777         dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
778         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
779 }
780
781 /**
782  * gigaset_isdn_hupD() - signal D channel hangup
783  * @bcs:        B channel descriptor structure.
784  *
785  * Called by main module at tasklet level to notify the LL that the D channel
786  * connection has been shut down.
787  */
788 void gigaset_isdn_hupD(struct bc_state *bcs)
789 {
790         struct gigaset_capi_appl *ap;
791
792         /*
793          * ToDo: pass on reason code reported by device
794          * (requires ev-layer state machine extension to collect
795          * ZCAU device reply)
796          */
797         for (ap = bcs->ap; ap != NULL; ap = ap->bcnext) {
798                 send_disconnect_b3_ind(bcs, ap);
799                 send_disconnect_ind(bcs, ap, 0);
800         }
801         bcs->ap = NULL;
802 }
803
804 /**
805  * gigaset_isdn_connB() - signal B channel connect
806  * @bcs:        B channel descriptor structure.
807  *
808  * Called by main module at tasklet level to notify the LL that the B channel
809  * connection has been established.
810  */
811 void gigaset_isdn_connB(struct bc_state *bcs)
812 {
813         struct cardstate *cs = bcs->cs;
814         struct gigaset_capi_ctr *iif = cs->iif;
815         struct gigaset_capi_appl *ap = bcs->ap;
816         struct sk_buff *skb;
817         unsigned int msgsize;
818         u8 command;
819
820         if (!ap) {
821                 dev_err(cs->dev, "%s: no application\n", __func__);
822                 return;
823         }
824         while (ap->bcnext) {
825                 /* this should never happen */
826                 dev_warn(cs->dev, "%s: dropping extra application %u\n",
827                          __func__, ap->bcnext->id);
828                 send_disconnect_ind(bcs, ap->bcnext,
829                                     CapiCallGivenToOtherApplication);
830                 ap->bcnext = ap->bcnext->bcnext;
831         }
832         if (!ap->connected) {
833                 dev_warn(cs->dev, "%s: application %u not connected\n",
834                          __func__, ap->id);
835                 return;
836         }
837
838         /*
839          * emit CONNECT_B3_ACTIVE_IND if we already got CONNECT_B3_REQ;
840          * otherwise we have to emit CONNECT_B3_IND first, and follow up with
841          * CONNECT_B3_ACTIVE_IND in reply to CONNECT_B3_RESP
842          * Parameters in both cases always: NCCI = 1, NCPI empty
843          */
844         if (ap->connected >= APCONN_ACTIVE) {
845                 command = CAPI_CONNECT_B3_ACTIVE;
846                 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
847         } else {
848                 command = CAPI_CONNECT_B3;
849                 msgsize = CAPI_CONNECT_B3_IND_BASELEN;
850         }
851         capi_cmsg_header(&iif->hcmsg, ap->id, command, CAPI_IND,
852                          ap->nextMessageNumber++,
853                          iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
854         skb = alloc_skb(msgsize, GFP_ATOMIC);
855         if (!skb) {
856                 dev_err(cs->dev, "%s: out of memory\n", __func__);
857                 return;
858         }
859         capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
860         dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
861         ap->connected = APCONN_ACTIVE;
862         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
863 }
864
865 /**
866  * gigaset_isdn_hupB() - signal B channel hangup
867  * @bcs:        B channel descriptor structure.
868  *
869  * Called by main module to notify the LL that the B channel connection has
870  * been shut down.
871  */
872 void gigaset_isdn_hupB(struct bc_state *bcs)
873 {
874         struct cardstate *cs = bcs->cs;
875         struct gigaset_capi_appl *ap = bcs->ap;
876
877         /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
878
879         if (!ap) {
880                 dev_err(cs->dev, "%s: no application\n", __func__);
881                 return;
882         }
883
884         send_disconnect_b3_ind(bcs, ap);
885 }
886
887 /**
888  * gigaset_isdn_start() - signal device availability
889  * @cs:         device descriptor structure.
890  *
891  * Called by main module to notify the LL that the device is available for
892  * use.
893  */
894 void gigaset_isdn_start(struct cardstate *cs)
895 {
896         struct gigaset_capi_ctr *iif = cs->iif;
897
898         /* fill profile data: manufacturer name */
899         strcpy(iif->ctr.manu, "Siemens");
900         /* CAPI and device version */
901         iif->ctr.version.majorversion = 2;              /* CAPI 2.0 */
902         iif->ctr.version.minorversion = 0;
903         /* ToDo: check/assert cs->gotfwver? */
904         iif->ctr.version.majormanuversion = cs->fwver[0];
905         iif->ctr.version.minormanuversion = cs->fwver[1];
906         /* number of B channels supported */
907         iif->ctr.profile.nbchannel = cs->channels;
908         /* global options: internal controller, supplementary services */
909         iif->ctr.profile.goptions = 0x11;
910         /* B1 protocols: 64 kbit/s HDLC or transparent */
911         iif->ctr.profile.support1 =  0x03;
912         /* B2 protocols: transparent only */
913         /* ToDo: X.75 SLP ? */
914         iif->ctr.profile.support2 =  0x02;
915         /* B3 protocols: transparent only */
916         iif->ctr.profile.support3 =  0x01;
917         /* no serial number */
918         strcpy(iif->ctr.serial, "0");
919         capi_ctr_ready(&iif->ctr);
920 }
921
922 /**
923  * gigaset_isdn_stop() - signal device unavailability
924  * @cs:         device descriptor structure.
925  *
926  * Called by main module to notify the LL that the device is no longer
927  * available for use.
928  */
929 void gigaset_isdn_stop(struct cardstate *cs)
930 {
931         struct gigaset_capi_ctr *iif = cs->iif;
932         capi_ctr_down(&iif->ctr);
933 }
934
935 /*
936  * kernel CAPI callback methods
937  * ============================
938  */
939
940 /*
941  * register CAPI application
942  */
943 static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
944                            capi_register_params *rp)
945 {
946         struct gigaset_capi_ctr *iif
947                 = container_of(ctr, struct gigaset_capi_ctr, ctr);
948         struct cardstate *cs = ctr->driverdata;
949         struct gigaset_capi_appl *ap;
950
951         list_for_each_entry(ap, &iif->appls, ctrlist)
952                 if (ap->id == appl) {
953                         dev_notice(cs->dev,
954                                    "application %u already registered\n", appl);
955                         return;
956                 }
957
958         ap = kzalloc(sizeof(*ap), GFP_KERNEL);
959         if (!ap) {
960                 dev_err(cs->dev, "%s: out of memory\n", __func__);
961                 return;
962         }
963         ap->id = appl;
964         ap->rp = *rp;
965
966         list_add(&ap->ctrlist, &iif->appls);
967 }
968
969 /*
970  * release CAPI application
971  */
972 static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
973 {
974         struct gigaset_capi_ctr *iif
975                 = container_of(ctr, struct gigaset_capi_ctr, ctr);
976         struct cardstate *cs = iif->ctr.driverdata;
977         struct gigaset_capi_appl *ap, *tmp;
978
979         list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
980                 if (ap->id == appl) {
981                         if (ap->connected != APCONN_NONE) {
982                                 dev_err(cs->dev,
983                                         "%s: application %u still connected\n",
984                                         __func__, ap->id);
985                                 /* ToDo: clear active connection */
986                         }
987                         list_del(&ap->ctrlist);
988                         kfree(ap);
989                 }
990
991 }
992
993 /*
994  * =====================================================================
995  * outgoing CAPI message handler
996  * =====================================================================
997  */
998
999 /*
1000  * helper function: emit reply message with given Info value
1001  */
1002 static void send_conf(struct gigaset_capi_ctr *iif,
1003                       struct gigaset_capi_appl *ap,
1004                       struct sk_buff *skb,
1005                       u16 info)
1006 {
1007         /*
1008          * _CONF replies always only have NCCI and Info parameters
1009          * so they'll fit into the _REQ message skb
1010          */
1011         capi_cmsg_answer(&iif->acmsg);
1012         iif->acmsg.Info = info;
1013         capi_cmsg2message(&iif->acmsg, skb->data);
1014         __skb_trim(skb, CAPI_STDCONF_LEN);
1015         dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1016         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1017 }
1018
1019 /*
1020  * process FACILITY_REQ message
1021  */
1022 static void do_facility_req(struct gigaset_capi_ctr *iif,
1023                             struct gigaset_capi_appl *ap,
1024                             struct sk_buff *skb)
1025 {
1026         struct cardstate *cs = iif->ctr.driverdata;
1027         _cmsg *cmsg = &iif->acmsg;
1028         struct sk_buff *cskb;
1029         u8 *pparam;
1030         unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
1031         u16 function, info;
1032         static u8 confparam[10];        /* max. 9 octets + length byte */
1033
1034         /* decode message */
1035         capi_message2cmsg(cmsg, skb->data);
1036         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1037
1038         /*
1039          * Facility Request Parameter is not decoded by capi_message2cmsg()
1040          * encoding depends on Facility Selector
1041          */
1042         switch (cmsg->FacilitySelector) {
1043         case CAPI_FACILITY_DTMF:        /* ToDo */
1044                 info = CapiFacilityNotSupported;
1045                 confparam[0] = 2;       /* length */
1046                 /* DTMF information: Unknown DTMF request */
1047                 capimsg_setu16(confparam, 1, 2);
1048                 break;
1049
1050         case CAPI_FACILITY_V42BIS:      /* not supported */
1051                 info = CapiFacilityNotSupported;
1052                 confparam[0] = 2;       /* length */
1053                 /* V.42 bis information: not available */
1054                 capimsg_setu16(confparam, 1, 1);
1055                 break;
1056
1057         case CAPI_FACILITY_SUPPSVC:
1058                 /* decode Function parameter */
1059                 pparam = cmsg->FacilityRequestParameter;
1060                 if (pparam == NULL || *pparam < 2) {
1061                         dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
1062                                    "Facility Request Parameter");
1063                         send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1064                         return;
1065                 }
1066                 function = CAPIMSG_U16(pparam, 1);
1067                 switch (function) {
1068                 case CAPI_SUPPSVC_GETSUPPORTED:
1069                         info = CapiSuccess;
1070                         /* Supplementary Service specific parameter */
1071                         confparam[3] = 6;       /* length */
1072                         /* Supplementary services info: Success */
1073                         capimsg_setu16(confparam, 4, CapiSuccess);
1074                         /* Supported Services: none */
1075                         capimsg_setu32(confparam, 6, 0);
1076                         break;
1077                 /* ToDo: add supported services */
1078                 default:
1079                         info = CapiFacilitySpecificFunctionNotSupported;
1080                         /* Supplementary Service specific parameter */
1081                         confparam[3] = 2;       /* length */
1082                         /* Supplementary services info: not supported */
1083                         capimsg_setu16(confparam, 4,
1084                                        CapiSupplementaryServiceNotSupported);
1085                 }
1086
1087                 /* Facility confirmation parameter */
1088                 confparam[0] = confparam[3] + 3;        /* total length */
1089                 /* Function: copy from _REQ message */
1090                 capimsg_setu16(confparam, 1, function);
1091                 /* Supplementary Service specific parameter already set above */
1092                 break;
1093
1094         case CAPI_FACILITY_WAKEUP:      /* ToDo */
1095                 info = CapiFacilityNotSupported;
1096                 confparam[0] = 2;       /* length */
1097                 /* Number of accepted awake request parameters: 0 */
1098                 capimsg_setu16(confparam, 1, 0);
1099                 break;
1100
1101         default:
1102                 info = CapiFacilityNotSupported;
1103                 confparam[0] = 0;       /* empty struct */
1104         }
1105
1106         /* send FACILITY_CONF with given Info and confirmation parameter */
1107         capi_cmsg_answer(cmsg);
1108         cmsg->Info = info;
1109         cmsg->FacilityConfirmationParameter = confparam;
1110         msgsize += confparam[0];        /* length */
1111         cskb = alloc_skb(msgsize, GFP_ATOMIC);
1112         if (!cskb) {
1113                 dev_err(cs->dev, "%s: out of memory\n", __func__);
1114                 return;
1115         }
1116         capi_cmsg2message(cmsg, __skb_put(cskb, msgsize));
1117         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1118         capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
1119 }
1120
1121
1122 /*
1123  * process LISTEN_REQ message
1124  * just store the masks in the application data structure
1125  */
1126 static void do_listen_req(struct gigaset_capi_ctr *iif,
1127                           struct gigaset_capi_appl *ap,
1128                           struct sk_buff *skb)
1129 {
1130         /* decode message */
1131         capi_message2cmsg(&iif->acmsg, skb->data);
1132         dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1133
1134         /* store listening parameters */
1135         ap->listenInfoMask = iif->acmsg.InfoMask;
1136         ap->listenCIPmask = iif->acmsg.CIPmask;
1137         send_conf(iif, ap, skb, CapiSuccess);
1138 }
1139
1140 /*
1141  * process ALERT_REQ message
1142  * nothing to do, Gigaset always alerts anyway
1143  */
1144 static void do_alert_req(struct gigaset_capi_ctr *iif,
1145                          struct gigaset_capi_appl *ap,
1146                          struct sk_buff *skb)
1147 {
1148         /* decode message */
1149         capi_message2cmsg(&iif->acmsg, skb->data);
1150         dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1151         send_conf(iif, ap, skb, CapiAlertAlreadySent);
1152 }
1153
1154 /*
1155  * process CONNECT_REQ message
1156  * allocate a B channel, prepare dial commands, queue a DIAL event,
1157  * emit CONNECT_CONF reply
1158  */
1159 static void do_connect_req(struct gigaset_capi_ctr *iif,
1160                            struct gigaset_capi_appl *ap,
1161                            struct sk_buff *skb)
1162 {
1163         struct cardstate *cs = iif->ctr.driverdata;
1164         _cmsg *cmsg = &iif->acmsg;
1165         struct bc_state *bcs;
1166         char **commands;
1167         char *s;
1168         u8 *pp;
1169         int i, l, lbc, lhlc;
1170         u16 info;
1171
1172         /* decode message */
1173         capi_message2cmsg(cmsg, skb->data);
1174         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1175
1176         /* get free B channel & construct PLCI */
1177         bcs = gigaset_get_free_channel(cs);
1178         if (!bcs) {
1179                 dev_notice(cs->dev, "%s: no B channel available\n",
1180                            "CONNECT_REQ");
1181                 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1182                 return;
1183         }
1184         ap->bcnext = NULL;
1185         bcs->ap = ap;
1186         bcs->rx_bufsize = ap->rp.datablklen;
1187         dev_kfree_skb(bcs->rx_skb);
1188         gigaset_new_rx_skb(bcs);
1189         cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1190
1191         /* build command table */
1192         commands = kzalloc(AT_NUM*(sizeof *commands), GFP_KERNEL);
1193         if (!commands)
1194                 goto oom;
1195
1196         /* encode parameter: Called party number */
1197         pp = cmsg->CalledPartyNumber;
1198         if (pp == NULL || *pp == 0) {
1199                 dev_notice(cs->dev, "%s: %s missing\n",
1200                            "CONNECT_REQ", "Called party number");
1201                 info = CapiIllMessageParmCoding;
1202                 goto error;
1203         }
1204         l = *pp++;
1205         /* check type of number/numbering plan byte */
1206         switch (*pp) {
1207         case 0x80:      /* unknown type / unknown numbering plan */
1208         case 0x81:      /* unknown type / ISDN/Telephony numbering plan */
1209                 break;
1210         default:        /* others: warn about potential misinterpretation */
1211                 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1212                            "CONNECT_REQ", "Called party number", *pp);
1213         }
1214         pp++;
1215         l--;
1216         /* translate "**" internal call prefix to CTP value */
1217         if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1218                 s = "^SCTP=0\r";
1219                 pp += 2;
1220                 l -= 2;
1221         } else {
1222                 s = "^SCTP=1\r";
1223         }
1224         commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1225         if (!commands[AT_TYPE])
1226                 goto oom;
1227         commands[AT_DIAL] = kmalloc(l+3, GFP_KERNEL);
1228         if (!commands[AT_DIAL])
1229                 goto oom;
1230         snprintf(commands[AT_DIAL], l+3, "D%.*s\r", l, pp);
1231
1232         /* encode parameter: Calling party number */
1233         pp = cmsg->CallingPartyNumber;
1234         if (pp != NULL && *pp > 0) {
1235                 l = *pp++;
1236
1237                 /* check type of number/numbering plan byte */
1238                 /* ToDo: allow for/handle Ext=1? */
1239                 switch (*pp) {
1240                 case 0x00:      /* unknown type / unknown numbering plan */
1241                 case 0x01:      /* unknown type / ISDN/Telephony num. plan */
1242                         break;
1243                 default:
1244                         dev_notice(cs->dev,
1245                                    "%s: %s type/plan 0x%02x unsupported\n",
1246                                    "CONNECT_REQ", "Calling party number", *pp);
1247                 }
1248                 pp++;
1249                 l--;
1250
1251                 /* check presentation indicator */
1252                 if (!l) {
1253                         dev_notice(cs->dev, "%s: %s IE truncated\n",
1254                                    "CONNECT_REQ", "Calling party number");
1255                         info = CapiIllMessageParmCoding;
1256                         goto error;
1257                 }
1258                 switch (*pp & 0xfc) { /* ignore Screening indicator */
1259                 case 0x80:      /* Presentation allowed */
1260                         s = "^SCLIP=1\r";
1261                         break;
1262                 case 0xa0:      /* Presentation restricted */
1263                         s = "^SCLIP=0\r";
1264                         break;
1265                 default:
1266                         dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1267                                    "CONNECT_REQ",
1268                                    "Presentation/Screening indicator",
1269                                    *pp);
1270                         s = "^SCLIP=1\r";
1271                 }
1272                 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1273                 if (!commands[AT_CLIP])
1274                         goto oom;
1275                 pp++;
1276                 l--;
1277
1278                 if (l) {
1279                         /* number */
1280                         commands[AT_MSN] = kmalloc(l+8, GFP_KERNEL);
1281                         if (!commands[AT_MSN])
1282                                 goto oom;
1283                         snprintf(commands[AT_MSN], l+8, "^SMSN=%*s\r", l, pp);
1284                 }
1285         }
1286
1287         /* check parameter: CIP Value */
1288         if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
1289             (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1290                 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1291                            "CONNECT_REQ", cmsg->CIPValue);
1292                 info = CapiCipValueUnknown;
1293                 goto error;
1294         }
1295
1296         /*
1297          * check/encode parameters: BC & HLC
1298          * must be encoded together as device doesn't accept HLC separately
1299          * explicit parameters override values derived from CIP
1300          */
1301
1302         /* determine lengths */
1303         if (cmsg->BC && cmsg->BC[0])            /* BC specified explicitly */
1304                 lbc = 2*cmsg->BC[0];
1305         else if (cip2bchlc[cmsg->CIPValue].bc)  /* BC derived from CIP */
1306                 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1307         else                                    /* no BC */
1308                 lbc = 0;
1309         if (cmsg->HLC && cmsg->HLC[0])          /* HLC specified explicitly */
1310                 lhlc = 2*cmsg->HLC[0];
1311         else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1312                 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1313         else                                    /* no HLC */
1314                 lhlc = 0;
1315
1316         if (lbc) {
1317                 /* have BC: allocate and assemble command string */
1318                 l = lbc + 7;            /* "^SBC=" + value + "\r" + null byte */
1319                 if (lhlc)
1320                         l += lhlc + 7;  /* ";^SHLC=" + value */
1321                 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1322                 if (!commands[AT_BC])
1323                         goto oom;
1324                 strcpy(commands[AT_BC], "^SBC=");
1325                 if (cmsg->BC && cmsg->BC[0])    /* BC specified explicitly */
1326                         decode_ie(cmsg->BC, commands[AT_BC] + 5);
1327                 else                            /* BC derived from CIP */
1328                         strcpy(commands[AT_BC] + 5,
1329                                cip2bchlc[cmsg->CIPValue].bc);
1330                 if (lhlc) {
1331                         strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1332                         if (cmsg->HLC && cmsg->HLC[0])
1333                                 /* HLC specified explicitly */
1334                                 decode_ie(cmsg->HLC,
1335                                           commands[AT_BC] + lbc + 12);
1336                         else    /* HLC derived from CIP */
1337                                 strcpy(commands[AT_BC] + lbc + 12,
1338                                        cip2bchlc[cmsg->CIPValue].hlc);
1339                 }
1340                 strcpy(commands[AT_BC] + l - 2, "\r");
1341         } else {
1342                 /* no BC */
1343                 if (lhlc) {
1344                         dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1345                                    "CONNECT_REQ");
1346                         info = CapiIllMessageParmCoding; /* ? */
1347                         goto error;
1348                 }
1349         }
1350
1351         /* check/encode parameter: B Protocol */
1352         if (cmsg->BProtocol == CAPI_DEFAULT) {
1353                 bcs->proto2 = L2_HDLC;
1354                 dev_warn(cs->dev,
1355                     "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1356         } else {
1357                 switch (cmsg->B1protocol) {
1358                 case 0:
1359                         bcs->proto2 = L2_HDLC;
1360                         break;
1361                 case 1:
1362                         bcs->proto2 = L2_VOICE;
1363                         break;
1364                 default:
1365                         dev_warn(cs->dev,
1366                             "B1 Protocol %u unsupported, using Transparent\n",
1367                                  cmsg->B1protocol);
1368                         bcs->proto2 = L2_VOICE;
1369                 }
1370                 if (cmsg->B2protocol != 1)
1371                         dev_warn(cs->dev,
1372                             "B2 Protocol %u unsupported, using Transparent\n",
1373                                  cmsg->B2protocol);
1374                 if (cmsg->B3protocol != 0)
1375                         dev_warn(cs->dev,
1376                             "B3 Protocol %u unsupported, using Transparent\n",
1377                                  cmsg->B3protocol);
1378                 ignore_cstruct_param(cs, cmsg->B1configuration,
1379                                         "CONNECT_REQ", "B1 Configuration");
1380                 ignore_cstruct_param(cs, cmsg->B2configuration,
1381                                         "CONNECT_REQ", "B2 Configuration");
1382                 ignore_cstruct_param(cs, cmsg->B3configuration,
1383                                         "CONNECT_REQ", "B3 Configuration");
1384         }
1385         commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1386         if (!commands[AT_PROTO])
1387                 goto oom;
1388         snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1389
1390         /* ToDo: check/encode remaining parameters */
1391         ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
1392                                         "CONNECT_REQ", "Called pty subaddr");
1393         ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
1394                                         "CONNECT_REQ", "Calling pty subaddr");
1395         ignore_cstruct_param(cs, cmsg->LLC,
1396                                         "CONNECT_REQ", "LLC");
1397         if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1398                 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1399                                         "CONNECT_REQ", "B Channel Information");
1400                 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1401                                         "CONNECT_REQ", "Keypad Facility");
1402                 ignore_cstruct_param(cs, cmsg->Useruserdata,
1403                                         "CONNECT_REQ", "User-User Data");
1404                 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1405                                         "CONNECT_REQ", "Facility Data Array");
1406         }
1407
1408         /* encode parameter: B channel to use */
1409         commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1410         if (!commands[AT_ISO])
1411                 goto oom;
1412         snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1413                  (unsigned) bcs->channel + 1);
1414
1415         /* queue & schedule EV_DIAL event */
1416         if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
1417                                bcs->at_state.seq_index, NULL)) {
1418                 info = CAPI_MSGOSRESOURCEERR;
1419                 goto error;
1420         }
1421         gigaset_schedule_event(cs);
1422         ap->connected = APCONN_SETUP;
1423         send_conf(iif, ap, skb, CapiSuccess);
1424         return;
1425
1426 oom:
1427         dev_err(cs->dev, "%s: out of memory\n", __func__);
1428         info = CAPI_MSGOSRESOURCEERR;
1429 error:
1430         if (commands)
1431                 for (i = 0; i < AT_NUM; i++)
1432                         kfree(commands[i]);
1433         kfree(commands);
1434         gigaset_free_channel(bcs);
1435         send_conf(iif, ap, skb, info);
1436 }
1437
1438 /*
1439  * process CONNECT_RESP message
1440  * checks protocol parameters and queues an ACCEPT or HUP event
1441  */
1442 static void do_connect_resp(struct gigaset_capi_ctr *iif,
1443                             struct gigaset_capi_appl *ap,
1444                             struct sk_buff *skb)
1445 {
1446         struct cardstate *cs = iif->ctr.driverdata;
1447         _cmsg *cmsg = &iif->acmsg;
1448         struct bc_state *bcs;
1449         struct gigaset_capi_appl *oap;
1450         int channel;
1451
1452         /* decode message */
1453         capi_message2cmsg(cmsg, skb->data);
1454         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1455         dev_kfree_skb_any(skb);
1456
1457         /* extract and check channel number from PLCI */
1458         channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1459         if (!channel || channel > cs->channels) {
1460                 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1461                            "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1462                 return;
1463         }
1464         bcs = cs->bcs + channel - 1;
1465
1466         switch (cmsg->Reject) {
1467         case 0:         /* Accept */
1468                 /* drop all competing applications, keep only this one */
1469                 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext)
1470                         if (oap != ap)
1471                                 send_disconnect_ind(bcs, oap,
1472                                         CapiCallGivenToOtherApplication);
1473                 ap->bcnext = NULL;
1474                 bcs->ap = ap;
1475                 bcs->rx_bufsize = ap->rp.datablklen;
1476                 dev_kfree_skb(bcs->rx_skb);
1477                 gigaset_new_rx_skb(bcs);
1478                 bcs->chstate |= CHS_NOTIFY_LL;
1479
1480                 /* check/encode B channel protocol */
1481                 if (cmsg->BProtocol == CAPI_DEFAULT) {
1482                         bcs->proto2 = L2_HDLC;
1483                         dev_warn(cs->dev,
1484                 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1485                 } else {
1486                         switch (cmsg->B1protocol) {
1487                         case 0:
1488                                 bcs->proto2 = L2_HDLC;
1489                                 break;
1490                         case 1:
1491                                 bcs->proto2 = L2_VOICE;
1492                                 break;
1493                         default:
1494                                 dev_warn(cs->dev,
1495                         "B1 Protocol %u unsupported, using Transparent\n",
1496                                          cmsg->B1protocol);
1497                                 bcs->proto2 = L2_VOICE;
1498                         }
1499                         if (cmsg->B2protocol != 1)
1500                                 dev_warn(cs->dev,
1501                         "B2 Protocol %u unsupported, using Transparent\n",
1502                                          cmsg->B2protocol);
1503                         if (cmsg->B3protocol != 0)
1504                                 dev_warn(cs->dev,
1505                         "B3 Protocol %u unsupported, using Transparent\n",
1506                                          cmsg->B3protocol);
1507                         ignore_cstruct_param(cs, cmsg->B1configuration,
1508                                         "CONNECT_RESP", "B1 Configuration");
1509                         ignore_cstruct_param(cs, cmsg->B2configuration,
1510                                         "CONNECT_RESP", "B2 Configuration");
1511                         ignore_cstruct_param(cs, cmsg->B3configuration,
1512                                         "CONNECT_RESP", "B3 Configuration");
1513                 }
1514
1515                 /* ToDo: check/encode remaining parameters */
1516                 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
1517                                         "CONNECT_RESP", "Connected Number");
1518                 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
1519                                         "CONNECT_RESP", "Connected Subaddress");
1520                 ignore_cstruct_param(cs, cmsg->LLC,
1521                                         "CONNECT_RESP", "LLC");
1522                 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1523                         ignore_cstruct_param(cs, cmsg->BChannelinformation,
1524                                         "CONNECT_RESP", "BChannel Information");
1525                         ignore_cstruct_param(cs, cmsg->Keypadfacility,
1526                                         "CONNECT_RESP", "Keypad Facility");
1527                         ignore_cstruct_param(cs, cmsg->Useruserdata,
1528                                         "CONNECT_RESP", "User-User Data");
1529                         ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1530                                         "CONNECT_RESP", "Facility Data Array");
1531                 }
1532
1533                 /* Accept call */
1534                 if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1535                                        EV_ACCEPT, NULL, 0, NULL))
1536                         return;
1537                 gigaset_schedule_event(cs);
1538                 return;
1539
1540         case 1:                 /* Ignore */
1541                 /* send DISCONNECT_IND to this application */
1542                 send_disconnect_ind(bcs, ap, 0);
1543
1544                 /* remove it from the list of listening apps */
1545                 if (bcs->ap == ap) {
1546                         bcs->ap = ap->bcnext;
1547                         if (bcs->ap == NULL)
1548                                 /* last one: stop ev-layer hupD notifications */
1549                                 bcs->chstate &= ~CHS_NOTIFY_LL;
1550                         return;
1551                 }
1552                 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1553                         if (oap->bcnext == ap) {
1554                                 oap->bcnext = oap->bcnext->bcnext;
1555                                 return;
1556                         }
1557                 }
1558                 dev_err(cs->dev, "%s: application %u not found\n",
1559                         __func__, ap->id);
1560                 return;
1561
1562         default:                /* Reject */
1563                 /* drop all competing applications, keep only this one */
1564                 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext)
1565                         if (oap != ap)
1566                                 send_disconnect_ind(bcs, oap,
1567                                         CapiCallGivenToOtherApplication);
1568                 ap->bcnext = NULL;
1569                 bcs->ap = ap;
1570
1571                 /* reject call - will trigger DISCONNECT_IND for this app */
1572                 dev_info(cs->dev, "%s: Reject=%x\n",
1573                          "CONNECT_RESP", cmsg->Reject);
1574                 if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1575                                        EV_HUP, NULL, 0, NULL))
1576                         return;
1577                 gigaset_schedule_event(cs);
1578                 return;
1579         }
1580 }
1581
1582 /*
1583  * process CONNECT_B3_REQ message
1584  * build NCCI and emit CONNECT_B3_CONF reply
1585  */
1586 static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1587                               struct gigaset_capi_appl *ap,
1588                               struct sk_buff *skb)
1589 {
1590         struct cardstate *cs = iif->ctr.driverdata;
1591         _cmsg *cmsg = &iif->acmsg;
1592         int channel;
1593
1594         /* decode message */
1595         capi_message2cmsg(cmsg, skb->data);
1596         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1597
1598         /* extract and check channel number from PLCI */
1599         channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1600         if (!channel || channel > cs->channels) {
1601                 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1602                            "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
1603                 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1604                 return;
1605         }
1606
1607         /* mark logical connection active */
1608         ap->connected = APCONN_ACTIVE;
1609
1610         /* build NCCI: always 1 (one B3 connection only) */
1611         cmsg->adr.adrNCCI |= 1 << 16;
1612
1613         /* NCPI parameter: not applicable for B3 Transparent */
1614         ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
1615         send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
1616                                 CapiNcpiNotSupportedByProtocol : CapiSuccess);
1617 }
1618
1619 /*
1620  * process CONNECT_B3_RESP message
1621  * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1622  * or queue EV_HUP and emit DISCONNECT_B3_IND.
1623  * The emitted message is always shorter than the received one,
1624  * allowing to reuse the skb.
1625  */
1626 static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1627                                struct gigaset_capi_appl *ap,
1628                                struct sk_buff *skb)
1629 {
1630         struct cardstate *cs = iif->ctr.driverdata;
1631         _cmsg *cmsg = &iif->acmsg;
1632         struct bc_state *bcs;
1633         int channel;
1634         unsigned int msgsize;
1635         u8 command;
1636
1637         /* decode message */
1638         capi_message2cmsg(cmsg, skb->data);
1639         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1640
1641         /* extract and check channel number and NCCI */
1642         channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
1643         if (!channel || channel > cs->channels ||
1644             ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
1645                 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1646                            "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
1647                 dev_kfree_skb_any(skb);
1648                 return;
1649         }
1650         bcs = &cs->bcs[channel-1];
1651
1652         if (cmsg->Reject) {
1653                 /* Reject: clear B3 connect received flag */
1654                 ap->connected = APCONN_SETUP;
1655
1656                 /* trigger hangup, causing eventual DISCONNECT_IND */
1657                 if (!gigaset_add_event(cs, &bcs->at_state,
1658                                        EV_HUP, NULL, 0, NULL)) {
1659                         dev_kfree_skb_any(skb);
1660                         return;
1661                 }
1662                 gigaset_schedule_event(cs);
1663
1664                 /* emit DISCONNECT_B3_IND */
1665                 command = CAPI_DISCONNECT_B3;
1666                 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1667         } else {
1668                 /*
1669                  * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1670                  * we only send CONNECT_B3_IND if the B channel is up
1671                  */
1672                 command = CAPI_CONNECT_B3_ACTIVE;
1673                 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1674         }
1675         capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1676                          ap->nextMessageNumber++, cmsg->adr.adrNCCI);
1677         __skb_trim(skb, msgsize);
1678         capi_cmsg2message(cmsg, skb->data);
1679         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1680         capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1681 }
1682
1683 /*
1684  * process DISCONNECT_REQ message
1685  * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1686  * emit DISCONNECT_CONF reply
1687  */
1688 static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1689                               struct gigaset_capi_appl *ap,
1690                               struct sk_buff *skb)
1691 {
1692         struct cardstate *cs = iif->ctr.driverdata;
1693         _cmsg *cmsg = &iif->acmsg;
1694         struct bc_state *bcs;
1695         _cmsg *b3cmsg;
1696         struct sk_buff *b3skb;
1697         int channel;
1698
1699         /* decode message */
1700         capi_message2cmsg(cmsg, skb->data);
1701         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1702
1703         /* extract and check channel number from PLCI */
1704         channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1705         if (!channel || channel > cs->channels) {
1706                 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1707                            "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
1708                 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1709                 return;
1710         }
1711         bcs = cs->bcs + channel - 1;
1712
1713         /* ToDo: process parameter: Additional info */
1714         if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1715                 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1716                                      "DISCONNECT_REQ", "B Channel Information");
1717                 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1718                                      "DISCONNECT_REQ", "Keypad Facility");
1719                 ignore_cstruct_param(cs, cmsg->Useruserdata,
1720                                      "DISCONNECT_REQ", "User-User Data");
1721                 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1722                                      "DISCONNECT_REQ", "Facility Data Array");
1723         }
1724
1725         /* skip if DISCONNECT_IND already sent */
1726         if (!ap->connected)
1727                 return;
1728
1729         /* check for active logical connection */
1730         if (ap->connected >= APCONN_ACTIVE) {
1731                 /*
1732                  * emit DISCONNECT_B3_IND with cause 0x3301
1733                  * use separate cmsg structure, as the content of iif->acmsg
1734                  * is still needed for creating the _CONF message
1735                  */
1736                 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1737                 if (!b3cmsg) {
1738                         dev_err(cs->dev, "%s: out of memory\n", __func__);
1739                         send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1740                         return;
1741                 }
1742                 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1743                                  ap->nextMessageNumber++,
1744                                  cmsg->adr.adrPLCI | (1 << 16));
1745                 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1746                 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1747                 if (b3skb == NULL) {
1748                         dev_err(cs->dev, "%s: out of memory\n", __func__);
1749                         send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1750                         return;
1751                 }
1752                 capi_cmsg2message(b3cmsg,
1753                         __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN));
1754                 kfree(b3cmsg);
1755                 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1756         }
1757
1758         /* trigger hangup, causing eventual DISCONNECT_IND */
1759         if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
1760                 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1761                 return;
1762         }
1763         gigaset_schedule_event(cs);
1764
1765         /* emit reply */
1766         send_conf(iif, ap, skb, CapiSuccess);
1767 }
1768
1769 /*
1770  * process DISCONNECT_B3_REQ message
1771  * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
1772  */
1773 static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
1774                                  struct gigaset_capi_appl *ap,
1775                                  struct sk_buff *skb)
1776 {
1777         struct cardstate *cs = iif->ctr.driverdata;
1778         _cmsg *cmsg = &iif->acmsg;
1779         int channel;
1780
1781         /* decode message */
1782         capi_message2cmsg(cmsg, skb->data);
1783         dump_cmsg(DEBUG_CMD, __func__, cmsg);
1784
1785         /* extract and check channel number and NCCI */
1786         channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
1787         if (!channel || channel > cs->channels ||
1788             ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
1789                 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1790                            "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
1791                 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1792                 return;
1793         }
1794
1795         /* reject if logical connection not active */
1796         if (ap->connected < APCONN_ACTIVE) {
1797                 send_conf(iif, ap, skb,
1798                           CapiMessageNotSupportedInCurrentState);
1799                 return;
1800         }
1801
1802         /* trigger hangup, causing eventual DISCONNECT_B3_IND */
1803         if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1804                                EV_HUP, NULL, 0, NULL)) {
1805                 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1806                 return;
1807         }
1808         gigaset_schedule_event(cs);
1809
1810         /* NCPI parameter: not applicable for B3 Transparent */
1811         ignore_cstruct_param(cs, cmsg->NCPI,
1812                                 "DISCONNECT_B3_REQ", "NCPI");
1813         send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
1814                                 CapiNcpiNotSupportedByProtocol : CapiSuccess);
1815 }
1816
1817 /*
1818  * process DATA_B3_REQ message
1819  */
1820 static void do_data_b3_req(struct gigaset_capi_ctr *iif,
1821                            struct gigaset_capi_appl *ap,
1822                            struct sk_buff *skb)
1823 {
1824         struct cardstate *cs = iif->ctr.driverdata;
1825         int channel = CAPIMSG_PLCI_PART(skb->data);
1826         u16 ncci = CAPIMSG_NCCI_PART(skb->data);
1827         u16 msglen = CAPIMSG_LEN(skb->data);
1828         u16 datalen = CAPIMSG_DATALEN(skb->data);
1829         u16 flags = CAPIMSG_FLAGS(skb->data);
1830         u16 msgid = CAPIMSG_MSGID(skb->data);
1831         u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
1832
1833         /* frequent message, avoid _cmsg overhead */
1834         dump_rawmsg(DEBUG_LLDATA, "DATA_B3_REQ", skb->data);
1835
1836         gig_dbg(DEBUG_LLDATA,
1837                 "Receiving data from LL (ch: %d, flg: %x, sz: %d|%d)",
1838                 channel, flags, msglen, datalen);
1839
1840         /* check parameters */
1841         if (channel == 0 || channel > cs->channels || ncci != 1) {
1842                 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1843                            "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
1844                 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1845                 return;
1846         }
1847         if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
1848                 dev_notice(cs->dev, "%s: unexpected length %d\n",
1849                            "DATA_B3_REQ", msglen);
1850         if (msglen + datalen != skb->len)
1851                 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
1852                            "DATA_B3_REQ", msglen, datalen, skb->len);
1853         if (msglen + datalen > skb->len) {
1854                 /* message too short for announced data length */
1855                 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
1856                 return;
1857         }
1858         if (flags & CAPI_FLAGS_RESERVED) {
1859                 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
1860                            "DATA_B3_REQ", flags);
1861                 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1862                 return;
1863         }
1864
1865         /* reject if logical connection not active */
1866         if (ap->connected < APCONN_ACTIVE) {
1867                 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
1868                 return;
1869         }
1870
1871         /* pull CAPI message into link layer header */
1872         skb_reset_mac_header(skb);
1873         skb->mac_len = msglen;
1874         skb_pull(skb, msglen);
1875
1876         /* pass to device-specific module */
1877         if (cs->ops->send_skb(&cs->bcs[channel-1], skb) < 0) {
1878                 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1879                 return;
1880         }
1881
1882         /*
1883          * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
1884          * confirmation" bit is set; otherwise we have to send it now
1885          */
1886         if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
1887                 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
1888                                   flags ? CapiFlagsNotSupportedByProtocol
1889                                         : CAPI_NOERROR);
1890 }
1891
1892 /*
1893  * process RESET_B3_REQ message
1894  * just always reply "not supported by current protocol"
1895  */
1896 static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
1897                             struct gigaset_capi_appl *ap,
1898                             struct sk_buff *skb)
1899 {
1900         /* decode message */
1901         capi_message2cmsg(&iif->acmsg, skb->data);
1902         dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1903         send_conf(iif, ap, skb,
1904                   CapiResetProcedureNotSupportedByCurrentProtocol);
1905 }
1906
1907 /*
1908  * dump unsupported/ignored messages at most twice per minute,
1909  * some apps send those very frequently
1910  */
1911 static unsigned long ignored_msg_dump_time;
1912
1913 /*
1914  * unsupported CAPI message handler
1915  */
1916 static void do_unsupported(struct gigaset_capi_ctr *iif,
1917                            struct gigaset_capi_appl *ap,
1918                            struct sk_buff *skb)
1919 {
1920         /* decode message */
1921         capi_message2cmsg(&iif->acmsg, skb->data);
1922         if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000))
1923                 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1924         send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
1925 }
1926
1927 /*
1928  * CAPI message handler: no-op
1929  */
1930 static void do_nothing(struct gigaset_capi_ctr *iif,
1931                        struct gigaset_capi_appl *ap,
1932                        struct sk_buff *skb)
1933 {
1934         if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000)) {
1935                 /* decode message */
1936                 capi_message2cmsg(&iif->acmsg, skb->data);
1937                 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1938         }
1939         dev_kfree_skb_any(skb);
1940 }
1941
1942 static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
1943                             struct gigaset_capi_appl *ap,
1944                             struct sk_buff *skb)
1945 {
1946         dump_rawmsg(DEBUG_LLDATA, __func__, skb->data);
1947         dev_kfree_skb_any(skb);
1948 }
1949
1950 /* table of outgoing CAPI message handlers with lookup function */
1951 typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
1952                                     struct gigaset_capi_appl *,
1953                                     struct sk_buff *);
1954
1955 static struct {
1956         u16 cmd;
1957         capi_send_handler_t handler;
1958 } capi_send_handler_table[] = {
1959         /* most frequent messages first for faster lookup */
1960         { CAPI_DATA_B3_REQ, do_data_b3_req },
1961         { CAPI_DATA_B3_RESP, do_data_b3_resp },
1962
1963         { CAPI_ALERT_REQ, do_alert_req },
1964         { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
1965         { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
1966         { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
1967         { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
1968         { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
1969         { CAPI_CONNECT_REQ, do_connect_req },
1970         { CAPI_CONNECT_RESP, do_connect_resp },
1971         { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
1972         { CAPI_DISCONNECT_B3_RESP, do_nothing },
1973         { CAPI_DISCONNECT_REQ, do_disconnect_req },
1974         { CAPI_DISCONNECT_RESP, do_nothing },
1975         { CAPI_FACILITY_REQ, do_facility_req },
1976         { CAPI_FACILITY_RESP, do_nothing },
1977         { CAPI_LISTEN_REQ, do_listen_req },
1978         { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
1979         { CAPI_RESET_B3_REQ, do_reset_b3_req },
1980         { CAPI_RESET_B3_RESP, do_nothing },
1981
1982         /*
1983          * ToDo: support overlap sending (requires ev-layer state
1984          * machine extension to generate additional ATD commands)
1985          */
1986         { CAPI_INFO_REQ, do_unsupported },
1987         { CAPI_INFO_RESP, do_nothing },
1988
1989         /*
1990          * ToDo: what's the proper response for these?
1991          */
1992         { CAPI_MANUFACTURER_REQ, do_nothing },
1993         { CAPI_MANUFACTURER_RESP, do_nothing },
1994 };
1995
1996 /* look up handler */
1997 static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
1998 {
1999         size_t i;
2000
2001         for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2002                 if (capi_send_handler_table[i].cmd == cmd)
2003                         return capi_send_handler_table[i].handler;
2004         return NULL;
2005 }
2006
2007
2008 /**
2009  * gigaset_send_message() - accept a CAPI message from an application
2010  * @ctr:        controller descriptor structure.
2011  * @skb:        CAPI message.
2012  *
2013  * Return value: CAPI error code
2014  * Note: capidrv (and probably others, too) only uses the return value to
2015  * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2016  */
2017 static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2018 {
2019         struct gigaset_capi_ctr *iif
2020                 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2021         struct cardstate *cs = ctr->driverdata;
2022         struct gigaset_capi_appl *ap;
2023         capi_send_handler_t handler;
2024
2025         /* can only handle linear sk_buffs */
2026         if (skb_linearize(skb) < 0) {
2027                 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2028                 return CAPI_MSGOSRESOURCEERR;
2029         }
2030
2031         /* retrieve application data structure */
2032         ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2033         if (!ap) {
2034                 dev_notice(cs->dev, "%s: application %u not registered\n",
2035                            __func__, CAPIMSG_APPID(skb->data));
2036                 return CAPI_ILLAPPNR;
2037         }
2038
2039         /* look up command */
2040         handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2041         if (!handler) {
2042                 /* unknown/unsupported message type */
2043                 if (printk_ratelimit())
2044                         dev_notice(cs->dev, "%s: unsupported message %u\n",
2045                                    __func__, CAPIMSG_CMD(skb->data));
2046                 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2047         }
2048
2049         /* serialize */
2050         if (atomic_add_return(1, &iif->sendqlen) > 1) {
2051                 /* queue behind other messages */
2052                 skb_queue_tail(&iif->sendqueue, skb);
2053                 return CAPI_NOERROR;
2054         }
2055
2056         /* process message */
2057         handler(iif, ap, skb);
2058
2059         /* process other messages arrived in the meantime */
2060         while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2061                 skb = skb_dequeue(&iif->sendqueue);
2062                 if (!skb) {
2063                         /* should never happen */
2064                         dev_err(cs->dev, "%s: send queue empty\n", __func__);
2065                         continue;
2066                 }
2067                 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2068                 if (!ap) {
2069                         /* could that happen? */
2070                         dev_warn(cs->dev, "%s: application %u vanished\n",
2071                                  __func__, CAPIMSG_APPID(skb->data));
2072                         continue;
2073                 }
2074                 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2075                 if (!handler) {
2076                         /* should never happen */
2077                         dev_err(cs->dev, "%s: handler %x vanished\n",
2078                                 __func__, CAPIMSG_CMD(skb->data));
2079                         continue;
2080                 }
2081                 handler(iif, ap, skb);
2082         }
2083
2084         return CAPI_NOERROR;
2085 }
2086
2087 /**
2088  * gigaset_procinfo() - build single line description for controller
2089  * @ctr:        controller descriptor structure.
2090  *
2091  * Return value: pointer to generated string (null terminated)
2092  */
2093 static char *gigaset_procinfo(struct capi_ctr *ctr)
2094 {
2095         return ctr->name;       /* ToDo: more? */
2096 }
2097
2098 static int gigaset_proc_show(struct seq_file *m, void *v)
2099 {
2100         struct capi_ctr *ctr = m->private;
2101         struct cardstate *cs = ctr->driverdata;
2102         char *s;
2103         int i;
2104
2105         seq_printf(m, "%-16s %s\n", "name", ctr->name);
2106         seq_printf(m, "%-16s %s %s\n", "dev",
2107                         dev_driver_string(cs->dev), dev_name(cs->dev));
2108         seq_printf(m, "%-16s %d\n", "id", cs->myid);
2109         if (cs->gotfwver)
2110                 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
2111                         cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
2112         seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2113         seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
2114
2115         switch (cs->mode) {
2116         case M_UNKNOWN:
2117                 s = "unknown";
2118                 break;
2119         case M_CONFIG:
2120                 s = "config";
2121                 break;
2122         case M_UNIMODEM:
2123                 s = "Unimodem";
2124                 break;
2125         case M_CID:
2126                 s = "CID";
2127                 break;
2128         default:
2129                 s = "??";
2130         }
2131         seq_printf(m, "%-16s %s\n", "mode", s);
2132
2133         switch (cs->mstate) {
2134         case MS_UNINITIALIZED:
2135                 s = "uninitialized";
2136                 break;
2137         case MS_INIT:
2138                 s = "init";
2139                 break;
2140         case MS_LOCKED:
2141                 s = "locked";
2142                 break;
2143         case MS_SHUTDOWN:
2144                 s = "shutdown";
2145                 break;
2146         case MS_RECOVER:
2147                 s = "recover";
2148                 break;
2149         case MS_READY:
2150                 s = "ready";
2151                 break;
2152         default:
2153                 s = "??";
2154         }
2155         seq_printf(m, "%-16s %s\n", "mstate", s);
2156
2157         seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2158         seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2159         seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2160         seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
2161
2162         for (i = 0; i < cs->channels; i++) {
2163                 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
2164                                 cs->bcs[i].corrupted);
2165                 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
2166                                 cs->bcs[i].trans_down);
2167                 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
2168                                 cs->bcs[i].trans_up);
2169                 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
2170                                 cs->bcs[i].chstate);
2171                 switch (cs->bcs[i].proto2) {
2172                 case L2_BITSYNC:
2173                         s = "bitsync";
2174                         break;
2175                 case L2_HDLC:
2176                         s = "HDLC";
2177                         break;
2178                 case L2_VOICE:
2179                         s = "voice";
2180                         break;
2181                 default:
2182                         s = "??";
2183                 }
2184                 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
2185         }
2186         return 0;
2187 }
2188
2189 static int gigaset_proc_open(struct inode *inode, struct file *file)
2190 {
2191         return single_open(file, gigaset_proc_show, PDE(inode)->data);
2192 }
2193
2194 static const struct file_operations gigaset_proc_fops = {
2195         .owner          = THIS_MODULE,
2196         .open           = gigaset_proc_open,
2197         .read           = seq_read,
2198         .llseek         = seq_lseek,
2199         .release        = single_release,
2200 };
2201
2202 /**
2203  * gigaset_isdn_regdev() - register device to LL
2204  * @cs:         device descriptor structure.
2205  * @isdnid:     device name.
2206  *
2207  * Return value: 1 for success, 0 for failure
2208  */
2209 int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
2210 {
2211         struct gigaset_capi_ctr *iif;
2212         int rc;
2213
2214         iif = kmalloc(sizeof(*iif), GFP_KERNEL);
2215         if (!iif) {
2216                 pr_err("%s: out of memory\n", __func__);
2217                 return 0;
2218         }
2219
2220         /* prepare controller structure */
2221         iif->ctr.owner         = THIS_MODULE;
2222         iif->ctr.driverdata    = cs;
2223         strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name));
2224         iif->ctr.driver_name   = "gigaset";
2225         iif->ctr.load_firmware = NULL;
2226         iif->ctr.reset_ctr     = NULL;
2227         iif->ctr.register_appl = gigaset_register_appl;
2228         iif->ctr.release_appl  = gigaset_release_appl;
2229         iif->ctr.send_message  = gigaset_send_message;
2230         iif->ctr.procinfo      = gigaset_procinfo;
2231         iif->ctr.proc_fops = &gigaset_proc_fops;
2232         INIT_LIST_HEAD(&iif->appls);
2233         skb_queue_head_init(&iif->sendqueue);
2234         atomic_set(&iif->sendqlen, 0);
2235
2236         /* register controller with CAPI */
2237         rc = attach_capi_ctr(&iif->ctr);
2238         if (rc) {
2239                 pr_err("attach_capi_ctr failed (%d)\n", rc);
2240                 kfree(iif);
2241                 return 0;
2242         }
2243
2244         cs->iif = iif;
2245         cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
2246         return 1;
2247 }
2248
2249 /**
2250  * gigaset_isdn_unregdev() - unregister device from LL
2251  * @cs:         device descriptor structure.
2252  */
2253 void gigaset_isdn_unregdev(struct cardstate *cs)
2254 {
2255         struct gigaset_capi_ctr *iif = cs->iif;
2256
2257         detach_capi_ctr(&iif->ctr);
2258         kfree(iif);
2259         cs->iif = NULL;
2260 }
2261
2262 static struct capi_driver capi_driver_gigaset = {
2263         .name           = "gigaset",
2264         .revision       = "1.0",
2265 };
2266
2267 /**
2268  * gigaset_isdn_regdrv() - register driver to LL
2269  */
2270 void gigaset_isdn_regdrv(void)
2271 {
2272         pr_info("Kernel CAPI interface\n");
2273         register_capi_driver(&capi_driver_gigaset);
2274 }
2275
2276 /**
2277  * gigaset_isdn_unregdrv() - unregister driver from LL
2278  */
2279 void gigaset_isdn_unregdrv(void)
2280 {
2281         unregister_capi_driver(&capi_driver_gigaset);
2282 }