solos-pci: Fix race condition in tasklet RX handling
[GitHub/mt8127/android_kernel_alcatel_ttab.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;
1055 return;
1056 }
1057 bcap = bcap->bcnext;
1058 } while (bcap != NULL);
1059 spin_unlock_irqrestore(&bcs->aplock, flags);
7bb5fdc2
TS
1060}
1061
1062/*
1063 * release CAPI application
1064 */
1065static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
1066{
1067 struct gigaset_capi_ctr *iif
1068 = container_of(ctr, struct gigaset_capi_ctr, ctr);
1069 struct cardstate *cs = iif->ctr.driverdata;
1070 struct gigaset_capi_appl *ap, *tmp;
1b4843c5 1071 unsigned ch;
7bb5fdc2 1072
6a75342a
TS
1073 gig_dbg(DEBUG_CMD, "%s [%u]", __func__, appl);
1074
7bb5fdc2
TS
1075 list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
1076 if (ap->id == appl) {
1b4843c5
TS
1077 /* remove from any channels */
1078 for (ch = 0; ch < cs->channels; ch++)
1079 remove_appl_from_channel(&cs->bcs[ch], ap);
1080
1081 /* remove from registration list */
7bb5fdc2
TS
1082 list_del(&ap->ctrlist);
1083 kfree(ap);
1b4843c5 1084 dev_info(cs->dev, "application %u released\n", appl);
7bb5fdc2 1085 }
7bb5fdc2
TS
1086}
1087
1088/*
1089 * =====================================================================
1090 * outgoing CAPI message handler
1091 * =====================================================================
1092 */
1093
1094/*
1095 * helper function: emit reply message with given Info value
1096 */
1097static void send_conf(struct gigaset_capi_ctr *iif,
1098 struct gigaset_capi_appl *ap,
1099 struct sk_buff *skb,
1100 u16 info)
1101{
1102 /*
1103 * _CONF replies always only have NCCI and Info parameters
1104 * so they'll fit into the _REQ message skb
1105 */
1106 capi_cmsg_answer(&iif->acmsg);
1107 iif->acmsg.Info = info;
1108 capi_cmsg2message(&iif->acmsg, skb->data);
1109 __skb_trim(skb, CAPI_STDCONF_LEN);
1110 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1111 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1112}
1113
1114/*
1115 * process FACILITY_REQ message
1116 */
1117static void do_facility_req(struct gigaset_capi_ctr *iif,
1118 struct gigaset_capi_appl *ap,
1119 struct sk_buff *skb)
1120{
1121 struct cardstate *cs = iif->ctr.driverdata;
6c911916 1122 _cmsg *cmsg = &iif->acmsg;
7bb5fdc2
TS
1123 struct sk_buff *cskb;
1124 u8 *pparam;
1125 unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
1126 u16 function, info;
1127 static u8 confparam[10]; /* max. 9 octets + length byte */
1128
1129 /* decode message */
6c911916
TS
1130 capi_message2cmsg(cmsg, skb->data);
1131 dump_cmsg(DEBUG_CMD, __func__, cmsg);
7bb5fdc2
TS
1132
1133 /*
1134 * Facility Request Parameter is not decoded by capi_message2cmsg()
1135 * encoding depends on Facility Selector
1136 */
6c911916 1137 switch (cmsg->FacilitySelector) {
7bb5fdc2
TS
1138 case CAPI_FACILITY_DTMF: /* ToDo */
1139 info = CapiFacilityNotSupported;
1140 confparam[0] = 2; /* length */
1141 /* DTMF information: Unknown DTMF request */
1142 capimsg_setu16(confparam, 1, 2);
1143 break;
1144
1145 case CAPI_FACILITY_V42BIS: /* not supported */
1146 info = CapiFacilityNotSupported;
1147 confparam[0] = 2; /* length */
1148 /* V.42 bis information: not available */
1149 capimsg_setu16(confparam, 1, 1);
1150 break;
1151
1152 case CAPI_FACILITY_SUPPSVC:
1153 /* decode Function parameter */
6c911916 1154 pparam = cmsg->FacilityRequestParameter;
18c2259c 1155 if (pparam == NULL || pparam[0] < 2) {
7bb5fdc2
TS
1156 dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
1157 "Facility Request Parameter");
1158 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1159 return;
1160 }
1161 function = CAPIMSG_U16(pparam, 1);
1162 switch (function) {
1163 case CAPI_SUPPSVC_GETSUPPORTED:
1164 info = CapiSuccess;
1165 /* Supplementary Service specific parameter */
1166 confparam[3] = 6; /* length */
1167 /* Supplementary services info: Success */
1168 capimsg_setu16(confparam, 4, CapiSuccess);
1169 /* Supported Services: none */
1170 capimsg_setu32(confparam, 6, 0);
1171 break;
18c2259c
TS
1172 case CAPI_SUPPSVC_LISTEN:
1173 if (pparam[0] < 7 || pparam[3] < 4) {
1174 dev_notice(cs->dev, "%s: %s missing\n",
1175 "FACILITY_REQ", "Notification Mask");
1176 send_conf(iif, ap, skb,
1177 CapiIllMessageParmCoding);
1178 return;
1179 }
1180 if (CAPIMSG_U32(pparam, 4) != 0) {
1181 dev_notice(cs->dev,
1182 "%s: unsupported supplementary service notification mask 0x%x\n",
1183 "FACILITY_REQ", CAPIMSG_U32(pparam, 4));
1184 info = CapiFacilitySpecificFunctionNotSupported;
1185 confparam[3] = 2; /* length */
1186 capimsg_setu16(confparam, 4,
1187 CapiSupplementaryServiceNotSupported);
1188 }
1189 info = CapiSuccess;
1190 confparam[3] = 2; /* length */
1191 capimsg_setu16(confparam, 4, CapiSuccess);
1192 break;
7bb5fdc2
TS
1193 /* ToDo: add supported services */
1194 default:
18c2259c
TS
1195 dev_notice(cs->dev,
1196 "%s: unsupported supplementary service function 0x%04x\n",
1197 "FACILITY_REQ", function);
7bb5fdc2
TS
1198 info = CapiFacilitySpecificFunctionNotSupported;
1199 /* Supplementary Service specific parameter */
1200 confparam[3] = 2; /* length */
1201 /* Supplementary services info: not supported */
1202 capimsg_setu16(confparam, 4,
1203 CapiSupplementaryServiceNotSupported);
1204 }
1205
1206 /* Facility confirmation parameter */
1207 confparam[0] = confparam[3] + 3; /* total length */
1208 /* Function: copy from _REQ message */
1209 capimsg_setu16(confparam, 1, function);
1210 /* Supplementary Service specific parameter already set above */
1211 break;
1212
1213 case CAPI_FACILITY_WAKEUP: /* ToDo */
1214 info = CapiFacilityNotSupported;
1215 confparam[0] = 2; /* length */
1216 /* Number of accepted awake request parameters: 0 */
1217 capimsg_setu16(confparam, 1, 0);
1218 break;
1219
1220 default:
1221 info = CapiFacilityNotSupported;
1222 confparam[0] = 0; /* empty struct */
1223 }
1224
1225 /* send FACILITY_CONF with given Info and confirmation parameter */
6c911916
TS
1226 capi_cmsg_answer(cmsg);
1227 cmsg->Info = info;
1228 cmsg->FacilityConfirmationParameter = confparam;
7bb5fdc2
TS
1229 msgsize += confparam[0]; /* length */
1230 cskb = alloc_skb(msgsize, GFP_ATOMIC);
1231 if (!cskb) {
1232 dev_err(cs->dev, "%s: out of memory\n", __func__);
1233 return;
1234 }
6c911916
TS
1235 capi_cmsg2message(cmsg, __skb_put(cskb, msgsize));
1236 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1237 capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
7bb5fdc2
TS
1238}
1239
1240
1241/*
1242 * process LISTEN_REQ message
1243 * just store the masks in the application data structure
1244 */
1245static void do_listen_req(struct gigaset_capi_ctr *iif,
1246 struct gigaset_capi_appl *ap,
1247 struct sk_buff *skb)
1248{
1249 /* decode message */
1250 capi_message2cmsg(&iif->acmsg, skb->data);
1251 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1252
1253 /* store listening parameters */
1254 ap->listenInfoMask = iif->acmsg.InfoMask;
1255 ap->listenCIPmask = iif->acmsg.CIPmask;
1256 send_conf(iif, ap, skb, CapiSuccess);
1257}
1258
1259/*
1260 * process ALERT_REQ message
1261 * nothing to do, Gigaset always alerts anyway
1262 */
1263static void do_alert_req(struct gigaset_capi_ctr *iif,
1264 struct gigaset_capi_appl *ap,
1265 struct sk_buff *skb)
1266{
1267 /* decode message */
1268 capi_message2cmsg(&iif->acmsg, skb->data);
1269 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1270 send_conf(iif, ap, skb, CapiAlertAlreadySent);
1271}
1272
1273/*
1274 * process CONNECT_REQ message
1275 * allocate a B channel, prepare dial commands, queue a DIAL event,
1276 * emit CONNECT_CONF reply
1277 */
1278static void do_connect_req(struct gigaset_capi_ctr *iif,
1279 struct gigaset_capi_appl *ap,
1280 struct sk_buff *skb)
1281{
1282 struct cardstate *cs = iif->ctr.driverdata;
1283 _cmsg *cmsg = &iif->acmsg;
1284 struct bc_state *bcs;
1285 char **commands;
1286 char *s;
1287 u8 *pp;
1b4843c5 1288 unsigned long flags;
1ce368ff 1289 int i, l, lbc, lhlc;
7bb5fdc2
TS
1290 u16 info;
1291
1292 /* decode message */
6c911916
TS
1293 capi_message2cmsg(cmsg, skb->data);
1294 dump_cmsg(DEBUG_CMD, __func__, cmsg);
7bb5fdc2
TS
1295
1296 /* get free B channel & construct PLCI */
1297 bcs = gigaset_get_free_channel(cs);
1298 if (!bcs) {
1299 dev_notice(cs->dev, "%s: no B channel available\n",
1300 "CONNECT_REQ");
1301 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1302 return;
1303 }
1b4843c5
TS
1304 spin_lock_irqsave(&bcs->aplock, flags);
1305 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE)
1306 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
1307 __func__, bcs->ap, bcs->apconnstate);
7bb5fdc2
TS
1308 ap->bcnext = NULL;
1309 bcs->ap = ap;
1b4843c5
TS
1310 bcs->apconnstate = APCONN_SETUP;
1311 spin_unlock_irqrestore(&bcs->aplock, flags);
1312
e7752ee2
TS
1313 bcs->rx_bufsize = ap->rp.datablklen;
1314 dev_kfree_skb(bcs->rx_skb);
1315 gigaset_new_rx_skb(bcs);
7bb5fdc2
TS
1316 cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1317
1318 /* build command table */
1319 commands = kzalloc(AT_NUM*(sizeof *commands), GFP_KERNEL);
1320 if (!commands)
1321 goto oom;
1322
1323 /* encode parameter: Called party number */
1324 pp = cmsg->CalledPartyNumber;
1325 if (pp == NULL || *pp == 0) {
1326 dev_notice(cs->dev, "%s: %s missing\n",
1327 "CONNECT_REQ", "Called party number");
1328 info = CapiIllMessageParmCoding;
1329 goto error;
1330 }
1331 l = *pp++;
1332 /* check type of number/numbering plan byte */
1333 switch (*pp) {
1334 case 0x80: /* unknown type / unknown numbering plan */
1335 case 0x81: /* unknown type / ISDN/Telephony numbering plan */
1336 break;
1337 default: /* others: warn about potential misinterpretation */
1338 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1339 "CONNECT_REQ", "Called party number", *pp);
1340 }
1341 pp++;
1342 l--;
1343 /* translate "**" internal call prefix to CTP value */
1344 if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1345 s = "^SCTP=0\r";
1346 pp += 2;
1347 l -= 2;
1348 } else {
1349 s = "^SCTP=1\r";
1350 }
1351 commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1352 if (!commands[AT_TYPE])
1353 goto oom;
1354 commands[AT_DIAL] = kmalloc(l+3, GFP_KERNEL);
1355 if (!commands[AT_DIAL])
1356 goto oom;
22077ebc 1357 snprintf(commands[AT_DIAL], l+3, "D%.*s\r", l, pp);
7bb5fdc2
TS
1358
1359 /* encode parameter: Calling party number */
1360 pp = cmsg->CallingPartyNumber;
1361 if (pp != NULL && *pp > 0) {
1362 l = *pp++;
1363
1364 /* check type of number/numbering plan byte */
1365 /* ToDo: allow for/handle Ext=1? */
1366 switch (*pp) {
1367 case 0x00: /* unknown type / unknown numbering plan */
1368 case 0x01: /* unknown type / ISDN/Telephony num. plan */
1369 break;
1370 default:
1371 dev_notice(cs->dev,
1372 "%s: %s type/plan 0x%02x unsupported\n",
1373 "CONNECT_REQ", "Calling party number", *pp);
1374 }
1375 pp++;
1376 l--;
1377
1378 /* check presentation indicator */
1379 if (!l) {
1380 dev_notice(cs->dev, "%s: %s IE truncated\n",
1381 "CONNECT_REQ", "Calling party number");
1382 info = CapiIllMessageParmCoding;
1383 goto error;
1384 }
1385 switch (*pp & 0xfc) { /* ignore Screening indicator */
1386 case 0x80: /* Presentation allowed */
1387 s = "^SCLIP=1\r";
1388 break;
1389 case 0xa0: /* Presentation restricted */
1390 s = "^SCLIP=0\r";
1391 break;
1392 default:
1393 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1394 "CONNECT_REQ",
1395 "Presentation/Screening indicator",
1396 *pp);
1397 s = "^SCLIP=1\r";
1398 }
1399 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1400 if (!commands[AT_CLIP])
1401 goto oom;
1402 pp++;
1403 l--;
1404
1405 if (l) {
1406 /* number */
1407 commands[AT_MSN] = kmalloc(l+8, GFP_KERNEL);
1408 if (!commands[AT_MSN])
1409 goto oom;
1410 snprintf(commands[AT_MSN], l+8, "^SMSN=%*s\r", l, pp);
1411 }
1412 }
1413
1414 /* check parameter: CIP Value */
6ad34145 1415 if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
7bb5fdc2
TS
1416 (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1417 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1418 "CONNECT_REQ", cmsg->CIPValue);
1419 info = CapiCipValueUnknown;
1420 goto error;
1421 }
1422
1ce368ff
TS
1423 /*
1424 * check/encode parameters: BC & HLC
1425 * must be encoded together as device doesn't accept HLC separately
1426 * explicit parameters override values derived from CIP
1427 */
1428
1429 /* determine lengths */
1430 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1431 lbc = 2*cmsg->BC[0];
1432 else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
1433 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1434 else /* no BC */
1435 lbc = 0;
1436 if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
1437 lhlc = 2*cmsg->HLC[0];
1438 else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1439 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1440 else /* no HLC */
1441 lhlc = 0;
1442
1443 if (lbc) {
1444 /* have BC: allocate and assemble command string */
1445 l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
1446 if (lhlc)
1447 l += lhlc + 7; /* ";^SHLC=" + value */
7bb5fdc2
TS
1448 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1449 if (!commands[AT_BC])
1450 goto oom;
1451 strcpy(commands[AT_BC], "^SBC=");
1ce368ff
TS
1452 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1453 decode_ie(cmsg->BC, commands[AT_BC] + 5);
1454 else /* BC derived from CIP */
1455 strcpy(commands[AT_BC] + 5,
1456 cip2bchlc[cmsg->CIPValue].bc);
1457 if (lhlc) {
1458 strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1459 if (cmsg->HLC && cmsg->HLC[0])
1460 /* HLC specified explicitly */
1461 decode_ie(cmsg->HLC,
1462 commands[AT_BC] + lbc + 12);
1463 else /* HLC derived from CIP */
1464 strcpy(commands[AT_BC] + lbc + 12,
1465 cip2bchlc[cmsg->CIPValue].hlc);
1466 }
7bb5fdc2 1467 strcpy(commands[AT_BC] + l - 2, "\r");
1ce368ff
TS
1468 } else {
1469 /* no BC */
1470 if (lhlc) {
1471 dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1472 "CONNECT_REQ");
1473 info = CapiIllMessageParmCoding; /* ? */
1474 goto error;
1475 }
7bb5fdc2
TS
1476 }
1477
1478 /* check/encode parameter: B Protocol */
1479 if (cmsg->BProtocol == CAPI_DEFAULT) {
1480 bcs->proto2 = L2_HDLC;
1481 dev_warn(cs->dev,
1482 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1483 } else {
1484 switch (cmsg->B1protocol) {
1485 case 0:
1486 bcs->proto2 = L2_HDLC;
1487 break;
1488 case 1:
278a5829 1489 bcs->proto2 = L2_VOICE;
7bb5fdc2
TS
1490 break;
1491 default:
1492 dev_warn(cs->dev,
1493 "B1 Protocol %u unsupported, using Transparent\n",
1494 cmsg->B1protocol);
278a5829 1495 bcs->proto2 = L2_VOICE;
7bb5fdc2
TS
1496 }
1497 if (cmsg->B2protocol != 1)
1498 dev_warn(cs->dev,
1499 "B2 Protocol %u unsupported, using Transparent\n",
1500 cmsg->B2protocol);
1501 if (cmsg->B3protocol != 0)
1502 dev_warn(cs->dev,
1503 "B3 Protocol %u unsupported, using Transparent\n",
1504 cmsg->B3protocol);
1505 ignore_cstruct_param(cs, cmsg->B1configuration,
1506 "CONNECT_REQ", "B1 Configuration");
1507 ignore_cstruct_param(cs, cmsg->B2configuration,
1508 "CONNECT_REQ", "B2 Configuration");
1509 ignore_cstruct_param(cs, cmsg->B3configuration,
1510 "CONNECT_REQ", "B3 Configuration");
1511 }
1512 commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1513 if (!commands[AT_PROTO])
1514 goto oom;
1515 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1516
1517 /* ToDo: check/encode remaining parameters */
1518 ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
1519 "CONNECT_REQ", "Called pty subaddr");
1520 ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
1521 "CONNECT_REQ", "Calling pty subaddr");
1522 ignore_cstruct_param(cs, cmsg->LLC,
1523 "CONNECT_REQ", "LLC");
6c911916
TS
1524 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1525 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1526 "CONNECT_REQ", "B Channel Information");
1527 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1528 "CONNECT_REQ", "Keypad Facility");
1529 ignore_cstruct_param(cs, cmsg->Useruserdata,
1530 "CONNECT_REQ", "User-User Data");
1531 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1532 "CONNECT_REQ", "Facility Data Array");
1533 }
7bb5fdc2
TS
1534
1535 /* encode parameter: B channel to use */
1536 commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1537 if (!commands[AT_ISO])
1538 goto oom;
1539 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1540 (unsigned) bcs->channel + 1);
1541
1542 /* queue & schedule EV_DIAL event */
1543 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
1528b18f
TS
1544 bcs->at_state.seq_index, NULL)) {
1545 info = CAPI_MSGOSRESOURCEERR;
1546 goto error;
1547 }
7bb5fdc2 1548 gigaset_schedule_event(cs);
7bb5fdc2
TS
1549 send_conf(iif, ap, skb, CapiSuccess);
1550 return;
1551
1552oom:
1553 dev_err(cs->dev, "%s: out of memory\n", __func__);
1554 info = CAPI_MSGOSRESOURCEERR;
1555error:
1556 if (commands)
1557 for (i = 0; i < AT_NUM; i++)
1558 kfree(commands[i]);
1559 kfree(commands);
1560 gigaset_free_channel(bcs);
1561 send_conf(iif, ap, skb, info);
1562}
1563
1564/*
1565 * process CONNECT_RESP message
1566 * checks protocol parameters and queues an ACCEPT or HUP event
1567 */
1568static void do_connect_resp(struct gigaset_capi_ctr *iif,
1569 struct gigaset_capi_appl *ap,
1570 struct sk_buff *skb)
1571{
1572 struct cardstate *cs = iif->ctr.driverdata;
1573 _cmsg *cmsg = &iif->acmsg;
1574 struct bc_state *bcs;
1575 struct gigaset_capi_appl *oap;
1b4843c5 1576 unsigned long flags;
7bb5fdc2
TS
1577 int channel;
1578
1579 /* decode message */
6c911916
TS
1580 capi_message2cmsg(cmsg, skb->data);
1581 dump_cmsg(DEBUG_CMD, __func__, cmsg);
4dd8230a 1582 dev_kfree_skb_any(skb);
7bb5fdc2
TS
1583
1584 /* extract and check channel number from PLCI */
1585 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1586 if (!channel || channel > cs->channels) {
1587 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1588 "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1589 return;
1590 }
1591 bcs = cs->bcs + channel - 1;
1592
1593 switch (cmsg->Reject) {
1594 case 0: /* Accept */
1595 /* drop all competing applications, keep only this one */
1b4843c5
TS
1596 spin_lock_irqsave(&bcs->aplock, flags);
1597 while (bcs->ap != NULL) {
1598 oap = bcs->ap;
1599 bcs->ap = oap->bcnext;
1600 if (oap != ap) {
1601 spin_unlock_irqrestore(&bcs->aplock, flags);
7bb5fdc2
TS
1602 send_disconnect_ind(bcs, oap,
1603 CapiCallGivenToOtherApplication);
1b4843c5
TS
1604 spin_lock_irqsave(&bcs->aplock, flags);
1605 }
1606 }
7bb5fdc2
TS
1607 ap->bcnext = NULL;
1608 bcs->ap = ap;
1b4843c5
TS
1609 spin_unlock_irqrestore(&bcs->aplock, flags);
1610
e7752ee2
TS
1611 bcs->rx_bufsize = ap->rp.datablklen;
1612 dev_kfree_skb(bcs->rx_skb);
1613 gigaset_new_rx_skb(bcs);
7bb5fdc2
TS
1614 bcs->chstate |= CHS_NOTIFY_LL;
1615
1616 /* check/encode B channel protocol */
1617 if (cmsg->BProtocol == CAPI_DEFAULT) {
1618 bcs->proto2 = L2_HDLC;
1619 dev_warn(cs->dev,
1620 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
1621 } else {
1622 switch (cmsg->B1protocol) {
1623 case 0:
1624 bcs->proto2 = L2_HDLC;
1625 break;
1626 case 1:
278a5829 1627 bcs->proto2 = L2_VOICE;
7bb5fdc2
TS
1628 break;
1629 default:
1630 dev_warn(cs->dev,
1631 "B1 Protocol %u unsupported, using Transparent\n",
1632 cmsg->B1protocol);
278a5829 1633 bcs->proto2 = L2_VOICE;
7bb5fdc2
TS
1634 }
1635 if (cmsg->B2protocol != 1)
1636 dev_warn(cs->dev,
1637 "B2 Protocol %u unsupported, using Transparent\n",
1638 cmsg->B2protocol);
1639 if (cmsg->B3protocol != 0)
1640 dev_warn(cs->dev,
1641 "B3 Protocol %u unsupported, using Transparent\n",
1642 cmsg->B3protocol);
1643 ignore_cstruct_param(cs, cmsg->B1configuration,
1644 "CONNECT_RESP", "B1 Configuration");
1645 ignore_cstruct_param(cs, cmsg->B2configuration,
1646 "CONNECT_RESP", "B2 Configuration");
1647 ignore_cstruct_param(cs, cmsg->B3configuration,
1648 "CONNECT_RESP", "B3 Configuration");
1649 }
1650
1651 /* ToDo: check/encode remaining parameters */
1652 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
1653 "CONNECT_RESP", "Connected Number");
1654 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
1655 "CONNECT_RESP", "Connected Subaddress");
1656 ignore_cstruct_param(cs, cmsg->LLC,
1657 "CONNECT_RESP", "LLC");
6c911916
TS
1658 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1659 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1660 "CONNECT_RESP", "BChannel Information");
1661 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1662 "CONNECT_RESP", "Keypad Facility");
1663 ignore_cstruct_param(cs, cmsg->Useruserdata,
1664 "CONNECT_RESP", "User-User Data");
1665 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1666 "CONNECT_RESP", "Facility Data Array");
1667 }
7bb5fdc2
TS
1668
1669 /* Accept call */
1670 if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1671 EV_ACCEPT, NULL, 0, NULL))
1672 return;
7bb5fdc2
TS
1673 gigaset_schedule_event(cs);
1674 return;
1675
1676 case 1: /* Ignore */
1677 /* send DISCONNECT_IND to this application */
1678 send_disconnect_ind(bcs, ap, 0);
1679
1680 /* remove it from the list of listening apps */
1b4843c5 1681 spin_lock_irqsave(&bcs->aplock, flags);
7bb5fdc2
TS
1682 if (bcs->ap == ap) {
1683 bcs->ap = ap->bcnext;
1b4843c5 1684 if (bcs->ap == NULL) {
7bb5fdc2 1685 /* last one: stop ev-layer hupD notifications */
1b4843c5 1686 bcs->apconnstate = APCONN_NONE;
7bb5fdc2 1687 bcs->chstate &= ~CHS_NOTIFY_LL;
1b4843c5
TS
1688 }
1689 spin_unlock_irqrestore(&bcs->aplock, flags);
7bb5fdc2
TS
1690 return;
1691 }
1692 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1693 if (oap->bcnext == ap) {
1694 oap->bcnext = oap->bcnext->bcnext;
1b4843c5 1695 spin_unlock_irqrestore(&bcs->aplock, flags);
7bb5fdc2
TS
1696 return;
1697 }
1698 }
1b4843c5 1699 spin_unlock_irqrestore(&bcs->aplock, flags);
7bb5fdc2
TS
1700 dev_err(cs->dev, "%s: application %u not found\n",
1701 __func__, ap->id);
1702 return;
1703
1704 default: /* Reject */
1705 /* drop all competing applications, keep only this one */
1b4843c5
TS
1706 spin_lock_irqsave(&bcs->aplock, flags);
1707 while (bcs->ap != NULL) {
1708 oap = bcs->ap;
1709 bcs->ap = oap->bcnext;
1710 if (oap != ap) {
1711 spin_unlock_irqrestore(&bcs->aplock, flags);
7bb5fdc2
TS
1712 send_disconnect_ind(bcs, oap,
1713 CapiCallGivenToOtherApplication);
1b4843c5
TS
1714 spin_lock_irqsave(&bcs->aplock, flags);
1715 }
1716 }
7bb5fdc2
TS
1717 ap->bcnext = NULL;
1718 bcs->ap = ap;
1b4843c5 1719 spin_unlock_irqrestore(&bcs->aplock, flags);
7bb5fdc2
TS
1720
1721 /* reject call - will trigger DISCONNECT_IND for this app */
1722 dev_info(cs->dev, "%s: Reject=%x\n",
1723 "CONNECT_RESP", cmsg->Reject);
1724 if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
1725 EV_HUP, NULL, 0, NULL))
1726 return;
7bb5fdc2
TS
1727 gigaset_schedule_event(cs);
1728 return;
1729 }
1730}
1731
1732/*
1733 * process CONNECT_B3_REQ message
1734 * build NCCI and emit CONNECT_B3_CONF reply
1735 */
1736static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1737 struct gigaset_capi_appl *ap,
1738 struct sk_buff *skb)
1739{
1740 struct cardstate *cs = iif->ctr.driverdata;
6c911916 1741 _cmsg *cmsg = &iif->acmsg;
1b4843c5 1742 struct bc_state *bcs;
7bb5fdc2
TS
1743 int channel;
1744
1745 /* decode message */
6c911916
TS
1746 capi_message2cmsg(cmsg, skb->data);
1747 dump_cmsg(DEBUG_CMD, __func__, cmsg);
7bb5fdc2
TS
1748
1749 /* extract and check channel number from PLCI */
6c911916 1750 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
7bb5fdc2
TS
1751 if (!channel || channel > cs->channels) {
1752 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
6c911916 1753 "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
7bb5fdc2
TS
1754 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1755 return;
1756 }
1b4843c5 1757 bcs = &cs->bcs[channel-1];
7bb5fdc2
TS
1758
1759 /* mark logical connection active */
1b4843c5 1760 bcs->apconnstate = APCONN_ACTIVE;
7bb5fdc2
TS
1761
1762 /* build NCCI: always 1 (one B3 connection only) */
6c911916 1763 cmsg->adr.adrNCCI |= 1 << 16;
7bb5fdc2
TS
1764
1765 /* NCPI parameter: not applicable for B3 Transparent */
6c911916
TS
1766 ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
1767 send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
1768 CapiNcpiNotSupportedByProtocol : CapiSuccess);
7bb5fdc2
TS
1769}
1770
1771/*
1772 * process CONNECT_B3_RESP message
1773 * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1774 * or queue EV_HUP and emit DISCONNECT_B3_IND.
1775 * The emitted message is always shorter than the received one,
1776 * allowing to reuse the skb.
1777 */
1778static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1779 struct gigaset_capi_appl *ap,
1780 struct sk_buff *skb)
1781{
1782 struct cardstate *cs = iif->ctr.driverdata;
6c911916
TS
1783 _cmsg *cmsg = &iif->acmsg;
1784 struct bc_state *bcs;
7bb5fdc2
TS
1785 int channel;
1786 unsigned int msgsize;
1787 u8 command;
1788
1789 /* decode message */
6c911916
TS
1790 capi_message2cmsg(cmsg, skb->data);
1791 dump_cmsg(DEBUG_CMD, __func__, cmsg);
7bb5fdc2
TS
1792
1793 /* extract and check channel number and NCCI */
6c911916 1794 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
7bb5fdc2 1795 if (!channel || channel > cs->channels ||
6c911916 1796 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
7bb5fdc2 1797 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
6c911916 1798 "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
4dd8230a 1799 dev_kfree_skb_any(skb);
7bb5fdc2
TS
1800 return;
1801 }
1802 bcs = &cs->bcs[channel-1];
1803
6c911916 1804 if (cmsg->Reject) {
7bb5fdc2 1805 /* Reject: clear B3 connect received flag */
1b4843c5 1806 bcs->apconnstate = APCONN_SETUP;
7bb5fdc2
TS
1807
1808 /* trigger hangup, causing eventual DISCONNECT_IND */
1809 if (!gigaset_add_event(cs, &bcs->at_state,
1810 EV_HUP, NULL, 0, NULL)) {
4dd8230a 1811 dev_kfree_skb_any(skb);
7bb5fdc2
TS
1812 return;
1813 }
7bb5fdc2
TS
1814 gigaset_schedule_event(cs);
1815
1816 /* emit DISCONNECT_B3_IND */
1817 command = CAPI_DISCONNECT_B3;
1818 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1819 } else {
1820 /*
1821 * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1822 * we only send CONNECT_B3_IND if the B channel is up
1823 */
1824 command = CAPI_CONNECT_B3_ACTIVE;
1825 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1826 }
6c911916
TS
1827 capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1828 ap->nextMessageNumber++, cmsg->adr.adrNCCI);
7bb5fdc2 1829 __skb_trim(skb, msgsize);
6c911916
TS
1830 capi_cmsg2message(cmsg, skb->data);
1831 dump_cmsg(DEBUG_CMD, __func__, cmsg);
7bb5fdc2
TS
1832 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1833}
1834
1835/*
1836 * process DISCONNECT_REQ message
1837 * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1838 * emit DISCONNECT_CONF reply
1839 */
1840static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1841 struct gigaset_capi_appl *ap,
1842 struct sk_buff *skb)
1843{
1844 struct cardstate *cs = iif->ctr.driverdata;
6c911916 1845 _cmsg *cmsg = &iif->acmsg;
7bb5fdc2
TS
1846 struct bc_state *bcs;
1847 _cmsg *b3cmsg;
1848 struct sk_buff *b3skb;
1849 int channel;
1850
1851 /* decode message */
6c911916
TS
1852 capi_message2cmsg(cmsg, skb->data);
1853 dump_cmsg(DEBUG_CMD, __func__, cmsg);
7bb5fdc2
TS
1854
1855 /* extract and check channel number from PLCI */
6c911916 1856 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
7bb5fdc2
TS
1857 if (!channel || channel > cs->channels) {
1858 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
6c911916 1859 "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
7bb5fdc2
TS
1860 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1861 return;
1862 }
1863 bcs = cs->bcs + channel - 1;
1864
1865 /* ToDo: process parameter: Additional info */
6c911916
TS
1866 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1867 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1868 "DISCONNECT_REQ", "B Channel Information");
1869 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1870 "DISCONNECT_REQ", "Keypad Facility");
1871 ignore_cstruct_param(cs, cmsg->Useruserdata,
1872 "DISCONNECT_REQ", "User-User Data");
1873 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1874 "DISCONNECT_REQ", "Facility Data Array");
1875 }
7bb5fdc2
TS
1876
1877 /* skip if DISCONNECT_IND already sent */
1b4843c5 1878 if (!bcs->apconnstate)
7bb5fdc2
TS
1879 return;
1880
1881 /* check for active logical connection */
1b4843c5 1882 if (bcs->apconnstate >= APCONN_ACTIVE) {
7bb5fdc2
TS
1883 /*
1884 * emit DISCONNECT_B3_IND with cause 0x3301
1885 * use separate cmsg structure, as the content of iif->acmsg
1886 * is still needed for creating the _CONF message
1887 */
1888 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1889 if (!b3cmsg) {
1890 dev_err(cs->dev, "%s: out of memory\n", __func__);
1891 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1892 return;
1893 }
1894 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1895 ap->nextMessageNumber++,
6c911916 1896 cmsg->adr.adrPLCI | (1 << 16));
7bb5fdc2
TS
1897 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1898 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1899 if (b3skb == NULL) {
1900 dev_err(cs->dev, "%s: out of memory\n", __func__);
1901 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1902 return;
1903 }
1904 capi_cmsg2message(b3cmsg,
1905 __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN));
1906 kfree(b3cmsg);
1907 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1908 }
1909
1910 /* trigger hangup, causing eventual DISCONNECT_IND */
1911 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
7bb5fdc2
TS
1912 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1913 return;
1914 }
7bb5fdc2
TS
1915 gigaset_schedule_event(cs);
1916
1917 /* emit reply */
1918 send_conf(iif, ap, skb, CapiSuccess);
1919}
1920
1921/*
1922 * process DISCONNECT_B3_REQ message
1923 * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
1924 */
1925static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
1926 struct gigaset_capi_appl *ap,
1927 struct sk_buff *skb)
1928{
1929 struct cardstate *cs = iif->ctr.driverdata;
6c911916 1930 _cmsg *cmsg = &iif->acmsg;
1b4843c5 1931 struct bc_state *bcs;
7bb5fdc2
TS
1932 int channel;
1933
1934 /* decode message */
6c911916
TS
1935 capi_message2cmsg(cmsg, skb->data);
1936 dump_cmsg(DEBUG_CMD, __func__, cmsg);
7bb5fdc2
TS
1937
1938 /* extract and check channel number and NCCI */
6c911916 1939 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
7bb5fdc2 1940 if (!channel || channel > cs->channels ||
6c911916 1941 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
7bb5fdc2 1942 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
6c911916 1943 "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
7bb5fdc2
TS
1944 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1945 return;
1946 }
1b4843c5 1947 bcs = &cs->bcs[channel-1];
7bb5fdc2
TS
1948
1949 /* reject if logical connection not active */
1b4843c5 1950 if (bcs->apconnstate < APCONN_ACTIVE) {
7bb5fdc2
TS
1951 send_conf(iif, ap, skb,
1952 CapiMessageNotSupportedInCurrentState);
1953 return;
1954 }
1955
1956 /* trigger hangup, causing eventual DISCONNECT_B3_IND */
1b4843c5 1957 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
7bb5fdc2
TS
1958 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1959 return;
1960 }
7bb5fdc2
TS
1961 gigaset_schedule_event(cs);
1962
1963 /* NCPI parameter: not applicable for B3 Transparent */
6c911916 1964 ignore_cstruct_param(cs, cmsg->NCPI,
7bb5fdc2 1965 "DISCONNECT_B3_REQ", "NCPI");
6c911916
TS
1966 send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
1967 CapiNcpiNotSupportedByProtocol : CapiSuccess);
7bb5fdc2
TS
1968}
1969
1970/*
1971 * process DATA_B3_REQ message
1972 */
1973static void do_data_b3_req(struct gigaset_capi_ctr *iif,
1974 struct gigaset_capi_appl *ap,
1975 struct sk_buff *skb)
1976{
1977 struct cardstate *cs = iif->ctr.driverdata;
1b4843c5 1978 struct bc_state *bcs;
7bb5fdc2
TS
1979 int channel = CAPIMSG_PLCI_PART(skb->data);
1980 u16 ncci = CAPIMSG_NCCI_PART(skb->data);
1981 u16 msglen = CAPIMSG_LEN(skb->data);
1982 u16 datalen = CAPIMSG_DATALEN(skb->data);
1983 u16 flags = CAPIMSG_FLAGS(skb->data);
23b36778
TS
1984 u16 msgid = CAPIMSG_MSGID(skb->data);
1985 u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
7bb5fdc2
TS
1986
1987 /* frequent message, avoid _cmsg overhead */
6a75342a 1988 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
7bb5fdc2
TS
1989
1990 /* check parameters */
1991 if (channel == 0 || channel > cs->channels || ncci != 1) {
1992 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1993 "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
1994 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1995 return;
1996 }
1b4843c5 1997 bcs = &cs->bcs[channel-1];
7bb5fdc2
TS
1998 if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
1999 dev_notice(cs->dev, "%s: unexpected length %d\n",
2000 "DATA_B3_REQ", msglen);
2001 if (msglen + datalen != skb->len)
2002 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
2003 "DATA_B3_REQ", msglen, datalen, skb->len);
2004 if (msglen + datalen > skb->len) {
2005 /* message too short for announced data length */
2006 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
2007 return;
2008 }
2009 if (flags & CAPI_FLAGS_RESERVED) {
2010 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
2011 "DATA_B3_REQ", flags);
2012 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
2013 return;
2014 }
2015
2016 /* reject if logical connection not active */
1b4843c5 2017 if (bcs->apconnstate < APCONN_ACTIVE) {
7bb5fdc2
TS
2018 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2019 return;
2020 }
2021
4dd8230a
TS
2022 /* pull CAPI message into link layer header */
2023 skb_reset_mac_header(skb);
2024 skb->mac_len = msglen;
7bb5fdc2 2025 skb_pull(skb, msglen);
4dd8230a
TS
2026
2027 /* pass to device-specific module */
1b4843c5 2028 if (cs->ops->send_skb(bcs, skb) < 0) {
7bb5fdc2
TS
2029 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2030 return;
2031 }
2032
7bb5fdc2 2033 /*
23b36778
TS
2034 * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
2035 * confirmation" bit is set; otherwise we have to send it now
7bb5fdc2 2036 */
23b36778
TS
2037 if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
2038 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
2039 flags ? CapiFlagsNotSupportedByProtocol
2040 : CAPI_NOERROR);
7bb5fdc2
TS
2041}
2042
2043/*
2044 * process RESET_B3_REQ message
2045 * just always reply "not supported by current protocol"
2046 */
2047static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
2048 struct gigaset_capi_appl *ap,
2049 struct sk_buff *skb)
2050{
2051 /* decode message */
2052 capi_message2cmsg(&iif->acmsg, skb->data);
2053 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2054 send_conf(iif, ap, skb,
2055 CapiResetProcedureNotSupportedByCurrentProtocol);
2056}
2057
2058/*
2059 * dump unsupported/ignored messages at most twice per minute,
2060 * some apps send those very frequently
2061 */
2062static unsigned long ignored_msg_dump_time;
2063
2064/*
2065 * unsupported CAPI message handler
2066 */
2067static void do_unsupported(struct gigaset_capi_ctr *iif,
2068 struct gigaset_capi_appl *ap,
2069 struct sk_buff *skb)
2070{
2071 /* decode message */
2072 capi_message2cmsg(&iif->acmsg, skb->data);
2073 if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000))
2074 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2075 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2076}
2077
2078/*
2079 * CAPI message handler: no-op
2080 */
2081static void do_nothing(struct gigaset_capi_ctr *iif,
2082 struct gigaset_capi_appl *ap,
2083 struct sk_buff *skb)
2084{
2085 if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000)) {
2086 /* decode message */
2087 capi_message2cmsg(&iif->acmsg, skb->data);
2088 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2089 }
4dd8230a 2090 dev_kfree_skb_any(skb);
7bb5fdc2
TS
2091}
2092
2093static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
2094 struct gigaset_capi_appl *ap,
2095 struct sk_buff *skb)
2096{
6a75342a 2097 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
4dd8230a 2098 dev_kfree_skb_any(skb);
7bb5fdc2
TS
2099}
2100
2101/* table of outgoing CAPI message handlers with lookup function */
2102typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
2103 struct gigaset_capi_appl *,
2104 struct sk_buff *);
2105
2106static struct {
2107 u16 cmd;
2108 capi_send_handler_t handler;
2109} capi_send_handler_table[] = {
2110 /* most frequent messages first for faster lookup */
2111 { CAPI_DATA_B3_REQ, do_data_b3_req },
2112 { CAPI_DATA_B3_RESP, do_data_b3_resp },
2113
2114 { CAPI_ALERT_REQ, do_alert_req },
2115 { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
2116 { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
2117 { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
2118 { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
2119 { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
2120 { CAPI_CONNECT_REQ, do_connect_req },
2121 { CAPI_CONNECT_RESP, do_connect_resp },
2122 { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
2123 { CAPI_DISCONNECT_B3_RESP, do_nothing },
2124 { CAPI_DISCONNECT_REQ, do_disconnect_req },
2125 { CAPI_DISCONNECT_RESP, do_nothing },
2126 { CAPI_FACILITY_REQ, do_facility_req },
2127 { CAPI_FACILITY_RESP, do_nothing },
2128 { CAPI_LISTEN_REQ, do_listen_req },
2129 { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
2130 { CAPI_RESET_B3_REQ, do_reset_b3_req },
2131 { CAPI_RESET_B3_RESP, do_nothing },
2132
2133 /*
2134 * ToDo: support overlap sending (requires ev-layer state
2135 * machine extension to generate additional ATD commands)
2136 */
2137 { CAPI_INFO_REQ, do_unsupported },
2138 { CAPI_INFO_RESP, do_nothing },
2139
2140 /*
2141 * ToDo: what's the proper response for these?
2142 */
2143 { CAPI_MANUFACTURER_REQ, do_nothing },
2144 { CAPI_MANUFACTURER_RESP, do_nothing },
2145};
2146
2147/* look up handler */
2148static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
2149{
2150 size_t i;
2151
2152 for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2153 if (capi_send_handler_table[i].cmd == cmd)
2154 return capi_send_handler_table[i].handler;
2155 return NULL;
2156}
2157
2158
2159/**
2160 * gigaset_send_message() - accept a CAPI message from an application
2161 * @ctr: controller descriptor structure.
2162 * @skb: CAPI message.
2163 *
2164 * Return value: CAPI error code
2165 * Note: capidrv (and probably others, too) only uses the return value to
2166 * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2167 */
2168static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2169{
2170 struct gigaset_capi_ctr *iif
2171 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2172 struct cardstate *cs = ctr->driverdata;
2173 struct gigaset_capi_appl *ap;
2174 capi_send_handler_t handler;
2175
2176 /* can only handle linear sk_buffs */
2177 if (skb_linearize(skb) < 0) {
2178 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2179 return CAPI_MSGOSRESOURCEERR;
2180 }
2181
2182 /* retrieve application data structure */
2183 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2184 if (!ap) {
2185 dev_notice(cs->dev, "%s: application %u not registered\n",
2186 __func__, CAPIMSG_APPID(skb->data));
2187 return CAPI_ILLAPPNR;
2188 }
2189
2190 /* look up command */
2191 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2192 if (!handler) {
2193 /* unknown/unsupported message type */
2194 if (printk_ratelimit())
2195 dev_notice(cs->dev, "%s: unsupported message %u\n",
2196 __func__, CAPIMSG_CMD(skb->data));
2197 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2198 }
2199
2200 /* serialize */
2201 if (atomic_add_return(1, &iif->sendqlen) > 1) {
2202 /* queue behind other messages */
2203 skb_queue_tail(&iif->sendqueue, skb);
2204 return CAPI_NOERROR;
2205 }
2206
2207 /* process message */
2208 handler(iif, ap, skb);
2209
2210 /* process other messages arrived in the meantime */
2211 while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2212 skb = skb_dequeue(&iif->sendqueue);
2213 if (!skb) {
2214 /* should never happen */
2215 dev_err(cs->dev, "%s: send queue empty\n", __func__);
2216 continue;
2217 }
2218 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2219 if (!ap) {
2220 /* could that happen? */
2221 dev_warn(cs->dev, "%s: application %u vanished\n",
2222 __func__, CAPIMSG_APPID(skb->data));
2223 continue;
2224 }
2225 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2226 if (!handler) {
2227 /* should never happen */
2228 dev_err(cs->dev, "%s: handler %x vanished\n",
2229 __func__, CAPIMSG_CMD(skb->data));
2230 continue;
2231 }
2232 handler(iif, ap, skb);
2233 }
2234
2235 return CAPI_NOERROR;
2236}
2237
2238/**
2239 * gigaset_procinfo() - build single line description for controller
2240 * @ctr: controller descriptor structure.
2241 *
2242 * Return value: pointer to generated string (null terminated)
2243 */
2244static char *gigaset_procinfo(struct capi_ctr *ctr)
2245{
2246 return ctr->name; /* ToDo: more? */
2247}
2248
9a58a80a 2249static int gigaset_proc_show(struct seq_file *m, void *v)
7bb5fdc2 2250{
9a58a80a 2251 struct capi_ctr *ctr = m->private;
7bb5fdc2
TS
2252 struct cardstate *cs = ctr->driverdata;
2253 char *s;
2254 int i;
9a58a80a
AD
2255
2256 seq_printf(m, "%-16s %s\n", "name", ctr->name);
2257 seq_printf(m, "%-16s %s %s\n", "dev",
7bb5fdc2 2258 dev_driver_string(cs->dev), dev_name(cs->dev));
9a58a80a 2259 seq_printf(m, "%-16s %d\n", "id", cs->myid);
7bb5fdc2 2260 if (cs->gotfwver)
9a58a80a 2261 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
7bb5fdc2 2262 cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
9a58a80a
AD
2263 seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2264 seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
7bb5fdc2
TS
2265
2266 switch (cs->mode) {
2267 case M_UNKNOWN:
2268 s = "unknown";
2269 break;
2270 case M_CONFIG:
2271 s = "config";
2272 break;
2273 case M_UNIMODEM:
2274 s = "Unimodem";
2275 break;
2276 case M_CID:
2277 s = "CID";
2278 break;
2279 default:
2280 s = "??";
2281 }
9a58a80a 2282 seq_printf(m, "%-16s %s\n", "mode", s);
7bb5fdc2
TS
2283
2284 switch (cs->mstate) {
2285 case MS_UNINITIALIZED:
2286 s = "uninitialized";
2287 break;
2288 case MS_INIT:
2289 s = "init";
2290 break;
2291 case MS_LOCKED:
2292 s = "locked";
2293 break;
2294 case MS_SHUTDOWN:
2295 s = "shutdown";
2296 break;
2297 case MS_RECOVER:
2298 s = "recover";
2299 break;
2300 case MS_READY:
2301 s = "ready";
2302 break;
2303 default:
2304 s = "??";
2305 }
9a58a80a 2306 seq_printf(m, "%-16s %s\n", "mstate", s);
7bb5fdc2 2307
9a58a80a
AD
2308 seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2309 seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2310 seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2311 seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
7bb5fdc2
TS
2312
2313 for (i = 0; i < cs->channels; i++) {
9a58a80a 2314 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
7bb5fdc2 2315 cs->bcs[i].corrupted);
9a58a80a 2316 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
7bb5fdc2 2317 cs->bcs[i].trans_down);
9a58a80a 2318 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
7bb5fdc2 2319 cs->bcs[i].trans_up);
9a58a80a 2320 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
7bb5fdc2
TS
2321 cs->bcs[i].chstate);
2322 switch (cs->bcs[i].proto2) {
2323 case L2_BITSYNC:
2324 s = "bitsync";
2325 break;
2326 case L2_HDLC:
2327 s = "HDLC";
2328 break;
2329 case L2_VOICE:
2330 s = "voice";
2331 break;
2332 default:
2333 s = "??";
2334 }
9a58a80a 2335 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
7bb5fdc2 2336 }
9a58a80a 2337 return 0;
7bb5fdc2
TS
2338}
2339
9a58a80a
AD
2340static int gigaset_proc_open(struct inode *inode, struct file *file)
2341{
2342 return single_open(file, gigaset_proc_show, PDE(inode)->data);
2343}
2344
2345static const struct file_operations gigaset_proc_fops = {
2346 .owner = THIS_MODULE,
2347 .open = gigaset_proc_open,
2348 .read = seq_read,
2349 .llseek = seq_lseek,
2350 .release = single_release,
2351};
7bb5fdc2 2352
7bb5fdc2 2353/**
bc35b4e3 2354 * gigaset_isdn_regdev() - register device to LL
7bb5fdc2
TS
2355 * @cs: device descriptor structure.
2356 * @isdnid: device name.
2357 *
7bb5fdc2
TS
2358 * Return value: 1 for success, 0 for failure
2359 */
bc35b4e3 2360int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
7bb5fdc2
TS
2361{
2362 struct gigaset_capi_ctr *iif;
2363 int rc;
2364
7bb5fdc2
TS
2365 iif = kmalloc(sizeof(*iif), GFP_KERNEL);
2366 if (!iif) {
2367 pr_err("%s: out of memory\n", __func__);
2368 return 0;
2369 }
2370
7bb5fdc2
TS
2371 /* prepare controller structure */
2372 iif->ctr.owner = THIS_MODULE;
2373 iif->ctr.driverdata = cs;
2374 strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name));
2375 iif->ctr.driver_name = "gigaset";
e487639d
TS
2376 iif->ctr.load_firmware = NULL;
2377 iif->ctr.reset_ctr = NULL;
7bb5fdc2
TS
2378 iif->ctr.register_appl = gigaset_register_appl;
2379 iif->ctr.release_appl = gigaset_release_appl;
2380 iif->ctr.send_message = gigaset_send_message;
2381 iif->ctr.procinfo = gigaset_procinfo;
9a58a80a 2382 iif->ctr.proc_fops = &gigaset_proc_fops;
7bb5fdc2
TS
2383 INIT_LIST_HEAD(&iif->appls);
2384 skb_queue_head_init(&iif->sendqueue);
2385 atomic_set(&iif->sendqlen, 0);
2386
2387 /* register controller with CAPI */
2388 rc = attach_capi_ctr(&iif->ctr);
2389 if (rc) {
2390 pr_err("attach_capi_ctr failed (%d)\n", rc);
7bb5fdc2
TS
2391 kfree(iif);
2392 return 0;
2393 }
2394
2395 cs->iif = iif;
2396 cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
2397 return 1;
2398}
2399
2400/**
bc35b4e3 2401 * gigaset_isdn_unregdev() - unregister device from LL
7bb5fdc2 2402 * @cs: device descriptor structure.
7bb5fdc2 2403 */
bc35b4e3 2404void gigaset_isdn_unregdev(struct cardstate *cs)
7bb5fdc2
TS
2405{
2406 struct gigaset_capi_ctr *iif = cs->iif;
2407
2408 detach_capi_ctr(&iif->ctr);
2409 kfree(iif);
2410 cs->iif = NULL;
bc35b4e3
TS
2411}
2412
2413static struct capi_driver capi_driver_gigaset = {
2414 .name = "gigaset",
2415 .revision = "1.0",
2416};
2417
2418/**
2419 * gigaset_isdn_regdrv() - register driver to LL
2420 */
2421void gigaset_isdn_regdrv(void)
2422{
2423 pr_info("Kernel CAPI interface\n");
2424 register_capi_driver(&capi_driver_gigaset);
2425}
2426
2427/**
2428 * gigaset_isdn_unregdrv() - unregister driver from LL
2429 */
2430void gigaset_isdn_unregdrv(void)
2431{
7bb5fdc2
TS
2432 unregister_capi_driver(&capi_driver_gigaset);
2433}