isdn/gigaset: encode HLC and BC together
[linux/fpc-iii.git] / drivers / isdn / gigaset / capi.c
blob6820828457ee203321f2450820eca68495a26ff6
1 /*
2 * Kernel CAPI interface for the Gigaset driver
4 * Copyright (c) 2009 by Tilman Schmidt <tilman@imap.cc>.
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 * =====================================================================
14 #include "gigaset.h"
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/isdn/capilli.h>
18 #include <linux/isdn/capicmd.h>
19 #include <linux/isdn/capiutil.h>
21 /* missing from kernelcapi.h */
22 #define CapiNcpiNotSupportedByProtocol 0x0001
23 #define CapiFlagsNotSupportedByProtocol 0x0002
24 #define CapiAlertAlreadySent 0x0003
25 #define CapiFacilitySpecificFunctionNotSupported 0x3011
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)
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
47 #define CAPI_SUPPSVC_GETSUPPORTED 0x0000
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)
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)
63 /* Flags (DATA_B3_REQ/_IND) */
64 #define CAPI_FLAGS_DELIVERY_CONFIRMATION 0x04
65 #define CAPI_FLAGS_RESERVED (~0x1f)
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
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 */
78 /* registered application data structure */
79 struct gigaset_capi_appl {
80 struct list_head ctrlist;
81 struct gigaset_capi_appl *bcnext;
82 u16 id;
83 struct capi_register_params rp;
84 u16 nextMessageNumber;
85 u32 listenInfoMask;
86 u32 listenCIPmask;
87 int connected;
90 /* CAPI specific controller data structure */
91 struct 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];
105 /* CIP Value table (from CAPI 2.0 standard, ch. 6.1) */
106 static 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 */
158 * helper functions
159 * ================
163 * emit unsupported parameter warning
165 static inline void ignore_cstruct_param(struct cardstate *cs, _cstruct param,
166 char *msgname, char *paramname)
168 if (param && *param)
169 dev_warn(cs->dev, "%s: ignoring unsupported parameter: %s\n",
170 msgname, paramname);
174 * convert hex to binary
176 static inline u8 hex2bin(char c)
178 int result = c & 0x0f;
179 if (c & 0x40)
180 result += 9;
181 return result;
185 * convert an IE from Gigaset hex string to ETSI binary representation
186 * including length byte
187 * return value: result length, -1 on error
189 static int encode_ie(char *in, u8 *out, int maxlen)
191 int l = 0;
192 while (*in) {
193 if (!isxdigit(in[0]) || !isxdigit(in[1]) || l >= maxlen)
194 return -1;
195 out[++l] = (hex2bin(in[0]) << 4) + hex2bin(in[1]);
196 in += 2;
198 out[0] = l;
199 return l;
203 * convert an IE from ETSI binary representation including length byte
204 * to Gigaset hex string
206 static void decode_ie(u8 *in, char *out)
208 int i = *in;
209 while (i-- > 0) {
210 /* ToDo: conversion to upper case necessary? */
211 *out++ = toupper(hex_asc_hi(*++in));
212 *out++ = toupper(hex_asc_lo(*in));
217 * retrieve application data structure for an application ID
219 static inline struct gigaset_capi_appl *
220 get_appl(struct gigaset_capi_ctr *iif, u16 appl)
222 struct gigaset_capi_appl *ap;
224 list_for_each_entry(ap, &iif->appls, ctrlist)
225 if (ap->id == appl)
226 return ap;
227 return NULL;
231 * dump CAPI message to kernel messages for debugging
233 static inline void dump_cmsg(enum debuglevel level, const char *tag, _cmsg *p)
235 #ifdef CONFIG_GIGASET_DEBUG
236 _cdebbuf *cdb;
238 if (!(gigaset_debuglevel & level))
239 return;
241 cdb = capi_cmsg2str(p);
242 if (cdb) {
243 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId, cdb->buf);
244 cdebbuf_free(cdb);
245 } else {
246 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId,
247 capi_cmd2str(p->Command, p->Subcommand));
249 #endif
252 static inline void dump_rawmsg(enum debuglevel level, const char *tag,
253 unsigned char *data)
255 #ifdef CONFIG_GIGASET_DEBUG
256 char *dbgline;
257 int i, l;
259 if (!(gigaset_debuglevel & level))
260 return;
262 l = CAPIMSG_LEN(data);
263 if (l < 12) {
264 gig_dbg(level, "%s: ??? LEN=%04d", tag, l);
265 return;
267 gig_dbg(level, "%s: 0x%02x:0x%02x: ID=%03d #0x%04x LEN=%04d NCCI=0x%x",
268 tag, CAPIMSG_COMMAND(data), CAPIMSG_SUBCOMMAND(data),
269 CAPIMSG_APPID(data), CAPIMSG_MSGID(data), l,
270 CAPIMSG_CONTROL(data));
271 l -= 12;
272 dbgline = kmalloc(3*l, GFP_ATOMIC);
273 if (!dbgline)
274 return;
275 for (i = 0; i < l; i++) {
276 dbgline[3*i] = hex_asc_hi(data[12+i]);
277 dbgline[3*i+1] = hex_asc_lo(data[12+i]);
278 dbgline[3*i+2] = ' ';
280 dbgline[3*l-1] = '\0';
281 gig_dbg(level, " %s", dbgline);
282 kfree(dbgline);
283 if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
284 (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
285 CAPIMSG_SUBCOMMAND(data) == CAPI_IND) &&
286 CAPIMSG_DATALEN(data) > 0) {
287 l = CAPIMSG_DATALEN(data);
288 dbgline = kmalloc(3*l, GFP_ATOMIC);
289 if (!dbgline)
290 return;
291 data += CAPIMSG_LEN(data);
292 for (i = 0; i < l; i++) {
293 dbgline[3*i] = hex_asc_hi(data[i]);
294 dbgline[3*i+1] = hex_asc_lo(data[i]);
295 dbgline[3*i+2] = ' ';
297 dbgline[3*l-1] = '\0';
298 gig_dbg(level, " %s", dbgline);
299 kfree(dbgline);
301 #endif
305 * format CAPI IE as string
308 static const char *format_ie(const char *ie)
310 static char result[3*MAX_FMT_IE_LEN];
311 int len, count;
312 char *pout = result;
314 if (!ie)
315 return "NULL";
317 count = len = ie[0];
318 if (count > MAX_FMT_IE_LEN)
319 count = MAX_FMT_IE_LEN-1;
320 while (count--) {
321 *pout++ = hex_asc_hi(*++ie);
322 *pout++ = hex_asc_lo(*ie);
323 *pout++ = ' ';
325 if (len > MAX_FMT_IE_LEN) {
326 *pout++ = '.';
327 *pout++ = '.';
328 *pout++ = '.';
330 *--pout = 0;
331 return result;
335 * emit DATA_B3_CONF message
337 static void send_data_b3_conf(struct cardstate *cs, struct capi_ctr *ctr,
338 u16 appl, u16 msgid, int channel,
339 u16 handle, u16 info)
341 struct sk_buff *cskb;
342 u8 *msg;
344 cskb = alloc_skb(CAPI_DATA_B3_CONF_LEN, GFP_ATOMIC);
345 if (!cskb) {
346 dev_err(cs->dev, "%s: out of memory\n", __func__);
347 return;
349 /* frequent message, avoid _cmsg overhead */
350 msg = __skb_put(cskb, CAPI_DATA_B3_CONF_LEN);
351 CAPIMSG_SETLEN(msg, CAPI_DATA_B3_CONF_LEN);
352 CAPIMSG_SETAPPID(msg, appl);
353 CAPIMSG_SETCOMMAND(msg, CAPI_DATA_B3);
354 CAPIMSG_SETSUBCOMMAND(msg, CAPI_CONF);
355 CAPIMSG_SETMSGID(msg, msgid);
356 CAPIMSG_SETCONTROLLER(msg, ctr->cnr);
357 CAPIMSG_SETPLCI_PART(msg, channel);
358 CAPIMSG_SETNCCI_PART(msg, 1);
359 CAPIMSG_SETHANDLE_CONF(msg, handle);
360 CAPIMSG_SETINFO_CONF(msg, info);
362 /* emit message */
363 dump_rawmsg(DEBUG_MCMD, __func__, msg);
364 capi_ctr_handle_message(ctr, appl, cskb);
369 * driver interface functions
370 * ==========================
374 * gigaset_skb_sent() - acknowledge transmission of outgoing skb
375 * @bcs: B channel descriptor structure.
376 * @skb: sent data.
378 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
379 * skb has been successfully sent, for signalling completion to the LL.
381 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *dskb)
383 struct cardstate *cs = bcs->cs;
384 struct gigaset_capi_ctr *iif = cs->iif;
385 struct gigaset_capi_appl *ap = bcs->ap;
386 unsigned char *req = skb_mac_header(dskb);
387 u16 flags;
389 /* update statistics */
390 ++bcs->trans_up;
392 if (!ap) {
393 dev_err(cs->dev, "%s: no application\n", __func__);
394 return;
397 /* don't send further B3 messages if disconnected */
398 if (ap->connected < APCONN_ACTIVE) {
399 gig_dbg(DEBUG_LLDATA, "disconnected, discarding ack");
400 return;
404 * send DATA_B3_CONF if "delivery confirmation" bit was set in request;
405 * otherwise it has already been sent by do_data_b3_req()
407 flags = CAPIMSG_FLAGS(req);
408 if (flags & CAPI_FLAGS_DELIVERY_CONFIRMATION)
409 send_data_b3_conf(cs, &iif->ctr, ap->id, CAPIMSG_MSGID(req),
410 bcs->channel + 1, CAPIMSG_HANDLE_REQ(req),
411 (flags & ~CAPI_FLAGS_DELIVERY_CONFIRMATION) ?
412 CapiFlagsNotSupportedByProtocol :
413 CAPI_NOERROR);
415 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
418 * gigaset_skb_rcvd() - pass received skb to LL
419 * @bcs: B channel descriptor structure.
420 * @skb: received data.
422 * Called by hardware module {bas,ser,usb}_gigaset when user data has
423 * been successfully received, for passing to the LL.
424 * Warning: skb must not be accessed anymore!
426 void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
428 struct cardstate *cs = bcs->cs;
429 struct gigaset_capi_ctr *iif = cs->iif;
430 struct gigaset_capi_appl *ap = bcs->ap;
431 int len = skb->len;
433 /* update statistics */
434 bcs->trans_down++;
436 if (!ap) {
437 dev_err(cs->dev, "%s: no application\n", __func__);
438 return;
441 /* don't send further B3 messages if disconnected */
442 if (ap->connected < APCONN_ACTIVE) {
443 gig_dbg(DEBUG_LLDATA, "disconnected, discarding data");
444 dev_kfree_skb_any(skb);
445 return;
449 * prepend DATA_B3_IND message to payload
450 * Parameters: NCCI = 1, all others 0/unused
451 * frequent message, avoid _cmsg overhead
453 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
454 CAPIMSG_SETLEN(skb->data, CAPI_DATA_B3_REQ_LEN);
455 CAPIMSG_SETAPPID(skb->data, ap->id);
456 CAPIMSG_SETCOMMAND(skb->data, CAPI_DATA_B3);
457 CAPIMSG_SETSUBCOMMAND(skb->data, CAPI_IND);
458 CAPIMSG_SETMSGID(skb->data, ap->nextMessageNumber++);
459 CAPIMSG_SETCONTROLLER(skb->data, iif->ctr.cnr);
460 CAPIMSG_SETPLCI_PART(skb->data, bcs->channel + 1);
461 CAPIMSG_SETNCCI_PART(skb->data, 1);
462 /* Data parameter not used */
463 CAPIMSG_SETDATALEN(skb->data, len);
464 /* Data handle parameter not used */
465 CAPIMSG_SETFLAGS(skb->data, 0);
466 /* Data64 parameter not present */
468 /* emit message */
469 dump_rawmsg(DEBUG_LLDATA, "DATA_B3_IND", skb->data);
470 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
472 EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
475 * gigaset_isdn_rcv_err() - signal receive error
476 * @bcs: B channel descriptor structure.
478 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
479 * has occurred, for signalling to the LL.
481 void gigaset_isdn_rcv_err(struct bc_state *bcs)
483 /* if currently ignoring packets, just count down */
484 if (bcs->ignore) {
485 bcs->ignore--;
486 return;
489 /* update statistics */
490 bcs->corrupted++;
492 /* ToDo: signal error -> LL */
494 EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
497 * gigaset_isdn_icall() - signal incoming call
498 * @at_state: connection state structure.
500 * Called by main module at tasklet level to notify the LL that an incoming
501 * call has been received. @at_state contains the parameters of the call.
503 * Return value: call disposition (ICALL_*)
505 int gigaset_isdn_icall(struct at_state_t *at_state)
507 struct cardstate *cs = at_state->cs;
508 struct bc_state *bcs = at_state->bcs;
509 struct gigaset_capi_ctr *iif = cs->iif;
510 struct gigaset_capi_appl *ap;
511 u32 actCIPmask;
512 struct sk_buff *skb;
513 unsigned int msgsize;
514 int i;
517 * ToDo: signal calls without a free B channel, too
518 * (requires a u8 handle for the at_state structure that can
519 * be stored in the PLCI and used in the CONNECT_RESP message
520 * handler to retrieve it)
522 if (!bcs)
523 return ICALL_IGNORE;
525 /* prepare CONNECT_IND message, using B channel number as PLCI */
526 capi_cmsg_header(&iif->hcmsg, 0, CAPI_CONNECT, CAPI_IND, 0,
527 iif->ctr.cnr | ((bcs->channel + 1) << 8));
529 /* minimum size, all structs empty */
530 msgsize = CAPI_CONNECT_IND_BASELEN;
532 /* Bearer Capability (mandatory) */
533 if (at_state->str_var[STR_ZBC]) {
534 /* pass on BC from Gigaset */
535 if (encode_ie(at_state->str_var[STR_ZBC], iif->bc_buf,
536 MAX_BC_OCTETS) < 0) {
537 dev_warn(cs->dev, "RING ignored - bad BC %s\n",
538 at_state->str_var[STR_ZBC]);
539 return ICALL_IGNORE;
542 /* look up corresponding CIP value */
543 iif->hcmsg.CIPValue = 0; /* default if nothing found */
544 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
545 if (cip2bchlc[i].bc != NULL &&
546 cip2bchlc[i].hlc == NULL &&
547 !strcmp(cip2bchlc[i].bc,
548 at_state->str_var[STR_ZBC])) {
549 iif->hcmsg.CIPValue = i;
550 break;
552 } else {
553 /* no BC (internal call): assume CIP 1 (speech, A-law) */
554 iif->hcmsg.CIPValue = 1;
555 encode_ie(cip2bchlc[1].bc, iif->bc_buf, MAX_BC_OCTETS);
557 iif->hcmsg.BC = iif->bc_buf;
558 msgsize += iif->hcmsg.BC[0];
560 /* High Layer Compatibility (optional) */
561 if (at_state->str_var[STR_ZHLC]) {
562 /* pass on HLC from Gigaset */
563 if (encode_ie(at_state->str_var[STR_ZHLC], iif->hlc_buf,
564 MAX_HLC_OCTETS) < 0) {
565 dev_warn(cs->dev, "RING ignored - bad HLC %s\n",
566 at_state->str_var[STR_ZHLC]);
567 return ICALL_IGNORE;
569 iif->hcmsg.HLC = iif->hlc_buf;
570 msgsize += iif->hcmsg.HLC[0];
572 /* look up corresponding CIP value */
573 /* keep BC based CIP value if none found */
574 if (at_state->str_var[STR_ZBC])
575 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
576 if (cip2bchlc[i].hlc != NULL &&
577 !strcmp(cip2bchlc[i].hlc,
578 at_state->str_var[STR_ZHLC]) &&
579 !strcmp(cip2bchlc[i].bc,
580 at_state->str_var[STR_ZBC])) {
581 iif->hcmsg.CIPValue = i;
582 break;
586 /* Called Party Number (optional) */
587 if (at_state->str_var[STR_ZCPN]) {
588 i = strlen(at_state->str_var[STR_ZCPN]);
589 if (i > MAX_NUMBER_DIGITS) {
590 dev_warn(cs->dev, "RING ignored - bad number %s\n",
591 at_state->str_var[STR_ZBC]);
592 return ICALL_IGNORE;
594 iif->cdpty_buf[0] = i + 1;
595 iif->cdpty_buf[1] = 0x80; /* type / numbering plan unknown */
596 memcpy(iif->cdpty_buf+2, at_state->str_var[STR_ZCPN], i);
597 iif->hcmsg.CalledPartyNumber = iif->cdpty_buf;
598 msgsize += iif->hcmsg.CalledPartyNumber[0];
601 /* Calling Party Number (optional) */
602 if (at_state->str_var[STR_NMBR]) {
603 i = strlen(at_state->str_var[STR_NMBR]);
604 if (i > MAX_NUMBER_DIGITS) {
605 dev_warn(cs->dev, "RING ignored - bad number %s\n",
606 at_state->str_var[STR_ZBC]);
607 return ICALL_IGNORE;
609 iif->cgpty_buf[0] = i + 2;
610 iif->cgpty_buf[1] = 0x00; /* type / numbering plan unknown */
611 iif->cgpty_buf[2] = 0x80; /* pres. allowed, not screened */
612 memcpy(iif->cgpty_buf+3, at_state->str_var[STR_NMBR], i);
613 iif->hcmsg.CallingPartyNumber = iif->cgpty_buf;
614 msgsize += iif->hcmsg.CallingPartyNumber[0];
617 /* remaining parameters (not supported, always left NULL):
618 * - CalledPartySubaddress
619 * - CallingPartySubaddress
620 * - AdditionalInfo
621 * - BChannelinformation
622 * - Keypadfacility
623 * - Useruserdata
624 * - Facilitydataarray
627 gig_dbg(DEBUG_CMD, "icall: PLCI %x CIP %d BC %s",
628 iif->hcmsg.adr.adrPLCI, iif->hcmsg.CIPValue,
629 format_ie(iif->hcmsg.BC));
630 gig_dbg(DEBUG_CMD, "icall: HLC %s",
631 format_ie(iif->hcmsg.HLC));
632 gig_dbg(DEBUG_CMD, "icall: CgPty %s",
633 format_ie(iif->hcmsg.CallingPartyNumber));
634 gig_dbg(DEBUG_CMD, "icall: CdPty %s",
635 format_ie(iif->hcmsg.CalledPartyNumber));
637 /* scan application list for matching listeners */
638 bcs->ap = NULL;
639 actCIPmask = 1 | (1 << iif->hcmsg.CIPValue);
640 list_for_each_entry(ap, &iif->appls, ctrlist)
641 if (actCIPmask & ap->listenCIPmask) {
642 /* build CONNECT_IND message for this application */
643 iif->hcmsg.ApplId = ap->id;
644 iif->hcmsg.Messagenumber = ap->nextMessageNumber++;
646 skb = alloc_skb(msgsize, GFP_ATOMIC);
647 if (!skb) {
648 dev_err(cs->dev, "%s: out of memory\n",
649 __func__);
650 break;
652 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
653 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
655 /* add to listeners on this B channel, update state */
656 ap->bcnext = bcs->ap;
657 bcs->ap = ap;
658 bcs->chstate |= CHS_NOTIFY_LL;
659 ap->connected = APCONN_SETUP;
661 /* emit message */
662 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
666 * Return "accept" if any listeners.
667 * Gigaset will send ALERTING.
668 * There doesn't seem to be a way to avoid this.
670 return bcs->ap ? ICALL_ACCEPT : ICALL_IGNORE;
674 * send a DISCONNECT_IND message to an application
675 * does not sleep, clobbers the controller's hcmsg structure
677 static void send_disconnect_ind(struct bc_state *bcs,
678 struct gigaset_capi_appl *ap, u16 reason)
680 struct cardstate *cs = bcs->cs;
681 struct gigaset_capi_ctr *iif = cs->iif;
682 struct sk_buff *skb;
684 if (ap->connected == APCONN_NONE)
685 return;
687 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT, CAPI_IND,
688 ap->nextMessageNumber++,
689 iif->ctr.cnr | ((bcs->channel + 1) << 8));
690 iif->hcmsg.Reason = reason;
691 skb = alloc_skb(CAPI_DISCONNECT_IND_LEN, GFP_ATOMIC);
692 if (!skb) {
693 dev_err(cs->dev, "%s: out of memory\n", __func__);
694 return;
696 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, CAPI_DISCONNECT_IND_LEN));
697 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
698 ap->connected = APCONN_NONE;
699 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
703 * send a DISCONNECT_B3_IND message to an application
704 * Parameters: NCCI = 1, NCPI empty, Reason_B3 = 0
705 * does not sleep, clobbers the controller's hcmsg structure
707 static void send_disconnect_b3_ind(struct bc_state *bcs,
708 struct gigaset_capi_appl *ap)
710 struct cardstate *cs = bcs->cs;
711 struct gigaset_capi_ctr *iif = cs->iif;
712 struct sk_buff *skb;
714 /* nothing to do if no logical connection active */
715 if (ap->connected < APCONN_ACTIVE)
716 return;
717 ap->connected = APCONN_SETUP;
719 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
720 ap->nextMessageNumber++,
721 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
722 skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_ATOMIC);
723 if (!skb) {
724 dev_err(cs->dev, "%s: out of memory\n", __func__);
725 return;
727 capi_cmsg2message(&iif->hcmsg,
728 __skb_put(skb, CAPI_DISCONNECT_B3_IND_BASELEN));
729 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
730 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
734 * gigaset_isdn_connD() - signal D channel connect
735 * @bcs: B channel descriptor structure.
737 * Called by main module at tasklet level to notify the LL that the D channel
738 * connection has been established.
740 void gigaset_isdn_connD(struct bc_state *bcs)
742 struct cardstate *cs = bcs->cs;
743 struct gigaset_capi_ctr *iif = cs->iif;
744 struct gigaset_capi_appl *ap = bcs->ap;
745 struct sk_buff *skb;
746 unsigned int msgsize;
748 if (!ap) {
749 dev_err(cs->dev, "%s: no application\n", __func__);
750 return;
752 while (ap->bcnext) {
753 /* this should never happen */
754 dev_warn(cs->dev, "%s: dropping extra application %u\n",
755 __func__, ap->bcnext->id);
756 send_disconnect_ind(bcs, ap->bcnext,
757 CapiCallGivenToOtherApplication);
758 ap->bcnext = ap->bcnext->bcnext;
760 if (ap->connected == APCONN_NONE) {
761 dev_warn(cs->dev, "%s: application %u not connected\n",
762 __func__, ap->id);
763 return;
766 /* prepare CONNECT_ACTIVE_IND message
767 * Note: LLC not supported by device
769 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_CONNECT_ACTIVE, CAPI_IND,
770 ap->nextMessageNumber++,
771 iif->ctr.cnr | ((bcs->channel + 1) << 8));
773 /* minimum size, all structs empty */
774 msgsize = CAPI_CONNECT_ACTIVE_IND_BASELEN;
776 /* ToDo: set parameter: Connected number
777 * (requires ev-layer state machine extension to collect
778 * ZCON device reply)
781 /* build and emit CONNECT_ACTIVE_IND message */
782 skb = alloc_skb(msgsize, GFP_ATOMIC);
783 if (!skb) {
784 dev_err(cs->dev, "%s: out of memory\n", __func__);
785 return;
787 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
788 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
789 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
793 * gigaset_isdn_hupD() - signal D channel hangup
794 * @bcs: B channel descriptor structure.
796 * Called by main module at tasklet level to notify the LL that the D channel
797 * connection has been shut down.
799 void gigaset_isdn_hupD(struct bc_state *bcs)
801 struct gigaset_capi_appl *ap;
804 * ToDo: pass on reason code reported by device
805 * (requires ev-layer state machine extension to collect
806 * ZCAU device reply)
808 for (ap = bcs->ap; ap != NULL; ap = ap->bcnext) {
809 send_disconnect_b3_ind(bcs, ap);
810 send_disconnect_ind(bcs, ap, 0);
812 bcs->ap = NULL;
816 * gigaset_isdn_connB() - signal B channel connect
817 * @bcs: B channel descriptor structure.
819 * Called by main module at tasklet level to notify the LL that the B channel
820 * connection has been established.
822 void gigaset_isdn_connB(struct bc_state *bcs)
824 struct cardstate *cs = bcs->cs;
825 struct gigaset_capi_ctr *iif = cs->iif;
826 struct gigaset_capi_appl *ap = bcs->ap;
827 struct sk_buff *skb;
828 unsigned int msgsize;
829 u8 command;
831 if (!ap) {
832 dev_err(cs->dev, "%s: no application\n", __func__);
833 return;
835 while (ap->bcnext) {
836 /* this should never happen */
837 dev_warn(cs->dev, "%s: dropping extra application %u\n",
838 __func__, ap->bcnext->id);
839 send_disconnect_ind(bcs, ap->bcnext,
840 CapiCallGivenToOtherApplication);
841 ap->bcnext = ap->bcnext->bcnext;
843 if (!ap->connected) {
844 dev_warn(cs->dev, "%s: application %u not connected\n",
845 __func__, ap->id);
846 return;
850 * emit CONNECT_B3_ACTIVE_IND if we already got CONNECT_B3_REQ;
851 * otherwise we have to emit CONNECT_B3_IND first, and follow up with
852 * CONNECT_B3_ACTIVE_IND in reply to CONNECT_B3_RESP
853 * Parameters in both cases always: NCCI = 1, NCPI empty
855 if (ap->connected >= APCONN_ACTIVE) {
856 command = CAPI_CONNECT_B3_ACTIVE;
857 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
858 } else {
859 command = CAPI_CONNECT_B3;
860 msgsize = CAPI_CONNECT_B3_IND_BASELEN;
862 capi_cmsg_header(&iif->hcmsg, ap->id, command, CAPI_IND,
863 ap->nextMessageNumber++,
864 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
865 skb = alloc_skb(msgsize, GFP_ATOMIC);
866 if (!skb) {
867 dev_err(cs->dev, "%s: out of memory\n", __func__);
868 return;
870 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
871 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
872 ap->connected = APCONN_ACTIVE;
873 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
877 * gigaset_isdn_hupB() - signal B channel hangup
878 * @bcs: B channel descriptor structure.
880 * Called by main module to notify the LL that the B channel connection has
881 * been shut down.
883 void gigaset_isdn_hupB(struct bc_state *bcs)
885 struct cardstate *cs = bcs->cs;
886 struct gigaset_capi_appl *ap = bcs->ap;
888 /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
890 if (!ap) {
891 dev_err(cs->dev, "%s: no application\n", __func__);
892 return;
895 send_disconnect_b3_ind(bcs, ap);
899 * gigaset_isdn_start() - signal device availability
900 * @cs: device descriptor structure.
902 * Called by main module to notify the LL that the device is available for
903 * use.
905 void gigaset_isdn_start(struct cardstate *cs)
907 struct gigaset_capi_ctr *iif = cs->iif;
909 /* fill profile data: manufacturer name */
910 strcpy(iif->ctr.manu, "Siemens");
911 /* CAPI and device version */
912 iif->ctr.version.majorversion = 2; /* CAPI 2.0 */
913 iif->ctr.version.minorversion = 0;
914 /* ToDo: check/assert cs->gotfwver? */
915 iif->ctr.version.majormanuversion = cs->fwver[0];
916 iif->ctr.version.minormanuversion = cs->fwver[1];
917 /* number of B channels supported */
918 iif->ctr.profile.nbchannel = cs->channels;
919 /* global options: internal controller, supplementary services */
920 iif->ctr.profile.goptions = 0x11;
921 /* B1 protocols: 64 kbit/s HDLC or transparent */
922 iif->ctr.profile.support1 = 0x03;
923 /* B2 protocols: transparent only */
924 /* ToDo: X.75 SLP ? */
925 iif->ctr.profile.support2 = 0x02;
926 /* B3 protocols: transparent only */
927 iif->ctr.profile.support3 = 0x01;
928 /* no serial number */
929 strcpy(iif->ctr.serial, "0");
930 capi_ctr_ready(&iif->ctr);
934 * gigaset_isdn_stop() - signal device unavailability
935 * @cs: device descriptor structure.
937 * Called by main module to notify the LL that the device is no longer
938 * available for use.
940 void gigaset_isdn_stop(struct cardstate *cs)
942 struct gigaset_capi_ctr *iif = cs->iif;
943 capi_ctr_down(&iif->ctr);
947 * kernel CAPI callback methods
948 * ============================
952 * register CAPI application
954 static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
955 capi_register_params *rp)
957 struct gigaset_capi_ctr *iif
958 = container_of(ctr, struct gigaset_capi_ctr, ctr);
959 struct cardstate *cs = ctr->driverdata;
960 struct gigaset_capi_appl *ap;
962 list_for_each_entry(ap, &iif->appls, ctrlist)
963 if (ap->id == appl) {
964 dev_notice(cs->dev,
965 "application %u already registered\n", appl);
966 return;
969 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
970 if (!ap) {
971 dev_err(cs->dev, "%s: out of memory\n", __func__);
972 return;
974 ap->id = appl;
975 ap->rp = *rp;
977 list_add(&ap->ctrlist, &iif->appls);
981 * release CAPI application
983 static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
985 struct gigaset_capi_ctr *iif
986 = container_of(ctr, struct gigaset_capi_ctr, ctr);
987 struct cardstate *cs = iif->ctr.driverdata;
988 struct gigaset_capi_appl *ap, *tmp;
990 list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
991 if (ap->id == appl) {
992 if (ap->connected != APCONN_NONE) {
993 dev_err(cs->dev,
994 "%s: application %u still connected\n",
995 __func__, ap->id);
996 /* ToDo: clear active connection */
998 list_del(&ap->ctrlist);
999 kfree(ap);
1005 * =====================================================================
1006 * outgoing CAPI message handler
1007 * =====================================================================
1011 * helper function: emit reply message with given Info value
1013 static void send_conf(struct gigaset_capi_ctr *iif,
1014 struct gigaset_capi_appl *ap,
1015 struct sk_buff *skb,
1016 u16 info)
1019 * _CONF replies always only have NCCI and Info parameters
1020 * so they'll fit into the _REQ message skb
1022 capi_cmsg_answer(&iif->acmsg);
1023 iif->acmsg.Info = info;
1024 capi_cmsg2message(&iif->acmsg, skb->data);
1025 __skb_trim(skb, CAPI_STDCONF_LEN);
1026 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1027 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1031 * process FACILITY_REQ message
1033 static void do_facility_req(struct gigaset_capi_ctr *iif,
1034 struct gigaset_capi_appl *ap,
1035 struct sk_buff *skb)
1037 struct cardstate *cs = iif->ctr.driverdata;
1038 _cmsg *cmsg = &iif->acmsg;
1039 struct sk_buff *cskb;
1040 u8 *pparam;
1041 unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
1042 u16 function, info;
1043 static u8 confparam[10]; /* max. 9 octets + length byte */
1045 /* decode message */
1046 capi_message2cmsg(cmsg, skb->data);
1047 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1050 * Facility Request Parameter is not decoded by capi_message2cmsg()
1051 * encoding depends on Facility Selector
1053 switch (cmsg->FacilitySelector) {
1054 case CAPI_FACILITY_DTMF: /* ToDo */
1055 info = CapiFacilityNotSupported;
1056 confparam[0] = 2; /* length */
1057 /* DTMF information: Unknown DTMF request */
1058 capimsg_setu16(confparam, 1, 2);
1059 break;
1061 case CAPI_FACILITY_V42BIS: /* not supported */
1062 info = CapiFacilityNotSupported;
1063 confparam[0] = 2; /* length */
1064 /* V.42 bis information: not available */
1065 capimsg_setu16(confparam, 1, 1);
1066 break;
1068 case CAPI_FACILITY_SUPPSVC:
1069 /* decode Function parameter */
1070 pparam = cmsg->FacilityRequestParameter;
1071 if (pparam == NULL || *pparam < 2) {
1072 dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
1073 "Facility Request Parameter");
1074 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1075 return;
1077 function = CAPIMSG_U16(pparam, 1);
1078 switch (function) {
1079 case CAPI_SUPPSVC_GETSUPPORTED:
1080 info = CapiSuccess;
1081 /* Supplementary Service specific parameter */
1082 confparam[3] = 6; /* length */
1083 /* Supplementary services info: Success */
1084 capimsg_setu16(confparam, 4, CapiSuccess);
1085 /* Supported Services: none */
1086 capimsg_setu32(confparam, 6, 0);
1087 break;
1088 /* ToDo: add supported services */
1089 default:
1090 info = CapiFacilitySpecificFunctionNotSupported;
1091 /* Supplementary Service specific parameter */
1092 confparam[3] = 2; /* length */
1093 /* Supplementary services info: not supported */
1094 capimsg_setu16(confparam, 4,
1095 CapiSupplementaryServiceNotSupported);
1098 /* Facility confirmation parameter */
1099 confparam[0] = confparam[3] + 3; /* total length */
1100 /* Function: copy from _REQ message */
1101 capimsg_setu16(confparam, 1, function);
1102 /* Supplementary Service specific parameter already set above */
1103 break;
1105 case CAPI_FACILITY_WAKEUP: /* ToDo */
1106 info = CapiFacilityNotSupported;
1107 confparam[0] = 2; /* length */
1108 /* Number of accepted awake request parameters: 0 */
1109 capimsg_setu16(confparam, 1, 0);
1110 break;
1112 default:
1113 info = CapiFacilityNotSupported;
1114 confparam[0] = 0; /* empty struct */
1117 /* send FACILITY_CONF with given Info and confirmation parameter */
1118 capi_cmsg_answer(cmsg);
1119 cmsg->Info = info;
1120 cmsg->FacilityConfirmationParameter = confparam;
1121 msgsize += confparam[0]; /* length */
1122 cskb = alloc_skb(msgsize, GFP_ATOMIC);
1123 if (!cskb) {
1124 dev_err(cs->dev, "%s: out of memory\n", __func__);
1125 return;
1127 capi_cmsg2message(cmsg, __skb_put(cskb, msgsize));
1128 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1129 capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
1134 * process LISTEN_REQ message
1135 * just store the masks in the application data structure
1137 static void do_listen_req(struct gigaset_capi_ctr *iif,
1138 struct gigaset_capi_appl *ap,
1139 struct sk_buff *skb)
1141 /* decode message */
1142 capi_message2cmsg(&iif->acmsg, skb->data);
1143 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1145 /* store listening parameters */
1146 ap->listenInfoMask = iif->acmsg.InfoMask;
1147 ap->listenCIPmask = iif->acmsg.CIPmask;
1148 send_conf(iif, ap, skb, CapiSuccess);
1152 * process ALERT_REQ message
1153 * nothing to do, Gigaset always alerts anyway
1155 static void do_alert_req(struct gigaset_capi_ctr *iif,
1156 struct gigaset_capi_appl *ap,
1157 struct sk_buff *skb)
1159 /* decode message */
1160 capi_message2cmsg(&iif->acmsg, skb->data);
1161 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1162 send_conf(iif, ap, skb, CapiAlertAlreadySent);
1166 * process CONNECT_REQ message
1167 * allocate a B channel, prepare dial commands, queue a DIAL event,
1168 * emit CONNECT_CONF reply
1170 static void do_connect_req(struct gigaset_capi_ctr *iif,
1171 struct gigaset_capi_appl *ap,
1172 struct sk_buff *skb)
1174 struct cardstate *cs = iif->ctr.driverdata;
1175 _cmsg *cmsg = &iif->acmsg;
1176 struct bc_state *bcs;
1177 char **commands;
1178 char *s;
1179 u8 *pp;
1180 int i, l, lbc, lhlc;
1181 u16 info;
1183 /* decode message */
1184 capi_message2cmsg(cmsg, skb->data);
1185 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1187 /* get free B channel & construct PLCI */
1188 bcs = gigaset_get_free_channel(cs);
1189 if (!bcs) {
1190 dev_notice(cs->dev, "%s: no B channel available\n",
1191 "CONNECT_REQ");
1192 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1193 return;
1195 ap->bcnext = NULL;
1196 bcs->ap = ap;
1197 bcs->rx_bufsize = ap->rp.datablklen;
1198 dev_kfree_skb(bcs->rx_skb);
1199 gigaset_new_rx_skb(bcs);
1200 cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1202 /* build command table */
1203 commands = kzalloc(AT_NUM*(sizeof *commands), GFP_KERNEL);
1204 if (!commands)
1205 goto oom;
1207 /* encode parameter: Called party number */
1208 pp = cmsg->CalledPartyNumber;
1209 if (pp == NULL || *pp == 0) {
1210 dev_notice(cs->dev, "%s: %s missing\n",
1211 "CONNECT_REQ", "Called party number");
1212 info = CapiIllMessageParmCoding;
1213 goto error;
1215 l = *pp++;
1216 /* check type of number/numbering plan byte */
1217 switch (*pp) {
1218 case 0x80: /* unknown type / unknown numbering plan */
1219 case 0x81: /* unknown type / ISDN/Telephony numbering plan */
1220 break;
1221 default: /* others: warn about potential misinterpretation */
1222 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1223 "CONNECT_REQ", "Called party number", *pp);
1225 pp++;
1226 l--;
1227 /* translate "**" internal call prefix to CTP value */
1228 if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1229 s = "^SCTP=0\r";
1230 pp += 2;
1231 l -= 2;
1232 } else {
1233 s = "^SCTP=1\r";
1235 commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1236 if (!commands[AT_TYPE])
1237 goto oom;
1238 commands[AT_DIAL] = kmalloc(l+3, GFP_KERNEL);
1239 if (!commands[AT_DIAL])
1240 goto oom;
1241 snprintf(commands[AT_DIAL], l+3, "D%.*s\r", l, pp);
1243 /* encode parameter: Calling party number */
1244 pp = cmsg->CallingPartyNumber;
1245 if (pp != NULL && *pp > 0) {
1246 l = *pp++;
1248 /* check type of number/numbering plan byte */
1249 /* ToDo: allow for/handle Ext=1? */
1250 switch (*pp) {
1251 case 0x00: /* unknown type / unknown numbering plan */
1252 case 0x01: /* unknown type / ISDN/Telephony num. plan */
1253 break;
1254 default:
1255 dev_notice(cs->dev,
1256 "%s: %s type/plan 0x%02x unsupported\n",
1257 "CONNECT_REQ", "Calling party number", *pp);
1259 pp++;
1260 l--;
1262 /* check presentation indicator */
1263 if (!l) {
1264 dev_notice(cs->dev, "%s: %s IE truncated\n",
1265 "CONNECT_REQ", "Calling party number");
1266 info = CapiIllMessageParmCoding;
1267 goto error;
1269 switch (*pp & 0xfc) { /* ignore Screening indicator */
1270 case 0x80: /* Presentation allowed */
1271 s = "^SCLIP=1\r";
1272 break;
1273 case 0xa0: /* Presentation restricted */
1274 s = "^SCLIP=0\r";
1275 break;
1276 default:
1277 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1278 "CONNECT_REQ",
1279 "Presentation/Screening indicator",
1280 *pp);
1281 s = "^SCLIP=1\r";
1283 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1284 if (!commands[AT_CLIP])
1285 goto oom;
1286 pp++;
1287 l--;
1289 if (l) {
1290 /* number */
1291 commands[AT_MSN] = kmalloc(l+8, GFP_KERNEL);
1292 if (!commands[AT_MSN])
1293 goto oom;
1294 snprintf(commands[AT_MSN], l+8, "^SMSN=%*s\r", l, pp);
1298 /* check parameter: CIP Value */
1299 if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
1300 (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1301 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1302 "CONNECT_REQ", cmsg->CIPValue);
1303 info = CapiCipValueUnknown;
1304 goto error;
1308 * check/encode parameters: BC & HLC
1309 * must be encoded together as device doesn't accept HLC separately
1310 * explicit parameters override values derived from CIP
1313 /* determine lengths */
1314 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1315 lbc = 2*cmsg->BC[0];
1316 else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
1317 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1318 else /* no BC */
1319 lbc = 0;
1320 if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
1321 lhlc = 2*cmsg->HLC[0];
1322 else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1323 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1324 else /* no HLC */
1325 lhlc = 0;
1327 if (lbc) {
1328 /* have BC: allocate and assemble command string */
1329 l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
1330 if (lhlc)
1331 l += lhlc + 7; /* ";^SHLC=" + value */
1332 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1333 if (!commands[AT_BC])
1334 goto oom;
1335 strcpy(commands[AT_BC], "^SBC=");
1336 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1337 decode_ie(cmsg->BC, commands[AT_BC] + 5);
1338 else /* BC derived from CIP */
1339 strcpy(commands[AT_BC] + 5,
1340 cip2bchlc[cmsg->CIPValue].bc);
1341 if (lhlc) {
1342 strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1343 if (cmsg->HLC && cmsg->HLC[0])
1344 /* HLC specified explicitly */
1345 decode_ie(cmsg->HLC,
1346 commands[AT_BC] + lbc + 12);
1347 else /* HLC derived from CIP */
1348 strcpy(commands[AT_BC] + lbc + 12,
1349 cip2bchlc[cmsg->CIPValue].hlc);
1351 strcpy(commands[AT_BC] + l - 2, "\r");
1352 } else {
1353 /* no BC */
1354 if (lhlc) {
1355 dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1356 "CONNECT_REQ");
1357 info = CapiIllMessageParmCoding; /* ? */
1358 goto error;
1362 /* check/encode parameter: B Protocol */
1363 if (cmsg->BProtocol == CAPI_DEFAULT) {
1364 bcs->proto2 = L2_HDLC;
1365 dev_warn(cs->dev,
1366 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1367 } else {
1368 switch (cmsg->B1protocol) {
1369 case 0:
1370 bcs->proto2 = L2_HDLC;
1371 break;
1372 case 1:
1373 bcs->proto2 = L2_VOICE;
1374 break;
1375 default:
1376 dev_warn(cs->dev,
1377 "B1 Protocol %u unsupported, using Transparent\n",
1378 cmsg->B1protocol);
1379 bcs->proto2 = L2_VOICE;
1381 if (cmsg->B2protocol != 1)
1382 dev_warn(cs->dev,
1383 "B2 Protocol %u unsupported, using Transparent\n",
1384 cmsg->B2protocol);
1385 if (cmsg->B3protocol != 0)
1386 dev_warn(cs->dev,
1387 "B3 Protocol %u unsupported, using Transparent\n",
1388 cmsg->B3protocol);
1389 ignore_cstruct_param(cs, cmsg->B1configuration,
1390 "CONNECT_REQ", "B1 Configuration");
1391 ignore_cstruct_param(cs, cmsg->B2configuration,
1392 "CONNECT_REQ", "B2 Configuration");
1393 ignore_cstruct_param(cs, cmsg->B3configuration,
1394 "CONNECT_REQ", "B3 Configuration");
1396 commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1397 if (!commands[AT_PROTO])
1398 goto oom;
1399 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1401 /* ToDo: check/encode remaining parameters */
1402 ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
1403 "CONNECT_REQ", "Called pty subaddr");
1404 ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
1405 "CONNECT_REQ", "Calling pty subaddr");
1406 ignore_cstruct_param(cs, cmsg->LLC,
1407 "CONNECT_REQ", "LLC");
1408 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1409 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1410 "CONNECT_REQ", "B Channel Information");
1411 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1412 "CONNECT_REQ", "Keypad Facility");
1413 ignore_cstruct_param(cs, cmsg->Useruserdata,
1414 "CONNECT_REQ", "User-User Data");
1415 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1416 "CONNECT_REQ", "Facility Data Array");
1419 /* encode parameter: B channel to use */
1420 commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1421 if (!commands[AT_ISO])
1422 goto oom;
1423 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1424 (unsigned) bcs->channel + 1);
1426 /* queue & schedule EV_DIAL event */
1427 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
1428 bcs->at_state.seq_index, NULL)) {
1429 info = CAPI_MSGOSRESOURCEERR;
1430 goto error;
1432 gigaset_schedule_event(cs);
1433 ap->connected = APCONN_SETUP;
1434 send_conf(iif, ap, skb, CapiSuccess);
1435 return;
1437 oom:
1438 dev_err(cs->dev, "%s: out of memory\n", __func__);
1439 info = CAPI_MSGOSRESOURCEERR;
1440 error:
1441 if (commands)
1442 for (i = 0; i < AT_NUM; i++)
1443 kfree(commands[i]);
1444 kfree(commands);
1445 gigaset_free_channel(bcs);
1446 send_conf(iif, ap, skb, info);
1450 * process CONNECT_RESP message
1451 * checks protocol parameters and queues an ACCEPT or HUP event
1453 static void do_connect_resp(struct gigaset_capi_ctr *iif,
1454 struct gigaset_capi_appl *ap,
1455 struct sk_buff *skb)
1457 struct cardstate *cs = iif->ctr.driverdata;
1458 _cmsg *cmsg = &iif->acmsg;
1459 struct bc_state *bcs;
1460 struct gigaset_capi_appl *oap;
1461 int channel;
1463 /* decode message */
1464 capi_message2cmsg(cmsg, skb->data);
1465 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1466 dev_kfree_skb_any(skb);
1468 /* extract and check channel number from PLCI */
1469 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1470 if (!channel || channel > cs->channels) {
1471 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1472 "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1473 return;
1475 bcs = cs->bcs + channel - 1;
1477 switch (cmsg->Reject) {
1478 case 0: /* Accept */
1479 /* drop all competing applications, keep only this one */
1480 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext)
1481 if (oap != ap)
1482 send_disconnect_ind(bcs, oap,
1483 CapiCallGivenToOtherApplication);
1484 ap->bcnext = NULL;
1485 bcs->ap = ap;
1486 bcs->rx_bufsize = ap->rp.datablklen;
1487 dev_kfree_skb(bcs->rx_skb);
1488 gigaset_new_rx_skb(bcs);
1489 bcs->chstate |= CHS_NOTIFY_LL;
1491 /* check/encode B channel protocol */
1492 if (cmsg->BProtocol == CAPI_DEFAULT) {
1493 bcs->proto2 = L2_HDLC;
1494 dev_warn(cs->dev,
1495 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1496 } else {
1497 switch (cmsg->B1protocol) {
1498 case 0:
1499 bcs->proto2 = L2_HDLC;
1500 break;
1501 case 1:
1502 bcs->proto2 = L2_VOICE;
1503 break;
1504 default:
1505 dev_warn(cs->dev,
1506 "B1 Protocol %u unsupported, using Transparent\n",
1507 cmsg->B1protocol);
1508 bcs->proto2 = L2_VOICE;
1510 if (cmsg->B2protocol != 1)
1511 dev_warn(cs->dev,
1512 "B2 Protocol %u unsupported, using Transparent\n",
1513 cmsg->B2protocol);
1514 if (cmsg->B3protocol != 0)
1515 dev_warn(cs->dev,
1516 "B3 Protocol %u unsupported, using Transparent\n",
1517 cmsg->B3protocol);
1518 ignore_cstruct_param(cs, cmsg->B1configuration,
1519 "CONNECT_RESP", "B1 Configuration");
1520 ignore_cstruct_param(cs, cmsg->B2configuration,
1521 "CONNECT_RESP", "B2 Configuration");
1522 ignore_cstruct_param(cs, cmsg->B3configuration,
1523 "CONNECT_RESP", "B3 Configuration");
1526 /* ToDo: check/encode remaining parameters */
1527 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
1528 "CONNECT_RESP", "Connected Number");
1529 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
1530 "CONNECT_RESP", "Connected Subaddress");
1531 ignore_cstruct_param(cs, cmsg->LLC,
1532 "CONNECT_RESP", "LLC");
1533 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1534 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1535 "CONNECT_RESP", "BChannel Information");
1536 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1537 "CONNECT_RESP", "Keypad Facility");
1538 ignore_cstruct_param(cs, cmsg->Useruserdata,
1539 "CONNECT_RESP", "User-User Data");
1540 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1541 "CONNECT_RESP", "Facility Data Array");
1544 /* Accept call */
1545 if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1546 EV_ACCEPT, NULL, 0, NULL))
1547 return;
1548 gigaset_schedule_event(cs);
1549 return;
1551 case 1: /* Ignore */
1552 /* send DISCONNECT_IND to this application */
1553 send_disconnect_ind(bcs, ap, 0);
1555 /* remove it from the list of listening apps */
1556 if (bcs->ap == ap) {
1557 bcs->ap = ap->bcnext;
1558 if (bcs->ap == NULL)
1559 /* last one: stop ev-layer hupD notifications */
1560 bcs->chstate &= ~CHS_NOTIFY_LL;
1561 return;
1563 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1564 if (oap->bcnext == ap) {
1565 oap->bcnext = oap->bcnext->bcnext;
1566 return;
1569 dev_err(cs->dev, "%s: application %u not found\n",
1570 __func__, ap->id);
1571 return;
1573 default: /* Reject */
1574 /* drop all competing applications, keep only this one */
1575 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext)
1576 if (oap != ap)
1577 send_disconnect_ind(bcs, oap,
1578 CapiCallGivenToOtherApplication);
1579 ap->bcnext = NULL;
1580 bcs->ap = ap;
1582 /* reject call - will trigger DISCONNECT_IND for this app */
1583 dev_info(cs->dev, "%s: Reject=%x\n",
1584 "CONNECT_RESP", cmsg->Reject);
1585 if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1586 EV_HUP, NULL, 0, NULL))
1587 return;
1588 gigaset_schedule_event(cs);
1589 return;
1594 * process CONNECT_B3_REQ message
1595 * build NCCI and emit CONNECT_B3_CONF reply
1597 static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1598 struct gigaset_capi_appl *ap,
1599 struct sk_buff *skb)
1601 struct cardstate *cs = iif->ctr.driverdata;
1602 _cmsg *cmsg = &iif->acmsg;
1603 int channel;
1605 /* decode message */
1606 capi_message2cmsg(cmsg, skb->data);
1607 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1609 /* extract and check channel number from PLCI */
1610 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1611 if (!channel || channel > cs->channels) {
1612 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1613 "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
1614 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1615 return;
1618 /* mark logical connection active */
1619 ap->connected = APCONN_ACTIVE;
1621 /* build NCCI: always 1 (one B3 connection only) */
1622 cmsg->adr.adrNCCI |= 1 << 16;
1624 /* NCPI parameter: not applicable for B3 Transparent */
1625 ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
1626 send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
1627 CapiNcpiNotSupportedByProtocol : CapiSuccess);
1631 * process CONNECT_B3_RESP message
1632 * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1633 * or queue EV_HUP and emit DISCONNECT_B3_IND.
1634 * The emitted message is always shorter than the received one,
1635 * allowing to reuse the skb.
1637 static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1638 struct gigaset_capi_appl *ap,
1639 struct sk_buff *skb)
1641 struct cardstate *cs = iif->ctr.driverdata;
1642 _cmsg *cmsg = &iif->acmsg;
1643 struct bc_state *bcs;
1644 int channel;
1645 unsigned int msgsize;
1646 u8 command;
1648 /* decode message */
1649 capi_message2cmsg(cmsg, skb->data);
1650 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1652 /* extract and check channel number and NCCI */
1653 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
1654 if (!channel || channel > cs->channels ||
1655 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
1656 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1657 "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
1658 dev_kfree_skb_any(skb);
1659 return;
1661 bcs = &cs->bcs[channel-1];
1663 if (cmsg->Reject) {
1664 /* Reject: clear B3 connect received flag */
1665 ap->connected = APCONN_SETUP;
1667 /* trigger hangup, causing eventual DISCONNECT_IND */
1668 if (!gigaset_add_event(cs, &bcs->at_state,
1669 EV_HUP, NULL, 0, NULL)) {
1670 dev_kfree_skb_any(skb);
1671 return;
1673 gigaset_schedule_event(cs);
1675 /* emit DISCONNECT_B3_IND */
1676 command = CAPI_DISCONNECT_B3;
1677 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1678 } else {
1680 * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1681 * we only send CONNECT_B3_IND if the B channel is up
1683 command = CAPI_CONNECT_B3_ACTIVE;
1684 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1686 capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1687 ap->nextMessageNumber++, cmsg->adr.adrNCCI);
1688 __skb_trim(skb, msgsize);
1689 capi_cmsg2message(cmsg, skb->data);
1690 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1691 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1695 * process DISCONNECT_REQ message
1696 * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1697 * emit DISCONNECT_CONF reply
1699 static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1700 struct gigaset_capi_appl *ap,
1701 struct sk_buff *skb)
1703 struct cardstate *cs = iif->ctr.driverdata;
1704 _cmsg *cmsg = &iif->acmsg;
1705 struct bc_state *bcs;
1706 _cmsg *b3cmsg;
1707 struct sk_buff *b3skb;
1708 int channel;
1710 /* decode message */
1711 capi_message2cmsg(cmsg, skb->data);
1712 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1714 /* extract and check channel number from PLCI */
1715 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1716 if (!channel || channel > cs->channels) {
1717 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1718 "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
1719 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1720 return;
1722 bcs = cs->bcs + channel - 1;
1724 /* ToDo: process parameter: Additional info */
1725 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1726 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1727 "DISCONNECT_REQ", "B Channel Information");
1728 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1729 "DISCONNECT_REQ", "Keypad Facility");
1730 ignore_cstruct_param(cs, cmsg->Useruserdata,
1731 "DISCONNECT_REQ", "User-User Data");
1732 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1733 "DISCONNECT_REQ", "Facility Data Array");
1736 /* skip if DISCONNECT_IND already sent */
1737 if (!ap->connected)
1738 return;
1740 /* check for active logical connection */
1741 if (ap->connected >= APCONN_ACTIVE) {
1743 * emit DISCONNECT_B3_IND with cause 0x3301
1744 * use separate cmsg structure, as the content of iif->acmsg
1745 * is still needed for creating the _CONF message
1747 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1748 if (!b3cmsg) {
1749 dev_err(cs->dev, "%s: out of memory\n", __func__);
1750 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1751 return;
1753 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1754 ap->nextMessageNumber++,
1755 cmsg->adr.adrPLCI | (1 << 16));
1756 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1757 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1758 if (b3skb == NULL) {
1759 dev_err(cs->dev, "%s: out of memory\n", __func__);
1760 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1761 return;
1763 capi_cmsg2message(b3cmsg,
1764 __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN));
1765 kfree(b3cmsg);
1766 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1769 /* trigger hangup, causing eventual DISCONNECT_IND */
1770 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
1771 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1772 return;
1774 gigaset_schedule_event(cs);
1776 /* emit reply */
1777 send_conf(iif, ap, skb, CapiSuccess);
1781 * process DISCONNECT_B3_REQ message
1782 * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
1784 static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
1785 struct gigaset_capi_appl *ap,
1786 struct sk_buff *skb)
1788 struct cardstate *cs = iif->ctr.driverdata;
1789 _cmsg *cmsg = &iif->acmsg;
1790 int channel;
1792 /* decode message */
1793 capi_message2cmsg(cmsg, skb->data);
1794 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1796 /* extract and check channel number and NCCI */
1797 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
1798 if (!channel || channel > cs->channels ||
1799 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
1800 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1801 "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
1802 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1803 return;
1806 /* reject if logical connection not active */
1807 if (ap->connected < APCONN_ACTIVE) {
1808 send_conf(iif, ap, skb,
1809 CapiMessageNotSupportedInCurrentState);
1810 return;
1813 /* trigger hangup, causing eventual DISCONNECT_B3_IND */
1814 if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1815 EV_HUP, NULL, 0, NULL)) {
1816 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1817 return;
1819 gigaset_schedule_event(cs);
1821 /* NCPI parameter: not applicable for B3 Transparent */
1822 ignore_cstruct_param(cs, cmsg->NCPI,
1823 "DISCONNECT_B3_REQ", "NCPI");
1824 send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
1825 CapiNcpiNotSupportedByProtocol : CapiSuccess);
1829 * process DATA_B3_REQ message
1831 static void do_data_b3_req(struct gigaset_capi_ctr *iif,
1832 struct gigaset_capi_appl *ap,
1833 struct sk_buff *skb)
1835 struct cardstate *cs = iif->ctr.driverdata;
1836 int channel = CAPIMSG_PLCI_PART(skb->data);
1837 u16 ncci = CAPIMSG_NCCI_PART(skb->data);
1838 u16 msglen = CAPIMSG_LEN(skb->data);
1839 u16 datalen = CAPIMSG_DATALEN(skb->data);
1840 u16 flags = CAPIMSG_FLAGS(skb->data);
1841 u16 msgid = CAPIMSG_MSGID(skb->data);
1842 u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
1844 /* frequent message, avoid _cmsg overhead */
1845 dump_rawmsg(DEBUG_LLDATA, "DATA_B3_REQ", skb->data);
1847 gig_dbg(DEBUG_LLDATA,
1848 "Receiving data from LL (ch: %d, flg: %x, sz: %d|%d)",
1849 channel, flags, msglen, datalen);
1851 /* check parameters */
1852 if (channel == 0 || channel > cs->channels || ncci != 1) {
1853 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1854 "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
1855 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1856 return;
1858 if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
1859 dev_notice(cs->dev, "%s: unexpected length %d\n",
1860 "DATA_B3_REQ", msglen);
1861 if (msglen + datalen != skb->len)
1862 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
1863 "DATA_B3_REQ", msglen, datalen, skb->len);
1864 if (msglen + datalen > skb->len) {
1865 /* message too short for announced data length */
1866 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
1867 return;
1869 if (flags & CAPI_FLAGS_RESERVED) {
1870 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
1871 "DATA_B3_REQ", flags);
1872 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1873 return;
1876 /* reject if logical connection not active */
1877 if (ap->connected < APCONN_ACTIVE) {
1878 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
1879 return;
1882 /* pull CAPI message into link layer header */
1883 skb_reset_mac_header(skb);
1884 skb->mac_len = msglen;
1885 skb_pull(skb, msglen);
1887 /* pass to device-specific module */
1888 if (cs->ops->send_skb(&cs->bcs[channel-1], skb) < 0) {
1889 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1890 return;
1894 * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
1895 * confirmation" bit is set; otherwise we have to send it now
1897 if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
1898 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
1899 flags ? CapiFlagsNotSupportedByProtocol
1900 : CAPI_NOERROR);
1904 * process RESET_B3_REQ message
1905 * just always reply "not supported by current protocol"
1907 static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
1908 struct gigaset_capi_appl *ap,
1909 struct sk_buff *skb)
1911 /* decode message */
1912 capi_message2cmsg(&iif->acmsg, skb->data);
1913 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1914 send_conf(iif, ap, skb,
1915 CapiResetProcedureNotSupportedByCurrentProtocol);
1919 * dump unsupported/ignored messages at most twice per minute,
1920 * some apps send those very frequently
1922 static unsigned long ignored_msg_dump_time;
1925 * unsupported CAPI message handler
1927 static void do_unsupported(struct gigaset_capi_ctr *iif,
1928 struct gigaset_capi_appl *ap,
1929 struct sk_buff *skb)
1931 /* decode message */
1932 capi_message2cmsg(&iif->acmsg, skb->data);
1933 if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000))
1934 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1935 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
1939 * CAPI message handler: no-op
1941 static void do_nothing(struct gigaset_capi_ctr *iif,
1942 struct gigaset_capi_appl *ap,
1943 struct sk_buff *skb)
1945 if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000)) {
1946 /* decode message */
1947 capi_message2cmsg(&iif->acmsg, skb->data);
1948 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1950 dev_kfree_skb_any(skb);
1953 static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
1954 struct gigaset_capi_appl *ap,
1955 struct sk_buff *skb)
1957 dump_rawmsg(DEBUG_LLDATA, __func__, skb->data);
1958 dev_kfree_skb_any(skb);
1961 /* table of outgoing CAPI message handlers with lookup function */
1962 typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
1963 struct gigaset_capi_appl *,
1964 struct sk_buff *);
1966 static struct {
1967 u16 cmd;
1968 capi_send_handler_t handler;
1969 } capi_send_handler_table[] = {
1970 /* most frequent messages first for faster lookup */
1971 { CAPI_DATA_B3_REQ, do_data_b3_req },
1972 { CAPI_DATA_B3_RESP, do_data_b3_resp },
1974 { CAPI_ALERT_REQ, do_alert_req },
1975 { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
1976 { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
1977 { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
1978 { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
1979 { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
1980 { CAPI_CONNECT_REQ, do_connect_req },
1981 { CAPI_CONNECT_RESP, do_connect_resp },
1982 { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
1983 { CAPI_DISCONNECT_B3_RESP, do_nothing },
1984 { CAPI_DISCONNECT_REQ, do_disconnect_req },
1985 { CAPI_DISCONNECT_RESP, do_nothing },
1986 { CAPI_FACILITY_REQ, do_facility_req },
1987 { CAPI_FACILITY_RESP, do_nothing },
1988 { CAPI_LISTEN_REQ, do_listen_req },
1989 { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
1990 { CAPI_RESET_B3_REQ, do_reset_b3_req },
1991 { CAPI_RESET_B3_RESP, do_nothing },
1994 * ToDo: support overlap sending (requires ev-layer state
1995 * machine extension to generate additional ATD commands)
1997 { CAPI_INFO_REQ, do_unsupported },
1998 { CAPI_INFO_RESP, do_nothing },
2001 * ToDo: what's the proper response for these?
2003 { CAPI_MANUFACTURER_REQ, do_nothing },
2004 { CAPI_MANUFACTURER_RESP, do_nothing },
2007 /* look up handler */
2008 static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
2010 size_t i;
2012 for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2013 if (capi_send_handler_table[i].cmd == cmd)
2014 return capi_send_handler_table[i].handler;
2015 return NULL;
2020 * gigaset_send_message() - accept a CAPI message from an application
2021 * @ctr: controller descriptor structure.
2022 * @skb: CAPI message.
2024 * Return value: CAPI error code
2025 * Note: capidrv (and probably others, too) only uses the return value to
2026 * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2028 static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2030 struct gigaset_capi_ctr *iif
2031 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2032 struct cardstate *cs = ctr->driverdata;
2033 struct gigaset_capi_appl *ap;
2034 capi_send_handler_t handler;
2036 /* can only handle linear sk_buffs */
2037 if (skb_linearize(skb) < 0) {
2038 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2039 return CAPI_MSGOSRESOURCEERR;
2042 /* retrieve application data structure */
2043 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2044 if (!ap) {
2045 dev_notice(cs->dev, "%s: application %u not registered\n",
2046 __func__, CAPIMSG_APPID(skb->data));
2047 return CAPI_ILLAPPNR;
2050 /* look up command */
2051 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2052 if (!handler) {
2053 /* unknown/unsupported message type */
2054 if (printk_ratelimit())
2055 dev_notice(cs->dev, "%s: unsupported message %u\n",
2056 __func__, CAPIMSG_CMD(skb->data));
2057 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2060 /* serialize */
2061 if (atomic_add_return(1, &iif->sendqlen) > 1) {
2062 /* queue behind other messages */
2063 skb_queue_tail(&iif->sendqueue, skb);
2064 return CAPI_NOERROR;
2067 /* process message */
2068 handler(iif, ap, skb);
2070 /* process other messages arrived in the meantime */
2071 while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2072 skb = skb_dequeue(&iif->sendqueue);
2073 if (!skb) {
2074 /* should never happen */
2075 dev_err(cs->dev, "%s: send queue empty\n", __func__);
2076 continue;
2078 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2079 if (!ap) {
2080 /* could that happen? */
2081 dev_warn(cs->dev, "%s: application %u vanished\n",
2082 __func__, CAPIMSG_APPID(skb->data));
2083 continue;
2085 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2086 if (!handler) {
2087 /* should never happen */
2088 dev_err(cs->dev, "%s: handler %x vanished\n",
2089 __func__, CAPIMSG_CMD(skb->data));
2090 continue;
2092 handler(iif, ap, skb);
2095 return CAPI_NOERROR;
2099 * gigaset_procinfo() - build single line description for controller
2100 * @ctr: controller descriptor structure.
2102 * Return value: pointer to generated string (null terminated)
2104 static char *gigaset_procinfo(struct capi_ctr *ctr)
2106 return ctr->name; /* ToDo: more? */
2109 static int gigaset_proc_show(struct seq_file *m, void *v)
2111 struct capi_ctr *ctr = m->private;
2112 struct cardstate *cs = ctr->driverdata;
2113 char *s;
2114 int i;
2116 seq_printf(m, "%-16s %s\n", "name", ctr->name);
2117 seq_printf(m, "%-16s %s %s\n", "dev",
2118 dev_driver_string(cs->dev), dev_name(cs->dev));
2119 seq_printf(m, "%-16s %d\n", "id", cs->myid);
2120 if (cs->gotfwver)
2121 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
2122 cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
2123 seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2124 seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
2126 switch (cs->mode) {
2127 case M_UNKNOWN:
2128 s = "unknown";
2129 break;
2130 case M_CONFIG:
2131 s = "config";
2132 break;
2133 case M_UNIMODEM:
2134 s = "Unimodem";
2135 break;
2136 case M_CID:
2137 s = "CID";
2138 break;
2139 default:
2140 s = "??";
2142 seq_printf(m, "%-16s %s\n", "mode", s);
2144 switch (cs->mstate) {
2145 case MS_UNINITIALIZED:
2146 s = "uninitialized";
2147 break;
2148 case MS_INIT:
2149 s = "init";
2150 break;
2151 case MS_LOCKED:
2152 s = "locked";
2153 break;
2154 case MS_SHUTDOWN:
2155 s = "shutdown";
2156 break;
2157 case MS_RECOVER:
2158 s = "recover";
2159 break;
2160 case MS_READY:
2161 s = "ready";
2162 break;
2163 default:
2164 s = "??";
2166 seq_printf(m, "%-16s %s\n", "mstate", s);
2168 seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2169 seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2170 seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2171 seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
2173 for (i = 0; i < cs->channels; i++) {
2174 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
2175 cs->bcs[i].corrupted);
2176 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
2177 cs->bcs[i].trans_down);
2178 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
2179 cs->bcs[i].trans_up);
2180 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
2181 cs->bcs[i].chstate);
2182 switch (cs->bcs[i].proto2) {
2183 case L2_BITSYNC:
2184 s = "bitsync";
2185 break;
2186 case L2_HDLC:
2187 s = "HDLC";
2188 break;
2189 case L2_VOICE:
2190 s = "voice";
2191 break;
2192 default:
2193 s = "??";
2195 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
2197 return 0;
2200 static int gigaset_proc_open(struct inode *inode, struct file *file)
2202 return single_open(file, gigaset_proc_show, PDE(inode)->data);
2205 static const struct file_operations gigaset_proc_fops = {
2206 .owner = THIS_MODULE,
2207 .open = gigaset_proc_open,
2208 .read = seq_read,
2209 .llseek = seq_lseek,
2210 .release = single_release,
2214 * gigaset_isdn_regdev() - register device to LL
2215 * @cs: device descriptor structure.
2216 * @isdnid: device name.
2218 * Return value: 1 for success, 0 for failure
2220 int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
2222 struct gigaset_capi_ctr *iif;
2223 int rc;
2225 iif = kmalloc(sizeof(*iif), GFP_KERNEL);
2226 if (!iif) {
2227 pr_err("%s: out of memory\n", __func__);
2228 return 0;
2231 /* prepare controller structure */
2232 iif->ctr.owner = THIS_MODULE;
2233 iif->ctr.driverdata = cs;
2234 strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name));
2235 iif->ctr.driver_name = "gigaset";
2236 iif->ctr.load_firmware = NULL;
2237 iif->ctr.reset_ctr = NULL;
2238 iif->ctr.register_appl = gigaset_register_appl;
2239 iif->ctr.release_appl = gigaset_release_appl;
2240 iif->ctr.send_message = gigaset_send_message;
2241 iif->ctr.procinfo = gigaset_procinfo;
2242 iif->ctr.proc_fops = &gigaset_proc_fops;
2243 INIT_LIST_HEAD(&iif->appls);
2244 skb_queue_head_init(&iif->sendqueue);
2245 atomic_set(&iif->sendqlen, 0);
2247 /* register controller with CAPI */
2248 rc = attach_capi_ctr(&iif->ctr);
2249 if (rc) {
2250 pr_err("attach_capi_ctr failed (%d)\n", rc);
2251 kfree(iif);
2252 return 0;
2255 cs->iif = iif;
2256 cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
2257 return 1;
2261 * gigaset_isdn_unregdev() - unregister device from LL
2262 * @cs: device descriptor structure.
2264 void gigaset_isdn_unregdev(struct cardstate *cs)
2266 struct gigaset_capi_ctr *iif = cs->iif;
2268 detach_capi_ctr(&iif->ctr);
2269 kfree(iif);
2270 cs->iif = NULL;
2273 static struct capi_driver capi_driver_gigaset = {
2274 .name = "gigaset",
2275 .revision = "1.0",
2279 * gigaset_isdn_regdrv() - register driver to LL
2281 void gigaset_isdn_regdrv(void)
2283 pr_info("Kernel CAPI interface\n");
2284 register_capi_driver(&capi_driver_gigaset);
2288 * gigaset_isdn_unregdrv() - unregister driver from LL
2290 void gigaset_isdn_unregdrv(void)
2292 unregister_capi_driver(&capi_driver_gigaset);