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