]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/isdn/gigaset/ev-layer.c
isdn/gigaset: limit raw CAPI message dump length
[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
1cec9727
TS
410/**
411 * gigaset_handle_modem_response() - process received modem response
412 * @cs: device descriptor structure.
413 *
414 * Called by asyncdata/isocdata if a block of data received from the
415 * device must be processed as a modem command response. The data is
416 * already in the cs structure.
14fa73a7
HL
417 */
418void gigaset_handle_modem_response(struct cardstate *cs)
419{
420 unsigned char *argv[MAX_REC_PARAMS + 1];
421 int params;
422 int i, j;
35dc8457 423 const struct resp_type_t *rt;
50e4fe91 424 const struct zsau_resp_t *zr;
14fa73a7
HL
425 int curarg;
426 unsigned long flags;
427 unsigned next, tail, head;
428 struct event_t *event;
429 int resp_code;
430 int param_type;
431 int abort;
432 size_t len;
433 int cid;
434 int rawstring;
435
14fa73a7
HL
436 len = cs->cbytes;
437 if (!len) {
438 /* ignore additional LFs/CRs (M10x config mode or cx100) */
63b203b4 439 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[0]);
14fa73a7
HL
440 return;
441 }
442 cs->respdata[len] = 0;
14fa73a7
HL
443 argv[0] = cs->respdata;
444 params = 1;
445 if (cs->at_state.getstring) {
446 /* getstring only allowed without cid at the moment */
447 cs->at_state.getstring = 0;
448 rawstring = 1;
449 cid = 0;
450 } else {
451 /* parse line */
452 for (i = 0; i < len; i++)
453 switch (cs->respdata[i]) {
454 case ';':
455 case ',':
456 case '=':
457 if (params > MAX_REC_PARAMS) {
784d5858 458 dev_warn(cs->dev,
475be4d8 459 "too many parameters in response\n");
14fa73a7
HL
460 /* need last parameter (might be CID) */
461 params--;
462 }
463 argv[params++] = cs->respdata + i + 1;
464 }
465
466 rawstring = 0;
475be4d8 467 cid = params > 1 ? cid_of_response(argv[params - 1]) : 0;
14fa73a7
HL
468 if (cid < 0) {
469 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
784d5858 470 NULL, 0, NULL);
14fa73a7
HL
471 return;
472 }
473
474 for (j = 1; j < params; ++j)
475 argv[j][-1] = 0;
476
1528b18f 477 gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]);
14fa73a7
HL
478 if (cid) {
479 --params;
1528b18f 480 gig_dbg(DEBUG_EVENT, "CID: %s", argv[params]);
14fa73a7 481 }
1528b18f 482 gig_dbg(DEBUG_EVENT, "available params: %d", params - 1);
14fa73a7 483 for (j = 1; j < params; j++)
1528b18f 484 gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]);
14fa73a7
HL
485 }
486
487 spin_lock_irqsave(&cs->ev_lock, flags);
69049cc8
TS
488 head = cs->ev_head;
489 tail = cs->ev_tail;
14fa73a7
HL
490
491 abort = 1;
492 curarg = 0;
493 while (curarg < params) {
494 next = (tail + 1) % MAX_EVENTS;
495 if (unlikely(next == head)) {
784d5858 496 dev_err(cs->dev, "event queue full\n");
14fa73a7
HL
497 break;
498 }
499
500 event = cs->events + tail;
501 event->at_state = NULL;
502 event->cid = cid;
503 event->ptr = NULL;
504 event->arg = NULL;
505 tail = next;
506
507 if (rawstring) {
508 resp_code = RSP_STRING;
509 param_type = RT_STRING;
510 } else {
511 for (rt = resp_type; rt->response; ++rt)
512 if (!strcmp(argv[curarg], rt->response))
513 break;
514
515 if (!rt->response) {
2ed5e4ff
TS
516 event->type = RSP_NONE;
517 gig_dbg(DEBUG_EVENT,
518 "unknown modem response: '%s'\n",
519 argv[curarg]);
14fa73a7
HL
520 break;
521 }
522
523 resp_code = rt->resp_code;
524 param_type = rt->type;
525 ++curarg;
526 }
527
528 event->type = resp_code;
529
530 switch (param_type) {
531 case RT_NOTHING:
532 break;
533 case RT_RING:
534 if (!cid) {
784d5858
TS
535 dev_err(cs->dev,
536 "received RING without CID!\n");
14fa73a7
HL
537 event->type = RSP_INVAL;
538 abort = 1;
539 } else {
540 event->cid = 0;
541 event->parameter = cid;
542 abort = 0;
543 }
544 break;
545 case RT_ZSAU:
546 if (curarg >= params) {
547 event->parameter = ZSAU_NONE;
548 break;
549 }
50e4fe91
TS
550 for (zr = zsau_resp; zr->str; ++zr)
551 if (!strcmp(argv[curarg], zr->str))
552 break;
553 event->parameter = zr->code;
554 if (!zr->str)
784d5858 555 dev_warn(cs->dev,
475be4d8 556 "%s: unknown parameter %s after ZSAU\n",
784d5858 557 __func__, argv[curarg]);
14fa73a7
HL
558 ++curarg;
559 break;
560 case RT_STRING:
561 if (curarg < params) {
784d5858
TS
562 event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
563 if (!event->ptr)
564 dev_err(cs->dev, "out of memory\n");
14fa73a7
HL
565 ++curarg;
566 }
1528b18f 567 gig_dbg(DEBUG_EVENT, "string==%s",
236b87c2 568 event->ptr ? (char *) event->ptr : "NULL");
14fa73a7
HL
569 break;
570 case RT_ZCAU:
571 event->parameter = -1;
572 if (curarg + 1 < params) {
19eccc2b 573 u8 type, value;
c9741380 574
19eccc2b
AD
575 i = kstrtou8(argv[curarg++], 16, &type);
576 j = kstrtou8(argv[curarg++], 16, &value);
577 if (i == 0 && j == 0)
c9741380 578 event->parameter = (type << 8) | value;
14fa73a7
HL
579 } else
580 curarg = params - 1;
581 break;
582 case RT_NUMBER:
19eccc2b
AD
583 if (curarg >= params ||
584 kstrtoint(argv[curarg++], 10, &event->parameter))
585 event->parameter = -1;
1528b18f 586 gig_dbg(DEBUG_EVENT, "parameter==%d", event->parameter);
14fa73a7
HL
587 break;
588 }
589
590 if (resp_code == RSP_ZDLE)
591 cs->dle = event->parameter;
592
593 if (abort)
594 break;
595 }
596
69049cc8 597 cs->ev_tail = tail;
14fa73a7
HL
598 spin_unlock_irqrestore(&cs->ev_lock, flags);
599
600 if (curarg != params)
1528b18f 601 gig_dbg(DEBUG_EVENT,
784d5858
TS
602 "invalid number of processed parameters: %d/%d",
603 curarg, params);
14fa73a7
HL
604}
605EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
606
607/* disconnect
608 * process closing of connection associated with given AT state structure
609 */
610static void disconnect(struct at_state_t **at_state_p)
611{
612 unsigned long flags;
d48c7784
TS
613 struct bc_state *bcs = (*at_state_p)->bcs;
614 struct cardstate *cs = (*at_state_p)->cs;
14fa73a7 615
69049cc8
TS
616 spin_lock_irqsave(&cs->lock, flags);
617 ++(*at_state_p)->seq_index;
14fa73a7
HL
618
619 /* revert to selected idle mode */
69049cc8 620 if (!cs->cidmode) {
14fa73a7 621 cs->at_state.pending_commands |= PC_UMMODE;
1528b18f 622 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
9d4bee2b 623 cs->commands_pending = 1;
14fa73a7 624 }
69049cc8 625 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7
HL
626
627 if (bcs) {
628 /* B channel assigned: invoke hardware specific handler */
629 cs->ops->close_bchannel(bcs);
3305adff
TS
630 /* notify LL */
631 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
632 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
088ec0cc 633 gigaset_isdn_hupD(bcs);
3305adff 634 }
14fa73a7
HL
635 } else {
636 /* no B channel assigned: just deallocate */
637 spin_lock_irqsave(&cs->lock, flags);
638 list_del(&(*at_state_p)->list);
639 kfree(*at_state_p);
640 *at_state_p = NULL;
641 spin_unlock_irqrestore(&cs->lock, flags);
642 }
643}
644
645/* get_free_channel
646 * get a free AT state structure: either one of those associated with the
647 * B channels of the Gigaset device, or if none of those is available,
648 * a newly allocated one with bcs=NULL
649 * The structure should be freed by calling disconnect() after use.
650 */
651static inline struct at_state_t *get_free_channel(struct cardstate *cs,
784d5858 652 int cid)
14fa73a7 653/* cids: >0: siemens-cid
f86936ff
TS
654 * 0: without cid
655 * -1: no cid assigned yet
656 */
14fa73a7
HL
657{
658 unsigned long flags;
659 int i;
660 struct at_state_t *ret;
661
662 for (i = 0; i < cs->channels; ++i)
81fa7b82 663 if (gigaset_get_channel(cs->bcs + i) >= 0) {
14fa73a7
HL
664 ret = &cs->bcs[i].at_state;
665 ret->cid = cid;
666 return ret;
667 }
668
669 spin_lock_irqsave(&cs->lock, flags);
670 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
671 if (ret) {
672 gigaset_at_init(ret, NULL, cs, cid);
673 list_add(&ret->list, &cs->temp_at_states);
674 }
675 spin_unlock_irqrestore(&cs->lock, flags);
676 return ret;
677}
678
679static void init_failed(struct cardstate *cs, int mode)
680{
681 int i;
682 struct at_state_t *at_state;
683
684 cs->at_state.pending_commands &= ~PC_INIT;
9d4bee2b
TS
685 cs->mode = mode;
686 cs->mstate = MS_UNINITIALIZED;
14fa73a7
HL
687 gigaset_free_channels(cs);
688 for (i = 0; i < cs->channels; ++i) {
689 at_state = &cs->bcs[i].at_state;
690 if (at_state->pending_commands & PC_CID) {
691 at_state->pending_commands &= ~PC_CID;
692 at_state->pending_commands |= PC_NOCID;
9d4bee2b 693 cs->commands_pending = 1;
14fa73a7
HL
694 }
695 }
696}
697
698static void schedule_init(struct cardstate *cs, int state)
699{
700 if (cs->at_state.pending_commands & PC_INIT) {
1528b18f 701 gig_dbg(DEBUG_EVENT, "not scheduling PC_INIT again");
14fa73a7
HL
702 return;
703 }
9d4bee2b
TS
704 cs->mstate = state;
705 cs->mode = M_UNKNOWN;
14fa73a7
HL
706 gigaset_block_channels(cs);
707 cs->at_state.pending_commands |= PC_INIT;
1528b18f 708 gig_dbg(DEBUG_EVENT, "Scheduling PC_INIT");
9d4bee2b 709 cs->commands_pending = 1;
14fa73a7
HL
710}
711
63b203b4
TS
712/* send an AT command
713 * adding the "AT" prefix, cid and DLE encapsulation as appropriate
714 */
715static void send_command(struct cardstate *cs, const char *cmd,
716 struct at_state_t *at_state)
14fa73a7 717{
63b203b4 718 int cid = at_state->cid;
e3628dd1
TS
719 struct cmdbuf_t *cb;
720 size_t buflen;
14fa73a7 721
e3628dd1 722 buflen = strlen(cmd) + 12; /* DLE ( A T 1 2 3 4 5 <cmd> DLE ) \0 */
63b203b4 723 cb = kmalloc(sizeof(struct cmdbuf_t) + buflen, GFP_ATOMIC);
e3628dd1
TS
724 if (!cb) {
725 dev_err(cs->dev, "%s: out of memory\n", __func__);
784d5858
TS
726 return;
727 }
e3628dd1
TS
728 if (cid > 0 && cid <= 65535)
729 cb->len = snprintf(cb->buf, buflen,
63b203b4 730 cs->dle ? "\020(AT%d%s\020)" : "AT%d%s",
475be4d8 731 cid, cmd);
e3628dd1
TS
732 else
733 cb->len = snprintf(cb->buf, buflen,
63b203b4 734 cs->dle ? "\020(AT%s\020)" : "AT%s",
475be4d8 735 cmd);
e3628dd1
TS
736 cb->offset = 0;
737 cb->next = NULL;
738 cb->wake_tasklet = NULL;
739 cs->ops->write_cmd(cs, cb);
14fa73a7
HL
740}
741
742static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
743{
744 struct at_state_t *at_state;
745 int i;
746 unsigned long flags;
747
748 if (cid == 0)
749 return &cs->at_state;
750
751 for (i = 0; i < cs->channels; ++i)
752 if (cid == cs->bcs[i].at_state.cid)
753 return &cs->bcs[i].at_state;
754
755 spin_lock_irqsave(&cs->lock, flags);
756
757 list_for_each_entry(at_state, &cs->temp_at_states, list)
758 if (cid == at_state->cid) {
759 spin_unlock_irqrestore(&cs->lock, flags);
760 return at_state;
761 }
762
763 spin_unlock_irqrestore(&cs->lock, flags);
764
765 return NULL;
766}
767
768static void bchannel_down(struct bc_state *bcs)
769{
14fa73a7
HL
770 if (bcs->chstate & CHS_B_UP) {
771 bcs->chstate &= ~CHS_B_UP;
088ec0cc 772 gigaset_isdn_hupB(bcs);
14fa73a7
HL
773 }
774
775 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
776 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
088ec0cc 777 gigaset_isdn_hupD(bcs);
14fa73a7
HL
778 }
779
780 gigaset_free_channel(bcs);
781
782 gigaset_bcs_reinit(bcs);
783}
784
785static void bchannel_up(struct bc_state *bcs)
786{
14fa73a7 787 if (bcs->chstate & CHS_B_UP) {
784d5858
TS
788 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
789 __func__);
14fa73a7
HL
790 return;
791 }
792
793 bcs->chstate |= CHS_B_UP;
088ec0cc 794 gigaset_isdn_connB(bcs);
14fa73a7
HL
795}
796
c35a87ff 797static void start_dial(struct at_state_t *at_state, void *data,
475be4d8 798 unsigned seq_index)
14fa73a7
HL
799{
800 struct bc_state *bcs = at_state->bcs;
801 struct cardstate *cs = at_state->cs;
088ec0cc 802 char **commands = data;
69049cc8 803 unsigned long flags;
088ec0cc 804 int i;
14fa73a7
HL
805
806 bcs->chstate |= CHS_NOTIFY_LL;
14fa73a7 807
69049cc8
TS
808 spin_lock_irqsave(&cs->lock, flags);
809 if (at_state->seq_index != seq_index) {
810 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7 811 goto error;
69049cc8
TS
812 }
813 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7 814
088ec0cc
TS
815 for (i = 0; i < AT_NUM; ++i) {
816 kfree(bcs->commands[i]);
817 bcs->commands[i] = commands[i];
818 }
14fa73a7
HL
819
820 at_state->pending_commands |= PC_CID;
1528b18f 821 gig_dbg(DEBUG_EVENT, "Scheduling PC_CID");
9d4bee2b 822 cs->commands_pending = 1;
14fa73a7
HL
823 return;
824
825error:
088ec0cc
TS
826 for (i = 0; i < AT_NUM; ++i) {
827 kfree(commands[i]);
828 commands[i] = NULL;
829 }
14fa73a7 830 at_state->pending_commands |= PC_NOCID;
1528b18f 831 gig_dbg(DEBUG_EVENT, "Scheduling PC_NOCID");
9d4bee2b 832 cs->commands_pending = 1;
14fa73a7
HL
833 return;
834}
835
836static void start_accept(struct at_state_t *at_state)
837{
838 struct cardstate *cs = at_state->cs;
088ec0cc
TS
839 struct bc_state *bcs = at_state->bcs;
840 int i;
14fa73a7 841
088ec0cc
TS
842 for (i = 0; i < AT_NUM; ++i) {
843 kfree(bcs->commands[i]);
844 bcs->commands[i] = NULL;
845 }
14fa73a7 846
088ec0cc
TS
847 bcs->commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
848 bcs->commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
849 if (!bcs->commands[AT_PROTO] || !bcs->commands[AT_ISO]) {
850 dev_err(at_state->cs->dev, "out of memory\n");
9d4bee2b 851 /* error reset */
14fa73a7 852 at_state->pending_commands |= PC_HUP;
1528b18f 853 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
9d4bee2b 854 cs->commands_pending = 1;
088ec0cc 855 return;
14fa73a7 856 }
088ec0cc
TS
857
858 snprintf(bcs->commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
859 snprintf(bcs->commands[AT_ISO], 9, "^SISO=%u\r", bcs->channel + 1);
860
861 at_state->pending_commands |= PC_ACCEPT;
1528b18f 862 gig_dbg(DEBUG_EVENT, "Scheduling PC_ACCEPT");
088ec0cc 863 cs->commands_pending = 1;
14fa73a7
HL
864}
865
866static void do_start(struct cardstate *cs)
867{
868 gigaset_free_channels(cs);
869
9d4bee2b 870 if (cs->mstate != MS_LOCKED)
14fa73a7
HL
871 schedule_init(cs, MS_INIT);
872
69049cc8 873 cs->isdn_up = 1;
088ec0cc 874 gigaset_isdn_start(cs);
14fa73a7
HL
875
876 cs->waiting = 0;
877 wake_up(&cs->waitqueue);
878}
879
880static void finish_shutdown(struct cardstate *cs)
881{
9d4bee2b
TS
882 if (cs->mstate != MS_LOCKED) {
883 cs->mstate = MS_UNINITIALIZED;
884 cs->mode = M_UNKNOWN;
14fa73a7
HL
885 }
886
69049cc8
TS
887 /* Tell the LL that the device is not available .. */
888 if (cs->isdn_up) {
889 cs->isdn_up = 0;
088ec0cc 890 gigaset_isdn_stop(cs);
69049cc8
TS
891 }
892
63b203b4 893 /* The rest is done by cleanup_cs() in process context. */
14fa73a7
HL
894
895 cs->cmd_result = -ENODEV;
896 cs->waiting = 0;
2869b23e 897 wake_up(&cs->waitqueue);
14fa73a7
HL
898}
899
900static void do_shutdown(struct cardstate *cs)
901{
902 gigaset_block_channels(cs);
903
9d4bee2b
TS
904 if (cs->mstate == MS_READY) {
905 cs->mstate = MS_SHUTDOWN;
14fa73a7 906 cs->at_state.pending_commands |= PC_SHUTDOWN;
1528b18f 907 gig_dbg(DEBUG_EVENT, "Scheduling PC_SHUTDOWN");
9d4bee2b 908 cs->commands_pending = 1;
14fa73a7
HL
909 } else
910 finish_shutdown(cs);
911}
912
913static void do_stop(struct cardstate *cs)
914{
69049cc8
TS
915 unsigned long flags;
916
917 spin_lock_irqsave(&cs->lock, flags);
918 cs->connected = 0;
919 spin_unlock_irqrestore(&cs->lock, flags);
920
14fa73a7
HL
921 do_shutdown(cs);
922}
923
924/* Entering cid mode or getting a cid failed:
925 * try to initialize the device and try again.
926 *
927 * channel >= 0: getting cid for the channel failed
928 * channel < 0: entering cid mode failed
929 *
81fa7b82 930 * returns 0 on success, <0 on failure
14fa73a7
HL
931 */
932static int reinit_and_retry(struct cardstate *cs, int channel)
933{
934 int i;
935
936 if (--cs->retry_count <= 0)
81fa7b82 937 return -EFAULT;
14fa73a7
HL
938
939 for (i = 0; i < cs->channels; ++i)
940 if (cs->bcs[i].at_state.cid > 0)
81fa7b82 941 return -EBUSY;
14fa73a7
HL
942
943 if (channel < 0)
784d5858 944 dev_warn(cs->dev,
475be4d8 945 "Could not enter cid mode. Reinit device and try again.\n");
14fa73a7 946 else {
784d5858 947 dev_warn(cs->dev,
475be4d8 948 "Could not get a call id. Reinit device and try again.\n");
14fa73a7
HL
949 cs->bcs[channel].at_state.pending_commands |= PC_CID;
950 }
951 schedule_init(cs, MS_INIT);
81fa7b82 952 return 0;
14fa73a7
HL
953}
954
955static int at_state_invalid(struct cardstate *cs,
784d5858 956 struct at_state_t *test_ptr)
14fa73a7
HL
957{
958 unsigned long flags;
959 unsigned channel;
960 struct at_state_t *at_state;
961 int retval = 0;
962
963 spin_lock_irqsave(&cs->lock, flags);
964
965 if (test_ptr == &cs->at_state)
966 goto exit;
967
968 list_for_each_entry(at_state, &cs->temp_at_states, list)
969 if (at_state == test_ptr)
970 goto exit;
971
972 for (channel = 0; channel < cs->channels; ++channel)
973 if (&cs->bcs[channel].at_state == test_ptr)
974 goto exit;
975
976 retval = 1;
977exit:
978 spin_unlock_irqrestore(&cs->lock, flags);
979 return retval;
980}
981
982static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
63b203b4 983 struct at_state_t *at_state)
14fa73a7
HL
984{
985 int retval;
14fa73a7
HL
986
987 retval = gigaset_isdn_icall(at_state);
988 switch (retval) {
989 case ICALL_ACCEPT:
990 break;
991 default:
784d5858 992 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
14fa73a7
HL
993 /* --v-- fall through --v-- */
994 case ICALL_IGNORE:
995 case ICALL_REJECT:
996 /* hang up actively
997 * Device doc says that would reject the call.
998 * In fact it doesn't.
999 */
1000 at_state->pending_commands |= PC_HUP;
9d4bee2b 1001 cs->commands_pending = 1;
14fa73a7
HL
1002 break;
1003 }
1004}
1005
1006static int do_lock(struct cardstate *cs)
1007{
1008 int mode;
1009 int i;
1010
9d4bee2b 1011 switch (cs->mstate) {
14fa73a7
HL
1012 case MS_UNINITIALIZED:
1013 case MS_READY:
1014 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1015 cs->at_state.pending_commands)
1016 return -EBUSY;
1017
1018 for (i = 0; i < cs->channels; ++i)
1019 if (cs->bcs[i].at_state.pending_commands)
1020 return -EBUSY;
1021
81fa7b82 1022 if (gigaset_get_channels(cs) < 0)
14fa73a7
HL
1023 return -EBUSY;
1024
1025 break;
1026 case MS_LOCKED:
14fa73a7
HL
1027 break;
1028 default:
1029 return -EBUSY;
1030 }
1031
9d4bee2b
TS
1032 mode = cs->mode;
1033 cs->mstate = MS_LOCKED;
1034 cs->mode = M_UNKNOWN;
14fa73a7
HL
1035
1036 return mode;
1037}
1038
1039static int do_unlock(struct cardstate *cs)
1040{
9d4bee2b 1041 if (cs->mstate != MS_LOCKED)
14fa73a7
HL
1042 return -EINVAL;
1043
9d4bee2b
TS
1044 cs->mstate = MS_UNINITIALIZED;
1045 cs->mode = M_UNKNOWN;
14fa73a7 1046 gigaset_free_channels(cs);
69049cc8 1047 if (cs->connected)
14fa73a7
HL
1048 schedule_init(cs, MS_INIT);
1049
1050 return 0;
1051}
1052
1053static void do_action(int action, struct cardstate *cs,
1054 struct bc_state *bcs,
1055 struct at_state_t **p_at_state, char **pp_command,
1056 int *p_genresp, int *p_resp_code,
1057 struct event_t *ev)
1058{
1059 struct at_state_t *at_state = *p_at_state;
1060 struct at_state_t *at_state2;
1061 unsigned long flags;
1062
1063 int channel;
1064
1065 unsigned char *s, *e;
1066 int i;
1067 unsigned long val;
1068
1069 switch (action) {
1070 case ACT_NOTHING:
1071 break;
1072 case ACT_TIMEOUT:
1073 at_state->waiting = 1;
1074 break;
1075 case ACT_INIT:
14fa73a7
HL
1076 cs->at_state.pending_commands &= ~PC_INIT;
1077 cs->cur_at_seq = SEQ_NONE;
9d4bee2b 1078 cs->mode = M_UNIMODEM;
69049cc8
TS
1079 spin_lock_irqsave(&cs->lock, flags);
1080 if (!cs->cidmode) {
1081 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7 1082 gigaset_free_channels(cs);
9d4bee2b 1083 cs->mstate = MS_READY;
14fa73a7
HL
1084 break;
1085 }
69049cc8 1086 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7 1087 cs->at_state.pending_commands |= PC_CIDMODE;
1528b18f 1088 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
9d4bee2b 1089 cs->commands_pending = 1;
14fa73a7
HL
1090 break;
1091 case ACT_FAILINIT:
784d5858 1092 dev_warn(cs->dev, "Could not initialize the device.\n");
14fa73a7
HL
1093 cs->dle = 0;
1094 init_failed(cs, M_UNKNOWN);
1095 cs->cur_at_seq = SEQ_NONE;
1096 break;
1097 case ACT_CONFIGMODE:
1098 init_failed(cs, M_CONFIG);
1099 cs->cur_at_seq = SEQ_NONE;
1100 break;
1101 case ACT_SETDLE1:
1102 cs->dle = 1;
1103 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1104 cs->inbuf[0].inputstate &=
1105 ~(INS_command | INS_DLE_command);
1106 break;
1107 case ACT_SETDLE0:
1108 cs->dle = 0;
1109 cs->inbuf[0].inputstate =
1110 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1111 | INS_command;
1112 break;
1113 case ACT_CMODESET:
9d4bee2b 1114 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
14fa73a7 1115 gigaset_free_channels(cs);
9d4bee2b 1116 cs->mstate = MS_READY;
14fa73a7 1117 }
9d4bee2b 1118 cs->mode = M_CID;
14fa73a7
HL
1119 cs->cur_at_seq = SEQ_NONE;
1120 break;
1121 case ACT_UMODESET:
9d4bee2b 1122 cs->mode = M_UNIMODEM;
14fa73a7
HL
1123 cs->cur_at_seq = SEQ_NONE;
1124 break;
1125 case ACT_FAILCMODE:
1126 cs->cur_at_seq = SEQ_NONE;
9d4bee2b 1127 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
14fa73a7
HL
1128 init_failed(cs, M_UNKNOWN);
1129 break;
1130 }
81fa7b82 1131 if (reinit_and_retry(cs, -1) < 0)
14fa73a7
HL
1132 schedule_init(cs, MS_RECOVER);
1133 break;
1134 case ACT_FAILUMODE:
1135 cs->cur_at_seq = SEQ_NONE;
1136 schedule_init(cs, MS_RECOVER);
1137 break;
1138 case ACT_HUPMODEM:
1139 /* send "+++" (hangup in unimodem mode) */
e3628dd1
TS
1140 if (cs->connected) {
1141 struct cmdbuf_t *cb;
1142
1143 cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC);
1144 if (!cb) {
1145 dev_err(cs->dev, "%s: out of memory\n",
1146 __func__);
1147 return;
1148 }
1149 memcpy(cb->buf, "+++", 3);
1150 cb->len = 3;
1151 cb->offset = 0;
1152 cb->next = NULL;
1153 cb->wake_tasklet = NULL;
1154 cs->ops->write_cmd(cs, cb);
1155 }
14fa73a7
HL
1156 break;
1157 case ACT_RING:
1158 /* get fresh AT state structure for new CID */
1159 at_state2 = get_free_channel(cs, ev->parameter);
1160 if (!at_state2) {
784d5858 1161 dev_warn(cs->dev,
475be4d8 1162 "RING ignored: could not allocate channel structure\n");
14fa73a7
HL
1163 break;
1164 }
1165
1166 /* initialize AT state structure
1167 * note that bcs may be NULL if no B channel is free
1168 */
1169 at_state2->ConState = 700;
3a0a3a6b
TS
1170 for (i = 0; i < STR_NUM; ++i) {
1171 kfree(at_state2->str_var[i]);
1172 at_state2->str_var[i] = NULL;
1173 }
14fa73a7
HL
1174 at_state2->int_var[VAR_ZCTP] = -1;
1175
1176 spin_lock_irqsave(&cs->lock, flags);
1177 at_state2->timer_expires = RING_TIMEOUT;
1178 at_state2->timer_active = 1;
1179 spin_unlock_irqrestore(&cs->lock, flags);
1180 break;
1181 case ACT_ICALL:
63b203b4 1182 handle_icall(cs, bcs, at_state);
14fa73a7
HL
1183 break;
1184 case ACT_FAILSDOWN:
784d5858 1185 dev_warn(cs->dev, "Could not shut down the device.\n");
14fa73a7
HL
1186 /* fall through */
1187 case ACT_FAKESDOWN:
1188 case ACT_SDOWN:
1189 cs->cur_at_seq = SEQ_NONE;
1190 finish_shutdown(cs);
1191 break;
1192 case ACT_CONNECT:
1193 if (cs->onechannel) {
1194 at_state->pending_commands |= PC_DLE1;
9d4bee2b 1195 cs->commands_pending = 1;
14fa73a7
HL
1196 break;
1197 }
1198 bcs->chstate |= CHS_D_UP;
088ec0cc 1199 gigaset_isdn_connD(bcs);
14fa73a7
HL
1200 cs->ops->init_bchannel(bcs);
1201 break;
1202 case ACT_DLE1:
1203 cs->cur_at_seq = SEQ_NONE;
1204 bcs = cs->bcs + cs->curchannel;
1205
1206 bcs->chstate |= CHS_D_UP;
088ec0cc 1207 gigaset_isdn_connD(bcs);
14fa73a7
HL
1208 cs->ops->init_bchannel(bcs);
1209 break;
1210 case ACT_FAKEHUP:
1211 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1212 /* fall through */
1213 case ACT_DISCONNECT:
1214 cs->cur_at_seq = SEQ_NONE;
1215 at_state->cid = -1;
1216 if (bcs && cs->onechannel && cs->dle) {
1217 /* Check for other open channels not needed:
1218 * DLE only used for M10x with one B channel.
1219 */
1220 at_state->pending_commands |= PC_DLE0;
9d4bee2b 1221 cs->commands_pending = 1;
698e3ed9 1222 } else
14fa73a7 1223 disconnect(p_at_state);
14fa73a7
HL
1224 break;
1225 case ACT_FAKEDLE0:
1226 at_state->int_var[VAR_ZDLE] = 0;
1227 cs->dle = 0;
1228 /* fall through */
1229 case ACT_DLE0:
1230 cs->cur_at_seq = SEQ_NONE;
1231 at_state2 = &cs->bcs[cs->curchannel].at_state;
1232 disconnect(&at_state2);
1233 break;
1234 case ACT_ABORTHUP:
1235 cs->cur_at_seq = SEQ_NONE;
784d5858 1236 dev_warn(cs->dev, "Could not hang up.\n");
14fa73a7
HL
1237 at_state->cid = -1;
1238 if (bcs && cs->onechannel)
1239 at_state->pending_commands |= PC_DLE0;
698e3ed9 1240 else
14fa73a7 1241 disconnect(p_at_state);
14fa73a7
HL
1242 schedule_init(cs, MS_RECOVER);
1243 break;
1244 case ACT_FAILDLE0:
1245 cs->cur_at_seq = SEQ_NONE;
5dcd7d84
TS
1246 dev_warn(cs->dev, "Error leaving DLE mode.\n");
1247 cs->dle = 0;
14fa73a7
HL
1248 at_state2 = &cs->bcs[cs->curchannel].at_state;
1249 disconnect(&at_state2);
1250 schedule_init(cs, MS_RECOVER);
1251 break;
1252 case ACT_FAILDLE1:
1253 cs->cur_at_seq = SEQ_NONE;
784d5858
TS
1254 dev_warn(cs->dev,
1255 "Could not enter DLE mode. Trying to hang up.\n");
14fa73a7
HL
1256 channel = cs->curchannel;
1257 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
9d4bee2b 1258 cs->commands_pending = 1;
14fa73a7
HL
1259 break;
1260
1261 case ACT_CID: /* got cid; start dialing */
1262 cs->cur_at_seq = SEQ_NONE;
1263 channel = cs->curchannel;
1264 if (ev->parameter > 0 && ev->parameter <= 65535) {
1265 cs->bcs[channel].at_state.cid = ev->parameter;
1266 cs->bcs[channel].at_state.pending_commands |=
1267 PC_DIAL;
9d4bee2b 1268 cs->commands_pending = 1;
14fa73a7
HL
1269 break;
1270 }
63b203b4 1271 /* bad cid: fall through */
14fa73a7
HL
1272 case ACT_FAILCID:
1273 cs->cur_at_seq = SEQ_NONE;
1274 channel = cs->curchannel;
81fa7b82 1275 if (reinit_and_retry(cs, channel) < 0) {
784d5858
TS
1276 dev_warn(cs->dev,
1277 "Could not get a call ID. Cannot dial.\n");
14fa73a7
HL
1278 at_state2 = &cs->bcs[channel].at_state;
1279 disconnect(&at_state2);
1280 }
1281 break;
1282 case ACT_ABORTCID:
1283 cs->cur_at_seq = SEQ_NONE;
1284 at_state2 = &cs->bcs[cs->curchannel].at_state;
1285 disconnect(&at_state2);
1286 break;
1287
1288 case ACT_DIALING:
1289 case ACT_ACCEPTED:
1290 cs->cur_at_seq = SEQ_NONE;
1291 break;
1292
c35a87ff 1293 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL procssng */
14fa73a7 1294 disconnect(p_at_state);
14fa73a7
HL
1295 break;
1296
1297 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1298 cs->cur_at_seq = SEQ_NONE;
1299 at_state->pending_commands |= PC_HUP;
9d4bee2b 1300 cs->commands_pending = 1;
14fa73a7
HL
1301 break;
1302
1303 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1304 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1305 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1306 at_state->pending_commands |= PC_HUP;
9d4bee2b 1307 cs->commands_pending = 1;
14fa73a7 1308 break;
917f5085 1309 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
784d5858 1310 are not handled properly anymore */
14fa73a7
HL
1311 at_state->getstring = 1;
1312 break;
1313 case ACT_SETVER:
1314 if (!ev->ptr) {
1315 *p_genresp = 1;
1316 *p_resp_code = RSP_ERROR;
1317 break;
1318 }
1319 s = ev->ptr;
1320
1321 if (!strcmp(s, "OK")) {
e055d03d 1322 /* OK without version string: assume old response */
14fa73a7 1323 *p_genresp = 1;
e055d03d 1324 *p_resp_code = RSP_NONE;
14fa73a7
HL
1325 break;
1326 }
1327
1328 for (i = 0; i < 4; ++i) {
1329 val = simple_strtoul(s, (char **) &e, 10);
1330 if (val > INT_MAX || e == s)
1331 break;
1332 if (i == 3) {
1333 if (*e)
1334 break;
1335 } else if (*e != '.')
1336 break;
1337 else
1338 s = e + 1;
1339 cs->fwver[i] = val;
1340 }
1341 if (i != 4) {
1342 *p_genresp = 1;
1343 *p_resp_code = RSP_ERROR;
1344 break;
1345 }
14fa73a7
HL
1346 cs->gotfwver = 0;
1347 break;
1348 case ACT_GOTVER:
1349 if (cs->gotfwver == 0) {
1350 cs->gotfwver = 1;
1528b18f 1351 gig_dbg(DEBUG_EVENT,
784d5858
TS
1352 "firmware version %02d.%03d.%02d.%02d",
1353 cs->fwver[0], cs->fwver[1],
1354 cs->fwver[2], cs->fwver[3]);
14fa73a7
HL
1355 break;
1356 }
1357 /* fall through */
1358 case ACT_FAILVER:
1359 cs->gotfwver = -1;
784d5858 1360 dev_err(cs->dev, "could not read firmware version.\n");
14fa73a7 1361 break;
14fa73a7 1362 case ACT_ERROR:
05eae94f
TS
1363 gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d",
1364 __func__, at_state->ConState);
1365 cs->cur_at_seq = SEQ_NONE;
14fa73a7 1366 break;
14fa73a7 1367 case ACT_DEBUG:
784d5858 1368 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
14fa73a7
HL
1369 __func__, ev->type, at_state->ConState);
1370 break;
1371 case ACT_WARN:
784d5858
TS
1372 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1373 __func__, ev->type, at_state->ConState);
14fa73a7
HL
1374 break;
1375 case ACT_ZCAU:
784d5858
TS
1376 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1377 ev->parameter, at_state->ConState);
14fa73a7
HL
1378 break;
1379
f86936ff
TS
1380 /* events from the LL */
1381
14fa73a7
HL
1382 case ACT_DIAL:
1383 start_dial(at_state, ev->ptr, ev->parameter);
1384 break;
1385 case ACT_ACCEPT:
1386 start_accept(at_state);
1387 break;
14fa73a7
HL
1388 case ACT_HUP:
1389 at_state->pending_commands |= PC_HUP;
1528b18f 1390 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
9d4bee2b 1391 cs->commands_pending = 1;
14fa73a7
HL
1392 break;
1393
f86936ff
TS
1394 /* hotplug events */
1395
14fa73a7
HL
1396 case ACT_STOP:
1397 do_stop(cs);
1398 break;
1399 case ACT_START:
1400 do_start(cs);
1401 break;
1402
f86936ff
TS
1403 /* events from the interface */
1404
14fa73a7
HL
1405 case ACT_IF_LOCK:
1406 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1407 cs->waiting = 0;
1408 wake_up(&cs->waitqueue);
1409 break;
1410 case ACT_IF_VER:
1411 if (ev->parameter != 0)
1412 cs->cmd_result = -EINVAL;
1413 else if (cs->gotfwver != 1) {
1414 cs->cmd_result = -ENOENT;
1415 } else {
1416 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1417 cs->cmd_result = 0;
1418 }
1419 cs->waiting = 0;
1420 wake_up(&cs->waitqueue);
1421 break;
1422
f86936ff
TS
1423 /* events from the proc file system */
1424
14fa73a7 1425 case ACT_PROC_CIDMODE:
69049cc8
TS
1426 spin_lock_irqsave(&cs->lock, flags);
1427 if (ev->parameter != cs->cidmode) {
1428 cs->cidmode = ev->parameter;
14fa73a7
HL
1429 if (ev->parameter) {
1430 cs->at_state.pending_commands |= PC_CIDMODE;
1528b18f 1431 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
14fa73a7
HL
1432 } else {
1433 cs->at_state.pending_commands |= PC_UMMODE;
1528b18f 1434 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
14fa73a7 1435 }
9d4bee2b 1436 cs->commands_pending = 1;
14fa73a7 1437 }
69049cc8 1438 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7
HL
1439 cs->waiting = 0;
1440 wake_up(&cs->waitqueue);
1441 break;
1442
f86936ff
TS
1443 /* events from the hardware drivers */
1444
14fa73a7
HL
1445 case ACT_NOTIFY_BC_DOWN:
1446 bchannel_down(bcs);
1447 break;
1448 case ACT_NOTIFY_BC_UP:
1449 bchannel_up(bcs);
1450 break;
1451 case ACT_SHUTDOWN:
1452 do_shutdown(cs);
1453 break;
1454
1455
1456 default:
1457 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1458 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1459 if (!*pp_command) {
1460 *p_genresp = 1;
1461 *p_resp_code = RSP_NULL;
1462 }
1463 } else
784d5858 1464 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
14fa73a7
HL
1465 }
1466}
1467
1468/* State machine to do the calling and hangup procedure */
1469static void process_event(struct cardstate *cs, struct event_t *ev)
1470{
1471 struct bc_state *bcs;
1472 char *p_command = NULL;
1473 struct reply_t *rep;
1474 int rcode;
1475 int genresp = 0;
1476 int resp_code = RSP_ERROR;
14fa73a7
HL
1477 struct at_state_t *at_state;
1478 int index;
1479 int curact;
1480 unsigned long flags;
1481
14fa73a7
HL
1482 if (ev->cid >= 0) {
1483 at_state = at_state_from_cid(cs, ev->cid);
1484 if (!at_state) {
1528b18f
TS
1485 gig_dbg(DEBUG_EVENT, "event %d for invalid cid %d",
1486 ev->type, ev->cid);
14fa73a7 1487 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
784d5858 1488 NULL, 0, NULL);
14fa73a7
HL
1489 return;
1490 }
1491 } else {
1492 at_state = ev->at_state;
1493 if (at_state_invalid(cs, at_state)) {
1528b18f 1494 gig_dbg(DEBUG_EVENT, "event for invalid at_state %p",
784d5858 1495 at_state);
14fa73a7
HL
1496 return;
1497 }
1498 }
1499
1528b18f 1500 gig_dbg(DEBUG_EVENT, "connection state %d, event %d",
784d5858 1501 at_state->ConState, ev->type);
14fa73a7
HL
1502
1503 bcs = at_state->bcs;
14fa73a7
HL
1504
1505 /* Setting the pointer to the dial array */
1506 rep = at_state->replystruct;
14fa73a7 1507
69049cc8 1508 spin_lock_irqsave(&cs->lock, flags);
14fa73a7 1509 if (ev->type == EV_TIMEOUT) {
69049cc8 1510 if (ev->parameter != at_state->timer_index
14fa73a7
HL
1511 || !at_state->timer_active) {
1512 ev->type = RSP_NONE; /* old timeout */
1528b18f 1513 gig_dbg(DEBUG_EVENT, "old timeout");
63b203b4
TS
1514 } else {
1515 if (at_state->waiting)
1516 gig_dbg(DEBUG_EVENT, "stopped waiting");
1517 else
1518 gig_dbg(DEBUG_EVENT, "timeout occurred");
1519 }
14fa73a7 1520 }
69049cc8 1521 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7 1522
917f5085
TS
1523 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1524 or at_state->str_var[STR_XXXX], set it */
14fa73a7
HL
1525 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1526 index = ev->type - RSP_VAR;
1527 at_state->int_var[index] = ev->parameter;
1528 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1529 index = ev->type - RSP_STR;
1530 kfree(at_state->str_var[index]);
1531 at_state->str_var[index] = ev->ptr;
917f5085
TS
1532 ev->ptr = NULL; /* prevent process_events() from
1533 deallocating ptr */
14fa73a7
HL
1534 }
1535
1536 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1537 at_state->getstring = 0;
1538
917f5085
TS
1539 /* Search row in dial array which matches modem response and current
1540 constate */
14fa73a7
HL
1541 for (;; rep++) {
1542 rcode = rep->resp_code;
14fa73a7
HL
1543 if (rcode == RSP_LAST) {
1544 /* found nothing...*/
784d5858 1545 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
475be4d8 1546 "resp_code %d in ConState %d!\n",
784d5858 1547 __func__, ev->type, at_state->ConState);
14fa73a7
HL
1548 return;
1549 }
1550 if ((rcode == RSP_ANY || rcode == ev->type)
475be4d8
JP
1551 && ((int) at_state->ConState >= rep->min_ConState)
1552 && (rep->max_ConState < 0
1553 || (int) at_state->ConState <= rep->max_ConState)
1554 && (rep->parameter < 0 || rep->parameter == ev->parameter))
14fa73a7
HL
1555 break;
1556 }
1557
1558 p_command = rep->command;
1559
1560 at_state->waiting = 0;
1561 for (curact = 0; curact < MAXACT; ++curact) {
1562 /* The row tells us what we should do ..
1563 */
c35a87ff
TS
1564 do_action(rep->action[curact], cs, bcs, &at_state, &p_command,
1565 &genresp, &resp_code, ev);
14fa73a7 1566 if (!at_state)
63b203b4
TS
1567 /* at_state destroyed by disconnect */
1568 return;
14fa73a7
HL
1569 }
1570
63b203b4
TS
1571 /* Jump to the next con-state regarding the array */
1572 if (rep->new_ConState >= 0)
1573 at_state->ConState = rep->new_ConState;
14fa73a7 1574
63b203b4
TS
1575 if (genresp) {
1576 spin_lock_irqsave(&cs->lock, flags);
1577 at_state->timer_expires = 0;
1578 at_state->timer_active = 0;
1579 spin_unlock_irqrestore(&cs->lock, flags);
1580 gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1581 } else {
1582 /* Send command to modem if not NULL... */
1583 if (p_command) {
1584 if (cs->connected)
1585 send_command(cs, p_command, at_state);
1586 else
1587 gigaset_add_event(cs, at_state, RSP_NODEV,
1588 NULL, 0, NULL);
1589 }
1590
1591 spin_lock_irqsave(&cs->lock, flags);
1592 if (!rep->timeout) {
c35a87ff
TS
1593 at_state->timer_expires = 0;
1594 at_state->timer_active = 0;
63b203b4
TS
1595 } else if (rep->timeout > 0) { /* new timeout */
1596 at_state->timer_expires = rep->timeout * 10;
1597 at_state->timer_active = 1;
1598 ++at_state->timer_index;
14fa73a7 1599 }
63b203b4 1600 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7
HL
1601 }
1602}
1603
1604static void schedule_sequence(struct cardstate *cs,
1605 struct at_state_t *at_state, int sequence)
1606{
1607 cs->cur_at_seq = sequence;
1608 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1609}
1610
1611static void process_command_flags(struct cardstate *cs)
1612{
1613 struct at_state_t *at_state = NULL;
1614 struct bc_state *bcs;
1615 int i;
1616 int sequence;
69049cc8 1617 unsigned long flags;
14fa73a7 1618
9d4bee2b 1619 cs->commands_pending = 0;
14fa73a7
HL
1620
1621 if (cs->cur_at_seq) {
1528b18f 1622 gig_dbg(DEBUG_EVENT, "not searching scheduled commands: busy");
14fa73a7
HL
1623 return;
1624 }
1625
1528b18f 1626 gig_dbg(DEBUG_EVENT, "searching scheduled commands");
14fa73a7
HL
1627
1628 sequence = SEQ_NONE;
1629
1630 /* clear pending_commands and hangup channels on shutdown */
1631 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1632 cs->at_state.pending_commands &= ~PC_CIDMODE;
1633 for (i = 0; i < cs->channels; ++i) {
1634 bcs = cs->bcs + i;
1635 at_state = &bcs->at_state;
1636 at_state->pending_commands &=
1637 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1638 if (at_state->cid > 0)
1639 at_state->pending_commands |= PC_HUP;
1640 if (at_state->pending_commands & PC_CID) {
1641 at_state->pending_commands |= PC_NOCID;
1642 at_state->pending_commands &= ~PC_CID;
1643 }
1644 }
1645 }
1646
1647 /* clear pending_commands and hangup channels on reset */
1648 if (cs->at_state.pending_commands & PC_INIT) {
1649 cs->at_state.pending_commands &= ~PC_CIDMODE;
1650 for (i = 0; i < cs->channels; ++i) {
1651 bcs = cs->bcs + i;
1652 at_state = &bcs->at_state;
1653 at_state->pending_commands &=
1654 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1655 if (at_state->cid > 0)
1656 at_state->pending_commands |= PC_HUP;
9d4bee2b 1657 if (cs->mstate == MS_RECOVER) {
14fa73a7
HL
1658 if (at_state->pending_commands & PC_CID) {
1659 at_state->pending_commands |= PC_NOCID;
1660 at_state->pending_commands &= ~PC_CID;
1661 }
1662 }
1663 }
1664 }
1665
c35a87ff
TS
1666 /* only switch back to unimodem mode if no commands are pending and
1667 * no channels are up */
69049cc8 1668 spin_lock_irqsave(&cs->lock, flags);
14fa73a7 1669 if (cs->at_state.pending_commands == PC_UMMODE
69049cc8 1670 && !cs->cidmode
14fa73a7 1671 && list_empty(&cs->temp_at_states)
9d4bee2b 1672 && cs->mode == M_CID) {
14fa73a7
HL
1673 sequence = SEQ_UMMODE;
1674 at_state = &cs->at_state;
1675 for (i = 0; i < cs->channels; ++i) {
1676 bcs = cs->bcs + i;
1677 if (bcs->at_state.pending_commands ||
1678 bcs->at_state.cid > 0) {
1679 sequence = SEQ_NONE;
1680 break;
1681 }
1682 }
1683 }
69049cc8 1684 spin_unlock_irqrestore(&cs->lock, flags);
14fa73a7
HL
1685 cs->at_state.pending_commands &= ~PC_UMMODE;
1686 if (sequence != SEQ_NONE) {
1687 schedule_sequence(cs, at_state, sequence);
1688 return;
1689 }
1690
1691 for (i = 0; i < cs->channels; ++i) {
1692 bcs = cs->bcs + i;
1693 if (bcs->at_state.pending_commands & PC_HUP) {
d2ca8481
TS
1694 if (cs->dle) {
1695 cs->curchannel = bcs->channel;
1696 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1697 return;
1698 }
14fa73a7
HL
1699 bcs->at_state.pending_commands &= ~PC_HUP;
1700 if (bcs->at_state.pending_commands & PC_CID) {
1701 /* not yet dialing: PC_NOCID is sufficient */
1702 bcs->at_state.pending_commands |= PC_NOCID;
1703 bcs->at_state.pending_commands &= ~PC_CID;
1704 } else {
1705 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1706 return;
1707 }
1708 }
1709 if (bcs->at_state.pending_commands & PC_NOCID) {
1710 bcs->at_state.pending_commands &= ~PC_NOCID;
1711 cs->curchannel = bcs->channel;
1712 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1713 return;
1714 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1715 bcs->at_state.pending_commands &= ~PC_DLE0;
1716 cs->curchannel = bcs->channel;
1717 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1718 return;
1719 }
1720 }
1721
1722 list_for_each_entry(at_state, &cs->temp_at_states, list)
1723 if (at_state->pending_commands & PC_HUP) {
1724 at_state->pending_commands &= ~PC_HUP;
1725 schedule_sequence(cs, at_state, SEQ_HUP);
1726 return;
1727 }
1728
1729 if (cs->at_state.pending_commands & PC_INIT) {
1730 cs->at_state.pending_commands &= ~PC_INIT;
c35a87ff 1731 cs->dle = 0;
14fa73a7 1732 cs->inbuf->inputstate = INS_command;
14fa73a7
HL
1733 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1734 return;
1735 }
1736 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1737 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1738 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1739 return;
1740 }
1741 if (cs->at_state.pending_commands & PC_CIDMODE) {
1742 cs->at_state.pending_commands &= ~PC_CIDMODE;
9d4bee2b 1743 if (cs->mode == M_UNIMODEM) {
14fa73a7 1744 cs->retry_count = 1;
14fa73a7
HL
1745 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1746 return;
1747 }
1748 }
1749
1750 for (i = 0; i < cs->channels; ++i) {
1751 bcs = cs->bcs + i;
1752 if (bcs->at_state.pending_commands & PC_DLE1) {
1753 bcs->at_state.pending_commands &= ~PC_DLE1;
1754 cs->curchannel = bcs->channel;
1755 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1756 return;
1757 }
1758 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1759 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1760 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1761 return;
1762 }
1763 if (bcs->at_state.pending_commands & PC_DIAL) {
1764 bcs->at_state.pending_commands &= ~PC_DIAL;
1765 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1766 return;
1767 }
1768 if (bcs->at_state.pending_commands & PC_CID) {
9d4bee2b 1769 switch (cs->mode) {
14fa73a7
HL
1770 case M_UNIMODEM:
1771 cs->at_state.pending_commands |= PC_CIDMODE;
1528b18f 1772 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
9d4bee2b 1773 cs->commands_pending = 1;
14fa73a7 1774 return;
14fa73a7
HL
1775 case M_UNKNOWN:
1776 schedule_init(cs, MS_INIT);
1777 return;
14fa73a7
HL
1778 }
1779 bcs->at_state.pending_commands &= ~PC_CID;
1780 cs->curchannel = bcs->channel;
14fa73a7 1781 cs->retry_count = 2;
14fa73a7
HL
1782 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1783 return;
1784 }
1785 }
1786}
1787
1788static void process_events(struct cardstate *cs)
1789{
1790 struct event_t *ev;
1791 unsigned head, tail;
1792 int i;
1793 int check_flags = 0;
1794 int was_busy;
69049cc8 1795 unsigned long flags;
14fa73a7 1796
69049cc8
TS
1797 spin_lock_irqsave(&cs->ev_lock, flags);
1798 head = cs->ev_head;
14fa73a7
HL
1799
1800 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
69049cc8 1801 tail = cs->ev_tail;
14fa73a7 1802 if (tail == head) {
9d4bee2b 1803 if (!check_flags && !cs->commands_pending)
14fa73a7
HL
1804 break;
1805 check_flags = 0;
69049cc8 1806 spin_unlock_irqrestore(&cs->ev_lock, flags);
14fa73a7 1807 process_command_flags(cs);
69049cc8
TS
1808 spin_lock_irqsave(&cs->ev_lock, flags);
1809 tail = cs->ev_tail;
14fa73a7 1810 if (tail == head) {
9d4bee2b 1811 if (!cs->commands_pending)
14fa73a7
HL
1812 break;
1813 continue;
1814 }
1815 }
1816
1817 ev = cs->events + head;
1818 was_busy = cs->cur_at_seq != SEQ_NONE;
69049cc8 1819 spin_unlock_irqrestore(&cs->ev_lock, flags);
14fa73a7 1820 process_event(cs, ev);
69049cc8 1821 spin_lock_irqsave(&cs->ev_lock, flags);
14fa73a7
HL
1822 kfree(ev->ptr);
1823 ev->ptr = NULL;
1824 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1825 check_flags = 1;
1826
1827 head = (head + 1) % MAX_EVENTS;
69049cc8 1828 cs->ev_head = head;
14fa73a7
HL
1829 }
1830
69049cc8
TS
1831 spin_unlock_irqrestore(&cs->ev_lock, flags);
1832
14fa73a7 1833 if (i == 2 * MAX_EVENTS) {
784d5858
TS
1834 dev_err(cs->dev,
1835 "infinite loop in process_events; aborting.\n");
14fa73a7
HL
1836 }
1837}
1838
1839/* tasklet scheduled on any event received from the Gigaset device
1840 * parameter:
1841 * data ISDN controller state structure
1842 */
1843void gigaset_handle_event(unsigned long data)
1844{
1845 struct cardstate *cs = (struct cardstate *) data;
1846
14fa73a7 1847 /* handle incoming data on control/common channel */
9d4bee2b 1848 if (cs->inbuf->head != cs->inbuf->tail) {
784d5858 1849 gig_dbg(DEBUG_INTR, "processing new data");
14fa73a7
HL
1850 cs->ops->handle_input(cs->inbuf);
1851 }
1852
1853 process_events(cs);
1854}