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