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