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