blob: ccec7778cad23d2eddf1664deefccd95dc45f1a5 [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;
Tilman Schmidt097933d2014-10-11 13:46:29 +0200253 if (l > 64)
254 l = 64; /* arbitrary limit */
Joe Perches475be4d2012-02-19 19:52:38 -0800255 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000256 if (!dbgline)
257 return;
258 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800259 dbgline[3 * i] = hex_asc_hi(data[12 + i]);
260 dbgline[3 * i + 1] = hex_asc_lo(data[12 + i]);
261 dbgline[3 * i + 2] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000262 }
Joe Perches475be4d2012-02-19 19:52:38 -0800263 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000264 gig_dbg(level, " %s", dbgline);
265 kfree(dbgline);
266 if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
267 (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
Tilman Schmidt6a753422010-07-05 14:18:59 +0000268 CAPIMSG_SUBCOMMAND(data) == CAPI_IND)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000269 l = CAPIMSG_DATALEN(data);
Tilman Schmidt6a753422010-07-05 14:18:59 +0000270 gig_dbg(level, " DataLength=%d", l);
271 if (l <= 0 || !(gigaset_debuglevel & DEBUG_LLDATA))
272 return;
273 if (l > 64)
274 l = 64; /* arbitrary limit */
Joe Perches475be4d2012-02-19 19:52:38 -0800275 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000276 if (!dbgline)
277 return;
278 data += CAPIMSG_LEN(data);
279 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800280 dbgline[3 * i] = hex_asc_hi(data[i]);
281 dbgline[3 * i + 1] = hex_asc_lo(data[i]);
282 dbgline[3 * i + 2] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000283 }
Joe Perches475be4d2012-02-19 19:52:38 -0800284 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000285 gig_dbg(level, " %s", dbgline);
286 kfree(dbgline);
287 }
288#endif
289}
290
291/*
292 * format CAPI IE as string
293 */
294
Paul Bolle6ba60472012-07-14 23:11:55 +0000295#ifdef CONFIG_GIGASET_DEBUG
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000296static const char *format_ie(const char *ie)
297{
Joe Perches475be4d2012-02-19 19:52:38 -0800298 static char result[3 * MAX_FMT_IE_LEN];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000299 int len, count;
300 char *pout = result;
301
302 if (!ie)
303 return "NULL";
304
305 count = len = ie[0];
306 if (count > MAX_FMT_IE_LEN)
Joe Perches475be4d2012-02-19 19:52:38 -0800307 count = MAX_FMT_IE_LEN - 1;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000308 while (count--) {
309 *pout++ = hex_asc_hi(*++ie);
310 *pout++ = hex_asc_lo(*ie);
311 *pout++ = ' ';
312 }
313 if (len > MAX_FMT_IE_LEN) {
314 *pout++ = '.';
315 *pout++ = '.';
316 *pout++ = '.';
317 }
318 *--pout = 0;
319 return result;
320}
Paul Bolle6ba60472012-07-14 23:11:55 +0000321#endif
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000322
Tilman Schmidt23b36772010-06-21 13:54:50 +0000323/*
324 * emit DATA_B3_CONF message
325 */
326static void send_data_b3_conf(struct cardstate *cs, struct capi_ctr *ctr,
327 u16 appl, u16 msgid, int channel,
328 u16 handle, u16 info)
329{
330 struct sk_buff *cskb;
331 u8 *msg;
332
333 cskb = alloc_skb(CAPI_DATA_B3_CONF_LEN, GFP_ATOMIC);
334 if (!cskb) {
335 dev_err(cs->dev, "%s: out of memory\n", __func__);
336 return;
337 }
338 /* frequent message, avoid _cmsg overhead */
339 msg = __skb_put(cskb, CAPI_DATA_B3_CONF_LEN);
340 CAPIMSG_SETLEN(msg, CAPI_DATA_B3_CONF_LEN);
341 CAPIMSG_SETAPPID(msg, appl);
342 CAPIMSG_SETCOMMAND(msg, CAPI_DATA_B3);
343 CAPIMSG_SETSUBCOMMAND(msg, CAPI_CONF);
344 CAPIMSG_SETMSGID(msg, msgid);
345 CAPIMSG_SETCONTROLLER(msg, ctr->cnr);
346 CAPIMSG_SETPLCI_PART(msg, channel);
347 CAPIMSG_SETNCCI_PART(msg, 1);
348 CAPIMSG_SETHANDLE_CONF(msg, handle);
349 CAPIMSG_SETINFO_CONF(msg, info);
350
351 /* emit message */
352 dump_rawmsg(DEBUG_MCMD, __func__, msg);
353 capi_ctr_handle_message(ctr, appl, cskb);
354}
355
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000356
357/*
358 * driver interface functions
359 * ==========================
360 */
361
362/**
363 * gigaset_skb_sent() - acknowledge transmission of outgoing skb
364 * @bcs: B channel descriptor structure.
365 * @skb: sent data.
366 *
367 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
368 * skb has been successfully sent, for signalling completion to the LL.
369 */
370void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *dskb)
371{
372 struct cardstate *cs = bcs->cs;
373 struct gigaset_capi_ctr *iif = cs->iif;
374 struct gigaset_capi_appl *ap = bcs->ap;
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000375 unsigned char *req = skb_mac_header(dskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000376 u16 flags;
377
378 /* update statistics */
379 ++bcs->trans_up;
380
381 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000382 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000383 return;
384 }
385
386 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000387 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000388 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000389 return;
390 }
391
Tilman Schmidt23b36772010-06-21 13:54:50 +0000392 /*
393 * send DATA_B3_CONF if "delivery confirmation" bit was set in request;
394 * otherwise it has already been sent by do_data_b3_req()
395 */
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000396 flags = CAPIMSG_FLAGS(req);
Tilman Schmidt23b36772010-06-21 13:54:50 +0000397 if (flags & CAPI_FLAGS_DELIVERY_CONFIRMATION)
398 send_data_b3_conf(cs, &iif->ctr, ap->id, CAPIMSG_MSGID(req),
399 bcs->channel + 1, CAPIMSG_HANDLE_REQ(req),
400 (flags & ~CAPI_FLAGS_DELIVERY_CONFIRMATION) ?
Joe Perches475be4d2012-02-19 19:52:38 -0800401 CapiFlagsNotSupportedByProtocol :
402 CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000403}
404EXPORT_SYMBOL_GPL(gigaset_skb_sent);
405
406/**
407 * gigaset_skb_rcvd() - pass received skb to LL
408 * @bcs: B channel descriptor structure.
409 * @skb: received data.
410 *
411 * Called by hardware module {bas,ser,usb}_gigaset when user data has
412 * been successfully received, for passing to the LL.
413 * Warning: skb must not be accessed anymore!
414 */
415void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
416{
417 struct cardstate *cs = bcs->cs;
418 struct gigaset_capi_ctr *iif = cs->iif;
419 struct gigaset_capi_appl *ap = bcs->ap;
420 int len = skb->len;
421
422 /* update statistics */
423 bcs->trans_down++;
424
425 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000426 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
427 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000428 return;
429 }
430
431 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000432 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000433 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000434 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000435 return;
436 }
437
438 /*
439 * prepend DATA_B3_IND message to payload
440 * Parameters: NCCI = 1, all others 0/unused
441 * frequent message, avoid _cmsg overhead
442 */
443 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
444 CAPIMSG_SETLEN(skb->data, CAPI_DATA_B3_REQ_LEN);
445 CAPIMSG_SETAPPID(skb->data, ap->id);
446 CAPIMSG_SETCOMMAND(skb->data, CAPI_DATA_B3);
447 CAPIMSG_SETSUBCOMMAND(skb->data, CAPI_IND);
448 CAPIMSG_SETMSGID(skb->data, ap->nextMessageNumber++);
449 CAPIMSG_SETCONTROLLER(skb->data, iif->ctr.cnr);
450 CAPIMSG_SETPLCI_PART(skb->data, bcs->channel + 1);
451 CAPIMSG_SETNCCI_PART(skb->data, 1);
452 /* Data parameter not used */
453 CAPIMSG_SETDATALEN(skb->data, len);
454 /* Data handle parameter not used */
455 CAPIMSG_SETFLAGS(skb->data, 0);
456 /* Data64 parameter not present */
457
458 /* emit message */
Tilman Schmidt6a753422010-07-05 14:18:59 +0000459 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000460 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
461}
462EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
463
464/**
465 * gigaset_isdn_rcv_err() - signal receive error
466 * @bcs: B channel descriptor structure.
467 *
468 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
469 * has occurred, for signalling to the LL.
470 */
471void gigaset_isdn_rcv_err(struct bc_state *bcs)
472{
473 /* if currently ignoring packets, just count down */
474 if (bcs->ignore) {
475 bcs->ignore--;
476 return;
477 }
478
479 /* update statistics */
480 bcs->corrupted++;
481
482 /* ToDo: signal error -> LL */
483}
484EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
485
486/**
487 * gigaset_isdn_icall() - signal incoming call
488 * @at_state: connection state structure.
489 *
490 * Called by main module at tasklet level to notify the LL that an incoming
491 * call has been received. @at_state contains the parameters of the call.
492 *
493 * Return value: call disposition (ICALL_*)
494 */
495int gigaset_isdn_icall(struct at_state_t *at_state)
496{
497 struct cardstate *cs = at_state->cs;
498 struct bc_state *bcs = at_state->bcs;
499 struct gigaset_capi_ctr *iif = cs->iif;
500 struct gigaset_capi_appl *ap;
501 u32 actCIPmask;
502 struct sk_buff *skb;
503 unsigned int msgsize;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000504 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000505 int i;
506
507 /*
508 * ToDo: signal calls without a free B channel, too
509 * (requires a u8 handle for the at_state structure that can
510 * be stored in the PLCI and used in the CONNECT_RESP message
511 * handler to retrieve it)
512 */
513 if (!bcs)
514 return ICALL_IGNORE;
515
516 /* prepare CONNECT_IND message, using B channel number as PLCI */
517 capi_cmsg_header(&iif->hcmsg, 0, CAPI_CONNECT, CAPI_IND, 0,
518 iif->ctr.cnr | ((bcs->channel + 1) << 8));
519
520 /* minimum size, all structs empty */
521 msgsize = CAPI_CONNECT_IND_BASELEN;
522
523 /* Bearer Capability (mandatory) */
524 if (at_state->str_var[STR_ZBC]) {
525 /* pass on BC from Gigaset */
526 if (encode_ie(at_state->str_var[STR_ZBC], iif->bc_buf,
527 MAX_BC_OCTETS) < 0) {
528 dev_warn(cs->dev, "RING ignored - bad BC %s\n",
529 at_state->str_var[STR_ZBC]);
530 return ICALL_IGNORE;
531 }
532
533 /* look up corresponding CIP value */
534 iif->hcmsg.CIPValue = 0; /* default if nothing found */
535 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
536 if (cip2bchlc[i].bc != NULL &&
537 cip2bchlc[i].hlc == NULL &&
538 !strcmp(cip2bchlc[i].bc,
539 at_state->str_var[STR_ZBC])) {
540 iif->hcmsg.CIPValue = i;
541 break;
542 }
543 } else {
544 /* no BC (internal call): assume CIP 1 (speech, A-law) */
545 iif->hcmsg.CIPValue = 1;
546 encode_ie(cip2bchlc[1].bc, iif->bc_buf, MAX_BC_OCTETS);
547 }
548 iif->hcmsg.BC = iif->bc_buf;
549 msgsize += iif->hcmsg.BC[0];
550
551 /* High Layer Compatibility (optional) */
552 if (at_state->str_var[STR_ZHLC]) {
553 /* pass on HLC from Gigaset */
554 if (encode_ie(at_state->str_var[STR_ZHLC], iif->hlc_buf,
555 MAX_HLC_OCTETS) < 0) {
556 dev_warn(cs->dev, "RING ignored - bad HLC %s\n",
557 at_state->str_var[STR_ZHLC]);
558 return ICALL_IGNORE;
559 }
560 iif->hcmsg.HLC = iif->hlc_buf;
561 msgsize += iif->hcmsg.HLC[0];
562
563 /* look up corresponding CIP value */
564 /* keep BC based CIP value if none found */
565 if (at_state->str_var[STR_ZBC])
566 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
567 if (cip2bchlc[i].hlc != NULL &&
568 !strcmp(cip2bchlc[i].hlc,
569 at_state->str_var[STR_ZHLC]) &&
570 !strcmp(cip2bchlc[i].bc,
571 at_state->str_var[STR_ZBC])) {
572 iif->hcmsg.CIPValue = i;
573 break;
574 }
575 }
576
577 /* Called Party Number (optional) */
578 if (at_state->str_var[STR_ZCPN]) {
579 i = strlen(at_state->str_var[STR_ZCPN]);
580 if (i > MAX_NUMBER_DIGITS) {
581 dev_warn(cs->dev, "RING ignored - bad number %s\n",
582 at_state->str_var[STR_ZBC]);
583 return ICALL_IGNORE;
584 }
585 iif->cdpty_buf[0] = i + 1;
586 iif->cdpty_buf[1] = 0x80; /* type / numbering plan unknown */
Joe Perches475be4d2012-02-19 19:52:38 -0800587 memcpy(iif->cdpty_buf + 2, at_state->str_var[STR_ZCPN], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000588 iif->hcmsg.CalledPartyNumber = iif->cdpty_buf;
589 msgsize += iif->hcmsg.CalledPartyNumber[0];
590 }
591
592 /* Calling Party Number (optional) */
593 if (at_state->str_var[STR_NMBR]) {
594 i = strlen(at_state->str_var[STR_NMBR]);
595 if (i > MAX_NUMBER_DIGITS) {
596 dev_warn(cs->dev, "RING ignored - bad number %s\n",
597 at_state->str_var[STR_ZBC]);
598 return ICALL_IGNORE;
599 }
600 iif->cgpty_buf[0] = i + 2;
601 iif->cgpty_buf[1] = 0x00; /* type / numbering plan unknown */
602 iif->cgpty_buf[2] = 0x80; /* pres. allowed, not screened */
Joe Perches475be4d2012-02-19 19:52:38 -0800603 memcpy(iif->cgpty_buf + 3, at_state->str_var[STR_NMBR], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000604 iif->hcmsg.CallingPartyNumber = iif->cgpty_buf;
605 msgsize += iif->hcmsg.CallingPartyNumber[0];
606 }
607
608 /* remaining parameters (not supported, always left NULL):
609 * - CalledPartySubaddress
610 * - CallingPartySubaddress
611 * - AdditionalInfo
612 * - BChannelinformation
613 * - Keypadfacility
614 * - Useruserdata
615 * - Facilitydataarray
616 */
617
618 gig_dbg(DEBUG_CMD, "icall: PLCI %x CIP %d BC %s",
619 iif->hcmsg.adr.adrPLCI, iif->hcmsg.CIPValue,
620 format_ie(iif->hcmsg.BC));
621 gig_dbg(DEBUG_CMD, "icall: HLC %s",
622 format_ie(iif->hcmsg.HLC));
623 gig_dbg(DEBUG_CMD, "icall: CgPty %s",
624 format_ie(iif->hcmsg.CallingPartyNumber));
625 gig_dbg(DEBUG_CMD, "icall: CdPty %s",
626 format_ie(iif->hcmsg.CalledPartyNumber));
627
628 /* scan application list for matching listeners */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000629 spin_lock_irqsave(&bcs->aplock, flags);
630 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE) {
631 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
632 __func__, bcs->ap, bcs->apconnstate);
633 bcs->ap = NULL;
634 bcs->apconnstate = APCONN_NONE;
635 }
636 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000637 actCIPmask = 1 | (1 << iif->hcmsg.CIPValue);
638 list_for_each_entry(ap, &iif->appls, ctrlist)
639 if (actCIPmask & ap->listenCIPmask) {
640 /* build CONNECT_IND message for this application */
641 iif->hcmsg.ApplId = ap->id;
642 iif->hcmsg.Messagenumber = ap->nextMessageNumber++;
643
644 skb = alloc_skb(msgsize, GFP_ATOMIC);
645 if (!skb) {
646 dev_err(cs->dev, "%s: out of memory\n",
647 __func__);
648 break;
649 }
Tilman Schmidt2bf3a092014-10-11 13:46:30 +0200650 if (capi_cmsg2message(&iif->hcmsg,
651 __skb_put(skb, msgsize))) {
652 dev_err(cs->dev, "%s: message parser failure\n",
653 __func__);
654 dev_kfree_skb_any(skb);
655 break;
656 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000657 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
658
659 /* add to listeners on this B channel, update state */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000660 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000661 ap->bcnext = bcs->ap;
662 bcs->ap = ap;
663 bcs->chstate |= CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000664 bcs->apconnstate = APCONN_SETUP;
665 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000666
667 /* emit message */
668 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
669 }
670
671 /*
672 * Return "accept" if any listeners.
673 * Gigaset will send ALERTING.
674 * There doesn't seem to be a way to avoid this.
675 */
676 return bcs->ap ? ICALL_ACCEPT : ICALL_IGNORE;
677}
678
679/*
680 * send a DISCONNECT_IND message to an application
681 * does not sleep, clobbers the controller's hcmsg structure
682 */
683static void send_disconnect_ind(struct bc_state *bcs,
684 struct gigaset_capi_appl *ap, u16 reason)
685{
686 struct cardstate *cs = bcs->cs;
687 struct gigaset_capi_ctr *iif = cs->iif;
688 struct sk_buff *skb;
689
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000690 if (bcs->apconnstate == APCONN_NONE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000691 return;
692
693 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT, CAPI_IND,
694 ap->nextMessageNumber++,
695 iif->ctr.cnr | ((bcs->channel + 1) << 8));
696 iif->hcmsg.Reason = reason;
697 skb = alloc_skb(CAPI_DISCONNECT_IND_LEN, GFP_ATOMIC);
698 if (!skb) {
699 dev_err(cs->dev, "%s: out of memory\n", __func__);
700 return;
701 }
Tilman Schmidt2bf3a092014-10-11 13:46:30 +0200702 if (capi_cmsg2message(&iif->hcmsg,
703 __skb_put(skb, CAPI_DISCONNECT_IND_LEN))) {
704 dev_err(cs->dev, "%s: message parser failure\n", __func__);
705 dev_kfree_skb_any(skb);
706 return;
707 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000708 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000709 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
710}
711
712/*
713 * send a DISCONNECT_B3_IND message to an application
714 * Parameters: NCCI = 1, NCPI empty, Reason_B3 = 0
715 * does not sleep, clobbers the controller's hcmsg structure
716 */
717static void send_disconnect_b3_ind(struct bc_state *bcs,
718 struct gigaset_capi_appl *ap)
719{
720 struct cardstate *cs = bcs->cs;
721 struct gigaset_capi_ctr *iif = cs->iif;
722 struct sk_buff *skb;
723
724 /* nothing to do if no logical connection active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000725 if (bcs->apconnstate < APCONN_ACTIVE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000726 return;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000727 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000728
729 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
730 ap->nextMessageNumber++,
731 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
732 skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_ATOMIC);
733 if (!skb) {
734 dev_err(cs->dev, "%s: out of memory\n", __func__);
735 return;
736 }
Tilman Schmidt2bf3a092014-10-11 13:46:30 +0200737 if (capi_cmsg2message(&iif->hcmsg,
738 __skb_put(skb, CAPI_DISCONNECT_B3_IND_BASELEN))) {
739 dev_err(cs->dev, "%s: message parser failure\n", __func__);
740 dev_kfree_skb_any(skb);
741 return;
742 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000743 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
744 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
745}
746
747/**
748 * gigaset_isdn_connD() - signal D channel connect
749 * @bcs: B channel descriptor structure.
750 *
751 * Called by main module at tasklet level to notify the LL that the D channel
752 * connection has been established.
753 */
754void gigaset_isdn_connD(struct bc_state *bcs)
755{
756 struct cardstate *cs = bcs->cs;
757 struct gigaset_capi_ctr *iif = cs->iif;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000758 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000759 struct sk_buff *skb;
760 unsigned int msgsize;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000761 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000762
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000763 spin_lock_irqsave(&bcs->aplock, flags);
764 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000765 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000766 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000767 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000768 return;
769 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000770 if (bcs->apconnstate == APCONN_NONE) {
771 spin_unlock_irqrestore(&bcs->aplock, flags);
772 dev_warn(cs->dev, "%s: application %u not connected\n",
773 __func__, ap->id);
774 return;
775 }
776 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000777 while (ap->bcnext) {
778 /* this should never happen */
779 dev_warn(cs->dev, "%s: dropping extra application %u\n",
780 __func__, ap->bcnext->id);
781 send_disconnect_ind(bcs, ap->bcnext,
782 CapiCallGivenToOtherApplication);
783 ap->bcnext = ap->bcnext->bcnext;
784 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000785
786 /* prepare CONNECT_ACTIVE_IND message
787 * Note: LLC not supported by device
788 */
789 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_CONNECT_ACTIVE, CAPI_IND,
790 ap->nextMessageNumber++,
791 iif->ctr.cnr | ((bcs->channel + 1) << 8));
792
793 /* minimum size, all structs empty */
794 msgsize = CAPI_CONNECT_ACTIVE_IND_BASELEN;
795
796 /* ToDo: set parameter: Connected number
797 * (requires ev-layer state machine extension to collect
798 * ZCON device reply)
799 */
800
801 /* build and emit CONNECT_ACTIVE_IND message */
802 skb = alloc_skb(msgsize, GFP_ATOMIC);
803 if (!skb) {
804 dev_err(cs->dev, "%s: out of memory\n", __func__);
805 return;
806 }
Tilman Schmidt2bf3a092014-10-11 13:46:30 +0200807 if (capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize))) {
808 dev_err(cs->dev, "%s: message parser failure\n", __func__);
809 dev_kfree_skb_any(skb);
810 return;
811 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000812 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
813 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
814}
815
816/**
817 * gigaset_isdn_hupD() - signal D channel hangup
818 * @bcs: B channel descriptor structure.
819 *
820 * Called by main module at tasklet level to notify the LL that the D channel
821 * connection has been shut down.
822 */
823void gigaset_isdn_hupD(struct bc_state *bcs)
824{
825 struct gigaset_capi_appl *ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000826 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000827
828 /*
829 * ToDo: pass on reason code reported by device
830 * (requires ev-layer state machine extension to collect
831 * ZCAU device reply)
832 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000833 spin_lock_irqsave(&bcs->aplock, flags);
834 while (bcs->ap != NULL) {
835 ap = bcs->ap;
836 bcs->ap = ap->bcnext;
837 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000838 send_disconnect_b3_ind(bcs, ap);
839 send_disconnect_ind(bcs, ap, 0);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000840 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000841 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000842 bcs->apconnstate = APCONN_NONE;
843 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000844}
845
846/**
847 * gigaset_isdn_connB() - signal B channel connect
848 * @bcs: B channel descriptor structure.
849 *
850 * Called by main module at tasklet level to notify the LL that the B channel
851 * connection has been established.
852 */
853void gigaset_isdn_connB(struct bc_state *bcs)
854{
855 struct cardstate *cs = bcs->cs;
856 struct gigaset_capi_ctr *iif = cs->iif;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000857 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000858 struct sk_buff *skb;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000859 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000860 unsigned int msgsize;
861 u8 command;
862
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000863 spin_lock_irqsave(&bcs->aplock, flags);
864 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000865 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000866 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000867 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000868 return;
869 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000870 if (!bcs->apconnstate) {
871 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000872 dev_warn(cs->dev, "%s: application %u not connected\n",
873 __func__, ap->id);
874 return;
875 }
876
877 /*
878 * emit CONNECT_B3_ACTIVE_IND if we already got CONNECT_B3_REQ;
879 * otherwise we have to emit CONNECT_B3_IND first, and follow up with
880 * CONNECT_B3_ACTIVE_IND in reply to CONNECT_B3_RESP
881 * Parameters in both cases always: NCCI = 1, NCPI empty
882 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000883 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000884 command = CAPI_CONNECT_B3_ACTIVE;
885 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
886 } else {
887 command = CAPI_CONNECT_B3;
888 msgsize = CAPI_CONNECT_B3_IND_BASELEN;
889 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000890 bcs->apconnstate = APCONN_ACTIVE;
891
892 spin_unlock_irqrestore(&bcs->aplock, flags);
893
894 while (ap->bcnext) {
895 /* this should never happen */
896 dev_warn(cs->dev, "%s: dropping extra application %u\n",
897 __func__, ap->bcnext->id);
898 send_disconnect_ind(bcs, ap->bcnext,
899 CapiCallGivenToOtherApplication);
900 ap->bcnext = ap->bcnext->bcnext;
901 }
902
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000903 capi_cmsg_header(&iif->hcmsg, ap->id, command, CAPI_IND,
904 ap->nextMessageNumber++,
905 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
906 skb = alloc_skb(msgsize, GFP_ATOMIC);
907 if (!skb) {
908 dev_err(cs->dev, "%s: out of memory\n", __func__);
909 return;
910 }
Tilman Schmidt2bf3a092014-10-11 13:46:30 +0200911 if (capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize))) {
912 dev_err(cs->dev, "%s: message parser failure\n", __func__);
913 dev_kfree_skb_any(skb);
914 return;
915 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000916 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000917 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
918}
919
920/**
921 * gigaset_isdn_hupB() - signal B channel hangup
922 * @bcs: B channel descriptor structure.
923 *
924 * Called by main module to notify the LL that the B channel connection has
925 * been shut down.
926 */
927void gigaset_isdn_hupB(struct bc_state *bcs)
928{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000929 struct gigaset_capi_appl *ap = bcs->ap;
930
931 /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
932
933 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000934 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000935 return;
936 }
937
938 send_disconnect_b3_ind(bcs, ap);
939}
940
941/**
942 * gigaset_isdn_start() - signal device availability
943 * @cs: device descriptor structure.
944 *
945 * Called by main module to notify the LL that the device is available for
946 * use.
947 */
948void gigaset_isdn_start(struct cardstate *cs)
949{
950 struct gigaset_capi_ctr *iif = cs->iif;
951
952 /* fill profile data: manufacturer name */
953 strcpy(iif->ctr.manu, "Siemens");
954 /* CAPI and device version */
955 iif->ctr.version.majorversion = 2; /* CAPI 2.0 */
956 iif->ctr.version.minorversion = 0;
957 /* ToDo: check/assert cs->gotfwver? */
958 iif->ctr.version.majormanuversion = cs->fwver[0];
959 iif->ctr.version.minormanuversion = cs->fwver[1];
960 /* number of B channels supported */
961 iif->ctr.profile.nbchannel = cs->channels;
962 /* global options: internal controller, supplementary services */
963 iif->ctr.profile.goptions = 0x11;
964 /* B1 protocols: 64 kbit/s HDLC or transparent */
965 iif->ctr.profile.support1 = 0x03;
966 /* B2 protocols: transparent only */
967 /* ToDo: X.75 SLP ? */
968 iif->ctr.profile.support2 = 0x02;
969 /* B3 protocols: transparent only */
970 iif->ctr.profile.support3 = 0x01;
971 /* no serial number */
972 strcpy(iif->ctr.serial, "0");
973 capi_ctr_ready(&iif->ctr);
974}
975
976/**
977 * gigaset_isdn_stop() - signal device unavailability
978 * @cs: device descriptor structure.
979 *
980 * Called by main module to notify the LL that the device is no longer
981 * available for use.
982 */
983void gigaset_isdn_stop(struct cardstate *cs)
984{
985 struct gigaset_capi_ctr *iif = cs->iif;
986 capi_ctr_down(&iif->ctr);
987}
988
989/*
990 * kernel CAPI callback methods
991 * ============================
992 */
993
994/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000995 * register CAPI application
996 */
997static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
Joe Perches475be4d2012-02-19 19:52:38 -0800998 capi_register_params *rp)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000999{
1000 struct gigaset_capi_ctr *iif
1001 = container_of(ctr, struct gigaset_capi_ctr, ctr);
1002 struct cardstate *cs = ctr->driverdata;
1003 struct gigaset_capi_appl *ap;
1004
Tilman Schmidt6a753422010-07-05 14:18:59 +00001005 gig_dbg(DEBUG_CMD, "%s [%u] l3cnt=%u blkcnt=%u blklen=%u",
1006 __func__, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
1007
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001008 list_for_each_entry(ap, &iif->appls, ctrlist)
1009 if (ap->id == appl) {
1010 dev_notice(cs->dev,
1011 "application %u already registered\n", appl);
1012 return;
1013 }
1014
1015 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
1016 if (!ap) {
1017 dev_err(cs->dev, "%s: out of memory\n", __func__);
1018 return;
1019 }
1020 ap->id = appl;
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001021 ap->rp = *rp;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001022
1023 list_add(&ap->ctrlist, &iif->appls);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001024 dev_info(cs->dev, "application %u registered\n", ap->id);
1025}
1026
1027/*
1028 * remove CAPI application from channel
1029 * helper function to keep indentation levels down and stay in 80 columns
1030 */
1031
1032static inline void remove_appl_from_channel(struct bc_state *bcs,
1033 struct gigaset_capi_appl *ap)
1034{
1035 struct cardstate *cs = bcs->cs;
1036 struct gigaset_capi_appl *bcap;
1037 unsigned long flags;
1038 int prevconnstate;
1039
1040 spin_lock_irqsave(&bcs->aplock, flags);
1041 bcap = bcs->ap;
1042 if (bcap == NULL) {
1043 spin_unlock_irqrestore(&bcs->aplock, flags);
1044 return;
1045 }
1046
1047 /* check first application on channel */
1048 if (bcap == ap) {
1049 bcs->ap = ap->bcnext;
1050 if (bcs->ap != NULL) {
1051 spin_unlock_irqrestore(&bcs->aplock, flags);
1052 return;
1053 }
1054
1055 /* none left, clear channel state */
1056 prevconnstate = bcs->apconnstate;
1057 bcs->apconnstate = APCONN_NONE;
1058 spin_unlock_irqrestore(&bcs->aplock, flags);
1059
1060 if (prevconnstate == APCONN_ACTIVE) {
1061 dev_notice(cs->dev, "%s: hanging up channel %u\n",
1062 __func__, bcs->channel);
1063 gigaset_add_event(cs, &bcs->at_state,
1064 EV_HUP, NULL, 0, NULL);
1065 gigaset_schedule_event(cs);
1066 }
1067 return;
1068 }
1069
1070 /* check remaining list */
1071 do {
1072 if (bcap->bcnext == ap) {
1073 bcap->bcnext = bcap->bcnext->bcnext;
Dan Carpenter7e27a0a2010-08-05 22:23:23 +00001074 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001075 return;
1076 }
1077 bcap = bcap->bcnext;
1078 } while (bcap != NULL);
1079 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001080}
1081
1082/*
1083 * release CAPI application
1084 */
1085static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
1086{
1087 struct gigaset_capi_ctr *iif
1088 = container_of(ctr, struct gigaset_capi_ctr, ctr);
1089 struct cardstate *cs = iif->ctr.driverdata;
1090 struct gigaset_capi_appl *ap, *tmp;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001091 unsigned ch;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001092
Tilman Schmidt6a753422010-07-05 14:18:59 +00001093 gig_dbg(DEBUG_CMD, "%s [%u]", __func__, appl);
1094
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001095 list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
1096 if (ap->id == appl) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001097 /* remove from any channels */
1098 for (ch = 0; ch < cs->channels; ch++)
1099 remove_appl_from_channel(&cs->bcs[ch], ap);
1100
1101 /* remove from registration list */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001102 list_del(&ap->ctrlist);
1103 kfree(ap);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001104 dev_info(cs->dev, "application %u released\n", appl);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001105 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001106}
1107
1108/*
1109 * =====================================================================
1110 * outgoing CAPI message handler
1111 * =====================================================================
1112 */
1113
1114/*
1115 * helper function: emit reply message with given Info value
1116 */
1117static void send_conf(struct gigaset_capi_ctr *iif,
1118 struct gigaset_capi_appl *ap,
1119 struct sk_buff *skb,
1120 u16 info)
1121{
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001122 struct cardstate *cs = iif->ctr.driverdata;
1123
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001124 /*
1125 * _CONF replies always only have NCCI and Info parameters
1126 * so they'll fit into the _REQ message skb
1127 */
1128 capi_cmsg_answer(&iif->acmsg);
1129 iif->acmsg.Info = info;
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001130 if (capi_cmsg2message(&iif->acmsg, skb->data)) {
1131 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1132 dev_kfree_skb_any(skb);
1133 return;
1134 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001135 __skb_trim(skb, CAPI_STDCONF_LEN);
1136 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1137 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1138}
1139
1140/*
1141 * process FACILITY_REQ message
1142 */
1143static void do_facility_req(struct gigaset_capi_ctr *iif,
1144 struct gigaset_capi_appl *ap,
1145 struct sk_buff *skb)
1146{
1147 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001148 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001149 struct sk_buff *cskb;
1150 u8 *pparam;
1151 unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
1152 u16 function, info;
1153 static u8 confparam[10]; /* max. 9 octets + length byte */
1154
1155 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001156 if (capi_message2cmsg(cmsg, skb->data)) {
1157 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1158 dev_kfree_skb_any(skb);
1159 return;
1160 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001161 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001162
1163 /*
1164 * Facility Request Parameter is not decoded by capi_message2cmsg()
1165 * encoding depends on Facility Selector
1166 */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001167 switch (cmsg->FacilitySelector) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001168 case CAPI_FACILITY_DTMF: /* ToDo */
1169 info = CapiFacilityNotSupported;
1170 confparam[0] = 2; /* length */
1171 /* DTMF information: Unknown DTMF request */
1172 capimsg_setu16(confparam, 1, 2);
1173 break;
1174
1175 case CAPI_FACILITY_V42BIS: /* not supported */
1176 info = CapiFacilityNotSupported;
1177 confparam[0] = 2; /* length */
1178 /* V.42 bis information: not available */
1179 capimsg_setu16(confparam, 1, 1);
1180 break;
1181
1182 case CAPI_FACILITY_SUPPSVC:
1183 /* decode Function parameter */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001184 pparam = cmsg->FacilityRequestParameter;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001185 if (pparam == NULL || pparam[0] < 2) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001186 dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
1187 "Facility Request Parameter");
1188 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1189 return;
1190 }
1191 function = CAPIMSG_U16(pparam, 1);
1192 switch (function) {
1193 case CAPI_SUPPSVC_GETSUPPORTED:
1194 info = CapiSuccess;
1195 /* Supplementary Service specific parameter */
1196 confparam[3] = 6; /* length */
1197 /* Supplementary services info: Success */
1198 capimsg_setu16(confparam, 4, CapiSuccess);
1199 /* Supported Services: none */
1200 capimsg_setu32(confparam, 6, 0);
1201 break;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001202 case CAPI_SUPPSVC_LISTEN:
1203 if (pparam[0] < 7 || pparam[3] < 4) {
1204 dev_notice(cs->dev, "%s: %s missing\n",
1205 "FACILITY_REQ", "Notification Mask");
1206 send_conf(iif, ap, skb,
1207 CapiIllMessageParmCoding);
1208 return;
1209 }
1210 if (CAPIMSG_U32(pparam, 4) != 0) {
1211 dev_notice(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001212 "%s: unsupported supplementary service notification mask 0x%x\n",
1213 "FACILITY_REQ", CAPIMSG_U32(pparam, 4));
Tilman Schmidt18c22592010-07-05 14:19:04 +00001214 info = CapiFacilitySpecificFunctionNotSupported;
1215 confparam[3] = 2; /* length */
1216 capimsg_setu16(confparam, 4,
Joe Perches475be4d2012-02-19 19:52:38 -08001217 CapiSupplementaryServiceNotSupported);
Tilman Schmidt1bdc07e2014-10-11 13:46:29 +02001218 break;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001219 }
1220 info = CapiSuccess;
1221 confparam[3] = 2; /* length */
1222 capimsg_setu16(confparam, 4, CapiSuccess);
1223 break;
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001224
1225 /* ToDo: add supported services */
1226
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001227 default:
Tilman Schmidt18c22592010-07-05 14:19:04 +00001228 dev_notice(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001229 "%s: unsupported supplementary service function 0x%04x\n",
Tilman Schmidt18c22592010-07-05 14:19:04 +00001230 "FACILITY_REQ", function);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001231 info = CapiFacilitySpecificFunctionNotSupported;
1232 /* Supplementary Service specific parameter */
1233 confparam[3] = 2; /* length */
1234 /* Supplementary services info: not supported */
1235 capimsg_setu16(confparam, 4,
1236 CapiSupplementaryServiceNotSupported);
1237 }
1238
1239 /* Facility confirmation parameter */
1240 confparam[0] = confparam[3] + 3; /* total length */
1241 /* Function: copy from _REQ message */
1242 capimsg_setu16(confparam, 1, function);
1243 /* Supplementary Service specific parameter already set above */
1244 break;
1245
1246 case CAPI_FACILITY_WAKEUP: /* ToDo */
1247 info = CapiFacilityNotSupported;
1248 confparam[0] = 2; /* length */
1249 /* Number of accepted awake request parameters: 0 */
1250 capimsg_setu16(confparam, 1, 0);
1251 break;
1252
1253 default:
1254 info = CapiFacilityNotSupported;
1255 confparam[0] = 0; /* empty struct */
1256 }
1257
1258 /* send FACILITY_CONF with given Info and confirmation parameter */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001259 dev_kfree_skb_any(skb);
Tilman Schmidt6c911912009-10-25 09:29:37 +00001260 capi_cmsg_answer(cmsg);
1261 cmsg->Info = info;
1262 cmsg->FacilityConfirmationParameter = confparam;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001263 msgsize += confparam[0]; /* length */
1264 cskb = alloc_skb(msgsize, GFP_ATOMIC);
1265 if (!cskb) {
1266 dev_err(cs->dev, "%s: out of memory\n", __func__);
1267 return;
1268 }
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001269 if (capi_cmsg2message(cmsg, __skb_put(cskb, msgsize))) {
1270 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1271 dev_kfree_skb_any(cskb);
1272 return;
1273 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001274 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1275 capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001276}
1277
1278
1279/*
1280 * process LISTEN_REQ message
1281 * just store the masks in the application data structure
1282 */
1283static void do_listen_req(struct gigaset_capi_ctr *iif,
1284 struct gigaset_capi_appl *ap,
1285 struct sk_buff *skb)
1286{
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001287 struct cardstate *cs = iif->ctr.driverdata;
1288
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001289 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001290 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
1291 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1292 dev_kfree_skb_any(skb);
1293 return;
1294 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001295 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1296
1297 /* store listening parameters */
1298 ap->listenInfoMask = iif->acmsg.InfoMask;
1299 ap->listenCIPmask = iif->acmsg.CIPmask;
1300 send_conf(iif, ap, skb, CapiSuccess);
1301}
1302
1303/*
1304 * process ALERT_REQ message
1305 * nothing to do, Gigaset always alerts anyway
1306 */
1307static void do_alert_req(struct gigaset_capi_ctr *iif,
1308 struct gigaset_capi_appl *ap,
1309 struct sk_buff *skb)
1310{
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001311 struct cardstate *cs = iif->ctr.driverdata;
1312
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001313 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001314 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
1315 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1316 dev_kfree_skb_any(skb);
1317 return;
1318 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001319 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1320 send_conf(iif, ap, skb, CapiAlertAlreadySent);
1321}
1322
1323/*
1324 * process CONNECT_REQ message
1325 * allocate a B channel, prepare dial commands, queue a DIAL event,
1326 * emit CONNECT_CONF reply
1327 */
1328static void do_connect_req(struct gigaset_capi_ctr *iif,
1329 struct gigaset_capi_appl *ap,
1330 struct sk_buff *skb)
1331{
1332 struct cardstate *cs = iif->ctr.driverdata;
1333 _cmsg *cmsg = &iif->acmsg;
1334 struct bc_state *bcs;
1335 char **commands;
1336 char *s;
1337 u8 *pp;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001338 unsigned long flags;
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001339 int i, l, lbc, lhlc;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001340 u16 info;
1341
1342 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001343 if (capi_message2cmsg(cmsg, skb->data)) {
1344 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1345 dev_kfree_skb_any(skb);
1346 return;
1347 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001348 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001349
1350 /* get free B channel & construct PLCI */
1351 bcs = gigaset_get_free_channel(cs);
1352 if (!bcs) {
1353 dev_notice(cs->dev, "%s: no B channel available\n",
1354 "CONNECT_REQ");
1355 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1356 return;
1357 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001358 spin_lock_irqsave(&bcs->aplock, flags);
1359 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE)
1360 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
1361 __func__, bcs->ap, bcs->apconnstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001362 ap->bcnext = NULL;
1363 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001364 bcs->apconnstate = APCONN_SETUP;
1365 spin_unlock_irqrestore(&bcs->aplock, flags);
1366
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001367 bcs->rx_bufsize = ap->rp.datablklen;
1368 dev_kfree_skb(bcs->rx_skb);
1369 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001370 cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1371
1372 /* build command table */
Joe Perches475be4d2012-02-19 19:52:38 -08001373 commands = kzalloc(AT_NUM * (sizeof *commands), GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001374 if (!commands)
1375 goto oom;
1376
1377 /* encode parameter: Called party number */
1378 pp = cmsg->CalledPartyNumber;
1379 if (pp == NULL || *pp == 0) {
1380 dev_notice(cs->dev, "%s: %s missing\n",
1381 "CONNECT_REQ", "Called party number");
1382 info = CapiIllMessageParmCoding;
1383 goto error;
1384 }
1385 l = *pp++;
1386 /* check type of number/numbering plan byte */
1387 switch (*pp) {
1388 case 0x80: /* unknown type / unknown numbering plan */
1389 case 0x81: /* unknown type / ISDN/Telephony numbering plan */
1390 break;
1391 default: /* others: warn about potential misinterpretation */
1392 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1393 "CONNECT_REQ", "Called party number", *pp);
1394 }
1395 pp++;
1396 l--;
1397 /* translate "**" internal call prefix to CTP value */
1398 if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1399 s = "^SCTP=0\r";
1400 pp += 2;
1401 l -= 2;
1402 } else {
1403 s = "^SCTP=1\r";
1404 }
1405 commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1406 if (!commands[AT_TYPE])
1407 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001408 commands[AT_DIAL] = kmalloc(l + 3, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001409 if (!commands[AT_DIAL])
1410 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001411 snprintf(commands[AT_DIAL], l + 3, "D%.*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001412
1413 /* encode parameter: Calling party number */
1414 pp = cmsg->CallingPartyNumber;
1415 if (pp != NULL && *pp > 0) {
1416 l = *pp++;
1417
1418 /* check type of number/numbering plan byte */
1419 /* ToDo: allow for/handle Ext=1? */
1420 switch (*pp) {
1421 case 0x00: /* unknown type / unknown numbering plan */
1422 case 0x01: /* unknown type / ISDN/Telephony num. plan */
1423 break;
1424 default:
1425 dev_notice(cs->dev,
1426 "%s: %s type/plan 0x%02x unsupported\n",
1427 "CONNECT_REQ", "Calling party number", *pp);
1428 }
1429 pp++;
1430 l--;
1431
1432 /* check presentation indicator */
1433 if (!l) {
1434 dev_notice(cs->dev, "%s: %s IE truncated\n",
1435 "CONNECT_REQ", "Calling party number");
1436 info = CapiIllMessageParmCoding;
1437 goto error;
1438 }
1439 switch (*pp & 0xfc) { /* ignore Screening indicator */
1440 case 0x80: /* Presentation allowed */
1441 s = "^SCLIP=1\r";
1442 break;
1443 case 0xa0: /* Presentation restricted */
1444 s = "^SCLIP=0\r";
1445 break;
1446 default:
1447 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1448 "CONNECT_REQ",
1449 "Presentation/Screening indicator",
1450 *pp);
1451 s = "^SCLIP=1\r";
1452 }
1453 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1454 if (!commands[AT_CLIP])
1455 goto oom;
1456 pp++;
1457 l--;
1458
1459 if (l) {
1460 /* number */
Joe Perches475be4d2012-02-19 19:52:38 -08001461 commands[AT_MSN] = kmalloc(l + 8, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001462 if (!commands[AT_MSN])
1463 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001464 snprintf(commands[AT_MSN], l + 8, "^SMSN=%*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001465 }
1466 }
1467
1468 /* check parameter: CIP Value */
Tilman Schmidt6ad34142010-03-16 07:04:01 +00001469 if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001470 (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1471 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1472 "CONNECT_REQ", cmsg->CIPValue);
1473 info = CapiCipValueUnknown;
1474 goto error;
1475 }
1476
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001477 /*
1478 * check/encode parameters: BC & HLC
1479 * must be encoded together as device doesn't accept HLC separately
1480 * explicit parameters override values derived from CIP
1481 */
1482
1483 /* determine lengths */
1484 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001485 lbc = 2 * cmsg->BC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001486 else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
1487 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1488 else /* no BC */
1489 lbc = 0;
1490 if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001491 lhlc = 2 * cmsg->HLC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001492 else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1493 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1494 else /* no HLC */
1495 lhlc = 0;
1496
1497 if (lbc) {
1498 /* have BC: allocate and assemble command string */
1499 l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
1500 if (lhlc)
1501 l += lhlc + 7; /* ";^SHLC=" + value */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001502 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1503 if (!commands[AT_BC])
1504 goto oom;
1505 strcpy(commands[AT_BC], "^SBC=");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001506 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1507 decode_ie(cmsg->BC, commands[AT_BC] + 5);
1508 else /* BC derived from CIP */
1509 strcpy(commands[AT_BC] + 5,
1510 cip2bchlc[cmsg->CIPValue].bc);
1511 if (lhlc) {
1512 strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1513 if (cmsg->HLC && cmsg->HLC[0])
1514 /* HLC specified explicitly */
1515 decode_ie(cmsg->HLC,
1516 commands[AT_BC] + lbc + 12);
1517 else /* HLC derived from CIP */
1518 strcpy(commands[AT_BC] + lbc + 12,
1519 cip2bchlc[cmsg->CIPValue].hlc);
1520 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001521 strcpy(commands[AT_BC] + l - 2, "\r");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001522 } else {
1523 /* no BC */
1524 if (lhlc) {
1525 dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1526 "CONNECT_REQ");
1527 info = CapiIllMessageParmCoding; /* ? */
1528 goto error;
1529 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001530 }
1531
1532 /* check/encode parameter: B Protocol */
1533 if (cmsg->BProtocol == CAPI_DEFAULT) {
1534 bcs->proto2 = L2_HDLC;
1535 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001536 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001537 } else {
1538 switch (cmsg->B1protocol) {
1539 case 0:
1540 bcs->proto2 = L2_HDLC;
1541 break;
1542 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001543 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001544 break;
1545 default:
1546 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001547 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001548 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001549 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001550 }
1551 if (cmsg->B2protocol != 1)
1552 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001553 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001554 cmsg->B2protocol);
1555 if (cmsg->B3protocol != 0)
1556 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001557 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001558 cmsg->B3protocol);
1559 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001560 "CONNECT_REQ", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001561 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001562 "CONNECT_REQ", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001563 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001564 "CONNECT_REQ", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001565 }
1566 commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1567 if (!commands[AT_PROTO])
1568 goto oom;
1569 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1570
1571 /* ToDo: check/encode remaining parameters */
1572 ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001573 "CONNECT_REQ", "Called pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001574 ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001575 "CONNECT_REQ", "Calling pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001576 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001577 "CONNECT_REQ", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001578 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1579 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001580 "CONNECT_REQ", "B Channel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001581 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001582 "CONNECT_REQ", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001583 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001584 "CONNECT_REQ", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001585 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001586 "CONNECT_REQ", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001587 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001588
1589 /* encode parameter: B channel to use */
1590 commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1591 if (!commands[AT_ISO])
1592 goto oom;
1593 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1594 (unsigned) bcs->channel + 1);
1595
1596 /* queue & schedule EV_DIAL event */
1597 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
Tilman Schmidt1528b182010-02-22 13:09:52 +00001598 bcs->at_state.seq_index, NULL)) {
1599 info = CAPI_MSGOSRESOURCEERR;
1600 goto error;
1601 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001602 gigaset_schedule_event(cs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001603 send_conf(iif, ap, skb, CapiSuccess);
1604 return;
1605
1606oom:
1607 dev_err(cs->dev, "%s: out of memory\n", __func__);
1608 info = CAPI_MSGOSRESOURCEERR;
1609error:
1610 if (commands)
1611 for (i = 0; i < AT_NUM; i++)
1612 kfree(commands[i]);
1613 kfree(commands);
1614 gigaset_free_channel(bcs);
1615 send_conf(iif, ap, skb, info);
1616}
1617
1618/*
1619 * process CONNECT_RESP message
1620 * checks protocol parameters and queues an ACCEPT or HUP event
1621 */
1622static void do_connect_resp(struct gigaset_capi_ctr *iif,
1623 struct gigaset_capi_appl *ap,
1624 struct sk_buff *skb)
1625{
1626 struct cardstate *cs = iif->ctr.driverdata;
1627 _cmsg *cmsg = &iif->acmsg;
1628 struct bc_state *bcs;
1629 struct gigaset_capi_appl *oap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001630 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001631 int channel;
1632
1633 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001634 if (capi_message2cmsg(cmsg, skb->data)) {
1635 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1636 dev_kfree_skb_any(skb);
1637 return;
1638 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001639 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001640 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001641
1642 /* extract and check channel number from PLCI */
1643 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1644 if (!channel || channel > cs->channels) {
1645 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1646 "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1647 return;
1648 }
1649 bcs = cs->bcs + channel - 1;
1650
1651 switch (cmsg->Reject) {
1652 case 0: /* Accept */
1653 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001654 spin_lock_irqsave(&bcs->aplock, flags);
1655 while (bcs->ap != NULL) {
1656 oap = bcs->ap;
1657 bcs->ap = oap->bcnext;
1658 if (oap != ap) {
1659 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001660 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001661 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001662 spin_lock_irqsave(&bcs->aplock, flags);
1663 }
1664 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001665 ap->bcnext = NULL;
1666 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001667 spin_unlock_irqrestore(&bcs->aplock, flags);
1668
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001669 bcs->rx_bufsize = ap->rp.datablklen;
1670 dev_kfree_skb(bcs->rx_skb);
1671 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001672 bcs->chstate |= CHS_NOTIFY_LL;
1673
1674 /* check/encode B channel protocol */
1675 if (cmsg->BProtocol == CAPI_DEFAULT) {
1676 bcs->proto2 = L2_HDLC;
1677 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001678 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001679 } else {
1680 switch (cmsg->B1protocol) {
1681 case 0:
1682 bcs->proto2 = L2_HDLC;
1683 break;
1684 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001685 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001686 break;
1687 default:
1688 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001689 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001690 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001691 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001692 }
1693 if (cmsg->B2protocol != 1)
1694 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001695 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001696 cmsg->B2protocol);
1697 if (cmsg->B3protocol != 0)
1698 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001699 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001700 cmsg->B3protocol);
1701 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001702 "CONNECT_RESP", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001703 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001704 "CONNECT_RESP", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001705 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001706 "CONNECT_RESP", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001707 }
1708
1709 /* ToDo: check/encode remaining parameters */
1710 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
Joe Perches475be4d2012-02-19 19:52:38 -08001711 "CONNECT_RESP", "Connected Number");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001712 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001713 "CONNECT_RESP", "Connected Subaddress");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001714 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001715 "CONNECT_RESP", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001716 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1717 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001718 "CONNECT_RESP", "BChannel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001719 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001720 "CONNECT_RESP", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001721 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001722 "CONNECT_RESP", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001723 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001724 "CONNECT_RESP", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001725 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001726
1727 /* Accept call */
Joe Perches475be4d2012-02-19 19:52:38 -08001728 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001729 EV_ACCEPT, NULL, 0, NULL))
1730 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001731 gigaset_schedule_event(cs);
1732 return;
1733
1734 case 1: /* Ignore */
1735 /* send DISCONNECT_IND to this application */
1736 send_disconnect_ind(bcs, ap, 0);
1737
1738 /* remove it from the list of listening apps */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001739 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001740 if (bcs->ap == ap) {
1741 bcs->ap = ap->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001742 if (bcs->ap == NULL) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001743 /* last one: stop ev-layer hupD notifications */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001744 bcs->apconnstate = APCONN_NONE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001745 bcs->chstate &= ~CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001746 }
1747 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001748 return;
1749 }
1750 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1751 if (oap->bcnext == ap) {
1752 oap->bcnext = oap->bcnext->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001753 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001754 return;
1755 }
1756 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001757 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001758 dev_err(cs->dev, "%s: application %u not found\n",
1759 __func__, ap->id);
1760 return;
1761
1762 default: /* Reject */
1763 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001764 spin_lock_irqsave(&bcs->aplock, flags);
1765 while (bcs->ap != NULL) {
1766 oap = bcs->ap;
1767 bcs->ap = oap->bcnext;
1768 if (oap != ap) {
1769 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001770 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001771 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001772 spin_lock_irqsave(&bcs->aplock, flags);
1773 }
1774 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001775 ap->bcnext = NULL;
1776 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001777 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001778
1779 /* reject call - will trigger DISCONNECT_IND for this app */
1780 dev_info(cs->dev, "%s: Reject=%x\n",
1781 "CONNECT_RESP", cmsg->Reject);
Joe Perches475be4d2012-02-19 19:52:38 -08001782 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001783 EV_HUP, NULL, 0, NULL))
1784 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001785 gigaset_schedule_event(cs);
1786 return;
1787 }
1788}
1789
1790/*
1791 * process CONNECT_B3_REQ message
1792 * build NCCI and emit CONNECT_B3_CONF reply
1793 */
1794static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1795 struct gigaset_capi_appl *ap,
1796 struct sk_buff *skb)
1797{
1798 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001799 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001800 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001801 int channel;
1802
1803 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001804 if (capi_message2cmsg(cmsg, skb->data)) {
1805 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1806 dev_kfree_skb_any(skb);
1807 return;
1808 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001809 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001810
1811 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001812 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001813 if (!channel || channel > cs->channels) {
1814 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001815 "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001816 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1817 return;
1818 }
Joe Perches475be4d2012-02-19 19:52:38 -08001819 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001820
1821 /* mark logical connection active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001822 bcs->apconnstate = APCONN_ACTIVE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001823
1824 /* build NCCI: always 1 (one B3 connection only) */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001825 cmsg->adr.adrNCCI |= 1 << 16;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001826
1827 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001828 ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001829 send_conf(iif, ap, skb,
1830 (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08001831 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001832}
1833
1834/*
1835 * process CONNECT_B3_RESP message
1836 * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1837 * or queue EV_HUP and emit DISCONNECT_B3_IND.
1838 * The emitted message is always shorter than the received one,
1839 * allowing to reuse the skb.
1840 */
1841static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1842 struct gigaset_capi_appl *ap,
1843 struct sk_buff *skb)
1844{
1845 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001846 _cmsg *cmsg = &iif->acmsg;
1847 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001848 int channel;
1849 unsigned int msgsize;
1850 u8 command;
1851
1852 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001853 if (capi_message2cmsg(cmsg, skb->data)) {
1854 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1855 dev_kfree_skb_any(skb);
1856 return;
1857 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001858 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001859
1860 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001861 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001862 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00001863 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001864 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001865 "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001866 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001867 return;
1868 }
Joe Perches475be4d2012-02-19 19:52:38 -08001869 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001870
Tilman Schmidt6c911912009-10-25 09:29:37 +00001871 if (cmsg->Reject) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001872 /* Reject: clear B3 connect received flag */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001873 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001874
1875 /* trigger hangup, causing eventual DISCONNECT_IND */
1876 if (!gigaset_add_event(cs, &bcs->at_state,
1877 EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001878 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001879 return;
1880 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001881 gigaset_schedule_event(cs);
1882
1883 /* emit DISCONNECT_B3_IND */
1884 command = CAPI_DISCONNECT_B3;
1885 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1886 } else {
1887 /*
1888 * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1889 * we only send CONNECT_B3_IND if the B channel is up
1890 */
1891 command = CAPI_CONNECT_B3_ACTIVE;
1892 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1893 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001894 capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1895 ap->nextMessageNumber++, cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001896 __skb_trim(skb, msgsize);
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001897 if (capi_cmsg2message(cmsg, skb->data)) {
1898 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1899 dev_kfree_skb_any(skb);
1900 return;
1901 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001902 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001903 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1904}
1905
1906/*
1907 * process DISCONNECT_REQ message
1908 * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1909 * emit DISCONNECT_CONF reply
1910 */
1911static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1912 struct gigaset_capi_appl *ap,
1913 struct sk_buff *skb)
1914{
1915 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001916 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001917 struct bc_state *bcs;
1918 _cmsg *b3cmsg;
1919 struct sk_buff *b3skb;
1920 int channel;
1921
1922 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001923 if (capi_message2cmsg(cmsg, skb->data)) {
1924 dev_err(cs->dev, "%s: message parser failure\n", __func__);
1925 dev_kfree_skb_any(skb);
1926 return;
1927 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001928 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001929
1930 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001931 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001932 if (!channel || channel > cs->channels) {
1933 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001934 "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001935 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1936 return;
1937 }
1938 bcs = cs->bcs + channel - 1;
1939
1940 /* ToDo: process parameter: Additional info */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001941 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1942 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1943 "DISCONNECT_REQ", "B Channel Information");
1944 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1945 "DISCONNECT_REQ", "Keypad Facility");
1946 ignore_cstruct_param(cs, cmsg->Useruserdata,
1947 "DISCONNECT_REQ", "User-User Data");
1948 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1949 "DISCONNECT_REQ", "Facility Data Array");
1950 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001951
1952 /* skip if DISCONNECT_IND already sent */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001953 if (!bcs->apconnstate)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001954 return;
1955
1956 /* check for active logical connection */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001957 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt62a1cfe2012-04-25 13:02:20 +00001958 /* clear it */
1959 bcs->apconnstate = APCONN_SETUP;
1960
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001961 /*
1962 * emit DISCONNECT_B3_IND with cause 0x3301
1963 * use separate cmsg structure, as the content of iif->acmsg
1964 * is still needed for creating the _CONF message
1965 */
1966 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1967 if (!b3cmsg) {
1968 dev_err(cs->dev, "%s: out of memory\n", __func__);
1969 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1970 return;
1971 }
1972 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1973 ap->nextMessageNumber++,
Tilman Schmidt6c911912009-10-25 09:29:37 +00001974 cmsg->adr.adrPLCI | (1 << 16));
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001975 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1976 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1977 if (b3skb == NULL) {
1978 dev_err(cs->dev, "%s: out of memory\n", __func__);
1979 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
Jesper Juhl2393c942010-12-26 09:59:58 +00001980 kfree(b3cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001981 return;
1982 }
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02001983 if (capi_cmsg2message(b3cmsg,
1984 __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN))) {
1985 dev_err(cs->dev, "%s: message parser failure\n",
1986 __func__);
1987 kfree(b3cmsg);
1988 dev_kfree_skb_any(b3skb);
1989 return;
1990 }
Tilman Schmidt62a1cfe2012-04-25 13:02:20 +00001991 dump_cmsg(DEBUG_CMD, __func__, b3cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001992 kfree(b3cmsg);
1993 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1994 }
1995
1996 /* trigger hangup, causing eventual DISCONNECT_IND */
1997 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001998 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1999 return;
2000 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002001 gigaset_schedule_event(cs);
2002
2003 /* emit reply */
2004 send_conf(iif, ap, skb, CapiSuccess);
2005}
2006
2007/*
2008 * process DISCONNECT_B3_REQ message
2009 * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
2010 */
2011static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
2012 struct gigaset_capi_appl *ap,
2013 struct sk_buff *skb)
2014{
2015 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00002016 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002017 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002018 int channel;
2019
2020 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02002021 if (capi_message2cmsg(cmsg, skb->data)) {
2022 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2023 dev_kfree_skb_any(skb);
2024 return;
2025 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00002026 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002027
2028 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00002029 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002030 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00002031 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002032 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00002033 "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002034 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
2035 return;
2036 }
Joe Perches475be4d2012-02-19 19:52:38 -08002037 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002038
2039 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002040 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002041 send_conf(iif, ap, skb,
2042 CapiMessageNotSupportedInCurrentState);
2043 return;
2044 }
2045
2046 /* trigger hangup, causing eventual DISCONNECT_B3_IND */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002047 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002048 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2049 return;
2050 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002051 gigaset_schedule_event(cs);
2052
2053 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00002054 ignore_cstruct_param(cs, cmsg->NCPI,
Joe Perches475be4d2012-02-19 19:52:38 -08002055 "DISCONNECT_B3_REQ", "NCPI");
Tilman Schmidtf86936f2012-04-25 13:02:20 +00002056 send_conf(iif, ap, skb,
2057 (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08002058 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002059}
2060
2061/*
2062 * process DATA_B3_REQ message
2063 */
2064static void do_data_b3_req(struct gigaset_capi_ctr *iif,
2065 struct gigaset_capi_appl *ap,
2066 struct sk_buff *skb)
2067{
2068 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002069 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002070 int channel = CAPIMSG_PLCI_PART(skb->data);
2071 u16 ncci = CAPIMSG_NCCI_PART(skb->data);
2072 u16 msglen = CAPIMSG_LEN(skb->data);
2073 u16 datalen = CAPIMSG_DATALEN(skb->data);
2074 u16 flags = CAPIMSG_FLAGS(skb->data);
Tilman Schmidt23b36772010-06-21 13:54:50 +00002075 u16 msgid = CAPIMSG_MSGID(skb->data);
2076 u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002077
2078 /* frequent message, avoid _cmsg overhead */
Tilman Schmidt6a753422010-07-05 14:18:59 +00002079 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002080
2081 /* check parameters */
2082 if (channel == 0 || channel > cs->channels || ncci != 1) {
2083 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
2084 "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
2085 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
2086 return;
2087 }
Joe Perches475be4d2012-02-19 19:52:38 -08002088 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002089 if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
2090 dev_notice(cs->dev, "%s: unexpected length %d\n",
2091 "DATA_B3_REQ", msglen);
2092 if (msglen + datalen != skb->len)
2093 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
2094 "DATA_B3_REQ", msglen, datalen, skb->len);
2095 if (msglen + datalen > skb->len) {
2096 /* message too short for announced data length */
2097 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
2098 return;
2099 }
2100 if (flags & CAPI_FLAGS_RESERVED) {
2101 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
2102 "DATA_B3_REQ", flags);
2103 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
2104 return;
2105 }
2106
2107 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002108 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002109 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2110 return;
2111 }
2112
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002113 /* pull CAPI message into link layer header */
2114 skb_reset_mac_header(skb);
2115 skb->mac_len = msglen;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002116 skb_pull(skb, msglen);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002117
2118 /* pass to device-specific module */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002119 if (cs->ops->send_skb(bcs, skb) < 0) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002120 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2121 return;
2122 }
2123
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002124 /*
Tilman Schmidt23b36772010-06-21 13:54:50 +00002125 * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
2126 * confirmation" bit is set; otherwise we have to send it now
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002127 */
Tilman Schmidt23b36772010-06-21 13:54:50 +00002128 if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
2129 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
2130 flags ? CapiFlagsNotSupportedByProtocol
Joe Perches475be4d2012-02-19 19:52:38 -08002131 : CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002132}
2133
2134/*
2135 * process RESET_B3_REQ message
2136 * just always reply "not supported by current protocol"
2137 */
2138static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
2139 struct gigaset_capi_appl *ap,
2140 struct sk_buff *skb)
2141{
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02002142 struct cardstate *cs = iif->ctr.driverdata;
2143
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002144 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02002145 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
2146 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2147 dev_kfree_skb_any(skb);
2148 return;
2149 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002150 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2151 send_conf(iif, ap, skb,
2152 CapiResetProcedureNotSupportedByCurrentProtocol);
2153}
2154
2155/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002156 * unsupported CAPI message handler
2157 */
2158static void do_unsupported(struct gigaset_capi_ctr *iif,
2159 struct gigaset_capi_appl *ap,
2160 struct sk_buff *skb)
2161{
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02002162 struct cardstate *cs = iif->ctr.driverdata;
2163
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002164 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02002165 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
2166 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2167 dev_kfree_skb_any(skb);
2168 return;
2169 }
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002170 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002171 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2172}
2173
2174/*
2175 * CAPI message handler: no-op
2176 */
2177static void do_nothing(struct gigaset_capi_ctr *iif,
2178 struct gigaset_capi_appl *ap,
2179 struct sk_buff *skb)
2180{
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02002181 struct cardstate *cs = iif->ctr.driverdata;
2182
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002183 /* decode message */
Tilman Schmidt2bf3a092014-10-11 13:46:30 +02002184 if (capi_message2cmsg(&iif->acmsg, skb->data)) {
2185 dev_err(cs->dev, "%s: message parser failure\n", __func__);
2186 dev_kfree_skb_any(skb);
2187 return;
2188 }
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002189 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002190 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002191}
2192
2193static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
2194 struct gigaset_capi_appl *ap,
2195 struct sk_buff *skb)
2196{
Tilman Schmidt6a753422010-07-05 14:18:59 +00002197 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002198 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002199}
2200
2201/* table of outgoing CAPI message handlers with lookup function */
2202typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
2203 struct gigaset_capi_appl *,
2204 struct sk_buff *);
2205
2206static struct {
2207 u16 cmd;
2208 capi_send_handler_t handler;
2209} capi_send_handler_table[] = {
2210 /* most frequent messages first for faster lookup */
2211 { CAPI_DATA_B3_REQ, do_data_b3_req },
2212 { CAPI_DATA_B3_RESP, do_data_b3_resp },
2213
2214 { CAPI_ALERT_REQ, do_alert_req },
2215 { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
2216 { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
2217 { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
2218 { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
2219 { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
2220 { CAPI_CONNECT_REQ, do_connect_req },
2221 { CAPI_CONNECT_RESP, do_connect_resp },
2222 { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
2223 { CAPI_DISCONNECT_B3_RESP, do_nothing },
2224 { CAPI_DISCONNECT_REQ, do_disconnect_req },
2225 { CAPI_DISCONNECT_RESP, do_nothing },
2226 { CAPI_FACILITY_REQ, do_facility_req },
2227 { CAPI_FACILITY_RESP, do_nothing },
2228 { CAPI_LISTEN_REQ, do_listen_req },
2229 { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
2230 { CAPI_RESET_B3_REQ, do_reset_b3_req },
2231 { CAPI_RESET_B3_RESP, do_nothing },
2232
2233 /*
2234 * ToDo: support overlap sending (requires ev-layer state
2235 * machine extension to generate additional ATD commands)
2236 */
2237 { CAPI_INFO_REQ, do_unsupported },
2238 { CAPI_INFO_RESP, do_nothing },
2239
2240 /*
2241 * ToDo: what's the proper response for these?
2242 */
2243 { CAPI_MANUFACTURER_REQ, do_nothing },
2244 { CAPI_MANUFACTURER_RESP, do_nothing },
2245};
2246
2247/* look up handler */
2248static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
2249{
2250 size_t i;
2251
2252 for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2253 if (capi_send_handler_table[i].cmd == cmd)
2254 return capi_send_handler_table[i].handler;
2255 return NULL;
2256}
2257
2258
2259/**
2260 * gigaset_send_message() - accept a CAPI message from an application
2261 * @ctr: controller descriptor structure.
2262 * @skb: CAPI message.
2263 *
2264 * Return value: CAPI error code
2265 * Note: capidrv (and probably others, too) only uses the return value to
2266 * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2267 */
2268static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2269{
2270 struct gigaset_capi_ctr *iif
2271 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2272 struct cardstate *cs = ctr->driverdata;
2273 struct gigaset_capi_appl *ap;
2274 capi_send_handler_t handler;
2275
2276 /* can only handle linear sk_buffs */
2277 if (skb_linearize(skb) < 0) {
2278 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2279 return CAPI_MSGOSRESOURCEERR;
2280 }
2281
2282 /* retrieve application data structure */
2283 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2284 if (!ap) {
2285 dev_notice(cs->dev, "%s: application %u not registered\n",
2286 __func__, CAPIMSG_APPID(skb->data));
2287 return CAPI_ILLAPPNR;
2288 }
2289
2290 /* look up command */
2291 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2292 if (!handler) {
2293 /* unknown/unsupported message type */
2294 if (printk_ratelimit())
2295 dev_notice(cs->dev, "%s: unsupported message %u\n",
2296 __func__, CAPIMSG_CMD(skb->data));
2297 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2298 }
2299
2300 /* serialize */
2301 if (atomic_add_return(1, &iif->sendqlen) > 1) {
2302 /* queue behind other messages */
2303 skb_queue_tail(&iif->sendqueue, skb);
2304 return CAPI_NOERROR;
2305 }
2306
2307 /* process message */
2308 handler(iif, ap, skb);
2309
2310 /* process other messages arrived in the meantime */
2311 while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2312 skb = skb_dequeue(&iif->sendqueue);
2313 if (!skb) {
2314 /* should never happen */
2315 dev_err(cs->dev, "%s: send queue empty\n", __func__);
2316 continue;
2317 }
2318 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2319 if (!ap) {
2320 /* could that happen? */
2321 dev_warn(cs->dev, "%s: application %u vanished\n",
2322 __func__, CAPIMSG_APPID(skb->data));
2323 continue;
2324 }
2325 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2326 if (!handler) {
2327 /* should never happen */
2328 dev_err(cs->dev, "%s: handler %x vanished\n",
2329 __func__, CAPIMSG_CMD(skb->data));
2330 continue;
2331 }
2332 handler(iif, ap, skb);
2333 }
2334
2335 return CAPI_NOERROR;
2336}
2337
2338/**
2339 * gigaset_procinfo() - build single line description for controller
2340 * @ctr: controller descriptor structure.
2341 *
2342 * Return value: pointer to generated string (null terminated)
2343 */
2344static char *gigaset_procinfo(struct capi_ctr *ctr)
2345{
2346 return ctr->name; /* ToDo: more? */
2347}
2348
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002349static int gigaset_proc_show(struct seq_file *m, void *v)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002350{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002351 struct capi_ctr *ctr = m->private;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002352 struct cardstate *cs = ctr->driverdata;
2353 char *s;
2354 int i;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002355
2356 seq_printf(m, "%-16s %s\n", "name", ctr->name);
2357 seq_printf(m, "%-16s %s %s\n", "dev",
Joe Perches475be4d2012-02-19 19:52:38 -08002358 dev_driver_string(cs->dev), dev_name(cs->dev));
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002359 seq_printf(m, "%-16s %d\n", "id", cs->myid);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002360 if (cs->gotfwver)
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002361 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
Joe Perches475be4d2012-02-19 19:52:38 -08002362 cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002363 seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2364 seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002365
2366 switch (cs->mode) {
2367 case M_UNKNOWN:
2368 s = "unknown";
2369 break;
2370 case M_CONFIG:
2371 s = "config";
2372 break;
2373 case M_UNIMODEM:
2374 s = "Unimodem";
2375 break;
2376 case M_CID:
2377 s = "CID";
2378 break;
2379 default:
2380 s = "??";
2381 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002382 seq_printf(m, "%-16s %s\n", "mode", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002383
2384 switch (cs->mstate) {
2385 case MS_UNINITIALIZED:
2386 s = "uninitialized";
2387 break;
2388 case MS_INIT:
2389 s = "init";
2390 break;
2391 case MS_LOCKED:
2392 s = "locked";
2393 break;
2394 case MS_SHUTDOWN:
2395 s = "shutdown";
2396 break;
2397 case MS_RECOVER:
2398 s = "recover";
2399 break;
2400 case MS_READY:
2401 s = "ready";
2402 break;
2403 default:
2404 s = "??";
2405 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002406 seq_printf(m, "%-16s %s\n", "mstate", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002407
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002408 seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2409 seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2410 seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2411 seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002412
2413 for (i = 0; i < cs->channels; i++) {
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002414 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
Joe Perches475be4d2012-02-19 19:52:38 -08002415 cs->bcs[i].corrupted);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002416 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
Joe Perches475be4d2012-02-19 19:52:38 -08002417 cs->bcs[i].trans_down);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002418 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
Joe Perches475be4d2012-02-19 19:52:38 -08002419 cs->bcs[i].trans_up);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002420 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
Joe Perches475be4d2012-02-19 19:52:38 -08002421 cs->bcs[i].chstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002422 switch (cs->bcs[i].proto2) {
2423 case L2_BITSYNC:
2424 s = "bitsync";
2425 break;
2426 case L2_HDLC:
2427 s = "HDLC";
2428 break;
2429 case L2_VOICE:
2430 s = "voice";
2431 break;
2432 default:
2433 s = "??";
2434 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002435 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002436 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002437 return 0;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002438}
2439
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002440static int gigaset_proc_open(struct inode *inode, struct file *file)
2441{
Al Virod9dda782013-03-31 18:16:14 -04002442 return single_open(file, gigaset_proc_show, PDE_DATA(inode));
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002443}
2444
2445static const struct file_operations gigaset_proc_fops = {
2446 .owner = THIS_MODULE,
2447 .open = gigaset_proc_open,
2448 .read = seq_read,
2449 .llseek = seq_lseek,
2450 .release = single_release,
2451};
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002452
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002453/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002454 * gigaset_isdn_regdev() - register device to LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002455 * @cs: device descriptor structure.
2456 * @isdnid: device name.
2457 *
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002458 * Return value: 0 on success, error code < 0 on failure
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002459 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002460int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002461{
2462 struct gigaset_capi_ctr *iif;
2463 int rc;
2464
Tilman Schmidtee7ff5f2014-10-11 13:46:29 +02002465 iif = kzalloc(sizeof(*iif), GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002466 if (!iif) {
2467 pr_err("%s: out of memory\n", __func__);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002468 return -ENOMEM;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002469 }
2470
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002471 /* prepare controller structure */
2472 iif->ctr.owner = THIS_MODULE;
2473 iif->ctr.driverdata = cs;
Tilman Schmidtee7ff5f2014-10-11 13:46:29 +02002474 strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name) - 1);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002475 iif->ctr.driver_name = "gigaset";
Tilman Schmidte4876392010-05-23 01:02:38 +00002476 iif->ctr.load_firmware = NULL;
2477 iif->ctr.reset_ctr = NULL;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002478 iif->ctr.register_appl = gigaset_register_appl;
2479 iif->ctr.release_appl = gigaset_release_appl;
2480 iif->ctr.send_message = gigaset_send_message;
2481 iif->ctr.procinfo = gigaset_procinfo;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002482 iif->ctr.proc_fops = &gigaset_proc_fops;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002483 INIT_LIST_HEAD(&iif->appls);
2484 skb_queue_head_init(&iif->sendqueue);
2485 atomic_set(&iif->sendqlen, 0);
2486
2487 /* register controller with CAPI */
2488 rc = attach_capi_ctr(&iif->ctr);
2489 if (rc) {
2490 pr_err("attach_capi_ctr failed (%d)\n", rc);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002491 kfree(iif);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002492 return rc;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002493 }
2494
2495 cs->iif = iif;
2496 cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002497 return 0;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002498}
2499
2500/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002501 * gigaset_isdn_unregdev() - unregister device from LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002502 * @cs: device descriptor structure.
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002503 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002504void gigaset_isdn_unregdev(struct cardstate *cs)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002505{
2506 struct gigaset_capi_ctr *iif = cs->iif;
2507
2508 detach_capi_ctr(&iif->ctr);
2509 kfree(iif);
2510 cs->iif = NULL;
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002511}
2512
2513static struct capi_driver capi_driver_gigaset = {
2514 .name = "gigaset",
2515 .revision = "1.0",
2516};
2517
2518/**
2519 * gigaset_isdn_regdrv() - register driver to LL
2520 */
2521void gigaset_isdn_regdrv(void)
2522{
2523 pr_info("Kernel CAPI interface\n");
2524 register_capi_driver(&capi_driver_gigaset);
2525}
2526
2527/**
2528 * gigaset_isdn_unregdrv() - unregister driver from LL
2529 */
2530void gigaset_isdn_unregdrv(void)
2531{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002532 unregister_capi_driver(&capi_driver_gigaset);
2533}