blob: 292ca2fc852c8a3a724b99da273f38b4f82654e1 [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[] = {
112 [1] = { "8090A3", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800113 /* Speech (A-law) */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000114 [2] = { "8890", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800115 /* Unrestricted digital information */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000116 [3] = { "8990", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800117 /* Restricted digital information */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000118 [4] = { "9090A3", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800119 /* 3,1 kHz audio (A-law) */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000120 [5] = { "9190", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800121 /* 7 kHz audio */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000122 [6] = { "9890", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800123 /* Video */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000124 [7] = { "88C0C6E6", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800125 /* Packet mode */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000126 [8] = { "8890218F", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800127 /* 56 kbit/s rate adaptation */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000128 [9] = { "9190A5", NULL },
Joe Perches475be4d2012-02-19 19:52:38 -0800129 /* Unrestricted digital information with tones/announcements */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000130 [16] = { "8090A3", "9181" },
Joe Perches475be4d2012-02-19 19:52:38 -0800131 /* Telephony */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000132 [17] = { "9090A3", "9184" },
Joe Perches475be4d2012-02-19 19:52:38 -0800133 /* Group 2/3 facsimile */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000134 [18] = { "8890", "91A1" },
Joe Perches475be4d2012-02-19 19:52:38 -0800135 /* Group 4 facsimile Class 1 */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000136 [19] = { "8890", "91A4" },
Joe Perches475be4d2012-02-19 19:52:38 -0800137 /* Teletex service basic and mixed mode
138 and Group 4 facsimile service Classes II and III */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000139 [20] = { "8890", "91A8" },
Joe Perches475be4d2012-02-19 19:52:38 -0800140 /* Teletex service basic and processable mode */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000141 [21] = { "8890", "91B1" },
Joe Perches475be4d2012-02-19 19:52:38 -0800142 /* Teletex service basic mode */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000143 [22] = { "8890", "91B2" },
Joe Perches475be4d2012-02-19 19:52:38 -0800144 /* International interworking for Videotex */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000145 [23] = { "8890", "91B5" },
Joe Perches475be4d2012-02-19 19:52:38 -0800146 /* Telex */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000147 [24] = { "8890", "91B8" },
Joe Perches475be4d2012-02-19 19:52:38 -0800148 /* Message Handling Systems in accordance with X.400 */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000149 [25] = { "8890", "91C1" },
Joe Perches475be4d2012-02-19 19:52:38 -0800150 /* OSI application in accordance with X.200 */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000151 [26] = { "9190A5", "9181" },
Joe Perches475be4d2012-02-19 19:52:38 -0800152 /* 7 kHz telephony */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000153 [27] = { "9190A5", "916001" },
Joe Perches475be4d2012-02-19 19:52:38 -0800154 /* Video telephony, first connection */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000155 [28] = { "8890", "916002" },
Joe Perches475be4d2012-02-19 19:52:38 -0800156 /* Video telephony, second connection */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000157};
158
159/*
160 * helper functions
161 * ================
162 */
163
164/*
165 * emit unsupported parameter warning
166 */
167static inline void ignore_cstruct_param(struct cardstate *cs, _cstruct param,
Joe Perches475be4d2012-02-19 19:52:38 -0800168 char *msgname, char *paramname)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000169{
170 if (param && *param)
171 dev_warn(cs->dev, "%s: ignoring unsupported parameter: %s\n",
172 msgname, paramname);
173}
174
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000175/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000176 * convert an IE from Gigaset hex string to ETSI binary representation
177 * including length byte
178 * return value: result length, -1 on error
179 */
180static int encode_ie(char *in, u8 *out, int maxlen)
181{
182 int l = 0;
183 while (*in) {
Andy Shevchenko003bdb22010-02-22 13:10:22 +0000184 if (!isxdigit(in[0]) || !isxdigit(in[1]) || l >= maxlen)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000185 return -1;
Andy Shevchenko0496b552010-05-24 14:33:25 -0700186 out[++l] = (hex_to_bin(in[0]) << 4) + hex_to_bin(in[1]);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000187 in += 2;
188 }
189 out[0] = l;
190 return l;
191}
192
193/*
194 * convert an IE from ETSI binary representation including length byte
195 * to Gigaset hex string
196 */
197static void decode_ie(u8 *in, char *out)
198{
199 int i = *in;
200 while (i-- > 0) {
201 /* ToDo: conversion to upper case necessary? */
202 *out++ = toupper(hex_asc_hi(*++in));
203 *out++ = toupper(hex_asc_lo(*in));
204 }
205}
206
207/*
208 * retrieve application data structure for an application ID
209 */
210static inline struct gigaset_capi_appl *
211get_appl(struct gigaset_capi_ctr *iif, u16 appl)
212{
213 struct gigaset_capi_appl *ap;
214
215 list_for_each_entry(ap, &iif->appls, ctrlist)
216 if (ap->id == appl)
217 return ap;
218 return NULL;
219}
220
221/*
222 * dump CAPI message to kernel messages for debugging
223 */
224static inline void dump_cmsg(enum debuglevel level, const char *tag, _cmsg *p)
225{
226#ifdef CONFIG_GIGASET_DEBUG
Tilman Schmidt8e618aa2012-04-25 13:02:19 +0000227 /* dump at most 20 messages in 20 secs */
228 static DEFINE_RATELIMIT_STATE(msg_dump_ratelimit, 20 * HZ, 20);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000229 _cdebbuf *cdb;
230
231 if (!(gigaset_debuglevel & level))
232 return;
Tilman Schmidt8e618aa2012-04-25 13:02:19 +0000233 if (!___ratelimit(&msg_dump_ratelimit, tag))
234 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000235
236 cdb = capi_cmsg2str(p);
237 if (cdb) {
238 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId, cdb->buf);
239 cdebbuf_free(cdb);
240 } else {
241 gig_dbg(level, "%s: [%d] %s", tag, p->ApplId,
242 capi_cmd2str(p->Command, p->Subcommand));
243 }
244#endif
245}
246
247static inline void dump_rawmsg(enum debuglevel level, const char *tag,
248 unsigned char *data)
249{
250#ifdef CONFIG_GIGASET_DEBUG
251 char *dbgline;
252 int i, l;
253
254 if (!(gigaset_debuglevel & level))
255 return;
256
257 l = CAPIMSG_LEN(data);
258 if (l < 12) {
259 gig_dbg(level, "%s: ??? LEN=%04d", tag, l);
260 return;
261 }
262 gig_dbg(level, "%s: 0x%02x:0x%02x: ID=%03d #0x%04x LEN=%04d NCCI=0x%x",
263 tag, CAPIMSG_COMMAND(data), CAPIMSG_SUBCOMMAND(data),
264 CAPIMSG_APPID(data), CAPIMSG_MSGID(data), l,
265 CAPIMSG_CONTROL(data));
266 l -= 12;
Joe Perches475be4d2012-02-19 19:52:38 -0800267 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000268 if (!dbgline)
269 return;
270 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800271 dbgline[3 * i] = hex_asc_hi(data[12 + i]);
272 dbgline[3 * i + 1] = hex_asc_lo(data[12 + i]);
273 dbgline[3 * i + 2] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000274 }
Joe Perches475be4d2012-02-19 19:52:38 -0800275 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000276 gig_dbg(level, " %s", dbgline);
277 kfree(dbgline);
278 if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
279 (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
Tilman Schmidt6a753422010-07-05 14:18:59 +0000280 CAPIMSG_SUBCOMMAND(data) == CAPI_IND)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000281 l = CAPIMSG_DATALEN(data);
Tilman Schmidt6a753422010-07-05 14:18:59 +0000282 gig_dbg(level, " DataLength=%d", l);
283 if (l <= 0 || !(gigaset_debuglevel & DEBUG_LLDATA))
284 return;
285 if (l > 64)
286 l = 64; /* arbitrary limit */
Joe Perches475be4d2012-02-19 19:52:38 -0800287 dbgline = kmalloc(3 * l, GFP_ATOMIC);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000288 if (!dbgline)
289 return;
290 data += CAPIMSG_LEN(data);
291 for (i = 0; i < l; i++) {
Joe Perches475be4d2012-02-19 19:52:38 -0800292 dbgline[3 * i] = hex_asc_hi(data[i]);
293 dbgline[3 * i + 1] = hex_asc_lo(data[i]);
294 dbgline[3 * i + 2] = ' ';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000295 }
Joe Perches475be4d2012-02-19 19:52:38 -0800296 dbgline[3 * l - 1] = '\0';
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000297 gig_dbg(level, " %s", dbgline);
298 kfree(dbgline);
299 }
300#endif
301}
302
303/*
304 * format CAPI IE as string
305 */
306
307static const char *format_ie(const char *ie)
308{
Joe Perches475be4d2012-02-19 19:52:38 -0800309 static char result[3 * MAX_FMT_IE_LEN];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000310 int len, count;
311 char *pout = result;
312
313 if (!ie)
314 return "NULL";
315
316 count = len = ie[0];
317 if (count > MAX_FMT_IE_LEN)
Joe Perches475be4d2012-02-19 19:52:38 -0800318 count = MAX_FMT_IE_LEN - 1;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000319 while (count--) {
320 *pout++ = hex_asc_hi(*++ie);
321 *pout++ = hex_asc_lo(*ie);
322 *pout++ = ' ';
323 }
324 if (len > MAX_FMT_IE_LEN) {
325 *pout++ = '.';
326 *pout++ = '.';
327 *pout++ = '.';
328 }
329 *--pout = 0;
330 return result;
331}
332
Tilman Schmidt23b36772010-06-21 13:54:50 +0000333/*
334 * emit DATA_B3_CONF message
335 */
336static void send_data_b3_conf(struct cardstate *cs, struct capi_ctr *ctr,
337 u16 appl, u16 msgid, int channel,
338 u16 handle, u16 info)
339{
340 struct sk_buff *cskb;
341 u8 *msg;
342
343 cskb = alloc_skb(CAPI_DATA_B3_CONF_LEN, GFP_ATOMIC);
344 if (!cskb) {
345 dev_err(cs->dev, "%s: out of memory\n", __func__);
346 return;
347 }
348 /* frequent message, avoid _cmsg overhead */
349 msg = __skb_put(cskb, CAPI_DATA_B3_CONF_LEN);
350 CAPIMSG_SETLEN(msg, CAPI_DATA_B3_CONF_LEN);
351 CAPIMSG_SETAPPID(msg, appl);
352 CAPIMSG_SETCOMMAND(msg, CAPI_DATA_B3);
353 CAPIMSG_SETSUBCOMMAND(msg, CAPI_CONF);
354 CAPIMSG_SETMSGID(msg, msgid);
355 CAPIMSG_SETCONTROLLER(msg, ctr->cnr);
356 CAPIMSG_SETPLCI_PART(msg, channel);
357 CAPIMSG_SETNCCI_PART(msg, 1);
358 CAPIMSG_SETHANDLE_CONF(msg, handle);
359 CAPIMSG_SETINFO_CONF(msg, info);
360
361 /* emit message */
362 dump_rawmsg(DEBUG_MCMD, __func__, msg);
363 capi_ctr_handle_message(ctr, appl, cskb);
364}
365
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000366
367/*
368 * driver interface functions
369 * ==========================
370 */
371
372/**
373 * gigaset_skb_sent() - acknowledge transmission of outgoing skb
374 * @bcs: B channel descriptor structure.
375 * @skb: sent data.
376 *
377 * Called by hardware module {bas,ser,usb}_gigaset when the data in a
378 * skb has been successfully sent, for signalling completion to the LL.
379 */
380void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *dskb)
381{
382 struct cardstate *cs = bcs->cs;
383 struct gigaset_capi_ctr *iif = cs->iif;
384 struct gigaset_capi_appl *ap = bcs->ap;
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000385 unsigned char *req = skb_mac_header(dskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000386 u16 flags;
387
388 /* update statistics */
389 ++bcs->trans_up;
390
391 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000392 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000393 return;
394 }
395
396 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000397 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000398 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000399 return;
400 }
401
Tilman Schmidt23b36772010-06-21 13:54:50 +0000402 /*
403 * send DATA_B3_CONF if "delivery confirmation" bit was set in request;
404 * otherwise it has already been sent by do_data_b3_req()
405 */
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000406 flags = CAPIMSG_FLAGS(req);
Tilman Schmidt23b36772010-06-21 13:54:50 +0000407 if (flags & CAPI_FLAGS_DELIVERY_CONFIRMATION)
408 send_data_b3_conf(cs, &iif->ctr, ap->id, CAPIMSG_MSGID(req),
409 bcs->channel + 1, CAPIMSG_HANDLE_REQ(req),
410 (flags & ~CAPI_FLAGS_DELIVERY_CONFIRMATION) ?
Joe Perches475be4d2012-02-19 19:52:38 -0800411 CapiFlagsNotSupportedByProtocol :
412 CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000413}
414EXPORT_SYMBOL_GPL(gigaset_skb_sent);
415
416/**
417 * gigaset_skb_rcvd() - pass received skb to LL
418 * @bcs: B channel descriptor structure.
419 * @skb: received data.
420 *
421 * Called by hardware module {bas,ser,usb}_gigaset when user data has
422 * been successfully received, for passing to the LL.
423 * Warning: skb must not be accessed anymore!
424 */
425void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
426{
427 struct cardstate *cs = bcs->cs;
428 struct gigaset_capi_ctr *iif = cs->iif;
429 struct gigaset_capi_appl *ap = bcs->ap;
430 int len = skb->len;
431
432 /* update statistics */
433 bcs->trans_down++;
434
435 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000436 gig_dbg(DEBUG_MCMD, "%s: application gone", __func__);
437 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000438 return;
439 }
440
441 /* don't send further B3 messages if disconnected */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000442 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000443 gig_dbg(DEBUG_MCMD, "%s: disconnected", __func__);
Tilman Schmidt4dd82302009-10-25 09:29:57 +0000444 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000445 return;
446 }
447
448 /*
449 * prepend DATA_B3_IND message to payload
450 * Parameters: NCCI = 1, all others 0/unused
451 * frequent message, avoid _cmsg overhead
452 */
453 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
454 CAPIMSG_SETLEN(skb->data, CAPI_DATA_B3_REQ_LEN);
455 CAPIMSG_SETAPPID(skb->data, ap->id);
456 CAPIMSG_SETCOMMAND(skb->data, CAPI_DATA_B3);
457 CAPIMSG_SETSUBCOMMAND(skb->data, CAPI_IND);
458 CAPIMSG_SETMSGID(skb->data, ap->nextMessageNumber++);
459 CAPIMSG_SETCONTROLLER(skb->data, iif->ctr.cnr);
460 CAPIMSG_SETPLCI_PART(skb->data, bcs->channel + 1);
461 CAPIMSG_SETNCCI_PART(skb->data, 1);
462 /* Data parameter not used */
463 CAPIMSG_SETDATALEN(skb->data, len);
464 /* Data handle parameter not used */
465 CAPIMSG_SETFLAGS(skb->data, 0);
466 /* Data64 parameter not present */
467
468 /* emit message */
Tilman Schmidt6a753422010-07-05 14:18:59 +0000469 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000470 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
471}
472EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
473
474/**
475 * gigaset_isdn_rcv_err() - signal receive error
476 * @bcs: B channel descriptor structure.
477 *
478 * Called by hardware module {bas,ser,usb}_gigaset when a receive error
479 * has occurred, for signalling to the LL.
480 */
481void gigaset_isdn_rcv_err(struct bc_state *bcs)
482{
483 /* if currently ignoring packets, just count down */
484 if (bcs->ignore) {
485 bcs->ignore--;
486 return;
487 }
488
489 /* update statistics */
490 bcs->corrupted++;
491
492 /* ToDo: signal error -> LL */
493}
494EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
495
496/**
497 * gigaset_isdn_icall() - signal incoming call
498 * @at_state: connection state structure.
499 *
500 * Called by main module at tasklet level to notify the LL that an incoming
501 * call has been received. @at_state contains the parameters of the call.
502 *
503 * Return value: call disposition (ICALL_*)
504 */
505int gigaset_isdn_icall(struct at_state_t *at_state)
506{
507 struct cardstate *cs = at_state->cs;
508 struct bc_state *bcs = at_state->bcs;
509 struct gigaset_capi_ctr *iif = cs->iif;
510 struct gigaset_capi_appl *ap;
511 u32 actCIPmask;
512 struct sk_buff *skb;
513 unsigned int msgsize;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000514 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000515 int i;
516
517 /*
518 * ToDo: signal calls without a free B channel, too
519 * (requires a u8 handle for the at_state structure that can
520 * be stored in the PLCI and used in the CONNECT_RESP message
521 * handler to retrieve it)
522 */
523 if (!bcs)
524 return ICALL_IGNORE;
525
526 /* prepare CONNECT_IND message, using B channel number as PLCI */
527 capi_cmsg_header(&iif->hcmsg, 0, CAPI_CONNECT, CAPI_IND, 0,
528 iif->ctr.cnr | ((bcs->channel + 1) << 8));
529
530 /* minimum size, all structs empty */
531 msgsize = CAPI_CONNECT_IND_BASELEN;
532
533 /* Bearer Capability (mandatory) */
534 if (at_state->str_var[STR_ZBC]) {
535 /* pass on BC from Gigaset */
536 if (encode_ie(at_state->str_var[STR_ZBC], iif->bc_buf,
537 MAX_BC_OCTETS) < 0) {
538 dev_warn(cs->dev, "RING ignored - bad BC %s\n",
539 at_state->str_var[STR_ZBC]);
540 return ICALL_IGNORE;
541 }
542
543 /* look up corresponding CIP value */
544 iif->hcmsg.CIPValue = 0; /* default if nothing found */
545 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
546 if (cip2bchlc[i].bc != NULL &&
547 cip2bchlc[i].hlc == NULL &&
548 !strcmp(cip2bchlc[i].bc,
549 at_state->str_var[STR_ZBC])) {
550 iif->hcmsg.CIPValue = i;
551 break;
552 }
553 } else {
554 /* no BC (internal call): assume CIP 1 (speech, A-law) */
555 iif->hcmsg.CIPValue = 1;
556 encode_ie(cip2bchlc[1].bc, iif->bc_buf, MAX_BC_OCTETS);
557 }
558 iif->hcmsg.BC = iif->bc_buf;
559 msgsize += iif->hcmsg.BC[0];
560
561 /* High Layer Compatibility (optional) */
562 if (at_state->str_var[STR_ZHLC]) {
563 /* pass on HLC from Gigaset */
564 if (encode_ie(at_state->str_var[STR_ZHLC], iif->hlc_buf,
565 MAX_HLC_OCTETS) < 0) {
566 dev_warn(cs->dev, "RING ignored - bad HLC %s\n",
567 at_state->str_var[STR_ZHLC]);
568 return ICALL_IGNORE;
569 }
570 iif->hcmsg.HLC = iif->hlc_buf;
571 msgsize += iif->hcmsg.HLC[0];
572
573 /* look up corresponding CIP value */
574 /* keep BC based CIP value if none found */
575 if (at_state->str_var[STR_ZBC])
576 for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
577 if (cip2bchlc[i].hlc != NULL &&
578 !strcmp(cip2bchlc[i].hlc,
579 at_state->str_var[STR_ZHLC]) &&
580 !strcmp(cip2bchlc[i].bc,
581 at_state->str_var[STR_ZBC])) {
582 iif->hcmsg.CIPValue = i;
583 break;
584 }
585 }
586
587 /* Called Party Number (optional) */
588 if (at_state->str_var[STR_ZCPN]) {
589 i = strlen(at_state->str_var[STR_ZCPN]);
590 if (i > MAX_NUMBER_DIGITS) {
591 dev_warn(cs->dev, "RING ignored - bad number %s\n",
592 at_state->str_var[STR_ZBC]);
593 return ICALL_IGNORE;
594 }
595 iif->cdpty_buf[0] = i + 1;
596 iif->cdpty_buf[1] = 0x80; /* type / numbering plan unknown */
Joe Perches475be4d2012-02-19 19:52:38 -0800597 memcpy(iif->cdpty_buf + 2, at_state->str_var[STR_ZCPN], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000598 iif->hcmsg.CalledPartyNumber = iif->cdpty_buf;
599 msgsize += iif->hcmsg.CalledPartyNumber[0];
600 }
601
602 /* Calling Party Number (optional) */
603 if (at_state->str_var[STR_NMBR]) {
604 i = strlen(at_state->str_var[STR_NMBR]);
605 if (i > MAX_NUMBER_DIGITS) {
606 dev_warn(cs->dev, "RING ignored - bad number %s\n",
607 at_state->str_var[STR_ZBC]);
608 return ICALL_IGNORE;
609 }
610 iif->cgpty_buf[0] = i + 2;
611 iif->cgpty_buf[1] = 0x00; /* type / numbering plan unknown */
612 iif->cgpty_buf[2] = 0x80; /* pres. allowed, not screened */
Joe Perches475be4d2012-02-19 19:52:38 -0800613 memcpy(iif->cgpty_buf + 3, at_state->str_var[STR_NMBR], i);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000614 iif->hcmsg.CallingPartyNumber = iif->cgpty_buf;
615 msgsize += iif->hcmsg.CallingPartyNumber[0];
616 }
617
618 /* remaining parameters (not supported, always left NULL):
619 * - CalledPartySubaddress
620 * - CallingPartySubaddress
621 * - AdditionalInfo
622 * - BChannelinformation
623 * - Keypadfacility
624 * - Useruserdata
625 * - Facilitydataarray
626 */
627
628 gig_dbg(DEBUG_CMD, "icall: PLCI %x CIP %d BC %s",
629 iif->hcmsg.adr.adrPLCI, iif->hcmsg.CIPValue,
630 format_ie(iif->hcmsg.BC));
631 gig_dbg(DEBUG_CMD, "icall: HLC %s",
632 format_ie(iif->hcmsg.HLC));
633 gig_dbg(DEBUG_CMD, "icall: CgPty %s",
634 format_ie(iif->hcmsg.CallingPartyNumber));
635 gig_dbg(DEBUG_CMD, "icall: CdPty %s",
636 format_ie(iif->hcmsg.CalledPartyNumber));
637
638 /* scan application list for matching listeners */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000639 spin_lock_irqsave(&bcs->aplock, flags);
640 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE) {
641 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
642 __func__, bcs->ap, bcs->apconnstate);
643 bcs->ap = NULL;
644 bcs->apconnstate = APCONN_NONE;
645 }
646 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000647 actCIPmask = 1 | (1 << iif->hcmsg.CIPValue);
648 list_for_each_entry(ap, &iif->appls, ctrlist)
649 if (actCIPmask & ap->listenCIPmask) {
650 /* build CONNECT_IND message for this application */
651 iif->hcmsg.ApplId = ap->id;
652 iif->hcmsg.Messagenumber = ap->nextMessageNumber++;
653
654 skb = alloc_skb(msgsize, GFP_ATOMIC);
655 if (!skb) {
656 dev_err(cs->dev, "%s: out of memory\n",
657 __func__);
658 break;
659 }
660 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
661 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
662
663 /* add to listeners on this B channel, update state */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000664 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000665 ap->bcnext = bcs->ap;
666 bcs->ap = ap;
667 bcs->chstate |= CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000668 bcs->apconnstate = APCONN_SETUP;
669 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000670
671 /* emit message */
672 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
673 }
674
675 /*
676 * Return "accept" if any listeners.
677 * Gigaset will send ALERTING.
678 * There doesn't seem to be a way to avoid this.
679 */
680 return bcs->ap ? ICALL_ACCEPT : ICALL_IGNORE;
681}
682
683/*
684 * send a DISCONNECT_IND message to an application
685 * does not sleep, clobbers the controller's hcmsg structure
686 */
687static void send_disconnect_ind(struct bc_state *bcs,
688 struct gigaset_capi_appl *ap, u16 reason)
689{
690 struct cardstate *cs = bcs->cs;
691 struct gigaset_capi_ctr *iif = cs->iif;
692 struct sk_buff *skb;
693
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000694 if (bcs->apconnstate == APCONN_NONE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000695 return;
696
697 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT, CAPI_IND,
698 ap->nextMessageNumber++,
699 iif->ctr.cnr | ((bcs->channel + 1) << 8));
700 iif->hcmsg.Reason = reason;
701 skb = alloc_skb(CAPI_DISCONNECT_IND_LEN, GFP_ATOMIC);
702 if (!skb) {
703 dev_err(cs->dev, "%s: out of memory\n", __func__);
704 return;
705 }
706 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, CAPI_DISCONNECT_IND_LEN));
707 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000708 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
709}
710
711/*
712 * send a DISCONNECT_B3_IND message to an application
713 * Parameters: NCCI = 1, NCPI empty, Reason_B3 = 0
714 * does not sleep, clobbers the controller's hcmsg structure
715 */
716static void send_disconnect_b3_ind(struct bc_state *bcs,
717 struct gigaset_capi_appl *ap)
718{
719 struct cardstate *cs = bcs->cs;
720 struct gigaset_capi_ctr *iif = cs->iif;
721 struct sk_buff *skb;
722
723 /* nothing to do if no logical connection active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000724 if (bcs->apconnstate < APCONN_ACTIVE)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000725 return;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000726 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000727
728 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
729 ap->nextMessageNumber++,
730 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
731 skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_ATOMIC);
732 if (!skb) {
733 dev_err(cs->dev, "%s: out of memory\n", __func__);
734 return;
735 }
736 capi_cmsg2message(&iif->hcmsg,
737 __skb_put(skb, CAPI_DISCONNECT_B3_IND_BASELEN));
738 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
739 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
740}
741
742/**
743 * gigaset_isdn_connD() - signal D channel connect
744 * @bcs: B channel descriptor structure.
745 *
746 * Called by main module at tasklet level to notify the LL that the D channel
747 * connection has been established.
748 */
749void gigaset_isdn_connD(struct bc_state *bcs)
750{
751 struct cardstate *cs = bcs->cs;
752 struct gigaset_capi_ctr *iif = cs->iif;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000753 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000754 struct sk_buff *skb;
755 unsigned int msgsize;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000756 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000757
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000758 spin_lock_irqsave(&bcs->aplock, flags);
759 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000760 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000761 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000762 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000763 return;
764 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000765 if (bcs->apconnstate == APCONN_NONE) {
766 spin_unlock_irqrestore(&bcs->aplock, flags);
767 dev_warn(cs->dev, "%s: application %u not connected\n",
768 __func__, ap->id);
769 return;
770 }
771 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000772 while (ap->bcnext) {
773 /* this should never happen */
774 dev_warn(cs->dev, "%s: dropping extra application %u\n",
775 __func__, ap->bcnext->id);
776 send_disconnect_ind(bcs, ap->bcnext,
777 CapiCallGivenToOtherApplication);
778 ap->bcnext = ap->bcnext->bcnext;
779 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000780
781 /* prepare CONNECT_ACTIVE_IND message
782 * Note: LLC not supported by device
783 */
784 capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_CONNECT_ACTIVE, CAPI_IND,
785 ap->nextMessageNumber++,
786 iif->ctr.cnr | ((bcs->channel + 1) << 8));
787
788 /* minimum size, all structs empty */
789 msgsize = CAPI_CONNECT_ACTIVE_IND_BASELEN;
790
791 /* ToDo: set parameter: Connected number
792 * (requires ev-layer state machine extension to collect
793 * ZCON device reply)
794 */
795
796 /* build and emit CONNECT_ACTIVE_IND message */
797 skb = alloc_skb(msgsize, GFP_ATOMIC);
798 if (!skb) {
799 dev_err(cs->dev, "%s: out of memory\n", __func__);
800 return;
801 }
802 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
803 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
804 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
805}
806
807/**
808 * gigaset_isdn_hupD() - signal D channel hangup
809 * @bcs: B channel descriptor structure.
810 *
811 * Called by main module at tasklet level to notify the LL that the D channel
812 * connection has been shut down.
813 */
814void gigaset_isdn_hupD(struct bc_state *bcs)
815{
816 struct gigaset_capi_appl *ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000817 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000818
819 /*
820 * ToDo: pass on reason code reported by device
821 * (requires ev-layer state machine extension to collect
822 * ZCAU device reply)
823 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000824 spin_lock_irqsave(&bcs->aplock, flags);
825 while (bcs->ap != NULL) {
826 ap = bcs->ap;
827 bcs->ap = ap->bcnext;
828 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000829 send_disconnect_b3_ind(bcs, ap);
830 send_disconnect_ind(bcs, ap, 0);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000831 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000832 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000833 bcs->apconnstate = APCONN_NONE;
834 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000835}
836
837/**
838 * gigaset_isdn_connB() - signal B channel connect
839 * @bcs: B channel descriptor structure.
840 *
841 * Called by main module at tasklet level to notify the LL that the B channel
842 * connection has been established.
843 */
844void gigaset_isdn_connB(struct bc_state *bcs)
845{
846 struct cardstate *cs = bcs->cs;
847 struct gigaset_capi_ctr *iif = cs->iif;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000848 struct gigaset_capi_appl *ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000849 struct sk_buff *skb;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000850 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000851 unsigned int msgsize;
852 u8 command;
853
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000854 spin_lock_irqsave(&bcs->aplock, flags);
855 ap = bcs->ap;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000856 if (!ap) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000857 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000858 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000859 return;
860 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000861 if (!bcs->apconnstate) {
862 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000863 dev_warn(cs->dev, "%s: application %u not connected\n",
864 __func__, ap->id);
865 return;
866 }
867
868 /*
869 * emit CONNECT_B3_ACTIVE_IND if we already got CONNECT_B3_REQ;
870 * otherwise we have to emit CONNECT_B3_IND first, and follow up with
871 * CONNECT_B3_ACTIVE_IND in reply to CONNECT_B3_RESP
872 * Parameters in both cases always: NCCI = 1, NCPI empty
873 */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000874 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000875 command = CAPI_CONNECT_B3_ACTIVE;
876 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
877 } else {
878 command = CAPI_CONNECT_B3;
879 msgsize = CAPI_CONNECT_B3_IND_BASELEN;
880 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +0000881 bcs->apconnstate = APCONN_ACTIVE;
882
883 spin_unlock_irqrestore(&bcs->aplock, flags);
884
885 while (ap->bcnext) {
886 /* this should never happen */
887 dev_warn(cs->dev, "%s: dropping extra application %u\n",
888 __func__, ap->bcnext->id);
889 send_disconnect_ind(bcs, ap->bcnext,
890 CapiCallGivenToOtherApplication);
891 ap->bcnext = ap->bcnext->bcnext;
892 }
893
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000894 capi_cmsg_header(&iif->hcmsg, ap->id, command, CAPI_IND,
895 ap->nextMessageNumber++,
896 iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
897 skb = alloc_skb(msgsize, GFP_ATOMIC);
898 if (!skb) {
899 dev_err(cs->dev, "%s: out of memory\n", __func__);
900 return;
901 }
902 capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
903 dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000904 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
905}
906
907/**
908 * gigaset_isdn_hupB() - signal B channel hangup
909 * @bcs: B channel descriptor structure.
910 *
911 * Called by main module to notify the LL that the B channel connection has
912 * been shut down.
913 */
914void gigaset_isdn_hupB(struct bc_state *bcs)
915{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000916 struct gigaset_capi_appl *ap = bcs->ap;
917
918 /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
919
920 if (!ap) {
Tilman Schmidt7d060ed2010-07-05 14:19:14 +0000921 gig_dbg(DEBUG_CMD, "%s: application gone", __func__);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000922 return;
923 }
924
925 send_disconnect_b3_ind(bcs, ap);
926}
927
928/**
929 * gigaset_isdn_start() - signal device availability
930 * @cs: device descriptor structure.
931 *
932 * Called by main module to notify the LL that the device is available for
933 * use.
934 */
935void gigaset_isdn_start(struct cardstate *cs)
936{
937 struct gigaset_capi_ctr *iif = cs->iif;
938
939 /* fill profile data: manufacturer name */
940 strcpy(iif->ctr.manu, "Siemens");
941 /* CAPI and device version */
942 iif->ctr.version.majorversion = 2; /* CAPI 2.0 */
943 iif->ctr.version.minorversion = 0;
944 /* ToDo: check/assert cs->gotfwver? */
945 iif->ctr.version.majormanuversion = cs->fwver[0];
946 iif->ctr.version.minormanuversion = cs->fwver[1];
947 /* number of B channels supported */
948 iif->ctr.profile.nbchannel = cs->channels;
949 /* global options: internal controller, supplementary services */
950 iif->ctr.profile.goptions = 0x11;
951 /* B1 protocols: 64 kbit/s HDLC or transparent */
952 iif->ctr.profile.support1 = 0x03;
953 /* B2 protocols: transparent only */
954 /* ToDo: X.75 SLP ? */
955 iif->ctr.profile.support2 = 0x02;
956 /* B3 protocols: transparent only */
957 iif->ctr.profile.support3 = 0x01;
958 /* no serial number */
959 strcpy(iif->ctr.serial, "0");
960 capi_ctr_ready(&iif->ctr);
961}
962
963/**
964 * gigaset_isdn_stop() - signal device unavailability
965 * @cs: device descriptor structure.
966 *
967 * Called by main module to notify the LL that the device is no longer
968 * available for use.
969 */
970void gigaset_isdn_stop(struct cardstate *cs)
971{
972 struct gigaset_capi_ctr *iif = cs->iif;
973 capi_ctr_down(&iif->ctr);
974}
975
976/*
977 * kernel CAPI callback methods
978 * ============================
979 */
980
981/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000982 * register CAPI application
983 */
984static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
Joe Perches475be4d2012-02-19 19:52:38 -0800985 capi_register_params *rp)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000986{
987 struct gigaset_capi_ctr *iif
988 = container_of(ctr, struct gigaset_capi_ctr, ctr);
989 struct cardstate *cs = ctr->driverdata;
990 struct gigaset_capi_appl *ap;
991
Tilman Schmidt6a753422010-07-05 14:18:59 +0000992 gig_dbg(DEBUG_CMD, "%s [%u] l3cnt=%u blkcnt=%u blklen=%u",
993 __func__, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
994
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +0000995 list_for_each_entry(ap, &iif->appls, ctrlist)
996 if (ap->id == appl) {
997 dev_notice(cs->dev,
998 "application %u already registered\n", appl);
999 return;
1000 }
1001
1002 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
1003 if (!ap) {
1004 dev_err(cs->dev, "%s: out of memory\n", __func__);
1005 return;
1006 }
1007 ap->id = appl;
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001008 ap->rp = *rp;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001009
1010 list_add(&ap->ctrlist, &iif->appls);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001011 dev_info(cs->dev, "application %u registered\n", ap->id);
1012}
1013
1014/*
1015 * remove CAPI application from channel
1016 * helper function to keep indentation levels down and stay in 80 columns
1017 */
1018
1019static inline void remove_appl_from_channel(struct bc_state *bcs,
1020 struct gigaset_capi_appl *ap)
1021{
1022 struct cardstate *cs = bcs->cs;
1023 struct gigaset_capi_appl *bcap;
1024 unsigned long flags;
1025 int prevconnstate;
1026
1027 spin_lock_irqsave(&bcs->aplock, flags);
1028 bcap = bcs->ap;
1029 if (bcap == NULL) {
1030 spin_unlock_irqrestore(&bcs->aplock, flags);
1031 return;
1032 }
1033
1034 /* check first application on channel */
1035 if (bcap == ap) {
1036 bcs->ap = ap->bcnext;
1037 if (bcs->ap != NULL) {
1038 spin_unlock_irqrestore(&bcs->aplock, flags);
1039 return;
1040 }
1041
1042 /* none left, clear channel state */
1043 prevconnstate = bcs->apconnstate;
1044 bcs->apconnstate = APCONN_NONE;
1045 spin_unlock_irqrestore(&bcs->aplock, flags);
1046
1047 if (prevconnstate == APCONN_ACTIVE) {
1048 dev_notice(cs->dev, "%s: hanging up channel %u\n",
1049 __func__, bcs->channel);
1050 gigaset_add_event(cs, &bcs->at_state,
1051 EV_HUP, NULL, 0, NULL);
1052 gigaset_schedule_event(cs);
1053 }
1054 return;
1055 }
1056
1057 /* check remaining list */
1058 do {
1059 if (bcap->bcnext == ap) {
1060 bcap->bcnext = bcap->bcnext->bcnext;
Dan Carpenter7e27a0a2010-08-05 22:23:23 +00001061 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001062 return;
1063 }
1064 bcap = bcap->bcnext;
1065 } while (bcap != NULL);
1066 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001067}
1068
1069/*
1070 * release CAPI application
1071 */
1072static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
1073{
1074 struct gigaset_capi_ctr *iif
1075 = container_of(ctr, struct gigaset_capi_ctr, ctr);
1076 struct cardstate *cs = iif->ctr.driverdata;
1077 struct gigaset_capi_appl *ap, *tmp;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001078 unsigned ch;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001079
Tilman Schmidt6a753422010-07-05 14:18:59 +00001080 gig_dbg(DEBUG_CMD, "%s [%u]", __func__, appl);
1081
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001082 list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
1083 if (ap->id == appl) {
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001084 /* remove from any channels */
1085 for (ch = 0; ch < cs->channels; ch++)
1086 remove_appl_from_channel(&cs->bcs[ch], ap);
1087
1088 /* remove from registration list */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001089 list_del(&ap->ctrlist);
1090 kfree(ap);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001091 dev_info(cs->dev, "application %u released\n", appl);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001092 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001093}
1094
1095/*
1096 * =====================================================================
1097 * outgoing CAPI message handler
1098 * =====================================================================
1099 */
1100
1101/*
1102 * helper function: emit reply message with given Info value
1103 */
1104static void send_conf(struct gigaset_capi_ctr *iif,
1105 struct gigaset_capi_appl *ap,
1106 struct sk_buff *skb,
1107 u16 info)
1108{
1109 /*
1110 * _CONF replies always only have NCCI and Info parameters
1111 * so they'll fit into the _REQ message skb
1112 */
1113 capi_cmsg_answer(&iif->acmsg);
1114 iif->acmsg.Info = info;
1115 capi_cmsg2message(&iif->acmsg, skb->data);
1116 __skb_trim(skb, CAPI_STDCONF_LEN);
1117 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1118 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1119}
1120
1121/*
1122 * process FACILITY_REQ message
1123 */
1124static void do_facility_req(struct gigaset_capi_ctr *iif,
1125 struct gigaset_capi_appl *ap,
1126 struct sk_buff *skb)
1127{
1128 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001129 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001130 struct sk_buff *cskb;
1131 u8 *pparam;
1132 unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
1133 u16 function, info;
1134 static u8 confparam[10]; /* max. 9 octets + length byte */
1135
1136 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001137 capi_message2cmsg(cmsg, skb->data);
1138 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001139
1140 /*
1141 * Facility Request Parameter is not decoded by capi_message2cmsg()
1142 * encoding depends on Facility Selector
1143 */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001144 switch (cmsg->FacilitySelector) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001145 case CAPI_FACILITY_DTMF: /* ToDo */
1146 info = CapiFacilityNotSupported;
1147 confparam[0] = 2; /* length */
1148 /* DTMF information: Unknown DTMF request */
1149 capimsg_setu16(confparam, 1, 2);
1150 break;
1151
1152 case CAPI_FACILITY_V42BIS: /* not supported */
1153 info = CapiFacilityNotSupported;
1154 confparam[0] = 2; /* length */
1155 /* V.42 bis information: not available */
1156 capimsg_setu16(confparam, 1, 1);
1157 break;
1158
1159 case CAPI_FACILITY_SUPPSVC:
1160 /* decode Function parameter */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001161 pparam = cmsg->FacilityRequestParameter;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001162 if (pparam == NULL || pparam[0] < 2) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001163 dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
1164 "Facility Request Parameter");
1165 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
1166 return;
1167 }
1168 function = CAPIMSG_U16(pparam, 1);
1169 switch (function) {
1170 case CAPI_SUPPSVC_GETSUPPORTED:
1171 info = CapiSuccess;
1172 /* Supplementary Service specific parameter */
1173 confparam[3] = 6; /* length */
1174 /* Supplementary services info: Success */
1175 capimsg_setu16(confparam, 4, CapiSuccess);
1176 /* Supported Services: none */
1177 capimsg_setu32(confparam, 6, 0);
1178 break;
Tilman Schmidt18c22592010-07-05 14:19:04 +00001179 case CAPI_SUPPSVC_LISTEN:
1180 if (pparam[0] < 7 || pparam[3] < 4) {
1181 dev_notice(cs->dev, "%s: %s missing\n",
1182 "FACILITY_REQ", "Notification Mask");
1183 send_conf(iif, ap, skb,
1184 CapiIllMessageParmCoding);
1185 return;
1186 }
1187 if (CAPIMSG_U32(pparam, 4) != 0) {
1188 dev_notice(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001189 "%s: unsupported supplementary service notification mask 0x%x\n",
1190 "FACILITY_REQ", CAPIMSG_U32(pparam, 4));
Tilman Schmidt18c22592010-07-05 14:19:04 +00001191 info = CapiFacilitySpecificFunctionNotSupported;
1192 confparam[3] = 2; /* length */
1193 capimsg_setu16(confparam, 4,
Joe Perches475be4d2012-02-19 19:52:38 -08001194 CapiSupplementaryServiceNotSupported);
Tilman Schmidt18c22592010-07-05 14:19:04 +00001195 }
1196 info = CapiSuccess;
1197 confparam[3] = 2; /* length */
1198 capimsg_setu16(confparam, 4, CapiSuccess);
1199 break;
Joe Perches475be4d2012-02-19 19:52:38 -08001200 /* ToDo: add supported services */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001201 default:
Tilman Schmidt18c22592010-07-05 14:19:04 +00001202 dev_notice(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001203 "%s: unsupported supplementary service function 0x%04x\n",
Tilman Schmidt18c22592010-07-05 14:19:04 +00001204 "FACILITY_REQ", function);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001205 info = CapiFacilitySpecificFunctionNotSupported;
1206 /* Supplementary Service specific parameter */
1207 confparam[3] = 2; /* length */
1208 /* Supplementary services info: not supported */
1209 capimsg_setu16(confparam, 4,
1210 CapiSupplementaryServiceNotSupported);
1211 }
1212
1213 /* Facility confirmation parameter */
1214 confparam[0] = confparam[3] + 3; /* total length */
1215 /* Function: copy from _REQ message */
1216 capimsg_setu16(confparam, 1, function);
1217 /* Supplementary Service specific parameter already set above */
1218 break;
1219
1220 case CAPI_FACILITY_WAKEUP: /* ToDo */
1221 info = CapiFacilityNotSupported;
1222 confparam[0] = 2; /* length */
1223 /* Number of accepted awake request parameters: 0 */
1224 capimsg_setu16(confparam, 1, 0);
1225 break;
1226
1227 default:
1228 info = CapiFacilityNotSupported;
1229 confparam[0] = 0; /* empty struct */
1230 }
1231
1232 /* send FACILITY_CONF with given Info and confirmation parameter */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001233 capi_cmsg_answer(cmsg);
1234 cmsg->Info = info;
1235 cmsg->FacilityConfirmationParameter = confparam;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001236 msgsize += confparam[0]; /* length */
1237 cskb = alloc_skb(msgsize, GFP_ATOMIC);
1238 if (!cskb) {
1239 dev_err(cs->dev, "%s: out of memory\n", __func__);
1240 return;
1241 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001242 capi_cmsg2message(cmsg, __skb_put(cskb, msgsize));
1243 dump_cmsg(DEBUG_CMD, __func__, cmsg);
1244 capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001245}
1246
1247
1248/*
1249 * process LISTEN_REQ message
1250 * just store the masks in the application data structure
1251 */
1252static void do_listen_req(struct gigaset_capi_ctr *iif,
1253 struct gigaset_capi_appl *ap,
1254 struct sk_buff *skb)
1255{
1256 /* decode message */
1257 capi_message2cmsg(&iif->acmsg, skb->data);
1258 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1259
1260 /* store listening parameters */
1261 ap->listenInfoMask = iif->acmsg.InfoMask;
1262 ap->listenCIPmask = iif->acmsg.CIPmask;
1263 send_conf(iif, ap, skb, CapiSuccess);
1264}
1265
1266/*
1267 * process ALERT_REQ message
1268 * nothing to do, Gigaset always alerts anyway
1269 */
1270static void do_alert_req(struct gigaset_capi_ctr *iif,
1271 struct gigaset_capi_appl *ap,
1272 struct sk_buff *skb)
1273{
1274 /* decode message */
1275 capi_message2cmsg(&iif->acmsg, skb->data);
1276 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
1277 send_conf(iif, ap, skb, CapiAlertAlreadySent);
1278}
1279
1280/*
1281 * process CONNECT_REQ message
1282 * allocate a B channel, prepare dial commands, queue a DIAL event,
1283 * emit CONNECT_CONF reply
1284 */
1285static void do_connect_req(struct gigaset_capi_ctr *iif,
1286 struct gigaset_capi_appl *ap,
1287 struct sk_buff *skb)
1288{
1289 struct cardstate *cs = iif->ctr.driverdata;
1290 _cmsg *cmsg = &iif->acmsg;
1291 struct bc_state *bcs;
1292 char **commands;
1293 char *s;
1294 u8 *pp;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001295 unsigned long flags;
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001296 int i, l, lbc, lhlc;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001297 u16 info;
1298
1299 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001300 capi_message2cmsg(cmsg, skb->data);
1301 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001302
1303 /* get free B channel & construct PLCI */
1304 bcs = gigaset_get_free_channel(cs);
1305 if (!bcs) {
1306 dev_notice(cs->dev, "%s: no B channel available\n",
1307 "CONNECT_REQ");
1308 send_conf(iif, ap, skb, CapiNoPlciAvailable);
1309 return;
1310 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001311 spin_lock_irqsave(&bcs->aplock, flags);
1312 if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE)
1313 dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
1314 __func__, bcs->ap, bcs->apconnstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001315 ap->bcnext = NULL;
1316 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001317 bcs->apconnstate = APCONN_SETUP;
1318 spin_unlock_irqrestore(&bcs->aplock, flags);
1319
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001320 bcs->rx_bufsize = ap->rp.datablklen;
1321 dev_kfree_skb(bcs->rx_skb);
1322 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001323 cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
1324
1325 /* build command table */
Joe Perches475be4d2012-02-19 19:52:38 -08001326 commands = kzalloc(AT_NUM * (sizeof *commands), GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001327 if (!commands)
1328 goto oom;
1329
1330 /* encode parameter: Called party number */
1331 pp = cmsg->CalledPartyNumber;
1332 if (pp == NULL || *pp == 0) {
1333 dev_notice(cs->dev, "%s: %s missing\n",
1334 "CONNECT_REQ", "Called party number");
1335 info = CapiIllMessageParmCoding;
1336 goto error;
1337 }
1338 l = *pp++;
1339 /* check type of number/numbering plan byte */
1340 switch (*pp) {
1341 case 0x80: /* unknown type / unknown numbering plan */
1342 case 0x81: /* unknown type / ISDN/Telephony numbering plan */
1343 break;
1344 default: /* others: warn about potential misinterpretation */
1345 dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
1346 "CONNECT_REQ", "Called party number", *pp);
1347 }
1348 pp++;
1349 l--;
1350 /* translate "**" internal call prefix to CTP value */
1351 if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
1352 s = "^SCTP=0\r";
1353 pp += 2;
1354 l -= 2;
1355 } else {
1356 s = "^SCTP=1\r";
1357 }
1358 commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
1359 if (!commands[AT_TYPE])
1360 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001361 commands[AT_DIAL] = kmalloc(l + 3, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001362 if (!commands[AT_DIAL])
1363 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001364 snprintf(commands[AT_DIAL], l + 3, "D%.*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001365
1366 /* encode parameter: Calling party number */
1367 pp = cmsg->CallingPartyNumber;
1368 if (pp != NULL && *pp > 0) {
1369 l = *pp++;
1370
1371 /* check type of number/numbering plan byte */
1372 /* ToDo: allow for/handle Ext=1? */
1373 switch (*pp) {
1374 case 0x00: /* unknown type / unknown numbering plan */
1375 case 0x01: /* unknown type / ISDN/Telephony num. plan */
1376 break;
1377 default:
1378 dev_notice(cs->dev,
1379 "%s: %s type/plan 0x%02x unsupported\n",
1380 "CONNECT_REQ", "Calling party number", *pp);
1381 }
1382 pp++;
1383 l--;
1384
1385 /* check presentation indicator */
1386 if (!l) {
1387 dev_notice(cs->dev, "%s: %s IE truncated\n",
1388 "CONNECT_REQ", "Calling party number");
1389 info = CapiIllMessageParmCoding;
1390 goto error;
1391 }
1392 switch (*pp & 0xfc) { /* ignore Screening indicator */
1393 case 0x80: /* Presentation allowed */
1394 s = "^SCLIP=1\r";
1395 break;
1396 case 0xa0: /* Presentation restricted */
1397 s = "^SCLIP=0\r";
1398 break;
1399 default:
1400 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1401 "CONNECT_REQ",
1402 "Presentation/Screening indicator",
1403 *pp);
1404 s = "^SCLIP=1\r";
1405 }
1406 commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
1407 if (!commands[AT_CLIP])
1408 goto oom;
1409 pp++;
1410 l--;
1411
1412 if (l) {
1413 /* number */
Joe Perches475be4d2012-02-19 19:52:38 -08001414 commands[AT_MSN] = kmalloc(l + 8, GFP_KERNEL);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001415 if (!commands[AT_MSN])
1416 goto oom;
Joe Perches475be4d2012-02-19 19:52:38 -08001417 snprintf(commands[AT_MSN], l + 8, "^SMSN=%*s\r", l, pp);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001418 }
1419 }
1420
1421 /* check parameter: CIP Value */
Tilman Schmidt6ad34142010-03-16 07:04:01 +00001422 if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001423 (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
1424 dev_notice(cs->dev, "%s: unknown CIP value %d\n",
1425 "CONNECT_REQ", cmsg->CIPValue);
1426 info = CapiCipValueUnknown;
1427 goto error;
1428 }
1429
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001430 /*
1431 * check/encode parameters: BC & HLC
1432 * must be encoded together as device doesn't accept HLC separately
1433 * explicit parameters override values derived from CIP
1434 */
1435
1436 /* determine lengths */
1437 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001438 lbc = 2 * cmsg->BC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001439 else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
1440 lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
1441 else /* no BC */
1442 lbc = 0;
1443 if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
Joe Perches475be4d2012-02-19 19:52:38 -08001444 lhlc = 2 * cmsg->HLC[0];
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001445 else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
1446 lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
1447 else /* no HLC */
1448 lhlc = 0;
1449
1450 if (lbc) {
1451 /* have BC: allocate and assemble command string */
1452 l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
1453 if (lhlc)
1454 l += lhlc + 7; /* ";^SHLC=" + value */
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001455 commands[AT_BC] = kmalloc(l, GFP_KERNEL);
1456 if (!commands[AT_BC])
1457 goto oom;
1458 strcpy(commands[AT_BC], "^SBC=");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001459 if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
1460 decode_ie(cmsg->BC, commands[AT_BC] + 5);
1461 else /* BC derived from CIP */
1462 strcpy(commands[AT_BC] + 5,
1463 cip2bchlc[cmsg->CIPValue].bc);
1464 if (lhlc) {
1465 strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
1466 if (cmsg->HLC && cmsg->HLC[0])
1467 /* HLC specified explicitly */
1468 decode_ie(cmsg->HLC,
1469 commands[AT_BC] + lbc + 12);
1470 else /* HLC derived from CIP */
1471 strcpy(commands[AT_BC] + lbc + 12,
1472 cip2bchlc[cmsg->CIPValue].hlc);
1473 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001474 strcpy(commands[AT_BC] + l - 2, "\r");
Tilman Schmidt1ce368f2010-06-21 13:55:05 +00001475 } else {
1476 /* no BC */
1477 if (lhlc) {
1478 dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
1479 "CONNECT_REQ");
1480 info = CapiIllMessageParmCoding; /* ? */
1481 goto error;
1482 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001483 }
1484
1485 /* check/encode parameter: B Protocol */
1486 if (cmsg->BProtocol == CAPI_DEFAULT) {
1487 bcs->proto2 = L2_HDLC;
1488 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001489 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001490 } else {
1491 switch (cmsg->B1protocol) {
1492 case 0:
1493 bcs->proto2 = L2_HDLC;
1494 break;
1495 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001496 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001497 break;
1498 default:
1499 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001500 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001501 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001502 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001503 }
1504 if (cmsg->B2protocol != 1)
1505 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001506 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001507 cmsg->B2protocol);
1508 if (cmsg->B3protocol != 0)
1509 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001510 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001511 cmsg->B3protocol);
1512 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001513 "CONNECT_REQ", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001514 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001515 "CONNECT_REQ", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001516 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001517 "CONNECT_REQ", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001518 }
1519 commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
1520 if (!commands[AT_PROTO])
1521 goto oom;
1522 snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
1523
1524 /* ToDo: check/encode remaining parameters */
1525 ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001526 "CONNECT_REQ", "Called pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001527 ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001528 "CONNECT_REQ", "Calling pty subaddr");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001529 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001530 "CONNECT_REQ", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001531 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1532 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001533 "CONNECT_REQ", "B Channel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001534 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001535 "CONNECT_REQ", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001536 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001537 "CONNECT_REQ", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001538 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001539 "CONNECT_REQ", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001540 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001541
1542 /* encode parameter: B channel to use */
1543 commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
1544 if (!commands[AT_ISO])
1545 goto oom;
1546 snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
1547 (unsigned) bcs->channel + 1);
1548
1549 /* queue & schedule EV_DIAL event */
1550 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
Tilman Schmidt1528b182010-02-22 13:09:52 +00001551 bcs->at_state.seq_index, NULL)) {
1552 info = CAPI_MSGOSRESOURCEERR;
1553 goto error;
1554 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001555 gigaset_schedule_event(cs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001556 send_conf(iif, ap, skb, CapiSuccess);
1557 return;
1558
1559oom:
1560 dev_err(cs->dev, "%s: out of memory\n", __func__);
1561 info = CAPI_MSGOSRESOURCEERR;
1562error:
1563 if (commands)
1564 for (i = 0; i < AT_NUM; i++)
1565 kfree(commands[i]);
1566 kfree(commands);
1567 gigaset_free_channel(bcs);
1568 send_conf(iif, ap, skb, info);
1569}
1570
1571/*
1572 * process CONNECT_RESP message
1573 * checks protocol parameters and queues an ACCEPT or HUP event
1574 */
1575static void do_connect_resp(struct gigaset_capi_ctr *iif,
1576 struct gigaset_capi_appl *ap,
1577 struct sk_buff *skb)
1578{
1579 struct cardstate *cs = iif->ctr.driverdata;
1580 _cmsg *cmsg = &iif->acmsg;
1581 struct bc_state *bcs;
1582 struct gigaset_capi_appl *oap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001583 unsigned long flags;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001584 int channel;
1585
1586 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001587 capi_message2cmsg(cmsg, skb->data);
1588 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001589 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001590
1591 /* extract and check channel number from PLCI */
1592 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
1593 if (!channel || channel > cs->channels) {
1594 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
1595 "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
1596 return;
1597 }
1598 bcs = cs->bcs + channel - 1;
1599
1600 switch (cmsg->Reject) {
1601 case 0: /* Accept */
1602 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001603 spin_lock_irqsave(&bcs->aplock, flags);
1604 while (bcs->ap != NULL) {
1605 oap = bcs->ap;
1606 bcs->ap = oap->bcnext;
1607 if (oap != ap) {
1608 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001609 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001610 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001611 spin_lock_irqsave(&bcs->aplock, flags);
1612 }
1613 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001614 ap->bcnext = NULL;
1615 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001616 spin_unlock_irqrestore(&bcs->aplock, flags);
1617
Tilman Schmidte7752ee2010-06-21 13:54:19 +00001618 bcs->rx_bufsize = ap->rp.datablklen;
1619 dev_kfree_skb(bcs->rx_skb);
1620 gigaset_new_rx_skb(bcs);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001621 bcs->chstate |= CHS_NOTIFY_LL;
1622
1623 /* check/encode B channel protocol */
1624 if (cmsg->BProtocol == CAPI_DEFAULT) {
1625 bcs->proto2 = L2_HDLC;
1626 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001627 "B2 Protocol X.75 SLP unsupported, using Transparent\n");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001628 } else {
1629 switch (cmsg->B1protocol) {
1630 case 0:
1631 bcs->proto2 = L2_HDLC;
1632 break;
1633 case 1:
Tilman Schmidt278a5822010-06-21 13:54:35 +00001634 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001635 break;
1636 default:
1637 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001638 "B1 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001639 cmsg->B1protocol);
Tilman Schmidt278a5822010-06-21 13:54:35 +00001640 bcs->proto2 = L2_VOICE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001641 }
1642 if (cmsg->B2protocol != 1)
1643 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001644 "B2 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001645 cmsg->B2protocol);
1646 if (cmsg->B3protocol != 0)
1647 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001648 "B3 Protocol %u unsupported, using Transparent\n",
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001649 cmsg->B3protocol);
1650 ignore_cstruct_param(cs, cmsg->B1configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001651 "CONNECT_RESP", "B1 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001652 ignore_cstruct_param(cs, cmsg->B2configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001653 "CONNECT_RESP", "B2 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001654 ignore_cstruct_param(cs, cmsg->B3configuration,
Joe Perches475be4d2012-02-19 19:52:38 -08001655 "CONNECT_RESP", "B3 Configuration");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001656 }
1657
1658 /* ToDo: check/encode remaining parameters */
1659 ignore_cstruct_param(cs, cmsg->ConnectedNumber,
Joe Perches475be4d2012-02-19 19:52:38 -08001660 "CONNECT_RESP", "Connected Number");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001661 ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
Joe Perches475be4d2012-02-19 19:52:38 -08001662 "CONNECT_RESP", "Connected Subaddress");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001663 ignore_cstruct_param(cs, cmsg->LLC,
Joe Perches475be4d2012-02-19 19:52:38 -08001664 "CONNECT_RESP", "LLC");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001665 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1666 ignore_cstruct_param(cs, cmsg->BChannelinformation,
Joe Perches475be4d2012-02-19 19:52:38 -08001667 "CONNECT_RESP", "BChannel Information");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001668 ignore_cstruct_param(cs, cmsg->Keypadfacility,
Joe Perches475be4d2012-02-19 19:52:38 -08001669 "CONNECT_RESP", "Keypad Facility");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001670 ignore_cstruct_param(cs, cmsg->Useruserdata,
Joe Perches475be4d2012-02-19 19:52:38 -08001671 "CONNECT_RESP", "User-User Data");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001672 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
Joe Perches475be4d2012-02-19 19:52:38 -08001673 "CONNECT_RESP", "Facility Data Array");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001674 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001675
1676 /* Accept call */
Joe Perches475be4d2012-02-19 19:52:38 -08001677 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001678 EV_ACCEPT, NULL, 0, NULL))
1679 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001680 gigaset_schedule_event(cs);
1681 return;
1682
1683 case 1: /* Ignore */
1684 /* send DISCONNECT_IND to this application */
1685 send_disconnect_ind(bcs, ap, 0);
1686
1687 /* remove it from the list of listening apps */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001688 spin_lock_irqsave(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001689 if (bcs->ap == ap) {
1690 bcs->ap = ap->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001691 if (bcs->ap == NULL) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001692 /* last one: stop ev-layer hupD notifications */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001693 bcs->apconnstate = APCONN_NONE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001694 bcs->chstate &= ~CHS_NOTIFY_LL;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001695 }
1696 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001697 return;
1698 }
1699 for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
1700 if (oap->bcnext == ap) {
1701 oap->bcnext = oap->bcnext->bcnext;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001702 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001703 return;
1704 }
1705 }
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001706 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001707 dev_err(cs->dev, "%s: application %u not found\n",
1708 __func__, ap->id);
1709 return;
1710
1711 default: /* Reject */
1712 /* drop all competing applications, keep only this one */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001713 spin_lock_irqsave(&bcs->aplock, flags);
1714 while (bcs->ap != NULL) {
1715 oap = bcs->ap;
1716 bcs->ap = oap->bcnext;
1717 if (oap != ap) {
1718 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001719 send_disconnect_ind(bcs, oap,
Joe Perches475be4d2012-02-19 19:52:38 -08001720 CapiCallGivenToOtherApplication);
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001721 spin_lock_irqsave(&bcs->aplock, flags);
1722 }
1723 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001724 ap->bcnext = NULL;
1725 bcs->ap = ap;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001726 spin_unlock_irqrestore(&bcs->aplock, flags);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001727
1728 /* reject call - will trigger DISCONNECT_IND for this app */
1729 dev_info(cs->dev, "%s: Reject=%x\n",
1730 "CONNECT_RESP", cmsg->Reject);
Joe Perches475be4d2012-02-19 19:52:38 -08001731 if (!gigaset_add_event(cs, &cs->bcs[channel - 1].at_state,
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001732 EV_HUP, NULL, 0, NULL))
1733 return;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001734 gigaset_schedule_event(cs);
1735 return;
1736 }
1737}
1738
1739/*
1740 * process CONNECT_B3_REQ message
1741 * build NCCI and emit CONNECT_B3_CONF reply
1742 */
1743static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
1744 struct gigaset_capi_appl *ap,
1745 struct sk_buff *skb)
1746{
1747 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001748 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001749 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001750 int channel;
1751
1752 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001753 capi_message2cmsg(cmsg, skb->data);
1754 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001755
1756 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001757 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001758 if (!channel || channel > cs->channels) {
1759 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001760 "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001761 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1762 return;
1763 }
Joe Perches475be4d2012-02-19 19:52:38 -08001764 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001765
1766 /* mark logical connection active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001767 bcs->apconnstate = APCONN_ACTIVE;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001768
1769 /* build NCCI: always 1 (one B3 connection only) */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001770 cmsg->adr.adrNCCI |= 1 << 16;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001771
1772 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001773 ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
1774 send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08001775 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001776}
1777
1778/*
1779 * process CONNECT_B3_RESP message
1780 * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
1781 * or queue EV_HUP and emit DISCONNECT_B3_IND.
1782 * The emitted message is always shorter than the received one,
1783 * allowing to reuse the skb.
1784 */
1785static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
1786 struct gigaset_capi_appl *ap,
1787 struct sk_buff *skb)
1788{
1789 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001790 _cmsg *cmsg = &iif->acmsg;
1791 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001792 int channel;
1793 unsigned int msgsize;
1794 u8 command;
1795
1796 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001797 capi_message2cmsg(cmsg, skb->data);
1798 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001799
1800 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001801 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001802 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00001803 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001804 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001805 "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001806 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001807 return;
1808 }
Joe Perches475be4d2012-02-19 19:52:38 -08001809 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001810
Tilman Schmidt6c911912009-10-25 09:29:37 +00001811 if (cmsg->Reject) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001812 /* Reject: clear B3 connect received flag */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001813 bcs->apconnstate = APCONN_SETUP;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001814
1815 /* trigger hangup, causing eventual DISCONNECT_IND */
1816 if (!gigaset_add_event(cs, &bcs->at_state,
1817 EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt4dd82302009-10-25 09:29:57 +00001818 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001819 return;
1820 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001821 gigaset_schedule_event(cs);
1822
1823 /* emit DISCONNECT_B3_IND */
1824 command = CAPI_DISCONNECT_B3;
1825 msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
1826 } else {
1827 /*
1828 * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
1829 * we only send CONNECT_B3_IND if the B channel is up
1830 */
1831 command = CAPI_CONNECT_B3_ACTIVE;
1832 msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
1833 }
Tilman Schmidt6c911912009-10-25 09:29:37 +00001834 capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
1835 ap->nextMessageNumber++, cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001836 __skb_trim(skb, msgsize);
Tilman Schmidt6c911912009-10-25 09:29:37 +00001837 capi_cmsg2message(cmsg, skb->data);
1838 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001839 capi_ctr_handle_message(&iif->ctr, ap->id, skb);
1840}
1841
1842/*
1843 * process DISCONNECT_REQ message
1844 * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
1845 * emit DISCONNECT_CONF reply
1846 */
1847static void do_disconnect_req(struct gigaset_capi_ctr *iif,
1848 struct gigaset_capi_appl *ap,
1849 struct sk_buff *skb)
1850{
1851 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001852 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001853 struct bc_state *bcs;
1854 _cmsg *b3cmsg;
1855 struct sk_buff *b3skb;
1856 int channel;
1857
1858 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001859 capi_message2cmsg(cmsg, skb->data);
1860 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001861
1862 /* extract and check channel number from PLCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001863 channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001864 if (!channel || channel > cs->channels) {
1865 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001866 "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001867 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1868 return;
1869 }
1870 bcs = cs->bcs + channel - 1;
1871
1872 /* ToDo: process parameter: Additional info */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001873 if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
1874 ignore_cstruct_param(cs, cmsg->BChannelinformation,
1875 "DISCONNECT_REQ", "B Channel Information");
1876 ignore_cstruct_param(cs, cmsg->Keypadfacility,
1877 "DISCONNECT_REQ", "Keypad Facility");
1878 ignore_cstruct_param(cs, cmsg->Useruserdata,
1879 "DISCONNECT_REQ", "User-User Data");
1880 ignore_cstruct_param(cs, cmsg->Facilitydataarray,
1881 "DISCONNECT_REQ", "Facility Data Array");
1882 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001883
1884 /* skip if DISCONNECT_IND already sent */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001885 if (!bcs->apconnstate)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001886 return;
1887
1888 /* check for active logical connection */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001889 if (bcs->apconnstate >= APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001890 /*
1891 * emit DISCONNECT_B3_IND with cause 0x3301
1892 * use separate cmsg structure, as the content of iif->acmsg
1893 * is still needed for creating the _CONF message
1894 */
1895 b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
1896 if (!b3cmsg) {
1897 dev_err(cs->dev, "%s: out of memory\n", __func__);
1898 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1899 return;
1900 }
1901 capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
1902 ap->nextMessageNumber++,
Tilman Schmidt6c911912009-10-25 09:29:37 +00001903 cmsg->adr.adrPLCI | (1 << 16));
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001904 b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
1905 b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
1906 if (b3skb == NULL) {
1907 dev_err(cs->dev, "%s: out of memory\n", __func__);
1908 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
Jesper Juhl2393c942010-12-26 09:59:58 +00001909 kfree(b3cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001910 return;
1911 }
1912 capi_cmsg2message(b3cmsg,
Joe Perches475be4d2012-02-19 19:52:38 -08001913 __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN));
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001914 kfree(b3cmsg);
1915 capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
1916 }
1917
1918 /* trigger hangup, causing eventual DISCONNECT_IND */
1919 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001920 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1921 return;
1922 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001923 gigaset_schedule_event(cs);
1924
1925 /* emit reply */
1926 send_conf(iif, ap, skb, CapiSuccess);
1927}
1928
1929/*
1930 * process DISCONNECT_B3_REQ message
1931 * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
1932 */
1933static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
1934 struct gigaset_capi_appl *ap,
1935 struct sk_buff *skb)
1936{
1937 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt6c911912009-10-25 09:29:37 +00001938 _cmsg *cmsg = &iif->acmsg;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001939 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001940 int channel;
1941
1942 /* decode message */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001943 capi_message2cmsg(cmsg, skb->data);
1944 dump_cmsg(DEBUG_CMD, __func__, cmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001945
1946 /* extract and check channel number and NCCI */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001947 channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001948 if (!channel || channel > cs->channels ||
Tilman Schmidt6c911912009-10-25 09:29:37 +00001949 ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001950 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
Tilman Schmidt6c911912009-10-25 09:29:37 +00001951 "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001952 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
1953 return;
1954 }
Joe Perches475be4d2012-02-19 19:52:38 -08001955 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001956
1957 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001958 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001959 send_conf(iif, ap, skb,
1960 CapiMessageNotSupportedInCurrentState);
1961 return;
1962 }
1963
1964 /* trigger hangup, causing eventual DISCONNECT_B3_IND */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001965 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001966 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
1967 return;
1968 }
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001969 gigaset_schedule_event(cs);
1970
1971 /* NCPI parameter: not applicable for B3 Transparent */
Tilman Schmidt6c911912009-10-25 09:29:37 +00001972 ignore_cstruct_param(cs, cmsg->NCPI,
Joe Perches475be4d2012-02-19 19:52:38 -08001973 "DISCONNECT_B3_REQ", "NCPI");
Tilman Schmidt6c911912009-10-25 09:29:37 +00001974 send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
Joe Perches475be4d2012-02-19 19:52:38 -08001975 CapiNcpiNotSupportedByProtocol : CapiSuccess);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001976}
1977
1978/*
1979 * process DATA_B3_REQ message
1980 */
1981static void do_data_b3_req(struct gigaset_capi_ctr *iif,
1982 struct gigaset_capi_appl *ap,
1983 struct sk_buff *skb)
1984{
1985 struct cardstate *cs = iif->ctr.driverdata;
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00001986 struct bc_state *bcs;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001987 int channel = CAPIMSG_PLCI_PART(skb->data);
1988 u16 ncci = CAPIMSG_NCCI_PART(skb->data);
1989 u16 msglen = CAPIMSG_LEN(skb->data);
1990 u16 datalen = CAPIMSG_DATALEN(skb->data);
1991 u16 flags = CAPIMSG_FLAGS(skb->data);
Tilman Schmidt23b36772010-06-21 13:54:50 +00001992 u16 msgid = CAPIMSG_MSGID(skb->data);
1993 u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001994
1995 /* frequent message, avoid _cmsg overhead */
Tilman Schmidt6a753422010-07-05 14:18:59 +00001996 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00001997
1998 /* check parameters */
1999 if (channel == 0 || channel > cs->channels || ncci != 1) {
2000 dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
2001 "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
2002 send_conf(iif, ap, skb, CapiIllContrPlciNcci);
2003 return;
2004 }
Joe Perches475be4d2012-02-19 19:52:38 -08002005 bcs = &cs->bcs[channel - 1];
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002006 if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
2007 dev_notice(cs->dev, "%s: unexpected length %d\n",
2008 "DATA_B3_REQ", msglen);
2009 if (msglen + datalen != skb->len)
2010 dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
2011 "DATA_B3_REQ", msglen, datalen, skb->len);
2012 if (msglen + datalen > skb->len) {
2013 /* message too short for announced data length */
2014 send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
2015 return;
2016 }
2017 if (flags & CAPI_FLAGS_RESERVED) {
2018 dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
2019 "DATA_B3_REQ", flags);
2020 send_conf(iif, ap, skb, CapiIllMessageParmCoding);
2021 return;
2022 }
2023
2024 /* reject if logical connection not active */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002025 if (bcs->apconnstate < APCONN_ACTIVE) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002026 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2027 return;
2028 }
2029
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002030 /* pull CAPI message into link layer header */
2031 skb_reset_mac_header(skb);
2032 skb->mac_len = msglen;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002033 skb_pull(skb, msglen);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002034
2035 /* pass to device-specific module */
Tilman Schmidt1b4843c2010-06-21 13:55:20 +00002036 if (cs->ops->send_skb(bcs, skb) < 0) {
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002037 send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
2038 return;
2039 }
2040
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002041 /*
Tilman Schmidt23b36772010-06-21 13:54:50 +00002042 * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
2043 * confirmation" bit is set; otherwise we have to send it now
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002044 */
Tilman Schmidt23b36772010-06-21 13:54:50 +00002045 if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
2046 send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
2047 flags ? CapiFlagsNotSupportedByProtocol
Joe Perches475be4d2012-02-19 19:52:38 -08002048 : CAPI_NOERROR);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002049}
2050
2051/*
2052 * process RESET_B3_REQ message
2053 * just always reply "not supported by current protocol"
2054 */
2055static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
2056 struct gigaset_capi_appl *ap,
2057 struct sk_buff *skb)
2058{
2059 /* decode message */
2060 capi_message2cmsg(&iif->acmsg, skb->data);
2061 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
2062 send_conf(iif, ap, skb,
2063 CapiResetProcedureNotSupportedByCurrentProtocol);
2064}
2065
2066/*
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002067 * unsupported CAPI message handler
2068 */
2069static void do_unsupported(struct gigaset_capi_ctr *iif,
2070 struct gigaset_capi_appl *ap,
2071 struct sk_buff *skb)
2072{
2073 /* decode message */
2074 capi_message2cmsg(&iif->acmsg, skb->data);
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002075 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002076 send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
2077}
2078
2079/*
2080 * CAPI message handler: no-op
2081 */
2082static void do_nothing(struct gigaset_capi_ctr *iif,
2083 struct gigaset_capi_appl *ap,
2084 struct sk_buff *skb)
2085{
Tilman Schmidt8e618aa2012-04-25 13:02:19 +00002086 /* decode message */
2087 capi_message2cmsg(&iif->acmsg, skb->data);
2088 dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002089 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002090}
2091
2092static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
2093 struct gigaset_capi_appl *ap,
2094 struct sk_buff *skb)
2095{
Tilman Schmidt6a753422010-07-05 14:18:59 +00002096 dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
Tilman Schmidt4dd82302009-10-25 09:29:57 +00002097 dev_kfree_skb_any(skb);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002098}
2099
2100/* table of outgoing CAPI message handlers with lookup function */
2101typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
2102 struct gigaset_capi_appl *,
2103 struct sk_buff *);
2104
2105static struct {
2106 u16 cmd;
2107 capi_send_handler_t handler;
2108} capi_send_handler_table[] = {
2109 /* most frequent messages first for faster lookup */
2110 { CAPI_DATA_B3_REQ, do_data_b3_req },
2111 { CAPI_DATA_B3_RESP, do_data_b3_resp },
2112
2113 { CAPI_ALERT_REQ, do_alert_req },
2114 { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
2115 { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
2116 { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
2117 { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
2118 { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
2119 { CAPI_CONNECT_REQ, do_connect_req },
2120 { CAPI_CONNECT_RESP, do_connect_resp },
2121 { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
2122 { CAPI_DISCONNECT_B3_RESP, do_nothing },
2123 { CAPI_DISCONNECT_REQ, do_disconnect_req },
2124 { CAPI_DISCONNECT_RESP, do_nothing },
2125 { CAPI_FACILITY_REQ, do_facility_req },
2126 { CAPI_FACILITY_RESP, do_nothing },
2127 { CAPI_LISTEN_REQ, do_listen_req },
2128 { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
2129 { CAPI_RESET_B3_REQ, do_reset_b3_req },
2130 { CAPI_RESET_B3_RESP, do_nothing },
2131
2132 /*
2133 * ToDo: support overlap sending (requires ev-layer state
2134 * machine extension to generate additional ATD commands)
2135 */
2136 { CAPI_INFO_REQ, do_unsupported },
2137 { CAPI_INFO_RESP, do_nothing },
2138
2139 /*
2140 * ToDo: what's the proper response for these?
2141 */
2142 { CAPI_MANUFACTURER_REQ, do_nothing },
2143 { CAPI_MANUFACTURER_RESP, do_nothing },
2144};
2145
2146/* look up handler */
2147static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
2148{
2149 size_t i;
2150
2151 for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
2152 if (capi_send_handler_table[i].cmd == cmd)
2153 return capi_send_handler_table[i].handler;
2154 return NULL;
2155}
2156
2157
2158/**
2159 * gigaset_send_message() - accept a CAPI message from an application
2160 * @ctr: controller descriptor structure.
2161 * @skb: CAPI message.
2162 *
2163 * Return value: CAPI error code
2164 * Note: capidrv (and probably others, too) only uses the return value to
2165 * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
2166 */
2167static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
2168{
2169 struct gigaset_capi_ctr *iif
2170 = container_of(ctr, struct gigaset_capi_ctr, ctr);
2171 struct cardstate *cs = ctr->driverdata;
2172 struct gigaset_capi_appl *ap;
2173 capi_send_handler_t handler;
2174
2175 /* can only handle linear sk_buffs */
2176 if (skb_linearize(skb) < 0) {
2177 dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
2178 return CAPI_MSGOSRESOURCEERR;
2179 }
2180
2181 /* retrieve application data structure */
2182 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2183 if (!ap) {
2184 dev_notice(cs->dev, "%s: application %u not registered\n",
2185 __func__, CAPIMSG_APPID(skb->data));
2186 return CAPI_ILLAPPNR;
2187 }
2188
2189 /* look up command */
2190 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2191 if (!handler) {
2192 /* unknown/unsupported message type */
2193 if (printk_ratelimit())
2194 dev_notice(cs->dev, "%s: unsupported message %u\n",
2195 __func__, CAPIMSG_CMD(skb->data));
2196 return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
2197 }
2198
2199 /* serialize */
2200 if (atomic_add_return(1, &iif->sendqlen) > 1) {
2201 /* queue behind other messages */
2202 skb_queue_tail(&iif->sendqueue, skb);
2203 return CAPI_NOERROR;
2204 }
2205
2206 /* process message */
2207 handler(iif, ap, skb);
2208
2209 /* process other messages arrived in the meantime */
2210 while (atomic_sub_return(1, &iif->sendqlen) > 0) {
2211 skb = skb_dequeue(&iif->sendqueue);
2212 if (!skb) {
2213 /* should never happen */
2214 dev_err(cs->dev, "%s: send queue empty\n", __func__);
2215 continue;
2216 }
2217 ap = get_appl(iif, CAPIMSG_APPID(skb->data));
2218 if (!ap) {
2219 /* could that happen? */
2220 dev_warn(cs->dev, "%s: application %u vanished\n",
2221 __func__, CAPIMSG_APPID(skb->data));
2222 continue;
2223 }
2224 handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
2225 if (!handler) {
2226 /* should never happen */
2227 dev_err(cs->dev, "%s: handler %x vanished\n",
2228 __func__, CAPIMSG_CMD(skb->data));
2229 continue;
2230 }
2231 handler(iif, ap, skb);
2232 }
2233
2234 return CAPI_NOERROR;
2235}
2236
2237/**
2238 * gigaset_procinfo() - build single line description for controller
2239 * @ctr: controller descriptor structure.
2240 *
2241 * Return value: pointer to generated string (null terminated)
2242 */
2243static char *gigaset_procinfo(struct capi_ctr *ctr)
2244{
2245 return ctr->name; /* ToDo: more? */
2246}
2247
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002248static int gigaset_proc_show(struct seq_file *m, void *v)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002249{
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002250 struct capi_ctr *ctr = m->private;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002251 struct cardstate *cs = ctr->driverdata;
2252 char *s;
2253 int i;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002254
2255 seq_printf(m, "%-16s %s\n", "name", ctr->name);
2256 seq_printf(m, "%-16s %s %s\n", "dev",
Joe Perches475be4d2012-02-19 19:52:38 -08002257 dev_driver_string(cs->dev), dev_name(cs->dev));
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002258 seq_printf(m, "%-16s %d\n", "id", cs->myid);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002259 if (cs->gotfwver)
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002260 seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
Joe Perches475be4d2012-02-19 19:52:38 -08002261 cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002262 seq_printf(m, "%-16s %d\n", "channels", cs->channels);
2263 seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002264
2265 switch (cs->mode) {
2266 case M_UNKNOWN:
2267 s = "unknown";
2268 break;
2269 case M_CONFIG:
2270 s = "config";
2271 break;
2272 case M_UNIMODEM:
2273 s = "Unimodem";
2274 break;
2275 case M_CID:
2276 s = "CID";
2277 break;
2278 default:
2279 s = "??";
2280 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002281 seq_printf(m, "%-16s %s\n", "mode", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002282
2283 switch (cs->mstate) {
2284 case MS_UNINITIALIZED:
2285 s = "uninitialized";
2286 break;
2287 case MS_INIT:
2288 s = "init";
2289 break;
2290 case MS_LOCKED:
2291 s = "locked";
2292 break;
2293 case MS_SHUTDOWN:
2294 s = "shutdown";
2295 break;
2296 case MS_RECOVER:
2297 s = "recover";
2298 break;
2299 case MS_READY:
2300 s = "ready";
2301 break;
2302 default:
2303 s = "??";
2304 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002305 seq_printf(m, "%-16s %s\n", "mstate", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002306
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002307 seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
2308 seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
2309 seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
2310 seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002311
2312 for (i = 0; i < cs->channels; i++) {
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002313 seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
Joe Perches475be4d2012-02-19 19:52:38 -08002314 cs->bcs[i].corrupted);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002315 seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
Joe Perches475be4d2012-02-19 19:52:38 -08002316 cs->bcs[i].trans_down);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002317 seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
Joe Perches475be4d2012-02-19 19:52:38 -08002318 cs->bcs[i].trans_up);
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002319 seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
Joe Perches475be4d2012-02-19 19:52:38 -08002320 cs->bcs[i].chstate);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002321 switch (cs->bcs[i].proto2) {
2322 case L2_BITSYNC:
2323 s = "bitsync";
2324 break;
2325 case L2_HDLC:
2326 s = "HDLC";
2327 break;
2328 case L2_VOICE:
2329 s = "voice";
2330 break;
2331 default:
2332 s = "??";
2333 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002334 seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002335 }
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002336 return 0;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002337}
2338
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002339static int gigaset_proc_open(struct inode *inode, struct file *file)
2340{
2341 return single_open(file, gigaset_proc_show, PDE(inode)->data);
2342}
2343
2344static const struct file_operations gigaset_proc_fops = {
2345 .owner = THIS_MODULE,
2346 .open = gigaset_proc_open,
2347 .read = seq_read,
2348 .llseek = seq_lseek,
2349 .release = single_release,
2350};
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002351
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002352/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002353 * gigaset_isdn_regdev() - register device to LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002354 * @cs: device descriptor structure.
2355 * @isdnid: device name.
2356 *
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002357 * Return value: 1 for success, 0 for failure
2358 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002359int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002360{
2361 struct gigaset_capi_ctr *iif;
2362 int rc;
2363
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002364 iif = kmalloc(sizeof(*iif), GFP_KERNEL);
2365 if (!iif) {
2366 pr_err("%s: out of memory\n", __func__);
2367 return 0;
2368 }
2369
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002370 /* prepare controller structure */
2371 iif->ctr.owner = THIS_MODULE;
2372 iif->ctr.driverdata = cs;
2373 strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name));
2374 iif->ctr.driver_name = "gigaset";
Tilman Schmidte4876392010-05-23 01:02:38 +00002375 iif->ctr.load_firmware = NULL;
2376 iif->ctr.reset_ctr = NULL;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002377 iif->ctr.register_appl = gigaset_register_appl;
2378 iif->ctr.release_appl = gigaset_release_appl;
2379 iif->ctr.send_message = gigaset_send_message;
2380 iif->ctr.procinfo = gigaset_procinfo;
Alexey Dobriyan9a58a802010-01-14 03:10:54 -08002381 iif->ctr.proc_fops = &gigaset_proc_fops;
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002382 INIT_LIST_HEAD(&iif->appls);
2383 skb_queue_head_init(&iif->sendqueue);
2384 atomic_set(&iif->sendqlen, 0);
2385
2386 /* register controller with CAPI */
2387 rc = attach_capi_ctr(&iif->ctr);
2388 if (rc) {
2389 pr_err("attach_capi_ctr failed (%d)\n", rc);
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002390 kfree(iif);
2391 return 0;
2392 }
2393
2394 cs->iif = iif;
2395 cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
2396 return 1;
2397}
2398
2399/**
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002400 * gigaset_isdn_unregdev() - unregister device from LL
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002401 * @cs: device descriptor structure.
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002402 */
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002403void gigaset_isdn_unregdev(struct cardstate *cs)
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002404{
2405 struct gigaset_capi_ctr *iif = cs->iif;
2406
2407 detach_capi_ctr(&iif->ctr);
2408 kfree(iif);
2409 cs->iif = NULL;
Tilman Schmidtbc35b4e2010-03-14 12:58:05 +00002410}
2411
2412static struct capi_driver capi_driver_gigaset = {
2413 .name = "gigaset",
2414 .revision = "1.0",
2415};
2416
2417/**
2418 * gigaset_isdn_regdrv() - register driver to LL
2419 */
2420void gigaset_isdn_regdrv(void)
2421{
2422 pr_info("Kernel CAPI interface\n");
2423 register_capi_driver(&capi_driver_gigaset);
2424}
2425
2426/**
2427 * gigaset_isdn_unregdrv() - unregister driver from LL
2428 */
2429void gigaset_isdn_unregdrv(void)
2430{
Tilman Schmidt7bb5fdc2009-10-06 12:19:17 +00002431 unregister_capi_driver(&capi_driver_gigaset);
2432}