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