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