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