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