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