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