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