linux/drivers/isdn/gigaset/ev-layer.c
<<
>>
Prefs
   1/*
   2 * Stuff used by all variants of the driver
   3 *
   4 * Copyright (c) 2001 by Stefan Eilers,
   5 *                       Hansjoerg Lipp <hjlipp@web.de>,
   6 *                       Tilman Schmidt <tilman@imap.cc>.
   7 *
   8 * =====================================================================
   9 *      This program is free software; you can redistribute it and/or
  10 *      modify it under the terms of the GNU General Public License as
  11 *      published by the Free Software Foundation; either version 2 of
  12 *      the License, or (at your option) any later version.
  13 * =====================================================================
  14 */
  15
  16#include <linux/export.h>
  17#include "gigaset.h"
  18
  19/* ========================================================== */
  20/* bit masks for pending commands */
  21#define PC_DIAL         0x001
  22#define PC_HUP          0x002
  23#define PC_INIT         0x004
  24#define PC_DLE0         0x008
  25#define PC_DLE1         0x010
  26#define PC_SHUTDOWN     0x020
  27#define PC_ACCEPT       0x040
  28#define PC_CID          0x080
  29#define PC_NOCID        0x100
  30#define PC_CIDMODE      0x200
  31#define PC_UMMODE       0x400
  32
  33/* types of modem responses */
  34#define RT_NOTHING      0
  35#define RT_ZSAU         1
  36#define RT_RING         2
  37#define RT_NUMBER       3
  38#define RT_STRING       4
  39#define RT_ZCAU         6
  40
  41/* Possible ASCII responses */
  42#define RSP_OK          0
  43#define RSP_ERROR       1
  44#define RSP_ZGCI        3
  45#define RSP_RING        4
  46#define RSP_ZVLS        5
  47#define RSP_ZCAU        6
  48
  49/* responses with values to store in at_state */
  50/* - numeric */
  51#define RSP_VAR         100
  52#define RSP_ZSAU        (RSP_VAR + VAR_ZSAU)
  53#define RSP_ZDLE        (RSP_VAR + VAR_ZDLE)
  54#define RSP_ZCTP        (RSP_VAR + VAR_ZCTP)
  55/* - string */
  56#define RSP_STR         (RSP_VAR + VAR_NUM)
  57#define RSP_NMBR        (RSP_STR + STR_NMBR)
  58#define RSP_ZCPN        (RSP_STR + STR_ZCPN)
  59#define RSP_ZCON        (RSP_STR + STR_ZCON)
  60#define RSP_ZBC         (RSP_STR + STR_ZBC)
  61#define RSP_ZHLC        (RSP_STR + STR_ZHLC)
  62
  63#define RSP_WRONG_CID   -2      /* unknown cid in cmd */
  64#define RSP_INVAL       -6      /* invalid response   */
  65#define RSP_NODEV       -9      /* device not connected */
  66
  67#define RSP_NONE        -19
  68#define RSP_STRING      -20
  69#define RSP_NULL        -21
  70#define RSP_INIT        -27
  71#define RSP_ANY         -26
  72#define RSP_LAST        -28
  73
  74/* actions for process_response */
  75#define ACT_NOTHING             0
  76#define ACT_SETDLE1             1
  77#define ACT_SETDLE0             2
  78#define ACT_FAILINIT            3
  79#define ACT_HUPMODEM            4
  80#define ACT_CONFIGMODE          5
  81#define ACT_INIT                6
  82#define ACT_DLE0                7
  83#define ACT_DLE1                8
  84#define ACT_FAILDLE0            9
  85#define ACT_FAILDLE1            10
  86#define ACT_RING                11
  87#define ACT_CID                 12
  88#define ACT_FAILCID             13
  89#define ACT_SDOWN               14
  90#define ACT_FAILSDOWN           15
  91#define ACT_DEBUG               16
  92#define ACT_WARN                17
  93#define ACT_DIALING             18
  94#define ACT_ABORTDIAL           19
  95#define ACT_DISCONNECT          20
  96#define ACT_CONNECT             21
  97#define ACT_REMOTEREJECT        22
  98#define ACT_CONNTIMEOUT         23
  99#define ACT_REMOTEHUP           24
 100#define ACT_ABORTHUP            25
 101#define ACT_ICALL               26
 102#define ACT_ACCEPTED            27
 103#define ACT_ABORTACCEPT         28
 104#define ACT_TIMEOUT             29
 105#define ACT_GETSTRING           30
 106#define ACT_SETVER              31
 107#define ACT_FAILVER             32
 108#define ACT_GOTVER              33
 109#define ACT_TEST                34
 110#define ACT_ERROR               35
 111#define ACT_ABORTCID            36
 112#define ACT_ZCAU                37
 113#define ACT_NOTIFY_BC_DOWN      38
 114#define ACT_NOTIFY_BC_UP        39
 115#define ACT_DIAL                40
 116#define ACT_ACCEPT              41
 117#define ACT_HUP                 43
 118#define ACT_IF_LOCK             44
 119#define ACT_START               45
 120#define ACT_STOP                46
 121#define ACT_FAKEDLE0            47
 122#define ACT_FAKEHUP             48
 123#define ACT_FAKESDOWN           49
 124#define ACT_SHUTDOWN            50
 125#define ACT_PROC_CIDMODE        51
 126#define ACT_UMODESET            52
 127#define ACT_FAILUMODE           53
 128#define ACT_CMODESET            54
 129#define ACT_FAILCMODE           55
 130#define ACT_IF_VER              56
 131#define ACT_CMD                 100
 132
 133/* at command sequences */
 134#define SEQ_NONE        0
 135#define SEQ_INIT        100
 136#define SEQ_DLE0        200
 137#define SEQ_DLE1        250
 138#define SEQ_CID         300
 139#define SEQ_NOCID       350
 140#define SEQ_HUP         400
 141#define SEQ_DIAL        600
 142#define SEQ_ACCEPT      720
 143#define SEQ_SHUTDOWN    500
 144#define SEQ_CIDMODE     10
 145#define SEQ_UMMODE      11
 146
 147
 148/* 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid),
 149 * 400: hup, 500: reset, 600: dial, 700: ring */
 150struct reply_t gigaset_tab_nocid[] =
 151{
 152/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
 153 * action, command */
 154
 155/* initialize device, set cid mode if possible */
 156        {RSP_INIT,       -1,  -1, SEQ_INIT,     100,  1, {ACT_TIMEOUT} },
 157
 158        {EV_TIMEOUT,    100, 100, -1,           101,  3, {0},   "Z\r"},
 159        {RSP_OK,        101, 103, -1,           120,  5, {ACT_GETSTRING},
 160                                                                "+GMR\r"},
 161
 162        {EV_TIMEOUT,    101, 101, -1,           102,  5, {0},   "Z\r"},
 163        {RSP_ERROR,     101, 101, -1,           102,  5, {0},   "Z\r"},
 164
 165        {EV_TIMEOUT,    102, 102, -1,           108,  5, {ACT_SETDLE1},
 166                                                                "^SDLE=0\r"},
 167        {RSP_OK,        108, 108, -1,           104, -1},
 168        {RSP_ZDLE,      104, 104,  0,           103,  5, {0},   "Z\r"},
 169        {EV_TIMEOUT,    104, 104, -1,             0,  0, {ACT_FAILINIT} },
 170        {RSP_ERROR,     108, 108, -1,             0,  0, {ACT_FAILINIT} },
 171
 172        {EV_TIMEOUT,    108, 108, -1,           105,  2, {ACT_SETDLE0,
 173                                                          ACT_HUPMODEM,
 174                                                          ACT_TIMEOUT} },
 175        {EV_TIMEOUT,    105, 105, -1,           103,  5, {0},   "Z\r"},
 176
 177        {RSP_ERROR,     102, 102, -1,           107,  5, {0},   "^GETPRE\r"},
 178        {RSP_OK,        107, 107, -1,             0,  0, {ACT_CONFIGMODE} },
 179        {RSP_ERROR,     107, 107, -1,             0,  0, {ACT_FAILINIT} },
 180        {EV_TIMEOUT,    107, 107, -1,             0,  0, {ACT_FAILINIT} },
 181
 182        {RSP_ERROR,     103, 103, -1,             0,  0, {ACT_FAILINIT} },
 183        {EV_TIMEOUT,    103, 103, -1,             0,  0, {ACT_FAILINIT} },
 184
 185        {RSP_STRING,    120, 120, -1,           121, -1, {ACT_SETVER} },
 186
 187        {EV_TIMEOUT,    120, 121, -1,             0,  0, {ACT_FAILVER,
 188                                                          ACT_INIT} },
 189        {RSP_ERROR,     120, 121, -1,             0,  0, {ACT_FAILVER,
 190                                                          ACT_INIT} },
 191        {RSP_OK,        121, 121, -1,             0,  0, {ACT_GOTVER,
 192                                                          ACT_INIT} },
 193        {RSP_NONE,      121, 121, -1,           120,  0, {ACT_GETSTRING} },
 194
 195/* leave dle mode */
 196        {RSP_INIT,        0,   0, SEQ_DLE0,     201,  5, {0},   "^SDLE=0\r"},
 197        {RSP_OK,        201, 201, -1,           202, -1},
 198        {RSP_ZDLE,      202, 202,  0,             0,  0, {ACT_DLE0} },
 199        {RSP_NODEV,     200, 249, -1,             0,  0, {ACT_FAKEDLE0} },
 200        {RSP_ERROR,     200, 249, -1,             0,  0, {ACT_FAILDLE0} },
 201        {EV_TIMEOUT,    200, 249, -1,             0,  0, {ACT_FAILDLE0} },
 202
 203/* enter dle mode */
 204        {RSP_INIT,        0,   0, SEQ_DLE1,     251,  5, {0},   "^SDLE=1\r"},
 205        {RSP_OK,        251, 251, -1,           252, -1},
 206        {RSP_ZDLE,      252, 252,  1,             0,  0, {ACT_DLE1} },
 207        {RSP_ERROR,     250, 299, -1,             0,  0, {ACT_FAILDLE1} },
 208        {EV_TIMEOUT,    250, 299, -1,             0,  0, {ACT_FAILDLE1} },
 209
 210/* incoming call */
 211        {RSP_RING,       -1,  -1, -1,            -1, -1, {ACT_RING} },
 212
 213/* get cid */
 214        {RSP_INIT,        0,   0, SEQ_CID,      301,  5, {0},   "^SGCI?\r"},
 215        {RSP_OK,        301, 301, -1,           302, -1},
 216        {RSP_ZGCI,      302, 302, -1,             0,  0, {ACT_CID} },
 217        {RSP_ERROR,     301, 349, -1,             0,  0, {ACT_FAILCID} },
 218        {EV_TIMEOUT,    301, 349, -1,             0,  0, {ACT_FAILCID} },
 219
 220/* enter cid mode */
 221        {RSP_INIT,        0,   0, SEQ_CIDMODE,  150,  5, {0},   "^SGCI=1\r"},
 222        {RSP_OK,        150, 150, -1,             0,  0, {ACT_CMODESET} },
 223        {RSP_ERROR,     150, 150, -1,             0,  0, {ACT_FAILCMODE} },
 224        {EV_TIMEOUT,    150, 150, -1,             0,  0, {ACT_FAILCMODE} },
 225
 226/* leave cid mode */
 227        {RSP_INIT,        0,   0, SEQ_UMMODE,   160,  5, {0},   "Z\r"},
 228        {RSP_OK,        160, 160, -1,             0,  0, {ACT_UMODESET} },
 229        {RSP_ERROR,     160, 160, -1,             0,  0, {ACT_FAILUMODE} },
 230        {EV_TIMEOUT,    160, 160, -1,             0,  0, {ACT_FAILUMODE} },
 231
 232/* abort getting cid */
 233        {RSP_INIT,        0,   0, SEQ_NOCID,      0,  0, {ACT_ABORTCID} },
 234
 235/* reset */
 236        {RSP_INIT,        0,   0, SEQ_SHUTDOWN, 504,  5, {0},   "Z\r"},
 237        {RSP_OK,        504, 504, -1,             0,  0, {ACT_SDOWN} },
 238        {RSP_ERROR,     501, 599, -1,             0,  0, {ACT_FAILSDOWN} },
 239        {EV_TIMEOUT,    501, 599, -1,             0,  0, {ACT_FAILSDOWN} },
 240        {RSP_NODEV,     501, 599, -1,             0,  0, {ACT_FAKESDOWN} },
 241
 242        {EV_PROC_CIDMODE, -1, -1, -1,            -1, -1, {ACT_PROC_CIDMODE} },
 243        {EV_IF_LOCK,     -1,  -1, -1,            -1, -1, {ACT_IF_LOCK} },
 244        {EV_IF_VER,      -1,  -1, -1,            -1, -1, {ACT_IF_VER} },
 245        {EV_START,       -1,  -1, -1,            -1, -1, {ACT_START} },
 246        {EV_STOP,        -1,  -1, -1,            -1, -1, {ACT_STOP} },
 247        {EV_SHUTDOWN,    -1,  -1, -1,            -1, -1, {ACT_SHUTDOWN} },
 248
 249/* misc. */
 250        {RSP_ERROR,      -1,  -1, -1,            -1, -1, {ACT_ERROR} },
 251        {RSP_ZCAU,       -1,  -1, -1,            -1, -1, {ACT_ZCAU} },
 252        {RSP_NONE,       -1,  -1, -1,            -1, -1, {ACT_DEBUG} },
 253        {RSP_ANY,        -1,  -1, -1,            -1, -1, {ACT_WARN} },
 254        {RSP_LAST}
 255};
 256
 257/* 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring,
 258 * 400: hup, 750: accepted icall */
 259struct reply_t gigaset_tab_cid[] =
 260{
 261/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
 262 * action, command */
 263
 264/* dial */
 265        {EV_DIAL,        -1,  -1, -1,            -1, -1, {ACT_DIAL} },
 266        {RSP_INIT,        0,   0, SEQ_DIAL,     601,  5, {ACT_CMD + AT_BC} },
 267        {RSP_OK,        601, 601, -1,           603,  5, {ACT_CMD + AT_PROTO} },
 268        {RSP_OK,        603, 603, -1,           604,  5, {ACT_CMD + AT_TYPE} },
 269        {RSP_OK,        604, 604, -1,           605,  5, {ACT_CMD + AT_MSN} },
 270        {RSP_NULL,      605, 605, -1,           606,  5, {ACT_CMD + AT_CLIP} },
 271        {RSP_OK,        605, 605, -1,           606,  5, {ACT_CMD + AT_CLIP} },
 272        {RSP_NULL,      606, 606, -1,           607,  5, {ACT_CMD + AT_ISO} },
 273        {RSP_OK,        606, 606, -1,           607,  5, {ACT_CMD + AT_ISO} },
 274        {RSP_OK,        607, 607, -1,           608,  5, {0},   "+VLS=17\r"},
 275        {RSP_OK,        608, 608, -1,           609, -1},
 276        {RSP_ZSAU,      609, 609, ZSAU_PROCEEDING, 610, 5, {ACT_CMD + AT_DIAL} },
 277        {RSP_OK,        610, 610, -1,           650,  0, {ACT_DIALING} },
 278
 279        {RSP_ERROR,     601, 610, -1,             0,  0, {ACT_ABORTDIAL} },
 280        {EV_TIMEOUT,    601, 610, -1,             0,  0, {ACT_ABORTDIAL} },
 281
 282/* optional dialing responses */
 283        {EV_BC_OPEN,    650, 650, -1,           651, -1},
 284        {RSP_ZVLS,      609, 651, 17,            -1, -1, {ACT_DEBUG} },
 285        {RSP_ZCTP,      610, 651, -1,            -1, -1, {ACT_DEBUG} },
 286        {RSP_ZCPN,      610, 651, -1,            -1, -1, {ACT_DEBUG} },
 287        {RSP_ZSAU,      650, 651, ZSAU_CALL_DELIVERED, -1, -1, {ACT_DEBUG} },
 288
 289/* connect */
 290        {RSP_ZSAU,      650, 650, ZSAU_ACTIVE,  800, -1, {ACT_CONNECT} },
 291        {RSP_ZSAU,      651, 651, ZSAU_ACTIVE,  800, -1, {ACT_CONNECT,
 292                                                          ACT_NOTIFY_BC_UP} },
 293        {RSP_ZSAU,      750, 750, ZSAU_ACTIVE,  800, -1, {ACT_CONNECT} },
 294        {RSP_ZSAU,      751, 751, ZSAU_ACTIVE,  800, -1, {ACT_CONNECT,
 295                                                          ACT_NOTIFY_BC_UP} },
 296        {EV_BC_OPEN,    800, 800, -1,           800, -1, {ACT_NOTIFY_BC_UP} },
 297
 298/* remote hangup */
 299        {RSP_ZSAU,      650, 651, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT} },
 300        {RSP_ZSAU,      750, 751, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
 301        {RSP_ZSAU,      800, 800, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
 302
 303/* hangup */
 304        {EV_HUP,         -1,  -1, -1,            -1, -1, {ACT_HUP} },
 305        {RSP_INIT,       -1,  -1, SEQ_HUP,      401,  5, {0},   "+VLS=0\r"},
 306        {RSP_OK,        401, 401, -1,           402,  5},
 307        {RSP_ZVLS,      402, 402,  0,           403,  5},
 308        {RSP_ZSAU,      403, 403, ZSAU_DISCONNECT_REQ, -1, -1, {ACT_DEBUG} },
 309        {RSP_ZSAU,      403, 403, ZSAU_NULL,      0,  0, {ACT_DISCONNECT} },
 310        {RSP_NODEV,     401, 403, -1,             0,  0, {ACT_FAKEHUP} },
 311        {RSP_ERROR,     401, 401, -1,             0,  0, {ACT_ABORTHUP} },
 312        {EV_TIMEOUT,    401, 403, -1,             0,  0, {ACT_ABORTHUP} },
 313
 314        {EV_BC_CLOSED,    0,   0, -1,             0, -1, {ACT_NOTIFY_BC_DOWN} },
 315
 316/* ring */
 317        {RSP_ZBC,       700, 700, -1,            -1, -1, {0} },
 318        {RSP_ZHLC,      700, 700, -1,            -1, -1, {0} },
 319        {RSP_NMBR,      700, 700, -1,            -1, -1, {0} },
 320        {RSP_ZCPN,      700, 700, -1,            -1, -1, {0} },
 321        {RSP_ZCTP,      700, 700, -1,            -1, -1, {0} },
 322        {EV_TIMEOUT,    700, 700, -1,           720, 720, {ACT_ICALL} },
 323        {EV_BC_CLOSED,  720, 720, -1,             0, -1, {ACT_NOTIFY_BC_DOWN} },
 324
 325/*accept icall*/
 326        {EV_ACCEPT,      -1,  -1, -1,            -1, -1, {ACT_ACCEPT} },
 327        {RSP_INIT,      720, 720, SEQ_ACCEPT,   721,  5, {ACT_CMD + AT_PROTO} },
 328        {RSP_OK,        721, 721, -1,           722,  5, {ACT_CMD + AT_ISO} },
 329        {RSP_OK,        722, 722, -1,           723,  5, {0},   "+VLS=17\r"},
 330        {RSP_OK,        723, 723, -1,           724,  5, {0} },
 331        {RSP_ZVLS,      724, 724, 17,           750, 50, {ACT_ACCEPTED} },
 332        {RSP_ERROR,     721, 729, -1,             0,  0, {ACT_ABORTACCEPT} },
 333        {EV_TIMEOUT,    721, 729, -1,             0,  0, {ACT_ABORTACCEPT} },
 334        {RSP_ZSAU,      700, 729, ZSAU_NULL,      0,  0, {ACT_ABORTACCEPT} },
 335        {RSP_ZSAU,      700, 729, ZSAU_ACTIVE,    0,  0, {ACT_ABORTACCEPT} },
 336        {RSP_ZSAU,      700, 729, ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT} },
 337
 338        {EV_BC_OPEN,    750, 750, -1,           751, -1},
 339        {EV_TIMEOUT,    750, 751, -1,             0,  0, {ACT_CONNTIMEOUT} },
 340
 341/* B channel closed (general case) */
 342        {EV_BC_CLOSED,   -1,  -1, -1,            -1, -1, {ACT_NOTIFY_BC_DOWN} },
 343
 344/* misc. */
 345        {RSP_ZCON,       -1,  -1, -1,            -1, -1, {ACT_DEBUG} },
 346        {RSP_ZCAU,       -1,  -1, -1,            -1, -1, {ACT_ZCAU} },
 347        {RSP_NONE,       -1,  -1, -1,            -1, -1, {ACT_DEBUG} },
 348        {RSP_ANY,        -1,  -1, -1,            -1, -1, {ACT_WARN} },
 349        {RSP_LAST}
 350};
 351
 352
 353static const struct resp_type_t {
 354        char    *response;
 355        int     resp_code;
 356        int     type;
 357}
 358resp_type[] =
 359{
 360        {"OK",          RSP_OK,         RT_NOTHING},
 361        {"ERROR",       RSP_ERROR,      RT_NOTHING},
 362        {"ZSAU",        RSP_ZSAU,       RT_ZSAU},
 363        {"ZCAU",        RSP_ZCAU,       RT_ZCAU},
 364        {"RING",        RSP_RING,       RT_RING},
 365        {"ZGCI",        RSP_ZGCI,       RT_NUMBER},
 366        {"ZVLS",        RSP_ZVLS,       RT_NUMBER},
 367        {"ZCTP",        RSP_ZCTP,       RT_NUMBER},
 368        {"ZDLE",        RSP_ZDLE,       RT_NUMBER},
 369        {"ZHLC",        RSP_ZHLC,       RT_STRING},
 370        {"ZBC",         RSP_ZBC,        RT_STRING},
 371        {"NMBR",        RSP_NMBR,       RT_STRING},
 372        {"ZCPN",        RSP_ZCPN,       RT_STRING},
 373        {"ZCON",        RSP_ZCON,       RT_STRING},
 374        {NULL,          0,              0}
 375};
 376
 377static const struct zsau_resp_t {
 378        char    *str;
 379        int     code;
 380}
 381zsau_resp[] =
 382{
 383        {"OUTGOING_CALL_PROCEEDING",    ZSAU_PROCEEDING},
 384        {"CALL_DELIVERED",              ZSAU_CALL_DELIVERED},
 385        {"ACTIVE",                      ZSAU_ACTIVE},
 386        {"DISCONNECT_IND",              ZSAU_DISCONNECT_IND},
 387        {"NULL",                        ZSAU_NULL},
 388        {"DISCONNECT_REQ",              ZSAU_DISCONNECT_REQ},
 389        {NULL,                          ZSAU_UNKNOWN}
 390};
 391
 392/* retrieve CID from parsed response
 393 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
 394 */
 395static int cid_of_response(char *s)
 396{
 397        int cid;
 398        int rc;
 399
 400        if (s[-1] != ';')
 401                return 0;       /* no CID separator */
 402        rc = kstrtoint(s, 10, &cid);
 403        if (rc)
 404                return 0;       /* CID not numeric */
 405        if (cid < 1 || cid > 65535)
 406                return -1;      /* CID out of range */
 407        return cid;
 408}
 409
 410/**
 411 * gigaset_handle_modem_response() - process received modem response
 412 * @cs:         device descriptor structure.
 413 *
 414 * Called by asyncdata/isocdata if a block of data received from the
 415 * device must be processed as a modem command response. The data is
 416 * already in the cs structure.
 417 */
 418void gigaset_handle_modem_response(struct cardstate *cs)
 419{
 420        unsigned char *argv[MAX_REC_PARAMS + 1];
 421        int params;
 422        int i, j;
 423        const struct resp_type_t *rt;
 424        const struct zsau_resp_t *zr;
 425        int curarg;
 426        unsigned long flags;
 427        unsigned next, tail, head;
 428        struct event_t *event;
 429        int resp_code;
 430        int param_type;
 431        int abort;
 432        size_t len;
 433        int cid;
 434        int rawstring;
 435
 436        len = cs->cbytes;
 437        if (!len) {
 438                /* ignore additional LFs/CRs (M10x config mode or cx100) */
 439                gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[0]);
 440                return;
 441        }
 442        cs->respdata[len] = 0;
 443        argv[0] = cs->respdata;
 444        params = 1;
 445        if (cs->at_state.getstring) {
 446                /* getstring only allowed without cid at the moment */
 447                cs->at_state.getstring = 0;
 448                rawstring = 1;
 449                cid = 0;
 450        } else {
 451                /* parse line */
 452                for (i = 0; i < len; i++)
 453                        switch (cs->respdata[i]) {
 454                        case ';':
 455                        case ',':
 456                        case '=':
 457                                if (params > MAX_REC_PARAMS) {
 458                                        dev_warn(cs->dev,
 459                                                 "too many parameters in response\n");
 460                                        /* need last parameter (might be CID) */
 461                                        params--;
 462                                }
 463                                argv[params++] = cs->respdata + i + 1;
 464                        }
 465
 466                rawstring = 0;
 467                cid = params > 1 ? cid_of_response(argv[params - 1]) : 0;
 468                if (cid < 0) {
 469                        gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
 470                                          NULL, 0, NULL);
 471                        return;
 472                }
 473
 474                for (j = 1; j < params; ++j)
 475                        argv[j][-1] = 0;
 476
 477                gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]);
 478                if (cid) {
 479                        --params;
 480                        gig_dbg(DEBUG_EVENT, "CID: %s", argv[params]);
 481                }
 482                gig_dbg(DEBUG_EVENT, "available params: %d", params - 1);
 483                for (j = 1; j < params; j++)
 484                        gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]);
 485        }
 486
 487        spin_lock_irqsave(&cs->ev_lock, flags);
 488        head = cs->ev_head;
 489        tail = cs->ev_tail;
 490
 491        abort = 1;
 492        curarg = 0;
 493        while (curarg < params) {
 494                next = (tail + 1) % MAX_EVENTS;
 495                if (unlikely(next == head)) {
 496                        dev_err(cs->dev, "event queue full\n");
 497                        break;
 498                }
 499
 500                event = cs->events + tail;
 501                event->at_state = NULL;
 502                event->cid = cid;
 503                event->ptr = NULL;
 504                event->arg = NULL;
 505                tail = next;
 506
 507                if (rawstring) {
 508                        resp_code = RSP_STRING;
 509                        param_type = RT_STRING;
 510                } else {
 511                        for (rt = resp_type; rt->response; ++rt)
 512                                if (!strcmp(argv[curarg], rt->response))
 513                                        break;
 514
 515                        if (!rt->response) {
 516                                event->type = RSP_NONE;
 517                                gig_dbg(DEBUG_EVENT,
 518                                        "unknown modem response: '%s'\n",
 519                                        argv[curarg]);
 520                                break;
 521                        }
 522
 523                        resp_code = rt->resp_code;
 524                        param_type = rt->type;
 525                        ++curarg;
 526                }
 527
 528                event->type = resp_code;
 529
 530                switch (param_type) {
 531                case RT_NOTHING:
 532                        break;
 533                case RT_RING:
 534                        if (!cid) {
 535                                dev_err(cs->dev,
 536                                        "received RING without CID!\n");
 537                                event->type = RSP_INVAL;
 538                                abort = 1;
 539                        } else {
 540                                event->cid = 0;
 541                                event->parameter = cid;
 542                                abort = 0;
 543                        }
 544                        break;
 545                case RT_ZSAU:
 546                        if (curarg >= params) {
 547                                event->parameter = ZSAU_NONE;
 548                                break;
 549                        }
 550                        for (zr = zsau_resp; zr->str; ++zr)
 551                                if (!strcmp(argv[curarg], zr->str))
 552                                        break;
 553                        event->parameter = zr->code;
 554                        if (!zr->str)
 555                                dev_warn(cs->dev,
 556                                         "%s: unknown parameter %s after ZSAU\n",
 557                                         __func__, argv[curarg]);
 558                        ++curarg;
 559                        break;
 560                case RT_STRING:
 561                        if (curarg < params) {
 562                                event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
 563                                if (!event->ptr)
 564                                        dev_err(cs->dev, "out of memory\n");
 565                                ++curarg;
 566                        }
 567                        gig_dbg(DEBUG_EVENT, "string==%s",
 568                                event->ptr ? (char *) event->ptr : "NULL");
 569                        break;
 570                case RT_ZCAU:
 571                        event->parameter = -1;
 572                        if (curarg + 1 < params) {
 573                                u8 type, value;
 574
 575                                i = kstrtou8(argv[curarg++], 16, &type);
 576                                j = kstrtou8(argv[curarg++], 16, &value);
 577                                if (i == 0 && j == 0)
 578                                        event->parameter = (type << 8) | value;
 579                        } else
 580                                curarg = params - 1;
 581                        break;
 582                case RT_NUMBER:
 583                        if (curarg >= params ||
 584                            kstrtoint(argv[curarg++], 10, &event->parameter))
 585                                event->parameter = -1;
 586                        gig_dbg(DEBUG_EVENT, "parameter==%d", event->parameter);
 587                        break;
 588                }
 589
 590                if (resp_code == RSP_ZDLE)
 591                        cs->dle = event->parameter;
 592
 593                if (abort)
 594                        break;
 595        }
 596
 597        cs->ev_tail = tail;
 598        spin_unlock_irqrestore(&cs->ev_lock, flags);
 599
 600        if (curarg != params)
 601                gig_dbg(DEBUG_EVENT,
 602                        "invalid number of processed parameters: %d/%d",
 603                        curarg, params);
 604}
 605EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
 606
 607/* disconnect_nobc
 608 * process closing of connection associated with given AT state structure
 609 * without B channel
 610 */
 611static void disconnect_nobc(struct at_state_t **at_state_p,
 612                            struct cardstate *cs)
 613{
 614        unsigned long flags;
 615
 616        spin_lock_irqsave(&cs->lock, flags);
 617        ++(*at_state_p)->seq_index;
 618
 619        /* revert to selected idle mode */
 620        if (!cs->cidmode) {
 621                cs->at_state.pending_commands |= PC_UMMODE;
 622                gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
 623                cs->commands_pending = 1;
 624        }
 625
 626        /* check for and deallocate temporary AT state */
 627        if (!list_empty(&(*at_state_p)->list)) {
 628                list_del(&(*at_state_p)->list);
 629                kfree(*at_state_p);
 630                *at_state_p = NULL;
 631        }
 632
 633        spin_unlock_irqrestore(&cs->lock, flags);
 634}
 635
 636/* disconnect_bc
 637 * process closing of connection associated with given AT state structure
 638 * and B channel
 639 */
 640static void disconnect_bc(struct at_state_t *at_state,
 641                          struct cardstate *cs, struct bc_state *bcs)
 642{
 643        unsigned long flags;
 644
 645        spin_lock_irqsave(&cs->lock, flags);
 646        ++at_state->seq_index;
 647
 648        /* revert to selected idle mode */
 649        if (!cs->cidmode) {
 650                cs->at_state.pending_commands |= PC_UMMODE;
 651                gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
 652                cs->commands_pending = 1;
 653        }
 654        spin_unlock_irqrestore(&cs->lock, flags);
 655
 656        /* invoke hardware specific handler */
 657        cs->ops->close_bchannel(bcs);
 658
 659        /* notify LL */
 660        if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
 661                bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
 662                gigaset_isdn_hupD(bcs);
 663        }
 664}
 665
 666/* get_free_channel
 667 * get a free AT state structure: either one of those associated with the
 668 * B channels of the Gigaset device, or if none of those is available,
 669 * a newly allocated one with bcs=NULL
 670 * The structure should be freed by calling disconnect_nobc() after use.
 671 */
 672static inline struct at_state_t *get_free_channel(struct cardstate *cs,
 673                                                  int cid)
 674/* cids: >0: siemens-cid
 675 *        0: without cid
 676 *       -1: no cid assigned yet
 677 */
 678{
 679        unsigned long flags;
 680        int i;
 681        struct at_state_t *ret;
 682
 683        for (i = 0; i < cs->channels; ++i)
 684                if (gigaset_get_channel(cs->bcs + i) >= 0) {
 685                        ret = &cs->bcs[i].at_state;
 686                        ret->cid = cid;
 687                        return ret;
 688                }
 689
 690        spin_lock_irqsave(&cs->lock, flags);
 691        ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
 692        if (ret) {
 693                gigaset_at_init(ret, NULL, cs, cid);
 694                list_add(&ret->list, &cs->temp_at_states);
 695        }
 696        spin_unlock_irqrestore(&cs->lock, flags);
 697        return ret;
 698}
 699
 700static void init_failed(struct cardstate *cs, int mode)
 701{
 702        int i;
 703        struct at_state_t *at_state;
 704
 705        cs->at_state.pending_commands &= ~PC_INIT;
 706        cs->mode = mode;
 707        cs->mstate = MS_UNINITIALIZED;
 708        gigaset_free_channels(cs);
 709        for (i = 0; i < cs->channels; ++i) {
 710                at_state = &cs->bcs[i].at_state;
 711                if (at_state->pending_commands & PC_CID) {
 712                        at_state->pending_commands &= ~PC_CID;
 713                        at_state->pending_commands |= PC_NOCID;
 714                        cs->commands_pending = 1;
 715                }
 716        }
 717}
 718
 719static void schedule_init(struct cardstate *cs, int state)
 720{
 721        if (cs->at_state.pending_commands & PC_INIT) {
 722                gig_dbg(DEBUG_EVENT, "not scheduling PC_INIT again");
 723                return;
 724        }
 725        cs->mstate = state;
 726        cs->mode = M_UNKNOWN;
 727        gigaset_block_channels(cs);
 728        cs->at_state.pending_commands |= PC_INIT;
 729        gig_dbg(DEBUG_EVENT, "Scheduling PC_INIT");
 730        cs->commands_pending = 1;
 731}
 732
 733/* send an AT command
 734 * adding the "AT" prefix, cid and DLE encapsulation as appropriate
 735 */
 736static void send_command(struct cardstate *cs, const char *cmd,
 737                         struct at_state_t *at_state)
 738{
 739        int cid = at_state->cid;
 740        struct cmdbuf_t *cb;
 741        size_t buflen;
 742
 743        buflen = strlen(cmd) + 12; /* DLE ( A T 1 2 3 4 5 <cmd> DLE ) \0 */
 744        cb = kmalloc(sizeof(struct cmdbuf_t) + buflen, GFP_ATOMIC);
 745        if (!cb) {
 746                dev_err(cs->dev, "%s: out of memory\n", __func__);
 747                return;
 748        }
 749        if (cid > 0 && cid <= 65535)
 750                cb->len = snprintf(cb->buf, buflen,
 751                                   cs->dle ? "\020(AT%d%s\020)" : "AT%d%s",
 752                                   cid, cmd);
 753        else
 754                cb->len = snprintf(cb->buf, buflen,
 755                                   cs->dle ? "\020(AT%s\020)" : "AT%s",
 756                                   cmd);
 757        cb->offset = 0;
 758        cb->next = NULL;
 759        cb->wake_tasklet = NULL;
 760        cs->ops->write_cmd(cs, cb);
 761}
 762
 763static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
 764{
 765        struct at_state_t *at_state;
 766        int i;
 767        unsigned long flags;
 768
 769        if (cid == 0)
 770                return &cs->at_state;
 771
 772        for (i = 0; i < cs->channels; ++i)
 773                if (cid == cs->bcs[i].at_state.cid)
 774                        return &cs->bcs[i].at_state;
 775
 776        spin_lock_irqsave(&cs->lock, flags);
 777
 778        list_for_each_entry(at_state, &cs->temp_at_states, list)
 779                if (cid == at_state->cid) {
 780                        spin_unlock_irqrestore(&cs->lock, flags);
 781                        return at_state;
 782                }
 783
 784        spin_unlock_irqrestore(&cs->lock, flags);
 785
 786        return NULL;
 787}
 788
 789static void bchannel_down(struct bc_state *bcs)
 790{
 791        if (bcs->chstate & CHS_B_UP) {
 792                bcs->chstate &= ~CHS_B_UP;
 793                gigaset_isdn_hupB(bcs);
 794        }
 795
 796        if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
 797                bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
 798                gigaset_isdn_hupD(bcs);
 799        }
 800
 801        gigaset_free_channel(bcs);
 802
 803        gigaset_bcs_reinit(bcs);
 804}
 805
 806static void bchannel_up(struct bc_state *bcs)
 807{
 808        if (bcs->chstate & CHS_B_UP) {
 809                dev_notice(bcs->cs->dev, "%s: B channel already up\n",
 810                           __func__);
 811                return;
 812        }
 813
 814        bcs->chstate |= CHS_B_UP;
 815        gigaset_isdn_connB(bcs);
 816}
 817
 818static void start_dial(struct at_state_t *at_state, void *data,
 819                       unsigned seq_index)
 820{
 821        struct bc_state *bcs = at_state->bcs;
 822        struct cardstate *cs = at_state->cs;
 823        char **commands = data;
 824        unsigned long flags;
 825        int i;
 826
 827        bcs->chstate |= CHS_NOTIFY_LL;
 828
 829        spin_lock_irqsave(&cs->lock, flags);
 830        if (at_state->seq_index != seq_index) {
 831                spin_unlock_irqrestore(&cs->lock, flags);
 832                goto error;
 833        }
 834        spin_unlock_irqrestore(&cs->lock, flags);
 835
 836        for (i = 0; i < AT_NUM; ++i) {
 837                kfree(bcs->commands[i]);
 838                bcs->commands[i] = commands[i];
 839        }
 840
 841        at_state->pending_commands |= PC_CID;
 842        gig_dbg(DEBUG_EVENT, "Scheduling PC_CID");
 843        cs->commands_pending = 1;
 844        return;
 845
 846error:
 847        for (i = 0; i < AT_NUM; ++i) {
 848                kfree(commands[i]);
 849                commands[i] = NULL;
 850        }
 851        at_state->pending_commands |= PC_NOCID;
 852        gig_dbg(DEBUG_EVENT, "Scheduling PC_NOCID");
 853        cs->commands_pending = 1;
 854        return;
 855}
 856
 857static void start_accept(struct at_state_t *at_state)
 858{
 859        struct cardstate *cs = at_state->cs;
 860        struct bc_state *bcs = at_state->bcs;
 861        int i;
 862
 863        for (i = 0; i < AT_NUM; ++i) {
 864                kfree(bcs->commands[i]);
 865                bcs->commands[i] = NULL;
 866        }
 867
 868        bcs->commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
 869        bcs->commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
 870        if (!bcs->commands[AT_PROTO] || !bcs->commands[AT_ISO]) {
 871                dev_err(at_state->cs->dev, "out of memory\n");
 872                /* error reset */
 873                at_state->pending_commands |= PC_HUP;
 874                gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
 875                cs->commands_pending = 1;
 876                return;
 877        }
 878
 879        snprintf(bcs->commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
 880        snprintf(bcs->commands[AT_ISO], 9, "^SISO=%u\r", bcs->channel + 1);
 881
 882        at_state->pending_commands |= PC_ACCEPT;
 883        gig_dbg(DEBUG_EVENT, "Scheduling PC_ACCEPT");
 884        cs->commands_pending = 1;
 885}
 886
 887static void do_start(struct cardstate *cs)
 888{
 889        gigaset_free_channels(cs);
 890
 891        if (cs->mstate != MS_LOCKED)
 892                schedule_init(cs, MS_INIT);
 893
 894        cs->isdn_up = 1;
 895        gigaset_isdn_start(cs);
 896
 897        cs->waiting = 0;
 898        wake_up(&cs->waitqueue);
 899}
 900
 901static void finish_shutdown(struct cardstate *cs)
 902{
 903        if (cs->mstate != MS_LOCKED) {
 904                cs->mstate = MS_UNINITIALIZED;
 905                cs->mode = M_UNKNOWN;
 906        }
 907
 908        /* Tell the LL that the device is not available .. */
 909        if (cs->isdn_up) {
 910                cs->isdn_up = 0;
 911                gigaset_isdn_stop(cs);
 912        }
 913
 914        /* The rest is done by cleanup_cs() in process context. */
 915
 916        cs->cmd_result = -ENODEV;
 917        cs->waiting = 0;
 918        wake_up(&cs->waitqueue);
 919}
 920
 921static void do_shutdown(struct cardstate *cs)
 922{
 923        gigaset_block_channels(cs);
 924
 925        if (cs->mstate == MS_READY) {
 926                cs->mstate = MS_SHUTDOWN;
 927                cs->at_state.pending_commands |= PC_SHUTDOWN;
 928                gig_dbg(DEBUG_EVENT, "Scheduling PC_SHUTDOWN");
 929                cs->commands_pending = 1;
 930        } else
 931                finish_shutdown(cs);
 932}
 933
 934static void do_stop(struct cardstate *cs)
 935{
 936        unsigned long flags;
 937
 938        spin_lock_irqsave(&cs->lock, flags);
 939        cs->connected = 0;
 940        spin_unlock_irqrestore(&cs->lock, flags);
 941
 942        do_shutdown(cs);
 943}
 944
 945/* Entering cid mode or getting a cid failed:
 946 * try to initialize the device and try again.
 947 *
 948 * channel >= 0: getting cid for the channel failed
 949 * channel < 0:  entering cid mode failed
 950 *
 951 * returns 0 on success, <0 on failure
 952 */
 953static int reinit_and_retry(struct cardstate *cs, int channel)
 954{
 955        int i;
 956
 957        if (--cs->retry_count <= 0)
 958                return -EFAULT;
 959
 960        for (i = 0; i < cs->channels; ++i)
 961                if (cs->bcs[i].at_state.cid > 0)
 962                        return -EBUSY;
 963
 964        if (channel < 0)
 965                dev_warn(cs->dev,
 966                         "Could not enter cid mode. Reinit device and try again.\n");
 967        else {
 968                dev_warn(cs->dev,
 969                         "Could not get a call id. Reinit device and try again.\n");
 970                cs->bcs[channel].at_state.pending_commands |= PC_CID;
 971        }
 972        schedule_init(cs, MS_INIT);
 973        return 0;
 974}
 975
 976static int at_state_invalid(struct cardstate *cs,
 977                            struct at_state_t *test_ptr)
 978{
 979        unsigned long flags;
 980        unsigned channel;
 981        struct at_state_t *at_state;
 982        int retval = 0;
 983
 984        spin_lock_irqsave(&cs->lock, flags);
 985
 986        if (test_ptr == &cs->at_state)
 987                goto exit;
 988
 989        list_for_each_entry(at_state, &cs->temp_at_states, list)
 990                if (at_state == test_ptr)
 991                        goto exit;
 992
 993        for (channel = 0; channel < cs->channels; ++channel)
 994                if (&cs->bcs[channel].at_state == test_ptr)
 995                        goto exit;
 996
 997        retval = 1;
 998exit:
 999        spin_unlock_irqrestore(&cs->lock, flags);
1000        return retval;
1001}
1002
1003static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
1004                         struct at_state_t *at_state)
1005{
1006        int retval;
1007
1008        retval = gigaset_isdn_icall(at_state);
1009        switch (retval) {
1010        case ICALL_ACCEPT:
1011                break;
1012        default:
1013                dev_err(cs->dev, "internal error: disposition=%d\n", retval);
1014                /* --v-- fall through --v-- */
1015        case ICALL_IGNORE:
1016        case ICALL_REJECT:
1017                /* hang up actively
1018                 * Device doc says that would reject the call.
1019                 * In fact it doesn't.
1020                 */
1021                at_state->pending_commands |= PC_HUP;
1022                cs->commands_pending = 1;
1023                break;
1024        }
1025}
1026
1027static int do_lock(struct cardstate *cs)
1028{
1029        int mode;
1030        int i;
1031
1032        switch (cs->mstate) {
1033        case MS_UNINITIALIZED:
1034        case MS_READY:
1035                if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1036                    cs->at_state.pending_commands)
1037                        return -EBUSY;
1038
1039                for (i = 0; i < cs->channels; ++i)
1040                        if (cs->bcs[i].at_state.pending_commands)
1041                                return -EBUSY;
1042
1043                if (gigaset_get_channels(cs) < 0)
1044                        return -EBUSY;
1045
1046                break;
1047        case MS_LOCKED:
1048                break;
1049        default:
1050                return -EBUSY;
1051        }
1052
1053        mode = cs->mode;
1054        cs->mstate = MS_LOCKED;
1055        cs->mode = M_UNKNOWN;
1056
1057        return mode;
1058}
1059
1060static int do_unlock(struct cardstate *cs)
1061{
1062        if (cs->mstate != MS_LOCKED)
1063                return -EINVAL;
1064
1065        cs->mstate = MS_UNINITIALIZED;
1066        cs->mode = M_UNKNOWN;
1067        gigaset_free_channels(cs);
1068        if (cs->connected)
1069                schedule_init(cs, MS_INIT);
1070
1071        return 0;
1072}
1073
1074static void do_action(int action, struct cardstate *cs,
1075                      struct bc_state *bcs,
1076                      struct at_state_t **p_at_state, char **pp_command,
1077                      int *p_genresp, int *p_resp_code,
1078                      struct event_t *ev)
1079{
1080        struct at_state_t *at_state = *p_at_state;
1081        struct bc_state *bcs2;
1082        unsigned long flags;
1083
1084        int channel;
1085
1086        unsigned char *s, *e;
1087        int i;
1088        unsigned long val;
1089
1090        switch (action) {
1091        case ACT_NOTHING:
1092                break;
1093        case ACT_TIMEOUT:
1094                at_state->waiting = 1;
1095                break;
1096        case ACT_INIT:
1097                cs->at_state.pending_commands &= ~PC_INIT;
1098                cs->cur_at_seq = SEQ_NONE;
1099                cs->mode = M_UNIMODEM;
1100                spin_lock_irqsave(&cs->lock, flags);
1101                if (!cs->cidmode) {
1102                        spin_unlock_irqrestore(&cs->lock, flags);
1103                        gigaset_free_channels(cs);
1104                        cs->mstate = MS_READY;
1105                        break;
1106                }
1107                spin_unlock_irqrestore(&cs->lock, flags);
1108                cs->at_state.pending_commands |= PC_CIDMODE;
1109                gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1110                cs->commands_pending = 1;
1111                break;
1112        case ACT_FAILINIT:
1113                dev_warn(cs->dev, "Could not initialize the device.\n");
1114                cs->dle = 0;
1115                init_failed(cs, M_UNKNOWN);
1116                cs->cur_at_seq = SEQ_NONE;
1117                break;
1118        case ACT_CONFIGMODE:
1119                init_failed(cs, M_CONFIG);
1120                cs->cur_at_seq = SEQ_NONE;
1121                break;
1122        case ACT_SETDLE1:
1123                cs->dle = 1;
1124                /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1125                cs->inbuf[0].inputstate &=
1126                        ~(INS_command | INS_DLE_command);
1127                break;
1128        case ACT_SETDLE0:
1129                cs->dle = 0;
1130                cs->inbuf[0].inputstate =
1131                        (cs->inbuf[0].inputstate & ~INS_DLE_command)
1132                        | INS_command;
1133                break;
1134        case ACT_CMODESET:
1135                if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1136                        gigaset_free_channels(cs);
1137                        cs->mstate = MS_READY;
1138                }
1139                cs->mode = M_CID;
1140                cs->cur_at_seq = SEQ_NONE;
1141                break;
1142        case ACT_UMODESET:
1143                cs->mode = M_UNIMODEM;
1144                cs->cur_at_seq = SEQ_NONE;
1145                break;
1146        case ACT_FAILCMODE:
1147                cs->cur_at_seq = SEQ_NONE;
1148                if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1149                        init_failed(cs, M_UNKNOWN);
1150                        break;
1151                }
1152                if (reinit_and_retry(cs, -1) < 0)
1153                        schedule_init(cs, MS_RECOVER);
1154                break;
1155        case ACT_FAILUMODE:
1156                cs->cur_at_seq = SEQ_NONE;
1157                schedule_init(cs, MS_RECOVER);
1158                break;
1159        case ACT_HUPMODEM:
1160                /* send "+++" (hangup in unimodem mode) */
1161                if (cs->connected) {
1162                        struct cmdbuf_t *cb;
1163
1164                        cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC);
1165                        if (!cb) {
1166                                dev_err(cs->dev, "%s: out of memory\n",
1167                                        __func__);
1168                                return;
1169                        }
1170                        memcpy(cb->buf, "+++", 3);
1171                        cb->len = 3;
1172                        cb->offset = 0;
1173                        cb->next = NULL;
1174                        cb->wake_tasklet = NULL;
1175                        cs->ops->write_cmd(cs, cb);
1176                }
1177                break;
1178        case ACT_RING:
1179                /* get fresh AT state structure for new CID */
1180                at_state = get_free_channel(cs, ev->parameter);
1181                if (!at_state) {
1182                        dev_warn(cs->dev,
1183                                 "RING ignored: could not allocate channel structure\n");
1184                        break;
1185                }
1186
1187                /* initialize AT state structure
1188                 * note that bcs may be NULL if no B channel is free
1189                 */
1190                at_state->ConState = 700;
1191                for (i = 0; i < STR_NUM; ++i) {
1192                        kfree(at_state->str_var[i]);
1193                        at_state->str_var[i] = NULL;
1194                }
1195                at_state->int_var[VAR_ZCTP] = -1;
1196
1197                spin_lock_irqsave(&cs->lock, flags);
1198                at_state->timer_expires = RING_TIMEOUT;
1199                at_state->timer_active = 1;
1200                spin_unlock_irqrestore(&cs->lock, flags);
1201                break;
1202        case ACT_ICALL:
1203                handle_icall(cs, bcs, at_state);
1204                break;
1205        case ACT_FAILSDOWN:
1206                dev_warn(cs->dev, "Could not shut down the device.\n");
1207                /* fall through */
1208        case ACT_FAKESDOWN:
1209        case ACT_SDOWN:
1210                cs->cur_at_seq = SEQ_NONE;
1211                finish_shutdown(cs);
1212                break;
1213        case ACT_CONNECT:
1214                if (cs->onechannel) {
1215                        at_state->pending_commands |= PC_DLE1;
1216                        cs->commands_pending = 1;
1217                        break;
1218                }
1219                bcs->chstate |= CHS_D_UP;
1220                gigaset_isdn_connD(bcs);
1221                cs->ops->init_bchannel(bcs);
1222                break;
1223        case ACT_DLE1:
1224                cs->cur_at_seq = SEQ_NONE;
1225                bcs = cs->bcs + cs->curchannel;
1226
1227                bcs->chstate |= CHS_D_UP;
1228                gigaset_isdn_connD(bcs);
1229                cs->ops->init_bchannel(bcs);
1230                break;
1231        case ACT_FAKEHUP:
1232                at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1233                /* fall through */
1234        case ACT_DISCONNECT:
1235                cs->cur_at_seq = SEQ_NONE;
1236                at_state->cid = -1;
1237                if (!bcs) {
1238                        disconnect_nobc(p_at_state, cs);
1239                } else if (cs->onechannel && cs->dle) {
1240                        /* Check for other open channels not needed:
1241                         * DLE only used for M10x with one B channel.
1242                         */
1243                        at_state->pending_commands |= PC_DLE0;
1244                        cs->commands_pending = 1;
1245                } else {
1246                        disconnect_bc(at_state, cs, bcs);
1247                }
1248                break;
1249        case ACT_FAKEDLE0:
1250                at_state->int_var[VAR_ZDLE] = 0;
1251                cs->dle = 0;
1252                /* fall through */
1253        case ACT_DLE0:
1254                cs->cur_at_seq = SEQ_NONE;
1255                bcs2 = cs->bcs + cs->curchannel;
1256                disconnect_bc(&bcs2->at_state, cs, bcs2);
1257                break;
1258        case ACT_ABORTHUP:
1259                cs->cur_at_seq = SEQ_NONE;
1260                dev_warn(cs->dev, "Could not hang up.\n");
1261                at_state->cid = -1;
1262                if (!bcs)
1263                        disconnect_nobc(p_at_state, cs);
1264                else if (cs->onechannel)
1265                        at_state->pending_commands |= PC_DLE0;
1266                else
1267                        disconnect_bc(at_state, cs, bcs);
1268                schedule_init(cs, MS_RECOVER);
1269                break;
1270        case ACT_FAILDLE0:
1271                cs->cur_at_seq = SEQ_NONE;
1272                dev_warn(cs->dev, "Error leaving DLE mode.\n");
1273                cs->dle = 0;
1274                bcs2 = cs->bcs + cs->curchannel;
1275                disconnect_bc(&bcs2->at_state, cs, bcs2);
1276                schedule_init(cs, MS_RECOVER);
1277                break;
1278        case ACT_FAILDLE1:
1279                cs->cur_at_seq = SEQ_NONE;
1280                dev_warn(cs->dev,
1281                         "Could not enter DLE mode. Trying to hang up.\n");
1282                channel = cs->curchannel;
1283                cs->bcs[channel].at_state.pending_commands |= PC_HUP;
1284                cs->commands_pending = 1;
1285                break;
1286
1287        case ACT_CID: /* got cid; start dialing */
1288                cs->cur_at_seq = SEQ_NONE;
1289                channel = cs->curchannel;
1290                if (ev->parameter > 0 && ev->parameter <= 65535) {
1291                        cs->bcs[channel].at_state.cid = ev->parameter;
1292                        cs->bcs[channel].at_state.pending_commands |=
1293                                PC_DIAL;
1294                        cs->commands_pending = 1;
1295                        break;
1296                }
1297                /* bad cid: fall through */
1298        case ACT_FAILCID:
1299                cs->cur_at_seq = SEQ_NONE;
1300                channel = cs->curchannel;
1301                if (reinit_and_retry(cs, channel) < 0) {
1302                        dev_warn(cs->dev,
1303                                 "Could not get a call ID. Cannot dial.\n");
1304                        bcs2 = cs->bcs + channel;
1305                        disconnect_bc(&bcs2->at_state, cs, bcs2);
1306                }
1307                break;
1308        case ACT_ABORTCID:
1309                cs->cur_at_seq = SEQ_NONE;
1310                bcs2 = cs->bcs + cs->curchannel;
1311                disconnect_bc(&bcs2->at_state, cs, bcs2);
1312                break;
1313
1314        case ACT_DIALING:
1315        case ACT_ACCEPTED:
1316                cs->cur_at_seq = SEQ_NONE;
1317                break;
1318
1319        case ACT_ABORTACCEPT:   /* hangup/error/timeout during ICALL procssng */
1320                if (bcs)
1321                        disconnect_bc(at_state, cs, bcs);
1322                else
1323                        disconnect_nobc(p_at_state, cs);
1324                break;
1325
1326        case ACT_ABORTDIAL:     /* error/timeout during dial preparation */
1327                cs->cur_at_seq = SEQ_NONE;
1328                at_state->pending_commands |= PC_HUP;
1329                cs->commands_pending = 1;
1330                break;
1331
1332        case ACT_REMOTEREJECT:  /* DISCONNECT_IND after dialling */
1333        case ACT_CONNTIMEOUT:   /* timeout waiting for ZSAU=ACTIVE */
1334        case ACT_REMOTEHUP:     /* DISCONNECT_IND with established connection */
1335                at_state->pending_commands |= PC_HUP;
1336                cs->commands_pending = 1;
1337                break;
1338        case ACT_GETSTRING: /* warning: RING, ZDLE, ...
1339                               are not handled properly anymore */
1340                at_state->getstring = 1;
1341                break;
1342        case ACT_SETVER:
1343                if (!ev->ptr) {
1344                        *p_genresp = 1;
1345                        *p_resp_code = RSP_ERROR;
1346                        break;
1347                }
1348                s = ev->ptr;
1349
1350                if (!strcmp(s, "OK")) {
1351                        /* OK without version string: assume old response */
1352                        *p_genresp = 1;
1353                        *p_resp_code = RSP_NONE;
1354                        break;
1355                }
1356
1357                for (i = 0; i < 4; ++i) {
1358                        val = simple_strtoul(s, (char **) &e, 10);
1359                        if (val > INT_MAX || e == s)
1360                                break;
1361                        if (i == 3) {
1362                                if (*e)
1363                                        break;
1364                        } else if (*e != '.')
1365                                break;
1366                        else
1367                                s = e + 1;
1368                        cs->fwver[i] = val;
1369                }
1370                if (i != 4) {
1371                        *p_genresp = 1;
1372                        *p_resp_code = RSP_ERROR;
1373                        break;
1374                }
1375                cs->gotfwver = 0;
1376                break;
1377        case ACT_GOTVER:
1378                if (cs->gotfwver == 0) {
1379                        cs->gotfwver = 1;
1380                        gig_dbg(DEBUG_EVENT,
1381                                "firmware version %02d.%03d.%02d.%02d",
1382                                cs->fwver[0], cs->fwver[1],
1383                                cs->fwver[2], cs->fwver[3]);
1384                        break;
1385                }
1386                /* fall through */
1387        case ACT_FAILVER:
1388                cs->gotfwver = -1;
1389                dev_err(cs->dev, "could not read firmware version.\n");
1390                break;
1391        case ACT_ERROR:
1392                gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d",
1393                        __func__, at_state->ConState);
1394                cs->cur_at_seq = SEQ_NONE;
1395                break;
1396        case ACT_DEBUG:
1397                gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
1398                        __func__, ev->type, at_state->ConState);
1399                break;
1400        case ACT_WARN:
1401                dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1402                         __func__, ev->type, at_state->ConState);
1403                break;
1404        case ACT_ZCAU:
1405                dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1406                         ev->parameter, at_state->ConState);
1407                break;
1408
1409        /* events from the LL */
1410
1411        case ACT_DIAL:
1412                if (!ev->ptr) {
1413                        *p_genresp = 1;
1414                        *p_resp_code = RSP_ERROR;
1415                        break;
1416                }
1417                start_dial(at_state, ev->ptr, ev->parameter);
1418                break;
1419        case ACT_ACCEPT:
1420                start_accept(at_state);
1421                break;
1422        case ACT_HUP:
1423                at_state->pending_commands |= PC_HUP;
1424                gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
1425                cs->commands_pending = 1;
1426                break;
1427
1428        /* hotplug events */
1429
1430        case ACT_STOP:
1431                do_stop(cs);
1432                break;
1433        case ACT_START:
1434                do_start(cs);
1435                break;
1436
1437        /* events from the interface */
1438
1439        case ACT_IF_LOCK:
1440                cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1441                cs->waiting = 0;
1442                wake_up(&cs->waitqueue);
1443                break;
1444        case ACT_IF_VER:
1445                if (ev->parameter != 0)
1446                        cs->cmd_result = -EINVAL;
1447                else if (cs->gotfwver != 1) {
1448                        cs->cmd_result = -ENOENT;
1449                } else {
1450                        memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1451                        cs->cmd_result = 0;
1452                }
1453                cs->waiting = 0;
1454                wake_up(&cs->waitqueue);
1455                break;
1456
1457        /* events from the proc file system */
1458
1459        case ACT_PROC_CIDMODE:
1460                spin_lock_irqsave(&cs->lock, flags);
1461                if (ev->parameter != cs->cidmode) {
1462                        cs->cidmode = ev->parameter;
1463                        if (ev->parameter) {
1464                                cs->at_state.pending_commands |= PC_CIDMODE;
1465                                gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1466                        } else {
1467                                cs->at_state.pending_commands |= PC_UMMODE;
1468                                gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
1469                        }
1470                        cs->commands_pending = 1;
1471                }
1472                spin_unlock_irqrestore(&cs->lock, flags);
1473                cs->waiting = 0;
1474                wake_up(&cs->waitqueue);
1475                break;
1476
1477        /* events from the hardware drivers */
1478
1479        case ACT_NOTIFY_BC_DOWN:
1480                bchannel_down(bcs);
1481                break;
1482        case ACT_NOTIFY_BC_UP:
1483                bchannel_up(bcs);
1484                break;
1485        case ACT_SHUTDOWN:
1486                do_shutdown(cs);
1487                break;
1488
1489
1490        default:
1491                if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1492                        *pp_command = at_state->bcs->commands[action - ACT_CMD];
1493                        if (!*pp_command) {
1494                                *p_genresp = 1;
1495                                *p_resp_code = RSP_NULL;
1496                        }
1497                } else
1498                        dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
1499        }
1500}
1501
1502/* State machine to do the calling and hangup procedure */
1503static void process_event(struct cardstate *cs, struct event_t *ev)
1504{
1505        struct bc_state *bcs;
1506        char *p_command = NULL;
1507        struct reply_t *rep;
1508        int rcode;
1509        int genresp = 0;
1510        int resp_code = RSP_ERROR;
1511        struct at_state_t *at_state;
1512        int index;
1513        int curact;
1514        unsigned long flags;
1515
1516        if (ev->cid >= 0) {
1517                at_state = at_state_from_cid(cs, ev->cid);
1518                if (!at_state) {
1519                        gig_dbg(DEBUG_EVENT, "event %d for invalid cid %d",
1520                                ev->type, ev->cid);
1521                        gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
1522                                          NULL, 0, NULL);
1523                        return;
1524                }
1525        } else {
1526                at_state = ev->at_state;
1527                if (at_state_invalid(cs, at_state)) {
1528                        gig_dbg(DEBUG_EVENT, "event for invalid at_state %p",
1529                                at_state);
1530                        return;
1531                }
1532        }
1533
1534        gig_dbg(DEBUG_EVENT, "connection state %d, event %d",
1535                at_state->ConState, ev->type);
1536
1537        bcs = at_state->bcs;
1538
1539        /* Setting the pointer to the dial array */
1540        rep = at_state->replystruct;
1541
1542        spin_lock_irqsave(&cs->lock, flags);
1543        if (ev->type == EV_TIMEOUT) {
1544                if (ev->parameter != at_state->timer_index
1545                    || !at_state->timer_active) {
1546                        ev->type = RSP_NONE; /* old timeout */
1547                        gig_dbg(DEBUG_EVENT, "old timeout");
1548                } else {
1549                        if (at_state->waiting)
1550                                gig_dbg(DEBUG_EVENT, "stopped waiting");
1551                        else
1552                                gig_dbg(DEBUG_EVENT, "timeout occurred");
1553                }
1554        }
1555        spin_unlock_irqrestore(&cs->lock, flags);
1556
1557        /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1558           or at_state->str_var[STR_XXXX], set it */
1559        if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1560                index = ev->type - RSP_VAR;
1561                at_state->int_var[index] = ev->parameter;
1562        } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1563                index = ev->type - RSP_STR;
1564                kfree(at_state->str_var[index]);
1565                at_state->str_var[index] = ev->ptr;
1566                ev->ptr = NULL; /* prevent process_events() from
1567                                   deallocating ptr */
1568        }
1569
1570        if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1571                at_state->getstring = 0;
1572
1573        /* Search row in dial array which matches modem response and current
1574           constate */
1575        for (;; rep++) {
1576                rcode = rep->resp_code;
1577                if (rcode == RSP_LAST) {
1578                        /* found nothing...*/
1579                        dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1580                                 "resp_code %d in ConState %d!\n",
1581                                 __func__, ev->type, at_state->ConState);
1582                        return;
1583                }
1584                if ((rcode == RSP_ANY || rcode == ev->type)
1585                    && ((int) at_state->ConState >= rep->min_ConState)
1586                    && (rep->max_ConState < 0
1587                        || (int) at_state->ConState <= rep->max_ConState)
1588                    && (rep->parameter < 0 || rep->parameter == ev->parameter))
1589                        break;
1590        }
1591
1592        p_command = rep->command;
1593
1594        at_state->waiting = 0;
1595        for (curact = 0; curact < MAXACT; ++curact) {
1596                /* The row tells us what we should do  ..
1597                 */
1598                do_action(rep->action[curact], cs, bcs, &at_state, &p_command,
1599                          &genresp, &resp_code, ev);
1600                if (!at_state)
1601                        /* at_state destroyed by disconnect */
1602                        return;
1603        }
1604
1605        /* Jump to the next con-state regarding the array */
1606        if (rep->new_ConState >= 0)
1607                at_state->ConState = rep->new_ConState;
1608
1609        if (genresp) {
1610                spin_lock_irqsave(&cs->lock, flags);
1611                at_state->timer_expires = 0;
1612                at_state->timer_active = 0;
1613                spin_unlock_irqrestore(&cs->lock, flags);
1614                gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1615        } else {
1616                /* Send command to modem if not NULL... */
1617                if (p_command) {
1618                        if (cs->connected)
1619                                send_command(cs, p_command, at_state);
1620                        else
1621                                gigaset_add_event(cs, at_state, RSP_NODEV,
1622                                                  NULL, 0, NULL);
1623                }
1624
1625                spin_lock_irqsave(&cs->lock, flags);
1626                if (!rep->timeout) {
1627                        at_state->timer_expires = 0;
1628                        at_state->timer_active = 0;
1629                } else if (rep->timeout > 0) { /* new timeout */
1630                        at_state->timer_expires = rep->timeout * 10;
1631                        at_state->timer_active = 1;
1632                        ++at_state->timer_index;
1633                }
1634                spin_unlock_irqrestore(&cs->lock, flags);
1635        }
1636}
1637
1638static void schedule_sequence(struct cardstate *cs,
1639                              struct at_state_t *at_state, int sequence)
1640{
1641        cs->cur_at_seq = sequence;
1642        gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1643}
1644
1645static void process_command_flags(struct cardstate *cs)
1646{
1647        struct at_state_t *at_state = NULL;
1648        struct bc_state *bcs;
1649        int i;
1650        int sequence;
1651        unsigned long flags;
1652
1653        cs->commands_pending = 0;
1654
1655        if (cs->cur_at_seq) {
1656                gig_dbg(DEBUG_EVENT, "not searching scheduled commands: busy");
1657                return;
1658        }
1659
1660        gig_dbg(DEBUG_EVENT, "searching scheduled commands");
1661
1662        sequence = SEQ_NONE;
1663
1664        /* clear pending_commands and hangup channels on shutdown */
1665        if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1666                cs->at_state.pending_commands &= ~PC_CIDMODE;
1667                for (i = 0; i < cs->channels; ++i) {
1668                        bcs = cs->bcs + i;
1669                        at_state = &bcs->at_state;
1670                        at_state->pending_commands &=
1671                                ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1672                        if (at_state->cid > 0)
1673                                at_state->pending_commands |= PC_HUP;
1674                        if (at_state->pending_commands & PC_CID) {
1675                                at_state->pending_commands |= PC_NOCID;
1676                                at_state->pending_commands &= ~PC_CID;
1677                        }
1678                }
1679        }
1680
1681        /* clear pending_commands and hangup channels on reset */
1682        if (cs->at_state.pending_commands & PC_INIT) {
1683                cs->at_state.pending_commands &= ~PC_CIDMODE;
1684                for (i = 0; i < cs->channels; ++i) {
1685                        bcs = cs->bcs + i;
1686                        at_state = &bcs->at_state;
1687                        at_state->pending_commands &=
1688                                ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1689                        if (at_state->cid > 0)
1690                                at_state->pending_commands |= PC_HUP;
1691                        if (cs->mstate == MS_RECOVER) {
1692                                if (at_state->pending_commands & PC_CID) {
1693                                        at_state->pending_commands |= PC_NOCID;
1694                                        at_state->pending_commands &= ~PC_CID;
1695                                }
1696                        }
1697                }
1698        }
1699
1700        /* only switch back to unimodem mode if no commands are pending and
1701         * no channels are up */
1702        spin_lock_irqsave(&cs->lock, flags);
1703        if (cs->at_state.pending_commands == PC_UMMODE
1704            && !cs->cidmode
1705            && list_empty(&cs->temp_at_states)
1706            && cs->mode == M_CID) {
1707                sequence = SEQ_UMMODE;
1708                at_state = &cs->at_state;
1709                for (i = 0; i < cs->channels; ++i) {
1710                        bcs = cs->bcs + i;
1711                        if (bcs->at_state.pending_commands ||
1712                            bcs->at_state.cid > 0) {
1713                                sequence = SEQ_NONE;
1714                                break;
1715                        }
1716                }
1717        }
1718        spin_unlock_irqrestore(&cs->lock, flags);
1719        cs->at_state.pending_commands &= ~PC_UMMODE;
1720        if (sequence != SEQ_NONE) {
1721                schedule_sequence(cs, at_state, sequence);
1722                return;
1723        }
1724
1725        for (i = 0; i < cs->channels; ++i) {
1726                bcs = cs->bcs + i;
1727                if (bcs->at_state.pending_commands & PC_HUP) {
1728                        if (cs->dle) {
1729                                cs->curchannel = bcs->channel;
1730                                schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1731                                return;
1732                        }
1733                        bcs->at_state.pending_commands &= ~PC_HUP;
1734                        if (bcs->at_state.pending_commands & PC_CID) {
1735                                /* not yet dialing: PC_NOCID is sufficient */
1736                                bcs->at_state.pending_commands |= PC_NOCID;
1737                                bcs->at_state.pending_commands &= ~PC_CID;
1738                        } else {
1739                                schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1740                                return;
1741                        }
1742                }
1743                if (bcs->at_state.pending_commands & PC_NOCID) {
1744                        bcs->at_state.pending_commands &= ~PC_NOCID;
1745                        cs->curchannel = bcs->channel;
1746                        schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1747                        return;
1748                } else if (bcs->at_state.pending_commands & PC_DLE0) {
1749                        bcs->at_state.pending_commands &= ~PC_DLE0;
1750                        cs->curchannel = bcs->channel;
1751                        schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1752                        return;
1753                }
1754        }
1755
1756        list_for_each_entry(at_state, &cs->temp_at_states, list)
1757                if (at_state->pending_commands & PC_HUP) {
1758                        at_state->pending_commands &= ~PC_HUP;
1759                        schedule_sequence(cs, at_state, SEQ_HUP);
1760                        return;
1761                }
1762
1763        if (cs->at_state.pending_commands & PC_INIT) {
1764                cs->at_state.pending_commands &= ~PC_INIT;
1765                cs->dle = 0;
1766                cs->inbuf->inputstate = INS_command;
1767                schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1768                return;
1769        }
1770        if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1771                cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1772                schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1773                return;
1774        }
1775        if (cs->at_state.pending_commands & PC_CIDMODE) {
1776                cs->at_state.pending_commands &= ~PC_CIDMODE;
1777                if (cs->mode == M_UNIMODEM) {
1778                        cs->retry_count = 1;
1779                        schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1780                        return;
1781                }
1782        }
1783
1784        for (i = 0; i < cs->channels; ++i) {
1785                bcs = cs->bcs + i;
1786                if (bcs->at_state.pending_commands & PC_DLE1) {
1787                        bcs->at_state.pending_commands &= ~PC_DLE1;
1788                        cs->curchannel = bcs->channel;
1789                        schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1790                        return;
1791                }
1792                if (bcs->at_state.pending_commands & PC_ACCEPT) {
1793                        bcs->at_state.pending_commands &= ~PC_ACCEPT;
1794                        schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1795                        return;
1796                }
1797                if (bcs->at_state.pending_commands & PC_DIAL) {
1798                        bcs->at_state.pending_commands &= ~PC_DIAL;
1799                        schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1800                        return;
1801                }
1802                if (bcs->at_state.pending_commands & PC_CID) {
1803                        switch (cs->mode) {
1804                        case M_UNIMODEM:
1805                                cs->at_state.pending_commands |= PC_CIDMODE;
1806                                gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1807                                cs->commands_pending = 1;
1808                                return;
1809                        case M_UNKNOWN:
1810                                schedule_init(cs, MS_INIT);
1811                                return;
1812                        }
1813                        bcs->at_state.pending_commands &= ~PC_CID;
1814                        cs->curchannel = bcs->channel;
1815                        cs->retry_count = 2;
1816                        schedule_sequence(cs, &cs->at_state, SEQ_CID);
1817                        return;
1818                }
1819        }
1820}
1821
1822static void process_events(struct cardstate *cs)
1823{
1824        struct event_t *ev;
1825        unsigned head, tail;
1826        int i;
1827        int check_flags = 0;
1828        int was_busy;
1829        unsigned long flags;
1830
1831        spin_lock_irqsave(&cs->ev_lock, flags);
1832        head = cs->ev_head;
1833
1834        for (i = 0; i < 2 * MAX_EVENTS; ++i) {
1835                tail = cs->ev_tail;
1836                if (tail == head) {
1837                        if (!check_flags && !cs->commands_pending)
1838                                break;
1839                        check_flags = 0;
1840                        spin_unlock_irqrestore(&cs->ev_lock, flags);
1841                        process_command_flags(cs);
1842                        spin_lock_irqsave(&cs->ev_lock, flags);
1843                        tail = cs->ev_tail;
1844                        if (tail == head) {
1845                                if (!cs->commands_pending)
1846                                        break;
1847                                continue;
1848                        }
1849                }
1850
1851                ev = cs->events + head;
1852                was_busy = cs->cur_at_seq != SEQ_NONE;
1853                spin_unlock_irqrestore(&cs->ev_lock, flags);
1854                process_event(cs, ev);
1855                spin_lock_irqsave(&cs->ev_lock, flags);
1856                kfree(ev->ptr);
1857                ev->ptr = NULL;
1858                if (was_busy && cs->cur_at_seq == SEQ_NONE)
1859                        check_flags = 1;
1860
1861                head = (head + 1) % MAX_EVENTS;
1862                cs->ev_head = head;
1863        }
1864
1865        spin_unlock_irqrestore(&cs->ev_lock, flags);
1866
1867        if (i == 2 * MAX_EVENTS) {
1868                dev_err(cs->dev,
1869                        "infinite loop in process_events; aborting.\n");
1870        }
1871}
1872
1873/* tasklet scheduled on any event received from the Gigaset device
1874 * parameter:
1875 *      data    ISDN controller state structure
1876 */
1877void gigaset_handle_event(unsigned long data)
1878{
1879        struct cardstate *cs = (struct cardstate *) data;
1880
1881        /* handle incoming data on control/common channel */
1882        if (cs->inbuf->head != cs->inbuf->tail) {
1883                gig_dbg(DEBUG_INTR, "processing new data");
1884                cs->ops->handle_input(cs->inbuf);
1885        }
1886
1887        process_events(cs);
1888}
1889