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