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