[PATCH] isdn4linux: Siemens Gigaset drivers: logging usage
[linux-2.6/linux-2.6-openrd.git] / drivers / isdn / gigaset / ev-layer.c
blob53490430a2e1d806e6176391b8f2a26ab3b68ea2
1 /*
2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers <Eilers.Stefan@epost.de>,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Tilman Schmidt <tilman@imap.cc>.
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 * =====================================================================
16 #include "gigaset.h"
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
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
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 */
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
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
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
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 */
165 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
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}},
173 {RSP_INIT, -1, -1,SEQ_INIT, 100, INIT_TIMEOUT,
174 {ACT_TIMEOUT}}, /* wait until device is ready */
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 */
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. */
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}},
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"},
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}},
198 {RSP_ERROR, 103,103, -1, 0, 0, {ACT_FAILINIT}},
199 {EV_TIMEOUT, 103,103, -1, 0, 0, {ACT_FAILINIT}},
201 {RSP_STRING, 120,120, -1, 121,-1, {ACT_SETVER}},
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"},
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
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}},
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}},
232 /* incoming call */
233 {RSP_RING, -1, -1, -1, -1,-1, {ACT_RING}},
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"},
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}},
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}},
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}},
259 /* abort getting cid */
260 {RSP_INIT, 0, 0,SEQ_NOCID, 0, 0, {ACT_ABORTCID}},
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}},
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
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
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}
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 */
298 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
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}},
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}},
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 */
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
331 {EV_BC_OPEN, 800,800, -1, 800,-1, {ACT_NOTIFY_BC_UP}}, //FIXME new constate + timeout
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}},
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}},
350 {EV_BC_CLOSED, 0, 0, -1, 0,-1, {ACT_NOTIFY_BC_DOWN}}, //FIXME new constate + timeout
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}},
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}},
374 {EV_TIMEOUT, 750,750, -1, 0, 0, {ACT_CONNTIMEOUT}},
376 /* misc. */
377 {EV_PROTO_L2, -1, -1, -1, -1,-1, {ACT_PROTO_L2}}, //FIXME
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
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}
391 #if 0
392 static struct reply_t tab_nocid[]= /* no dle mode */ //FIXME
394 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
396 {RSP_ANY, -1, -1, -1, -1,-1, ACT_WARN, NULL},
397 {RSP_LAST,0,0,0,0,0,0}
400 static struct reply_t tab_cid[] = /* no dle mode */ //FIXME
402 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
404 {RSP_ANY, -1, -1, -1, -1,-1, ACT_WARN, NULL},
405 {RSP_LAST,0,0,0,0,0,0}
407 #endif
409 static struct resp_type_t resp_type[]=
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}
439 * Get integer from char-pointer
441 static int isdn_getnum(char *p)
443 int v = -1;
445 IFNULLRETVAL(p, -1);
447 gig_dbg(DEBUG_TRANSCMD, "string: %s", p);
449 while (*p >= '0' && *p <= '9')
450 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p++) - '0');
451 if (*p)
452 v = -1; /* invalid Character */
453 return v;
457 * Get integer from char-pointer
459 static int isdn_gethex(char *p)
461 int v = 0;
462 int c;
464 IFNULLRETVAL(p, -1);
466 gig_dbg(DEBUG_TRANSCMD, "string: %s", p);
468 if (!*p)
469 return -1;
471 do {
472 if (v > (INT_MAX - 15) / 16)
473 return -1;
474 c = *p;
475 if (c >= '0' && c <= '9')
476 c -= '0';
477 else if (c >= 'a' && c <= 'f')
478 c -= 'a' - 10;
479 else if (c >= 'A' && c <= 'F')
480 c -= 'A' - 10;
481 else
482 return -1;
483 v = v * 16 + c;
484 } while (*++p);
486 return v;
489 static inline void new_index(atomic_t *index, int max)
491 if (atomic_read(index) == max) //FIXME race?
492 atomic_set(index, 0);
493 else
494 atomic_inc(index);
497 /* retrieve CID from parsed response
498 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
500 static int cid_of_response(char *s)
502 int cid;
504 if (s[-1] != ';')
505 return 0; /* no CID separator */
506 cid = isdn_getnum(s);
507 if (cid < 0)
508 return 0; /* CID not numeric */
509 if (cid < 1 || cid > 65535)
510 return -1; /* CID out of range */
511 return cid;
512 //FIXME is ;<digit>+ at end of non-CID response really impossible?
515 /* This function will be called via task queue from the callback handler.
516 * We received a modem response and have to handle it..
518 void gigaset_handle_modem_response(struct cardstate *cs)
520 unsigned char *argv[MAX_REC_PARAMS + 1];
521 int params;
522 int i, j;
523 struct resp_type_t *rt;
524 int curarg;
525 unsigned long flags;
526 unsigned next, tail, head;
527 struct event_t *event;
528 int resp_code;
529 int param_type;
530 int abort;
531 size_t len;
532 int cid;
533 int rawstring;
535 IFNULLRET(cs);
537 len = cs->cbytes;
538 if (!len) {
539 /* ignore additional LFs/CRs (M10x config mode or cx100) */
540 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[len]);
541 return;
543 cs->respdata[len] = 0;
544 gig_dbg(DEBUG_TRANSCMD, "raw string: '%s'", cs->respdata);
545 argv[0] = cs->respdata;
546 params = 1;
547 if (cs->at_state.getstring) {
548 /* getstring only allowed without cid at the moment */
549 cs->at_state.getstring = 0;
550 rawstring = 1;
551 cid = 0;
552 } else {
553 /* parse line */
554 for (i = 0; i < len; i++)
555 switch (cs->respdata[i]) {
556 case ';':
557 case ',':
558 case '=':
559 if (params > MAX_REC_PARAMS) {
560 dev_warn(cs->dev,
561 "too many parameters in response\n");
562 /* need last parameter (might be CID) */
563 params--;
565 argv[params++] = cs->respdata + i + 1;
568 rawstring = 0;
569 cid = params > 1 ? cid_of_response(argv[params-1]) : 0;
570 if (cid < 0) {
571 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
572 NULL, 0, NULL);
573 return;
576 for (j = 1; j < params; ++j)
577 argv[j][-1] = 0;
579 gig_dbg(DEBUG_TRANSCMD, "CMD received: %s", argv[0]);
580 if (cid) {
581 --params;
582 gig_dbg(DEBUG_TRANSCMD, "CID: %s", argv[params]);
584 gig_dbg(DEBUG_TRANSCMD, "available params: %d", params - 1);
585 for (j = 1; j < params; j++)
586 gig_dbg(DEBUG_TRANSCMD, "param %d: %s", j, argv[j]);
589 spin_lock_irqsave(&cs->ev_lock, flags);
590 head = atomic_read(&cs->ev_head);
591 tail = atomic_read(&cs->ev_tail);
593 abort = 1;
594 curarg = 0;
595 while (curarg < params) {
596 next = (tail + 1) % MAX_EVENTS;
597 if (unlikely(next == head)) {
598 dev_err(cs->dev, "event queue full\n");
599 break;
602 event = cs->events + tail;
603 event->at_state = NULL;
604 event->cid = cid;
605 event->ptr = NULL;
606 event->arg = NULL;
607 tail = next;
609 if (rawstring) {
610 resp_code = RSP_STRING;
611 param_type = RT_STRING;
612 } else {
613 for (rt = resp_type; rt->response; ++rt)
614 if (!strcmp(argv[curarg], rt->response))
615 break;
617 if (!rt->response) {
618 event->type = RSP_UNKNOWN;
619 dev_warn(cs->dev,
620 "unknown modem response: %s\n",
621 argv[curarg]);
622 break;
625 resp_code = rt->resp_code;
626 param_type = rt->type;
627 ++curarg;
630 event->type = resp_code;
632 switch (param_type) {
633 case RT_NOTHING:
634 break;
635 case RT_RING:
636 if (!cid) {
637 dev_err(cs->dev,
638 "received RING without CID!\n");
639 event->type = RSP_INVAL;
640 abort = 1;
641 } else {
642 event->cid = 0;
643 event->parameter = cid;
644 abort = 0;
646 break;
647 case RT_ZSAU:
648 if (curarg >= params) {
649 event->parameter = ZSAU_NONE;
650 break;
652 if (!strcmp(argv[curarg], "OUTGOING_CALL_PROCEEDING"))
653 event->parameter = ZSAU_OUTGOING_CALL_PROCEEDING;
654 else if (!strcmp(argv[curarg], "CALL_DELIVERED"))
655 event->parameter = ZSAU_CALL_DELIVERED;
656 else if (!strcmp(argv[curarg], "ACTIVE"))
657 event->parameter = ZSAU_ACTIVE;
658 else if (!strcmp(argv[curarg], "DISCONNECT_IND"))
659 event->parameter = ZSAU_DISCONNECT_IND;
660 else if (!strcmp(argv[curarg], "NULL"))
661 event->parameter = ZSAU_NULL;
662 else if (!strcmp(argv[curarg], "DISCONNECT_REQ"))
663 event->parameter = ZSAU_DISCONNECT_REQ;
664 else {
665 event->parameter = ZSAU_UNKNOWN;
666 dev_warn(cs->dev,
667 "%s: unknown parameter %s after ZSAU\n",
668 __func__, argv[curarg]);
670 ++curarg;
671 break;
672 case RT_STRING:
673 if (curarg < params) {
674 event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
675 if (!event->ptr)
676 dev_err(cs->dev, "out of memory\n");
677 ++curarg;
679 #ifdef CONFIG_GIGASET_DEBUG
680 if (!event->ptr)
681 gig_dbg(DEBUG_CMD, "string==NULL");
682 else
683 gig_dbg(DEBUG_CMD, "string==%s",
684 (char *) event->ptr);
685 #endif
686 break;
687 case RT_ZCAU:
688 event->parameter = -1;
689 if (curarg + 1 < params) {
690 i = isdn_gethex(argv[curarg]);
691 j = isdn_gethex(argv[curarg + 1]);
692 if (i >= 0 && i < 256 && j >= 0 && j < 256)
693 event->parameter = (unsigned) i << 8
694 | j;
695 curarg += 2;
696 } else
697 curarg = params - 1;
698 break;
699 case RT_NUMBER:
700 case RT_HEX:
701 if (curarg < params) {
702 if (param_type == RT_HEX)
703 event->parameter =
704 isdn_gethex(argv[curarg]);
705 else
706 event->parameter =
707 isdn_getnum(argv[curarg]);
708 ++curarg;
709 } else
710 event->parameter = -1;
711 #ifdef CONFIG_GIGASET_DEBUG
712 gig_dbg(DEBUG_CMD, "parameter==%d", event->parameter);
713 #endif
714 break;
717 if (resp_code == RSP_ZDLE)
718 cs->dle = event->parameter;
720 if (abort)
721 break;
724 atomic_set(&cs->ev_tail, tail);
725 spin_unlock_irqrestore(&cs->ev_lock, flags);
727 if (curarg != params)
728 gig_dbg(DEBUG_ANY,
729 "invalid number of processed parameters: %d/%d",
730 curarg, params);
732 EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
734 /* disconnect
735 * process closing of connection associated with given AT state structure
737 static void disconnect(struct at_state_t **at_state_p)
739 unsigned long flags;
740 struct bc_state *bcs;
741 struct cardstate *cs;
743 IFNULLRET(at_state_p);
744 IFNULLRET(*at_state_p);
745 bcs = (*at_state_p)->bcs;
746 cs = (*at_state_p)->cs;
747 IFNULLRET(cs);
749 new_index(&(*at_state_p)->seq_index, MAX_SEQ_INDEX);
751 /* revert to selected idle mode */
752 if (!atomic_read(&cs->cidmode)) {
753 cs->at_state.pending_commands |= PC_UMMODE;
754 atomic_set(&cs->commands_pending, 1); //FIXME
755 gig_dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
758 if (bcs) {
759 /* B channel assigned: invoke hardware specific handler */
760 cs->ops->close_bchannel(bcs);
761 } else {
762 /* no B channel assigned: just deallocate */
763 spin_lock_irqsave(&cs->lock, flags);
764 list_del(&(*at_state_p)->list);
765 kfree(*at_state_p);
766 *at_state_p = NULL;
767 spin_unlock_irqrestore(&cs->lock, flags);
771 /* get_free_channel
772 * get a free AT state structure: either one of those associated with the
773 * B channels of the Gigaset device, or if none of those is available,
774 * a newly allocated one with bcs=NULL
775 * The structure should be freed by calling disconnect() after use.
777 static inline struct at_state_t *get_free_channel(struct cardstate *cs,
778 int cid)
779 /* cids: >0: siemens-cid
780 0: without cid
781 -1: no cid assigned yet
784 unsigned long flags;
785 int i;
786 struct at_state_t *ret;
788 for (i = 0; i < cs->channels; ++i)
789 if (gigaset_get_channel(cs->bcs + i)) {
790 ret = &cs->bcs[i].at_state;
791 ret->cid = cid;
792 return ret;
795 spin_lock_irqsave(&cs->lock, flags);
796 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
797 if (ret) {
798 gigaset_at_init(ret, NULL, cs, cid);
799 list_add(&ret->list, &cs->temp_at_states);
801 spin_unlock_irqrestore(&cs->lock, flags);
802 return ret;
805 static void init_failed(struct cardstate *cs, int mode)
807 int i;
808 struct at_state_t *at_state;
810 cs->at_state.pending_commands &= ~PC_INIT;
811 atomic_set(&cs->mode, mode);
812 atomic_set(&cs->mstate, MS_UNINITIALIZED);
813 gigaset_free_channels(cs);
814 for (i = 0; i < cs->channels; ++i) {
815 at_state = &cs->bcs[i].at_state;
816 if (at_state->pending_commands & PC_CID) {
817 at_state->pending_commands &= ~PC_CID;
818 at_state->pending_commands |= PC_NOCID;
819 atomic_set(&cs->commands_pending, 1);
824 static void schedule_init(struct cardstate *cs, int state)
826 if (cs->at_state.pending_commands & PC_INIT) {
827 gig_dbg(DEBUG_CMD, "not scheduling PC_INIT again");
828 return;
830 atomic_set(&cs->mstate, state);
831 atomic_set(&cs->mode, M_UNKNOWN);
832 gigaset_block_channels(cs);
833 cs->at_state.pending_commands |= PC_INIT;
834 atomic_set(&cs->commands_pending, 1);
835 gig_dbg(DEBUG_CMD, "Scheduling PC_INIT");
838 /* Add "AT" to a command, add the cid, dle encode it, send the result to the
839 hardware. */
840 static void send_command(struct cardstate *cs, const char *cmd, int cid,
841 int dle, gfp_t kmallocflags)
843 size_t cmdlen, buflen;
844 char *cmdpos, *cmdbuf, *cmdtail;
846 cmdlen = strlen(cmd);
847 buflen = 11 + cmdlen;
848 if (unlikely(buflen <= cmdlen)) {
849 dev_err(cs->dev, "integer overflow in buflen\n");
850 return;
853 cmdbuf = kmalloc(buflen, kmallocflags);
854 if (unlikely(!cmdbuf)) {
855 dev_err(cs->dev, "out of memory\n");
856 return;
859 cmdpos = cmdbuf + 9;
860 cmdtail = cmdpos + cmdlen;
861 memcpy(cmdpos, cmd, cmdlen);
863 if (cid > 0 && cid <= 65535) {
864 do {
865 *--cmdpos = '0' + cid % 10;
866 cid /= 10;
867 ++cmdlen;
868 } while (cid);
871 cmdlen += 2;
872 *--cmdpos = 'T';
873 *--cmdpos = 'A';
875 if (dle) {
876 cmdlen += 4;
877 *--cmdpos = '(';
878 *--cmdpos = 0x10;
879 *cmdtail++ = 0x10;
880 *cmdtail++ = ')';
883 cs->ops->write_cmd(cs, cmdpos, cmdlen, NULL);
884 kfree(cmdbuf);
887 static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
889 struct at_state_t *at_state;
890 int i;
891 unsigned long flags;
893 if (cid == 0)
894 return &cs->at_state;
896 for (i = 0; i < cs->channels; ++i)
897 if (cid == cs->bcs[i].at_state.cid)
898 return &cs->bcs[i].at_state;
900 spin_lock_irqsave(&cs->lock, flags);
902 list_for_each_entry(at_state, &cs->temp_at_states, list)
903 if (cid == at_state->cid) {
904 spin_unlock_irqrestore(&cs->lock, flags);
905 return at_state;
908 spin_unlock_irqrestore(&cs->lock, flags);
910 return NULL;
913 static void bchannel_down(struct bc_state *bcs)
915 IFNULLRET(bcs);
916 IFNULLRET(bcs->cs);
918 if (bcs->chstate & CHS_B_UP) {
919 bcs->chstate &= ~CHS_B_UP;
920 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
923 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
924 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
925 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
928 gigaset_free_channel(bcs);
930 gigaset_bcs_reinit(bcs);
933 static void bchannel_up(struct bc_state *bcs)
935 IFNULLRET(bcs);
937 if (!(bcs->chstate & CHS_D_UP)) {
938 dev_notice(bcs->cs->dev, "%s: D channel not up\n", __func__);
939 bcs->chstate |= CHS_D_UP;
940 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
943 if (bcs->chstate & CHS_B_UP) {
944 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
945 __func__);
946 return;
949 bcs->chstate |= CHS_B_UP;
950 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
953 static void start_dial(struct at_state_t *at_state, void *data, int seq_index)
955 struct bc_state *bcs = at_state->bcs;
956 struct cardstate *cs = at_state->cs;
957 int retval;
959 bcs->chstate |= CHS_NOTIFY_LL;
960 //atomic_set(&bcs->status, BCS_INIT);
962 if (atomic_read(&at_state->seq_index) != seq_index)
963 goto error;
965 retval = gigaset_isdn_setup_dial(at_state, data);
966 if (retval != 0)
967 goto error;
970 at_state->pending_commands |= PC_CID;
971 gig_dbg(DEBUG_CMD, "Scheduling PC_CID");
972 atomic_set(&cs->commands_pending, 1);
973 return;
975 error:
976 at_state->pending_commands |= PC_NOCID;
977 gig_dbg(DEBUG_CMD, "Scheduling PC_NOCID");
978 atomic_set(&cs->commands_pending, 1);
979 return;
982 static void start_accept(struct at_state_t *at_state)
984 struct cardstate *cs = at_state->cs;
985 int retval;
987 retval = gigaset_isdn_setup_accept(at_state);
989 if (retval == 0) {
990 at_state->pending_commands |= PC_ACCEPT;
991 gig_dbg(DEBUG_CMD, "Scheduling PC_ACCEPT");
992 atomic_set(&cs->commands_pending, 1);
993 } else {
994 //FIXME
995 at_state->pending_commands |= PC_HUP;
996 gig_dbg(DEBUG_CMD, "Scheduling PC_HUP");
997 atomic_set(&cs->commands_pending, 1);
1001 static void do_start(struct cardstate *cs)
1003 gigaset_free_channels(cs);
1005 if (atomic_read(&cs->mstate) != MS_LOCKED)
1006 schedule_init(cs, MS_INIT);
1008 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
1009 // FIXME: not in locked mode
1010 // FIXME 2: only after init sequence
1012 cs->waiting = 0;
1013 wake_up(&cs->waitqueue);
1016 static void finish_shutdown(struct cardstate *cs)
1018 if (atomic_read(&cs->mstate) != MS_LOCKED) {
1019 atomic_set(&cs->mstate, MS_UNINITIALIZED);
1020 atomic_set(&cs->mode, M_UNKNOWN);
1023 /* The rest is done by cleanup_cs () in user mode. */
1025 cs->cmd_result = -ENODEV;
1026 cs->waiting = 0;
1027 wake_up_interruptible(&cs->waitqueue);
1030 static void do_shutdown(struct cardstate *cs)
1032 gigaset_block_channels(cs);
1034 if (atomic_read(&cs->mstate) == MS_READY) {
1035 atomic_set(&cs->mstate, MS_SHUTDOWN);
1036 cs->at_state.pending_commands |= PC_SHUTDOWN;
1037 atomic_set(&cs->commands_pending, 1);
1038 gig_dbg(DEBUG_CMD, "Scheduling PC_SHUTDOWN");
1039 } else
1040 finish_shutdown(cs);
1043 static void do_stop(struct cardstate *cs)
1045 do_shutdown(cs);
1048 /* Entering cid mode or getting a cid failed:
1049 * try to initialize the device and try again.
1051 * channel >= 0: getting cid for the channel failed
1052 * channel < 0: entering cid mode failed
1054 * returns 0 on failure
1056 static int reinit_and_retry(struct cardstate *cs, int channel)
1058 int i;
1060 if (--cs->retry_count <= 0)
1061 return 0;
1063 for (i = 0; i < cs->channels; ++i)
1064 if (cs->bcs[i].at_state.cid > 0)
1065 return 0;
1067 if (channel < 0)
1068 dev_warn(cs->dev,
1069 "Could not enter cid mode. Reinit device and try again.\n");
1070 else {
1071 dev_warn(cs->dev,
1072 "Could not get a call id. Reinit device and try again.\n");
1073 cs->bcs[channel].at_state.pending_commands |= PC_CID;
1075 schedule_init(cs, MS_INIT);
1076 return 1;
1079 static int at_state_invalid(struct cardstate *cs,
1080 struct at_state_t *test_ptr)
1082 unsigned long flags;
1083 unsigned channel;
1084 struct at_state_t *at_state;
1085 int retval = 0;
1087 spin_lock_irqsave(&cs->lock, flags);
1089 if (test_ptr == &cs->at_state)
1090 goto exit;
1092 list_for_each_entry(at_state, &cs->temp_at_states, list)
1093 if (at_state == test_ptr)
1094 goto exit;
1096 for (channel = 0; channel < cs->channels; ++channel)
1097 if (&cs->bcs[channel].at_state == test_ptr)
1098 goto exit;
1100 retval = 1;
1101 exit:
1102 spin_unlock_irqrestore(&cs->lock, flags);
1103 return retval;
1106 static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
1107 struct at_state_t **p_at_state)
1109 int retval;
1110 struct at_state_t *at_state = *p_at_state;
1112 retval = gigaset_isdn_icall(at_state);
1113 switch (retval) {
1114 case ICALL_ACCEPT:
1115 break;
1116 default:
1117 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
1118 /* --v-- fall through --v-- */
1119 case ICALL_IGNORE:
1120 case ICALL_REJECT:
1121 /* hang up actively
1122 * Device doc says that would reject the call.
1123 * In fact it doesn't.
1125 at_state->pending_commands |= PC_HUP;
1126 atomic_set(&cs->commands_pending, 1);
1127 break;
1131 static int do_lock(struct cardstate *cs)
1133 int mode;
1134 int i;
1136 switch (atomic_read(&cs->mstate)) {
1137 case MS_UNINITIALIZED:
1138 case MS_READY:
1139 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1140 cs->at_state.pending_commands)
1141 return -EBUSY;
1143 for (i = 0; i < cs->channels; ++i)
1144 if (cs->bcs[i].at_state.pending_commands)
1145 return -EBUSY;
1147 if (!gigaset_get_channels(cs))
1148 return -EBUSY;
1150 break;
1151 case MS_LOCKED:
1152 //retval = -EACCES;
1153 break;
1154 default:
1155 return -EBUSY;
1158 mode = atomic_read(&cs->mode);
1159 atomic_set(&cs->mstate, MS_LOCKED);
1160 atomic_set(&cs->mode, M_UNKNOWN);
1162 return mode;
1165 static int do_unlock(struct cardstate *cs)
1167 if (atomic_read(&cs->mstate) != MS_LOCKED)
1168 return -EINVAL;
1170 atomic_set(&cs->mstate, MS_UNINITIALIZED);
1171 atomic_set(&cs->mode, M_UNKNOWN);
1172 gigaset_free_channels(cs);
1173 if (atomic_read(&cs->connected))
1174 schedule_init(cs, MS_INIT);
1176 return 0;
1179 static void do_action(int action, struct cardstate *cs,
1180 struct bc_state *bcs,
1181 struct at_state_t **p_at_state, char **pp_command,
1182 int *p_genresp, int *p_resp_code,
1183 struct event_t *ev)
1185 struct at_state_t *at_state = *p_at_state;
1186 struct at_state_t *at_state2;
1187 unsigned long flags;
1189 int channel;
1191 unsigned char *s, *e;
1192 int i;
1193 unsigned long val;
1195 switch (action) {
1196 case ACT_NOTHING:
1197 break;
1198 case ACT_TIMEOUT:
1199 at_state->waiting = 1;
1200 break;
1201 case ACT_INIT:
1202 cs->at_state.pending_commands &= ~PC_INIT;
1203 cs->cur_at_seq = SEQ_NONE;
1204 atomic_set(&cs->mode, M_UNIMODEM);
1205 if (!atomic_read(&cs->cidmode)) {
1206 gigaset_free_channels(cs);
1207 atomic_set(&cs->mstate, MS_READY);
1208 break;
1210 cs->at_state.pending_commands |= PC_CIDMODE;
1211 atomic_set(&cs->commands_pending, 1);
1212 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1213 break;
1214 case ACT_FAILINIT:
1215 dev_warn(cs->dev, "Could not initialize the device.\n");
1216 cs->dle = 0;
1217 init_failed(cs, M_UNKNOWN);
1218 cs->cur_at_seq = SEQ_NONE;
1219 break;
1220 case ACT_CONFIGMODE:
1221 init_failed(cs, M_CONFIG);
1222 cs->cur_at_seq = SEQ_NONE;
1223 break;
1224 case ACT_SETDLE1:
1225 cs->dle = 1;
1226 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1227 cs->inbuf[0].inputstate &=
1228 ~(INS_command | INS_DLE_command);
1229 break;
1230 case ACT_SETDLE0:
1231 cs->dle = 0;
1232 cs->inbuf[0].inputstate =
1233 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1234 | INS_command;
1235 break;
1236 case ACT_CMODESET:
1237 if (atomic_read(&cs->mstate) == MS_INIT ||
1238 atomic_read(&cs->mstate) == MS_RECOVER) {
1239 gigaset_free_channels(cs);
1240 atomic_set(&cs->mstate, MS_READY);
1242 atomic_set(&cs->mode, M_CID);
1243 cs->cur_at_seq = SEQ_NONE;
1244 break;
1245 case ACT_UMODESET:
1246 atomic_set(&cs->mode, M_UNIMODEM);
1247 cs->cur_at_seq = SEQ_NONE;
1248 break;
1249 case ACT_FAILCMODE:
1250 cs->cur_at_seq = SEQ_NONE;
1251 if (atomic_read(&cs->mstate) == MS_INIT ||
1252 atomic_read(&cs->mstate) == MS_RECOVER) {
1253 init_failed(cs, M_UNKNOWN);
1254 break;
1256 if (!reinit_and_retry(cs, -1))
1257 schedule_init(cs, MS_RECOVER);
1258 break;
1259 case ACT_FAILUMODE:
1260 cs->cur_at_seq = SEQ_NONE;
1261 schedule_init(cs, MS_RECOVER);
1262 break;
1263 case ACT_HUPMODEM:
1264 /* send "+++" (hangup in unimodem mode) */
1265 cs->ops->write_cmd(cs, "+++", 3, NULL);
1266 break;
1267 case ACT_RING:
1268 /* get fresh AT state structure for new CID */
1269 at_state2 = get_free_channel(cs, ev->parameter);
1270 if (!at_state2) {
1271 dev_warn(cs->dev,
1272 "RING ignored: could not allocate channel structure\n");
1273 break;
1276 /* initialize AT state structure
1277 * note that bcs may be NULL if no B channel is free
1279 at_state2->ConState = 700;
1280 kfree(at_state2->str_var[STR_NMBR]);
1281 at_state2->str_var[STR_NMBR] = NULL;
1282 kfree(at_state2->str_var[STR_ZCPN]);
1283 at_state2->str_var[STR_ZCPN] = NULL;
1284 kfree(at_state2->str_var[STR_ZBC]);
1285 at_state2->str_var[STR_ZBC] = NULL;
1286 kfree(at_state2->str_var[STR_ZHLC]);
1287 at_state2->str_var[STR_ZHLC] = NULL;
1288 at_state2->int_var[VAR_ZCTP] = -1;
1290 spin_lock_irqsave(&cs->lock, flags);
1291 at_state2->timer_expires = RING_TIMEOUT;
1292 at_state2->timer_active = 1;
1293 spin_unlock_irqrestore(&cs->lock, flags);
1294 break;
1295 case ACT_ICALL:
1296 handle_icall(cs, bcs, p_at_state);
1297 at_state = *p_at_state;
1298 break;
1299 case ACT_FAILSDOWN:
1300 dev_warn(cs->dev, "Could not shut down the device.\n");
1301 /* fall through */
1302 case ACT_FAKESDOWN:
1303 case ACT_SDOWN:
1304 cs->cur_at_seq = SEQ_NONE;
1305 finish_shutdown(cs);
1306 break;
1307 case ACT_CONNECT:
1308 if (cs->onechannel) {
1309 at_state->pending_commands |= PC_DLE1;
1310 atomic_set(&cs->commands_pending, 1);
1311 break;
1313 bcs->chstate |= CHS_D_UP;
1314 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
1315 cs->ops->init_bchannel(bcs);
1316 break;
1317 case ACT_DLE1:
1318 cs->cur_at_seq = SEQ_NONE;
1319 bcs = cs->bcs + cs->curchannel;
1321 bcs->chstate |= CHS_D_UP;
1322 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
1323 cs->ops->init_bchannel(bcs);
1324 break;
1325 case ACT_FAKEHUP:
1326 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1327 /* fall through */
1328 case ACT_DISCONNECT:
1329 cs->cur_at_seq = SEQ_NONE;
1330 at_state->cid = -1;
1331 if (bcs && cs->onechannel && cs->dle) {
1332 /* Check for other open channels not needed:
1333 * DLE only used for M10x with one B channel.
1335 at_state->pending_commands |= PC_DLE0;
1336 atomic_set(&cs->commands_pending, 1);
1337 } else {
1338 disconnect(p_at_state);
1339 at_state = *p_at_state;
1341 break;
1342 case ACT_FAKEDLE0:
1343 at_state->int_var[VAR_ZDLE] = 0;
1344 cs->dle = 0;
1345 /* fall through */
1346 case ACT_DLE0:
1347 cs->cur_at_seq = SEQ_NONE;
1348 at_state2 = &cs->bcs[cs->curchannel].at_state;
1349 disconnect(&at_state2);
1350 break;
1351 case ACT_ABORTHUP:
1352 cs->cur_at_seq = SEQ_NONE;
1353 dev_warn(cs->dev, "Could not hang up.\n");
1354 at_state->cid = -1;
1355 if (bcs && cs->onechannel)
1356 at_state->pending_commands |= PC_DLE0;
1357 else {
1358 disconnect(p_at_state);
1359 at_state = *p_at_state;
1361 schedule_init(cs, MS_RECOVER);
1362 break;
1363 case ACT_FAILDLE0:
1364 cs->cur_at_seq = SEQ_NONE;
1365 dev_warn(cs->dev, "Could not leave DLE mode.\n");
1366 at_state2 = &cs->bcs[cs->curchannel].at_state;
1367 disconnect(&at_state2);
1368 schedule_init(cs, MS_RECOVER);
1369 break;
1370 case ACT_FAILDLE1:
1371 cs->cur_at_seq = SEQ_NONE;
1372 dev_warn(cs->dev,
1373 "Could not enter DLE mode. Trying to hang up.\n");
1374 channel = cs->curchannel;
1375 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
1376 atomic_set(&cs->commands_pending, 1);
1377 break;
1379 case ACT_CID: /* got cid; start dialing */
1380 cs->cur_at_seq = SEQ_NONE;
1381 channel = cs->curchannel;
1382 if (ev->parameter > 0 && ev->parameter <= 65535) {
1383 cs->bcs[channel].at_state.cid = ev->parameter;
1384 cs->bcs[channel].at_state.pending_commands |=
1385 PC_DIAL;
1386 atomic_set(&cs->commands_pending, 1);
1387 break;
1389 /* fall through */
1390 case ACT_FAILCID:
1391 cs->cur_at_seq = SEQ_NONE;
1392 channel = cs->curchannel;
1393 if (!reinit_and_retry(cs, channel)) {
1394 dev_warn(cs->dev,
1395 "Could not get a call ID. Cannot dial.\n");
1396 at_state2 = &cs->bcs[channel].at_state;
1397 disconnect(&at_state2);
1399 break;
1400 case ACT_ABORTCID:
1401 cs->cur_at_seq = SEQ_NONE;
1402 at_state2 = &cs->bcs[cs->curchannel].at_state;
1403 disconnect(&at_state2);
1404 break;
1406 case ACT_DIALING:
1407 case ACT_ACCEPTED:
1408 cs->cur_at_seq = SEQ_NONE;
1409 break;
1411 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL processing */
1412 disconnect(p_at_state);
1413 at_state = *p_at_state;
1414 break;
1416 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1417 cs->cur_at_seq = SEQ_NONE;
1418 at_state->pending_commands |= PC_HUP;
1419 atomic_set(&cs->commands_pending, 1);
1420 break;
1422 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1423 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1424 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1425 at_state->pending_commands |= PC_HUP;
1426 atomic_set(&cs->commands_pending, 1);
1427 break;
1428 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
1429 are not handled properly anymore */
1430 at_state->getstring = 1;
1431 break;
1432 case ACT_SETVER:
1433 if (!ev->ptr) {
1434 *p_genresp = 1;
1435 *p_resp_code = RSP_ERROR;
1436 break;
1438 s = ev->ptr;
1440 if (!strcmp(s, "OK")) {
1441 *p_genresp = 1;
1442 *p_resp_code = RSP_ERROR;
1443 break;
1446 for (i = 0; i < 4; ++i) {
1447 val = simple_strtoul(s, (char **) &e, 10);
1448 if (val > INT_MAX || e == s)
1449 break;
1450 if (i == 3) {
1451 if (*e)
1452 break;
1453 } else if (*e != '.')
1454 break;
1455 else
1456 s = e + 1;
1457 cs->fwver[i] = val;
1459 if (i != 4) {
1460 *p_genresp = 1;
1461 *p_resp_code = RSP_ERROR;
1462 break;
1464 /*at_state->getstring = 1;*/
1465 cs->gotfwver = 0;
1466 break;
1467 case ACT_GOTVER:
1468 if (cs->gotfwver == 0) {
1469 cs->gotfwver = 1;
1470 gig_dbg(DEBUG_ANY,
1471 "firmware version %02d.%03d.%02d.%02d",
1472 cs->fwver[0], cs->fwver[1],
1473 cs->fwver[2], cs->fwver[3]);
1474 break;
1476 /* fall through */
1477 case ACT_FAILVER:
1478 cs->gotfwver = -1;
1479 dev_err(cs->dev, "could not read firmware version.\n");
1480 break;
1481 #ifdef CONFIG_GIGASET_DEBUG
1482 case ACT_ERROR:
1483 *p_genresp = 1;
1484 *p_resp_code = RSP_ERROR;
1485 break;
1486 case ACT_TEST:
1488 static int count = 3; //2; //1;
1489 *p_genresp = 1;
1490 *p_resp_code = count ? RSP_ERROR : RSP_OK;
1491 if (count > 0)
1492 --count;
1494 break;
1495 #endif
1496 case ACT_DEBUG:
1497 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
1498 __func__, ev->type, at_state->ConState);
1499 break;
1500 case ACT_WARN:
1501 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1502 __func__, ev->type, at_state->ConState);
1503 break;
1504 case ACT_ZCAU:
1505 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1506 ev->parameter, at_state->ConState);
1507 break;
1509 /* events from the LL */
1510 case ACT_DIAL:
1511 start_dial(at_state, ev->ptr, ev->parameter);
1512 break;
1513 case ACT_ACCEPT:
1514 start_accept(at_state);
1515 break;
1516 case ACT_PROTO_L2:
1517 gig_dbg(DEBUG_CMD, "set protocol to %u",
1518 (unsigned) ev->parameter);
1519 at_state->bcs->proto2 = ev->parameter;
1520 break;
1521 case ACT_HUP:
1522 at_state->pending_commands |= PC_HUP;
1523 atomic_set(&cs->commands_pending, 1);
1524 gig_dbg(DEBUG_CMD, "Scheduling PC_HUP");
1525 break;
1527 /* hotplug events */
1528 case ACT_STOP:
1529 do_stop(cs);
1530 break;
1531 case ACT_START:
1532 do_start(cs);
1533 break;
1535 /* events from the interface */ // FIXME without ACT_xxxx?
1536 case ACT_IF_LOCK:
1537 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1538 cs->waiting = 0;
1539 wake_up(&cs->waitqueue);
1540 break;
1541 case ACT_IF_VER:
1542 if (ev->parameter != 0)
1543 cs->cmd_result = -EINVAL;
1544 else if (cs->gotfwver != 1) {
1545 cs->cmd_result = -ENOENT;
1546 } else {
1547 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1548 cs->cmd_result = 0;
1550 cs->waiting = 0;
1551 wake_up(&cs->waitqueue);
1552 break;
1554 /* events from the proc file system */ // FIXME without ACT_xxxx?
1555 case ACT_PROC_CIDMODE:
1556 if (ev->parameter != atomic_read(&cs->cidmode)) {
1557 atomic_set(&cs->cidmode, ev->parameter);
1558 if (ev->parameter) {
1559 cs->at_state.pending_commands |= PC_CIDMODE;
1560 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1561 } else {
1562 cs->at_state.pending_commands |= PC_UMMODE;
1563 gig_dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
1565 atomic_set(&cs->commands_pending, 1);
1567 cs->waiting = 0;
1568 wake_up(&cs->waitqueue);
1569 break;
1571 /* events from the hardware drivers */
1572 case ACT_NOTIFY_BC_DOWN:
1573 bchannel_down(bcs);
1574 break;
1575 case ACT_NOTIFY_BC_UP:
1576 bchannel_up(bcs);
1577 break;
1578 case ACT_SHUTDOWN:
1579 do_shutdown(cs);
1580 break;
1583 default:
1584 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1585 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1586 if (!*pp_command) {
1587 *p_genresp = 1;
1588 *p_resp_code = RSP_NULL;
1590 } else
1591 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
1595 /* State machine to do the calling and hangup procedure */
1596 static void process_event(struct cardstate *cs, struct event_t *ev)
1598 struct bc_state *bcs;
1599 char *p_command = NULL;
1600 struct reply_t *rep;
1601 int rcode;
1602 int genresp = 0;
1603 int resp_code = RSP_ERROR;
1604 int sendcid;
1605 struct at_state_t *at_state;
1606 int index;
1607 int curact;
1608 unsigned long flags;
1610 IFNULLRET(cs);
1611 IFNULLRET(ev);
1613 if (ev->cid >= 0) {
1614 at_state = at_state_from_cid(cs, ev->cid);
1615 if (!at_state) {
1616 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
1617 NULL, 0, NULL);
1618 return;
1620 } else {
1621 at_state = ev->at_state;
1622 if (at_state_invalid(cs, at_state)) {
1623 gig_dbg(DEBUG_ANY, "event for invalid at_state %p",
1624 at_state);
1625 return;
1629 gig_dbg(DEBUG_CMD, "connection state %d, event %d",
1630 at_state->ConState, ev->type);
1632 bcs = at_state->bcs;
1633 sendcid = at_state->cid;
1635 /* Setting the pointer to the dial array */
1636 rep = at_state->replystruct;
1637 IFNULLRET(rep);
1639 if (ev->type == EV_TIMEOUT) {
1640 if (ev->parameter != atomic_read(&at_state->timer_index)
1641 || !at_state->timer_active) {
1642 ev->type = RSP_NONE; /* old timeout */
1643 gig_dbg(DEBUG_ANY, "old timeout");
1644 } else if (!at_state->waiting)
1645 gig_dbg(DEBUG_ANY, "timeout occurred");
1646 else
1647 gig_dbg(DEBUG_ANY, "stopped waiting");
1650 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1651 or at_state->str_var[STR_XXXX], set it */
1652 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1653 index = ev->type - RSP_VAR;
1654 at_state->int_var[index] = ev->parameter;
1655 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1656 index = ev->type - RSP_STR;
1657 kfree(at_state->str_var[index]);
1658 at_state->str_var[index] = ev->ptr;
1659 ev->ptr = NULL; /* prevent process_events() from
1660 deallocating ptr */
1663 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1664 at_state->getstring = 0;
1666 /* Search row in dial array which matches modem response and current
1667 constate */
1668 for (;; rep++) {
1669 rcode = rep->resp_code;
1670 if (rcode == RSP_LAST) {
1671 /* found nothing...*/
1672 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1673 "resp_code %d in ConState %d!\n",
1674 __func__, ev->type, at_state->ConState);
1675 return;
1677 if ((rcode == RSP_ANY || rcode == ev->type)
1678 && ((int) at_state->ConState >= rep->min_ConState)
1679 && (rep->max_ConState < 0
1680 || (int) at_state->ConState <= rep->max_ConState)
1681 && (rep->parameter < 0 || rep->parameter == ev->parameter))
1682 break;
1685 p_command = rep->command;
1687 at_state->waiting = 0;
1688 for (curact = 0; curact < MAXACT; ++curact) {
1689 /* The row tells us what we should do ..
1691 do_action(rep->action[curact], cs, bcs, &at_state, &p_command, &genresp, &resp_code, ev);
1692 if (!at_state)
1693 break; /* may be freed after disconnect */
1696 if (at_state) {
1697 /* Jump to the next con-state regarding the array */
1698 if (rep->new_ConState >= 0)
1699 at_state->ConState = rep->new_ConState;
1701 if (genresp) {
1702 spin_lock_irqsave(&cs->lock, flags);
1703 at_state->timer_expires = 0; //FIXME
1704 at_state->timer_active = 0; //FIXME
1705 spin_unlock_irqrestore(&cs->lock, flags);
1706 gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1707 } else {
1708 /* Send command to modem if not NULL... */
1709 if (p_command/*rep->command*/) {
1710 if (atomic_read(&cs->connected))
1711 send_command(cs, p_command,
1712 sendcid, cs->dle,
1713 GFP_ATOMIC);
1714 else
1715 gigaset_add_event(cs, at_state,
1716 RSP_NODEV,
1717 NULL, 0, NULL);
1720 spin_lock_irqsave(&cs->lock, flags);
1721 if (!rep->timeout) {
1722 at_state->timer_expires = 0;
1723 at_state->timer_active = 0;
1724 } else if (rep->timeout > 0) { /* new timeout */
1725 at_state->timer_expires = rep->timeout * 10;
1726 at_state->timer_active = 1;
1727 new_index(&at_state->timer_index,
1728 MAX_TIMER_INDEX);
1730 spin_unlock_irqrestore(&cs->lock, flags);
1735 static void schedule_sequence(struct cardstate *cs,
1736 struct at_state_t *at_state, int sequence)
1738 cs->cur_at_seq = sequence;
1739 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1742 static void process_command_flags(struct cardstate *cs)
1744 struct at_state_t *at_state = NULL;
1745 struct bc_state *bcs;
1746 int i;
1747 int sequence;
1749 IFNULLRET(cs);
1751 atomic_set(&cs->commands_pending, 0);
1753 if (cs->cur_at_seq) {
1754 gig_dbg(DEBUG_CMD, "not searching scheduled commands: busy");
1755 return;
1758 gig_dbg(DEBUG_CMD, "searching scheduled commands");
1760 sequence = SEQ_NONE;
1762 /* clear pending_commands and hangup channels on shutdown */
1763 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1764 cs->at_state.pending_commands &= ~PC_CIDMODE;
1765 for (i = 0; i < cs->channels; ++i) {
1766 bcs = cs->bcs + i;
1767 at_state = &bcs->at_state;
1768 at_state->pending_commands &=
1769 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1770 if (at_state->cid > 0)
1771 at_state->pending_commands |= PC_HUP;
1772 if (at_state->pending_commands & PC_CID) {
1773 at_state->pending_commands |= PC_NOCID;
1774 at_state->pending_commands &= ~PC_CID;
1779 /* clear pending_commands and hangup channels on reset */
1780 if (cs->at_state.pending_commands & PC_INIT) {
1781 cs->at_state.pending_commands &= ~PC_CIDMODE;
1782 for (i = 0; i < cs->channels; ++i) {
1783 bcs = cs->bcs + i;
1784 at_state = &bcs->at_state;
1785 at_state->pending_commands &=
1786 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1787 if (at_state->cid > 0)
1788 at_state->pending_commands |= PC_HUP;
1789 if (atomic_read(&cs->mstate) == MS_RECOVER) {
1790 if (at_state->pending_commands & PC_CID) {
1791 at_state->pending_commands |= PC_NOCID;
1792 at_state->pending_commands &= ~PC_CID;
1798 /* only switch back to unimodem mode, if no commands are pending and no channels are up */
1799 if (cs->at_state.pending_commands == PC_UMMODE
1800 && !atomic_read(&cs->cidmode)
1801 && list_empty(&cs->temp_at_states)
1802 && atomic_read(&cs->mode) == M_CID) {
1803 sequence = SEQ_UMMODE;
1804 at_state = &cs->at_state;
1805 for (i = 0; i < cs->channels; ++i) {
1806 bcs = cs->bcs + i;
1807 if (bcs->at_state.pending_commands ||
1808 bcs->at_state.cid > 0) {
1809 sequence = SEQ_NONE;
1810 break;
1814 cs->at_state.pending_commands &= ~PC_UMMODE;
1815 if (sequence != SEQ_NONE) {
1816 schedule_sequence(cs, at_state, sequence);
1817 return;
1820 for (i = 0; i < cs->channels; ++i) {
1821 bcs = cs->bcs + i;
1822 if (bcs->at_state.pending_commands & PC_HUP) {
1823 bcs->at_state.pending_commands &= ~PC_HUP;
1824 if (bcs->at_state.pending_commands & PC_CID) {
1825 /* not yet dialing: PC_NOCID is sufficient */
1826 bcs->at_state.pending_commands |= PC_NOCID;
1827 bcs->at_state.pending_commands &= ~PC_CID;
1828 } else {
1829 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1830 return;
1833 if (bcs->at_state.pending_commands & PC_NOCID) {
1834 bcs->at_state.pending_commands &= ~PC_NOCID;
1835 cs->curchannel = bcs->channel;
1836 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1837 return;
1838 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1839 bcs->at_state.pending_commands &= ~PC_DLE0;
1840 cs->curchannel = bcs->channel;
1841 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1842 return;
1846 list_for_each_entry(at_state, &cs->temp_at_states, list)
1847 if (at_state->pending_commands & PC_HUP) {
1848 at_state->pending_commands &= ~PC_HUP;
1849 schedule_sequence(cs, at_state, SEQ_HUP);
1850 return;
1853 if (cs->at_state.pending_commands & PC_INIT) {
1854 cs->at_state.pending_commands &= ~PC_INIT;
1855 cs->dle = 0; //FIXME
1856 cs->inbuf->inputstate = INS_command;
1857 //FIXME reset card state (or -> LOCK0)?
1858 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1859 return;
1861 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1862 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1863 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1864 return;
1866 if (cs->at_state.pending_commands & PC_CIDMODE) {
1867 cs->at_state.pending_commands &= ~PC_CIDMODE;
1868 if (atomic_read(&cs->mode) == M_UNIMODEM) {
1869 cs->retry_count = 1;
1870 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1871 return;
1875 for (i = 0; i < cs->channels; ++i) {
1876 bcs = cs->bcs + i;
1877 if (bcs->at_state.pending_commands & PC_DLE1) {
1878 bcs->at_state.pending_commands &= ~PC_DLE1;
1879 cs->curchannel = bcs->channel;
1880 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1881 return;
1883 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1884 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1885 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1886 return;
1888 if (bcs->at_state.pending_commands & PC_DIAL) {
1889 bcs->at_state.pending_commands &= ~PC_DIAL;
1890 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1891 return;
1893 if (bcs->at_state.pending_commands & PC_CID) {
1894 switch (atomic_read(&cs->mode)) {
1895 case M_UNIMODEM:
1896 cs->at_state.pending_commands |= PC_CIDMODE;
1897 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1898 atomic_set(&cs->commands_pending, 1);
1899 return;
1900 #ifdef GIG_MAYINITONDIAL
1901 case M_UNKNOWN:
1902 schedule_init(cs, MS_INIT);
1903 return;
1904 #endif
1906 bcs->at_state.pending_commands &= ~PC_CID;
1907 cs->curchannel = bcs->channel;
1908 #ifdef GIG_RETRYCID
1909 cs->retry_count = 2;
1910 #else
1911 cs->retry_count = 1;
1912 #endif
1913 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1914 return;
1919 static void process_events(struct cardstate *cs)
1921 struct event_t *ev;
1922 unsigned head, tail;
1923 int i;
1924 int check_flags = 0;
1925 int was_busy;
1927 /* no locking needed (only one reader) */
1928 head = atomic_read(&cs->ev_head);
1930 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
1931 tail = atomic_read(&cs->ev_tail);
1932 if (tail == head) {
1933 if (!check_flags && !atomic_read(&cs->commands_pending))
1934 break;
1935 check_flags = 0;
1936 process_command_flags(cs);
1937 tail = atomic_read(&cs->ev_tail);
1938 if (tail == head) {
1939 if (!atomic_read(&cs->commands_pending))
1940 break;
1941 continue;
1945 ev = cs->events + head;
1946 was_busy = cs->cur_at_seq != SEQ_NONE;
1947 process_event(cs, ev);
1948 kfree(ev->ptr);
1949 ev->ptr = NULL;
1950 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1951 check_flags = 1;
1953 head = (head + 1) % MAX_EVENTS;
1954 atomic_set(&cs->ev_head, head);
1957 if (i == 2 * MAX_EVENTS) {
1958 dev_err(cs->dev,
1959 "infinite loop in process_events; aborting.\n");
1963 /* tasklet scheduled on any event received from the Gigaset device
1964 * parameter:
1965 * data ISDN controller state structure
1967 void gigaset_handle_event(unsigned long data)
1969 struct cardstate *cs = (struct cardstate *) data;
1971 IFNULLRET(cs);
1972 IFNULLRET(cs->inbuf);
1974 /* handle incoming data on control/common channel */
1975 if (atomic_read(&cs->inbuf->head) != atomic_read(&cs->inbuf->tail)) {
1976 gig_dbg(DEBUG_INTR, "processing new data");
1977 cs->ops->handle_input(cs->inbuf);
1980 process_events(cs);