blob: a14187605f5e477549b11143d5da129c55aec9b0 [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
Tilman Schmidt917f5082006-04-10 22:55:00 -070038#define RT_ZCAU 6
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080039
40/* Possible ASCII responses */
Tilman Schmidt917f5082006-04-10 22:55:00 -070041#define RSP_OK 0
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000042#define RSP_ERROR 1
Tilman Schmidt917f5082006-04-10 22:55:00 -070043#define RSP_ZGCI 3
44#define RSP_RING 4
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000045#define RSP_ZVLS 5
46#define RSP_ZCAU 6
47
48/* responses with values to store in at_state */
49/* - numeric */
Tilman Schmidt917f5082006-04-10 22:55:00 -070050#define RSP_VAR 100
51#define RSP_ZSAU (RSP_VAR + VAR_ZSAU)
52#define RSP_ZDLE (RSP_VAR + VAR_ZDLE)
Tilman Schmidt917f5082006-04-10 22:55:00 -070053#define RSP_ZCTP (RSP_VAR + VAR_ZCTP)
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000054/* - string */
Tilman Schmidt917f5082006-04-10 22:55:00 -070055#define RSP_STR (RSP_VAR + VAR_NUM)
56#define RSP_NMBR (RSP_STR + STR_NMBR)
57#define RSP_ZCPN (RSP_STR + STR_ZCPN)
58#define RSP_ZCON (RSP_STR + STR_ZCON)
59#define RSP_ZBC (RSP_STR + STR_ZBC)
60#define RSP_ZHLC (RSP_STR + STR_ZHLC)
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000061
Tilman Schmidt917f5082006-04-10 22:55:00 -070062#define RSP_WRONG_CID -2 /* unknown cid in cmd */
Tilman Schmidt917f5082006-04-10 22:55:00 -070063#define RSP_INVAL -6 /* invalid response */
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000064#define RSP_NODEV -9 /* device not connected */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080065
Tilman Schmidt917f5082006-04-10 22:55:00 -070066#define RSP_NONE -19
67#define RSP_STRING -20
68#define RSP_NULL -21
Tilman Schmidt917f5082006-04-10 22:55:00 -070069#define RSP_INIT -27
70#define RSP_ANY -26
71#define RSP_LAST -28
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080072
73/* actions for process_response */
74#define ACT_NOTHING 0
75#define ACT_SETDLE1 1
76#define ACT_SETDLE0 2
77#define ACT_FAILINIT 3
78#define ACT_HUPMODEM 4
79#define ACT_CONFIGMODE 5
80#define ACT_INIT 6
81#define ACT_DLE0 7
82#define ACT_DLE1 8
83#define ACT_FAILDLE0 9
84#define ACT_FAILDLE1 10
85#define ACT_RING 11
86#define ACT_CID 12
87#define ACT_FAILCID 13
88#define ACT_SDOWN 14
89#define ACT_FAILSDOWN 15
90#define ACT_DEBUG 16
91#define ACT_WARN 17
92#define ACT_DIALING 18
93#define ACT_ABORTDIAL 19
94#define ACT_DISCONNECT 20
95#define ACT_CONNECT 21
96#define ACT_REMOTEREJECT 22
Tilman Schmidt917f5082006-04-10 22:55:00 -070097#define ACT_CONNTIMEOUT 23
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080098#define ACT_REMOTEHUP 24
99#define ACT_ABORTHUP 25
100#define ACT_ICALL 26
101#define ACT_ACCEPTED 27
102#define ACT_ABORTACCEPT 28
103#define ACT_TIMEOUT 29
104#define ACT_GETSTRING 30
105#define ACT_SETVER 31
106#define ACT_FAILVER 32
107#define ACT_GOTVER 33
108#define ACT_TEST 34
109#define ACT_ERROR 35
110#define ACT_ABORTCID 36
111#define ACT_ZCAU 37
Tilman Schmidt917f5082006-04-10 22:55:00 -0700112#define ACT_NOTIFY_BC_DOWN 38
113#define ACT_NOTIFY_BC_UP 39
114#define ACT_DIAL 40
115#define ACT_ACCEPT 41
Tilman Schmidt917f5082006-04-10 22:55:00 -0700116#define ACT_HUP 43
117#define ACT_IF_LOCK 44
118#define ACT_START 45
119#define ACT_STOP 46
120#define ACT_FAKEDLE0 47
121#define ACT_FAKEHUP 48
122#define ACT_FAKESDOWN 49
123#define ACT_SHUTDOWN 50
124#define ACT_PROC_CIDMODE 51
125#define ACT_UMODESET 52
126#define ACT_FAILUMODE 53
127#define ACT_CMODESET 54
128#define ACT_FAILCMODE 55
129#define ACT_IF_VER 56
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800130#define ACT_CMD 100
131
132/* at command sequences */
Tilman Schmidt917f5082006-04-10 22:55:00 -0700133#define SEQ_NONE 0
134#define SEQ_INIT 100
135#define SEQ_DLE0 200
136#define SEQ_DLE1 250
137#define SEQ_CID 300
138#define SEQ_NOCID 350
139#define SEQ_HUP 400
140#define SEQ_DIAL 600
141#define SEQ_ACCEPT 720
142#define SEQ_SHUTDOWN 500
143#define SEQ_CIDMODE 10
144#define SEQ_UMMODE 11
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800145
146
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000147/* 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid),
148 * 400: hup, 500: reset, 600: dial, 700: ring */
Tilman Schmidt528efc62009-05-13 12:44:17 +0000149struct reply_t gigaset_tab_nocid[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800150{
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000151/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
152 * action, command */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800153
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000154/* initialize device, set cid mode if possible */
155{RSP_INIT, -1, -1, SEQ_INIT, 100, 1, {ACT_TIMEOUT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800156
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000157{EV_TIMEOUT, 100, 100, -1, 101, 3, {0}, "Z\r"},
158{RSP_OK, 101, 103, -1, 120, 5, {ACT_GETSTRING},
159 "+GMR\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800160
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000161{EV_TIMEOUT, 101, 101, -1, 102, 5, {0}, "Z\r"},
162{RSP_ERROR, 101, 101, -1, 102, 5, {0}, "Z\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800163
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000164{EV_TIMEOUT, 102, 102, -1, 108, 5, {ACT_SETDLE1},
165 "^SDLE=0\r"},
166{RSP_OK, 108, 108, -1, 104, -1},
167{RSP_ZDLE, 104, 104, 0, 103, 5, {0}, "Z\r"},
168{EV_TIMEOUT, 104, 104, -1, 0, 0, {ACT_FAILINIT} },
169{RSP_ERROR, 108, 108, -1, 0, 0, {ACT_FAILINIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800170
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000171{EV_TIMEOUT, 108, 108, -1, 105, 2, {ACT_SETDLE0,
172 ACT_HUPMODEM,
173 ACT_TIMEOUT} },
174{EV_TIMEOUT, 105, 105, -1, 103, 5, {0}, "Z\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800175
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000176{RSP_ERROR, 102, 102, -1, 107, 5, {0}, "^GETPRE\r"},
177{RSP_OK, 107, 107, -1, 0, 0, {ACT_CONFIGMODE} },
178{RSP_ERROR, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
179{EV_TIMEOUT, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800180
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000181{RSP_ERROR, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
182{EV_TIMEOUT, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800183
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000184{RSP_STRING, 120, 120, -1, 121, -1, {ACT_SETVER} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800185
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000186{EV_TIMEOUT, 120, 121, -1, 0, 0, {ACT_FAILVER,
187 ACT_INIT} },
188{RSP_ERROR, 120, 121, -1, 0, 0, {ACT_FAILVER,
189 ACT_INIT} },
190{RSP_OK, 121, 121, -1, 0, 0, {ACT_GOTVER,
191 ACT_INIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800192
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000193/* leave dle mode */
194{RSP_INIT, 0, 0, SEQ_DLE0, 201, 5, {0}, "^SDLE=0\r"},
195{RSP_OK, 201, 201, -1, 202, -1},
196{RSP_ZDLE, 202, 202, 0, 0, 0, {ACT_DLE0} },
197{RSP_NODEV, 200, 249, -1, 0, 0, {ACT_FAKEDLE0} },
198{RSP_ERROR, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
199{EV_TIMEOUT, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800200
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000201/* enter dle mode */
202{RSP_INIT, 0, 0, SEQ_DLE1, 251, 5, {0}, "^SDLE=1\r"},
203{RSP_OK, 251, 251, -1, 252, -1},
204{RSP_ZDLE, 252, 252, 1, 0, 0, {ACT_DLE1} },
205{RSP_ERROR, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
206{EV_TIMEOUT, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800207
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000208/* incoming call */
209{RSP_RING, -1, -1, -1, -1, -1, {ACT_RING} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800210
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000211/* get cid */
212{RSP_INIT, 0, 0, SEQ_CID, 301, 5, {0}, "^SGCI?\r"},
213{RSP_OK, 301, 301, -1, 302, -1},
214{RSP_ZGCI, 302, 302, -1, 0, 0, {ACT_CID} },
215{RSP_ERROR, 301, 349, -1, 0, 0, {ACT_FAILCID} },
216{EV_TIMEOUT, 301, 349, -1, 0, 0, {ACT_FAILCID} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800217
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000218/* enter cid mode */
219{RSP_INIT, 0, 0, SEQ_CIDMODE, 150, 5, {0}, "^SGCI=1\r"},
220{RSP_OK, 150, 150, -1, 0, 0, {ACT_CMODESET} },
221{RSP_ERROR, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
222{EV_TIMEOUT, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800223
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000224/* leave cid mode */
225{RSP_INIT, 0, 0, SEQ_UMMODE, 160, 5, {0}, "Z\r"},
226{RSP_OK, 160, 160, -1, 0, 0, {ACT_UMODESET} },
227{RSP_ERROR, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
228{EV_TIMEOUT, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800229
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000230/* abort getting cid */
231{RSP_INIT, 0, 0, SEQ_NOCID, 0, 0, {ACT_ABORTCID} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800232
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000233/* reset */
234{RSP_INIT, 0, 0, SEQ_SHUTDOWN, 504, 5, {0}, "Z\r"},
235{RSP_OK, 504, 504, -1, 0, 0, {ACT_SDOWN} },
236{RSP_ERROR, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
237{EV_TIMEOUT, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
238{RSP_NODEV, 501, 599, -1, 0, 0, {ACT_FAKESDOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800239
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000240{EV_PROC_CIDMODE, -1, -1, -1, -1, -1, {ACT_PROC_CIDMODE} },
241{EV_IF_LOCK, -1, -1, -1, -1, -1, {ACT_IF_LOCK} },
242{EV_IF_VER, -1, -1, -1, -1, -1, {ACT_IF_VER} },
243{EV_START, -1, -1, -1, -1, -1, {ACT_START} },
244{EV_STOP, -1, -1, -1, -1, -1, {ACT_STOP} },
245{EV_SHUTDOWN, -1, -1, -1, -1, -1, {ACT_SHUTDOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800246
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000247/* misc. */
248{RSP_ERROR, -1, -1, -1, -1, -1, {ACT_ERROR} },
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000249{RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
250{RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
251{RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
252{RSP_LAST}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800253};
254
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000255/* 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring,
256 * 400: hup, 750: accepted icall */
Tilman Schmidt528efc62009-05-13 12:44:17 +0000257struct reply_t gigaset_tab_cid[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800258{
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000259/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
260 * action, command */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800261
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000262/* dial */
263{EV_DIAL, -1, -1, -1, -1, -1, {ACT_DIAL} },
264{RSP_INIT, 0, 0, SEQ_DIAL, 601, 5, {ACT_CMD+AT_BC} },
Tilman Schmidt1ce368f2010-06-21 13:55:05 +0000265{RSP_OK, 601, 601, -1, 603, 5, {ACT_CMD+AT_PROTO} },
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000266{RSP_OK, 603, 603, -1, 604, 5, {ACT_CMD+AT_TYPE} },
267{RSP_OK, 604, 604, -1, 605, 5, {ACT_CMD+AT_MSN} },
268{RSP_NULL, 605, 605, -1, 606, 5, {ACT_CMD+AT_CLIP} },
269{RSP_OK, 605, 605, -1, 606, 5, {ACT_CMD+AT_CLIP} },
270{RSP_NULL, 606, 606, -1, 607, 5, {ACT_CMD+AT_ISO} },
271{RSP_OK, 606, 606, -1, 607, 5, {ACT_CMD+AT_ISO} },
272{RSP_OK, 607, 607, -1, 608, 5, {0}, "+VLS=17\r"},
273{RSP_OK, 608, 608, -1, 609, -1},
274{RSP_ZSAU, 609, 609, ZSAU_PROCEEDING, 610, 5, {ACT_CMD+AT_DIAL} },
275{RSP_OK, 610, 610, -1, 650, 0, {ACT_DIALING} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800276
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000277{RSP_ERROR, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
278{EV_TIMEOUT, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800279
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000280/* optional dialing responses */
281{EV_BC_OPEN, 650, 650, -1, 651, -1},
282{RSP_ZVLS, 609, 651, 17, -1, -1, {ACT_DEBUG} },
283{RSP_ZCTP, 610, 651, -1, -1, -1, {ACT_DEBUG} },
284{RSP_ZCPN, 610, 651, -1, -1, -1, {ACT_DEBUG} },
285{RSP_ZSAU, 650, 651, ZSAU_CALL_DELIVERED, -1, -1, {ACT_DEBUG} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800286
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000287/* connect */
288{RSP_ZSAU, 650, 650, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
289{RSP_ZSAU, 651, 651, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
290 ACT_NOTIFY_BC_UP} },
291{RSP_ZSAU, 750, 750, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
292{RSP_ZSAU, 751, 751, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
293 ACT_NOTIFY_BC_UP} },
294{EV_BC_OPEN, 800, 800, -1, 800, -1, {ACT_NOTIFY_BC_UP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800295
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000296/* remote hangup */
297{RSP_ZSAU, 650, 651, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT} },
298{RSP_ZSAU, 750, 751, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
299{RSP_ZSAU, 800, 800, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800300
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000301/* hangup */
302{EV_HUP, -1, -1, -1, -1, -1, {ACT_HUP} },
303{RSP_INIT, -1, -1, SEQ_HUP, 401, 5, {0}, "+VLS=0\r"},
304{RSP_OK, 401, 401, -1, 402, 5},
305{RSP_ZVLS, 402, 402, 0, 403, 5},
306{RSP_ZSAU, 403, 403, ZSAU_DISCONNECT_REQ, -1, -1, {ACT_DEBUG} },
307{RSP_ZSAU, 403, 403, ZSAU_NULL, 0, 0, {ACT_DISCONNECT} },
308{RSP_NODEV, 401, 403, -1, 0, 0, {ACT_FAKEHUP} },
309{RSP_ERROR, 401, 401, -1, 0, 0, {ACT_ABORTHUP} },
310{EV_TIMEOUT, 401, 403, -1, 0, 0, {ACT_ABORTHUP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800311
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000312{EV_BC_CLOSED, 0, 0, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800313
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000314/* ring */
315{RSP_ZBC, 700, 700, -1, -1, -1, {0} },
316{RSP_ZHLC, 700, 700, -1, -1, -1, {0} },
317{RSP_NMBR, 700, 700, -1, -1, -1, {0} },
318{RSP_ZCPN, 700, 700, -1, -1, -1, {0} },
319{RSP_ZCTP, 700, 700, -1, -1, -1, {0} },
320{EV_TIMEOUT, 700, 700, -1, 720, 720, {ACT_ICALL} },
321{EV_BC_CLOSED, 720, 720, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800322
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000323/*accept icall*/
324{EV_ACCEPT, -1, -1, -1, -1, -1, {ACT_ACCEPT} },
325{RSP_INIT, 720, 720, SEQ_ACCEPT, 721, 5, {ACT_CMD+AT_PROTO} },
326{RSP_OK, 721, 721, -1, 722, 5, {ACT_CMD+AT_ISO} },
327{RSP_OK, 722, 722, -1, 723, 5, {0}, "+VLS=17\r"},
328{RSP_OK, 723, 723, -1, 724, 5, {0} },
329{RSP_ZVLS, 724, 724, 17, 750, 50, {ACT_ACCEPTED} },
330{RSP_ERROR, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
331{EV_TIMEOUT, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
332{RSP_ZSAU, 700, 729, ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT} },
333{RSP_ZSAU, 700, 729, ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT} },
334{RSP_ZSAU, 700, 729, ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800335
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000336{EV_BC_OPEN, 750, 750, -1, 751, -1},
337{EV_TIMEOUT, 750, 751, -1, 0, 0, {ACT_CONNTIMEOUT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800338
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000339/* B channel closed (general case) */
340{EV_BC_CLOSED, -1, -1, -1, -1, -1, {ACT_NOTIFY_BC_DOWN} },
Tilman Schmidt73a88812006-04-22 02:35:30 -0700341
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000342/* misc. */
343{RSP_ZCON, -1, -1, -1, -1, -1, {ACT_DEBUG} },
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000344{RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
345{RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
346{RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
347{RSP_LAST}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800348};
349
350
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000351static const struct resp_type_t {
352 unsigned char *response;
353 int resp_code;
354 int type;
355} resp_type[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800356{
Tilman Schmidt917f5082006-04-10 22:55:00 -0700357 {"OK", RSP_OK, RT_NOTHING},
358 {"ERROR", RSP_ERROR, RT_NOTHING},
359 {"ZSAU", RSP_ZSAU, RT_ZSAU},
360 {"ZCAU", RSP_ZCAU, RT_ZCAU},
361 {"RING", RSP_RING, RT_RING},
362 {"ZGCI", RSP_ZGCI, RT_NUMBER},
363 {"ZVLS", RSP_ZVLS, RT_NUMBER},
364 {"ZCTP", RSP_ZCTP, RT_NUMBER},
365 {"ZDLE", RSP_ZDLE, RT_NUMBER},
Tilman Schmidt917f5082006-04-10 22:55:00 -0700366 {"ZHLC", RSP_ZHLC, RT_STRING},
367 {"ZBC", RSP_ZBC, RT_STRING},
368 {"NMBR", RSP_NMBR, RT_STRING},
369 {"ZCPN", RSP_ZCPN, RT_STRING},
370 {"ZCON", RSP_ZCON, RT_STRING},
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000371 {NULL, 0, 0}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800372};
373
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000374static const struct zsau_resp_t {
375 unsigned char *str;
376 int code;
377} zsau_resp[] =
378{
379 {"OUTGOING_CALL_PROCEEDING", ZSAU_OUTGOING_CALL_PROCEEDING},
380 {"CALL_DELIVERED", ZSAU_CALL_DELIVERED},
381 {"ACTIVE", ZSAU_ACTIVE},
382 {"DISCONNECT_IND", ZSAU_DISCONNECT_IND},
383 {"NULL", ZSAU_NULL},
384 {"DISCONNECT_REQ", ZSAU_DISCONNECT_REQ},
385 {NULL, ZSAU_UNKNOWN}
386};
387
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800388/* retrieve CID from parsed response
389 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
390 */
391static int cid_of_response(char *s)
392{
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000393 unsigned long cid;
394 int rc;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800395
396 if (s[-1] != ';')
397 return 0; /* no CID separator */
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000398 rc = strict_strtoul(s, 10, &cid);
399 if (rc)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800400 return 0; /* CID not numeric */
401 if (cid < 1 || cid > 65535)
402 return -1; /* CID out of range */
403 return cid;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800404}
405
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000406/**
407 * gigaset_handle_modem_response() - process received modem response
408 * @cs: device descriptor structure.
409 *
410 * Called by asyncdata/isocdata if a block of data received from the
411 * device must be processed as a modem command response. The data is
412 * already in the cs structure.
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800413 */
414void gigaset_handle_modem_response(struct cardstate *cs)
415{
416 unsigned char *argv[MAX_REC_PARAMS + 1];
417 int params;
418 int i, j;
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700419 const struct resp_type_t *rt;
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000420 const struct zsau_resp_t *zr;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800421 int curarg;
422 unsigned long flags;
423 unsigned next, tail, head;
424 struct event_t *event;
425 int resp_code;
426 int param_type;
427 int abort;
428 size_t len;
429 int cid;
430 int rawstring;
431
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800432 len = cs->cbytes;
433 if (!len) {
434 /* ignore additional LFs/CRs (M10x config mode or cx100) */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700435 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[len]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800436 return;
437 }
438 cs->respdata[len] = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800439 argv[0] = cs->respdata;
440 params = 1;
441 if (cs->at_state.getstring) {
442 /* getstring only allowed without cid at the moment */
443 cs->at_state.getstring = 0;
444 rawstring = 1;
445 cid = 0;
446 } else {
447 /* parse line */
448 for (i = 0; i < len; i++)
449 switch (cs->respdata[i]) {
450 case ';':
451 case ',':
452 case '=':
453 if (params > MAX_REC_PARAMS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700454 dev_warn(cs->dev,
455 "too many parameters in response\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800456 /* need last parameter (might be CID) */
457 params--;
458 }
459 argv[params++] = cs->respdata + i + 1;
460 }
461
462 rawstring = 0;
463 cid = params > 1 ? cid_of_response(argv[params-1]) : 0;
464 if (cid < 0) {
465 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700466 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800467 return;
468 }
469
470 for (j = 1; j < params; ++j)
471 argv[j][-1] = 0;
472
Tilman Schmidt1528b182010-02-22 13:09:52 +0000473 gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800474 if (cid) {
475 --params;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000476 gig_dbg(DEBUG_EVENT, "CID: %s", argv[params]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800477 }
Tilman Schmidt1528b182010-02-22 13:09:52 +0000478 gig_dbg(DEBUG_EVENT, "available params: %d", params - 1);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800479 for (j = 1; j < params; j++)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000480 gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800481 }
482
483 spin_lock_irqsave(&cs->ev_lock, flags);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700484 head = cs->ev_head;
485 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800486
487 abort = 1;
488 curarg = 0;
489 while (curarg < params) {
490 next = (tail + 1) % MAX_EVENTS;
491 if (unlikely(next == head)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700492 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800493 break;
494 }
495
496 event = cs->events + tail;
497 event->at_state = NULL;
498 event->cid = cid;
499 event->ptr = NULL;
500 event->arg = NULL;
501 tail = next;
502
503 if (rawstring) {
504 resp_code = RSP_STRING;
505 param_type = RT_STRING;
506 } else {
507 for (rt = resp_type; rt->response; ++rt)
508 if (!strcmp(argv[curarg], rt->response))
509 break;
510
511 if (!rt->response) {
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +0000512 event->type = RSP_NONE;
513 gig_dbg(DEBUG_EVENT,
514 "unknown modem response: '%s'\n",
515 argv[curarg]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800516 break;
517 }
518
519 resp_code = rt->resp_code;
520 param_type = rt->type;
521 ++curarg;
522 }
523
524 event->type = resp_code;
525
526 switch (param_type) {
527 case RT_NOTHING:
528 break;
529 case RT_RING:
530 if (!cid) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700531 dev_err(cs->dev,
532 "received RING without CID!\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800533 event->type = RSP_INVAL;
534 abort = 1;
535 } else {
536 event->cid = 0;
537 event->parameter = cid;
538 abort = 0;
539 }
540 break;
541 case RT_ZSAU:
542 if (curarg >= params) {
543 event->parameter = ZSAU_NONE;
544 break;
545 }
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000546 for (zr = zsau_resp; zr->str; ++zr)
547 if (!strcmp(argv[curarg], zr->str))
548 break;
549 event->parameter = zr->code;
550 if (!zr->str)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700551 dev_warn(cs->dev,
552 "%s: unknown parameter %s after ZSAU\n",
553 __func__, argv[curarg]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800554 ++curarg;
555 break;
556 case RT_STRING:
557 if (curarg < params) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700558 event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
559 if (!event->ptr)
560 dev_err(cs->dev, "out of memory\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800561 ++curarg;
562 }
Tilman Schmidt1528b182010-02-22 13:09:52 +0000563 gig_dbg(DEBUG_EVENT, "string==%s",
Tilman Schmidt236b87c2008-12-26 01:22:03 -0800564 event->ptr ? (char *) event->ptr : "NULL");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800565 break;
566 case RT_ZCAU:
567 event->parameter = -1;
568 if (curarg + 1 < params) {
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000569 unsigned long type, value;
570
571 i = strict_strtoul(argv[curarg++], 16, &type);
572 j = strict_strtoul(argv[curarg++], 16, &value);
573
574 if (i == 0 && type < 256 &&
575 j == 0 && value < 256)
576 event->parameter = (type << 8) | value;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800577 } else
578 curarg = params - 1;
579 break;
580 case RT_NUMBER:
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000581 event->parameter = -1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800582 if (curarg < params) {
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000583 unsigned long res;
584 int rc;
585
586 rc = strict_strtoul(argv[curarg++], 10, &res);
587 if (rc == 0)
588 event->parameter = res;
589 }
Tilman Schmidt1528b182010-02-22 13:09:52 +0000590 gig_dbg(DEBUG_EVENT, "parameter==%d", event->parameter);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800591 break;
592 }
593
594 if (resp_code == RSP_ZDLE)
595 cs->dle = event->parameter;
596
597 if (abort)
598 break;
599 }
600
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700601 cs->ev_tail = tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800602 spin_unlock_irqrestore(&cs->ev_lock, flags);
603
604 if (curarg != params)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000605 gig_dbg(DEBUG_EVENT,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700606 "invalid number of processed parameters: %d/%d",
607 curarg, params);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800608}
609EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
610
611/* disconnect
612 * process closing of connection associated with given AT state structure
613 */
614static void disconnect(struct at_state_t **at_state_p)
615{
616 unsigned long flags;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700617 struct bc_state *bcs = (*at_state_p)->bcs;
618 struct cardstate *cs = (*at_state_p)->cs;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800619
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700620 spin_lock_irqsave(&cs->lock, flags);
621 ++(*at_state_p)->seq_index;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800622
623 /* revert to selected idle mode */
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700624 if (!cs->cidmode) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800625 cs->at_state.pending_commands |= PC_UMMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000626 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800627 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800628 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700629 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800630
631 if (bcs) {
632 /* B channel assigned: invoke hardware specific handler */
633 cs->ops->close_bchannel(bcs);
Tilman Schmidt3305adf2009-10-06 12:18:26 +0000634 /* notify LL */
635 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
636 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000637 gigaset_isdn_hupD(bcs);
Tilman Schmidt3305adf2009-10-06 12:18:26 +0000638 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800639 } else {
640 /* no B channel assigned: just deallocate */
641 spin_lock_irqsave(&cs->lock, flags);
642 list_del(&(*at_state_p)->list);
643 kfree(*at_state_p);
644 *at_state_p = NULL;
645 spin_unlock_irqrestore(&cs->lock, flags);
646 }
647}
648
649/* get_free_channel
650 * get a free AT state structure: either one of those associated with the
651 * B channels of the Gigaset device, or if none of those is available,
652 * a newly allocated one with bcs=NULL
653 * The structure should be freed by calling disconnect() after use.
654 */
655static inline struct at_state_t *get_free_channel(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700656 int cid)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800657/* cids: >0: siemens-cid
658 0: without cid
659 -1: no cid assigned yet
660*/
661{
662 unsigned long flags;
663 int i;
664 struct at_state_t *ret;
665
666 for (i = 0; i < cs->channels; ++i)
667 if (gigaset_get_channel(cs->bcs + i)) {
668 ret = &cs->bcs[i].at_state;
669 ret->cid = cid;
670 return ret;
671 }
672
673 spin_lock_irqsave(&cs->lock, flags);
674 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
675 if (ret) {
676 gigaset_at_init(ret, NULL, cs, cid);
677 list_add(&ret->list, &cs->temp_at_states);
678 }
679 spin_unlock_irqrestore(&cs->lock, flags);
680 return ret;
681}
682
683static void init_failed(struct cardstate *cs, int mode)
684{
685 int i;
686 struct at_state_t *at_state;
687
688 cs->at_state.pending_commands &= ~PC_INIT;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800689 cs->mode = mode;
690 cs->mstate = MS_UNINITIALIZED;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800691 gigaset_free_channels(cs);
692 for (i = 0; i < cs->channels; ++i) {
693 at_state = &cs->bcs[i].at_state;
694 if (at_state->pending_commands & PC_CID) {
695 at_state->pending_commands &= ~PC_CID;
696 at_state->pending_commands |= PC_NOCID;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800697 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800698 }
699 }
700}
701
702static void schedule_init(struct cardstate *cs, int state)
703{
704 if (cs->at_state.pending_commands & PC_INIT) {
Tilman Schmidt1528b182010-02-22 13:09:52 +0000705 gig_dbg(DEBUG_EVENT, "not scheduling PC_INIT again");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800706 return;
707 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800708 cs->mstate = state;
709 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800710 gigaset_block_channels(cs);
711 cs->at_state.pending_commands |= PC_INIT;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000712 gig_dbg(DEBUG_EVENT, "Scheduling PC_INIT");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800713 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800714}
715
Tilman Schmidt917f5082006-04-10 22:55:00 -0700716/* Add "AT" to a command, add the cid, dle encode it, send the result to the
717 hardware. */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800718static void send_command(struct cardstate *cs, const char *cmd, int cid,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700719 int dle, gfp_t kmallocflags)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800720{
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000721 struct cmdbuf_t *cb;
722 size_t buflen;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800723
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000724 buflen = strlen(cmd) + 12; /* DLE ( A T 1 2 3 4 5 <cmd> DLE ) \0 */
725 cb = kmalloc(sizeof(struct cmdbuf_t) + buflen, kmallocflags);
726 if (!cb) {
727 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700728 return;
729 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000730 if (cid > 0 && cid <= 65535)
731 cb->len = snprintf(cb->buf, buflen,
732 dle ? "\020(AT%d%s\020)" : "AT%d%s",
733 cid, cmd);
734 else
735 cb->len = snprintf(cb->buf, buflen,
736 dle ? "\020(AT%s\020)" : "AT%s",
737 cmd);
738 cb->offset = 0;
739 cb->next = NULL;
740 cb->wake_tasklet = NULL;
741 cs->ops->write_cmd(cs, cb);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800742}
743
744static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
745{
746 struct at_state_t *at_state;
747 int i;
748 unsigned long flags;
749
750 if (cid == 0)
751 return &cs->at_state;
752
753 for (i = 0; i < cs->channels; ++i)
754 if (cid == cs->bcs[i].at_state.cid)
755 return &cs->bcs[i].at_state;
756
757 spin_lock_irqsave(&cs->lock, flags);
758
759 list_for_each_entry(at_state, &cs->temp_at_states, list)
760 if (cid == at_state->cid) {
761 spin_unlock_irqrestore(&cs->lock, flags);
762 return at_state;
763 }
764
765 spin_unlock_irqrestore(&cs->lock, flags);
766
767 return NULL;
768}
769
770static void bchannel_down(struct bc_state *bcs)
771{
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800772 if (bcs->chstate & CHS_B_UP) {
773 bcs->chstate &= ~CHS_B_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000774 gigaset_isdn_hupB(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800775 }
776
777 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
778 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000779 gigaset_isdn_hupD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800780 }
781
782 gigaset_free_channel(bcs);
783
784 gigaset_bcs_reinit(bcs);
785}
786
787static void bchannel_up(struct bc_state *bcs)
788{
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800789 if (bcs->chstate & CHS_B_UP) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700790 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
791 __func__);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800792 return;
793 }
794
795 bcs->chstate |= CHS_B_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000796 gigaset_isdn_connB(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800797}
798
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000799static void start_dial(struct at_state_t *at_state, void *data,
800 unsigned seq_index)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800801{
802 struct bc_state *bcs = at_state->bcs;
803 struct cardstate *cs = at_state->cs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000804 char **commands = data;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700805 unsigned long flags;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000806 int i;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800807
808 bcs->chstate |= CHS_NOTIFY_LL;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800809
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700810 spin_lock_irqsave(&cs->lock, flags);
811 if (at_state->seq_index != seq_index) {
812 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800813 goto error;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700814 }
815 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800816
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000817 for (i = 0; i < AT_NUM; ++i) {
818 kfree(bcs->commands[i]);
819 bcs->commands[i] = commands[i];
820 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800821
822 at_state->pending_commands |= PC_CID;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000823 gig_dbg(DEBUG_EVENT, "Scheduling PC_CID");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800824 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800825 return;
826
827error:
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000828 for (i = 0; i < AT_NUM; ++i) {
829 kfree(commands[i]);
830 commands[i] = NULL;
831 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800832 at_state->pending_commands |= PC_NOCID;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000833 gig_dbg(DEBUG_EVENT, "Scheduling PC_NOCID");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800834 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800835 return;
836}
837
838static void start_accept(struct at_state_t *at_state)
839{
840 struct cardstate *cs = at_state->cs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000841 struct bc_state *bcs = at_state->bcs;
842 int i;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800843
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000844 for (i = 0; i < AT_NUM; ++i) {
845 kfree(bcs->commands[i]);
846 bcs->commands[i] = NULL;
847 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800848
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000849 bcs->commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
850 bcs->commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
851 if (!bcs->commands[AT_PROTO] || !bcs->commands[AT_ISO]) {
852 dev_err(at_state->cs->dev, "out of memory\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800853 /* error reset */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800854 at_state->pending_commands |= PC_HUP;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000855 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800856 cs->commands_pending = 1;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000857 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800858 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000859
860 snprintf(bcs->commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
861 snprintf(bcs->commands[AT_ISO], 9, "^SISO=%u\r", bcs->channel + 1);
862
863 at_state->pending_commands |= PC_ACCEPT;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000864 gig_dbg(DEBUG_EVENT, "Scheduling PC_ACCEPT");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000865 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800866}
867
868static void do_start(struct cardstate *cs)
869{
870 gigaset_free_channels(cs);
871
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800872 if (cs->mstate != MS_LOCKED)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800873 schedule_init(cs, MS_INIT);
874
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700875 cs->isdn_up = 1;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000876 gigaset_isdn_start(cs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800877
878 cs->waiting = 0;
879 wake_up(&cs->waitqueue);
880}
881
882static void finish_shutdown(struct cardstate *cs)
883{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800884 if (cs->mstate != MS_LOCKED) {
885 cs->mstate = MS_UNINITIALIZED;
886 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800887 }
888
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700889 /* Tell the LL that the device is not available .. */
890 if (cs->isdn_up) {
891 cs->isdn_up = 0;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000892 gigaset_isdn_stop(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700893 }
894
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800895 /* The rest is done by cleanup_cs () in user mode. */
896
897 cs->cmd_result = -ENODEV;
898 cs->waiting = 0;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800899 wake_up(&cs->waitqueue);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800900}
901
902static void do_shutdown(struct cardstate *cs)
903{
904 gigaset_block_channels(cs);
905
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800906 if (cs->mstate == MS_READY) {
907 cs->mstate = MS_SHUTDOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800908 cs->at_state.pending_commands |= PC_SHUTDOWN;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000909 gig_dbg(DEBUG_EVENT, "Scheduling PC_SHUTDOWN");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800910 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800911 } else
912 finish_shutdown(cs);
913}
914
915static void do_stop(struct cardstate *cs)
916{
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700917 unsigned long flags;
918
919 spin_lock_irqsave(&cs->lock, flags);
920 cs->connected = 0;
921 spin_unlock_irqrestore(&cs->lock, flags);
922
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800923 do_shutdown(cs);
924}
925
926/* Entering cid mode or getting a cid failed:
927 * try to initialize the device and try again.
928 *
929 * channel >= 0: getting cid for the channel failed
930 * channel < 0: entering cid mode failed
931 *
932 * returns 0 on failure
933 */
934static int reinit_and_retry(struct cardstate *cs, int channel)
935{
936 int i;
937
938 if (--cs->retry_count <= 0)
939 return 0;
940
941 for (i = 0; i < cs->channels; ++i)
942 if (cs->bcs[i].at_state.cid > 0)
943 return 0;
944
945 if (channel < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700946 dev_warn(cs->dev,
947 "Could not enter cid mode. Reinit device and try again.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800948 else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700949 dev_warn(cs->dev,
950 "Could not get a call id. Reinit device and try again.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800951 cs->bcs[channel].at_state.pending_commands |= PC_CID;
952 }
953 schedule_init(cs, MS_INIT);
954 return 1;
955}
956
957static int at_state_invalid(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700958 struct at_state_t *test_ptr)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800959{
960 unsigned long flags;
961 unsigned channel;
962 struct at_state_t *at_state;
963 int retval = 0;
964
965 spin_lock_irqsave(&cs->lock, flags);
966
967 if (test_ptr == &cs->at_state)
968 goto exit;
969
970 list_for_each_entry(at_state, &cs->temp_at_states, list)
971 if (at_state == test_ptr)
972 goto exit;
973
974 for (channel = 0; channel < cs->channels; ++channel)
975 if (&cs->bcs[channel].at_state == test_ptr)
976 goto exit;
977
978 retval = 1;
979exit:
980 spin_unlock_irqrestore(&cs->lock, flags);
981 return retval;
982}
983
984static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
985 struct at_state_t **p_at_state)
986{
987 int retval;
988 struct at_state_t *at_state = *p_at_state;
989
990 retval = gigaset_isdn_icall(at_state);
991 switch (retval) {
992 case ICALL_ACCEPT:
993 break;
994 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700995 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800996 /* --v-- fall through --v-- */
997 case ICALL_IGNORE:
998 case ICALL_REJECT:
999 /* hang up actively
1000 * Device doc says that would reject the call.
1001 * In fact it doesn't.
1002 */
1003 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001004 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001005 break;
1006 }
1007}
1008
1009static int do_lock(struct cardstate *cs)
1010{
1011 int mode;
1012 int i;
1013
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001014 switch (cs->mstate) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001015 case MS_UNINITIALIZED:
1016 case MS_READY:
1017 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1018 cs->at_state.pending_commands)
1019 return -EBUSY;
1020
1021 for (i = 0; i < cs->channels; ++i)
1022 if (cs->bcs[i].at_state.pending_commands)
1023 return -EBUSY;
1024
1025 if (!gigaset_get_channels(cs))
1026 return -EBUSY;
1027
1028 break;
1029 case MS_LOCKED:
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001030 break;
1031 default:
1032 return -EBUSY;
1033 }
1034
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001035 mode = cs->mode;
1036 cs->mstate = MS_LOCKED;
1037 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001038
1039 return mode;
1040}
1041
1042static int do_unlock(struct cardstate *cs)
1043{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001044 if (cs->mstate != MS_LOCKED)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001045 return -EINVAL;
1046
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001047 cs->mstate = MS_UNINITIALIZED;
1048 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001049 gigaset_free_channels(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001050 if (cs->connected)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001051 schedule_init(cs, MS_INIT);
1052
1053 return 0;
1054}
1055
1056static void do_action(int action, struct cardstate *cs,
1057 struct bc_state *bcs,
1058 struct at_state_t **p_at_state, char **pp_command,
1059 int *p_genresp, int *p_resp_code,
1060 struct event_t *ev)
1061{
1062 struct at_state_t *at_state = *p_at_state;
1063 struct at_state_t *at_state2;
1064 unsigned long flags;
1065
1066 int channel;
1067
1068 unsigned char *s, *e;
1069 int i;
1070 unsigned long val;
1071
1072 switch (action) {
1073 case ACT_NOTHING:
1074 break;
1075 case ACT_TIMEOUT:
1076 at_state->waiting = 1;
1077 break;
1078 case ACT_INIT:
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001079 cs->at_state.pending_commands &= ~PC_INIT;
1080 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001081 cs->mode = M_UNIMODEM;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001082 spin_lock_irqsave(&cs->lock, flags);
1083 if (!cs->cidmode) {
1084 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001085 gigaset_free_channels(cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001086 cs->mstate = MS_READY;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001087 break;
1088 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001089 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001090 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001091 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001092 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001093 break;
1094 case ACT_FAILINIT:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001095 dev_warn(cs->dev, "Could not initialize the device.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001096 cs->dle = 0;
1097 init_failed(cs, M_UNKNOWN);
1098 cs->cur_at_seq = SEQ_NONE;
1099 break;
1100 case ACT_CONFIGMODE:
1101 init_failed(cs, M_CONFIG);
1102 cs->cur_at_seq = SEQ_NONE;
1103 break;
1104 case ACT_SETDLE1:
1105 cs->dle = 1;
1106 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1107 cs->inbuf[0].inputstate &=
1108 ~(INS_command | INS_DLE_command);
1109 break;
1110 case ACT_SETDLE0:
1111 cs->dle = 0;
1112 cs->inbuf[0].inputstate =
1113 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1114 | INS_command;
1115 break;
1116 case ACT_CMODESET:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001117 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001118 gigaset_free_channels(cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001119 cs->mstate = MS_READY;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001120 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001121 cs->mode = M_CID;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001122 cs->cur_at_seq = SEQ_NONE;
1123 break;
1124 case ACT_UMODESET:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001125 cs->mode = M_UNIMODEM;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001126 cs->cur_at_seq = SEQ_NONE;
1127 break;
1128 case ACT_FAILCMODE:
1129 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001130 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001131 init_failed(cs, M_UNKNOWN);
1132 break;
1133 }
1134 if (!reinit_and_retry(cs, -1))
1135 schedule_init(cs, MS_RECOVER);
1136 break;
1137 case ACT_FAILUMODE:
1138 cs->cur_at_seq = SEQ_NONE;
1139 schedule_init(cs, MS_RECOVER);
1140 break;
1141 case ACT_HUPMODEM:
1142 /* send "+++" (hangup in unimodem mode) */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001143 if (cs->connected) {
1144 struct cmdbuf_t *cb;
1145
1146 cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC);
1147 if (!cb) {
1148 dev_err(cs->dev, "%s: out of memory\n",
1149 __func__);
1150 return;
1151 }
1152 memcpy(cb->buf, "+++", 3);
1153 cb->len = 3;
1154 cb->offset = 0;
1155 cb->next = NULL;
1156 cb->wake_tasklet = NULL;
1157 cs->ops->write_cmd(cs, cb);
1158 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001159 break;
1160 case ACT_RING:
1161 /* get fresh AT state structure for new CID */
1162 at_state2 = get_free_channel(cs, ev->parameter);
1163 if (!at_state2) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001164 dev_warn(cs->dev,
1165 "RING ignored: could not allocate channel structure\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001166 break;
1167 }
1168
1169 /* initialize AT state structure
1170 * note that bcs may be NULL if no B channel is free
1171 */
1172 at_state2->ConState = 700;
Tilman Schmidt3a0a3a62010-03-14 12:58:05 +00001173 for (i = 0; i < STR_NUM; ++i) {
1174 kfree(at_state2->str_var[i]);
1175 at_state2->str_var[i] = NULL;
1176 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001177 at_state2->int_var[VAR_ZCTP] = -1;
1178
1179 spin_lock_irqsave(&cs->lock, flags);
1180 at_state2->timer_expires = RING_TIMEOUT;
1181 at_state2->timer_active = 1;
1182 spin_unlock_irqrestore(&cs->lock, flags);
1183 break;
1184 case ACT_ICALL:
1185 handle_icall(cs, bcs, p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001186 break;
1187 case ACT_FAILSDOWN:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001188 dev_warn(cs->dev, "Could not shut down the device.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001189 /* fall through */
1190 case ACT_FAKESDOWN:
1191 case ACT_SDOWN:
1192 cs->cur_at_seq = SEQ_NONE;
1193 finish_shutdown(cs);
1194 break;
1195 case ACT_CONNECT:
1196 if (cs->onechannel) {
1197 at_state->pending_commands |= PC_DLE1;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001198 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001199 break;
1200 }
1201 bcs->chstate |= CHS_D_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001202 gigaset_isdn_connD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001203 cs->ops->init_bchannel(bcs);
1204 break;
1205 case ACT_DLE1:
1206 cs->cur_at_seq = SEQ_NONE;
1207 bcs = cs->bcs + cs->curchannel;
1208
1209 bcs->chstate |= CHS_D_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001210 gigaset_isdn_connD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001211 cs->ops->init_bchannel(bcs);
1212 break;
1213 case ACT_FAKEHUP:
1214 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1215 /* fall through */
1216 case ACT_DISCONNECT:
1217 cs->cur_at_seq = SEQ_NONE;
1218 at_state->cid = -1;
1219 if (bcs && cs->onechannel && cs->dle) {
1220 /* Check for other open channels not needed:
1221 * DLE only used for M10x with one B channel.
1222 */
1223 at_state->pending_commands |= PC_DLE0;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001224 cs->commands_pending = 1;
Tilman Schmidt698e3ed2006-06-26 00:25:34 -07001225 } else
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001226 disconnect(p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001227 break;
1228 case ACT_FAKEDLE0:
1229 at_state->int_var[VAR_ZDLE] = 0;
1230 cs->dle = 0;
1231 /* fall through */
1232 case ACT_DLE0:
1233 cs->cur_at_seq = SEQ_NONE;
1234 at_state2 = &cs->bcs[cs->curchannel].at_state;
1235 disconnect(&at_state2);
1236 break;
1237 case ACT_ABORTHUP:
1238 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001239 dev_warn(cs->dev, "Could not hang up.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001240 at_state->cid = -1;
1241 if (bcs && cs->onechannel)
1242 at_state->pending_commands |= PC_DLE0;
Tilman Schmidt698e3ed2006-06-26 00:25:34 -07001243 else
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001244 disconnect(p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001245 schedule_init(cs, MS_RECOVER);
1246 break;
1247 case ACT_FAILDLE0:
1248 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001249 dev_warn(cs->dev, "Could not leave DLE mode.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001250 at_state2 = &cs->bcs[cs->curchannel].at_state;
1251 disconnect(&at_state2);
1252 schedule_init(cs, MS_RECOVER);
1253 break;
1254 case ACT_FAILDLE1:
1255 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001256 dev_warn(cs->dev,
1257 "Could not enter DLE mode. Trying to hang up.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001258 channel = cs->curchannel;
1259 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001260 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001261 break;
1262
1263 case ACT_CID: /* got cid; start dialing */
1264 cs->cur_at_seq = SEQ_NONE;
1265 channel = cs->curchannel;
1266 if (ev->parameter > 0 && ev->parameter <= 65535) {
1267 cs->bcs[channel].at_state.cid = ev->parameter;
1268 cs->bcs[channel].at_state.pending_commands |=
1269 PC_DIAL;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001270 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001271 break;
1272 }
1273 /* fall through */
1274 case ACT_FAILCID:
1275 cs->cur_at_seq = SEQ_NONE;
1276 channel = cs->curchannel;
1277 if (!reinit_and_retry(cs, channel)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001278 dev_warn(cs->dev,
1279 "Could not get a call ID. Cannot dial.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001280 at_state2 = &cs->bcs[channel].at_state;
1281 disconnect(&at_state2);
1282 }
1283 break;
1284 case ACT_ABORTCID:
1285 cs->cur_at_seq = SEQ_NONE;
1286 at_state2 = &cs->bcs[cs->curchannel].at_state;
1287 disconnect(&at_state2);
1288 break;
1289
1290 case ACT_DIALING:
1291 case ACT_ACCEPTED:
1292 cs->cur_at_seq = SEQ_NONE;
1293 break;
1294
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001295 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL procssng */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001296 disconnect(p_at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001297 break;
1298
1299 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1300 cs->cur_at_seq = SEQ_NONE;
1301 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001302 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001303 break;
1304
1305 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1306 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1307 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1308 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001309 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001310 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001311 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
Tilman Schmidt784d5852006-04-10 22:55:04 -07001312 are not handled properly anymore */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001313 at_state->getstring = 1;
1314 break;
1315 case ACT_SETVER:
1316 if (!ev->ptr) {
1317 *p_genresp = 1;
1318 *p_resp_code = RSP_ERROR;
1319 break;
1320 }
1321 s = ev->ptr;
1322
1323 if (!strcmp(s, "OK")) {
1324 *p_genresp = 1;
1325 *p_resp_code = RSP_ERROR;
1326 break;
1327 }
1328
1329 for (i = 0; i < 4; ++i) {
1330 val = simple_strtoul(s, (char **) &e, 10);
1331 if (val > INT_MAX || e == s)
1332 break;
1333 if (i == 3) {
1334 if (*e)
1335 break;
1336 } else if (*e != '.')
1337 break;
1338 else
1339 s = e + 1;
1340 cs->fwver[i] = val;
1341 }
1342 if (i != 4) {
1343 *p_genresp = 1;
1344 *p_resp_code = RSP_ERROR;
1345 break;
1346 }
1347 /*at_state->getstring = 1;*/
1348 cs->gotfwver = 0;
1349 break;
1350 case ACT_GOTVER:
1351 if (cs->gotfwver == 0) {
1352 cs->gotfwver = 1;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001353 gig_dbg(DEBUG_EVENT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001354 "firmware version %02d.%03d.%02d.%02d",
1355 cs->fwver[0], cs->fwver[1],
1356 cs->fwver[2], cs->fwver[3]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001357 break;
1358 }
1359 /* fall through */
1360 case ACT_FAILVER:
1361 cs->gotfwver = -1;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001362 dev_err(cs->dev, "could not read firmware version.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001363 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001364 case ACT_ERROR:
Tilman Schmidt05eae942009-10-06 12:18:46 +00001365 gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d",
1366 __func__, at_state->ConState);
1367 cs->cur_at_seq = SEQ_NONE;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001368 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001369 case ACT_DEBUG:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001370 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001371 __func__, ev->type, at_state->ConState);
1372 break;
1373 case ACT_WARN:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001374 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1375 __func__, ev->type, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001376 break;
1377 case ACT_ZCAU:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001378 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1379 ev->parameter, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001380 break;
1381
1382 /* events from the LL */
1383 case ACT_DIAL:
1384 start_dial(at_state, ev->ptr, ev->parameter);
1385 break;
1386 case ACT_ACCEPT:
1387 start_accept(at_state);
1388 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001389 case ACT_HUP:
1390 at_state->pending_commands |= PC_HUP;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001391 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001392 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001393 break;
1394
1395 /* hotplug events */
1396 case ACT_STOP:
1397 do_stop(cs);
1398 break;
1399 case ACT_START:
1400 do_start(cs);
1401 break;
1402
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001403 /* events from the interface */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001404 case ACT_IF_LOCK:
1405 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1406 cs->waiting = 0;
1407 wake_up(&cs->waitqueue);
1408 break;
1409 case ACT_IF_VER:
1410 if (ev->parameter != 0)
1411 cs->cmd_result = -EINVAL;
1412 else if (cs->gotfwver != 1) {
1413 cs->cmd_result = -ENOENT;
1414 } else {
1415 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1416 cs->cmd_result = 0;
1417 }
1418 cs->waiting = 0;
1419 wake_up(&cs->waitqueue);
1420 break;
1421
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001422 /* events from the proc file system */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001423 case ACT_PROC_CIDMODE:
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001424 spin_lock_irqsave(&cs->lock, flags);
1425 if (ev->parameter != cs->cidmode) {
1426 cs->cidmode = ev->parameter;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001427 if (ev->parameter) {
1428 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001429 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001430 } else {
1431 cs->at_state.pending_commands |= PC_UMMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001432 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001433 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001434 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001435 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001436 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001437 cs->waiting = 0;
1438 wake_up(&cs->waitqueue);
1439 break;
1440
1441 /* events from the hardware drivers */
1442 case ACT_NOTIFY_BC_DOWN:
1443 bchannel_down(bcs);
1444 break;
1445 case ACT_NOTIFY_BC_UP:
1446 bchannel_up(bcs);
1447 break;
1448 case ACT_SHUTDOWN:
1449 do_shutdown(cs);
1450 break;
1451
1452
1453 default:
1454 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1455 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1456 if (!*pp_command) {
1457 *p_genresp = 1;
1458 *p_resp_code = RSP_NULL;
1459 }
1460 } else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001461 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001462 }
1463}
1464
1465/* State machine to do the calling and hangup procedure */
1466static void process_event(struct cardstate *cs, struct event_t *ev)
1467{
1468 struct bc_state *bcs;
1469 char *p_command = NULL;
1470 struct reply_t *rep;
1471 int rcode;
1472 int genresp = 0;
1473 int resp_code = RSP_ERROR;
1474 int sendcid;
1475 struct at_state_t *at_state;
1476 int index;
1477 int curact;
1478 unsigned long flags;
1479
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001480 if (ev->cid >= 0) {
1481 at_state = at_state_from_cid(cs, ev->cid);
1482 if (!at_state) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001483 gig_dbg(DEBUG_EVENT, "event %d for invalid cid %d",
1484 ev->type, ev->cid);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001485 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001486 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001487 return;
1488 }
1489 } else {
1490 at_state = ev->at_state;
1491 if (at_state_invalid(cs, at_state)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001492 gig_dbg(DEBUG_EVENT, "event for invalid at_state %p",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001493 at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001494 return;
1495 }
1496 }
1497
Tilman Schmidt1528b182010-02-22 13:09:52 +00001498 gig_dbg(DEBUG_EVENT, "connection state %d, event %d",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001499 at_state->ConState, ev->type);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001500
1501 bcs = at_state->bcs;
1502 sendcid = at_state->cid;
1503
1504 /* Setting the pointer to the dial array */
1505 rep = at_state->replystruct;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001506
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001507 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001508 if (ev->type == EV_TIMEOUT) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001509 if (ev->parameter != at_state->timer_index
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001510 || !at_state->timer_active) {
1511 ev->type = RSP_NONE; /* old timeout */
Tilman Schmidt1528b182010-02-22 13:09:52 +00001512 gig_dbg(DEBUG_EVENT, "old timeout");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001513 } else if (!at_state->waiting)
Tilman Schmidt1528b182010-02-22 13:09:52 +00001514 gig_dbg(DEBUG_EVENT, "timeout occurred");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001515 else
Tilman Schmidt1528b182010-02-22 13:09:52 +00001516 gig_dbg(DEBUG_EVENT, "stopped waiting");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001517 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001518 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001519
Tilman Schmidt917f5082006-04-10 22:55:00 -07001520 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1521 or at_state->str_var[STR_XXXX], set it */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001522 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1523 index = ev->type - RSP_VAR;
1524 at_state->int_var[index] = ev->parameter;
1525 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1526 index = ev->type - RSP_STR;
1527 kfree(at_state->str_var[index]);
1528 at_state->str_var[index] = ev->ptr;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001529 ev->ptr = NULL; /* prevent process_events() from
1530 deallocating ptr */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001531 }
1532
1533 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1534 at_state->getstring = 0;
1535
Tilman Schmidt917f5082006-04-10 22:55:00 -07001536 /* Search row in dial array which matches modem response and current
1537 constate */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001538 for (;; rep++) {
1539 rcode = rep->resp_code;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001540 if (rcode == RSP_LAST) {
1541 /* found nothing...*/
Tilman Schmidt784d5852006-04-10 22:55:04 -07001542 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1543 "resp_code %d in ConState %d!\n",
1544 __func__, ev->type, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001545 return;
1546 }
1547 if ((rcode == RSP_ANY || rcode == ev->type)
1548 && ((int) at_state->ConState >= rep->min_ConState)
1549 && (rep->max_ConState < 0
1550 || (int) at_state->ConState <= rep->max_ConState)
1551 && (rep->parameter < 0 || rep->parameter == ev->parameter))
1552 break;
1553 }
1554
1555 p_command = rep->command;
1556
1557 at_state->waiting = 0;
1558 for (curact = 0; curact < MAXACT; ++curact) {
1559 /* The row tells us what we should do ..
1560 */
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001561 do_action(rep->action[curact], cs, bcs, &at_state, &p_command,
1562 &genresp, &resp_code, ev);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001563 if (!at_state)
1564 break; /* may be freed after disconnect */
1565 }
1566
1567 if (at_state) {
1568 /* Jump to the next con-state regarding the array */
1569 if (rep->new_ConState >= 0)
1570 at_state->ConState = rep->new_ConState;
1571
1572 if (genresp) {
1573 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001574 at_state->timer_expires = 0;
1575 at_state->timer_active = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001576 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001577 gigaset_add_event(cs, at_state, resp_code,
1578 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001579 } else {
1580 /* Send command to modem if not NULL... */
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001581 if (p_command) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001582 if (cs->connected)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001583 send_command(cs, p_command,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001584 sendcid, cs->dle,
1585 GFP_ATOMIC);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001586 else
1587 gigaset_add_event(cs, at_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001588 RSP_NODEV,
1589 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001590 }
1591
1592 spin_lock_irqsave(&cs->lock, flags);
1593 if (!rep->timeout) {
1594 at_state->timer_expires = 0;
1595 at_state->timer_active = 0;
1596 } else if (rep->timeout > 0) { /* new timeout */
1597 at_state->timer_expires = rep->timeout * 10;
1598 at_state->timer_active = 1;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001599 ++at_state->timer_index;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001600 }
1601 spin_unlock_irqrestore(&cs->lock, flags);
1602 }
1603 }
1604}
1605
1606static void schedule_sequence(struct cardstate *cs,
1607 struct at_state_t *at_state, int sequence)
1608{
1609 cs->cur_at_seq = sequence;
1610 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1611}
1612
1613static void process_command_flags(struct cardstate *cs)
1614{
1615 struct at_state_t *at_state = NULL;
1616 struct bc_state *bcs;
1617 int i;
1618 int sequence;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001619 unsigned long flags;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001620
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001621 cs->commands_pending = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001622
1623 if (cs->cur_at_seq) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001624 gig_dbg(DEBUG_EVENT, "not searching scheduled commands: busy");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001625 return;
1626 }
1627
Tilman Schmidt1528b182010-02-22 13:09:52 +00001628 gig_dbg(DEBUG_EVENT, "searching scheduled commands");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001629
1630 sequence = SEQ_NONE;
1631
1632 /* clear pending_commands and hangup channels on shutdown */
1633 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1634 cs->at_state.pending_commands &= ~PC_CIDMODE;
1635 for (i = 0; i < cs->channels; ++i) {
1636 bcs = cs->bcs + i;
1637 at_state = &bcs->at_state;
1638 at_state->pending_commands &=
1639 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1640 if (at_state->cid > 0)
1641 at_state->pending_commands |= PC_HUP;
1642 if (at_state->pending_commands & PC_CID) {
1643 at_state->pending_commands |= PC_NOCID;
1644 at_state->pending_commands &= ~PC_CID;
1645 }
1646 }
1647 }
1648
1649 /* clear pending_commands and hangup channels on reset */
1650 if (cs->at_state.pending_commands & PC_INIT) {
1651 cs->at_state.pending_commands &= ~PC_CIDMODE;
1652 for (i = 0; i < cs->channels; ++i) {
1653 bcs = cs->bcs + i;
1654 at_state = &bcs->at_state;
1655 at_state->pending_commands &=
1656 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1657 if (at_state->cid > 0)
1658 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001659 if (cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001660 if (at_state->pending_commands & PC_CID) {
1661 at_state->pending_commands |= PC_NOCID;
1662 at_state->pending_commands &= ~PC_CID;
1663 }
1664 }
1665 }
1666 }
1667
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001668 /* only switch back to unimodem mode if no commands are pending and
1669 * no channels are up */
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001670 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001671 if (cs->at_state.pending_commands == PC_UMMODE
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001672 && !cs->cidmode
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001673 && list_empty(&cs->temp_at_states)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001674 && cs->mode == M_CID) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001675 sequence = SEQ_UMMODE;
1676 at_state = &cs->at_state;
1677 for (i = 0; i < cs->channels; ++i) {
1678 bcs = cs->bcs + i;
1679 if (bcs->at_state.pending_commands ||
1680 bcs->at_state.cid > 0) {
1681 sequence = SEQ_NONE;
1682 break;
1683 }
1684 }
1685 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001686 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001687 cs->at_state.pending_commands &= ~PC_UMMODE;
1688 if (sequence != SEQ_NONE) {
1689 schedule_sequence(cs, at_state, sequence);
1690 return;
1691 }
1692
1693 for (i = 0; i < cs->channels; ++i) {
1694 bcs = cs->bcs + i;
1695 if (bcs->at_state.pending_commands & PC_HUP) {
1696 bcs->at_state.pending_commands &= ~PC_HUP;
1697 if (bcs->at_state.pending_commands & PC_CID) {
1698 /* not yet dialing: PC_NOCID is sufficient */
1699 bcs->at_state.pending_commands |= PC_NOCID;
1700 bcs->at_state.pending_commands &= ~PC_CID;
1701 } else {
1702 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1703 return;
1704 }
1705 }
1706 if (bcs->at_state.pending_commands & PC_NOCID) {
1707 bcs->at_state.pending_commands &= ~PC_NOCID;
1708 cs->curchannel = bcs->channel;
1709 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1710 return;
1711 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1712 bcs->at_state.pending_commands &= ~PC_DLE0;
1713 cs->curchannel = bcs->channel;
1714 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1715 return;
1716 }
1717 }
1718
1719 list_for_each_entry(at_state, &cs->temp_at_states, list)
1720 if (at_state->pending_commands & PC_HUP) {
1721 at_state->pending_commands &= ~PC_HUP;
1722 schedule_sequence(cs, at_state, SEQ_HUP);
1723 return;
1724 }
1725
1726 if (cs->at_state.pending_commands & PC_INIT) {
1727 cs->at_state.pending_commands &= ~PC_INIT;
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001728 cs->dle = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001729 cs->inbuf->inputstate = INS_command;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001730 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1731 return;
1732 }
1733 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1734 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1735 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1736 return;
1737 }
1738 if (cs->at_state.pending_commands & PC_CIDMODE) {
1739 cs->at_state.pending_commands &= ~PC_CIDMODE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001740 if (cs->mode == M_UNIMODEM) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001741 cs->retry_count = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001742 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1743 return;
1744 }
1745 }
1746
1747 for (i = 0; i < cs->channels; ++i) {
1748 bcs = cs->bcs + i;
1749 if (bcs->at_state.pending_commands & PC_DLE1) {
1750 bcs->at_state.pending_commands &= ~PC_DLE1;
1751 cs->curchannel = bcs->channel;
1752 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1753 return;
1754 }
1755 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1756 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1757 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1758 return;
1759 }
1760 if (bcs->at_state.pending_commands & PC_DIAL) {
1761 bcs->at_state.pending_commands &= ~PC_DIAL;
1762 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1763 return;
1764 }
1765 if (bcs->at_state.pending_commands & PC_CID) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001766 switch (cs->mode) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001767 case M_UNIMODEM:
1768 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001769 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001770 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001771 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001772 case M_UNKNOWN:
1773 schedule_init(cs, MS_INIT);
1774 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001775 }
1776 bcs->at_state.pending_commands &= ~PC_CID;
1777 cs->curchannel = bcs->channel;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001778 cs->retry_count = 2;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001779 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1780 return;
1781 }
1782 }
1783}
1784
1785static void process_events(struct cardstate *cs)
1786{
1787 struct event_t *ev;
1788 unsigned head, tail;
1789 int i;
1790 int check_flags = 0;
1791 int was_busy;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001792 unsigned long flags;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001793
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001794 spin_lock_irqsave(&cs->ev_lock, flags);
1795 head = cs->ev_head;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001796
1797 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001798 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001799 if (tail == head) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001800 if (!check_flags && !cs->commands_pending)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001801 break;
1802 check_flags = 0;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001803 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001804 process_command_flags(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001805 spin_lock_irqsave(&cs->ev_lock, flags);
1806 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001807 if (tail == head) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001808 if (!cs->commands_pending)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001809 break;
1810 continue;
1811 }
1812 }
1813
1814 ev = cs->events + head;
1815 was_busy = cs->cur_at_seq != SEQ_NONE;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001816 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001817 process_event(cs, ev);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001818 spin_lock_irqsave(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001819 kfree(ev->ptr);
1820 ev->ptr = NULL;
1821 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1822 check_flags = 1;
1823
1824 head = (head + 1) % MAX_EVENTS;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001825 cs->ev_head = head;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001826 }
1827
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001828 spin_unlock_irqrestore(&cs->ev_lock, flags);
1829
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001830 if (i == 2 * MAX_EVENTS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001831 dev_err(cs->dev,
1832 "infinite loop in process_events; aborting.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001833 }
1834}
1835
1836/* tasklet scheduled on any event received from the Gigaset device
1837 * parameter:
1838 * data ISDN controller state structure
1839 */
1840void gigaset_handle_event(unsigned long data)
1841{
1842 struct cardstate *cs = (struct cardstate *) data;
1843
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001844 /* handle incoming data on control/common channel */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001845 if (cs->inbuf->head != cs->inbuf->tail) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001846 gig_dbg(DEBUG_INTR, "processing new data");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001847 cs->ops->handle_input(cs->inbuf);
1848 }
1849
1850 process_events(cs);
1851}