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