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