blob: 3286903a95d26a19d548dc7854c336f1ead2628f [file] [log] [blame]
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001/*
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"
Alexey Dobriyan9a58a802010-01-14 03:10:54 -080015#include <linux/proc_fs.h>
16#include <linux/seq_file.h>
Tilman Schmidt8e618aa2012-04-25 13:02:19 +000017#include <linux/ratelimit.h>
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000018#include <linux/isdn/capilli.h>
19#include <linux/isdn/capicmd.h>
20#include <linux/isdn/capiutil.h>
Paul Gortmaker5d76fc22011-07-10 12:23:16 -040021#include <linux/export.h>
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000022
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 */
Joe Perches475be4d2012-02-19 19:52:38 -080030#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)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000039/* most _CONF messages contain only Controller/PLCI/NCCI and Info parameters */
Joe Perches475be4d2012-02-19 19:52:38 -080040#define CAPI_STDCONF_LEN (CAPI_MSG_BASELEN + 4 + 2)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000041
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
Tilman Schmidt18c22592010-07-05 14:19:04 +000050#define CAPI_SUPPSVC_LISTEN 0x0001
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000051
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
Tilman Schmidt1b4843c2010-06-21 13:55:20 +000076/* values for bcs->apconnstate */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000077#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;
Tilman Schmidte7752ee2010-06-21 13:54:19 +000086 struct capi_register_params rp;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000087 u16 nextMessageNumber;
88 u32 listenInfoMask;
89 u32 listenCIPmask;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +000090};
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 */
Joe Perches475be4d2012-02-19 19:52:38 -0800101 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];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000105};
106
107/* CIP Value table (from CAPI 2.0 standard, ch. 6.1) */
108static struct {
109 u8 *bc;
110 u8 *hlc;
111} cip2bchlc[] = {
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000112 [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 */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000141};
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,
Joe Perches475be4d2012-02-19 19:52:38 -0800152 char *msgname, char *paramname)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000153{
154 if (param && *param)
155 dev_warn(cs->dev, "%s: ignoring unsupported parameter: %s\n",
156 msgname, paramname);
157}
158
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000159/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000160 * 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) {
Andy Shevchenko003bdb22010-02-22 13:10:22 +0000168 if (!isxdigit(in[0]) || !isxdigit(in[1]) || l >= maxlen)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000169 return -1;
Andy Shevchenko0496b552010-05-24 14:33:25 -0700170 out[++l] = (hex_to_bin(in[0]) << 4) + hex_to_bin(in[1]);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000171 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
Tilman Schmidt8e618aa2012-04-25 13:02:19 +0000211 /* dump at most 20 messages in 20 secs */
212 static DEFINE_RATELIMIT_STATE(msg_dump_ratelimit, 20 * HZ, 20);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000213 _cdebbuf *cdb;
214
215 if (!(gigaset_debuglevel & level))
216 return;
Tilman Schmidt8e618aa2012-04-25 13:02:19 +0000217 if (!___ratelimit(&msg_dump_ratelimit, tag))
218 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000219
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;
Tilman Schmidtd721a172013-01-21 11:57:21 +0000251 if (l <= 0)
252 return;
Joe Perches475be4d2012-02-19 19:52:38 -0800253 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000254 if (!dbgline)
255 return;
256 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800257 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] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000260 }
Joe Perches475be4d2012-02-19 19:52:38 -0800261 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000262 gig_dbg(level, " %s", dbgline);
263 kfree(dbgline);
264 if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
265 (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
Tilman Schmidt6a753422010-07-05 14:18:59 +0000266 CAPIMSG_SUBCOMMAND(data) == CAPI_IND)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000267 l = CAPIMSG_DATALEN(data);
Tilman Schmidt6a753422010-07-05 14:18:59 +0000268 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 */
Joe Perches475be4d2012-02-19 19:52:38 -0800273 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000274 if (!dbgline)
275 return;
276 data += CAPIMSG_LEN(data);
277 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800278 dbgline[3 * i] = hex_asc_hi(data[i]);
279 dbgline[3 * i + 1] = hex_asc_lo(data[i]);
280 dbgline[3 * i + 2] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000281 }
Joe Perches475be4d2012-02-19 19:52:38 -0800282 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000283 gig_dbg(level, " %s", dbgline);
284 kfree(dbgline);
285 }
286#endif
287}
288
289/*
290 * format CAPI IE as string
291 */
292
Paul Bolle6ba60472012-07-14 23:11:55 +0000293#ifdef CONFIG_GIGASET_DEBUG
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000294static const char *format_ie(const char *ie)
295{
Joe Perches475be4d2012-02-19 19:52:38 -0800296 static char result[3 * MAX_FMT_IE_LEN];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000297 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)
Joe Perches475be4d2012-02-19 19:52:38 -0800305 count = MAX_FMT_IE_LEN - 1;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000306 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}
Paul Bolle6ba60472012-07-14 23:11:55 +0000319#endif
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000320
Tilman Schmidt23b36772010-06-21 13:54:50 +0000321/*
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
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000354
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;
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000373 unsigned char *req = skb_mac_header(dskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000374 u16 flags;
375
376 /* update statistics */
377 ++bcs->trans_up;
378
379 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000380 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000381 return;
382 }
383
384 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000385 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000386 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000387 return;
388 }
389
Tilman Schmidt23b36772010-06-21 13:54:50 +0000390 /*
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 */
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000394 flags = CAPIMSG_FLAGS(req);
Tilman Schmidt23b36772010-06-21 13:54:50 +0000395 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) ?
Joe Perches475be4d2012-02-19 19:52:38 -0800399 CapiFlagsNotSupportedByProtocol :
400 CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000401}
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) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000424 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
425 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000426 return;
427 }
428
429 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000430 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000431 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000432 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000433 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 */
Tilman Schmidt6a753422010-07-05 14:18:59 +0000457 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000458 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;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000502 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000503 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 */
Joe Perches475be4d2012-02-19 19:52:38 -0800585 memcpy(iif->cdpty_buf + 2, at_state->str_var[STR_ZCPN], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000586 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 */
Joe Perches475be4d2012-02-19 19:52:38 -0800601 memcpy(iif->cgpty_buf + 3, at_state->str_var[STR_NMBR], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000602 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 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000627 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);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000635 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 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000652 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000653 ap->bcnext = bcs->ap;
654 bcs->ap = ap;
655 bcs->chstate |= CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000656 bcs->apconnstate = APCONN_SETUP;
657 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000658
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
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000682 if (bcs->apconnstate == APCONN_NONE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000683 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);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000696 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 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000712 if (bcs->apconnstate < APCONN_ACTIVE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000713 return;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000714 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000715
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;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000741 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000742 struct sk_buff *skb;
743 unsigned int msgsize;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000744 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000745
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000746 spin_lock_irqsave(&bcs->aplock, flags);
747 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000748 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000749 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000750 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000751 return;
752 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000753 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);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000760 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 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000768
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;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000805 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000806
807 /*
808 * ToDo: pass on reason code reported by device
809 * (requires ev-layer state machine extension to collect
810 * ZCAU device reply)
811 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000812 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);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000817 send_disconnect_b3_ind(bcs, ap);
818 send_disconnect_ind(bcs, ap, 0);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000819 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000820 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000821 bcs->apconnstate = APCONN_NONE;
822 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000823}
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;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000836 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000837 struct sk_buff *skb;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000838 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000839 unsigned int msgsize;
840 u8 command;
841
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000842 spin_lock_irqsave(&bcs->aplock, flags);
843 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000844 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000845 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000846 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000847 return;
848 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000849 if (!bcs->apconnstate) {
850 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000851 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 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000862 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000863 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 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000869 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
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000882 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);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000892 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{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000904 struct gigaset_capi_appl *ap = bcs->ap;
905
906 /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
907
908 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000909 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000910 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
969/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000970 * register CAPI application
971 */
972static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
Joe Perches475be4d2012-02-19 19:52:38 -0800973 capi_register_params *rp)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000974{
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
Tilman Schmidt6a753422010-07-05 14:18:59 +0000980 gig_dbg(DEBUG_CMD, "%s [%u] l3cnt=%u blkcnt=%u blklen=%u",
981 __func__, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
982
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000983 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;
Tilman Schmidte7752ee2010-06-21 13:54:19 +0000996 ap->rp = *rp;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000997
998 list_add(&ap->ctrlist, &iif->appls);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000999 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;
Dan Carpenter7e27a0a2010-08-05 22:23:23 +00001049 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001050 return;
1051 }
1052 bcap = bcap->bcnext;
1053 } while (bcap != NULL);
1054 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001055}
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;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001066 unsigned ch;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001067
Tilman Schmidt6a753422010-07-05 14:18:59 +00001068 gig_dbg(DEBUG_CMD, "%s [%u]", __func__, appl);
1069
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001070 list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
1071 if (ap->id == appl) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001072 /* 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 */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001077 list_del(&ap->ctrlist);
1078 kfree(ap);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001079 dev_info(cs->dev, "application %u released\n", appl);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001080 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001081}
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;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001117 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001118 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 */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001125 capi_message2cmsg(cmsg, skb->data);
1126 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001127
1128 /*
1129 * Facility Request Parameter is not decoded by capi_message2cmsg()
1130 * encoding depends on Facility Selector
1131 */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001132 switch (cmsg->FacilitySelector) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001133 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 */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001149 pparam = cmsg->FacilityRequestParameter;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001150 if (pparam == NULL || pparam[0] < 2) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001151 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;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001167 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,
Joe Perches475be4d2012-02-19 19:52:38 -08001177 "%s: unsupported supplementary service notification mask 0x%x\n",
1178 "FACILITY_REQ", CAPIMSG_U32(pparam, 4));
Tilman Schmidt18c22592010-07-05 14:19:04 +00001179 info = CapiFacilitySpecificFunctionNotSupported;
1180 confparam[3] = 2; /* length */
1181 capimsg_setu16(confparam, 4,
Joe Perches475be4d2012-02-19 19:52:38 -08001182 CapiSupplementaryServiceNotSupported);
Tilman Schmidt18c22592010-07-05 14:19:04 +00001183 }
1184 info = CapiSuccess;
1185 confparam[3] = 2; /* length */
1186 capimsg_setu16(confparam, 4, CapiSuccess);
1187 break;
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001188
1189 /* ToDo: add supported services */
1190
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001191 default:
Tilman Schmidt18c22592010-07-05 14:19:04 +00001192 dev_notice(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001193 "%s: unsupported supplementary service function 0x%04x\n",
Tilman Schmidt18c22592010-07-05 14:19:04 +00001194 "FACILITY_REQ", function);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001195 info = CapiFacilitySpecificFunctionNotSupported;
1196 /* Supplementary Service specific parameter */
1197 confparam[3] = 2; /* length */
1198 /* Supplementary services info: not supported */
1199 capimsg_setu16(confparam, 4,
1200 CapiSupplementaryServiceNotSupported);
1201 }
1202
1203 /* Facility confirmation parameter */
1204 confparam[0] = confparam[3] + 3; /* total length */
1205 /* Function: copy from _REQ message */
1206 capimsg_setu16(confparam, 1, function);
1207 /* Supplementary Service specific parameter already set above */
1208 break;
1209
1210 case CAPI_FACILITY_WAKEUP: /* ToDo */
1211 info = CapiFacilityNotSupported;
1212 confparam[0] = 2; /* length */
1213 /* Number of accepted awake request parameters: 0 */
1214 capimsg_setu16(confparam, 1, 0);
1215 break;
1216
1217 default:
1218 info = CapiFacilityNotSupported;
1219 confparam[0] = 0; /* empty struct */
1220 }
1221
1222 /* send FACILITY_CONF with given Info and confirmation parameter */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001223 capi_cmsg_answer(cmsg);
1224 cmsg->Info = info;
1225 cmsg->FacilityConfirmationParameter = confparam;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001226 msgsize += confparam[0]; /* length */
1227 cskb = alloc_skb(msgsize, GFP_ATOMIC);
1228 if (!cskb) {
1229 dev_err(cs->dev, "%s: out of memory\n", __func__);
1230 return;
1231 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001232 capi_cmsg2message(cmsg, __skb_put(cskb, msgsize));
1233 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1234 capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001235}
1236
1237
1238/*
1239 * process LISTEN_REQ message
1240 * just store the masks in the application data structure
1241 */
1242static void do_listen_req(struct gigaset_capi_ctr *iif,
1243 struct gigaset_capi_appl *ap,
1244 struct sk_buff *skb)
1245{
1246 /* decode message */
1247 capi_message2cmsg(&iif->acmsg, skb->data);
1248 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1249
1250 /* store listening parameters */
1251 ap->listenInfoMask = iif->acmsg.InfoMask;
1252 ap->listenCIPmask = iif->acmsg.CIPmask;
1253 send_conf(iif, ap, skb, CapiSuccess);
1254}
1255
1256/*
1257 * process ALERT_REQ message
1258 * nothing to do, Gigaset always alerts anyway
1259 */
1260static void do_alert_req(struct gigaset_capi_ctr *iif,
1261 struct gigaset_capi_appl *ap,
1262 struct sk_buff *skb)
1263{
1264 /* decode message */
1265 capi_message2cmsg(&iif->acmsg, skb->data);
1266 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1267 send_conf(iif, ap, skb, CapiAlertAlreadySent);
1268}
1269
1270/*
1271 * process CONNECT_REQ message
1272 * allocate a B channel, prepare dial commands, queue a DIAL event,
1273 * emit CONNECT_CONF reply
1274 */
1275static void do_connect_req(struct gigaset_capi_ctr *iif,
1276 struct gigaset_capi_appl *ap,
1277 struct sk_buff *skb)
1278{
1279 struct cardstate *cs = iif->ctr.driverdata;
1280 _cmsg *cmsg = &iif->acmsg;
1281 struct bc_state *bcs;
1282 char **commands;
1283 char *s;
1284 u8 *pp;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001285 unsigned long flags;
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001286 int i, l, lbc, lhlc;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001287 u16 info;
1288
1289 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001290 capi_message2cmsg(cmsg, skb->data);
1291 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001292
1293 /* get free B channel & construct PLCI */
1294 bcs = gigaset_get_free_channel(cs);
1295 if (!bcs) {
1296 dev_notice(cs->dev, "%s: no B channel available\n",
1297 "CONNECT_REQ");
1298 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1299 return;
1300 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001301 spin_lock_irqsave(&bcs->aplock, flags);
1302 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE)
1303 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
1304 __func__, bcs->ap, bcs->apconnstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001305 ap->bcnext = NULL;
1306 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001307 bcs->apconnstate = APCONN_SETUP;
1308 spin_unlock_irqrestore(&bcs->aplock, flags);
1309
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001310 bcs->rx_bufsize = ap->rp.datablklen;
1311 dev_kfree_skb(bcs->rx_skb);
1312 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001313 cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1314
1315 /* build command table */
Joe Perches475be4d2012-02-19 19:52:38 -08001316 commands = kzalloc(AT_NUM * (sizeof *commands), GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001317 if (!commands)
1318 goto oom;
1319
1320 /* encode parameter: Called party number */
1321 pp = cmsg->CalledPartyNumber;
1322 if (pp == NULL || *pp == 0) {
1323 dev_notice(cs->dev, "%s: %s missing\n",
1324 "CONNECT_REQ", "Called party number");
1325 info = CapiIllMessageParmCoding;
1326 goto error;
1327 }
1328 l = *pp++;
1329 /* check type of number/numbering plan byte */
1330 switch (*pp) {
1331 case 0x80: /* unknown type / unknown numbering plan */
1332 case 0x81: /* unknown type / ISDN/Telephony numbering plan */
1333 break;
1334 default: /* others: warn about potential misinterpretation */
1335 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1336 "CONNECT_REQ", "Called party number", *pp);
1337 }
1338 pp++;
1339 l--;
1340 /* translate "**" internal call prefix to CTP value */
1341 if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1342 s = "^SCTP=0\r";
1343 pp += 2;
1344 l -= 2;
1345 } else {
1346 s = "^SCTP=1\r";
1347 }
1348 commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1349 if (!commands[AT_TYPE])
1350 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001351 commands[AT_DIAL] = kmalloc(l + 3, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001352 if (!commands[AT_DIAL])
1353 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001354 snprintf(commands[AT_DIAL], l + 3, "D%.*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001355
1356 /* encode parameter: Calling party number */
1357 pp = cmsg->CallingPartyNumber;
1358 if (pp != NULL && *pp > 0) {
1359 l = *pp++;
1360
1361 /* check type of number/numbering plan byte */
1362 /* ToDo: allow for/handle Ext=1? */
1363 switch (*pp) {
1364 case 0x00: /* unknown type / unknown numbering plan */
1365 case 0x01: /* unknown type / ISDN/Telephony num. plan */
1366 break;
1367 default:
1368 dev_notice(cs->dev,
1369 "%s: %s type/plan 0x%02x unsupported\n",
1370 "CONNECT_REQ", "Calling party number", *pp);
1371 }
1372 pp++;
1373 l--;
1374
1375 /* check presentation indicator */
1376 if (!l) {
1377 dev_notice(cs->dev, "%s: %s IE truncated\n",
1378 "CONNECT_REQ", "Calling party number");
1379 info = CapiIllMessageParmCoding;
1380 goto error;
1381 }
1382 switch (*pp & 0xfc) { /* ignore Screening indicator */
1383 case 0x80: /* Presentation allowed */
1384 s = "^SCLIP=1\r";
1385 break;
1386 case 0xa0: /* Presentation restricted */
1387 s = "^SCLIP=0\r";
1388 break;
1389 default:
1390 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1391 "CONNECT_REQ",
1392 "Presentation/Screening indicator",
1393 *pp);
1394 s = "^SCLIP=1\r";
1395 }
1396 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1397 if (!commands[AT_CLIP])
1398 goto oom;
1399 pp++;
1400 l--;
1401
1402 if (l) {
1403 /* number */
Joe Perches475be4d2012-02-19 19:52:38 -08001404 commands[AT_MSN] = kmalloc(l + 8, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001405 if (!commands[AT_MSN])
1406 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001407 snprintf(commands[AT_MSN], l + 8, "^SMSN=%*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001408 }
1409 }
1410
1411 /* check parameter: CIP Value */
Tilman Schmidt6ad34142010-03-16 07:04:01 +00001412 if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001413 (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1414 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1415 "CONNECT_REQ", cmsg->CIPValue);
1416 info = CapiCipValueUnknown;
1417 goto error;
1418 }
1419
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001420 /*
1421 * check/encode parameters: BC & HLC
1422 * must be encoded together as device doesn't accept HLC separately
1423 * explicit parameters override values derived from CIP
1424 */
1425
1426 /* determine lengths */
1427 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001428 lbc = 2 * cmsg->BC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001429 else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
1430 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1431 else /* no BC */
1432 lbc = 0;
1433 if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001434 lhlc = 2 * cmsg->HLC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001435 else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1436 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1437 else /* no HLC */
1438 lhlc = 0;
1439
1440 if (lbc) {
1441 /* have BC: allocate and assemble command string */
1442 l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
1443 if (lhlc)
1444 l += lhlc + 7; /* ";^SHLC=" + value */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001445 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1446 if (!commands[AT_BC])
1447 goto oom;
1448 strcpy(commands[AT_BC], "^SBC=");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001449 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1450 decode_ie(cmsg->BC, commands[AT_BC] + 5);
1451 else /* BC derived from CIP */
1452 strcpy(commands[AT_BC] + 5,
1453 cip2bchlc[cmsg->CIPValue].bc);
1454 if (lhlc) {
1455 strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1456 if (cmsg->HLC && cmsg->HLC[0])
1457 /* HLC specified explicitly */
1458 decode_ie(cmsg->HLC,
1459 commands[AT_BC] + lbc + 12);
1460 else /* HLC derived from CIP */
1461 strcpy(commands[AT_BC] + lbc + 12,
1462 cip2bchlc[cmsg->CIPValue].hlc);
1463 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001464 strcpy(commands[AT_BC] + l - 2, "\r");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001465 } else {
1466 /* no BC */
1467 if (lhlc) {
1468 dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1469 "CONNECT_REQ");
1470 info = CapiIllMessageParmCoding; /* ? */
1471 goto error;
1472 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001473 }
1474
1475 /* check/encode parameter: B Protocol */
1476 if (cmsg->BProtocol == CAPI_DEFAULT) {
1477 bcs->proto2 = L2_HDLC;
1478 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001479 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001480 } else {
1481 switch (cmsg->B1protocol) {
1482 case 0:
1483 bcs->proto2 = L2_HDLC;
1484 break;
1485 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001486 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001487 break;
1488 default:
1489 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001490 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001491 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001492 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001493 }
1494 if (cmsg->B2protocol != 1)
1495 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001496 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001497 cmsg->B2protocol);
1498 if (cmsg->B3protocol != 0)
1499 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001500 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001501 cmsg->B3protocol);
1502 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001503 "CONNECT_REQ", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001504 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001505 "CONNECT_REQ", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001506 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001507 "CONNECT_REQ", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001508 }
1509 commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1510 if (!commands[AT_PROTO])
1511 goto oom;
1512 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1513
1514 /* ToDo: check/encode remaining parameters */
1515 ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001516 "CONNECT_REQ", "Called pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001517 ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001518 "CONNECT_REQ", "Calling pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001519 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001520 "CONNECT_REQ", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001521 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1522 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001523 "CONNECT_REQ", "B Channel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001524 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001525 "CONNECT_REQ", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001526 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001527 "CONNECT_REQ", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001528 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001529 "CONNECT_REQ", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001530 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001531
1532 /* encode parameter: B channel to use */
1533 commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1534 if (!commands[AT_ISO])
1535 goto oom;
1536 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1537 (unsigned) bcs->channel + 1);
1538
1539 /* queue & schedule EV_DIAL event */
1540 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
Tilman Schmidt1528b182010-02-22 13:09:52 +00001541 bcs->at_state.seq_index, NULL)) {
1542 info = CAPI_MSGOSRESOURCEERR;
1543 goto error;
1544 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001545 gigaset_schedule_event(cs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001546 send_conf(iif, ap, skb, CapiSuccess);
1547 return;
1548
1549oom:
1550 dev_err(cs->dev, "%s: out of memory\n", __func__);
1551 info = CAPI_MSGOSRESOURCEERR;
1552error:
1553 if (commands)
1554 for (i = 0; i < AT_NUM; i++)
1555 kfree(commands[i]);
1556 kfree(commands);
1557 gigaset_free_channel(bcs);
1558 send_conf(iif, ap, skb, info);
1559}
1560
1561/*
1562 * process CONNECT_RESP message
1563 * checks protocol parameters and queues an ACCEPT or HUP event
1564 */
1565static void do_connect_resp(struct gigaset_capi_ctr *iif,
1566 struct gigaset_capi_appl *ap,
1567 struct sk_buff *skb)
1568{
1569 struct cardstate *cs = iif->ctr.driverdata;
1570 _cmsg *cmsg = &iif->acmsg;
1571 struct bc_state *bcs;
1572 struct gigaset_capi_appl *oap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001573 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001574 int channel;
1575
1576 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001577 capi_message2cmsg(cmsg, skb->data);
1578 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001579 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001580
1581 /* extract and check channel number from PLCI */
1582 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1583 if (!channel || channel > cs->channels) {
1584 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1585 "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1586 return;
1587 }
1588 bcs = cs->bcs + channel - 1;
1589
1590 switch (cmsg->Reject) {
1591 case 0: /* Accept */
1592 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001593 spin_lock_irqsave(&bcs->aplock, flags);
1594 while (bcs->ap != NULL) {
1595 oap = bcs->ap;
1596 bcs->ap = oap->bcnext;
1597 if (oap != ap) {
1598 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001599 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001600 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001601 spin_lock_irqsave(&bcs->aplock, flags);
1602 }
1603 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001604 ap->bcnext = NULL;
1605 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001606 spin_unlock_irqrestore(&bcs->aplock, flags);
1607
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001608 bcs->rx_bufsize = ap->rp.datablklen;
1609 dev_kfree_skb(bcs->rx_skb);
1610 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001611 bcs->chstate |= CHS_NOTIFY_LL;
1612
1613 /* check/encode B channel protocol */
1614 if (cmsg->BProtocol == CAPI_DEFAULT) {
1615 bcs->proto2 = L2_HDLC;
1616 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001617 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001618 } else {
1619 switch (cmsg->B1protocol) {
1620 case 0:
1621 bcs->proto2 = L2_HDLC;
1622 break;
1623 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001624 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001625 break;
1626 default:
1627 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001628 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001629 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001630 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001631 }
1632 if (cmsg->B2protocol != 1)
1633 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001634 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001635 cmsg->B2protocol);
1636 if (cmsg->B3protocol != 0)
1637 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001638 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001639 cmsg->B3protocol);
1640 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001641 "CONNECT_RESP", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001642 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001643 "CONNECT_RESP", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001644 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001645 "CONNECT_RESP", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001646 }
1647
1648 /* ToDo: check/encode remaining parameters */
1649 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
Joe Perches475be4d2012-02-19 19:52:38 -08001650 "CONNECT_RESP", "Connected Number");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001651 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001652 "CONNECT_RESP", "Connected Subaddress");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001653 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001654 "CONNECT_RESP", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001655 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1656 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001657 "CONNECT_RESP", "BChannel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001658 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001659 "CONNECT_RESP", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001660 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001661 "CONNECT_RESP", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001662 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001663 "CONNECT_RESP", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001664 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001665
1666 /* Accept call */
Joe Perches475be4d2012-02-19 19:52:38 -08001667 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001668 EV_ACCEPT, NULL, 0, NULL))
1669 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001670 gigaset_schedule_event(cs);
1671 return;
1672
1673 case 1: /* Ignore */
1674 /* send DISCONNECT_IND to this application */
1675 send_disconnect_ind(bcs, ap, 0);
1676
1677 /* remove it from the list of listening apps */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001678 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001679 if (bcs->ap == ap) {
1680 bcs->ap = ap->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001681 if (bcs->ap == NULL) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001682 /* last one: stop ev-layer hupD notifications */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001683 bcs->apconnstate = APCONN_NONE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001684 bcs->chstate &= ~CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001685 }
1686 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001687 return;
1688 }
1689 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1690 if (oap->bcnext == ap) {
1691 oap->bcnext = oap->bcnext->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001692 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001693 return;
1694 }
1695 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001696 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001697 dev_err(cs->dev, "%s: application %u not found\n",
1698 __func__, ap->id);
1699 return;
1700
1701 default: /* Reject */
1702 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001703 spin_lock_irqsave(&bcs->aplock, flags);
1704 while (bcs->ap != NULL) {
1705 oap = bcs->ap;
1706 bcs->ap = oap->bcnext;
1707 if (oap != ap) {
1708 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001709 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001710 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001711 spin_lock_irqsave(&bcs->aplock, flags);
1712 }
1713 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001714 ap->bcnext = NULL;
1715 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001716 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001717
1718 /* reject call - will trigger DISCONNECT_IND for this app */
1719 dev_info(cs->dev, "%s: Reject=%x\n",
1720 "CONNECT_RESP", cmsg->Reject);
Joe Perches475be4d2012-02-19 19:52:38 -08001721 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001722 EV_HUP, NULL, 0, NULL))
1723 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001724 gigaset_schedule_event(cs);
1725 return;
1726 }
1727}
1728
1729/*
1730 * process CONNECT_B3_REQ message
1731 * build NCCI and emit CONNECT_B3_CONF reply
1732 */
1733static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1734 struct gigaset_capi_appl *ap,
1735 struct sk_buff *skb)
1736{
1737 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001738 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001739 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001740 int channel;
1741
1742 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001743 capi_message2cmsg(cmsg, skb->data);
1744 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001745
1746 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001747 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001748 if (!channel || channel > cs->channels) {
1749 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001750 "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001751 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1752 return;
1753 }
Joe Perches475be4d2012-02-19 19:52:38 -08001754 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001755
1756 /* mark logical connection active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001757 bcs->apconnstate = APCONN_ACTIVE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001758
1759 /* build NCCI: always 1 (one B3 connection only) */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001760 cmsg->adr.adrNCCI |= 1 << 16;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001761
1762 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001763 ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001764 send_conf(iif, ap, skb,
1765 (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08001766 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001767}
1768
1769/*
1770 * process CONNECT_B3_RESP message
1771 * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1772 * or queue EV_HUP and emit DISCONNECT_B3_IND.
1773 * The emitted message is always shorter than the received one,
1774 * allowing to reuse the skb.
1775 */
1776static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1777 struct gigaset_capi_appl *ap,
1778 struct sk_buff *skb)
1779{
1780 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001781 _cmsg *cmsg = &iif->acmsg;
1782 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001783 int channel;
1784 unsigned int msgsize;
1785 u8 command;
1786
1787 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001788 capi_message2cmsg(cmsg, skb->data);
1789 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001790
1791 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001792 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001793 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00001794 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001795 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001796 "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001797 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001798 return;
1799 }
Joe Perches475be4d2012-02-19 19:52:38 -08001800 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001801
Tilman Schmidt6c911912009-10-25 09:29:37 +00001802 if (cmsg->Reject) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001803 /* Reject: clear B3 connect received flag */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001804 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001805
1806 /* trigger hangup, causing eventual DISCONNECT_IND */
1807 if (!gigaset_add_event(cs, &bcs->at_state,
1808 EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001809 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001810 return;
1811 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001812 gigaset_schedule_event(cs);
1813
1814 /* emit DISCONNECT_B3_IND */
1815 command = CAPI_DISCONNECT_B3;
1816 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1817 } else {
1818 /*
1819 * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1820 * we only send CONNECT_B3_IND if the B channel is up
1821 */
1822 command = CAPI_CONNECT_B3_ACTIVE;
1823 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1824 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001825 capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1826 ap->nextMessageNumber++, cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001827 __skb_trim(skb, msgsize);
Tilman Schmidt6c911912009-10-25 09:29:37 +00001828 capi_cmsg2message(cmsg, skb->data);
1829 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001830 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1831}
1832
1833/*
1834 * process DISCONNECT_REQ message
1835 * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1836 * emit DISCONNECT_CONF reply
1837 */
1838static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1839 struct gigaset_capi_appl *ap,
1840 struct sk_buff *skb)
1841{
1842 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001843 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001844 struct bc_state *bcs;
1845 _cmsg *b3cmsg;
1846 struct sk_buff *b3skb;
1847 int channel;
1848
1849 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001850 capi_message2cmsg(cmsg, skb->data);
1851 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001852
1853 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001854 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001855 if (!channel || channel > cs->channels) {
1856 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001857 "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001858 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1859 return;
1860 }
1861 bcs = cs->bcs + channel - 1;
1862
1863 /* ToDo: process parameter: Additional info */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001864 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1865 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1866 "DISCONNECT_REQ", "B Channel Information");
1867 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1868 "DISCONNECT_REQ", "Keypad Facility");
1869 ignore_cstruct_param(cs, cmsg->Useruserdata,
1870 "DISCONNECT_REQ", "User-User Data");
1871 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1872 "DISCONNECT_REQ", "Facility Data Array");
1873 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001874
1875 /* skip if DISCONNECT_IND already sent */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001876 if (!bcs->apconnstate)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001877 return;
1878
1879 /* check for active logical connection */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001880 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt62a1cfe2012-04-25 13:02:20 +00001881 /* clear it */
1882 bcs->apconnstate = APCONN_SETUP;
1883
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001884 /*
1885 * emit DISCONNECT_B3_IND with cause 0x3301
1886 * use separate cmsg structure, as the content of iif->acmsg
1887 * is still needed for creating the _CONF message
1888 */
1889 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1890 if (!b3cmsg) {
1891 dev_err(cs->dev, "%s: out of memory\n", __func__);
1892 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1893 return;
1894 }
1895 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1896 ap->nextMessageNumber++,
Tilman Schmidt6c911912009-10-25 09:29:37 +00001897 cmsg->adr.adrPLCI | (1 << 16));
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001898 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1899 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1900 if (b3skb == NULL) {
1901 dev_err(cs->dev, "%s: out of memory\n", __func__);
1902 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
Jesper Juhl2393c942010-12-26 09:59:58 +00001903 kfree(b3cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001904 return;
1905 }
1906 capi_cmsg2message(b3cmsg,
Joe Perches475be4d2012-02-19 19:52:38 -08001907 __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN));
Tilman Schmidt62a1cfe2012-04-25 13:02:20 +00001908 dump_cmsg(DEBUG_CMD, __func__, b3cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001909 kfree(b3cmsg);
1910 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1911 }
1912
1913 /* trigger hangup, causing eventual DISCONNECT_IND */
1914 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001915 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1916 return;
1917 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001918 gigaset_schedule_event(cs);
1919
1920 /* emit reply */
1921 send_conf(iif, ap, skb, CapiSuccess);
1922}
1923
1924/*
1925 * process DISCONNECT_B3_REQ message
1926 * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
1927 */
1928static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
1929 struct gigaset_capi_appl *ap,
1930 struct sk_buff *skb)
1931{
1932 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001933 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001934 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001935 int channel;
1936
1937 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001938 capi_message2cmsg(cmsg, skb->data);
1939 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001940
1941 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001942 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001943 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00001944 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001945 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001946 "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001947 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1948 return;
1949 }
Joe Perches475be4d2012-02-19 19:52:38 -08001950 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001951
1952 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001953 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001954 send_conf(iif, ap, skb,
1955 CapiMessageNotSupportedInCurrentState);
1956 return;
1957 }
1958
1959 /* trigger hangup, causing eventual DISCONNECT_B3_IND */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001960 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001961 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1962 return;
1963 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001964 gigaset_schedule_event(cs);
1965
1966 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001967 ignore_cstruct_param(cs, cmsg->NCPI,
Joe Perches475be4d2012-02-19 19:52:38 -08001968 "DISCONNECT_B3_REQ", "NCPI");
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001969 send_conf(iif, ap, skb,
1970 (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08001971 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001972}
1973
1974/*
1975 * process DATA_B3_REQ message
1976 */
1977static void do_data_b3_req(struct gigaset_capi_ctr *iif,
1978 struct gigaset_capi_appl *ap,
1979 struct sk_buff *skb)
1980{
1981 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001982 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001983 int channel = CAPIMSG_PLCI_PART(skb->data);
1984 u16 ncci = CAPIMSG_NCCI_PART(skb->data);
1985 u16 msglen = CAPIMSG_LEN(skb->data);
1986 u16 datalen = CAPIMSG_DATALEN(skb->data);
1987 u16 flags = CAPIMSG_FLAGS(skb->data);
Tilman Schmidt23b36772010-06-21 13:54:50 +00001988 u16 msgid = CAPIMSG_MSGID(skb->data);
1989 u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001990
1991 /* frequent message, avoid _cmsg overhead */
Tilman Schmidt6a753422010-07-05 14:18:59 +00001992 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001993
1994 /* check parameters */
1995 if (channel == 0 || channel > cs->channels || ncci != 1) {
1996 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1997 "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
1998 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1999 return;
2000 }
Joe Perches475be4d2012-02-19 19:52:38 -08002001 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002002 if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
2003 dev_notice(cs->dev, "%s: unexpected length %d\n",
2004 "DATA_B3_REQ", msglen);
2005 if (msglen + datalen != skb->len)
2006 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
2007 "DATA_B3_REQ", msglen, datalen, skb->len);
2008 if (msglen + datalen > skb->len) {
2009 /* message too short for announced data length */
2010 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
2011 return;
2012 }
2013 if (flags & CAPI_FLAGS_RESERVED) {
2014 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
2015 "DATA_B3_REQ", flags);
2016 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
2017 return;
2018 }
2019
2020 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002021 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002022 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2023 return;
2024 }
2025
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002026 /* pull CAPI message into link layer header */
2027 skb_reset_mac_header(skb);
2028 skb->mac_len = msglen;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002029 skb_pull(skb, msglen);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002030
2031 /* pass to device-specific module */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002032 if (cs->ops->send_skb(bcs, skb) < 0) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002033 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2034 return;
2035 }
2036
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002037 /*
Tilman Schmidt23b36772010-06-21 13:54:50 +00002038 * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
2039 * confirmation" bit is set; otherwise we have to send it now
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002040 */
Tilman Schmidt23b36772010-06-21 13:54:50 +00002041 if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
2042 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
2043 flags ? CapiFlagsNotSupportedByProtocol
Joe Perches475be4d2012-02-19 19:52:38 -08002044 : CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002045}
2046
2047/*
2048 * process RESET_B3_REQ message
2049 * just always reply "not supported by current protocol"
2050 */
2051static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
2052 struct gigaset_capi_appl *ap,
2053 struct sk_buff *skb)
2054{
2055 /* decode message */
2056 capi_message2cmsg(&iif->acmsg, skb->data);
2057 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2058 send_conf(iif, ap, skb,
2059 CapiResetProcedureNotSupportedByCurrentProtocol);
2060}
2061
2062/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002063 * unsupported CAPI message handler
2064 */
2065static void do_unsupported(struct gigaset_capi_ctr *iif,
2066 struct gigaset_capi_appl *ap,
2067 struct sk_buff *skb)
2068{
2069 /* decode message */
2070 capi_message2cmsg(&iif->acmsg, skb->data);
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002071 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002072 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2073}
2074
2075/*
2076 * CAPI message handler: no-op
2077 */
2078static void do_nothing(struct gigaset_capi_ctr *iif,
2079 struct gigaset_capi_appl *ap,
2080 struct sk_buff *skb)
2081{
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002082 /* decode message */
2083 capi_message2cmsg(&iif->acmsg, skb->data);
2084 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002085 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002086}
2087
2088static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
2089 struct gigaset_capi_appl *ap,
2090 struct sk_buff *skb)
2091{
Tilman Schmidt6a753422010-07-05 14:18:59 +00002092 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002093 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002094}
2095
2096/* table of outgoing CAPI message handlers with lookup function */
2097typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
2098 struct gigaset_capi_appl *,
2099 struct sk_buff *);
2100
2101static struct {
2102 u16 cmd;
2103 capi_send_handler_t handler;
2104} capi_send_handler_table[] = {
2105 /* most frequent messages first for faster lookup */
2106 { CAPI_DATA_B3_REQ, do_data_b3_req },
2107 { CAPI_DATA_B3_RESP, do_data_b3_resp },
2108
2109 { CAPI_ALERT_REQ, do_alert_req },
2110 { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
2111 { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
2112 { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
2113 { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
2114 { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
2115 { CAPI_CONNECT_REQ, do_connect_req },
2116 { CAPI_CONNECT_RESP, do_connect_resp },
2117 { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
2118 { CAPI_DISCONNECT_B3_RESP, do_nothing },
2119 { CAPI_DISCONNECT_REQ, do_disconnect_req },
2120 { CAPI_DISCONNECT_RESP, do_nothing },
2121 { CAPI_FACILITY_REQ, do_facility_req },
2122 { CAPI_FACILITY_RESP, do_nothing },
2123 { CAPI_LISTEN_REQ, do_listen_req },
2124 { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
2125 { CAPI_RESET_B3_REQ, do_reset_b3_req },
2126 { CAPI_RESET_B3_RESP, do_nothing },
2127
2128 /*
2129 * ToDo: support overlap sending (requires ev-layer state
2130 * machine extension to generate additional ATD commands)
2131 */
2132 { CAPI_INFO_REQ, do_unsupported },
2133 { CAPI_INFO_RESP, do_nothing },
2134
2135 /*
2136 * ToDo: what's the proper response for these?
2137 */
2138 { CAPI_MANUFACTURER_REQ, do_nothing },
2139 { CAPI_MANUFACTURER_RESP, do_nothing },
2140};
2141
2142/* look up handler */
2143static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
2144{
2145 size_t i;
2146
2147 for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2148 if (capi_send_handler_table[i].cmd == cmd)
2149 return capi_send_handler_table[i].handler;
2150 return NULL;
2151}
2152
2153
2154/**
2155 * gigaset_send_message() - accept a CAPI message from an application
2156 * @ctr: controller descriptor structure.
2157 * @skb: CAPI message.
2158 *
2159 * Return value: CAPI error code
2160 * Note: capidrv (and probably others, too) only uses the return value to
2161 * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2162 */
2163static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2164{
2165 struct gigaset_capi_ctr *iif
2166 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2167 struct cardstate *cs = ctr->driverdata;
2168 struct gigaset_capi_appl *ap;
2169 capi_send_handler_t handler;
2170
2171 /* can only handle linear sk_buffs */
2172 if (skb_linearize(skb) < 0) {
2173 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2174 return CAPI_MSGOSRESOURCEERR;
2175 }
2176
2177 /* retrieve application data structure */
2178 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2179 if (!ap) {
2180 dev_notice(cs->dev, "%s: application %u not registered\n",
2181 __func__, CAPIMSG_APPID(skb->data));
2182 return CAPI_ILLAPPNR;
2183 }
2184
2185 /* look up command */
2186 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2187 if (!handler) {
2188 /* unknown/unsupported message type */
2189 if (printk_ratelimit())
2190 dev_notice(cs->dev, "%s: unsupported message %u\n",
2191 __func__, CAPIMSG_CMD(skb->data));
2192 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2193 }
2194
2195 /* serialize */
2196 if (atomic_add_return(1, &iif->sendqlen) > 1) {
2197 /* queue behind other messages */
2198 skb_queue_tail(&iif->sendqueue, skb);
2199 return CAPI_NOERROR;
2200 }
2201
2202 /* process message */
2203 handler(iif, ap, skb);
2204
2205 /* process other messages arrived in the meantime */
2206 while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2207 skb = skb_dequeue(&iif->sendqueue);
2208 if (!skb) {
2209 /* should never happen */
2210 dev_err(cs->dev, "%s: send queue empty\n", __func__);
2211 continue;
2212 }
2213 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2214 if (!ap) {
2215 /* could that happen? */
2216 dev_warn(cs->dev, "%s: application %u vanished\n",
2217 __func__, CAPIMSG_APPID(skb->data));
2218 continue;
2219 }
2220 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2221 if (!handler) {
2222 /* should never happen */
2223 dev_err(cs->dev, "%s: handler %x vanished\n",
2224 __func__, CAPIMSG_CMD(skb->data));
2225 continue;
2226 }
2227 handler(iif, ap, skb);
2228 }
2229
2230 return CAPI_NOERROR;
2231}
2232
2233/**
2234 * gigaset_procinfo() - build single line description for controller
2235 * @ctr: controller descriptor structure.
2236 *
2237 * Return value: pointer to generated string (null terminated)
2238 */
2239static char *gigaset_procinfo(struct capi_ctr *ctr)
2240{
2241 return ctr->name; /* ToDo: more? */
2242}
2243
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002244static int gigaset_proc_show(struct seq_file *m, void *v)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002245{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002246 struct capi_ctr *ctr = m->private;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002247 struct cardstate *cs = ctr->driverdata;
2248 char *s;
2249 int i;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002250
2251 seq_printf(m, "%-16s %s\n", "name", ctr->name);
2252 seq_printf(m, "%-16s %s %s\n", "dev",
Joe Perches475be4d2012-02-19 19:52:38 -08002253 dev_driver_string(cs->dev), dev_name(cs->dev));
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002254 seq_printf(m, "%-16s %d\n", "id", cs->myid);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002255 if (cs->gotfwver)
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002256 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
Joe Perches475be4d2012-02-19 19:52:38 -08002257 cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002258 seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2259 seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002260
2261 switch (cs->mode) {
2262 case M_UNKNOWN:
2263 s = "unknown";
2264 break;
2265 case M_CONFIG:
2266 s = "config";
2267 break;
2268 case M_UNIMODEM:
2269 s = "Unimodem";
2270 break;
2271 case M_CID:
2272 s = "CID";
2273 break;
2274 default:
2275 s = "??";
2276 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002277 seq_printf(m, "%-16s %s\n", "mode", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002278
2279 switch (cs->mstate) {
2280 case MS_UNINITIALIZED:
2281 s = "uninitialized";
2282 break;
2283 case MS_INIT:
2284 s = "init";
2285 break;
2286 case MS_LOCKED:
2287 s = "locked";
2288 break;
2289 case MS_SHUTDOWN:
2290 s = "shutdown";
2291 break;
2292 case MS_RECOVER:
2293 s = "recover";
2294 break;
2295 case MS_READY:
2296 s = "ready";
2297 break;
2298 default:
2299 s = "??";
2300 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002301 seq_printf(m, "%-16s %s\n", "mstate", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002302
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002303 seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2304 seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2305 seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2306 seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002307
2308 for (i = 0; i < cs->channels; i++) {
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002309 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
Joe Perches475be4d2012-02-19 19:52:38 -08002310 cs->bcs[i].corrupted);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002311 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
Joe Perches475be4d2012-02-19 19:52:38 -08002312 cs->bcs[i].trans_down);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002313 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
Joe Perches475be4d2012-02-19 19:52:38 -08002314 cs->bcs[i].trans_up);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002315 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
Joe Perches475be4d2012-02-19 19:52:38 -08002316 cs->bcs[i].chstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002317 switch (cs->bcs[i].proto2) {
2318 case L2_BITSYNC:
2319 s = "bitsync";
2320 break;
2321 case L2_HDLC:
2322 s = "HDLC";
2323 break;
2324 case L2_VOICE:
2325 s = "voice";
2326 break;
2327 default:
2328 s = "??";
2329 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002330 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002331 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002332 return 0;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002333}
2334
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002335static int gigaset_proc_open(struct inode *inode, struct file *file)
2336{
Al Virod9dda782013-03-31 18:16:14 -04002337 return single_open(file, gigaset_proc_show, PDE_DATA(inode));
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002338}
2339
2340static const struct file_operations gigaset_proc_fops = {
2341 .owner = THIS_MODULE,
2342 .open = gigaset_proc_open,
2343 .read = seq_read,
2344 .llseek = seq_lseek,
2345 .release = single_release,
2346};
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002347
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002348/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002349 * gigaset_isdn_regdev() - register device to LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002350 * @cs: device descriptor structure.
2351 * @isdnid: device name.
2352 *
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002353 * Return value: 0 on success, error code < 0 on failure
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002354 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002355int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002356{
2357 struct gigaset_capi_ctr *iif;
2358 int rc;
2359
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002360 iif = kmalloc(sizeof(*iif), GFP_KERNEL);
2361 if (!iif) {
2362 pr_err("%s: out of memory\n", __func__);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002363 return -ENOMEM;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002364 }
2365
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002366 /* prepare controller structure */
2367 iif->ctr.owner = THIS_MODULE;
2368 iif->ctr.driverdata = cs;
2369 strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name));
2370 iif->ctr.driver_name = "gigaset";
Tilman Schmidte4876392010-05-23 01:02:38 +00002371 iif->ctr.load_firmware = NULL;
2372 iif->ctr.reset_ctr = NULL;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002373 iif->ctr.register_appl = gigaset_register_appl;
2374 iif->ctr.release_appl = gigaset_release_appl;
2375 iif->ctr.send_message = gigaset_send_message;
2376 iif->ctr.procinfo = gigaset_procinfo;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002377 iif->ctr.proc_fops = &gigaset_proc_fops;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002378 INIT_LIST_HEAD(&iif->appls);
2379 skb_queue_head_init(&iif->sendqueue);
2380 atomic_set(&iif->sendqlen, 0);
2381
2382 /* register controller with CAPI */
2383 rc = attach_capi_ctr(&iif->ctr);
2384 if (rc) {
2385 pr_err("attach_capi_ctr failed (%d)\n", rc);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002386 kfree(iif);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002387 return rc;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002388 }
2389
2390 cs->iif = iif;
2391 cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002392 return 0;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002393}
2394
2395/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002396 * gigaset_isdn_unregdev() - unregister device from LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002397 * @cs: device descriptor structure.
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002398 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002399void gigaset_isdn_unregdev(struct cardstate *cs)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002400{
2401 struct gigaset_capi_ctr *iif = cs->iif;
2402
2403 detach_capi_ctr(&iif->ctr);
2404 kfree(iif);
2405 cs->iif = NULL;
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002406}
2407
2408static struct capi_driver capi_driver_gigaset = {
2409 .name = "gigaset",
2410 .revision = "1.0",
2411};
2412
2413/**
2414 * gigaset_isdn_regdrv() - register driver to LL
2415 */
2416void gigaset_isdn_regdrv(void)
2417{
2418 pr_info("Kernel CAPI interface\n");
2419 register_capi_driver(&capi_driver_gigaset);
2420}
2421
2422/**
2423 * gigaset_isdn_unregdrv() - unregister driver from LL
2424 */
2425void gigaset_isdn_unregdrv(void)
2426{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002427 unregister_capi_driver(&capi_driver_gigaset);
2428}