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