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