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