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