blob: 27e4a3e21d6437a662fb5981090183119c4caa0a [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;
Joe Perches475be4d2012-02-19 19:52:38 -0800251 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000252 if (!dbgline)
253 return;
254 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800255 dbgline[3 * i] = hex_asc_hi(data[12 + i]);
256 dbgline[3 * i + 1] = hex_asc_lo(data[12 + i]);
257 dbgline[3 * i + 2] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000258 }
Joe Perches475be4d2012-02-19 19:52:38 -0800259 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000260 gig_dbg(level, " %s", dbgline);
261 kfree(dbgline);
262 if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
263 (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
Tilman Schmidt6a753422010-07-05 14:18:59 +0000264 CAPIMSG_SUBCOMMAND(data) == CAPI_IND)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000265 l = CAPIMSG_DATALEN(data);
Tilman Schmidt6a753422010-07-05 14:18:59 +0000266 gig_dbg(level, " DataLength=%d", l);
267 if (l <= 0 || !(gigaset_debuglevel & DEBUG_LLDATA))
268 return;
269 if (l > 64)
270 l = 64; /* arbitrary limit */
Joe Perches475be4d2012-02-19 19:52:38 -0800271 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000272 if (!dbgline)
273 return;
274 data += CAPIMSG_LEN(data);
275 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800276 dbgline[3 * i] = hex_asc_hi(data[i]);
277 dbgline[3 * i + 1] = hex_asc_lo(data[i]);
278 dbgline[3 * i + 2] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000279 }
Joe Perches475be4d2012-02-19 19:52:38 -0800280 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000281 gig_dbg(level, " %s", dbgline);
282 kfree(dbgline);
283 }
284#endif
285}
286
287/*
288 * format CAPI IE as string
289 */
290
291static const char *format_ie(const char *ie)
292{
Joe Perches475be4d2012-02-19 19:52:38 -0800293 static char result[3 * MAX_FMT_IE_LEN];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000294 int len, count;
295 char *pout = result;
296
297 if (!ie)
298 return "NULL";
299
300 count = len = ie[0];
301 if (count > MAX_FMT_IE_LEN)
Joe Perches475be4d2012-02-19 19:52:38 -0800302 count = MAX_FMT_IE_LEN - 1;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000303 while (count--) {
304 *pout++ = hex_asc_hi(*++ie);
305 *pout++ = hex_asc_lo(*ie);
306 *pout++ = ' ';
307 }
308 if (len > MAX_FMT_IE_LEN) {
309 *pout++ = '.';
310 *pout++ = '.';
311 *pout++ = '.';
312 }
313 *--pout = 0;
314 return result;
315}
316
Tilman Schmidt23b36772010-06-21 13:54:50 +0000317/*
318 * emit DATA_B3_CONF message
319 */
320static void send_data_b3_conf(struct cardstate *cs, struct capi_ctr *ctr,
321 u16 appl, u16 msgid, int channel,
322 u16 handle, u16 info)
323{
324 struct sk_buff *cskb;
325 u8 *msg;
326
327 cskb = alloc_skb(CAPI_DATA_B3_CONF_LEN, GFP_ATOMIC);
328 if (!cskb) {
329 dev_err(cs->dev, "%s: out of memory\n", __func__);
330 return;
331 }
332 /* frequent message, avoid _cmsg overhead */
333 msg = __skb_put(cskb, CAPI_DATA_B3_CONF_LEN);
334 CAPIMSG_SETLEN(msg, CAPI_DATA_B3_CONF_LEN);
335 CAPIMSG_SETAPPID(msg, appl);
336 CAPIMSG_SETCOMMAND(msg, CAPI_DATA_B3);
337 CAPIMSG_SETSUBCOMMAND(msg, CAPI_CONF);
338 CAPIMSG_SETMSGID(msg, msgid);
339 CAPIMSG_SETCONTROLLER(msg, ctr->cnr);
340 CAPIMSG_SETPLCI_PART(msg, channel);
341 CAPIMSG_SETNCCI_PART(msg, 1);
342 CAPIMSG_SETHANDLE_CONF(msg, handle);
343 CAPIMSG_SETINFO_CONF(msg, info);
344
345 /* emit message */
346 dump_rawmsg(DEBUG_MCMD, __func__, msg);
347 capi_ctr_handle_message(ctr, appl, cskb);
348}
349
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000350
351/*
352 * driver interface functions
353 * ==========================
354 */
355
356/**
357 * gigaset_skb_sent() - acknowledge transmission of outgoing skb
358 * @bcs: B channel descriptor structure.
359 * @skb: sent data.
360 *
361 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
362 * skb has been successfully sent, for signalling completion to the LL.
363 */
364void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *dskb)
365{
366 struct cardstate *cs = bcs->cs;
367 struct gigaset_capi_ctr *iif = cs->iif;
368 struct gigaset_capi_appl *ap = bcs->ap;
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000369 unsigned char *req = skb_mac_header(dskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000370 u16 flags;
371
372 /* update statistics */
373 ++bcs->trans_up;
374
375 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000376 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000377 return;
378 }
379
380 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000381 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000382 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000383 return;
384 }
385
Tilman Schmidt23b36772010-06-21 13:54:50 +0000386 /*
387 * send DATA_B3_CONF if "delivery confirmation" bit was set in request;
388 * otherwise it has already been sent by do_data_b3_req()
389 */
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000390 flags = CAPIMSG_FLAGS(req);
Tilman Schmidt23b36772010-06-21 13:54:50 +0000391 if (flags & CAPI_FLAGS_DELIVERY_CONFIRMATION)
392 send_data_b3_conf(cs, &iif->ctr, ap->id, CAPIMSG_MSGID(req),
393 bcs->channel + 1, CAPIMSG_HANDLE_REQ(req),
394 (flags & ~CAPI_FLAGS_DELIVERY_CONFIRMATION) ?
Joe Perches475be4d2012-02-19 19:52:38 -0800395 CapiFlagsNotSupportedByProtocol :
396 CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000397}
398EXPORT_SYMBOL_GPL(gigaset_skb_sent);
399
400/**
401 * gigaset_skb_rcvd() - pass received skb to LL
402 * @bcs: B channel descriptor structure.
403 * @skb: received data.
404 *
405 * Called by hardware module {bas,ser,usb}_gigaset when user data has
406 * been successfully received, for passing to the LL.
407 * Warning: skb must not be accessed anymore!
408 */
409void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
410{
411 struct cardstate *cs = bcs->cs;
412 struct gigaset_capi_ctr *iif = cs->iif;
413 struct gigaset_capi_appl *ap = bcs->ap;
414 int len = skb->len;
415
416 /* update statistics */
417 bcs->trans_down++;
418
419 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000420 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
421 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000422 return;
423 }
424
425 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000426 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000427 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000428 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000429 return;
430 }
431
432 /*
433 * prepend DATA_B3_IND message to payload
434 * Parameters: NCCI = 1, all others 0/unused
435 * frequent message, avoid _cmsg overhead
436 */
437 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
438 CAPIMSG_SETLEN(skb->data, CAPI_DATA_B3_REQ_LEN);
439 CAPIMSG_SETAPPID(skb->data, ap->id);
440 CAPIMSG_SETCOMMAND(skb->data, CAPI_DATA_B3);
441 CAPIMSG_SETSUBCOMMAND(skb->data, CAPI_IND);
442 CAPIMSG_SETMSGID(skb->data, ap->nextMessageNumber++);
443 CAPIMSG_SETCONTROLLER(skb->data, iif->ctr.cnr);
444 CAPIMSG_SETPLCI_PART(skb->data, bcs->channel + 1);
445 CAPIMSG_SETNCCI_PART(skb->data, 1);
446 /* Data parameter not used */
447 CAPIMSG_SETDATALEN(skb->data, len);
448 /* Data handle parameter not used */
449 CAPIMSG_SETFLAGS(skb->data, 0);
450 /* Data64 parameter not present */
451
452 /* emit message */
Tilman Schmidt6a753422010-07-05 14:18:59 +0000453 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000454 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
455}
456EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
457
458/**
459 * gigaset_isdn_rcv_err() - signal receive error
460 * @bcs: B channel descriptor structure.
461 *
462 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
463 * has occurred, for signalling to the LL.
464 */
465void gigaset_isdn_rcv_err(struct bc_state *bcs)
466{
467 /* if currently ignoring packets, just count down */
468 if (bcs->ignore) {
469 bcs->ignore--;
470 return;
471 }
472
473 /* update statistics */
474 bcs->corrupted++;
475
476 /* ToDo: signal error -> LL */
477}
478EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
479
480/**
481 * gigaset_isdn_icall() - signal incoming call
482 * @at_state: connection state structure.
483 *
484 * Called by main module at tasklet level to notify the LL that an incoming
485 * call has been received. @at_state contains the parameters of the call.
486 *
487 * Return value: call disposition (ICALL_*)
488 */
489int gigaset_isdn_icall(struct at_state_t *at_state)
490{
491 struct cardstate *cs = at_state->cs;
492 struct bc_state *bcs = at_state->bcs;
493 struct gigaset_capi_ctr *iif = cs->iif;
494 struct gigaset_capi_appl *ap;
495 u32 actCIPmask;
496 struct sk_buff *skb;
497 unsigned int msgsize;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000498 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000499 int i;
500
501 /*
502 * ToDo: signal calls without a free B channel, too
503 * (requires a u8 handle for the at_state structure that can
504 * be stored in the PLCI and used in the CONNECT_RESP message
505 * handler to retrieve it)
506 */
507 if (!bcs)
508 return ICALL_IGNORE;
509
510 /* prepare CONNECT_IND message, using B channel number as PLCI */
511 capi_cmsg_header(&iif->hcmsg, 0, CAPI_CONNECT, CAPI_IND, 0,
512 iif->ctr.cnr | ((bcs->channel + 1) << 8));
513
514 /* minimum size, all structs empty */
515 msgsize = CAPI_CONNECT_IND_BASELEN;
516
517 /* Bearer Capability (mandatory) */
518 if (at_state->str_var[STR_ZBC]) {
519 /* pass on BC from Gigaset */
520 if (encode_ie(at_state->str_var[STR_ZBC], iif->bc_buf,
521 MAX_BC_OCTETS) < 0) {
522 dev_warn(cs->dev, "RING ignored - bad BC %s\n",
523 at_state->str_var[STR_ZBC]);
524 return ICALL_IGNORE;
525 }
526
527 /* look up corresponding CIP value */
528 iif->hcmsg.CIPValue = 0; /* default if nothing found */
529 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
530 if (cip2bchlc[i].bc != NULL &&
531 cip2bchlc[i].hlc == NULL &&
532 !strcmp(cip2bchlc[i].bc,
533 at_state->str_var[STR_ZBC])) {
534 iif->hcmsg.CIPValue = i;
535 break;
536 }
537 } else {
538 /* no BC (internal call): assume CIP 1 (speech, A-law) */
539 iif->hcmsg.CIPValue = 1;
540 encode_ie(cip2bchlc[1].bc, iif->bc_buf, MAX_BC_OCTETS);
541 }
542 iif->hcmsg.BC = iif->bc_buf;
543 msgsize += iif->hcmsg.BC[0];
544
545 /* High Layer Compatibility (optional) */
546 if (at_state->str_var[STR_ZHLC]) {
547 /* pass on HLC from Gigaset */
548 if (encode_ie(at_state->str_var[STR_ZHLC], iif->hlc_buf,
549 MAX_HLC_OCTETS) < 0) {
550 dev_warn(cs->dev, "RING ignored - bad HLC %s\n",
551 at_state->str_var[STR_ZHLC]);
552 return ICALL_IGNORE;
553 }
554 iif->hcmsg.HLC = iif->hlc_buf;
555 msgsize += iif->hcmsg.HLC[0];
556
557 /* look up corresponding CIP value */
558 /* keep BC based CIP value if none found */
559 if (at_state->str_var[STR_ZBC])
560 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
561 if (cip2bchlc[i].hlc != NULL &&
562 !strcmp(cip2bchlc[i].hlc,
563 at_state->str_var[STR_ZHLC]) &&
564 !strcmp(cip2bchlc[i].bc,
565 at_state->str_var[STR_ZBC])) {
566 iif->hcmsg.CIPValue = i;
567 break;
568 }
569 }
570
571 /* Called Party Number (optional) */
572 if (at_state->str_var[STR_ZCPN]) {
573 i = strlen(at_state->str_var[STR_ZCPN]);
574 if (i > MAX_NUMBER_DIGITS) {
575 dev_warn(cs->dev, "RING ignored - bad number %s\n",
576 at_state->str_var[STR_ZBC]);
577 return ICALL_IGNORE;
578 }
579 iif->cdpty_buf[0] = i + 1;
580 iif->cdpty_buf[1] = 0x80; /* type / numbering plan unknown */
Joe Perches475be4d2012-02-19 19:52:38 -0800581 memcpy(iif->cdpty_buf + 2, at_state->str_var[STR_ZCPN], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000582 iif->hcmsg.CalledPartyNumber = iif->cdpty_buf;
583 msgsize += iif->hcmsg.CalledPartyNumber[0];
584 }
585
586 /* Calling Party Number (optional) */
587 if (at_state->str_var[STR_NMBR]) {
588 i = strlen(at_state->str_var[STR_NMBR]);
589 if (i > MAX_NUMBER_DIGITS) {
590 dev_warn(cs->dev, "RING ignored - bad number %s\n",
591 at_state->str_var[STR_ZBC]);
592 return ICALL_IGNORE;
593 }
594 iif->cgpty_buf[0] = i + 2;
595 iif->cgpty_buf[1] = 0x00; /* type / numbering plan unknown */
596 iif->cgpty_buf[2] = 0x80; /* pres. allowed, not screened */
Joe Perches475be4d2012-02-19 19:52:38 -0800597 memcpy(iif->cgpty_buf + 3, at_state->str_var[STR_NMBR], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000598 iif->hcmsg.CallingPartyNumber = iif->cgpty_buf;
599 msgsize += iif->hcmsg.CallingPartyNumber[0];
600 }
601
602 /* remaining parameters (not supported, always left NULL):
603 * - CalledPartySubaddress
604 * - CallingPartySubaddress
605 * - AdditionalInfo
606 * - BChannelinformation
607 * - Keypadfacility
608 * - Useruserdata
609 * - Facilitydataarray
610 */
611
612 gig_dbg(DEBUG_CMD, "icall: PLCI %x CIP %d BC %s",
613 iif->hcmsg.adr.adrPLCI, iif->hcmsg.CIPValue,
614 format_ie(iif->hcmsg.BC));
615 gig_dbg(DEBUG_CMD, "icall: HLC %s",
616 format_ie(iif->hcmsg.HLC));
617 gig_dbg(DEBUG_CMD, "icall: CgPty %s",
618 format_ie(iif->hcmsg.CallingPartyNumber));
619 gig_dbg(DEBUG_CMD, "icall: CdPty %s",
620 format_ie(iif->hcmsg.CalledPartyNumber));
621
622 /* scan application list for matching listeners */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000623 spin_lock_irqsave(&bcs->aplock, flags);
624 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE) {
625 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
626 __func__, bcs->ap, bcs->apconnstate);
627 bcs->ap = NULL;
628 bcs->apconnstate = APCONN_NONE;
629 }
630 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000631 actCIPmask = 1 | (1 << iif->hcmsg.CIPValue);
632 list_for_each_entry(ap, &iif->appls, ctrlist)
633 if (actCIPmask & ap->listenCIPmask) {
634 /* build CONNECT_IND message for this application */
635 iif->hcmsg.ApplId = ap->id;
636 iif->hcmsg.Messagenumber = ap->nextMessageNumber++;
637
638 skb = alloc_skb(msgsize, GFP_ATOMIC);
639 if (!skb) {
640 dev_err(cs->dev, "%s: out of memory\n",
641 __func__);
642 break;
643 }
644 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
645 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
646
647 /* add to listeners on this B channel, update state */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000648 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000649 ap->bcnext = bcs->ap;
650 bcs->ap = ap;
651 bcs->chstate |= CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000652 bcs->apconnstate = APCONN_SETUP;
653 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000654
655 /* emit message */
656 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
657 }
658
659 /*
660 * Return "accept" if any listeners.
661 * Gigaset will send ALERTING.
662 * There doesn't seem to be a way to avoid this.
663 */
664 return bcs->ap ? ICALL_ACCEPT : ICALL_IGNORE;
665}
666
667/*
668 * send a DISCONNECT_IND message to an application
669 * does not sleep, clobbers the controller's hcmsg structure
670 */
671static void send_disconnect_ind(struct bc_state *bcs,
672 struct gigaset_capi_appl *ap, u16 reason)
673{
674 struct cardstate *cs = bcs->cs;
675 struct gigaset_capi_ctr *iif = cs->iif;
676 struct sk_buff *skb;
677
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000678 if (bcs->apconnstate == APCONN_NONE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000679 return;
680
681 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT, CAPI_IND,
682 ap->nextMessageNumber++,
683 iif->ctr.cnr | ((bcs->channel + 1) << 8));
684 iif->hcmsg.Reason = reason;
685 skb = alloc_skb(CAPI_DISCONNECT_IND_LEN, GFP_ATOMIC);
686 if (!skb) {
687 dev_err(cs->dev, "%s: out of memory\n", __func__);
688 return;
689 }
690 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, CAPI_DISCONNECT_IND_LEN));
691 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000692 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
693}
694
695/*
696 * send a DISCONNECT_B3_IND message to an application
697 * Parameters: NCCI = 1, NCPI empty, Reason_B3 = 0
698 * does not sleep, clobbers the controller's hcmsg structure
699 */
700static void send_disconnect_b3_ind(struct bc_state *bcs,
701 struct gigaset_capi_appl *ap)
702{
703 struct cardstate *cs = bcs->cs;
704 struct gigaset_capi_ctr *iif = cs->iif;
705 struct sk_buff *skb;
706
707 /* nothing to do if no logical connection active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000708 if (bcs->apconnstate < APCONN_ACTIVE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000709 return;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000710 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000711
712 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
713 ap->nextMessageNumber++,
714 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
715 skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_ATOMIC);
716 if (!skb) {
717 dev_err(cs->dev, "%s: out of memory\n", __func__);
718 return;
719 }
720 capi_cmsg2message(&iif->hcmsg,
721 __skb_put(skb, CAPI_DISCONNECT_B3_IND_BASELEN));
722 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
723 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
724}
725
726/**
727 * gigaset_isdn_connD() - signal D channel connect
728 * @bcs: B channel descriptor structure.
729 *
730 * Called by main module at tasklet level to notify the LL that the D channel
731 * connection has been established.
732 */
733void gigaset_isdn_connD(struct bc_state *bcs)
734{
735 struct cardstate *cs = bcs->cs;
736 struct gigaset_capi_ctr *iif = cs->iif;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000737 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000738 struct sk_buff *skb;
739 unsigned int msgsize;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000740 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000741
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000742 spin_lock_irqsave(&bcs->aplock, flags);
743 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000744 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000745 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000746 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000747 return;
748 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000749 if (bcs->apconnstate == APCONN_NONE) {
750 spin_unlock_irqrestore(&bcs->aplock, flags);
751 dev_warn(cs->dev, "%s: application %u not connected\n",
752 __func__, ap->id);
753 return;
754 }
755 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000756 while (ap->bcnext) {
757 /* this should never happen */
758 dev_warn(cs->dev, "%s: dropping extra application %u\n",
759 __func__, ap->bcnext->id);
760 send_disconnect_ind(bcs, ap->bcnext,
761 CapiCallGivenToOtherApplication);
762 ap->bcnext = ap->bcnext->bcnext;
763 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000764
765 /* prepare CONNECT_ACTIVE_IND message
766 * Note: LLC not supported by device
767 */
768 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_CONNECT_ACTIVE, CAPI_IND,
769 ap->nextMessageNumber++,
770 iif->ctr.cnr | ((bcs->channel + 1) << 8));
771
772 /* minimum size, all structs empty */
773 msgsize = CAPI_CONNECT_ACTIVE_IND_BASELEN;
774
775 /* ToDo: set parameter: Connected number
776 * (requires ev-layer state machine extension to collect
777 * ZCON device reply)
778 */
779
780 /* build and emit CONNECT_ACTIVE_IND message */
781 skb = alloc_skb(msgsize, GFP_ATOMIC);
782 if (!skb) {
783 dev_err(cs->dev, "%s: out of memory\n", __func__);
784 return;
785 }
786 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
787 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
788 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
789}
790
791/**
792 * gigaset_isdn_hupD() - signal D channel hangup
793 * @bcs: B channel descriptor structure.
794 *
795 * Called by main module at tasklet level to notify the LL that the D channel
796 * connection has been shut down.
797 */
798void gigaset_isdn_hupD(struct bc_state *bcs)
799{
800 struct gigaset_capi_appl *ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000801 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000802
803 /*
804 * ToDo: pass on reason code reported by device
805 * (requires ev-layer state machine extension to collect
806 * ZCAU device reply)
807 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000808 spin_lock_irqsave(&bcs->aplock, flags);
809 while (bcs->ap != NULL) {
810 ap = bcs->ap;
811 bcs->ap = ap->bcnext;
812 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000813 send_disconnect_b3_ind(bcs, ap);
814 send_disconnect_ind(bcs, ap, 0);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000815 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000816 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000817 bcs->apconnstate = APCONN_NONE;
818 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000819}
820
821/**
822 * gigaset_isdn_connB() - signal B channel connect
823 * @bcs: B channel descriptor structure.
824 *
825 * Called by main module at tasklet level to notify the LL that the B channel
826 * connection has been established.
827 */
828void gigaset_isdn_connB(struct bc_state *bcs)
829{
830 struct cardstate *cs = bcs->cs;
831 struct gigaset_capi_ctr *iif = cs->iif;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000832 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000833 struct sk_buff *skb;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000834 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000835 unsigned int msgsize;
836 u8 command;
837
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000838 spin_lock_irqsave(&bcs->aplock, flags);
839 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000840 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000841 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000842 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000843 return;
844 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000845 if (!bcs->apconnstate) {
846 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000847 dev_warn(cs->dev, "%s: application %u not connected\n",
848 __func__, ap->id);
849 return;
850 }
851
852 /*
853 * emit CONNECT_B3_ACTIVE_IND if we already got CONNECT_B3_REQ;
854 * otherwise we have to emit CONNECT_B3_IND first, and follow up with
855 * CONNECT_B3_ACTIVE_IND in reply to CONNECT_B3_RESP
856 * Parameters in both cases always: NCCI = 1, NCPI empty
857 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000858 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000859 command = CAPI_CONNECT_B3_ACTIVE;
860 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
861 } else {
862 command = CAPI_CONNECT_B3;
863 msgsize = CAPI_CONNECT_B3_IND_BASELEN;
864 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000865 bcs->apconnstate = APCONN_ACTIVE;
866
867 spin_unlock_irqrestore(&bcs->aplock, flags);
868
869 while (ap->bcnext) {
870 /* this should never happen */
871 dev_warn(cs->dev, "%s: dropping extra application %u\n",
872 __func__, ap->bcnext->id);
873 send_disconnect_ind(bcs, ap->bcnext,
874 CapiCallGivenToOtherApplication);
875 ap->bcnext = ap->bcnext->bcnext;
876 }
877
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000878 capi_cmsg_header(&iif->hcmsg, ap->id, command, CAPI_IND,
879 ap->nextMessageNumber++,
880 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
881 skb = alloc_skb(msgsize, GFP_ATOMIC);
882 if (!skb) {
883 dev_err(cs->dev, "%s: out of memory\n", __func__);
884 return;
885 }
886 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
887 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000888 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
889}
890
891/**
892 * gigaset_isdn_hupB() - signal B channel hangup
893 * @bcs: B channel descriptor structure.
894 *
895 * Called by main module to notify the LL that the B channel connection has
896 * been shut down.
897 */
898void gigaset_isdn_hupB(struct bc_state *bcs)
899{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000900 struct gigaset_capi_appl *ap = bcs->ap;
901
902 /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
903
904 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000905 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000906 return;
907 }
908
909 send_disconnect_b3_ind(bcs, ap);
910}
911
912/**
913 * gigaset_isdn_start() - signal device availability
914 * @cs: device descriptor structure.
915 *
916 * Called by main module to notify the LL that the device is available for
917 * use.
918 */
919void gigaset_isdn_start(struct cardstate *cs)
920{
921 struct gigaset_capi_ctr *iif = cs->iif;
922
923 /* fill profile data: manufacturer name */
924 strcpy(iif->ctr.manu, "Siemens");
925 /* CAPI and device version */
926 iif->ctr.version.majorversion = 2; /* CAPI 2.0 */
927 iif->ctr.version.minorversion = 0;
928 /* ToDo: check/assert cs->gotfwver? */
929 iif->ctr.version.majormanuversion = cs->fwver[0];
930 iif->ctr.version.minormanuversion = cs->fwver[1];
931 /* number of B channels supported */
932 iif->ctr.profile.nbchannel = cs->channels;
933 /* global options: internal controller, supplementary services */
934 iif->ctr.profile.goptions = 0x11;
935 /* B1 protocols: 64 kbit/s HDLC or transparent */
936 iif->ctr.profile.support1 = 0x03;
937 /* B2 protocols: transparent only */
938 /* ToDo: X.75 SLP ? */
939 iif->ctr.profile.support2 = 0x02;
940 /* B3 protocols: transparent only */
941 iif->ctr.profile.support3 = 0x01;
942 /* no serial number */
943 strcpy(iif->ctr.serial, "0");
944 capi_ctr_ready(&iif->ctr);
945}
946
947/**
948 * gigaset_isdn_stop() - signal device unavailability
949 * @cs: device descriptor structure.
950 *
951 * Called by main module to notify the LL that the device is no longer
952 * available for use.
953 */
954void gigaset_isdn_stop(struct cardstate *cs)
955{
956 struct gigaset_capi_ctr *iif = cs->iif;
957 capi_ctr_down(&iif->ctr);
958}
959
960/*
961 * kernel CAPI callback methods
962 * ============================
963 */
964
965/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000966 * register CAPI application
967 */
968static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
Joe Perches475be4d2012-02-19 19:52:38 -0800969 capi_register_params *rp)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000970{
971 struct gigaset_capi_ctr *iif
972 = container_of(ctr, struct gigaset_capi_ctr, ctr);
973 struct cardstate *cs = ctr->driverdata;
974 struct gigaset_capi_appl *ap;
975
Tilman Schmidt6a753422010-07-05 14:18:59 +0000976 gig_dbg(DEBUG_CMD, "%s [%u] l3cnt=%u blkcnt=%u blklen=%u",
977 __func__, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
978
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000979 list_for_each_entry(ap, &iif->appls, ctrlist)
980 if (ap->id == appl) {
981 dev_notice(cs->dev,
982 "application %u already registered\n", appl);
983 return;
984 }
985
986 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
987 if (!ap) {
988 dev_err(cs->dev, "%s: out of memory\n", __func__);
989 return;
990 }
991 ap->id = appl;
Tilman Schmidte7752ee2010-06-21 13:54:19 +0000992 ap->rp = *rp;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000993
994 list_add(&ap->ctrlist, &iif->appls);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000995 dev_info(cs->dev, "application %u registered\n", ap->id);
996}
997
998/*
999 * remove CAPI application from channel
1000 * helper function to keep indentation levels down and stay in 80 columns
1001 */
1002
1003static inline void remove_appl_from_channel(struct bc_state *bcs,
1004 struct gigaset_capi_appl *ap)
1005{
1006 struct cardstate *cs = bcs->cs;
1007 struct gigaset_capi_appl *bcap;
1008 unsigned long flags;
1009 int prevconnstate;
1010
1011 spin_lock_irqsave(&bcs->aplock, flags);
1012 bcap = bcs->ap;
1013 if (bcap == NULL) {
1014 spin_unlock_irqrestore(&bcs->aplock, flags);
1015 return;
1016 }
1017
1018 /* check first application on channel */
1019 if (bcap == ap) {
1020 bcs->ap = ap->bcnext;
1021 if (bcs->ap != NULL) {
1022 spin_unlock_irqrestore(&bcs->aplock, flags);
1023 return;
1024 }
1025
1026 /* none left, clear channel state */
1027 prevconnstate = bcs->apconnstate;
1028 bcs->apconnstate = APCONN_NONE;
1029 spin_unlock_irqrestore(&bcs->aplock, flags);
1030
1031 if (prevconnstate == APCONN_ACTIVE) {
1032 dev_notice(cs->dev, "%s: hanging up channel %u\n",
1033 __func__, bcs->channel);
1034 gigaset_add_event(cs, &bcs->at_state,
1035 EV_HUP, NULL, 0, NULL);
1036 gigaset_schedule_event(cs);
1037 }
1038 return;
1039 }
1040
1041 /* check remaining list */
1042 do {
1043 if (bcap->bcnext == ap) {
1044 bcap->bcnext = bcap->bcnext->bcnext;
Dan Carpenter7e27a0a2010-08-05 22:23:23 +00001045 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001046 return;
1047 }
1048 bcap = bcap->bcnext;
1049 } while (bcap != NULL);
1050 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001051}
1052
1053/*
1054 * release CAPI application
1055 */
1056static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
1057{
1058 struct gigaset_capi_ctr *iif
1059 = container_of(ctr, struct gigaset_capi_ctr, ctr);
1060 struct cardstate *cs = iif->ctr.driverdata;
1061 struct gigaset_capi_appl *ap, *tmp;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001062 unsigned ch;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001063
Tilman Schmidt6a753422010-07-05 14:18:59 +00001064 gig_dbg(DEBUG_CMD, "%s [%u]", __func__, appl);
1065
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001066 list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
1067 if (ap->id == appl) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001068 /* remove from any channels */
1069 for (ch = 0; ch < cs->channels; ch++)
1070 remove_appl_from_channel(&cs->bcs[ch], ap);
1071
1072 /* remove from registration list */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001073 list_del(&ap->ctrlist);
1074 kfree(ap);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001075 dev_info(cs->dev, "application %u released\n", appl);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001076 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001077}
1078
1079/*
1080 * =====================================================================
1081 * outgoing CAPI message handler
1082 * =====================================================================
1083 */
1084
1085/*
1086 * helper function: emit reply message with given Info value
1087 */
1088static void send_conf(struct gigaset_capi_ctr *iif,
1089 struct gigaset_capi_appl *ap,
1090 struct sk_buff *skb,
1091 u16 info)
1092{
1093 /*
1094 * _CONF replies always only have NCCI and Info parameters
1095 * so they'll fit into the _REQ message skb
1096 */
1097 capi_cmsg_answer(&iif->acmsg);
1098 iif->acmsg.Info = info;
1099 capi_cmsg2message(&iif->acmsg, skb->data);
1100 __skb_trim(skb, CAPI_STDCONF_LEN);
1101 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1102 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1103}
1104
1105/*
1106 * process FACILITY_REQ message
1107 */
1108static void do_facility_req(struct gigaset_capi_ctr *iif,
1109 struct gigaset_capi_appl *ap,
1110 struct sk_buff *skb)
1111{
1112 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001113 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001114 struct sk_buff *cskb;
1115 u8 *pparam;
1116 unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
1117 u16 function, info;
1118 static u8 confparam[10]; /* max. 9 octets + length byte */
1119
1120 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001121 capi_message2cmsg(cmsg, skb->data);
1122 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001123
1124 /*
1125 * Facility Request Parameter is not decoded by capi_message2cmsg()
1126 * encoding depends on Facility Selector
1127 */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001128 switch (cmsg->FacilitySelector) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001129 case CAPI_FACILITY_DTMF: /* ToDo */
1130 info = CapiFacilityNotSupported;
1131 confparam[0] = 2; /* length */
1132 /* DTMF information: Unknown DTMF request */
1133 capimsg_setu16(confparam, 1, 2);
1134 break;
1135
1136 case CAPI_FACILITY_V42BIS: /* not supported */
1137 info = CapiFacilityNotSupported;
1138 confparam[0] = 2; /* length */
1139 /* V.42 bis information: not available */
1140 capimsg_setu16(confparam, 1, 1);
1141 break;
1142
1143 case CAPI_FACILITY_SUPPSVC:
1144 /* decode Function parameter */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001145 pparam = cmsg->FacilityRequestParameter;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001146 if (pparam == NULL || pparam[0] < 2) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001147 dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
1148 "Facility Request Parameter");
1149 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1150 return;
1151 }
1152 function = CAPIMSG_U16(pparam, 1);
1153 switch (function) {
1154 case CAPI_SUPPSVC_GETSUPPORTED:
1155 info = CapiSuccess;
1156 /* Supplementary Service specific parameter */
1157 confparam[3] = 6; /* length */
1158 /* Supplementary services info: Success */
1159 capimsg_setu16(confparam, 4, CapiSuccess);
1160 /* Supported Services: none */
1161 capimsg_setu32(confparam, 6, 0);
1162 break;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001163 case CAPI_SUPPSVC_LISTEN:
1164 if (pparam[0] < 7 || pparam[3] < 4) {
1165 dev_notice(cs->dev, "%s: %s missing\n",
1166 "FACILITY_REQ", "Notification Mask");
1167 send_conf(iif, ap, skb,
1168 CapiIllMessageParmCoding);
1169 return;
1170 }
1171 if (CAPIMSG_U32(pparam, 4) != 0) {
1172 dev_notice(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001173 "%s: unsupported supplementary service notification mask 0x%x\n",
1174 "FACILITY_REQ", CAPIMSG_U32(pparam, 4));
Tilman Schmidt18c22592010-07-05 14:19:04 +00001175 info = CapiFacilitySpecificFunctionNotSupported;
1176 confparam[3] = 2; /* length */
1177 capimsg_setu16(confparam, 4,
Joe Perches475be4d2012-02-19 19:52:38 -08001178 CapiSupplementaryServiceNotSupported);
Tilman Schmidt18c22592010-07-05 14:19:04 +00001179 }
1180 info = CapiSuccess;
1181 confparam[3] = 2; /* length */
1182 capimsg_setu16(confparam, 4, CapiSuccess);
1183 break;
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001184
1185 /* ToDo: add supported services */
1186
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001187 default:
Tilman Schmidt18c22592010-07-05 14:19:04 +00001188 dev_notice(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001189 "%s: unsupported supplementary service function 0x%04x\n",
Tilman Schmidt18c22592010-07-05 14:19:04 +00001190 "FACILITY_REQ", function);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001191 info = CapiFacilitySpecificFunctionNotSupported;
1192 /* Supplementary Service specific parameter */
1193 confparam[3] = 2; /* length */
1194 /* Supplementary services info: not supported */
1195 capimsg_setu16(confparam, 4,
1196 CapiSupplementaryServiceNotSupported);
1197 }
1198
1199 /* Facility confirmation parameter */
1200 confparam[0] = confparam[3] + 3; /* total length */
1201 /* Function: copy from _REQ message */
1202 capimsg_setu16(confparam, 1, function);
1203 /* Supplementary Service specific parameter already set above */
1204 break;
1205
1206 case CAPI_FACILITY_WAKEUP: /* ToDo */
1207 info = CapiFacilityNotSupported;
1208 confparam[0] = 2; /* length */
1209 /* Number of accepted awake request parameters: 0 */
1210 capimsg_setu16(confparam, 1, 0);
1211 break;
1212
1213 default:
1214 info = CapiFacilityNotSupported;
1215 confparam[0] = 0; /* empty struct */
1216 }
1217
1218 /* send FACILITY_CONF with given Info and confirmation parameter */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001219 capi_cmsg_answer(cmsg);
1220 cmsg->Info = info;
1221 cmsg->FacilityConfirmationParameter = confparam;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001222 msgsize += confparam[0]; /* length */
1223 cskb = alloc_skb(msgsize, GFP_ATOMIC);
1224 if (!cskb) {
1225 dev_err(cs->dev, "%s: out of memory\n", __func__);
1226 return;
1227 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001228 capi_cmsg2message(cmsg, __skb_put(cskb, msgsize));
1229 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1230 capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001231}
1232
1233
1234/*
1235 * process LISTEN_REQ message
1236 * just store the masks in the application data structure
1237 */
1238static void do_listen_req(struct gigaset_capi_ctr *iif,
1239 struct gigaset_capi_appl *ap,
1240 struct sk_buff *skb)
1241{
1242 /* decode message */
1243 capi_message2cmsg(&iif->acmsg, skb->data);
1244 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1245
1246 /* store listening parameters */
1247 ap->listenInfoMask = iif->acmsg.InfoMask;
1248 ap->listenCIPmask = iif->acmsg.CIPmask;
1249 send_conf(iif, ap, skb, CapiSuccess);
1250}
1251
1252/*
1253 * process ALERT_REQ message
1254 * nothing to do, Gigaset always alerts anyway
1255 */
1256static void do_alert_req(struct gigaset_capi_ctr *iif,
1257 struct gigaset_capi_appl *ap,
1258 struct sk_buff *skb)
1259{
1260 /* decode message */
1261 capi_message2cmsg(&iif->acmsg, skb->data);
1262 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1263 send_conf(iif, ap, skb, CapiAlertAlreadySent);
1264}
1265
1266/*
1267 * process CONNECT_REQ message
1268 * allocate a B channel, prepare dial commands, queue a DIAL event,
1269 * emit CONNECT_CONF reply
1270 */
1271static void do_connect_req(struct gigaset_capi_ctr *iif,
1272 struct gigaset_capi_appl *ap,
1273 struct sk_buff *skb)
1274{
1275 struct cardstate *cs = iif->ctr.driverdata;
1276 _cmsg *cmsg = &iif->acmsg;
1277 struct bc_state *bcs;
1278 char **commands;
1279 char *s;
1280 u8 *pp;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001281 unsigned long flags;
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001282 int i, l, lbc, lhlc;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001283 u16 info;
1284
1285 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001286 capi_message2cmsg(cmsg, skb->data);
1287 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001288
1289 /* get free B channel & construct PLCI */
1290 bcs = gigaset_get_free_channel(cs);
1291 if (!bcs) {
1292 dev_notice(cs->dev, "%s: no B channel available\n",
1293 "CONNECT_REQ");
1294 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1295 return;
1296 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001297 spin_lock_irqsave(&bcs->aplock, flags);
1298 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE)
1299 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
1300 __func__, bcs->ap, bcs->apconnstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001301 ap->bcnext = NULL;
1302 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001303 bcs->apconnstate = APCONN_SETUP;
1304 spin_unlock_irqrestore(&bcs->aplock, flags);
1305
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001306 bcs->rx_bufsize = ap->rp.datablklen;
1307 dev_kfree_skb(bcs->rx_skb);
1308 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001309 cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1310
1311 /* build command table */
Joe Perches475be4d2012-02-19 19:52:38 -08001312 commands = kzalloc(AT_NUM * (sizeof *commands), GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001313 if (!commands)
1314 goto oom;
1315
1316 /* encode parameter: Called party number */
1317 pp = cmsg->CalledPartyNumber;
1318 if (pp == NULL || *pp == 0) {
1319 dev_notice(cs->dev, "%s: %s missing\n",
1320 "CONNECT_REQ", "Called party number");
1321 info = CapiIllMessageParmCoding;
1322 goto error;
1323 }
1324 l = *pp++;
1325 /* check type of number/numbering plan byte */
1326 switch (*pp) {
1327 case 0x80: /* unknown type / unknown numbering plan */
1328 case 0x81: /* unknown type / ISDN/Telephony numbering plan */
1329 break;
1330 default: /* others: warn about potential misinterpretation */
1331 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1332 "CONNECT_REQ", "Called party number", *pp);
1333 }
1334 pp++;
1335 l--;
1336 /* translate "**" internal call prefix to CTP value */
1337 if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1338 s = "^SCTP=0\r";
1339 pp += 2;
1340 l -= 2;
1341 } else {
1342 s = "^SCTP=1\r";
1343 }
1344 commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1345 if (!commands[AT_TYPE])
1346 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001347 commands[AT_DIAL] = kmalloc(l + 3, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001348 if (!commands[AT_DIAL])
1349 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001350 snprintf(commands[AT_DIAL], l + 3, "D%.*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001351
1352 /* encode parameter: Calling party number */
1353 pp = cmsg->CallingPartyNumber;
1354 if (pp != NULL && *pp > 0) {
1355 l = *pp++;
1356
1357 /* check type of number/numbering plan byte */
1358 /* ToDo: allow for/handle Ext=1? */
1359 switch (*pp) {
1360 case 0x00: /* unknown type / unknown numbering plan */
1361 case 0x01: /* unknown type / ISDN/Telephony num. plan */
1362 break;
1363 default:
1364 dev_notice(cs->dev,
1365 "%s: %s type/plan 0x%02x unsupported\n",
1366 "CONNECT_REQ", "Calling party number", *pp);
1367 }
1368 pp++;
1369 l--;
1370
1371 /* check presentation indicator */
1372 if (!l) {
1373 dev_notice(cs->dev, "%s: %s IE truncated\n",
1374 "CONNECT_REQ", "Calling party number");
1375 info = CapiIllMessageParmCoding;
1376 goto error;
1377 }
1378 switch (*pp & 0xfc) { /* ignore Screening indicator */
1379 case 0x80: /* Presentation allowed */
1380 s = "^SCLIP=1\r";
1381 break;
1382 case 0xa0: /* Presentation restricted */
1383 s = "^SCLIP=0\r";
1384 break;
1385 default:
1386 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1387 "CONNECT_REQ",
1388 "Presentation/Screening indicator",
1389 *pp);
1390 s = "^SCLIP=1\r";
1391 }
1392 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1393 if (!commands[AT_CLIP])
1394 goto oom;
1395 pp++;
1396 l--;
1397
1398 if (l) {
1399 /* number */
Joe Perches475be4d2012-02-19 19:52:38 -08001400 commands[AT_MSN] = kmalloc(l + 8, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001401 if (!commands[AT_MSN])
1402 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001403 snprintf(commands[AT_MSN], l + 8, "^SMSN=%*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001404 }
1405 }
1406
1407 /* check parameter: CIP Value */
Tilman Schmidt6ad34142010-03-16 07:04:01 +00001408 if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001409 (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1410 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1411 "CONNECT_REQ", cmsg->CIPValue);
1412 info = CapiCipValueUnknown;
1413 goto error;
1414 }
1415
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001416 /*
1417 * check/encode parameters: BC & HLC
1418 * must be encoded together as device doesn't accept HLC separately
1419 * explicit parameters override values derived from CIP
1420 */
1421
1422 /* determine lengths */
1423 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001424 lbc = 2 * cmsg->BC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001425 else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
1426 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1427 else /* no BC */
1428 lbc = 0;
1429 if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001430 lhlc = 2 * cmsg->HLC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001431 else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1432 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1433 else /* no HLC */
1434 lhlc = 0;
1435
1436 if (lbc) {
1437 /* have BC: allocate and assemble command string */
1438 l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
1439 if (lhlc)
1440 l += lhlc + 7; /* ";^SHLC=" + value */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001441 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1442 if (!commands[AT_BC])
1443 goto oom;
1444 strcpy(commands[AT_BC], "^SBC=");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001445 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1446 decode_ie(cmsg->BC, commands[AT_BC] + 5);
1447 else /* BC derived from CIP */
1448 strcpy(commands[AT_BC] + 5,
1449 cip2bchlc[cmsg->CIPValue].bc);
1450 if (lhlc) {
1451 strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1452 if (cmsg->HLC && cmsg->HLC[0])
1453 /* HLC specified explicitly */
1454 decode_ie(cmsg->HLC,
1455 commands[AT_BC] + lbc + 12);
1456 else /* HLC derived from CIP */
1457 strcpy(commands[AT_BC] + lbc + 12,
1458 cip2bchlc[cmsg->CIPValue].hlc);
1459 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001460 strcpy(commands[AT_BC] + l - 2, "\r");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001461 } else {
1462 /* no BC */
1463 if (lhlc) {
1464 dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1465 "CONNECT_REQ");
1466 info = CapiIllMessageParmCoding; /* ? */
1467 goto error;
1468 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001469 }
1470
1471 /* check/encode parameter: B Protocol */
1472 if (cmsg->BProtocol == CAPI_DEFAULT) {
1473 bcs->proto2 = L2_HDLC;
1474 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001475 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001476 } else {
1477 switch (cmsg->B1protocol) {
1478 case 0:
1479 bcs->proto2 = L2_HDLC;
1480 break;
1481 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001482 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001483 break;
1484 default:
1485 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001486 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001487 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001488 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001489 }
1490 if (cmsg->B2protocol != 1)
1491 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001492 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001493 cmsg->B2protocol);
1494 if (cmsg->B3protocol != 0)
1495 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001496 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001497 cmsg->B3protocol);
1498 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001499 "CONNECT_REQ", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001500 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001501 "CONNECT_REQ", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001502 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001503 "CONNECT_REQ", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001504 }
1505 commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1506 if (!commands[AT_PROTO])
1507 goto oom;
1508 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1509
1510 /* ToDo: check/encode remaining parameters */
1511 ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001512 "CONNECT_REQ", "Called pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001513 ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001514 "CONNECT_REQ", "Calling pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001515 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001516 "CONNECT_REQ", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001517 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1518 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001519 "CONNECT_REQ", "B Channel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001520 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001521 "CONNECT_REQ", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001522 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001523 "CONNECT_REQ", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001524 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001525 "CONNECT_REQ", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001526 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001527
1528 /* encode parameter: B channel to use */
1529 commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1530 if (!commands[AT_ISO])
1531 goto oom;
1532 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1533 (unsigned) bcs->channel + 1);
1534
1535 /* queue & schedule EV_DIAL event */
1536 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
Tilman Schmidt1528b182010-02-22 13:09:52 +00001537 bcs->at_state.seq_index, NULL)) {
1538 info = CAPI_MSGOSRESOURCEERR;
1539 goto error;
1540 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001541 gigaset_schedule_event(cs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001542 send_conf(iif, ap, skb, CapiSuccess);
1543 return;
1544
1545oom:
1546 dev_err(cs->dev, "%s: out of memory\n", __func__);
1547 info = CAPI_MSGOSRESOURCEERR;
1548error:
1549 if (commands)
1550 for (i = 0; i < AT_NUM; i++)
1551 kfree(commands[i]);
1552 kfree(commands);
1553 gigaset_free_channel(bcs);
1554 send_conf(iif, ap, skb, info);
1555}
1556
1557/*
1558 * process CONNECT_RESP message
1559 * checks protocol parameters and queues an ACCEPT or HUP event
1560 */
1561static void do_connect_resp(struct gigaset_capi_ctr *iif,
1562 struct gigaset_capi_appl *ap,
1563 struct sk_buff *skb)
1564{
1565 struct cardstate *cs = iif->ctr.driverdata;
1566 _cmsg *cmsg = &iif->acmsg;
1567 struct bc_state *bcs;
1568 struct gigaset_capi_appl *oap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001569 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001570 int channel;
1571
1572 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001573 capi_message2cmsg(cmsg, skb->data);
1574 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001575 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001576
1577 /* extract and check channel number from PLCI */
1578 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1579 if (!channel || channel > cs->channels) {
1580 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1581 "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1582 return;
1583 }
1584 bcs = cs->bcs + channel - 1;
1585
1586 switch (cmsg->Reject) {
1587 case 0: /* Accept */
1588 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001589 spin_lock_irqsave(&bcs->aplock, flags);
1590 while (bcs->ap != NULL) {
1591 oap = bcs->ap;
1592 bcs->ap = oap->bcnext;
1593 if (oap != ap) {
1594 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001595 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001596 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001597 spin_lock_irqsave(&bcs->aplock, flags);
1598 }
1599 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001600 ap->bcnext = NULL;
1601 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001602 spin_unlock_irqrestore(&bcs->aplock, flags);
1603
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001604 bcs->rx_bufsize = ap->rp.datablklen;
1605 dev_kfree_skb(bcs->rx_skb);
1606 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001607 bcs->chstate |= CHS_NOTIFY_LL;
1608
1609 /* check/encode B channel protocol */
1610 if (cmsg->BProtocol == CAPI_DEFAULT) {
1611 bcs->proto2 = L2_HDLC;
1612 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001613 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001614 } else {
1615 switch (cmsg->B1protocol) {
1616 case 0:
1617 bcs->proto2 = L2_HDLC;
1618 break;
1619 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001620 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001621 break;
1622 default:
1623 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001624 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001625 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001626 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001627 }
1628 if (cmsg->B2protocol != 1)
1629 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001630 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001631 cmsg->B2protocol);
1632 if (cmsg->B3protocol != 0)
1633 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001634 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001635 cmsg->B3protocol);
1636 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001637 "CONNECT_RESP", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001638 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001639 "CONNECT_RESP", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001640 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001641 "CONNECT_RESP", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001642 }
1643
1644 /* ToDo: check/encode remaining parameters */
1645 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
Joe Perches475be4d2012-02-19 19:52:38 -08001646 "CONNECT_RESP", "Connected Number");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001647 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001648 "CONNECT_RESP", "Connected Subaddress");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001649 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001650 "CONNECT_RESP", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001651 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1652 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001653 "CONNECT_RESP", "BChannel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001654 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001655 "CONNECT_RESP", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001656 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001657 "CONNECT_RESP", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001658 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001659 "CONNECT_RESP", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001660 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001661
1662 /* Accept call */
Joe Perches475be4d2012-02-19 19:52:38 -08001663 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001664 EV_ACCEPT, NULL, 0, NULL))
1665 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001666 gigaset_schedule_event(cs);
1667 return;
1668
1669 case 1: /* Ignore */
1670 /* send DISCONNECT_IND to this application */
1671 send_disconnect_ind(bcs, ap, 0);
1672
1673 /* remove it from the list of listening apps */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001674 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001675 if (bcs->ap == ap) {
1676 bcs->ap = ap->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001677 if (bcs->ap == NULL) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001678 /* last one: stop ev-layer hupD notifications */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001679 bcs->apconnstate = APCONN_NONE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001680 bcs->chstate &= ~CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001681 }
1682 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001683 return;
1684 }
1685 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1686 if (oap->bcnext == ap) {
1687 oap->bcnext = oap->bcnext->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001688 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001689 return;
1690 }
1691 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001692 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001693 dev_err(cs->dev, "%s: application %u not found\n",
1694 __func__, ap->id);
1695 return;
1696
1697 default: /* Reject */
1698 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001699 spin_lock_irqsave(&bcs->aplock, flags);
1700 while (bcs->ap != NULL) {
1701 oap = bcs->ap;
1702 bcs->ap = oap->bcnext;
1703 if (oap != ap) {
1704 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001705 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001706 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001707 spin_lock_irqsave(&bcs->aplock, flags);
1708 }
1709 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001710 ap->bcnext = NULL;
1711 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001712 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001713
1714 /* reject call - will trigger DISCONNECT_IND for this app */
1715 dev_info(cs->dev, "%s: Reject=%x\n",
1716 "CONNECT_RESP", cmsg->Reject);
Joe Perches475be4d2012-02-19 19:52:38 -08001717 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001718 EV_HUP, NULL, 0, NULL))
1719 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001720 gigaset_schedule_event(cs);
1721 return;
1722 }
1723}
1724
1725/*
1726 * process CONNECT_B3_REQ message
1727 * build NCCI and emit CONNECT_B3_CONF reply
1728 */
1729static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1730 struct gigaset_capi_appl *ap,
1731 struct sk_buff *skb)
1732{
1733 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001734 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001735 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001736 int channel;
1737
1738 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001739 capi_message2cmsg(cmsg, skb->data);
1740 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001741
1742 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001743 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001744 if (!channel || channel > cs->channels) {
1745 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001746 "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001747 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1748 return;
1749 }
Joe Perches475be4d2012-02-19 19:52:38 -08001750 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001751
1752 /* mark logical connection active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001753 bcs->apconnstate = APCONN_ACTIVE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001754
1755 /* build NCCI: always 1 (one B3 connection only) */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001756 cmsg->adr.adrNCCI |= 1 << 16;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001757
1758 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001759 ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001760 send_conf(iif, ap, skb,
1761 (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08001762 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001763}
1764
1765/*
1766 * process CONNECT_B3_RESP message
1767 * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1768 * or queue EV_HUP and emit DISCONNECT_B3_IND.
1769 * The emitted message is always shorter than the received one,
1770 * allowing to reuse the skb.
1771 */
1772static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1773 struct gigaset_capi_appl *ap,
1774 struct sk_buff *skb)
1775{
1776 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001777 _cmsg *cmsg = &iif->acmsg;
1778 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001779 int channel;
1780 unsigned int msgsize;
1781 u8 command;
1782
1783 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001784 capi_message2cmsg(cmsg, skb->data);
1785 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001786
1787 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001788 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001789 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00001790 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001791 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001792 "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001793 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001794 return;
1795 }
Joe Perches475be4d2012-02-19 19:52:38 -08001796 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001797
Tilman Schmidt6c911912009-10-25 09:29:37 +00001798 if (cmsg->Reject) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001799 /* Reject: clear B3 connect received flag */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001800 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001801
1802 /* trigger hangup, causing eventual DISCONNECT_IND */
1803 if (!gigaset_add_event(cs, &bcs->at_state,
1804 EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001805 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001806 return;
1807 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001808 gigaset_schedule_event(cs);
1809
1810 /* emit DISCONNECT_B3_IND */
1811 command = CAPI_DISCONNECT_B3;
1812 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1813 } else {
1814 /*
1815 * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1816 * we only send CONNECT_B3_IND if the B channel is up
1817 */
1818 command = CAPI_CONNECT_B3_ACTIVE;
1819 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1820 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001821 capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1822 ap->nextMessageNumber++, cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001823 __skb_trim(skb, msgsize);
Tilman Schmidt6c911912009-10-25 09:29:37 +00001824 capi_cmsg2message(cmsg, skb->data);
1825 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001826 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1827}
1828
1829/*
1830 * process DISCONNECT_REQ message
1831 * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1832 * emit DISCONNECT_CONF reply
1833 */
1834static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1835 struct gigaset_capi_appl *ap,
1836 struct sk_buff *skb)
1837{
1838 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001839 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001840 struct bc_state *bcs;
1841 _cmsg *b3cmsg;
1842 struct sk_buff *b3skb;
1843 int channel;
1844
1845 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001846 capi_message2cmsg(cmsg, skb->data);
1847 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001848
1849 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001850 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001851 if (!channel || channel > cs->channels) {
1852 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001853 "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001854 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1855 return;
1856 }
1857 bcs = cs->bcs + channel - 1;
1858
1859 /* ToDo: process parameter: Additional info */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001860 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1861 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1862 "DISCONNECT_REQ", "B Channel Information");
1863 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1864 "DISCONNECT_REQ", "Keypad Facility");
1865 ignore_cstruct_param(cs, cmsg->Useruserdata,
1866 "DISCONNECT_REQ", "User-User Data");
1867 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1868 "DISCONNECT_REQ", "Facility Data Array");
1869 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001870
1871 /* skip if DISCONNECT_IND already sent */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001872 if (!bcs->apconnstate)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001873 return;
1874
1875 /* check for active logical connection */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001876 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt62a1cfe2012-04-25 13:02:20 +00001877 /* clear it */
1878 bcs->apconnstate = APCONN_SETUP;
1879
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001880 /*
1881 * emit DISCONNECT_B3_IND with cause 0x3301
1882 * use separate cmsg structure, as the content of iif->acmsg
1883 * is still needed for creating the _CONF message
1884 */
1885 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1886 if (!b3cmsg) {
1887 dev_err(cs->dev, "%s: out of memory\n", __func__);
1888 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1889 return;
1890 }
1891 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1892 ap->nextMessageNumber++,
Tilman Schmidt6c911912009-10-25 09:29:37 +00001893 cmsg->adr.adrPLCI | (1 << 16));
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001894 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1895 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1896 if (b3skb == NULL) {
1897 dev_err(cs->dev, "%s: out of memory\n", __func__);
1898 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
Jesper Juhl2393c942010-12-26 09:59:58 +00001899 kfree(b3cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001900 return;
1901 }
1902 capi_cmsg2message(b3cmsg,
Joe Perches475be4d2012-02-19 19:52:38 -08001903 __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN));
Tilman Schmidt62a1cfe2012-04-25 13:02:20 +00001904 dump_cmsg(DEBUG_CMD, __func__, b3cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001905 kfree(b3cmsg);
1906 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1907 }
1908
1909 /* trigger hangup, causing eventual DISCONNECT_IND */
1910 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001911 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1912 return;
1913 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001914 gigaset_schedule_event(cs);
1915
1916 /* emit reply */
1917 send_conf(iif, ap, skb, CapiSuccess);
1918}
1919
1920/*
1921 * process DISCONNECT_B3_REQ message
1922 * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
1923 */
1924static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
1925 struct gigaset_capi_appl *ap,
1926 struct sk_buff *skb)
1927{
1928 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001929 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001930 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001931 int channel;
1932
1933 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001934 capi_message2cmsg(cmsg, skb->data);
1935 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001936
1937 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001938 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001939 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00001940 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001941 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001942 "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001943 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1944 return;
1945 }
Joe Perches475be4d2012-02-19 19:52:38 -08001946 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001947
1948 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001949 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001950 send_conf(iif, ap, skb,
1951 CapiMessageNotSupportedInCurrentState);
1952 return;
1953 }
1954
1955 /* trigger hangup, causing eventual DISCONNECT_B3_IND */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001956 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001957 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1958 return;
1959 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001960 gigaset_schedule_event(cs);
1961
1962 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001963 ignore_cstruct_param(cs, cmsg->NCPI,
Joe Perches475be4d2012-02-19 19:52:38 -08001964 "DISCONNECT_B3_REQ", "NCPI");
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001965 send_conf(iif, ap, skb,
1966 (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08001967 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001968}
1969
1970/*
1971 * process DATA_B3_REQ message
1972 */
1973static void do_data_b3_req(struct gigaset_capi_ctr *iif,
1974 struct gigaset_capi_appl *ap,
1975 struct sk_buff *skb)
1976{
1977 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001978 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001979 int channel = CAPIMSG_PLCI_PART(skb->data);
1980 u16 ncci = CAPIMSG_NCCI_PART(skb->data);
1981 u16 msglen = CAPIMSG_LEN(skb->data);
1982 u16 datalen = CAPIMSG_DATALEN(skb->data);
1983 u16 flags = CAPIMSG_FLAGS(skb->data);
Tilman Schmidt23b36772010-06-21 13:54:50 +00001984 u16 msgid = CAPIMSG_MSGID(skb->data);
1985 u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001986
1987 /* frequent message, avoid _cmsg overhead */
Tilman Schmidt6a753422010-07-05 14:18:59 +00001988 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001989
1990 /* check parameters */
1991 if (channel == 0 || channel > cs->channels || ncci != 1) {
1992 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1993 "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
1994 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1995 return;
1996 }
Joe Perches475be4d2012-02-19 19:52:38 -08001997 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001998 if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
1999 dev_notice(cs->dev, "%s: unexpected length %d\n",
2000 "DATA_B3_REQ", msglen);
2001 if (msglen + datalen != skb->len)
2002 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
2003 "DATA_B3_REQ", msglen, datalen, skb->len);
2004 if (msglen + datalen > skb->len) {
2005 /* message too short for announced data length */
2006 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
2007 return;
2008 }
2009 if (flags & CAPI_FLAGS_RESERVED) {
2010 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
2011 "DATA_B3_REQ", flags);
2012 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
2013 return;
2014 }
2015
2016 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002017 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002018 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2019 return;
2020 }
2021
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002022 /* pull CAPI message into link layer header */
2023 skb_reset_mac_header(skb);
2024 skb->mac_len = msglen;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002025 skb_pull(skb, msglen);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002026
2027 /* pass to device-specific module */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002028 if (cs->ops->send_skb(bcs, skb) < 0) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002029 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2030 return;
2031 }
2032
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002033 /*
Tilman Schmidt23b36772010-06-21 13:54:50 +00002034 * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
2035 * confirmation" bit is set; otherwise we have to send it now
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002036 */
Tilman Schmidt23b36772010-06-21 13:54:50 +00002037 if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
2038 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
2039 flags ? CapiFlagsNotSupportedByProtocol
Joe Perches475be4d2012-02-19 19:52:38 -08002040 : CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002041}
2042
2043/*
2044 * process RESET_B3_REQ message
2045 * just always reply "not supported by current protocol"
2046 */
2047static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
2048 struct gigaset_capi_appl *ap,
2049 struct sk_buff *skb)
2050{
2051 /* decode message */
2052 capi_message2cmsg(&iif->acmsg, skb->data);
2053 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2054 send_conf(iif, ap, skb,
2055 CapiResetProcedureNotSupportedByCurrentProtocol);
2056}
2057
2058/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002059 * unsupported CAPI message handler
2060 */
2061static void do_unsupported(struct gigaset_capi_ctr *iif,
2062 struct gigaset_capi_appl *ap,
2063 struct sk_buff *skb)
2064{
2065 /* decode message */
2066 capi_message2cmsg(&iif->acmsg, skb->data);
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002067 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002068 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2069}
2070
2071/*
2072 * CAPI message handler: no-op
2073 */
2074static void do_nothing(struct gigaset_capi_ctr *iif,
2075 struct gigaset_capi_appl *ap,
2076 struct sk_buff *skb)
2077{
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002078 /* decode message */
2079 capi_message2cmsg(&iif->acmsg, skb->data);
2080 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002081 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002082}
2083
2084static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
2085 struct gigaset_capi_appl *ap,
2086 struct sk_buff *skb)
2087{
Tilman Schmidt6a753422010-07-05 14:18:59 +00002088 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002089 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002090}
2091
2092/* table of outgoing CAPI message handlers with lookup function */
2093typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
2094 struct gigaset_capi_appl *,
2095 struct sk_buff *);
2096
2097static struct {
2098 u16 cmd;
2099 capi_send_handler_t handler;
2100} capi_send_handler_table[] = {
2101 /* most frequent messages first for faster lookup */
2102 { CAPI_DATA_B3_REQ, do_data_b3_req },
2103 { CAPI_DATA_B3_RESP, do_data_b3_resp },
2104
2105 { CAPI_ALERT_REQ, do_alert_req },
2106 { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
2107 { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
2108 { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
2109 { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
2110 { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
2111 { CAPI_CONNECT_REQ, do_connect_req },
2112 { CAPI_CONNECT_RESP, do_connect_resp },
2113 { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
2114 { CAPI_DISCONNECT_B3_RESP, do_nothing },
2115 { CAPI_DISCONNECT_REQ, do_disconnect_req },
2116 { CAPI_DISCONNECT_RESP, do_nothing },
2117 { CAPI_FACILITY_REQ, do_facility_req },
2118 { CAPI_FACILITY_RESP, do_nothing },
2119 { CAPI_LISTEN_REQ, do_listen_req },
2120 { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
2121 { CAPI_RESET_B3_REQ, do_reset_b3_req },
2122 { CAPI_RESET_B3_RESP, do_nothing },
2123
2124 /*
2125 * ToDo: support overlap sending (requires ev-layer state
2126 * machine extension to generate additional ATD commands)
2127 */
2128 { CAPI_INFO_REQ, do_unsupported },
2129 { CAPI_INFO_RESP, do_nothing },
2130
2131 /*
2132 * ToDo: what's the proper response for these?
2133 */
2134 { CAPI_MANUFACTURER_REQ, do_nothing },
2135 { CAPI_MANUFACTURER_RESP, do_nothing },
2136};
2137
2138/* look up handler */
2139static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
2140{
2141 size_t i;
2142
2143 for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2144 if (capi_send_handler_table[i].cmd == cmd)
2145 return capi_send_handler_table[i].handler;
2146 return NULL;
2147}
2148
2149
2150/**
2151 * gigaset_send_message() - accept a CAPI message from an application
2152 * @ctr: controller descriptor structure.
2153 * @skb: CAPI message.
2154 *
2155 * Return value: CAPI error code
2156 * Note: capidrv (and probably others, too) only uses the return value to
2157 * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2158 */
2159static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2160{
2161 struct gigaset_capi_ctr *iif
2162 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2163 struct cardstate *cs = ctr->driverdata;
2164 struct gigaset_capi_appl *ap;
2165 capi_send_handler_t handler;
2166
2167 /* can only handle linear sk_buffs */
2168 if (skb_linearize(skb) < 0) {
2169 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2170 return CAPI_MSGOSRESOURCEERR;
2171 }
2172
2173 /* retrieve application data structure */
2174 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2175 if (!ap) {
2176 dev_notice(cs->dev, "%s: application %u not registered\n",
2177 __func__, CAPIMSG_APPID(skb->data));
2178 return CAPI_ILLAPPNR;
2179 }
2180
2181 /* look up command */
2182 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2183 if (!handler) {
2184 /* unknown/unsupported message type */
2185 if (printk_ratelimit())
2186 dev_notice(cs->dev, "%s: unsupported message %u\n",
2187 __func__, CAPIMSG_CMD(skb->data));
2188 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2189 }
2190
2191 /* serialize */
2192 if (atomic_add_return(1, &iif->sendqlen) > 1) {
2193 /* queue behind other messages */
2194 skb_queue_tail(&iif->sendqueue, skb);
2195 return CAPI_NOERROR;
2196 }
2197
2198 /* process message */
2199 handler(iif, ap, skb);
2200
2201 /* process other messages arrived in the meantime */
2202 while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2203 skb = skb_dequeue(&iif->sendqueue);
2204 if (!skb) {
2205 /* should never happen */
2206 dev_err(cs->dev, "%s: send queue empty\n", __func__);
2207 continue;
2208 }
2209 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2210 if (!ap) {
2211 /* could that happen? */
2212 dev_warn(cs->dev, "%s: application %u vanished\n",
2213 __func__, CAPIMSG_APPID(skb->data));
2214 continue;
2215 }
2216 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2217 if (!handler) {
2218 /* should never happen */
2219 dev_err(cs->dev, "%s: handler %x vanished\n",
2220 __func__, CAPIMSG_CMD(skb->data));
2221 continue;
2222 }
2223 handler(iif, ap, skb);
2224 }
2225
2226 return CAPI_NOERROR;
2227}
2228
2229/**
2230 * gigaset_procinfo() - build single line description for controller
2231 * @ctr: controller descriptor structure.
2232 *
2233 * Return value: pointer to generated string (null terminated)
2234 */
2235static char *gigaset_procinfo(struct capi_ctr *ctr)
2236{
2237 return ctr->name; /* ToDo: more? */
2238}
2239
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002240static int gigaset_proc_show(struct seq_file *m, void *v)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002241{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002242 struct capi_ctr *ctr = m->private;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002243 struct cardstate *cs = ctr->driverdata;
2244 char *s;
2245 int i;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002246
2247 seq_printf(m, "%-16s %s\n", "name", ctr->name);
2248 seq_printf(m, "%-16s %s %s\n", "dev",
Joe Perches475be4d2012-02-19 19:52:38 -08002249 dev_driver_string(cs->dev), dev_name(cs->dev));
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002250 seq_printf(m, "%-16s %d\n", "id", cs->myid);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002251 if (cs->gotfwver)
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002252 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
Joe Perches475be4d2012-02-19 19:52:38 -08002253 cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002254 seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2255 seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002256
2257 switch (cs->mode) {
2258 case M_UNKNOWN:
2259 s = "unknown";
2260 break;
2261 case M_CONFIG:
2262 s = "config";
2263 break;
2264 case M_UNIMODEM:
2265 s = "Unimodem";
2266 break;
2267 case M_CID:
2268 s = "CID";
2269 break;
2270 default:
2271 s = "??";
2272 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002273 seq_printf(m, "%-16s %s\n", "mode", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002274
2275 switch (cs->mstate) {
2276 case MS_UNINITIALIZED:
2277 s = "uninitialized";
2278 break;
2279 case MS_INIT:
2280 s = "init";
2281 break;
2282 case MS_LOCKED:
2283 s = "locked";
2284 break;
2285 case MS_SHUTDOWN:
2286 s = "shutdown";
2287 break;
2288 case MS_RECOVER:
2289 s = "recover";
2290 break;
2291 case MS_READY:
2292 s = "ready";
2293 break;
2294 default:
2295 s = "??";
2296 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002297 seq_printf(m, "%-16s %s\n", "mstate", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002298
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002299 seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2300 seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2301 seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2302 seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002303
2304 for (i = 0; i < cs->channels; i++) {
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002305 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
Joe Perches475be4d2012-02-19 19:52:38 -08002306 cs->bcs[i].corrupted);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002307 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
Joe Perches475be4d2012-02-19 19:52:38 -08002308 cs->bcs[i].trans_down);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002309 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
Joe Perches475be4d2012-02-19 19:52:38 -08002310 cs->bcs[i].trans_up);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002311 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
Joe Perches475be4d2012-02-19 19:52:38 -08002312 cs->bcs[i].chstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002313 switch (cs->bcs[i].proto2) {
2314 case L2_BITSYNC:
2315 s = "bitsync";
2316 break;
2317 case L2_HDLC:
2318 s = "HDLC";
2319 break;
2320 case L2_VOICE:
2321 s = "voice";
2322 break;
2323 default:
2324 s = "??";
2325 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002326 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002327 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002328 return 0;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002329}
2330
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002331static int gigaset_proc_open(struct inode *inode, struct file *file)
2332{
2333 return single_open(file, gigaset_proc_show, PDE(inode)->data);
2334}
2335
2336static const struct file_operations gigaset_proc_fops = {
2337 .owner = THIS_MODULE,
2338 .open = gigaset_proc_open,
2339 .read = seq_read,
2340 .llseek = seq_lseek,
2341 .release = single_release,
2342};
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002343
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002344/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002345 * gigaset_isdn_regdev() - register device to LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002346 * @cs: device descriptor structure.
2347 * @isdnid: device name.
2348 *
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002349 * Return value: 0 on success, error code < 0 on failure
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002350 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002351int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002352{
2353 struct gigaset_capi_ctr *iif;
2354 int rc;
2355
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002356 iif = kmalloc(sizeof(*iif), GFP_KERNEL);
2357 if (!iif) {
2358 pr_err("%s: out of memory\n", __func__);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002359 return -ENOMEM;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002360 }
2361
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002362 /* prepare controller structure */
2363 iif->ctr.owner = THIS_MODULE;
2364 iif->ctr.driverdata = cs;
2365 strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name));
2366 iif->ctr.driver_name = "gigaset";
Tilman Schmidte4876392010-05-23 01:02:38 +00002367 iif->ctr.load_firmware = NULL;
2368 iif->ctr.reset_ctr = NULL;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002369 iif->ctr.register_appl = gigaset_register_appl;
2370 iif->ctr.release_appl = gigaset_release_appl;
2371 iif->ctr.send_message = gigaset_send_message;
2372 iif->ctr.procinfo = gigaset_procinfo;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002373 iif->ctr.proc_fops = &gigaset_proc_fops;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002374 INIT_LIST_HEAD(&iif->appls);
2375 skb_queue_head_init(&iif->sendqueue);
2376 atomic_set(&iif->sendqlen, 0);
2377
2378 /* register controller with CAPI */
2379 rc = attach_capi_ctr(&iif->ctr);
2380 if (rc) {
2381 pr_err("attach_capi_ctr failed (%d)\n", rc);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002382 kfree(iif);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002383 return rc;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002384 }
2385
2386 cs->iif = iif;
2387 cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00002388 return 0;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002389}
2390
2391/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002392 * gigaset_isdn_unregdev() - unregister device from LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002393 * @cs: device descriptor structure.
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002394 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002395void gigaset_isdn_unregdev(struct cardstate *cs)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002396{
2397 struct gigaset_capi_ctr *iif = cs->iif;
2398
2399 detach_capi_ctr(&iif->ctr);
2400 kfree(iif);
2401 cs->iif = NULL;
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002402}
2403
2404static struct capi_driver capi_driver_gigaset = {
2405 .name = "gigaset",
2406 .revision = "1.0",
2407};
2408
2409/**
2410 * gigaset_isdn_regdrv() - register driver to LL
2411 */
2412void gigaset_isdn_regdrv(void)
2413{
2414 pr_info("Kernel CAPI interface\n");
2415 register_capi_driver(&capi_driver_gigaset);
2416}
2417
2418/**
2419 * gigaset_isdn_unregdrv() - unregister driver from LL
2420 */
2421void gigaset_isdn_unregdrv(void)
2422{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002423 unregister_capi_driver(&capi_driver_gigaset);
2424}