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