blob: 0d5984a8679573af2525d9915592831653db0f29 [file] [log] [blame]
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001/*
2 * Stuff used by all variants of the driver
3 *
Tilman Schmidt70440cf2006-04-10 22:55:14 -07004 * Copyright (c) 2001 by Stefan Eilers,
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08005 * 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 * =====================================================================
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080014 */
15
16#include "gigaset.h"
17
18/* ========================================================== */
19/* bit masks for pending commands */
Tilman Schmidt917f5082006-04-10 22:55:00 -070020#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
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080031
32/* types of modem responses */
Tilman Schmidt917f5082006-04-10 22:55:00 -070033#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
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080040
41/* Possible ASCII responses */
Tilman Schmidt917f5082006-04-10 22:55:00 -070042#define RSP_OK 0
Tilman Schmidtc35a87f2009-10-25 09:30:47 +000043#define RSP_BUSY 1
44#define RSP_CONNECT 2
Tilman Schmidt917f5082006-04-10 22:55:00 -070045#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 */
Tilman Schmidt917f5082006-04-10 22:55:00 -070071#define RSP_UNKNOWN -4 /* unknown response */
72#define RSP_FAIL -5 /* internal error */
73#define RSP_INVAL -6 /* invalid response */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080074
Tilman Schmidt917f5082006-04-10 22:55:00 -070075#define RSP_NONE -19
76#define RSP_STRING -20
77#define RSP_NULL -21
Tilman Schmidtc35a87f2009-10-25 09:30:47 +000078#define RSP_RETRYFAIL -22
79#define RSP_RETRY -23
80#define RSP_SKIP -24
Tilman Schmidt917f5082006-04-10 22:55:00 -070081#define RSP_INIT -27
82#define RSP_ANY -26
83#define RSP_LAST -28
84#define RSP_NODEV -9
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080085
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
Tilman Schmidt917f5082006-04-10 22:55:00 -0700110#define ACT_CONNTIMEOUT 23
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800111#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
Tilman Schmidt917f5082006-04-10 22:55:00 -0700125#define ACT_NOTIFY_BC_DOWN 38
126#define ACT_NOTIFY_BC_UP 39
127#define ACT_DIAL 40
128#define ACT_ACCEPT 41
Tilman Schmidt917f5082006-04-10 22:55:00 -0700129#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
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800143#define ACT_CMD 100
144
145/* at command sequences */
Tilman Schmidt917f5082006-04-10 22:55:00 -0700146#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
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800158
159
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000160/* 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid),
161 * 400: hup, 500: reset, 600: dial, 700: ring */
Tilman Schmidt528efc62009-05-13 12:44:17 +0000162struct reply_t gigaset_tab_nocid[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800163{
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000164/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
165 * action, command */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800166
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000167/* initialize device, set cid mode if possible */
168{RSP_INIT, -1, -1, SEQ_INIT, 100, 1, {ACT_TIMEOUT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800169
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000170{EV_TIMEOUT, 100, 100, -1, 101, 3, {0}, "Z\r"},
171{RSP_OK, 101, 103, -1, 120, 5, {ACT_GETSTRING},
172 "+GMR\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800173
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000174{EV_TIMEOUT, 101, 101, -1, 102, 5, {0}, "Z\r"},
175{RSP_ERROR, 101, 101, -1, 102, 5, {0}, "Z\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800176
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000177{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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800183
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000184{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"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800188
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000189{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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800193
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000194{RSP_ERROR, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
195{EV_TIMEOUT, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800196
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000197{RSP_STRING, 120, 120, -1, 121, -1, {ACT_SETVER} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800198
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000199{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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800205
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000206/* 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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800213
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000214/* 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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800220
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000221/* incoming call */
222{RSP_RING, -1, -1, -1, -1, -1, {ACT_RING} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800223
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000224/* 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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800230
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000231/* 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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800236
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000237/* 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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800242
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000243/* abort getting cid */
244{RSP_INIT, 0, 0, SEQ_NOCID, 0, 0, {ACT_ABORTCID} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800245
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000246/* 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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800252
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000253{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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800259
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000260/* 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} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800268
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000269{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}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800273};
274
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000275/* 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring,
276 * 400: hup, 750: accepted icall */
Tilman Schmidt528efc62009-05-13 12:44:17 +0000277struct reply_t gigaset_tab_cid[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800278{
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000279/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
280 * action, command */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800281
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000282/* dial */
283{EV_DIAL, -1, -1, -1, -1, -1, {ACT_DIAL} },
284{RSP_INIT, 0, 0, SEQ_DIAL, 601, 5, {ACT_CMD+AT_BC} },
Tilman Schmidt1ce368f2010-06-21 13:55:05 +0000285{RSP_OK, 601, 601, -1, 603, 5, {ACT_CMD+AT_PROTO} },
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000286{RSP_OK, 603, 603, -1, 604, 5, {ACT_CMD+AT_TYPE} },
287{RSP_OK, 604, 604, -1, 605, 5, {ACT_CMD+AT_MSN} },
288{RSP_NULL, 605, 605, -1, 606, 5, {ACT_CMD+AT_CLIP} },
289{RSP_OK, 605, 605, -1, 606, 5, {ACT_CMD+AT_CLIP} },
290{RSP_NULL, 606, 606, -1, 607, 5, {ACT_CMD+AT_ISO} },
291{RSP_OK, 606, 606, -1, 607, 5, {ACT_CMD+AT_ISO} },
292{RSP_OK, 607, 607, -1, 608, 5, {0}, "+VLS=17\r"},
293{RSP_OK, 608, 608, -1, 609, -1},
294{RSP_ZSAU, 609, 609, ZSAU_PROCEEDING, 610, 5, {ACT_CMD+AT_DIAL} },
295{RSP_OK, 610, 610, -1, 650, 0, {ACT_DIALING} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800296
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000297{RSP_ERROR, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
298{EV_TIMEOUT, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800299
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000300/* optional dialing responses */
301{EV_BC_OPEN, 650, 650, -1, 651, -1},
302{RSP_ZVLS, 609, 651, 17, -1, -1, {ACT_DEBUG} },
303{RSP_ZCTP, 610, 651, -1, -1, -1, {ACT_DEBUG} },
304{RSP_ZCPN, 610, 651, -1, -1, -1, {ACT_DEBUG} },
305{RSP_ZSAU, 650, 651, ZSAU_CALL_DELIVERED, -1, -1, {ACT_DEBUG} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800306
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000307/* connect */
308{RSP_ZSAU, 650, 650, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
309{RSP_ZSAU, 651, 651, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
310 ACT_NOTIFY_BC_UP} },
311{RSP_ZSAU, 750, 750, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
312{RSP_ZSAU, 751, 751, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
313 ACT_NOTIFY_BC_UP} },
314{EV_BC_OPEN, 800, 800, -1, 800, -1, {ACT_NOTIFY_BC_UP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800315
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000316/* remote hangup */
317{RSP_ZSAU, 650, 651, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT} },
318{RSP_ZSAU, 750, 751, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
319{RSP_ZSAU, 800, 800, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800320
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000321/* hangup */
322{EV_HUP, -1, -1, -1, -1, -1, {ACT_HUP} },
323{RSP_INIT, -1, -1, SEQ_HUP, 401, 5, {0}, "+VLS=0\r"},
324{RSP_OK, 401, 401, -1, 402, 5},
325{RSP_ZVLS, 402, 402, 0, 403, 5},
326{RSP_ZSAU, 403, 403, ZSAU_DISCONNECT_REQ, -1, -1, {ACT_DEBUG} },
327{RSP_ZSAU, 403, 403, ZSAU_NULL, 0, 0, {ACT_DISCONNECT} },
328{RSP_NODEV, 401, 403, -1, 0, 0, {ACT_FAKEHUP} },
329{RSP_ERROR, 401, 401, -1, 0, 0, {ACT_ABORTHUP} },
330{EV_TIMEOUT, 401, 403, -1, 0, 0, {ACT_ABORTHUP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800331
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000332{EV_BC_CLOSED, 0, 0, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800333
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000334/* ring */
335{RSP_ZBC, 700, 700, -1, -1, -1, {0} },
336{RSP_ZHLC, 700, 700, -1, -1, -1, {0} },
337{RSP_NMBR, 700, 700, -1, -1, -1, {0} },
338{RSP_ZCPN, 700, 700, -1, -1, -1, {0} },
339{RSP_ZCTP, 700, 700, -1, -1, -1, {0} },
340{EV_TIMEOUT, 700, 700, -1, 720, 720, {ACT_ICALL} },
341{EV_BC_CLOSED, 720, 720, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800342
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000343/*accept icall*/
344{EV_ACCEPT, -1, -1, -1, -1, -1, {ACT_ACCEPT} },
345{RSP_INIT, 720, 720, SEQ_ACCEPT, 721, 5, {ACT_CMD+AT_PROTO} },
346{RSP_OK, 721, 721, -1, 722, 5, {ACT_CMD+AT_ISO} },
347{RSP_OK, 722, 722, -1, 723, 5, {0}, "+VLS=17\r"},
348{RSP_OK, 723, 723, -1, 724, 5, {0} },
349{RSP_ZVLS, 724, 724, 17, 750, 50, {ACT_ACCEPTED} },
350{RSP_ERROR, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
351{EV_TIMEOUT, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
352{RSP_ZSAU, 700, 729, ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT} },
353{RSP_ZSAU, 700, 729, ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT} },
354{RSP_ZSAU, 700, 729, ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800355
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000356{EV_BC_OPEN, 750, 750, -1, 751, -1},
357{EV_TIMEOUT, 750, 751, -1, 0, 0, {ACT_CONNTIMEOUT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800358
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000359/* B channel closed (general case) */
360{EV_BC_CLOSED, -1, -1, -1, -1, -1, {ACT_NOTIFY_BC_DOWN} },
Tilman Schmidt73a88812006-04-22 02:35:30 -0700361
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000362/* misc. */
363{RSP_ZCON, -1, -1, -1, -1, -1, {ACT_DEBUG} },
364{RSP_ZCCR, -1, -1, -1, -1, -1, {ACT_DEBUG} },
365{RSP_ZAOC, -1, -1, -1, -1, -1, {ACT_DEBUG} },
366{RSP_ZCSTR, -1, -1, -1, -1, -1, {ACT_DEBUG} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800367
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000368{RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
369{RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
370{RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
371{RSP_LAST}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800372};
373
374
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000375static const struct resp_type_t {
376 unsigned char *response;
377 int resp_code;
378 int type;
379} resp_type[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800380{
Tilman Schmidt917f5082006-04-10 22:55:00 -0700381 {"OK", RSP_OK, RT_NOTHING},
382 {"ERROR", RSP_ERROR, RT_NOTHING},
383 {"ZSAU", RSP_ZSAU, RT_ZSAU},
384 {"ZCAU", RSP_ZCAU, RT_ZCAU},
385 {"RING", RSP_RING, RT_RING},
386 {"ZGCI", RSP_ZGCI, RT_NUMBER},
387 {"ZVLS", RSP_ZVLS, RT_NUMBER},
388 {"ZCTP", RSP_ZCTP, RT_NUMBER},
389 {"ZDLE", RSP_ZDLE, RT_NUMBER},
390 {"ZCFGT", RSP_ZCFGT, RT_NUMBER},
391 {"ZCCR", RSP_ZCCR, RT_NUMBER},
392 {"ZMWI", RSP_ZMWI, RT_NUMBER},
393 {"ZHLC", RSP_ZHLC, RT_STRING},
394 {"ZBC", RSP_ZBC, RT_STRING},
395 {"NMBR", RSP_NMBR, RT_STRING},
396 {"ZCPN", RSP_ZCPN, RT_STRING},
397 {"ZCON", RSP_ZCON, RT_STRING},
398 {"ZAOC", RSP_ZAOC, RT_STRING},
399 {"ZCSTR", RSP_ZCSTR, RT_STRING},
400 {"ZCFG", RSP_ZCFG, RT_HEX},
401 {"ZLOG", RSP_ZLOG, RT_NOTHING},
402 {"ZABINFO", RSP_ZABINFO, RT_NOTHING},
403 {"ZSMLSTCHG", RSP_ZSMLSTCHG, RT_NOTHING},
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000404 {NULL, 0, 0}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800405};
406
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000407static const struct zsau_resp_t {
408 unsigned char *str;
409 int code;
410} zsau_resp[] =
411{
412 {"OUTGOING_CALL_PROCEEDING", ZSAU_OUTGOING_CALL_PROCEEDING},
413 {"CALL_DELIVERED", ZSAU_CALL_DELIVERED},
414 {"ACTIVE", ZSAU_ACTIVE},
415 {"DISCONNECT_IND", ZSAU_DISCONNECT_IND},
416 {"NULL", ZSAU_NULL},
417 {"DISCONNECT_REQ", ZSAU_DISCONNECT_REQ},
418 {NULL, ZSAU_UNKNOWN}
419};
420
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800421/*
422 * Get integer from char-pointer
423 */
424static int isdn_getnum(char *p)
425{
426 int v = -1;
427
Tilman Schmidt1528b182010-02-22 13:09:52 +0000428 gig_dbg(DEBUG_EVENT, "string: %s", p);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800429
430 while (*p >= '0' && *p <= '9')
431 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p++) - '0');
432 if (*p)
433 v = -1; /* invalid Character */
434 return v;
435}
436
437/*
438 * Get integer from char-pointer
439 */
440static int isdn_gethex(char *p)
441{
442 int v = 0;
443 int c;
444
Tilman Schmidt1528b182010-02-22 13:09:52 +0000445 gig_dbg(DEBUG_EVENT, "string: %s", p);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800446
447 if (!*p)
448 return -1;
449
450 do {
451 if (v > (INT_MAX - 15) / 16)
452 return -1;
453 c = *p;
454 if (c >= '0' && c <= '9')
455 c -= '0';
456 else if (c >= 'a' && c <= 'f')
457 c -= 'a' - 10;
458 else if (c >= 'A' && c <= 'F')
459 c -= 'A' - 10;
460 else
461 return -1;
462 v = v * 16 + c;
463 } while (*++p);
464
465 return v;
466}
467
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800468/* retrieve CID from parsed response
469 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
470 */
471static int cid_of_response(char *s)
472{
473 int cid;
474
475 if (s[-1] != ';')
476 return 0; /* no CID separator */
477 cid = isdn_getnum(s);
478 if (cid < 0)
479 return 0; /* CID not numeric */
480 if (cid < 1 || cid > 65535)
481 return -1; /* CID out of range */
482 return cid;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800483}
484
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000485/**
486 * gigaset_handle_modem_response() - process received modem response
487 * @cs: device descriptor structure.
488 *
489 * Called by asyncdata/isocdata if a block of data received from the
490 * device must be processed as a modem command response. The data is
491 * already in the cs structure.
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800492 */
493void gigaset_handle_modem_response(struct cardstate *cs)
494{
495 unsigned char *argv[MAX_REC_PARAMS + 1];
496 int params;
497 int i, j;
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700498 const struct resp_type_t *rt;
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000499 const struct zsau_resp_t *zr;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800500 int curarg;
501 unsigned long flags;
502 unsigned next, tail, head;
503 struct event_t *event;
504 int resp_code;
505 int param_type;
506 int abort;
507 size_t len;
508 int cid;
509 int rawstring;
510
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800511 len = cs->cbytes;
512 if (!len) {
513 /* ignore additional LFs/CRs (M10x config mode or cx100) */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700514 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[len]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800515 return;
516 }
517 cs->respdata[len] = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800518 argv[0] = cs->respdata;
519 params = 1;
520 if (cs->at_state.getstring) {
521 /* getstring only allowed without cid at the moment */
522 cs->at_state.getstring = 0;
523 rawstring = 1;
524 cid = 0;
525 } else {
526 /* parse line */
527 for (i = 0; i < len; i++)
528 switch (cs->respdata[i]) {
529 case ';':
530 case ',':
531 case '=':
532 if (params > MAX_REC_PARAMS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700533 dev_warn(cs->dev,
534 "too many parameters in response\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800535 /* need last parameter (might be CID) */
536 params--;
537 }
538 argv[params++] = cs->respdata + i + 1;
539 }
540
541 rawstring = 0;
542 cid = params > 1 ? cid_of_response(argv[params-1]) : 0;
543 if (cid < 0) {
544 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700545 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800546 return;
547 }
548
549 for (j = 1; j < params; ++j)
550 argv[j][-1] = 0;
551
Tilman Schmidt1528b182010-02-22 13:09:52 +0000552 gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800553 if (cid) {
554 --params;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000555 gig_dbg(DEBUG_EVENT, "CID: %s", argv[params]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800556 }
Tilman Schmidt1528b182010-02-22 13:09:52 +0000557 gig_dbg(DEBUG_EVENT, "available params: %d", params - 1);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800558 for (j = 1; j < params; j++)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000559 gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800560 }
561
562 spin_lock_irqsave(&cs->ev_lock, flags);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700563 head = cs->ev_head;
564 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800565
566 abort = 1;
567 curarg = 0;
568 while (curarg < params) {
569 next = (tail + 1) % MAX_EVENTS;
570 if (unlikely(next == head)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700571 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800572 break;
573 }
574
575 event = cs->events + tail;
576 event->at_state = NULL;
577 event->cid = cid;
578 event->ptr = NULL;
579 event->arg = NULL;
580 tail = next;
581
582 if (rawstring) {
583 resp_code = RSP_STRING;
584 param_type = RT_STRING;
585 } else {
586 for (rt = resp_type; rt->response; ++rt)
587 if (!strcmp(argv[curarg], rt->response))
588 break;
589
590 if (!rt->response) {
591 event->type = RSP_UNKNOWN;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700592 dev_warn(cs->dev,
593 "unknown modem response: %s\n",
594 argv[curarg]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800595 break;
596 }
597
598 resp_code = rt->resp_code;
599 param_type = rt->type;
600 ++curarg;
601 }
602
603 event->type = resp_code;
604
605 switch (param_type) {
606 case RT_NOTHING:
607 break;
608 case RT_RING:
609 if (!cid) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700610 dev_err(cs->dev,
611 "received RING without CID!\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800612 event->type = RSP_INVAL;
613 abort = 1;
614 } else {
615 event->cid = 0;
616 event->parameter = cid;
617 abort = 0;
618 }
619 break;
620 case RT_ZSAU:
621 if (curarg >= params) {
622 event->parameter = ZSAU_NONE;
623 break;
624 }
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000625 for (zr = zsau_resp; zr->str; ++zr)
626 if (!strcmp(argv[curarg], zr->str))
627 break;
628 event->parameter = zr->code;
629 if (!zr->str)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700630 dev_warn(cs->dev,
631 "%s: unknown parameter %s after ZSAU\n",
632 __func__, argv[curarg]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800633 ++curarg;
634 break;
635 case RT_STRING:
636 if (curarg < params) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700637 event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
638 if (!event->ptr)
639 dev_err(cs->dev, "out of memory\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800640 ++curarg;
641 }
Tilman Schmidt1528b182010-02-22 13:09:52 +0000642 gig_dbg(DEBUG_EVENT, "string==%s",
Tilman Schmidt236b87c2008-12-26 01:22:03 -0800643 event->ptr ? (char *) event->ptr : "NULL");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800644 break;
645 case RT_ZCAU:
646 event->parameter = -1;
647 if (curarg + 1 < params) {
648 i = isdn_gethex(argv[curarg]);
649 j = isdn_gethex(argv[curarg + 1]);
650 if (i >= 0 && i < 256 && j >= 0 && j < 256)
651 event->parameter = (unsigned) i << 8
Tilman Schmidt784d5852006-04-10 22:55:04 -0700652 | j;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800653 curarg += 2;
654 } else
655 curarg = params - 1;
656 break;
657 case RT_NUMBER:
658 case RT_HEX:
659 if (curarg < params) {
660 if (param_type == RT_HEX)
661 event->parameter =
662 isdn_gethex(argv[curarg]);
663 else
664 event->parameter =
665 isdn_getnum(argv[curarg]);
666 ++curarg;
667 } else
668 event->parameter = -1;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000669 gig_dbg(DEBUG_EVENT, "parameter==%d", event->parameter);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800670 break;
671 }
672
673 if (resp_code == RSP_ZDLE)
674 cs->dle = event->parameter;
675
676 if (abort)
677 break;
678 }
679
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700680 cs->ev_tail = tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800681 spin_unlock_irqrestore(&cs->ev_lock, flags);
682
683 if (curarg != params)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000684 gig_dbg(DEBUG_EVENT,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700685 "invalid number of processed parameters: %d/%d",
686 curarg, params);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800687}
688EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
689
690/* disconnect
691 * process closing of connection associated with given AT state structure
692 */
693static void disconnect(struct at_state_t **at_state_p)
694{
695 unsigned long flags;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700696 struct bc_state *bcs = (*at_state_p)->bcs;
697 struct cardstate *cs = (*at_state_p)->cs;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800698
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700699 spin_lock_irqsave(&cs->lock, flags);
700 ++(*at_state_p)->seq_index;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800701
702 /* revert to selected idle mode */
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700703 if (!cs->cidmode) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800704 cs->at_state.pending_commands |= PC_UMMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000705 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800706 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800707 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700708 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800709
710 if (bcs) {
711 /* B channel assigned: invoke hardware specific handler */
712 cs->ops->close_bchannel(bcs);
Tilman Schmidt3305adf2009-10-06 12:18:26 +0000713 /* notify LL */
714 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
715 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000716 gigaset_isdn_hupD(bcs);
Tilman Schmidt3305adf2009-10-06 12:18:26 +0000717 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800718 } else {
719 /* no B channel assigned: just deallocate */
720 spin_lock_irqsave(&cs->lock, flags);
721 list_del(&(*at_state_p)->list);
722 kfree(*at_state_p);
723 *at_state_p = NULL;
724 spin_unlock_irqrestore(&cs->lock, flags);
725 }
726}
727
728/* get_free_channel
729 * get a free AT state structure: either one of those associated with the
730 * B channels of the Gigaset device, or if none of those is available,
731 * a newly allocated one with bcs=NULL
732 * The structure should be freed by calling disconnect() after use.
733 */
734static inline struct at_state_t *get_free_channel(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700735 int cid)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800736/* cids: >0: siemens-cid
737 0: without cid
738 -1: no cid assigned yet
739*/
740{
741 unsigned long flags;
742 int i;
743 struct at_state_t *ret;
744
745 for (i = 0; i < cs->channels; ++i)
746 if (gigaset_get_channel(cs->bcs + i)) {
747 ret = &cs->bcs[i].at_state;
748 ret->cid = cid;
749 return ret;
750 }
751
752 spin_lock_irqsave(&cs->lock, flags);
753 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
754 if (ret) {
755 gigaset_at_init(ret, NULL, cs, cid);
756 list_add(&ret->list, &cs->temp_at_states);
757 }
758 spin_unlock_irqrestore(&cs->lock, flags);
759 return ret;
760}
761
762static void init_failed(struct cardstate *cs, int mode)
763{
764 int i;
765 struct at_state_t *at_state;
766
767 cs->at_state.pending_commands &= ~PC_INIT;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800768 cs->mode = mode;
769 cs->mstate = MS_UNINITIALIZED;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800770 gigaset_free_channels(cs);
771 for (i = 0; i < cs->channels; ++i) {
772 at_state = &cs->bcs[i].at_state;
773 if (at_state->pending_commands & PC_CID) {
774 at_state->pending_commands &= ~PC_CID;
775 at_state->pending_commands |= PC_NOCID;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800776 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800777 }
778 }
779}
780
781static void schedule_init(struct cardstate *cs, int state)
782{
783 if (cs->at_state.pending_commands & PC_INIT) {
Tilman Schmidt1528b182010-02-22 13:09:52 +0000784 gig_dbg(DEBUG_EVENT, "not scheduling PC_INIT again");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800785 return;
786 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800787 cs->mstate = state;
788 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800789 gigaset_block_channels(cs);
790 cs->at_state.pending_commands |= PC_INIT;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000791 gig_dbg(DEBUG_EVENT, "Scheduling PC_INIT");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800792 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800793}
794
Tilman Schmidt917f5082006-04-10 22:55:00 -0700795/* Add "AT" to a command, add the cid, dle encode it, send the result to the
796 hardware. */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800797static void send_command(struct cardstate *cs, const char *cmd, int cid,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700798 int dle, gfp_t kmallocflags)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800799{
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000800 struct cmdbuf_t *cb;
801 size_t buflen;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800802
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000803 buflen = strlen(cmd) + 12; /* DLE ( A T 1 2 3 4 5 <cmd> DLE ) \0 */
804 cb = kmalloc(sizeof(struct cmdbuf_t) + buflen, kmallocflags);
805 if (!cb) {
806 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700807 return;
808 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000809 if (cid > 0 && cid <= 65535)
810 cb->len = snprintf(cb->buf, buflen,
811 dle ? "\020(AT%d%s\020)" : "AT%d%s",
812 cid, cmd);
813 else
814 cb->len = snprintf(cb->buf, buflen,
815 dle ? "\020(AT%s\020)" : "AT%s",
816 cmd);
817 cb->offset = 0;
818 cb->next = NULL;
819 cb->wake_tasklet = NULL;
820 cs->ops->write_cmd(cs, cb);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800821}
822
823static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
824{
825 struct at_state_t *at_state;
826 int i;
827 unsigned long flags;
828
829 if (cid == 0)
830 return &cs->at_state;
831
832 for (i = 0; i < cs->channels; ++i)
833 if (cid == cs->bcs[i].at_state.cid)
834 return &cs->bcs[i].at_state;
835
836 spin_lock_irqsave(&cs->lock, flags);
837
838 list_for_each_entry(at_state, &cs->temp_at_states, list)
839 if (cid == at_state->cid) {
840 spin_unlock_irqrestore(&cs->lock, flags);
841 return at_state;
842 }
843
844 spin_unlock_irqrestore(&cs->lock, flags);
845
846 return NULL;
847}
848
849static void bchannel_down(struct bc_state *bcs)
850{
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800851 if (bcs->chstate & CHS_B_UP) {
852 bcs->chstate &= ~CHS_B_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000853 gigaset_isdn_hupB(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800854 }
855
856 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
857 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000858 gigaset_isdn_hupD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800859 }
860
861 gigaset_free_channel(bcs);
862
863 gigaset_bcs_reinit(bcs);
864}
865
866static void bchannel_up(struct bc_state *bcs)
867{
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800868 if (bcs->chstate & CHS_B_UP) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700869 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
870 __func__);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800871 return;
872 }
873
874 bcs->chstate |= CHS_B_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000875 gigaset_isdn_connB(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800876}
877
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000878static void start_dial(struct at_state_t *at_state, void *data,
879 unsigned seq_index)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800880{
881 struct bc_state *bcs = at_state->bcs;
882 struct cardstate *cs = at_state->cs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000883 char **commands = data;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700884 unsigned long flags;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000885 int i;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800886
887 bcs->chstate |= CHS_NOTIFY_LL;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800888
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700889 spin_lock_irqsave(&cs->lock, flags);
890 if (at_state->seq_index != seq_index) {
891 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800892 goto error;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700893 }
894 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800895
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000896 for (i = 0; i < AT_NUM; ++i) {
897 kfree(bcs->commands[i]);
898 bcs->commands[i] = commands[i];
899 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800900
901 at_state->pending_commands |= PC_CID;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000902 gig_dbg(DEBUG_EVENT, "Scheduling PC_CID");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800903 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800904 return;
905
906error:
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000907 for (i = 0; i < AT_NUM; ++i) {
908 kfree(commands[i]);
909 commands[i] = NULL;
910 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800911 at_state->pending_commands |= PC_NOCID;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000912 gig_dbg(DEBUG_EVENT, "Scheduling PC_NOCID");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800913 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800914 return;
915}
916
917static void start_accept(struct at_state_t *at_state)
918{
919 struct cardstate *cs = at_state->cs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000920 struct bc_state *bcs = at_state->bcs;
921 int i;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800922
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000923 for (i = 0; i < AT_NUM; ++i) {
924 kfree(bcs->commands[i]);
925 bcs->commands[i] = NULL;
926 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800927
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000928 bcs->commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
929 bcs->commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
930 if (!bcs->commands[AT_PROTO] || !bcs->commands[AT_ISO]) {
931 dev_err(at_state->cs->dev, "out of memory\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800932 /* error reset */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800933 at_state->pending_commands |= PC_HUP;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000934 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800935 cs->commands_pending = 1;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000936 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800937 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000938
939 snprintf(bcs->commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
940 snprintf(bcs->commands[AT_ISO], 9, "^SISO=%u\r", bcs->channel + 1);
941
942 at_state->pending_commands |= PC_ACCEPT;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000943 gig_dbg(DEBUG_EVENT, "Scheduling PC_ACCEPT");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000944 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800945}
946
947static void do_start(struct cardstate *cs)
948{
949 gigaset_free_channels(cs);
950
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800951 if (cs->mstate != MS_LOCKED)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800952 schedule_init(cs, MS_INIT);
953
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700954 cs->isdn_up = 1;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000955 gigaset_isdn_start(cs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800956
957 cs->waiting = 0;
958 wake_up(&cs->waitqueue);
959}
960
961static void finish_shutdown(struct cardstate *cs)
962{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800963 if (cs->mstate != MS_LOCKED) {
964 cs->mstate = MS_UNINITIALIZED;
965 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800966 }
967
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700968 /* Tell the LL that the device is not available .. */
969 if (cs->isdn_up) {
970 cs->isdn_up = 0;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000971 gigaset_isdn_stop(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700972 }
973
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800974 /* The rest is done by cleanup_cs () in user mode. */
975
976 cs->cmd_result = -ENODEV;
977 cs->waiting = 0;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800978 wake_up(&cs->waitqueue);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800979}
980
981static void do_shutdown(struct cardstate *cs)
982{
983 gigaset_block_channels(cs);
984
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800985 if (cs->mstate == MS_READY) {
986 cs->mstate = MS_SHUTDOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800987 cs->at_state.pending_commands |= PC_SHUTDOWN;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000988 gig_dbg(DEBUG_EVENT, "Scheduling PC_SHUTDOWN");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800989 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800990 } else
991 finish_shutdown(cs);
992}
993
994static void do_stop(struct cardstate *cs)
995{
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700996 unsigned long flags;
997
998 spin_lock_irqsave(&cs->lock, flags);
999 cs->connected = 0;
1000 spin_unlock_irqrestore(&cs->lock, flags);
1001
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001002 do_shutdown(cs);
1003}
1004
1005/* Entering cid mode or getting a cid failed:
1006 * try to initialize the device and try again.
1007 *
1008 * channel >= 0: getting cid for the channel failed
1009 * channel < 0: entering cid mode failed
1010 *
1011 * returns 0 on failure
1012 */
1013static int reinit_and_retry(struct cardstate *cs, int channel)
1014{
1015 int i;
1016
1017 if (--cs->retry_count <= 0)
1018 return 0;
1019
1020 for (i = 0; i < cs->channels; ++i)
1021 if (cs->bcs[i].at_state.cid > 0)
1022 return 0;
1023
1024 if (channel < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -07001025 dev_warn(cs->dev,
1026 "Could not enter cid mode. Reinit device and try again.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001027 else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001028 dev_warn(cs->dev,
1029 "Could not get a call id. Reinit device and try again.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001030 cs->bcs[channel].at_state.pending_commands |= PC_CID;
1031 }
1032 schedule_init(cs, MS_INIT);
1033 return 1;
1034}
1035
1036static int at_state_invalid(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001037 struct at_state_t *test_ptr)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001038{
1039 unsigned long flags;
1040 unsigned channel;
1041 struct at_state_t *at_state;
1042 int retval = 0;
1043
1044 spin_lock_irqsave(&cs->lock, flags);
1045
1046 if (test_ptr == &cs->at_state)
1047 goto exit;
1048
1049 list_for_each_entry(at_state, &cs->temp_at_states, list)
1050 if (at_state == test_ptr)
1051 goto exit;
1052
1053 for (channel = 0; channel < cs->channels; ++channel)
1054 if (&cs->bcs[channel].at_state == test_ptr)
1055 goto exit;
1056
1057 retval = 1;
1058exit:
1059 spin_unlock_irqrestore(&cs->lock, flags);
1060 return retval;
1061}
1062
1063static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
1064 struct at_state_t **p_at_state)
1065{
1066 int retval;
1067 struct at_state_t *at_state = *p_at_state;
1068
1069 retval = gigaset_isdn_icall(at_state);
1070 switch (retval) {
1071 case ICALL_ACCEPT:
1072 break;
1073 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001074 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001075 /* --v-- fall through --v-- */
1076 case ICALL_IGNORE:
1077 case ICALL_REJECT:
1078 /* hang up actively
1079 * Device doc says that would reject the call.
1080 * In fact it doesn't.
1081 */
1082 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001083 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001084 break;
1085 }
1086}
1087
1088static int do_lock(struct cardstate *cs)
1089{
1090 int mode;
1091 int i;
1092
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001093 switch (cs->mstate) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001094 case MS_UNINITIALIZED:
1095 case MS_READY:
1096 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1097 cs->at_state.pending_commands)
1098 return -EBUSY;
1099
1100 for (i = 0; i < cs->channels; ++i)
1101 if (cs->bcs[i].at_state.pending_commands)
1102 return -EBUSY;
1103
1104 if (!gigaset_get_channels(cs))
1105 return -EBUSY;
1106
1107 break;
1108 case MS_LOCKED:
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001109 break;
1110 default:
1111 return -EBUSY;
1112 }
1113
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001114 mode = cs->mode;
1115 cs->mstate = MS_LOCKED;
1116 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001117
1118 return mode;
1119}
1120
1121static int do_unlock(struct cardstate *cs)
1122{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001123 if (cs->mstate != MS_LOCKED)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001124 return -EINVAL;
1125
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001126 cs->mstate = MS_UNINITIALIZED;
1127 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001128 gigaset_free_channels(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001129 if (cs->connected)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001130 schedule_init(cs, MS_INIT);
1131
1132 return 0;
1133}
1134
1135static void do_action(int action, struct cardstate *cs,
1136 struct bc_state *bcs,
1137 struct at_state_t **p_at_state, char **pp_command,
1138 int *p_genresp, int *p_resp_code,
1139 struct event_t *ev)
1140{
1141 struct at_state_t *at_state = *p_at_state;
1142 struct at_state_t *at_state2;
1143 unsigned long flags;
1144
1145 int channel;
1146
1147 unsigned char *s, *e;
1148 int i;
1149 unsigned long val;
1150
1151 switch (action) {
1152 case ACT_NOTHING:
1153 break;
1154 case ACT_TIMEOUT:
1155 at_state->waiting = 1;
1156 break;
1157 case ACT_INIT:
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001158 cs->at_state.pending_commands &= ~PC_INIT;
1159 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001160 cs->mode = M_UNIMODEM;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001161 spin_lock_irqsave(&cs->lock, flags);
1162 if (!cs->cidmode) {
1163 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001164 gigaset_free_channels(cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001165 cs->mstate = MS_READY;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001166 break;
1167 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001168 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001169 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001170 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001171 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001172 break;
1173 case ACT_FAILINIT:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001174 dev_warn(cs->dev, "Could not initialize the device.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001175 cs->dle = 0;
1176 init_failed(cs, M_UNKNOWN);
1177 cs->cur_at_seq = SEQ_NONE;
1178 break;
1179 case ACT_CONFIGMODE:
1180 init_failed(cs, M_CONFIG);
1181 cs->cur_at_seq = SEQ_NONE;
1182 break;
1183 case ACT_SETDLE1:
1184 cs->dle = 1;
1185 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1186 cs->inbuf[0].inputstate &=
1187 ~(INS_command | INS_DLE_command);
1188 break;
1189 case ACT_SETDLE0:
1190 cs->dle = 0;
1191 cs->inbuf[0].inputstate =
1192 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1193 | INS_command;
1194 break;
1195 case ACT_CMODESET:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001196 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001197 gigaset_free_channels(cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001198 cs->mstate = MS_READY;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001199 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001200 cs->mode = M_CID;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001201 cs->cur_at_seq = SEQ_NONE;
1202 break;
1203 case ACT_UMODESET:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001204 cs->mode = M_UNIMODEM;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001205 cs->cur_at_seq = SEQ_NONE;
1206 break;
1207 case ACT_FAILCMODE:
1208 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001209 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001210 init_failed(cs, M_UNKNOWN);
1211 break;
1212 }
1213 if (!reinit_and_retry(cs, -1))
1214 schedule_init(cs, MS_RECOVER);
1215 break;
1216 case ACT_FAILUMODE:
1217 cs->cur_at_seq = SEQ_NONE;
1218 schedule_init(cs, MS_RECOVER);
1219 break;
1220 case ACT_HUPMODEM:
1221 /* send "+++" (hangup in unimodem mode) */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001222 if (cs->connected) {
1223 struct cmdbuf_t *cb;
1224
1225 cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC);
1226 if (!cb) {
1227 dev_err(cs->dev, "%s: out of memory\n",
1228 __func__);
1229 return;
1230 }
1231 memcpy(cb->buf, "+++", 3);
1232 cb->len = 3;
1233 cb->offset = 0;
1234 cb->next = NULL;
1235 cb->wake_tasklet = NULL;
1236 cs->ops->write_cmd(cs, cb);
1237 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001238 break;
1239 case ACT_RING:
1240 /* get fresh AT state structure for new CID */
1241 at_state2 = get_free_channel(cs, ev->parameter);
1242 if (!at_state2) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001243 dev_warn(cs->dev,
1244 "RING ignored: could not allocate channel structure\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001245 break;
1246 }
1247
1248 /* initialize AT state structure
1249 * note that bcs may be NULL if no B channel is free
1250 */
1251 at_state2->ConState = 700;
Tilman Schmidt3a0a3a62010-03-14 12:58:05 +00001252 for (i = 0; i < STR_NUM; ++i) {
1253 kfree(at_state2->str_var[i]);
1254 at_state2->str_var[i] = NULL;
1255 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001256 at_state2->int_var[VAR_ZCTP] = -1;
1257
1258 spin_lock_irqsave(&cs->lock, flags);
1259 at_state2->timer_expires = RING_TIMEOUT;
1260 at_state2->timer_active = 1;
1261 spin_unlock_irqrestore(&cs->lock, flags);
1262 break;
1263 case ACT_ICALL:
1264 handle_icall(cs, bcs, p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001265 break;
1266 case ACT_FAILSDOWN:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001267 dev_warn(cs->dev, "Could not shut down the device.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001268 /* fall through */
1269 case ACT_FAKESDOWN:
1270 case ACT_SDOWN:
1271 cs->cur_at_seq = SEQ_NONE;
1272 finish_shutdown(cs);
1273 break;
1274 case ACT_CONNECT:
1275 if (cs->onechannel) {
1276 at_state->pending_commands |= PC_DLE1;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001277 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001278 break;
1279 }
1280 bcs->chstate |= CHS_D_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001281 gigaset_isdn_connD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001282 cs->ops->init_bchannel(bcs);
1283 break;
1284 case ACT_DLE1:
1285 cs->cur_at_seq = SEQ_NONE;
1286 bcs = cs->bcs + cs->curchannel;
1287
1288 bcs->chstate |= CHS_D_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001289 gigaset_isdn_connD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001290 cs->ops->init_bchannel(bcs);
1291 break;
1292 case ACT_FAKEHUP:
1293 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1294 /* fall through */
1295 case ACT_DISCONNECT:
1296 cs->cur_at_seq = SEQ_NONE;
1297 at_state->cid = -1;
1298 if (bcs && cs->onechannel && cs->dle) {
1299 /* Check for other open channels not needed:
1300 * DLE only used for M10x with one B channel.
1301 */
1302 at_state->pending_commands |= PC_DLE0;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001303 cs->commands_pending = 1;
Tilman Schmidt698e3ed2006-06-26 00:25:34 -07001304 } else
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001305 disconnect(p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001306 break;
1307 case ACT_FAKEDLE0:
1308 at_state->int_var[VAR_ZDLE] = 0;
1309 cs->dle = 0;
1310 /* fall through */
1311 case ACT_DLE0:
1312 cs->cur_at_seq = SEQ_NONE;
1313 at_state2 = &cs->bcs[cs->curchannel].at_state;
1314 disconnect(&at_state2);
1315 break;
1316 case ACT_ABORTHUP:
1317 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001318 dev_warn(cs->dev, "Could not hang up.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001319 at_state->cid = -1;
1320 if (bcs && cs->onechannel)
1321 at_state->pending_commands |= PC_DLE0;
Tilman Schmidt698e3ed2006-06-26 00:25:34 -07001322 else
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001323 disconnect(p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001324 schedule_init(cs, MS_RECOVER);
1325 break;
1326 case ACT_FAILDLE0:
1327 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001328 dev_warn(cs->dev, "Could not leave DLE mode.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001329 at_state2 = &cs->bcs[cs->curchannel].at_state;
1330 disconnect(&at_state2);
1331 schedule_init(cs, MS_RECOVER);
1332 break;
1333 case ACT_FAILDLE1:
1334 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001335 dev_warn(cs->dev,
1336 "Could not enter DLE mode. Trying to hang up.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001337 channel = cs->curchannel;
1338 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001339 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001340 break;
1341
1342 case ACT_CID: /* got cid; start dialing */
1343 cs->cur_at_seq = SEQ_NONE;
1344 channel = cs->curchannel;
1345 if (ev->parameter > 0 && ev->parameter <= 65535) {
1346 cs->bcs[channel].at_state.cid = ev->parameter;
1347 cs->bcs[channel].at_state.pending_commands |=
1348 PC_DIAL;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001349 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001350 break;
1351 }
1352 /* fall through */
1353 case ACT_FAILCID:
1354 cs->cur_at_seq = SEQ_NONE;
1355 channel = cs->curchannel;
1356 if (!reinit_and_retry(cs, channel)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001357 dev_warn(cs->dev,
1358 "Could not get a call ID. Cannot dial.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001359 at_state2 = &cs->bcs[channel].at_state;
1360 disconnect(&at_state2);
1361 }
1362 break;
1363 case ACT_ABORTCID:
1364 cs->cur_at_seq = SEQ_NONE;
1365 at_state2 = &cs->bcs[cs->curchannel].at_state;
1366 disconnect(&at_state2);
1367 break;
1368
1369 case ACT_DIALING:
1370 case ACT_ACCEPTED:
1371 cs->cur_at_seq = SEQ_NONE;
1372 break;
1373
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001374 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL procssng */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001375 disconnect(p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001376 break;
1377
1378 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1379 cs->cur_at_seq = SEQ_NONE;
1380 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001381 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001382 break;
1383
1384 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1385 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1386 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1387 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001388 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001389 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001390 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
Tilman Schmidt784d5852006-04-10 22:55:04 -07001391 are not handled properly anymore */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001392 at_state->getstring = 1;
1393 break;
1394 case ACT_SETVER:
1395 if (!ev->ptr) {
1396 *p_genresp = 1;
1397 *p_resp_code = RSP_ERROR;
1398 break;
1399 }
1400 s = ev->ptr;
1401
1402 if (!strcmp(s, "OK")) {
1403 *p_genresp = 1;
1404 *p_resp_code = RSP_ERROR;
1405 break;
1406 }
1407
1408 for (i = 0; i < 4; ++i) {
1409 val = simple_strtoul(s, (char **) &e, 10);
1410 if (val > INT_MAX || e == s)
1411 break;
1412 if (i == 3) {
1413 if (*e)
1414 break;
1415 } else if (*e != '.')
1416 break;
1417 else
1418 s = e + 1;
1419 cs->fwver[i] = val;
1420 }
1421 if (i != 4) {
1422 *p_genresp = 1;
1423 *p_resp_code = RSP_ERROR;
1424 break;
1425 }
1426 /*at_state->getstring = 1;*/
1427 cs->gotfwver = 0;
1428 break;
1429 case ACT_GOTVER:
1430 if (cs->gotfwver == 0) {
1431 cs->gotfwver = 1;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001432 gig_dbg(DEBUG_EVENT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001433 "firmware version %02d.%03d.%02d.%02d",
1434 cs->fwver[0], cs->fwver[1],
1435 cs->fwver[2], cs->fwver[3]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001436 break;
1437 }
1438 /* fall through */
1439 case ACT_FAILVER:
1440 cs->gotfwver = -1;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001441 dev_err(cs->dev, "could not read firmware version.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001442 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001443 case ACT_ERROR:
Tilman Schmidt05eae94f2009-10-06 12:18:46 +00001444 gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d",
1445 __func__, at_state->ConState);
1446 cs->cur_at_seq = SEQ_NONE;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001447 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001448 case ACT_DEBUG:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001449 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001450 __func__, ev->type, at_state->ConState);
1451 break;
1452 case ACT_WARN:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001453 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1454 __func__, ev->type, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001455 break;
1456 case ACT_ZCAU:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001457 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1458 ev->parameter, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001459 break;
1460
1461 /* events from the LL */
1462 case ACT_DIAL:
1463 start_dial(at_state, ev->ptr, ev->parameter);
1464 break;
1465 case ACT_ACCEPT:
1466 start_accept(at_state);
1467 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001468 case ACT_HUP:
1469 at_state->pending_commands |= PC_HUP;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001470 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001471 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001472 break;
1473
1474 /* hotplug events */
1475 case ACT_STOP:
1476 do_stop(cs);
1477 break;
1478 case ACT_START:
1479 do_start(cs);
1480 break;
1481
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001482 /* events from the interface */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001483 case ACT_IF_LOCK:
1484 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1485 cs->waiting = 0;
1486 wake_up(&cs->waitqueue);
1487 break;
1488 case ACT_IF_VER:
1489 if (ev->parameter != 0)
1490 cs->cmd_result = -EINVAL;
1491 else if (cs->gotfwver != 1) {
1492 cs->cmd_result = -ENOENT;
1493 } else {
1494 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1495 cs->cmd_result = 0;
1496 }
1497 cs->waiting = 0;
1498 wake_up(&cs->waitqueue);
1499 break;
1500
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001501 /* events from the proc file system */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001502 case ACT_PROC_CIDMODE:
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001503 spin_lock_irqsave(&cs->lock, flags);
1504 if (ev->parameter != cs->cidmode) {
1505 cs->cidmode = ev->parameter;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001506 if (ev->parameter) {
1507 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001508 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001509 } else {
1510 cs->at_state.pending_commands |= PC_UMMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001511 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001512 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001513 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001514 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001515 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001516 cs->waiting = 0;
1517 wake_up(&cs->waitqueue);
1518 break;
1519
1520 /* events from the hardware drivers */
1521 case ACT_NOTIFY_BC_DOWN:
1522 bchannel_down(bcs);
1523 break;
1524 case ACT_NOTIFY_BC_UP:
1525 bchannel_up(bcs);
1526 break;
1527 case ACT_SHUTDOWN:
1528 do_shutdown(cs);
1529 break;
1530
1531
1532 default:
1533 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1534 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1535 if (!*pp_command) {
1536 *p_genresp = 1;
1537 *p_resp_code = RSP_NULL;
1538 }
1539 } else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001540 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001541 }
1542}
1543
1544/* State machine to do the calling and hangup procedure */
1545static void process_event(struct cardstate *cs, struct event_t *ev)
1546{
1547 struct bc_state *bcs;
1548 char *p_command = NULL;
1549 struct reply_t *rep;
1550 int rcode;
1551 int genresp = 0;
1552 int resp_code = RSP_ERROR;
1553 int sendcid;
1554 struct at_state_t *at_state;
1555 int index;
1556 int curact;
1557 unsigned long flags;
1558
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001559 if (ev->cid >= 0) {
1560 at_state = at_state_from_cid(cs, ev->cid);
1561 if (!at_state) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001562 gig_dbg(DEBUG_EVENT, "event %d for invalid cid %d",
1563 ev->type, ev->cid);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001564 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001565 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001566 return;
1567 }
1568 } else {
1569 at_state = ev->at_state;
1570 if (at_state_invalid(cs, at_state)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001571 gig_dbg(DEBUG_EVENT, "event for invalid at_state %p",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001572 at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001573 return;
1574 }
1575 }
1576
Tilman Schmidt1528b182010-02-22 13:09:52 +00001577 gig_dbg(DEBUG_EVENT, "connection state %d, event %d",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001578 at_state->ConState, ev->type);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001579
1580 bcs = at_state->bcs;
1581 sendcid = at_state->cid;
1582
1583 /* Setting the pointer to the dial array */
1584 rep = at_state->replystruct;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001585
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001586 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001587 if (ev->type == EV_TIMEOUT) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001588 if (ev->parameter != at_state->timer_index
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001589 || !at_state->timer_active) {
1590 ev->type = RSP_NONE; /* old timeout */
Tilman Schmidt1528b182010-02-22 13:09:52 +00001591 gig_dbg(DEBUG_EVENT, "old timeout");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001592 } else if (!at_state->waiting)
Tilman Schmidt1528b182010-02-22 13:09:52 +00001593 gig_dbg(DEBUG_EVENT, "timeout occurred");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001594 else
Tilman Schmidt1528b182010-02-22 13:09:52 +00001595 gig_dbg(DEBUG_EVENT, "stopped waiting");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001596 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001597 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001598
Tilman Schmidt917f5082006-04-10 22:55:00 -07001599 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1600 or at_state->str_var[STR_XXXX], set it */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001601 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1602 index = ev->type - RSP_VAR;
1603 at_state->int_var[index] = ev->parameter;
1604 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1605 index = ev->type - RSP_STR;
1606 kfree(at_state->str_var[index]);
1607 at_state->str_var[index] = ev->ptr;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001608 ev->ptr = NULL; /* prevent process_events() from
1609 deallocating ptr */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001610 }
1611
1612 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1613 at_state->getstring = 0;
1614
Tilman Schmidt917f5082006-04-10 22:55:00 -07001615 /* Search row in dial array which matches modem response and current
1616 constate */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001617 for (;; rep++) {
1618 rcode = rep->resp_code;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001619 if (rcode == RSP_LAST) {
1620 /* found nothing...*/
Tilman Schmidt784d5852006-04-10 22:55:04 -07001621 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1622 "resp_code %d in ConState %d!\n",
1623 __func__, ev->type, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001624 return;
1625 }
1626 if ((rcode == RSP_ANY || rcode == ev->type)
1627 && ((int) at_state->ConState >= rep->min_ConState)
1628 && (rep->max_ConState < 0
1629 || (int) at_state->ConState <= rep->max_ConState)
1630 && (rep->parameter < 0 || rep->parameter == ev->parameter))
1631 break;
1632 }
1633
1634 p_command = rep->command;
1635
1636 at_state->waiting = 0;
1637 for (curact = 0; curact < MAXACT; ++curact) {
1638 /* The row tells us what we should do ..
1639 */
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001640 do_action(rep->action[curact], cs, bcs, &at_state, &p_command,
1641 &genresp, &resp_code, ev);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001642 if (!at_state)
1643 break; /* may be freed after disconnect */
1644 }
1645
1646 if (at_state) {
1647 /* Jump to the next con-state regarding the array */
1648 if (rep->new_ConState >= 0)
1649 at_state->ConState = rep->new_ConState;
1650
1651 if (genresp) {
1652 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001653 at_state->timer_expires = 0;
1654 at_state->timer_active = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001655 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001656 gigaset_add_event(cs, at_state, resp_code,
1657 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001658 } else {
1659 /* Send command to modem if not NULL... */
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001660 if (p_command) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001661 if (cs->connected)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001662 send_command(cs, p_command,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001663 sendcid, cs->dle,
1664 GFP_ATOMIC);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001665 else
1666 gigaset_add_event(cs, at_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001667 RSP_NODEV,
1668 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001669 }
1670
1671 spin_lock_irqsave(&cs->lock, flags);
1672 if (!rep->timeout) {
1673 at_state->timer_expires = 0;
1674 at_state->timer_active = 0;
1675 } else if (rep->timeout > 0) { /* new timeout */
1676 at_state->timer_expires = rep->timeout * 10;
1677 at_state->timer_active = 1;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001678 ++at_state->timer_index;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001679 }
1680 spin_unlock_irqrestore(&cs->lock, flags);
1681 }
1682 }
1683}
1684
1685static void schedule_sequence(struct cardstate *cs,
1686 struct at_state_t *at_state, int sequence)
1687{
1688 cs->cur_at_seq = sequence;
1689 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1690}
1691
1692static void process_command_flags(struct cardstate *cs)
1693{
1694 struct at_state_t *at_state = NULL;
1695 struct bc_state *bcs;
1696 int i;
1697 int sequence;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001698 unsigned long flags;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001699
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001700 cs->commands_pending = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001701
1702 if (cs->cur_at_seq) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001703 gig_dbg(DEBUG_EVENT, "not searching scheduled commands: busy");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001704 return;
1705 }
1706
Tilman Schmidt1528b182010-02-22 13:09:52 +00001707 gig_dbg(DEBUG_EVENT, "searching scheduled commands");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001708
1709 sequence = SEQ_NONE;
1710
1711 /* clear pending_commands and hangup channels on shutdown */
1712 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1713 cs->at_state.pending_commands &= ~PC_CIDMODE;
1714 for (i = 0; i < cs->channels; ++i) {
1715 bcs = cs->bcs + i;
1716 at_state = &bcs->at_state;
1717 at_state->pending_commands &=
1718 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1719 if (at_state->cid > 0)
1720 at_state->pending_commands |= PC_HUP;
1721 if (at_state->pending_commands & PC_CID) {
1722 at_state->pending_commands |= PC_NOCID;
1723 at_state->pending_commands &= ~PC_CID;
1724 }
1725 }
1726 }
1727
1728 /* clear pending_commands and hangup channels on reset */
1729 if (cs->at_state.pending_commands & PC_INIT) {
1730 cs->at_state.pending_commands &= ~PC_CIDMODE;
1731 for (i = 0; i < cs->channels; ++i) {
1732 bcs = cs->bcs + i;
1733 at_state = &bcs->at_state;
1734 at_state->pending_commands &=
1735 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1736 if (at_state->cid > 0)
1737 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001738 if (cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001739 if (at_state->pending_commands & PC_CID) {
1740 at_state->pending_commands |= PC_NOCID;
1741 at_state->pending_commands &= ~PC_CID;
1742 }
1743 }
1744 }
1745 }
1746
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001747 /* only switch back to unimodem mode if no commands are pending and
1748 * no channels are up */
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001749 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001750 if (cs->at_state.pending_commands == PC_UMMODE
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001751 && !cs->cidmode
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001752 && list_empty(&cs->temp_at_states)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001753 && cs->mode == M_CID) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001754 sequence = SEQ_UMMODE;
1755 at_state = &cs->at_state;
1756 for (i = 0; i < cs->channels; ++i) {
1757 bcs = cs->bcs + i;
1758 if (bcs->at_state.pending_commands ||
1759 bcs->at_state.cid > 0) {
1760 sequence = SEQ_NONE;
1761 break;
1762 }
1763 }
1764 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001765 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001766 cs->at_state.pending_commands &= ~PC_UMMODE;
1767 if (sequence != SEQ_NONE) {
1768 schedule_sequence(cs, at_state, sequence);
1769 return;
1770 }
1771
1772 for (i = 0; i < cs->channels; ++i) {
1773 bcs = cs->bcs + i;
1774 if (bcs->at_state.pending_commands & PC_HUP) {
1775 bcs->at_state.pending_commands &= ~PC_HUP;
1776 if (bcs->at_state.pending_commands & PC_CID) {
1777 /* not yet dialing: PC_NOCID is sufficient */
1778 bcs->at_state.pending_commands |= PC_NOCID;
1779 bcs->at_state.pending_commands &= ~PC_CID;
1780 } else {
1781 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1782 return;
1783 }
1784 }
1785 if (bcs->at_state.pending_commands & PC_NOCID) {
1786 bcs->at_state.pending_commands &= ~PC_NOCID;
1787 cs->curchannel = bcs->channel;
1788 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1789 return;
1790 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1791 bcs->at_state.pending_commands &= ~PC_DLE0;
1792 cs->curchannel = bcs->channel;
1793 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1794 return;
1795 }
1796 }
1797
1798 list_for_each_entry(at_state, &cs->temp_at_states, list)
1799 if (at_state->pending_commands & PC_HUP) {
1800 at_state->pending_commands &= ~PC_HUP;
1801 schedule_sequence(cs, at_state, SEQ_HUP);
1802 return;
1803 }
1804
1805 if (cs->at_state.pending_commands & PC_INIT) {
1806 cs->at_state.pending_commands &= ~PC_INIT;
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001807 cs->dle = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001808 cs->inbuf->inputstate = INS_command;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001809 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1810 return;
1811 }
1812 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1813 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1814 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1815 return;
1816 }
1817 if (cs->at_state.pending_commands & PC_CIDMODE) {
1818 cs->at_state.pending_commands &= ~PC_CIDMODE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001819 if (cs->mode == M_UNIMODEM) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001820 cs->retry_count = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001821 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1822 return;
1823 }
1824 }
1825
1826 for (i = 0; i < cs->channels; ++i) {
1827 bcs = cs->bcs + i;
1828 if (bcs->at_state.pending_commands & PC_DLE1) {
1829 bcs->at_state.pending_commands &= ~PC_DLE1;
1830 cs->curchannel = bcs->channel;
1831 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1832 return;
1833 }
1834 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1835 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1836 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1837 return;
1838 }
1839 if (bcs->at_state.pending_commands & PC_DIAL) {
1840 bcs->at_state.pending_commands &= ~PC_DIAL;
1841 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1842 return;
1843 }
1844 if (bcs->at_state.pending_commands & PC_CID) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001845 switch (cs->mode) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001846 case M_UNIMODEM:
1847 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001848 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001849 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001850 return;
1851#ifdef GIG_MAYINITONDIAL
1852 case M_UNKNOWN:
1853 schedule_init(cs, MS_INIT);
1854 return;
1855#endif
1856 }
1857 bcs->at_state.pending_commands &= ~PC_CID;
1858 cs->curchannel = bcs->channel;
1859#ifdef GIG_RETRYCID
1860 cs->retry_count = 2;
1861#else
1862 cs->retry_count = 1;
1863#endif
1864 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1865 return;
1866 }
1867 }
1868}
1869
1870static void process_events(struct cardstate *cs)
1871{
1872 struct event_t *ev;
1873 unsigned head, tail;
1874 int i;
1875 int check_flags = 0;
1876 int was_busy;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001877 unsigned long flags;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001878
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001879 spin_lock_irqsave(&cs->ev_lock, flags);
1880 head = cs->ev_head;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001881
1882 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001883 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001884 if (tail == head) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001885 if (!check_flags && !cs->commands_pending)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001886 break;
1887 check_flags = 0;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001888 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001889 process_command_flags(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001890 spin_lock_irqsave(&cs->ev_lock, flags);
1891 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001892 if (tail == head) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001893 if (!cs->commands_pending)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001894 break;
1895 continue;
1896 }
1897 }
1898
1899 ev = cs->events + head;
1900 was_busy = cs->cur_at_seq != SEQ_NONE;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001901 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001902 process_event(cs, ev);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001903 spin_lock_irqsave(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001904 kfree(ev->ptr);
1905 ev->ptr = NULL;
1906 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1907 check_flags = 1;
1908
1909 head = (head + 1) % MAX_EVENTS;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001910 cs->ev_head = head;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001911 }
1912
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001913 spin_unlock_irqrestore(&cs->ev_lock, flags);
1914
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001915 if (i == 2 * MAX_EVENTS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001916 dev_err(cs->dev,
1917 "infinite loop in process_events; aborting.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001918 }
1919}
1920
1921/* tasklet scheduled on any event received from the Gigaset device
1922 * parameter:
1923 * data ISDN controller state structure
1924 */
1925void gigaset_handle_event(unsigned long data)
1926{
1927 struct cardstate *cs = (struct cardstate *) data;
1928
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001929 /* handle incoming data on control/common channel */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001930 if (cs->inbuf->head != cs->inbuf->tail) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001931 gig_dbg(DEBUG_INTR, "processing new data");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001932 cs->ops->handle_input(cs->inbuf);
1933 }
1934
1935 process_events(cs);
1936}