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