root/drivers/staging/isdn/gigaset/capi.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ignore_cstruct_param
  2. encode_ie
  3. decode_ie
  4. get_appl
  5. dump_cmsg
  6. dump_rawmsg
  7. format_ie
  8. send_data_b3_conf
  9. gigaset_skb_sent
  10. gigaset_skb_rcvd
  11. gigaset_isdn_rcv_err
  12. gigaset_isdn_icall
  13. send_disconnect_ind
  14. send_disconnect_b3_ind
  15. gigaset_isdn_connD
  16. gigaset_isdn_hupD
  17. gigaset_isdn_connB
  18. gigaset_isdn_hupB
  19. gigaset_isdn_start
  20. gigaset_isdn_stop
  21. gigaset_register_appl
  22. remove_appl_from_channel
  23. gigaset_release_appl
  24. send_conf
  25. do_facility_req
  26. do_listen_req
  27. do_alert_req
  28. do_connect_req
  29. do_connect_resp
  30. do_connect_b3_req
  31. do_connect_b3_resp
  32. do_disconnect_req
  33. do_disconnect_b3_req
  34. do_data_b3_req
  35. do_reset_b3_req
  36. do_unsupported
  37. do_nothing
  38. do_data_b3_resp
  39. lookup_capi_send_handler
  40. gigaset_send_message
  41. gigaset_procinfo
  42. gigaset_proc_show
  43. gigaset_isdn_regdev
  44. gigaset_isdn_unregdev
  45. gigaset_isdn_regdrv
  46. gigaset_isdn_unregdrv

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

/* [<][>][^][v][top][bottom][index][help] */