1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Kernel CAPI interface for the Gigaset driver
5 * Copyright (c) 2009 by Tilman Schmidt <tilman@imap.cc>.
7 * =====================================================================
8 * =====================================================================
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <linux/ratelimit.h>
15 #include <linux/isdn/capilli.h>
16 #include <linux/isdn/capicmd.h>
17 #include <linux/isdn/capiutil.h>
18 #include <linux/export.h>
20 /* missing from kernelcapi.h */
21 #define CapiNcpiNotSupportedByProtocol 0x0001
22 #define CapiFlagsNotSupportedByProtocol 0x0002
23 #define CapiAlertAlreadySent 0x0003
24 #define CapiFacilitySpecificFunctionNotSupported 0x3011
26 /* missing from capicmd.h */
27 #define CAPI_CONNECT_IND_BASELEN (CAPI_MSG_BASELEN + 4 + 2 + 8 * 1)
28 #define CAPI_CONNECT_ACTIVE_IND_BASELEN (CAPI_MSG_BASELEN + 4 + 3 * 1)
29 #define CAPI_CONNECT_B3_IND_BASELEN (CAPI_MSG_BASELEN + 4 + 1)
30 #define CAPI_CONNECT_B3_ACTIVE_IND_BASELEN (CAPI_MSG_BASELEN + 4 + 1)
31 #define CAPI_DATA_B3_REQ_LEN64 (CAPI_MSG_BASELEN + 4 + 4 + 2 + 2 + 2 + 8)
32 #define CAPI_DATA_B3_CONF_LEN (CAPI_MSG_BASELEN + 4 + 2 + 2)
33 #define CAPI_DISCONNECT_IND_LEN (CAPI_MSG_BASELEN + 4 + 2)
34 #define CAPI_DISCONNECT_B3_IND_BASELEN (CAPI_MSG_BASELEN + 4 + 2 + 1)
35 #define CAPI_FACILITY_CONF_BASELEN (CAPI_MSG_BASELEN + 4 + 2 + 2 + 1)
36 /* most _CONF messages contain only Controller/PLCI/NCCI and Info parameters */
37 #define CAPI_STDCONF_LEN (CAPI_MSG_BASELEN + 4 + 2)
39 #define CAPI_FACILITY_HANDSET 0x0000
40 #define CAPI_FACILITY_DTMF 0x0001
41 #define CAPI_FACILITY_V42BIS 0x0002
42 #define CAPI_FACILITY_SUPPSVC 0x0003
43 #define CAPI_FACILITY_WAKEUP 0x0004
44 #define CAPI_FACILITY_LI 0x0005
46 #define CAPI_SUPPSVC_GETSUPPORTED 0x0000
47 #define CAPI_SUPPSVC_LISTEN 0x0001
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)
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 bcs->apconnstate */
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;
83 struct capi_register_params rp;
84 u16 nextMessageNumber;
89 /* CAPI specific controller data structure */
90 struct gigaset_capi_ctr {
92 struct list_head appls;
93 struct sk_buff_head sendqueue;
95 /* two _cmsg structures possibly used concurrently: */
96 _cmsg hcmsg; /* for message composition triggered from hardware */
97 _cmsg acmsg; /* for dissection of messages sent from application */
98 u8 bc_buf[MAX_BC_OCTETS + 1];
99 u8 hlc_buf[MAX_HLC_OCTETS + 1];
100 u8 cgpty_buf[MAX_NUMBER_DIGITS + 3];
101 u8 cdpty_buf[MAX_NUMBER_DIGITS + 2];
104 /* CIP Value table (from CAPI 2.0 standard, ch. 6.1) */
109 [1] = { "8090A3", NULL }, /* Speech (A-law) */
110 [2] = { "8890", NULL }, /* Unrestricted digital information */
111 [3] = { "8990", NULL }, /* Restricted digital information */
112 [4] = { "9090A3", NULL }, /* 3,1 kHz audio (A-law) */
113 [5] = { "9190", NULL }, /* 7 kHz audio */
114 [6] = { "9890", NULL }, /* Video */
115 [7] = { "88C0C6E6", NULL }, /* Packet mode */
116 [8] = { "8890218F", NULL }, /* 56 kbit/s rate adaptation */
117 [9] = { "9190A5", NULL }, /* Unrestricted digital information
118 * with tones/announcements */
119 [16] = { "8090A3", "9181" }, /* Telephony */
120 [17] = { "9090A3", "9184" }, /* Group 2/3 facsimile */
121 [18] = { "8890", "91A1" }, /* Group 4 facsimile Class 1 */
122 [19] = { "8890", "91A4" }, /* Teletex service basic and mixed mode
123 * and Group 4 facsimile service
124 * Classes II and III */
125 [20] = { "8890", "91A8" }, /* Teletex service basic and
126 * processable mode */
127 [21] = { "8890", "91B1" }, /* Teletex service basic mode */
128 [22] = { "8890", "91B2" }, /* International interworking for
130 [23] = { "8890", "91B5" }, /* Telex */
131 [24] = { "8890", "91B8" }, /* Message Handling Systems
132 * in accordance with X.400 */
133 [25] = { "8890", "91C1" }, /* OSI application
134 * in accordance with X.200 */
135 [26] = { "9190A5", "9181" }, /* 7 kHz telephony */
136 [27] = { "9190A5", "916001" }, /* Video telephony, first connection */
137 [28] = { "8890", "916002" }, /* Video telephony, second connection */
146 * emit unsupported parameter warning
148 static inline void ignore_cstruct_param(struct cardstate *cs, _cstruct param,
149 char *msgname, char *paramname)
152 dev_warn(cs->dev, "%s: ignoring unsupported parameter: %s\n",
157 * convert an IE from Gigaset hex string to ETSI binary representation
158 * including length byte
159 * return value: result length, -1 on error
161 static int encode_ie(char *in, u8 *out, int maxlen)
165 if (!isxdigit(in[0]) || !isxdigit(in[1]) || l >= maxlen)
167 out[++l] = (hex_to_bin(in[0]) << 4) + hex_to_bin(in[1]);
175 * convert an IE from ETSI binary representation including length byte
176 * to Gigaset hex string
178 static void decode_ie(u8 *in, char *out)
182 /* ToDo: conversion to upper case necessary? */
183 *out++ = toupper(hex_asc_hi(*++in));
184 *out++ = toupper(hex_asc_lo(*in));
189 * retrieve application data structure for an application ID
191 static inline struct gigaset_capi_appl *
192 get_appl(struct gigaset_capi_ctr *iif, u16 appl)
194 struct gigaset_capi_appl *ap;
196 list_for_each_entry(ap, &iif->appls, ctrlist)
203 * dump CAPI message to kernel messages for debugging
205 static inline void dump_cmsg(enum debuglevel level, const char *tag, _cmsg *p)
207 #ifdef CONFIG_GIGASET_DEBUG
208 /* dump at most 20 messages in 20 secs */
209 static DEFINE_RATELIMIT_STATE(msg_dump_ratelimit, 20 * HZ, 20);
212 if (!(gigaset_debuglevel & level))
214 if (!___ratelimit(&msg_dump_ratelimit, tag))
217 cdb = capi_cmsg2str(p);
219 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId, cdb->buf);
222 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId,
223 capi_cmd2str(p->Command, p->Subcommand));
228 static inline void dump_rawmsg(enum debuglevel level, const char *tag,
231 #ifdef CONFIG_GIGASET_DEBUG
235 if (!(gigaset_debuglevel & level))
238 l = CAPIMSG_LEN(data);
240 gig_dbg(level, "%s: ??? LEN=%04d", tag, l);
243 gig_dbg(level, "%s: 0x%02x:0x%02x: ID=%03d #0x%04x LEN=%04d NCCI=0x%x",
244 tag, CAPIMSG_COMMAND(data), CAPIMSG_SUBCOMMAND(data),
245 CAPIMSG_APPID(data), CAPIMSG_MSGID(data), l,
246 CAPIMSG_CONTROL(data));
251 l = 64; /* arbitrary limit */
252 dbgline = kmalloc_array(3, l, GFP_ATOMIC);
255 for (i = 0; i < l; i++) {
256 dbgline[3 * i] = hex_asc_hi(data[12 + i]);
257 dbgline[3 * i + 1] = hex_asc_lo(data[12 + i]);
258 dbgline[3 * i + 2] = ' ';
260 dbgline[3 * l - 1] = '\0';
261 gig_dbg(level, " %s", dbgline);
263 if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
264 (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
265 CAPIMSG_SUBCOMMAND(data) == CAPI_IND)) {
266 l = CAPIMSG_DATALEN(data);
267 gig_dbg(level, " DataLength=%d", l);
268 if (l <= 0 || !(gigaset_debuglevel & DEBUG_LLDATA))
271 l = 64; /* arbitrary limit */
272 dbgline = kmalloc_array(3, l, GFP_ATOMIC);
275 data += CAPIMSG_LEN(data);
276 for (i = 0; i < l; i++) {
277 dbgline[3 * i] = hex_asc_hi(data[i]);
278 dbgline[3 * i + 1] = hex_asc_lo(data[i]);
279 dbgline[3 * i + 2] = ' ';
281 dbgline[3 * l - 1] = '\0';
282 gig_dbg(level, " %s", dbgline);
289 * format CAPI IE as string
292 #ifdef CONFIG_GIGASET_DEBUG
293 static const char *format_ie(const char *ie)
295 static char result[3 * MAX_FMT_IE_LEN];
303 if (count > MAX_FMT_IE_LEN)
304 count = MAX_FMT_IE_LEN - 1;
306 *pout++ = hex_asc_hi(*++ie);
307 *pout++ = hex_asc_lo(*ie);
310 if (len > MAX_FMT_IE_LEN) {
321 * emit DATA_B3_CONF message
323 static void send_data_b3_conf(struct cardstate *cs, struct capi_ctr *ctr,
324 u16 appl, u16 msgid, int channel,
325 u16 handle, u16 info)
327 struct sk_buff *cskb;
330 cskb = alloc_skb(CAPI_DATA_B3_CONF_LEN, GFP_ATOMIC);
332 dev_err(cs->dev, "%s: out of memory\n", __func__);
335 /* frequent message, avoid _cmsg overhead */
336 msg = __skb_put(cskb, CAPI_DATA_B3_CONF_LEN);
337 CAPIMSG_SETLEN(msg, CAPI_DATA_B3_CONF_LEN);
338 CAPIMSG_SETAPPID(msg, appl);
339 CAPIMSG_SETCOMMAND(msg, CAPI_DATA_B3);
340 CAPIMSG_SETSUBCOMMAND(msg, CAPI_CONF);
341 CAPIMSG_SETMSGID(msg, msgid);
342 CAPIMSG_SETCONTROLLER(msg, ctr->cnr);
343 CAPIMSG_SETPLCI_PART(msg, channel);
344 CAPIMSG_SETNCCI_PART(msg, 1);
345 CAPIMSG_SETHANDLE_CONF(msg, handle);
346 CAPIMSG_SETINFO_CONF(msg, info);
349 dump_rawmsg(DEBUG_MCMD, __func__, msg);
350 capi_ctr_handle_message(ctr, appl, cskb);
355 * driver interface functions
356 * ==========================
360 * gigaset_skb_sent() - acknowledge transmission of outgoing skb
361 * @bcs: B channel descriptor structure.
364 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
365 * skb has been successfully sent, for signalling completion to the LL.
367 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *dskb)
369 struct cardstate *cs = bcs->cs;
370 struct gigaset_capi_ctr *iif = cs->iif;
371 struct gigaset_capi_appl *ap = bcs->ap;
372 unsigned char *req = skb_mac_header(dskb);
375 /* update statistics */
379 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
383 /* don't send further B3 messages if disconnected */
384 if (bcs->apconnstate < APCONN_ACTIVE) {
385 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
390 * send DATA_B3_CONF if "delivery confirmation" bit was set in request;
391 * otherwise it has already been sent by do_data_b3_req()
393 flags = CAPIMSG_FLAGS(req);
394 if (flags & CAPI_FLAGS_DELIVERY_CONFIRMATION)
395 send_data_b3_conf(cs, &iif->ctr, ap->id, CAPIMSG_MSGID(req),
396 bcs->channel + 1, CAPIMSG_HANDLE_REQ(req),
397 (flags & ~CAPI_FLAGS_DELIVERY_CONFIRMATION) ?
398 CapiFlagsNotSupportedByProtocol :
401 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
404 * gigaset_skb_rcvd() - pass received skb to LL
405 * @bcs: B channel descriptor structure.
406 * @skb: received data.
408 * Called by hardware module {bas,ser,usb}_gigaset when user data has
409 * been successfully received, for passing to the LL.
410 * Warning: skb must not be accessed anymore!
412 void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
414 struct cardstate *cs = bcs->cs;
415 struct gigaset_capi_ctr *iif = cs->iif;
416 struct gigaset_capi_appl *ap = bcs->ap;
419 /* update statistics */
423 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
424 dev_kfree_skb_any(skb);
428 /* don't send further B3 messages if disconnected */
429 if (bcs->apconnstate < APCONN_ACTIVE) {
430 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
431 dev_kfree_skb_any(skb);
436 * prepend DATA_B3_IND message to payload
437 * Parameters: NCCI = 1, all others 0/unused
438 * frequent message, avoid _cmsg overhead
440 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
441 CAPIMSG_SETLEN(skb->data, CAPI_DATA_B3_REQ_LEN);
442 CAPIMSG_SETAPPID(skb->data, ap->id);
443 CAPIMSG_SETCOMMAND(skb->data, CAPI_DATA_B3);
444 CAPIMSG_SETSUBCOMMAND(skb->data, CAPI_IND);
445 CAPIMSG_SETMSGID(skb->data, ap->nextMessageNumber++);
446 CAPIMSG_SETCONTROLLER(skb->data, iif->ctr.cnr);
447 CAPIMSG_SETPLCI_PART(skb->data, bcs->channel + 1);
448 CAPIMSG_SETNCCI_PART(skb->data, 1);
449 /* Data parameter not used */
450 CAPIMSG_SETDATALEN(skb->data, len);
451 /* Data handle parameter not used */
452 CAPIMSG_SETFLAGS(skb->data, 0);
453 /* Data64 parameter not present */
456 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
457 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
459 EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
462 * gigaset_isdn_rcv_err() - signal receive error
463 * @bcs: B channel descriptor structure.
465 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
466 * has occurred, for signalling to the LL.
468 void gigaset_isdn_rcv_err(struct bc_state *bcs)
470 /* if currently ignoring packets, just count down */
476 /* update statistics */
479 /* ToDo: signal error -> LL */
481 EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
484 * gigaset_isdn_icall() - signal incoming call
485 * @at_state: connection state structure.
487 * Called by main module at tasklet level to notify the LL that an incoming
488 * call has been received. @at_state contains the parameters of the call.
490 * Return value: call disposition (ICALL_*)
492 int gigaset_isdn_icall(struct at_state_t *at_state)
494 struct cardstate *cs = at_state->cs;
495 struct bc_state *bcs = at_state->bcs;
496 struct gigaset_capi_ctr *iif = cs->iif;
497 struct gigaset_capi_appl *ap;
500 unsigned int msgsize;
505 * ToDo: signal calls without a free B channel, too
506 * (requires a u8 handle for the at_state structure that can
507 * be stored in the PLCI and used in the CONNECT_RESP message
508 * handler to retrieve it)
513 /* prepare CONNECT_IND message, using B channel number as PLCI */
514 capi_cmsg_header(&iif->hcmsg, 0, CAPI_CONNECT, CAPI_IND, 0,
515 iif->ctr.cnr | ((bcs->channel + 1) << 8));
517 /* minimum size, all structs empty */
518 msgsize = CAPI_CONNECT_IND_BASELEN;
520 /* Bearer Capability (mandatory) */
521 if (at_state->str_var[STR_ZBC]) {
522 /* pass on BC from Gigaset */
523 if (encode_ie(at_state->str_var[STR_ZBC], iif->bc_buf,
524 MAX_BC_OCTETS) < 0) {
525 dev_warn(cs->dev, "RING ignored - bad BC %s\n",
526 at_state->str_var[STR_ZBC]);
530 /* look up corresponding CIP value */
531 iif->hcmsg.CIPValue = 0; /* default if nothing found */
532 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
533 if (cip2bchlc[i].bc != NULL &&
534 cip2bchlc[i].hlc == NULL &&
535 !strcmp(cip2bchlc[i].bc,
536 at_state->str_var[STR_ZBC])) {
537 iif->hcmsg.CIPValue = i;
541 /* no BC (internal call): assume CIP 1 (speech, A-law) */
542 iif->hcmsg.CIPValue = 1;
543 encode_ie(cip2bchlc[1].bc, iif->bc_buf, MAX_BC_OCTETS);
545 iif->hcmsg.BC = iif->bc_buf;
546 msgsize += iif->hcmsg.BC[0];
548 /* High Layer Compatibility (optional) */
549 if (at_state->str_var[STR_ZHLC]) {
550 /* pass on HLC from Gigaset */
551 if (encode_ie(at_state->str_var[STR_ZHLC], iif->hlc_buf,
552 MAX_HLC_OCTETS) < 0) {
553 dev_warn(cs->dev, "RING ignored - bad HLC %s\n",
554 at_state->str_var[STR_ZHLC]);
557 iif->hcmsg.HLC = iif->hlc_buf;
558 msgsize += iif->hcmsg.HLC[0];
560 /* look up corresponding CIP value */
561 /* keep BC based CIP value if none found */
562 if (at_state->str_var[STR_ZBC])
563 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
564 if (cip2bchlc[i].hlc != NULL &&
565 !strcmp(cip2bchlc[i].hlc,
566 at_state->str_var[STR_ZHLC]) &&
567 !strcmp(cip2bchlc[i].bc,
568 at_state->str_var[STR_ZBC])) {
569 iif->hcmsg.CIPValue = i;
574 /* Called Party Number (optional) */
575 if (at_state->str_var[STR_ZCPN]) {
576 i = strlen(at_state->str_var[STR_ZCPN]);
577 if (i > MAX_NUMBER_DIGITS) {
578 dev_warn(cs->dev, "RING ignored - bad number %s\n",
579 at_state->str_var[STR_ZBC]);
582 iif->cdpty_buf[0] = i + 1;
583 iif->cdpty_buf[1] = 0x80; /* type / numbering plan unknown */
584 memcpy(iif->cdpty_buf + 2, at_state->str_var[STR_ZCPN], i);
585 iif->hcmsg.CalledPartyNumber = iif->cdpty_buf;
586 msgsize += iif->hcmsg.CalledPartyNumber[0];
589 /* Calling Party Number (optional) */
590 if (at_state->str_var[STR_NMBR]) {
591 i = strlen(at_state->str_var[STR_NMBR]);
592 if (i > MAX_NUMBER_DIGITS) {
593 dev_warn(cs->dev, "RING ignored - bad number %s\n",
594 at_state->str_var[STR_ZBC]);
597 iif->cgpty_buf[0] = i + 2;
598 iif->cgpty_buf[1] = 0x00; /* type / numbering plan unknown */
599 iif->cgpty_buf[2] = 0x80; /* pres. allowed, not screened */
600 memcpy(iif->cgpty_buf + 3, at_state->str_var[STR_NMBR], i);
601 iif->hcmsg.CallingPartyNumber = iif->cgpty_buf;
602 msgsize += iif->hcmsg.CallingPartyNumber[0];
605 /* remaining parameters (not supported, always left NULL):
606 * - CalledPartySubaddress
607 * - CallingPartySubaddress
609 * - BChannelinformation
612 * - Facilitydataarray
615 gig_dbg(DEBUG_CMD, "icall: PLCI %x CIP %d BC %s",
616 iif->hcmsg.adr.adrPLCI, iif->hcmsg.CIPValue,
617 format_ie(iif->hcmsg.BC));
618 gig_dbg(DEBUG_CMD, "icall: HLC %s",
619 format_ie(iif->hcmsg.HLC));
620 gig_dbg(DEBUG_CMD, "icall: CgPty %s",
621 format_ie(iif->hcmsg.CallingPartyNumber));
622 gig_dbg(DEBUG_CMD, "icall: CdPty %s",
623 format_ie(iif->hcmsg.CalledPartyNumber));
625 /* scan application list for matching listeners */
626 spin_lock_irqsave(&bcs->aplock, flags);
627 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE) {
628 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
629 __func__, bcs->ap, bcs->apconnstate);
631 bcs->apconnstate = APCONN_NONE;
633 spin_unlock_irqrestore(&bcs->aplock, flags);
634 actCIPmask = 1 | (1 << iif->hcmsg.CIPValue);
635 list_for_each_entry(ap, &iif->appls, ctrlist)
636 if (actCIPmask & ap->listenCIPmask) {
637 /* build CONNECT_IND message for this application */
638 iif->hcmsg.ApplId = ap->id;
639 iif->hcmsg.Messagenumber = ap->nextMessageNumber++;
641 skb = alloc_skb(msgsize, GFP_ATOMIC);
643 dev_err(cs->dev, "%s: out of memory\n",
647 if (capi_cmsg2message(&iif->hcmsg,
648 __skb_put(skb, msgsize))) {
649 dev_err(cs->dev, "%s: message parser failure\n",
651 dev_kfree_skb_any(skb);
654 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
656 /* add to listeners on this B channel, update state */
657 spin_lock_irqsave(&bcs->aplock, flags);
658 ap->bcnext = bcs->ap;
660 bcs->chstate |= CHS_NOTIFY_LL;
661 bcs->apconnstate = APCONN_SETUP;
662 spin_unlock_irqrestore(&bcs->aplock, flags);
665 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
669 * Return "accept" if any listeners.
670 * Gigaset will send ALERTING.
671 * There doesn't seem to be a way to avoid this.
673 return bcs->ap ? ICALL_ACCEPT : ICALL_IGNORE;
677 * send a DISCONNECT_IND message to an application
678 * does not sleep, clobbers the controller's hcmsg structure
680 static void send_disconnect_ind(struct bc_state *bcs,
681 struct gigaset_capi_appl *ap, u16 reason)
683 struct cardstate *cs = bcs->cs;
684 struct gigaset_capi_ctr *iif = cs->iif;
687 if (bcs->apconnstate == APCONN_NONE)
690 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT, CAPI_IND,
691 ap->nextMessageNumber++,
692 iif->ctr.cnr | ((bcs->channel + 1) << 8));
693 iif->hcmsg.Reason = reason;
694 skb = alloc_skb(CAPI_DISCONNECT_IND_LEN, GFP_ATOMIC);
696 dev_err(cs->dev, "%s: out of memory\n", __func__);
699 if (capi_cmsg2message(&iif->hcmsg,
700 __skb_put(skb, CAPI_DISCONNECT_IND_LEN))) {
701 dev_err(cs->dev, "%s: message parser failure\n", __func__);
702 dev_kfree_skb_any(skb);
705 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
706 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
710 * send a DISCONNECT_B3_IND message to an application
711 * Parameters: NCCI = 1, NCPI empty, Reason_B3 = 0
712 * does not sleep, clobbers the controller's hcmsg structure
714 static void send_disconnect_b3_ind(struct bc_state *bcs,
715 struct gigaset_capi_appl *ap)
717 struct cardstate *cs = bcs->cs;
718 struct gigaset_capi_ctr *iif = cs->iif;
721 /* nothing to do if no logical connection active */
722 if (bcs->apconnstate < APCONN_ACTIVE)
724 bcs->apconnstate = APCONN_SETUP;
726 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
727 ap->nextMessageNumber++,
728 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
729 skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_ATOMIC);
731 dev_err(cs->dev, "%s: out of memory\n", __func__);
734 if (capi_cmsg2message(&iif->hcmsg,
735 __skb_put(skb, CAPI_DISCONNECT_B3_IND_BASELEN))) {
736 dev_err(cs->dev, "%s: message parser failure\n", __func__);
737 dev_kfree_skb_any(skb);
740 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
741 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
745 * gigaset_isdn_connD() - signal D channel connect
746 * @bcs: B channel descriptor structure.
748 * Called by main module at tasklet level to notify the LL that the D channel
749 * connection has been established.
751 void gigaset_isdn_connD(struct bc_state *bcs)
753 struct cardstate *cs = bcs->cs;
754 struct gigaset_capi_ctr *iif = cs->iif;
755 struct gigaset_capi_appl *ap;
757 unsigned int msgsize;
760 spin_lock_irqsave(&bcs->aplock, flags);
763 spin_unlock_irqrestore(&bcs->aplock, flags);
764 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
767 if (bcs->apconnstate == APCONN_NONE) {
768 spin_unlock_irqrestore(&bcs->aplock, flags);
769 dev_warn(cs->dev, "%s: application %u not connected\n",
773 spin_unlock_irqrestore(&bcs->aplock, flags);
775 /* this should never happen */
776 dev_warn(cs->dev, "%s: dropping extra application %u\n",
777 __func__, ap->bcnext->id);
778 send_disconnect_ind(bcs, ap->bcnext,
779 CapiCallGivenToOtherApplication);
780 ap->bcnext = ap->bcnext->bcnext;
783 /* prepare CONNECT_ACTIVE_IND message
784 * Note: LLC not supported by device
786 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_CONNECT_ACTIVE, CAPI_IND,
787 ap->nextMessageNumber++,
788 iif->ctr.cnr | ((bcs->channel + 1) << 8));
790 /* minimum size, all structs empty */
791 msgsize = CAPI_CONNECT_ACTIVE_IND_BASELEN;
793 /* ToDo: set parameter: Connected number
794 * (requires ev-layer state machine extension to collect
798 /* build and emit CONNECT_ACTIVE_IND message */
799 skb = alloc_skb(msgsize, GFP_ATOMIC);
801 dev_err(cs->dev, "%s: out of memory\n", __func__);
804 if (capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize))) {
805 dev_err(cs->dev, "%s: message parser failure\n", __func__);
806 dev_kfree_skb_any(skb);
809 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
810 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
814 * gigaset_isdn_hupD() - signal D channel hangup
815 * @bcs: B channel descriptor structure.
817 * Called by main module at tasklet level to notify the LL that the D channel
818 * connection has been shut down.
820 void gigaset_isdn_hupD(struct bc_state *bcs)
822 struct gigaset_capi_appl *ap;
826 * ToDo: pass on reason code reported by device
827 * (requires ev-layer state machine extension to collect
830 spin_lock_irqsave(&bcs->aplock, flags);
831 while (bcs->ap != NULL) {
833 bcs->ap = ap->bcnext;
834 spin_unlock_irqrestore(&bcs->aplock, flags);
835 send_disconnect_b3_ind(bcs, ap);
836 send_disconnect_ind(bcs, ap, 0);
837 spin_lock_irqsave(&bcs->aplock, flags);
839 bcs->apconnstate = APCONN_NONE;
840 spin_unlock_irqrestore(&bcs->aplock, flags);
844 * gigaset_isdn_connB() - signal B channel connect
845 * @bcs: B channel descriptor structure.
847 * Called by main module at tasklet level to notify the LL that the B channel
848 * connection has been established.
850 void gigaset_isdn_connB(struct bc_state *bcs)
852 struct cardstate *cs = bcs->cs;
853 struct gigaset_capi_ctr *iif = cs->iif;
854 struct gigaset_capi_appl *ap;
857 unsigned int msgsize;
860 spin_lock_irqsave(&bcs->aplock, flags);
863 spin_unlock_irqrestore(&bcs->aplock, flags);
864 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
867 if (!bcs->apconnstate) {
868 spin_unlock_irqrestore(&bcs->aplock, flags);
869 dev_warn(cs->dev, "%s: application %u not connected\n",
875 * emit CONNECT_B3_ACTIVE_IND if we already got CONNECT_B3_REQ;
876 * otherwise we have to emit CONNECT_B3_IND first, and follow up with
877 * CONNECT_B3_ACTIVE_IND in reply to CONNECT_B3_RESP
878 * Parameters in both cases always: NCCI = 1, NCPI empty
880 if (bcs->apconnstate >= APCONN_ACTIVE) {
881 command = CAPI_CONNECT_B3_ACTIVE;
882 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
884 command = CAPI_CONNECT_B3;
885 msgsize = CAPI_CONNECT_B3_IND_BASELEN;
887 bcs->apconnstate = APCONN_ACTIVE;
889 spin_unlock_irqrestore(&bcs->aplock, flags);
892 /* this should never happen */
893 dev_warn(cs->dev, "%s: dropping extra application %u\n",
894 __func__, ap->bcnext->id);
895 send_disconnect_ind(bcs, ap->bcnext,
896 CapiCallGivenToOtherApplication);
897 ap->bcnext = ap->bcnext->bcnext;
900 capi_cmsg_header(&iif->hcmsg, ap->id, command, CAPI_IND,
901 ap->nextMessageNumber++,
902 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
903 skb = alloc_skb(msgsize, GFP_ATOMIC);
905 dev_err(cs->dev, "%s: out of memory\n", __func__);
908 if (capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize))) {
909 dev_err(cs->dev, "%s: message parser failure\n", __func__);
910 dev_kfree_skb_any(skb);
913 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
914 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
918 * gigaset_isdn_hupB() - signal B channel hangup
919 * @bcs: B channel descriptor structure.
921 * Called by main module to notify the LL that the B channel connection has
924 void gigaset_isdn_hupB(struct bc_state *bcs)
926 struct gigaset_capi_appl *ap = bcs->ap;
928 /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
931 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
935 send_disconnect_b3_ind(bcs, ap);
939 * gigaset_isdn_start() - signal device availability
940 * @cs: device descriptor structure.
942 * Called by main module to notify the LL that the device is available for
945 void gigaset_isdn_start(struct cardstate *cs)
947 struct gigaset_capi_ctr *iif = cs->iif;
949 /* fill profile data: manufacturer name */
950 strcpy(iif->ctr.manu, "Siemens");
951 /* CAPI and device version */
952 iif->ctr.version.majorversion = 2; /* CAPI 2.0 */
953 iif->ctr.version.minorversion = 0;
954 /* ToDo: check/assert cs->gotfwver? */
955 iif->ctr.version.majormanuversion = cs->fwver[0];
956 iif->ctr.version.minormanuversion = cs->fwver[1];
957 /* number of B channels supported */
958 iif->ctr.profile.nbchannel = cs->channels;
959 /* global options: internal controller, supplementary services */
960 iif->ctr.profile.goptions = 0x11;
961 /* B1 protocols: 64 kbit/s HDLC or transparent */
962 iif->ctr.profile.support1 = 0x03;
963 /* B2 protocols: transparent only */
964 /* ToDo: X.75 SLP ? */
965 iif->ctr.profile.support2 = 0x02;
966 /* B3 protocols: transparent only */
967 iif->ctr.profile.support3 = 0x01;
968 /* no serial number */
969 strcpy(iif->ctr.serial, "0");
970 capi_ctr_ready(&iif->ctr);
974 * gigaset_isdn_stop() - signal device unavailability
975 * @cs: device descriptor structure.
977 * Called by main module to notify the LL that the device is no longer
980 void gigaset_isdn_stop(struct cardstate *cs)
982 struct gigaset_capi_ctr *iif = cs->iif;
983 capi_ctr_down(&iif->ctr);
987 * kernel CAPI callback methods
988 * ============================
992 * register CAPI application
994 static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
995 capi_register_params *rp)
997 struct gigaset_capi_ctr *iif
998 = container_of(ctr, struct gigaset_capi_ctr, ctr);
999 struct cardstate *cs = ctr->driverdata;
1000 struct gigaset_capi_appl *ap;
1002 gig_dbg(DEBUG_CMD, "%s [%u] l3cnt=%u blkcnt=%u blklen=%u",
1003 __func__, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
1005 list_for_each_entry(ap, &iif->appls, ctrlist)
1006 if (ap->id == appl) {
1008 "application %u already registered\n", appl);
1012 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
1014 dev_err(cs->dev, "%s: out of memory\n", __func__);
1020 list_add(&ap->ctrlist, &iif->appls);
1021 dev_info(cs->dev, "application %u registered\n", ap->id);
1025 * remove CAPI application from channel
1026 * helper function to keep indentation levels down and stay in 80 columns
1029 static inline void remove_appl_from_channel(struct bc_state *bcs,
1030 struct gigaset_capi_appl *ap)
1032 struct cardstate *cs = bcs->cs;
1033 struct gigaset_capi_appl *bcap;
1034 unsigned long flags;
1037 spin_lock_irqsave(&bcs->aplock, flags);
1040 spin_unlock_irqrestore(&bcs->aplock, flags);
1044 /* check first application on channel */
1046 bcs->ap = ap->bcnext;
1047 if (bcs->ap != NULL) {
1048 spin_unlock_irqrestore(&bcs->aplock, flags);
1052 /* none left, clear channel state */
1053 prevconnstate = bcs->apconnstate;
1054 bcs->apconnstate = APCONN_NONE;
1055 spin_unlock_irqrestore(&bcs->aplock, flags);
1057 if (prevconnstate == APCONN_ACTIVE) {
1058 dev_notice(cs->dev, "%s: hanging up channel %u\n",
1059 __func__, bcs->channel);
1060 gigaset_add_event(cs, &bcs->at_state,
1061 EV_HUP, NULL, 0, NULL);
1062 gigaset_schedule_event(cs);
1067 /* check remaining list */
1069 if (bcap->bcnext == ap) {
1070 bcap->bcnext = bcap->bcnext->bcnext;
1071 spin_unlock_irqrestore(&bcs->aplock, flags);
1074 bcap = bcap->bcnext;
1075 } while (bcap != NULL);
1076 spin_unlock_irqrestore(&bcs->aplock, flags);
1080 * release CAPI application
1082 static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
1084 struct gigaset_capi_ctr *iif
1085 = container_of(ctr, struct gigaset_capi_ctr, ctr);
1086 struct cardstate *cs = iif->ctr.driverdata;
1087 struct gigaset_capi_appl *ap, *tmp;
1090 gig_dbg(DEBUG_CMD, "%s [%u]", __func__, appl);
1092 list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
1093 if (ap->id == appl) {
1094 /* remove from any channels */
1095 for (ch = 0; ch < cs->channels; ch++)
1096 remove_appl_from_channel(&cs->bcs[ch], ap);
1098 /* remove from registration list */
1099 list_del(&ap->ctrlist);
1101 dev_info(cs->dev, "application %u released\n", appl);
1106 * =====================================================================
1107 * outgoing CAPI message handler
1108 * =====================================================================
1112 * helper function: emit reply message with given Info value
1114 static void send_conf(struct gigaset_capi_ctr *iif,
1115 struct gigaset_capi_appl *ap,
1116 struct sk_buff *skb,
1119 struct cardstate *cs = iif->ctr.driverdata;
1122 * _CONF replies always only have NCCI and Info parameters
1123 * so they'll fit into the _REQ message skb
1125 capi_cmsg_answer(&iif->acmsg);
1126 iif->acmsg.Info = info;
1127 if (capi_cmsg2message(&iif->acmsg, skb->data)) {
1128 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1129 dev_kfree_skb_any(skb);
1132 __skb_trim(skb, CAPI_STDCONF_LEN);
1133 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1134 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1138 * process FACILITY_REQ message
1140 static void do_facility_req(struct gigaset_capi_ctr *iif,
1141 struct gigaset_capi_appl *ap,
1142 struct sk_buff *skb)
1144 struct cardstate *cs = iif->ctr.driverdata;
1145 _cmsg *cmsg = &iif->acmsg;
1146 struct sk_buff *cskb;
1148 unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
1150 static u8 confparam[10]; /* max. 9 octets + length byte */
1152 /* decode message */
1153 if (capi_message2cmsg(cmsg, skb->data)) {
1154 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1155 dev_kfree_skb_any(skb);
1158 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1161 * Facility Request Parameter is not decoded by capi_message2cmsg()
1162 * encoding depends on Facility Selector
1164 switch (cmsg->FacilitySelector) {
1165 case CAPI_FACILITY_DTMF: /* ToDo */
1166 info = CapiFacilityNotSupported;
1167 confparam[0] = 2; /* length */
1168 /* DTMF information: Unknown DTMF request */
1169 capimsg_setu16(confparam, 1, 2);
1172 case CAPI_FACILITY_V42BIS: /* not supported */
1173 info = CapiFacilityNotSupported;
1174 confparam[0] = 2; /* length */
1175 /* V.42 bis information: not available */
1176 capimsg_setu16(confparam, 1, 1);
1179 case CAPI_FACILITY_SUPPSVC:
1180 /* decode Function parameter */
1181 pparam = cmsg->FacilityRequestParameter;
1182 if (pparam == NULL || pparam[0] < 2) {
1183 dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
1184 "Facility Request Parameter");
1185 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1188 function = CAPIMSG_U16(pparam, 1);
1190 case CAPI_SUPPSVC_GETSUPPORTED:
1192 /* Supplementary Service specific parameter */
1193 confparam[3] = 6; /* length */
1194 /* Supplementary services info: Success */
1195 capimsg_setu16(confparam, 4, CapiSuccess);
1196 /* Supported Services: none */
1197 capimsg_setu32(confparam, 6, 0);
1199 case CAPI_SUPPSVC_LISTEN:
1200 if (pparam[0] < 7 || pparam[3] < 4) {
1201 dev_notice(cs->dev, "%s: %s missing\n",
1202 "FACILITY_REQ", "Notification Mask");
1203 send_conf(iif, ap, skb,
1204 CapiIllMessageParmCoding);
1207 if (CAPIMSG_U32(pparam, 4) != 0) {
1209 "%s: unsupported supplementary service notification mask 0x%x\n",
1210 "FACILITY_REQ", CAPIMSG_U32(pparam, 4));
1211 info = CapiFacilitySpecificFunctionNotSupported;
1212 confparam[3] = 2; /* length */
1213 capimsg_setu16(confparam, 4,
1214 CapiSupplementaryServiceNotSupported);
1218 confparam[3] = 2; /* length */
1219 capimsg_setu16(confparam, 4, CapiSuccess);
1222 /* ToDo: add supported services */
1226 "%s: unsupported supplementary service function 0x%04x\n",
1227 "FACILITY_REQ", function);
1228 info = CapiFacilitySpecificFunctionNotSupported;
1229 /* Supplementary Service specific parameter */
1230 confparam[3] = 2; /* length */
1231 /* Supplementary services info: not supported */
1232 capimsg_setu16(confparam, 4,
1233 CapiSupplementaryServiceNotSupported);
1236 /* Facility confirmation parameter */
1237 confparam[0] = confparam[3] + 3; /* total length */
1238 /* Function: copy from _REQ message */
1239 capimsg_setu16(confparam, 1, function);
1240 /* Supplementary Service specific parameter already set above */
1243 case CAPI_FACILITY_WAKEUP: /* ToDo */
1244 info = CapiFacilityNotSupported;
1245 confparam[0] = 2; /* length */
1246 /* Number of accepted awake request parameters: 0 */
1247 capimsg_setu16(confparam, 1, 0);
1251 info = CapiFacilityNotSupported;
1252 confparam[0] = 0; /* empty struct */
1255 /* send FACILITY_CONF with given Info and confirmation parameter */
1256 dev_kfree_skb_any(skb);
1257 capi_cmsg_answer(cmsg);
1259 cmsg->FacilityConfirmationParameter = confparam;
1260 msgsize += confparam[0]; /* length */
1261 cskb = alloc_skb(msgsize, GFP_ATOMIC);
1263 dev_err(cs->dev, "%s: out of memory\n", __func__);
1266 if (capi_cmsg2message(cmsg, __skb_put(cskb, msgsize))) {
1267 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1268 dev_kfree_skb_any(cskb);
1271 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1272 capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
1277 * process LISTEN_REQ message
1278 * just store the masks in the application data structure
1280 static void do_listen_req(struct gigaset_capi_ctr *iif,
1281 struct gigaset_capi_appl *ap,
1282 struct sk_buff *skb)
1284 struct cardstate *cs = iif->ctr.driverdata;
1286 /* decode message */
1287 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
1288 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1289 dev_kfree_skb_any(skb);
1292 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1294 /* store listening parameters */
1295 ap->listenInfoMask = iif->acmsg.InfoMask;
1296 ap->listenCIPmask = iif->acmsg.CIPmask;
1297 send_conf(iif, ap, skb, CapiSuccess);
1301 * process ALERT_REQ message
1302 * nothing to do, Gigaset always alerts anyway
1304 static void do_alert_req(struct gigaset_capi_ctr *iif,
1305 struct gigaset_capi_appl *ap,
1306 struct sk_buff *skb)
1308 struct cardstate *cs = iif->ctr.driverdata;
1310 /* decode message */
1311 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
1312 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1313 dev_kfree_skb_any(skb);
1316 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1317 send_conf(iif, ap, skb, CapiAlertAlreadySent);
1321 * process CONNECT_REQ message
1322 * allocate a B channel, prepare dial commands, queue a DIAL event,
1323 * emit CONNECT_CONF reply
1325 static void do_connect_req(struct gigaset_capi_ctr *iif,
1326 struct gigaset_capi_appl *ap,
1327 struct sk_buff *skb)
1329 struct cardstate *cs = iif->ctr.driverdata;
1330 _cmsg *cmsg = &iif->acmsg;
1331 struct bc_state *bcs;
1335 unsigned long flags;
1336 int i, l, lbc, lhlc;
1339 /* decode message */
1340 if (capi_message2cmsg(cmsg, skb->data)) {
1341 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1342 dev_kfree_skb_any(skb);
1345 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1347 /* get free B channel & construct PLCI */
1348 bcs = gigaset_get_free_channel(cs);
1350 dev_notice(cs->dev, "%s: no B channel available\n",
1352 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1355 spin_lock_irqsave(&bcs->aplock, flags);
1356 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE)
1357 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
1358 __func__, bcs->ap, bcs->apconnstate);
1361 bcs->apconnstate = APCONN_SETUP;
1362 spin_unlock_irqrestore(&bcs->aplock, flags);
1364 bcs->rx_bufsize = ap->rp.datablklen;
1365 dev_kfree_skb(bcs->rx_skb);
1366 gigaset_new_rx_skb(bcs);
1367 cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1369 /* build command table */
1370 commands = kcalloc(AT_NUM, sizeof(*commands), GFP_KERNEL);
1374 /* encode parameter: Called party number */
1375 pp = cmsg->CalledPartyNumber;
1376 if (pp == NULL || *pp == 0) {
1377 dev_notice(cs->dev, "%s: %s missing\n",
1378 "CONNECT_REQ", "Called party number");
1379 info = CapiIllMessageParmCoding;
1383 /* check type of number/numbering plan byte */
1385 case 0x80: /* unknown type / unknown numbering plan */
1386 case 0x81: /* unknown type / ISDN/Telephony numbering plan */
1388 default: /* others: warn about potential misinterpretation */
1389 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1390 "CONNECT_REQ", "Called party number", *pp);
1394 /* translate "**" internal call prefix to CTP value */
1395 if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1402 commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1403 if (!commands[AT_TYPE])
1405 commands[AT_DIAL] = kmalloc(l + 3, GFP_KERNEL);
1406 if (!commands[AT_DIAL])
1408 snprintf(commands[AT_DIAL], l + 3, "D%.*s\r", l, pp);
1410 /* encode parameter: Calling party number */
1411 pp = cmsg->CallingPartyNumber;
1412 if (pp != NULL && *pp > 0) {
1415 /* check type of number/numbering plan byte */
1416 /* ToDo: allow for/handle Ext=1? */
1418 case 0x00: /* unknown type / unknown numbering plan */
1419 case 0x01: /* unknown type / ISDN/Telephony num. plan */
1423 "%s: %s type/plan 0x%02x unsupported\n",
1424 "CONNECT_REQ", "Calling party number", *pp);
1429 /* check presentation indicator */
1431 dev_notice(cs->dev, "%s: %s IE truncated\n",
1432 "CONNECT_REQ", "Calling party number");
1433 info = CapiIllMessageParmCoding;
1436 switch (*pp & 0xfc) { /* ignore Screening indicator */
1437 case 0x80: /* Presentation allowed */
1440 case 0xa0: /* Presentation restricted */
1444 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1446 "Presentation/Screening indicator",
1450 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1451 if (!commands[AT_CLIP])
1458 commands[AT_MSN] = kmalloc(l + 8, GFP_KERNEL);
1459 if (!commands[AT_MSN])
1461 snprintf(commands[AT_MSN], l + 8, "^SMSN=%*s\r", l, pp);
1465 /* check parameter: CIP Value */
1466 if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
1467 (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1468 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1469 "CONNECT_REQ", cmsg->CIPValue);
1470 info = CapiCipValueUnknown;
1475 * check/encode parameters: BC & HLC
1476 * must be encoded together as device doesn't accept HLC separately
1477 * explicit parameters override values derived from CIP
1480 /* determine lengths */
1481 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1482 lbc = 2 * cmsg->BC[0];
1483 else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
1484 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1487 if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
1488 lhlc = 2 * cmsg->HLC[0];
1489 else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1490 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1495 /* have BC: allocate and assemble command string */
1496 l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
1498 l += lhlc + 7; /* ";^SHLC=" + value */
1499 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1500 if (!commands[AT_BC])
1502 strcpy(commands[AT_BC], "^SBC=");
1503 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1504 decode_ie(cmsg->BC, commands[AT_BC] + 5);
1505 else /* BC derived from CIP */
1506 strcpy(commands[AT_BC] + 5,
1507 cip2bchlc[cmsg->CIPValue].bc);
1509 strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1510 if (cmsg->HLC && cmsg->HLC[0])
1511 /* HLC specified explicitly */
1512 decode_ie(cmsg->HLC,
1513 commands[AT_BC] + lbc + 12);
1514 else /* HLC derived from CIP */
1515 strcpy(commands[AT_BC] + lbc + 12,
1516 cip2bchlc[cmsg->CIPValue].hlc);
1518 strcpy(commands[AT_BC] + l - 2, "\r");
1522 dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1524 info = CapiIllMessageParmCoding; /* ? */
1529 /* check/encode parameter: B Protocol */
1530 if (cmsg->BProtocol == CAPI_DEFAULT) {
1531 bcs->proto2 = L2_HDLC;
1533 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1535 switch (cmsg->B1protocol) {
1537 bcs->proto2 = L2_HDLC;
1540 bcs->proto2 = L2_VOICE;
1544 "B1 Protocol %u unsupported, using Transparent\n",
1546 bcs->proto2 = L2_VOICE;
1548 if (cmsg->B2protocol != 1)
1550 "B2 Protocol %u unsupported, using Transparent\n",
1552 if (cmsg->B3protocol != 0)
1554 "B3 Protocol %u unsupported, using Transparent\n",
1556 ignore_cstruct_param(cs, cmsg->B1configuration,
1557 "CONNECT_REQ", "B1 Configuration");
1558 ignore_cstruct_param(cs, cmsg->B2configuration,
1559 "CONNECT_REQ", "B2 Configuration");
1560 ignore_cstruct_param(cs, cmsg->B3configuration,
1561 "CONNECT_REQ", "B3 Configuration");
1563 commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1564 if (!commands[AT_PROTO])
1566 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1568 /* ToDo: check/encode remaining parameters */
1569 ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
1570 "CONNECT_REQ", "Called pty subaddr");
1571 ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
1572 "CONNECT_REQ", "Calling pty subaddr");
1573 ignore_cstruct_param(cs, cmsg->LLC,
1574 "CONNECT_REQ", "LLC");
1575 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1576 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1577 "CONNECT_REQ", "B Channel Information");
1578 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1579 "CONNECT_REQ", "Keypad Facility");
1580 ignore_cstruct_param(cs, cmsg->Useruserdata,
1581 "CONNECT_REQ", "User-User Data");
1582 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1583 "CONNECT_REQ", "Facility Data Array");
1586 /* encode parameter: B channel to use */
1587 commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1588 if (!commands[AT_ISO])
1590 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1591 (unsigned) bcs->channel + 1);
1593 /* queue & schedule EV_DIAL event */
1594 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
1595 bcs->at_state.seq_index, NULL)) {
1596 info = CAPI_MSGOSRESOURCEERR;
1599 gigaset_schedule_event(cs);
1600 send_conf(iif, ap, skb, CapiSuccess);
1604 dev_err(cs->dev, "%s: out of memory\n", __func__);
1605 info = CAPI_MSGOSRESOURCEERR;
1608 for (i = 0; i < AT_NUM; i++)
1611 gigaset_free_channel(bcs);
1612 send_conf(iif, ap, skb, info);
1616 * process CONNECT_RESP message
1617 * checks protocol parameters and queues an ACCEPT or HUP event
1619 static void do_connect_resp(struct gigaset_capi_ctr *iif,
1620 struct gigaset_capi_appl *ap,
1621 struct sk_buff *skb)
1623 struct cardstate *cs = iif->ctr.driverdata;
1624 _cmsg *cmsg = &iif->acmsg;
1625 struct bc_state *bcs;
1626 struct gigaset_capi_appl *oap;
1627 unsigned long flags;
1630 /* decode message */
1631 if (capi_message2cmsg(cmsg, skb->data)) {
1632 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1633 dev_kfree_skb_any(skb);
1636 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1637 dev_kfree_skb_any(skb);
1639 /* extract and check channel number from PLCI */
1640 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1641 if (!channel || channel > cs->channels) {
1642 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1643 "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1646 bcs = cs->bcs + channel - 1;
1648 switch (cmsg->Reject) {
1649 case 0: /* Accept */
1650 /* drop all competing applications, keep only this one */
1651 spin_lock_irqsave(&bcs->aplock, flags);
1652 while (bcs->ap != NULL) {
1654 bcs->ap = oap->bcnext;
1656 spin_unlock_irqrestore(&bcs->aplock, flags);
1657 send_disconnect_ind(bcs, oap,
1658 CapiCallGivenToOtherApplication);
1659 spin_lock_irqsave(&bcs->aplock, flags);
1664 spin_unlock_irqrestore(&bcs->aplock, flags);
1666 bcs->rx_bufsize = ap->rp.datablklen;
1667 dev_kfree_skb(bcs->rx_skb);
1668 gigaset_new_rx_skb(bcs);
1669 bcs->chstate |= CHS_NOTIFY_LL;
1671 /* check/encode B channel protocol */
1672 if (cmsg->BProtocol == CAPI_DEFAULT) {
1673 bcs->proto2 = L2_HDLC;
1675 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1677 switch (cmsg->B1protocol) {
1679 bcs->proto2 = L2_HDLC;
1682 bcs->proto2 = L2_VOICE;
1686 "B1 Protocol %u unsupported, using Transparent\n",
1688 bcs->proto2 = L2_VOICE;
1690 if (cmsg->B2protocol != 1)
1692 "B2 Protocol %u unsupported, using Transparent\n",
1694 if (cmsg->B3protocol != 0)
1696 "B3 Protocol %u unsupported, using Transparent\n",
1698 ignore_cstruct_param(cs, cmsg->B1configuration,
1699 "CONNECT_RESP", "B1 Configuration");
1700 ignore_cstruct_param(cs, cmsg->B2configuration,
1701 "CONNECT_RESP", "B2 Configuration");
1702 ignore_cstruct_param(cs, cmsg->B3configuration,
1703 "CONNECT_RESP", "B3 Configuration");
1706 /* ToDo: check/encode remaining parameters */
1707 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
1708 "CONNECT_RESP", "Connected Number");
1709 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
1710 "CONNECT_RESP", "Connected Subaddress");
1711 ignore_cstruct_param(cs, cmsg->LLC,
1712 "CONNECT_RESP", "LLC");
1713 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1714 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1715 "CONNECT_RESP", "BChannel Information");
1716 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1717 "CONNECT_RESP", "Keypad Facility");
1718 ignore_cstruct_param(cs, cmsg->Useruserdata,
1719 "CONNECT_RESP", "User-User Data");
1720 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1721 "CONNECT_RESP", "Facility Data Array");
1725 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
1726 EV_ACCEPT, NULL, 0, NULL))
1728 gigaset_schedule_event(cs);
1731 case 1: /* Ignore */
1732 /* send DISCONNECT_IND to this application */
1733 send_disconnect_ind(bcs, ap, 0);
1735 /* remove it from the list of listening apps */
1736 spin_lock_irqsave(&bcs->aplock, flags);
1737 if (bcs->ap == ap) {
1738 bcs->ap = ap->bcnext;
1739 if (bcs->ap == NULL) {
1740 /* last one: stop ev-layer hupD notifications */
1741 bcs->apconnstate = APCONN_NONE;
1742 bcs->chstate &= ~CHS_NOTIFY_LL;
1744 spin_unlock_irqrestore(&bcs->aplock, flags);
1747 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1748 if (oap->bcnext == ap) {
1749 oap->bcnext = oap->bcnext->bcnext;
1750 spin_unlock_irqrestore(&bcs->aplock, flags);
1754 spin_unlock_irqrestore(&bcs->aplock, flags);
1755 dev_err(cs->dev, "%s: application %u not found\n",
1759 default: /* Reject */
1760 /* drop all competing applications, keep only this one */
1761 spin_lock_irqsave(&bcs->aplock, flags);
1762 while (bcs->ap != NULL) {
1764 bcs->ap = oap->bcnext;
1766 spin_unlock_irqrestore(&bcs->aplock, flags);
1767 send_disconnect_ind(bcs, oap,
1768 CapiCallGivenToOtherApplication);
1769 spin_lock_irqsave(&bcs->aplock, flags);
1774 spin_unlock_irqrestore(&bcs->aplock, flags);
1776 /* reject call - will trigger DISCONNECT_IND for this app */
1777 dev_info(cs->dev, "%s: Reject=%x\n",
1778 "CONNECT_RESP", cmsg->Reject);
1779 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
1780 EV_HUP, NULL, 0, NULL))
1782 gigaset_schedule_event(cs);
1788 * process CONNECT_B3_REQ message
1789 * build NCCI and emit CONNECT_B3_CONF reply
1791 static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1792 struct gigaset_capi_appl *ap,
1793 struct sk_buff *skb)
1795 struct cardstate *cs = iif->ctr.driverdata;
1796 _cmsg *cmsg = &iif->acmsg;
1797 struct bc_state *bcs;
1800 /* decode message */
1801 if (capi_message2cmsg(cmsg, skb->data)) {
1802 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1803 dev_kfree_skb_any(skb);
1806 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1808 /* extract and check channel number from PLCI */
1809 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1810 if (!channel || channel > cs->channels) {
1811 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1812 "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
1813 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1816 bcs = &cs->bcs[channel - 1];
1818 /* mark logical connection active */
1819 bcs->apconnstate = APCONN_ACTIVE;
1821 /* build NCCI: always 1 (one B3 connection only) */
1822 cmsg->adr.adrNCCI |= 1 << 16;
1824 /* NCPI parameter: not applicable for B3 Transparent */
1825 ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
1826 send_conf(iif, ap, skb,
1827 (cmsg->NCPI && cmsg->NCPI[0]) ?
1828 CapiNcpiNotSupportedByProtocol : CapiSuccess);
1832 * process CONNECT_B3_RESP message
1833 * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1834 * or queue EV_HUP and emit DISCONNECT_B3_IND.
1835 * The emitted message is always shorter than the received one,
1836 * allowing to reuse the skb.
1838 static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1839 struct gigaset_capi_appl *ap,
1840 struct sk_buff *skb)
1842 struct cardstate *cs = iif->ctr.driverdata;
1843 _cmsg *cmsg = &iif->acmsg;
1844 struct bc_state *bcs;
1846 unsigned int msgsize;
1849 /* decode message */
1850 if (capi_message2cmsg(cmsg, skb->data)) {
1851 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1852 dev_kfree_skb_any(skb);
1855 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1857 /* extract and check channel number and NCCI */
1858 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
1859 if (!channel || channel > cs->channels ||
1860 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
1861 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1862 "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
1863 dev_kfree_skb_any(skb);
1866 bcs = &cs->bcs[channel - 1];
1869 /* Reject: clear B3 connect received flag */
1870 bcs->apconnstate = APCONN_SETUP;
1872 /* trigger hangup, causing eventual DISCONNECT_IND */
1873 if (!gigaset_add_event(cs, &bcs->at_state,
1874 EV_HUP, NULL, 0, NULL)) {
1875 dev_kfree_skb_any(skb);
1878 gigaset_schedule_event(cs);
1880 /* emit DISCONNECT_B3_IND */
1881 command = CAPI_DISCONNECT_B3;
1882 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1885 * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1886 * we only send CONNECT_B3_IND if the B channel is up
1888 command = CAPI_CONNECT_B3_ACTIVE;
1889 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1891 capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1892 ap->nextMessageNumber++, cmsg->adr.adrNCCI);
1893 __skb_trim(skb, msgsize);
1894 if (capi_cmsg2message(cmsg, skb->data)) {
1895 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1896 dev_kfree_skb_any(skb);
1899 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1900 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1904 * process DISCONNECT_REQ message
1905 * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1906 * emit DISCONNECT_CONF reply
1908 static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1909 struct gigaset_capi_appl *ap,
1910 struct sk_buff *skb)
1912 struct cardstate *cs = iif->ctr.driverdata;
1913 _cmsg *cmsg = &iif->acmsg;
1914 struct bc_state *bcs;
1916 struct sk_buff *b3skb;
1919 /* decode message */
1920 if (capi_message2cmsg(cmsg, skb->data)) {
1921 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1922 dev_kfree_skb_any(skb);
1925 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1927 /* extract and check channel number from PLCI */
1928 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1929 if (!channel || channel > cs->channels) {
1930 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1931 "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
1932 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1935 bcs = cs->bcs + channel - 1;
1937 /* ToDo: process parameter: Additional info */
1938 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1939 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1940 "DISCONNECT_REQ", "B Channel Information");
1941 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1942 "DISCONNECT_REQ", "Keypad Facility");
1943 ignore_cstruct_param(cs, cmsg->Useruserdata,
1944 "DISCONNECT_REQ", "User-User Data");
1945 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1946 "DISCONNECT_REQ", "Facility Data Array");
1949 /* skip if DISCONNECT_IND already sent */
1950 if (!bcs->apconnstate)
1953 /* check for active logical connection */
1954 if (bcs->apconnstate >= APCONN_ACTIVE) {
1956 bcs->apconnstate = APCONN_SETUP;
1959 * emit DISCONNECT_B3_IND with cause 0x3301
1960 * use separate cmsg structure, as the content of iif->acmsg
1961 * is still needed for creating the _CONF message
1963 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1965 dev_err(cs->dev, "%s: out of memory\n", __func__);
1966 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1969 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1970 ap->nextMessageNumber++,
1971 cmsg->adr.adrPLCI | (1 << 16));
1972 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1973 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1974 if (b3skb == NULL) {
1975 dev_err(cs->dev, "%s: out of memory\n", __func__);
1976 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1980 if (capi_cmsg2message(b3cmsg,
1981 __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN))) {
1982 dev_err(cs->dev, "%s: message parser failure\n",
1985 dev_kfree_skb_any(b3skb);
1988 dump_cmsg(DEBUG_CMD, __func__, b3cmsg);
1990 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1993 /* trigger hangup, causing eventual DISCONNECT_IND */
1994 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
1995 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1998 gigaset_schedule_event(cs);
2001 send_conf(iif, ap, skb, CapiSuccess);
2005 * process DISCONNECT_B3_REQ message
2006 * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
2008 static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
2009 struct gigaset_capi_appl *ap,
2010 struct sk_buff *skb)
2012 struct cardstate *cs = iif->ctr.driverdata;
2013 _cmsg *cmsg = &iif->acmsg;
2014 struct bc_state *bcs;
2017 /* decode message */
2018 if (capi_message2cmsg(cmsg, skb->data)) {
2019 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2020 dev_kfree_skb_any(skb);
2023 dump_cmsg(DEBUG_CMD, __func__, cmsg);
2025 /* extract and check channel number and NCCI */
2026 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
2027 if (!channel || channel > cs->channels ||
2028 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
2029 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
2030 "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
2031 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
2034 bcs = &cs->bcs[channel - 1];
2036 /* reject if logical connection not active */
2037 if (bcs->apconnstate < APCONN_ACTIVE) {
2038 send_conf(iif, ap, skb,
2039 CapiMessageNotSupportedInCurrentState);
2043 /* trigger hangup, causing eventual DISCONNECT_B3_IND */
2044 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
2045 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2048 gigaset_schedule_event(cs);
2050 /* NCPI parameter: not applicable for B3 Transparent */
2051 ignore_cstruct_param(cs, cmsg->NCPI,
2052 "DISCONNECT_B3_REQ", "NCPI");
2053 send_conf(iif, ap, skb,
2054 (cmsg->NCPI && cmsg->NCPI[0]) ?
2055 CapiNcpiNotSupportedByProtocol : CapiSuccess);
2059 * process DATA_B3_REQ message
2061 static void do_data_b3_req(struct gigaset_capi_ctr *iif,
2062 struct gigaset_capi_appl *ap,
2063 struct sk_buff *skb)
2065 struct cardstate *cs = iif->ctr.driverdata;
2066 struct bc_state *bcs;
2067 int channel = CAPIMSG_PLCI_PART(skb->data);
2068 u16 ncci = CAPIMSG_NCCI_PART(skb->data);
2069 u16 msglen = CAPIMSG_LEN(skb->data);
2070 u16 datalen = CAPIMSG_DATALEN(skb->data);
2071 u16 flags = CAPIMSG_FLAGS(skb->data);
2072 u16 msgid = CAPIMSG_MSGID(skb->data);
2073 u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
2075 /* frequent message, avoid _cmsg overhead */
2076 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
2078 /* check parameters */
2079 if (channel == 0 || channel > cs->channels || ncci != 1) {
2080 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
2081 "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
2082 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
2085 bcs = &cs->bcs[channel - 1];
2086 if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
2087 dev_notice(cs->dev, "%s: unexpected length %d\n",
2088 "DATA_B3_REQ", msglen);
2089 if (msglen + datalen != skb->len)
2090 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
2091 "DATA_B3_REQ", msglen, datalen, skb->len);
2092 if (msglen + datalen > skb->len) {
2093 /* message too short for announced data length */
2094 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
2097 if (flags & CAPI_FLAGS_RESERVED) {
2098 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
2099 "DATA_B3_REQ", flags);
2100 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
2104 /* reject if logical connection not active */
2105 if (bcs->apconnstate < APCONN_ACTIVE) {
2106 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2110 /* pull CAPI message into link layer header */
2111 skb_reset_mac_header(skb);
2112 skb->mac_len = msglen;
2113 skb_pull(skb, msglen);
2115 /* pass to device-specific module */
2116 if (cs->ops->send_skb(bcs, skb) < 0) {
2117 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2122 * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
2123 * confirmation" bit is set; otherwise we have to send it now
2125 if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
2126 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
2127 flags ? CapiFlagsNotSupportedByProtocol
2132 * process RESET_B3_REQ message
2133 * just always reply "not supported by current protocol"
2135 static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
2136 struct gigaset_capi_appl *ap,
2137 struct sk_buff *skb)
2139 struct cardstate *cs = iif->ctr.driverdata;
2141 /* decode message */
2142 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
2143 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2144 dev_kfree_skb_any(skb);
2147 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2148 send_conf(iif, ap, skb,
2149 CapiResetProcedureNotSupportedByCurrentProtocol);
2153 * unsupported CAPI message handler
2155 static void do_unsupported(struct gigaset_capi_ctr *iif,
2156 struct gigaset_capi_appl *ap,
2157 struct sk_buff *skb)
2159 struct cardstate *cs = iif->ctr.driverdata;
2161 /* decode message */
2162 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
2163 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2164 dev_kfree_skb_any(skb);
2167 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2168 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2172 * CAPI message handler: no-op
2174 static void do_nothing(struct gigaset_capi_ctr *iif,
2175 struct gigaset_capi_appl *ap,
2176 struct sk_buff *skb)
2178 struct cardstate *cs = iif->ctr.driverdata;
2180 /* decode message */
2181 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
2182 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2183 dev_kfree_skb_any(skb);
2186 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2187 dev_kfree_skb_any(skb);
2190 static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
2191 struct gigaset_capi_appl *ap,
2192 struct sk_buff *skb)
2194 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
2195 dev_kfree_skb_any(skb);
2198 /* table of outgoing CAPI message handlers with lookup function */
2199 typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
2200 struct gigaset_capi_appl *,
2205 capi_send_handler_t handler;
2206 } capi_send_handler_table[] = {
2207 /* most frequent messages first for faster lookup */
2208 { CAPI_DATA_B3_REQ, do_data_b3_req },
2209 { CAPI_DATA_B3_RESP, do_data_b3_resp },
2211 { CAPI_ALERT_REQ, do_alert_req },
2212 { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
2213 { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
2214 { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
2215 { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
2216 { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
2217 { CAPI_CONNECT_REQ, do_connect_req },
2218 { CAPI_CONNECT_RESP, do_connect_resp },
2219 { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
2220 { CAPI_DISCONNECT_B3_RESP, do_nothing },
2221 { CAPI_DISCONNECT_REQ, do_disconnect_req },
2222 { CAPI_DISCONNECT_RESP, do_nothing },
2223 { CAPI_FACILITY_REQ, do_facility_req },
2224 { CAPI_FACILITY_RESP, do_nothing },
2225 { CAPI_LISTEN_REQ, do_listen_req },
2226 { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
2227 { CAPI_RESET_B3_REQ, do_reset_b3_req },
2228 { CAPI_RESET_B3_RESP, do_nothing },
2231 * ToDo: support overlap sending (requires ev-layer state
2232 * machine extension to generate additional ATD commands)
2234 { CAPI_INFO_REQ, do_unsupported },
2235 { CAPI_INFO_RESP, do_nothing },
2238 * ToDo: what's the proper response for these?
2240 { CAPI_MANUFACTURER_REQ, do_nothing },
2241 { CAPI_MANUFACTURER_RESP, do_nothing },
2244 /* look up handler */
2245 static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
2249 for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2250 if (capi_send_handler_table[i].cmd == cmd)
2251 return capi_send_handler_table[i].handler;
2257 * gigaset_send_message() - accept a CAPI message from an application
2258 * @ctr: controller descriptor structure.
2259 * @skb: CAPI message.
2261 * Return value: CAPI error code
2262 * Note: capidrv (and probably others, too) only uses the return value to
2263 * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2265 static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2267 struct gigaset_capi_ctr *iif
2268 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2269 struct cardstate *cs = ctr->driverdata;
2270 struct gigaset_capi_appl *ap;
2271 capi_send_handler_t handler;
2273 /* can only handle linear sk_buffs */
2274 if (skb_linearize(skb) < 0) {
2275 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2276 return CAPI_MSGOSRESOURCEERR;
2279 /* retrieve application data structure */
2280 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2282 dev_notice(cs->dev, "%s: application %u not registered\n",
2283 __func__, CAPIMSG_APPID(skb->data));
2284 return CAPI_ILLAPPNR;
2287 /* look up command */
2288 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2290 /* unknown/unsupported message type */
2291 if (printk_ratelimit())
2292 dev_notice(cs->dev, "%s: unsupported message %u\n",
2293 __func__, CAPIMSG_CMD(skb->data));
2294 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2298 if (atomic_add_return(1, &iif->sendqlen) > 1) {
2299 /* queue behind other messages */
2300 skb_queue_tail(&iif->sendqueue, skb);
2301 return CAPI_NOERROR;
2304 /* process message */
2305 handler(iif, ap, skb);
2307 /* process other messages arrived in the meantime */
2308 while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2309 skb = skb_dequeue(&iif->sendqueue);
2311 /* should never happen */
2312 dev_err(cs->dev, "%s: send queue empty\n", __func__);
2315 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2317 /* could that happen? */
2318 dev_warn(cs->dev, "%s: application %u vanished\n",
2319 __func__, CAPIMSG_APPID(skb->data));
2322 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2324 /* should never happen */
2325 dev_err(cs->dev, "%s: handler %x vanished\n",
2326 __func__, CAPIMSG_CMD(skb->data));
2329 handler(iif, ap, skb);
2332 return CAPI_NOERROR;
2336 * gigaset_procinfo() - build single line description for controller
2337 * @ctr: controller descriptor structure.
2339 * Return value: pointer to generated string (null terminated)
2341 static char *gigaset_procinfo(struct capi_ctr *ctr)
2343 return ctr->name; /* ToDo: more? */
2346 static int gigaset_proc_show(struct seq_file *m, void *v)
2348 struct capi_ctr *ctr = m->private;
2349 struct cardstate *cs = ctr->driverdata;
2353 seq_printf(m, "%-16s %s\n", "name", ctr->name);
2354 seq_printf(m, "%-16s %s %s\n", "dev",
2355 dev_driver_string(cs->dev), dev_name(cs->dev));
2356 seq_printf(m, "%-16s %d\n", "id", cs->myid);
2358 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
2359 cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
2360 seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2361 seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
2379 seq_printf(m, "%-16s %s\n", "mode", s);
2381 switch (cs->mstate) {
2382 case MS_UNINITIALIZED:
2383 s = "uninitialized";
2403 seq_printf(m, "%-16s %s\n", "mstate", s);
2405 seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2406 seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2407 seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2408 seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
2410 for (i = 0; i < cs->channels; i++) {
2411 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
2412 cs->bcs[i].corrupted);
2413 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
2414 cs->bcs[i].trans_down);
2415 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
2416 cs->bcs[i].trans_up);
2417 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
2418 cs->bcs[i].chstate);
2419 switch (cs->bcs[i].proto2) {
2432 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
2438 * gigaset_isdn_regdev() - register device to LL
2439 * @cs: device descriptor structure.
2440 * @isdnid: device name.
2442 * Return value: 0 on success, error code < 0 on failure
2444 int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
2446 struct gigaset_capi_ctr *iif;
2449 iif = kzalloc(sizeof(*iif), GFP_KERNEL);
2451 pr_err("%s: out of memory\n", __func__);
2455 /* prepare controller structure */
2456 iif->ctr.owner = THIS_MODULE;
2457 iif->ctr.driverdata = cs;
2458 strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name) - 1);
2459 iif->ctr.driver_name = "gigaset";
2460 iif->ctr.load_firmware = NULL;
2461 iif->ctr.reset_ctr = NULL;
2462 iif->ctr.register_appl = gigaset_register_appl;
2463 iif->ctr.release_appl = gigaset_release_appl;
2464 iif->ctr.send_message = gigaset_send_message;
2465 iif->ctr.procinfo = gigaset_procinfo;
2466 iif->ctr.proc_show = gigaset_proc_show,
2467 INIT_LIST_HEAD(&iif->appls);
2468 skb_queue_head_init(&iif->sendqueue);
2469 atomic_set(&iif->sendqlen, 0);
2471 /* register controller with CAPI */
2472 rc = attach_capi_ctr(&iif->ctr);
2474 pr_err("attach_capi_ctr failed (%d)\n", rc);
2480 cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
2485 * gigaset_isdn_unregdev() - unregister device from LL
2486 * @cs: device descriptor structure.
2488 void gigaset_isdn_unregdev(struct cardstate *cs)
2490 struct gigaset_capi_ctr *iif = cs->iif;
2492 detach_capi_ctr(&iif->ctr);
2497 static struct capi_driver capi_driver_gigaset = {
2503 * gigaset_isdn_regdrv() - register driver to LL
2505 void gigaset_isdn_regdrv(void)
2507 pr_info("Kernel CAPI interface\n");
2508 register_capi_driver(&capi_driver_gigaset);
2512 * gigaset_isdn_unregdrv() - unregister driver from LL
2514 void gigaset_isdn_unregdrv(void)
2516 unregister_capi_driver(&capi_driver_gigaset);