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