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