blob: d1b84f4888340e3010aecd7ae1787e855a38dd70 [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
Paul Gortmaker5d76fc22011-07-10 12:23:16 -040016#include <linux/export.h>
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080017#include "gigaset.h"
18
19/* ========================================================== */
20/* bit masks for pending commands */
Tilman Schmidt917f5082006-04-10 22:55:00 -070021#define PC_DIAL 0x001
22#define PC_HUP 0x002
23#define PC_INIT 0x004
24#define PC_DLE0 0x008
25#define PC_DLE1 0x010
26#define PC_SHUTDOWN 0x020
27#define PC_ACCEPT 0x040
28#define PC_CID 0x080
29#define PC_NOCID 0x100
30#define PC_CIDMODE 0x200
31#define PC_UMMODE 0x400
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080032
33/* types of modem responses */
Tilman Schmidt917f5082006-04-10 22:55:00 -070034#define RT_NOTHING 0
35#define RT_ZSAU 1
36#define RT_RING 2
37#define RT_NUMBER 3
38#define RT_STRING 4
Tilman Schmidt917f5082006-04-10 22:55:00 -070039#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 Schmidt2ed5e4f2010-07-05 14:18:48 +000043#define RSP_ERROR 1
Tilman Schmidt917f5082006-04-10 22:55:00 -070044#define RSP_ZGCI 3
45#define RSP_RING 4
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000046#define RSP_ZVLS 5
47#define RSP_ZCAU 6
48
49/* responses with values to store in at_state */
50/* - numeric */
Tilman Schmidt917f5082006-04-10 22:55:00 -070051#define RSP_VAR 100
52#define RSP_ZSAU (RSP_VAR + VAR_ZSAU)
53#define RSP_ZDLE (RSP_VAR + VAR_ZDLE)
Tilman Schmidt917f5082006-04-10 22:55:00 -070054#define RSP_ZCTP (RSP_VAR + VAR_ZCTP)
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000055/* - string */
Tilman Schmidt917f5082006-04-10 22:55:00 -070056#define RSP_STR (RSP_VAR + VAR_NUM)
57#define RSP_NMBR (RSP_STR + STR_NMBR)
58#define RSP_ZCPN (RSP_STR + STR_ZCPN)
59#define RSP_ZCON (RSP_STR + STR_ZCON)
60#define RSP_ZBC (RSP_STR + STR_ZBC)
61#define RSP_ZHLC (RSP_STR + STR_ZHLC)
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000062
Tilman Schmidt917f5082006-04-10 22:55:00 -070063#define RSP_WRONG_CID -2 /* unknown cid in cmd */
Tilman Schmidt917f5082006-04-10 22:55:00 -070064#define RSP_INVAL -6 /* invalid response */
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +000065#define RSP_NODEV -9 /* device not connected */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080066
Tilman Schmidt917f5082006-04-10 22:55:00 -070067#define RSP_NONE -19
68#define RSP_STRING -20
69#define RSP_NULL -21
Tilman Schmidt917f5082006-04-10 22:55:00 -070070#define RSP_INIT -27
71#define RSP_ANY -26
72#define RSP_LAST -28
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080073
74/* actions for process_response */
75#define ACT_NOTHING 0
76#define ACT_SETDLE1 1
77#define ACT_SETDLE0 2
78#define ACT_FAILINIT 3
79#define ACT_HUPMODEM 4
80#define ACT_CONFIGMODE 5
81#define ACT_INIT 6
82#define ACT_DLE0 7
83#define ACT_DLE1 8
84#define ACT_FAILDLE0 9
85#define ACT_FAILDLE1 10
86#define ACT_RING 11
87#define ACT_CID 12
88#define ACT_FAILCID 13
89#define ACT_SDOWN 14
90#define ACT_FAILSDOWN 15
91#define ACT_DEBUG 16
92#define ACT_WARN 17
93#define ACT_DIALING 18
94#define ACT_ABORTDIAL 19
95#define ACT_DISCONNECT 20
96#define ACT_CONNECT 21
97#define ACT_REMOTEREJECT 22
Tilman Schmidt917f5082006-04-10 22:55:00 -070098#define ACT_CONNTIMEOUT 23
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -080099#define ACT_REMOTEHUP 24
100#define ACT_ABORTHUP 25
101#define ACT_ICALL 26
102#define ACT_ACCEPTED 27
103#define ACT_ABORTACCEPT 28
104#define ACT_TIMEOUT 29
105#define ACT_GETSTRING 30
106#define ACT_SETVER 31
107#define ACT_FAILVER 32
108#define ACT_GOTVER 33
109#define ACT_TEST 34
110#define ACT_ERROR 35
111#define ACT_ABORTCID 36
112#define ACT_ZCAU 37
Tilman Schmidt917f5082006-04-10 22:55:00 -0700113#define ACT_NOTIFY_BC_DOWN 38
114#define ACT_NOTIFY_BC_UP 39
115#define ACT_DIAL 40
116#define ACT_ACCEPT 41
Tilman Schmidt917f5082006-04-10 22:55:00 -0700117#define ACT_HUP 43
118#define ACT_IF_LOCK 44
119#define ACT_START 45
120#define ACT_STOP 46
121#define ACT_FAKEDLE0 47
122#define ACT_FAKEHUP 48
123#define ACT_FAKESDOWN 49
124#define ACT_SHUTDOWN 50
125#define ACT_PROC_CIDMODE 51
126#define ACT_UMODESET 52
127#define ACT_FAILUMODE 53
128#define ACT_CMODESET 54
129#define ACT_FAILCMODE 55
130#define ACT_IF_VER 56
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800131#define ACT_CMD 100
132
133/* at command sequences */
Tilman Schmidt917f5082006-04-10 22:55:00 -0700134#define SEQ_NONE 0
135#define SEQ_INIT 100
136#define SEQ_DLE0 200
137#define SEQ_DLE1 250
138#define SEQ_CID 300
139#define SEQ_NOCID 350
140#define SEQ_HUP 400
141#define SEQ_DIAL 600
142#define SEQ_ACCEPT 720
143#define SEQ_SHUTDOWN 500
144#define SEQ_CIDMODE 10
145#define SEQ_UMMODE 11
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800146
147
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000148/* 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid),
149 * 400: hup, 500: reset, 600: dial, 700: ring */
Tilman Schmidt528efc62009-05-13 12:44:17 +0000150struct reply_t gigaset_tab_nocid[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800151{
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000152/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
153 * action, command */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800154
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000155/* initialize device, set cid mode if possible */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000156 {RSP_INIT, -1, -1, SEQ_INIT, 100, 1, {ACT_TIMEOUT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800157
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000158 {EV_TIMEOUT, 100, 100, -1, 101, 3, {0}, "Z\r"},
159 {RSP_OK, 101, 103, -1, 120, 5, {ACT_GETSTRING},
160 "+GMR\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800161
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000162 {EV_TIMEOUT, 101, 101, -1, 102, 5, {0}, "Z\r"},
163 {RSP_ERROR, 101, 101, -1, 102, 5, {0}, "Z\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800164
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000165 {EV_TIMEOUT, 102, 102, -1, 108, 5, {ACT_SETDLE1},
166 "^SDLE=0\r"},
167 {RSP_OK, 108, 108, -1, 104, -1},
168 {RSP_ZDLE, 104, 104, 0, 103, 5, {0}, "Z\r"},
169 {EV_TIMEOUT, 104, 104, -1, 0, 0, {ACT_FAILINIT} },
170 {RSP_ERROR, 108, 108, -1, 0, 0, {ACT_FAILINIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800171
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000172 {EV_TIMEOUT, 108, 108, -1, 105, 2, {ACT_SETDLE0,
173 ACT_HUPMODEM,
174 ACT_TIMEOUT} },
175 {EV_TIMEOUT, 105, 105, -1, 103, 5, {0}, "Z\r"},
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800176
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000177 {RSP_ERROR, 102, 102, -1, 107, 5, {0}, "^GETPRE\r"},
178 {RSP_OK, 107, 107, -1, 0, 0, {ACT_CONFIGMODE} },
179 {RSP_ERROR, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
180 {EV_TIMEOUT, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800181
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000182 {RSP_ERROR, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
183 {EV_TIMEOUT, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800184
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000185 {RSP_STRING, 120, 120, -1, 121, -1, {ACT_SETVER} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800186
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000187 {EV_TIMEOUT, 120, 121, -1, 0, 0, {ACT_FAILVER,
188 ACT_INIT} },
189 {RSP_ERROR, 120, 121, -1, 0, 0, {ACT_FAILVER,
190 ACT_INIT} },
191 {RSP_OK, 121, 121, -1, 0, 0, {ACT_GOTVER,
192 ACT_INIT} },
193 {RSP_NONE, 121, 121, -1, 120, 0, {ACT_GETSTRING} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800194
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000195/* leave dle mode */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000196 {RSP_INIT, 0, 0, SEQ_DLE0, 201, 5, {0}, "^SDLE=0\r"},
197 {RSP_OK, 201, 201, -1, 202, -1},
198 {RSP_ZDLE, 202, 202, 0, 0, 0, {ACT_DLE0} },
199 {RSP_NODEV, 200, 249, -1, 0, 0, {ACT_FAKEDLE0} },
200 {RSP_ERROR, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
201 {EV_TIMEOUT, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800202
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000203/* enter dle mode */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000204 {RSP_INIT, 0, 0, SEQ_DLE1, 251, 5, {0}, "^SDLE=1\r"},
205 {RSP_OK, 251, 251, -1, 252, -1},
206 {RSP_ZDLE, 252, 252, 1, 0, 0, {ACT_DLE1} },
207 {RSP_ERROR, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
208 {EV_TIMEOUT, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800209
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000210/* incoming call */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000211 {RSP_RING, -1, -1, -1, -1, -1, {ACT_RING} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800212
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000213/* get cid */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000214 {RSP_INIT, 0, 0, SEQ_CID, 301, 5, {0}, "^SGCI?\r"},
215 {RSP_OK, 301, 301, -1, 302, -1},
216 {RSP_ZGCI, 302, 302, -1, 0, 0, {ACT_CID} },
217 {RSP_ERROR, 301, 349, -1, 0, 0, {ACT_FAILCID} },
218 {EV_TIMEOUT, 301, 349, -1, 0, 0, {ACT_FAILCID} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800219
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000220/* enter cid mode */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000221 {RSP_INIT, 0, 0, SEQ_CIDMODE, 150, 5, {0}, "^SGCI=1\r"},
222 {RSP_OK, 150, 150, -1, 0, 0, {ACT_CMODESET} },
223 {RSP_ERROR, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
224 {EV_TIMEOUT, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800225
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000226/* leave cid mode */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000227 {RSP_INIT, 0, 0, SEQ_UMMODE, 160, 5, {0}, "Z\r"},
228 {RSP_OK, 160, 160, -1, 0, 0, {ACT_UMODESET} },
229 {RSP_ERROR, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
230 {EV_TIMEOUT, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800231
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000232/* abort getting cid */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000233 {RSP_INIT, 0, 0, SEQ_NOCID, 0, 0, {ACT_ABORTCID} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800234
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000235/* reset */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000236 {RSP_INIT, 0, 0, SEQ_SHUTDOWN, 504, 5, {0}, "Z\r"},
237 {RSP_OK, 504, 504, -1, 0, 0, {ACT_SDOWN} },
238 {RSP_ERROR, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
239 {EV_TIMEOUT, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
240 {RSP_NODEV, 501, 599, -1, 0, 0, {ACT_FAKESDOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800241
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000242 {EV_PROC_CIDMODE, -1, -1, -1, -1, -1, {ACT_PROC_CIDMODE} },
243 {EV_IF_LOCK, -1, -1, -1, -1, -1, {ACT_IF_LOCK} },
244 {EV_IF_VER, -1, -1, -1, -1, -1, {ACT_IF_VER} },
245 {EV_START, -1, -1, -1, -1, -1, {ACT_START} },
246 {EV_STOP, -1, -1, -1, -1, -1, {ACT_STOP} },
247 {EV_SHUTDOWN, -1, -1, -1, -1, -1, {ACT_SHUTDOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800248
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000249/* misc. */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000250 {RSP_ERROR, -1, -1, -1, -1, -1, {ACT_ERROR} },
251 {RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
252 {RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
253 {RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
Joe Perches475be4d2012-02-19 19:52:38 -0800254 {RSP_LAST}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800255};
256
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000257/* 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring,
258 * 400: hup, 750: accepted icall */
Tilman Schmidt528efc62009-05-13 12:44:17 +0000259struct reply_t gigaset_tab_cid[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800260{
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000261/* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
262 * action, command */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800263
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000264/* dial */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000265 {EV_DIAL, -1, -1, -1, -1, -1, {ACT_DIAL} },
266 {RSP_INIT, 0, 0, SEQ_DIAL, 601, 5, {ACT_CMD + AT_BC} },
267 {RSP_OK, 601, 601, -1, 603, 5, {ACT_CMD + AT_PROTO} },
268 {RSP_OK, 603, 603, -1, 604, 5, {ACT_CMD + AT_TYPE} },
269 {RSP_OK, 604, 604, -1, 605, 5, {ACT_CMD + AT_MSN} },
270 {RSP_NULL, 605, 605, -1, 606, 5, {ACT_CMD + AT_CLIP} },
271 {RSP_OK, 605, 605, -1, 606, 5, {ACT_CMD + AT_CLIP} },
272 {RSP_NULL, 606, 606, -1, 607, 5, {ACT_CMD + AT_ISO} },
273 {RSP_OK, 606, 606, -1, 607, 5, {ACT_CMD + AT_ISO} },
274 {RSP_OK, 607, 607, -1, 608, 5, {0}, "+VLS=17\r"},
275 {RSP_OK, 608, 608, -1, 609, -1},
276 {RSP_ZSAU, 609, 609, ZSAU_PROCEEDING, 610, 5, {ACT_CMD + AT_DIAL} },
277 {RSP_OK, 610, 610, -1, 650, 0, {ACT_DIALING} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800278
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000279 {RSP_ERROR, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
280 {EV_TIMEOUT, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800281
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000282/* optional dialing responses */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000283 {EV_BC_OPEN, 650, 650, -1, 651, -1},
284 {RSP_ZVLS, 609, 651, 17, -1, -1, {ACT_DEBUG} },
285 {RSP_ZCTP, 610, 651, -1, -1, -1, {ACT_DEBUG} },
286 {RSP_ZCPN, 610, 651, -1, -1, -1, {ACT_DEBUG} },
287 {RSP_ZSAU, 650, 651, ZSAU_CALL_DELIVERED, -1, -1, {ACT_DEBUG} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800288
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000289/* connect */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000290 {RSP_ZSAU, 650, 650, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
291 {RSP_ZSAU, 651, 651, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
292 ACT_NOTIFY_BC_UP} },
293 {RSP_ZSAU, 750, 750, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
294 {RSP_ZSAU, 751, 751, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
295 ACT_NOTIFY_BC_UP} },
296 {EV_BC_OPEN, 800, 800, -1, 800, -1, {ACT_NOTIFY_BC_UP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800297
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000298/* remote hangup */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000299 {RSP_ZSAU, 650, 651, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT} },
300 {RSP_ZSAU, 750, 751, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
301 {RSP_ZSAU, 800, 800, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800302
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000303/* hangup */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000304 {EV_HUP, -1, -1, -1, -1, -1, {ACT_HUP} },
305 {RSP_INIT, -1, -1, SEQ_HUP, 401, 5, {0}, "+VLS=0\r"},
306 {RSP_OK, 401, 401, -1, 402, 5},
307 {RSP_ZVLS, 402, 402, 0, 403, 5},
308 {RSP_ZSAU, 403, 403, ZSAU_DISCONNECT_REQ, -1, -1, {ACT_DEBUG} },
309 {RSP_ZSAU, 403, 403, ZSAU_NULL, 0, 0, {ACT_DISCONNECT} },
310 {RSP_NODEV, 401, 403, -1, 0, 0, {ACT_FAKEHUP} },
311 {RSP_ERROR, 401, 401, -1, 0, 0, {ACT_ABORTHUP} },
312 {EV_TIMEOUT, 401, 403, -1, 0, 0, {ACT_ABORTHUP} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800313
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000314 {EV_BC_CLOSED, 0, 0, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800315
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000316/* ring */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000317 {RSP_ZBC, 700, 700, -1, -1, -1, {0} },
318 {RSP_ZHLC, 700, 700, -1, -1, -1, {0} },
319 {RSP_NMBR, 700, 700, -1, -1, -1, {0} },
320 {RSP_ZCPN, 700, 700, -1, -1, -1, {0} },
321 {RSP_ZCTP, 700, 700, -1, -1, -1, {0} },
322 {EV_TIMEOUT, 700, 700, -1, 720, 720, {ACT_ICALL} },
323 {EV_BC_CLOSED, 720, 720, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800324
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000325/*accept icall*/
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000326 {EV_ACCEPT, -1, -1, -1, -1, -1, {ACT_ACCEPT} },
327 {RSP_INIT, 720, 720, SEQ_ACCEPT, 721, 5, {ACT_CMD + AT_PROTO} },
328 {RSP_OK, 721, 721, -1, 722, 5, {ACT_CMD + AT_ISO} },
329 {RSP_OK, 722, 722, -1, 723, 5, {0}, "+VLS=17\r"},
330 {RSP_OK, 723, 723, -1, 724, 5, {0} },
331 {RSP_ZVLS, 724, 724, 17, 750, 50, {ACT_ACCEPTED} },
332 {RSP_ERROR, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
333 {EV_TIMEOUT, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
334 {RSP_ZSAU, 700, 729, ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT} },
335 {RSP_ZSAU, 700, 729, ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT} },
336 {RSP_ZSAU, 700, 729, ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800337
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000338 {EV_BC_OPEN, 750, 750, -1, 751, -1},
339 {EV_TIMEOUT, 750, 751, -1, 0, 0, {ACT_CONNTIMEOUT} },
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800340
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000341/* B channel closed (general case) */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000342 {EV_BC_CLOSED, -1, -1, -1, -1, -1, {ACT_NOTIFY_BC_DOWN} },
Tilman Schmidt73a88812006-04-22 02:35:30 -0700343
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000344/* misc. */
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000345 {RSP_ZCON, -1, -1, -1, -1, -1, {ACT_DEBUG} },
346 {RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
347 {RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
348 {RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
Joe Perches475be4d2012-02-19 19:52:38 -0800349 {RSP_LAST}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800350};
351
352
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000353static const struct resp_type_t {
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000354 char *response;
355 int resp_code;
356 int type;
357}
358resp_type[] =
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800359{
Tilman Schmidt917f5082006-04-10 22:55:00 -0700360 {"OK", RSP_OK, RT_NOTHING},
361 {"ERROR", RSP_ERROR, RT_NOTHING},
362 {"ZSAU", RSP_ZSAU, RT_ZSAU},
363 {"ZCAU", RSP_ZCAU, RT_ZCAU},
364 {"RING", RSP_RING, RT_RING},
365 {"ZGCI", RSP_ZGCI, RT_NUMBER},
366 {"ZVLS", RSP_ZVLS, RT_NUMBER},
367 {"ZCTP", RSP_ZCTP, RT_NUMBER},
368 {"ZDLE", RSP_ZDLE, RT_NUMBER},
Tilman Schmidt917f5082006-04-10 22:55:00 -0700369 {"ZHLC", RSP_ZHLC, RT_STRING},
370 {"ZBC", RSP_ZBC, RT_STRING},
371 {"NMBR", RSP_NMBR, RT_STRING},
372 {"ZCPN", RSP_ZCPN, RT_STRING},
373 {"ZCON", RSP_ZCON, RT_STRING},
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000374 {NULL, 0, 0}
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800375};
376
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000377static const struct zsau_resp_t {
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000378 char *str;
379 int code;
380}
381zsau_resp[] =
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000382{
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000383 {"OUTGOING_CALL_PROCEEDING", ZSAU_PROCEEDING},
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000384 {"CALL_DELIVERED", ZSAU_CALL_DELIVERED},
385 {"ACTIVE", ZSAU_ACTIVE},
386 {"DISCONNECT_IND", ZSAU_DISCONNECT_IND},
387 {"NULL", ZSAU_NULL},
388 {"DISCONNECT_REQ", ZSAU_DISCONNECT_REQ},
389 {NULL, ZSAU_UNKNOWN}
390};
391
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800392/* retrieve CID from parsed response
393 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
394 */
395static int cid_of_response(char *s)
396{
Alexey Dobriyan19eccc22011-03-27 02:58:35 +0000397 int cid;
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000398 int rc;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800399
400 if (s[-1] != ';')
401 return 0; /* no CID separator */
Alexey Dobriyan19eccc22011-03-27 02:58:35 +0000402 rc = kstrtoint(s, 10, &cid);
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000403 if (rc)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800404 return 0; /* CID not numeric */
405 if (cid < 1 || cid > 65535)
406 return -1; /* CID out of range */
407 return cid;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800408}
409
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100410/* queue event with CID */
411static void add_cid_event(struct cardstate *cs, int cid, int type,
412 void *ptr, int parameter)
413{
414 unsigned long flags;
415 unsigned next, tail;
416 struct event_t *event;
417
418 gig_dbg(DEBUG_EVENT, "queueing event %d for cid %d", type, cid);
419
420 spin_lock_irqsave(&cs->ev_lock, flags);
421
422 tail = cs->ev_tail;
423 next = (tail + 1) % MAX_EVENTS;
424 if (unlikely(next == cs->ev_head)) {
425 dev_err(cs->dev, "event queue full\n");
426 kfree(ptr);
427 } else {
428 event = cs->events + tail;
429 event->type = type;
430 event->cid = cid;
431 event->ptr = ptr;
432 event->arg = NULL;
433 event->parameter = parameter;
434 event->at_state = NULL;
435 cs->ev_tail = next;
436 }
437
438 spin_unlock_irqrestore(&cs->ev_lock, flags);
439}
440
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000441/**
442 * gigaset_handle_modem_response() - process received modem response
443 * @cs: device descriptor structure.
444 *
445 * Called by asyncdata/isocdata if a block of data received from the
446 * device must be processed as a modem command response. The data is
447 * already in the cs structure.
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800448 */
449void gigaset_handle_modem_response(struct cardstate *cs)
450{
451 unsigned char *argv[MAX_REC_PARAMS + 1];
452 int params;
453 int i, j;
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100454 char *ptr;
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700455 const struct resp_type_t *rt;
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000456 const struct zsau_resp_t *zr;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800457 int curarg;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800458 int resp_code;
459 int param_type;
460 int abort;
461 size_t len;
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100462 int cid, parameter;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800463 int rawstring;
464
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800465 len = cs->cbytes;
466 if (!len) {
467 /* ignore additional LFs/CRs (M10x config mode or cx100) */
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000468 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[0]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800469 return;
470 }
471 cs->respdata[len] = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800472 argv[0] = cs->respdata;
473 params = 1;
474 if (cs->at_state.getstring) {
475 /* getstring only allowed without cid at the moment */
476 cs->at_state.getstring = 0;
477 rawstring = 1;
478 cid = 0;
479 } else {
480 /* parse line */
481 for (i = 0; i < len; i++)
482 switch (cs->respdata[i]) {
483 case ';':
484 case ',':
485 case '=':
486 if (params > MAX_REC_PARAMS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700487 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800488 "too many parameters in response\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800489 /* need last parameter (might be CID) */
490 params--;
491 }
492 argv[params++] = cs->respdata + i + 1;
493 }
494
495 rawstring = 0;
Joe Perches475be4d2012-02-19 19:52:38 -0800496 cid = params > 1 ? cid_of_response(argv[params - 1]) : 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800497 if (cid < 0) {
498 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700499 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800500 return;
501 }
502
503 for (j = 1; j < params; ++j)
504 argv[j][-1] = 0;
505
Tilman Schmidt1528b182010-02-22 13:09:52 +0000506 gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800507 if (cid) {
508 --params;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000509 gig_dbg(DEBUG_EVENT, "CID: %s", argv[params]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800510 }
Tilman Schmidt1528b182010-02-22 13:09:52 +0000511 gig_dbg(DEBUG_EVENT, "available params: %d", params - 1);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800512 for (j = 1; j < params; j++)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000513 gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800514 }
515
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800516 abort = 1;
517 curarg = 0;
518 while (curarg < params) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800519 if (rawstring) {
520 resp_code = RSP_STRING;
521 param_type = RT_STRING;
522 } else {
523 for (rt = resp_type; rt->response; ++rt)
524 if (!strcmp(argv[curarg], rt->response))
525 break;
526
527 if (!rt->response) {
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100528 add_cid_event(cs, 0, RSP_NONE, NULL, 0);
Tilman Schmidt2ed5e4f2010-07-05 14:18:48 +0000529 gig_dbg(DEBUG_EVENT,
530 "unknown modem response: '%s'\n",
531 argv[curarg]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800532 break;
533 }
534
535 resp_code = rt->resp_code;
536 param_type = rt->type;
537 ++curarg;
538 }
539
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800540 switch (param_type) {
541 case RT_NOTHING:
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100542 add_cid_event(cs, cid, resp_code, NULL, 0);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800543 break;
544 case RT_RING:
545 if (!cid) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700546 dev_err(cs->dev,
547 "received RING without CID!\n");
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100548 add_cid_event(cs, 0, RSP_INVAL, NULL, 0);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800549 abort = 1;
550 } else {
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100551 add_cid_event(cs, 0, resp_code, NULL, cid);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800552 abort = 0;
553 }
554 break;
555 case RT_ZSAU:
556 if (curarg >= params) {
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100557 add_cid_event(cs, cid, resp_code, NULL,
558 ZSAU_NONE);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800559 break;
560 }
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000561 for (zr = zsau_resp; zr->str; ++zr)
562 if (!strcmp(argv[curarg], zr->str))
563 break;
Tilman Schmidt50e4fe92009-10-25 09:30:57 +0000564 if (!zr->str)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700565 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800566 "%s: unknown parameter %s after ZSAU\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700567 __func__, argv[curarg]);
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100568 add_cid_event(cs, cid, resp_code, NULL, zr->code);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800569 ++curarg;
570 break;
571 case RT_STRING:
572 if (curarg < params) {
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100573 ptr = kstrdup(argv[curarg], GFP_ATOMIC);
574 if (!ptr)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700575 dev_err(cs->dev, "out of memory\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800576 ++curarg;
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100577 } else {
578 ptr = NULL;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800579 }
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100580 gig_dbg(DEBUG_EVENT, "string==%s", ptr ? ptr : "NULL");
581 add_cid_event(cs, cid, resp_code, ptr, 0);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800582 break;
583 case RT_ZCAU:
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100584 parameter = -1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800585 if (curarg + 1 < params) {
Alexey Dobriyan19eccc22011-03-27 02:58:35 +0000586 u8 type, value;
Andy Shevchenkoc9741382010-07-15 02:37:20 +0000587
Alexey Dobriyan19eccc22011-03-27 02:58:35 +0000588 i = kstrtou8(argv[curarg++], 16, &type);
589 j = kstrtou8(argv[curarg++], 16, &value);
590 if (i == 0 && j == 0)
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100591 parameter = (type << 8) | value;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800592 } else
593 curarg = params - 1;
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100594 add_cid_event(cs, cid, resp_code, NULL, parameter);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800595 break;
596 case RT_NUMBER:
Alexey Dobriyan19eccc22011-03-27 02:58:35 +0000597 if (curarg >= params ||
Tilman Schmidt6bf50112015-03-21 20:15:32 +0100598 kstrtoint(argv[curarg++], 10, &parameter))
599 parameter = -1;
600 gig_dbg(DEBUG_EVENT, "parameter==%d", parameter);
601 add_cid_event(cs, cid, resp_code, NULL, parameter);
602 if (resp_code == RSP_ZDLE)
603 cs->dle = parameter;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800604 break;
605 }
606
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800607 if (abort)
608 break;
609 }
610
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800611 if (curarg != params)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000612 gig_dbg(DEBUG_EVENT,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700613 "invalid number of processed parameters: %d/%d",
614 curarg, params);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800615}
616EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
617
Tilman Schmidtb8324f92014-10-11 13:46:30 +0200618/* disconnect_nobc
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800619 * process closing of connection associated with given AT state structure
Tilman Schmidtb8324f92014-10-11 13:46:30 +0200620 * without B channel
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800621 */
Tilman Schmidtb8324f92014-10-11 13:46:30 +0200622static void disconnect_nobc(struct at_state_t **at_state_p,
623 struct cardstate *cs)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800624{
625 unsigned long flags;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800626
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700627 spin_lock_irqsave(&cs->lock, flags);
628 ++(*at_state_p)->seq_index;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800629
630 /* revert to selected idle mode */
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700631 if (!cs->cidmode) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800632 cs->at_state.pending_commands |= PC_UMMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000633 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800634 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800635 }
636
Tilman Schmidtb8324f92014-10-11 13:46:30 +0200637 /* check for and deallocate temporary AT state */
638 if (!list_empty(&(*at_state_p)->list)) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800639 list_del(&(*at_state_p)->list);
640 kfree(*at_state_p);
641 *at_state_p = NULL;
Tilman Schmidtb8324f92014-10-11 13:46:30 +0200642 }
643
644 spin_unlock_irqrestore(&cs->lock, flags);
645}
646
647/* disconnect_bc
648 * process closing of connection associated with given AT state structure
649 * and B channel
650 */
651static void disconnect_bc(struct at_state_t *at_state,
652 struct cardstate *cs, struct bc_state *bcs)
653{
654 unsigned long flags;
655
656 spin_lock_irqsave(&cs->lock, flags);
657 ++at_state->seq_index;
658
659 /* revert to selected idle mode */
660 if (!cs->cidmode) {
661 cs->at_state.pending_commands |= PC_UMMODE;
662 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
663 cs->commands_pending = 1;
664 }
665 spin_unlock_irqrestore(&cs->lock, flags);
666
667 /* invoke hardware specific handler */
668 cs->ops->close_bchannel(bcs);
669
670 /* notify LL */
671 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
672 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
673 gigaset_isdn_hupD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800674 }
675}
676
677/* get_free_channel
678 * get a free AT state structure: either one of those associated with the
679 * B channels of the Gigaset device, or if none of those is available,
680 * a newly allocated one with bcs=NULL
Tilman Schmidtb8324f92014-10-11 13:46:30 +0200681 * The structure should be freed by calling disconnect_nobc() after use.
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800682 */
683static inline struct at_state_t *get_free_channel(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700684 int cid)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800685/* cids: >0: siemens-cid
Tilman Schmidtf86936f2012-04-25 13:02:20 +0000686 * 0: without cid
687 * -1: no cid assigned yet
688 */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800689{
690 unsigned long flags;
691 int i;
692 struct at_state_t *ret;
693
694 for (i = 0; i < cs->channels; ++i)
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000695 if (gigaset_get_channel(cs->bcs + i) >= 0) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800696 ret = &cs->bcs[i].at_state;
697 ret->cid = cid;
698 return ret;
699 }
700
701 spin_lock_irqsave(&cs->lock, flags);
702 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
703 if (ret) {
704 gigaset_at_init(ret, NULL, cs, cid);
705 list_add(&ret->list, &cs->temp_at_states);
706 }
707 spin_unlock_irqrestore(&cs->lock, flags);
708 return ret;
709}
710
711static void init_failed(struct cardstate *cs, int mode)
712{
713 int i;
714 struct at_state_t *at_state;
715
716 cs->at_state.pending_commands &= ~PC_INIT;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800717 cs->mode = mode;
718 cs->mstate = MS_UNINITIALIZED;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800719 gigaset_free_channels(cs);
720 for (i = 0; i < cs->channels; ++i) {
721 at_state = &cs->bcs[i].at_state;
722 if (at_state->pending_commands & PC_CID) {
723 at_state->pending_commands &= ~PC_CID;
724 at_state->pending_commands |= PC_NOCID;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800725 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800726 }
727 }
728}
729
730static void schedule_init(struct cardstate *cs, int state)
731{
732 if (cs->at_state.pending_commands & PC_INIT) {
Tilman Schmidt1528b182010-02-22 13:09:52 +0000733 gig_dbg(DEBUG_EVENT, "not scheduling PC_INIT again");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800734 return;
735 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800736 cs->mstate = state;
737 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800738 gigaset_block_channels(cs);
739 cs->at_state.pending_commands |= PC_INIT;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000740 gig_dbg(DEBUG_EVENT, "Scheduling PC_INIT");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800741 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800742}
743
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000744/* send an AT command
745 * adding the "AT" prefix, cid and DLE encapsulation as appropriate
746 */
747static void send_command(struct cardstate *cs, const char *cmd,
748 struct at_state_t *at_state)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800749{
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000750 int cid = at_state->cid;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000751 struct cmdbuf_t *cb;
752 size_t buflen;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800753
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000754 buflen = strlen(cmd) + 12; /* DLE ( A T 1 2 3 4 5 <cmd> DLE ) \0 */
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000755 cb = kmalloc(sizeof(struct cmdbuf_t) + buflen, GFP_ATOMIC);
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000756 if (!cb) {
757 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700758 return;
759 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000760 if (cid > 0 && cid <= 65535)
761 cb->len = snprintf(cb->buf, buflen,
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000762 cs->dle ? "\020(AT%d%s\020)" : "AT%d%s",
Joe Perches475be4d2012-02-19 19:52:38 -0800763 cid, cmd);
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000764 else
765 cb->len = snprintf(cb->buf, buflen,
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000766 cs->dle ? "\020(AT%s\020)" : "AT%s",
Joe Perches475be4d2012-02-19 19:52:38 -0800767 cmd);
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000768 cb->offset = 0;
769 cb->next = NULL;
770 cb->wake_tasklet = NULL;
771 cs->ops->write_cmd(cs, cb);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800772}
773
774static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
775{
776 struct at_state_t *at_state;
777 int i;
778 unsigned long flags;
779
780 if (cid == 0)
781 return &cs->at_state;
782
783 for (i = 0; i < cs->channels; ++i)
784 if (cid == cs->bcs[i].at_state.cid)
785 return &cs->bcs[i].at_state;
786
787 spin_lock_irqsave(&cs->lock, flags);
788
789 list_for_each_entry(at_state, &cs->temp_at_states, list)
790 if (cid == at_state->cid) {
791 spin_unlock_irqrestore(&cs->lock, flags);
792 return at_state;
793 }
794
795 spin_unlock_irqrestore(&cs->lock, flags);
796
797 return NULL;
798}
799
800static void bchannel_down(struct bc_state *bcs)
801{
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800802 if (bcs->chstate & CHS_B_UP) {
803 bcs->chstate &= ~CHS_B_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000804 gigaset_isdn_hupB(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800805 }
806
807 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
808 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000809 gigaset_isdn_hupD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800810 }
811
812 gigaset_free_channel(bcs);
813
814 gigaset_bcs_reinit(bcs);
815}
816
817static void bchannel_up(struct bc_state *bcs)
818{
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800819 if (bcs->chstate & CHS_B_UP) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700820 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
821 __func__);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800822 return;
823 }
824
825 bcs->chstate |= CHS_B_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000826 gigaset_isdn_connB(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800827}
828
Tilman Schmidtc35a87f2009-10-25 09:30:47 +0000829static void start_dial(struct at_state_t *at_state, void *data,
Joe Perches475be4d2012-02-19 19:52:38 -0800830 unsigned seq_index)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800831{
832 struct bc_state *bcs = at_state->bcs;
833 struct cardstate *cs = at_state->cs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000834 char **commands = data;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700835 unsigned long flags;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000836 int i;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800837
838 bcs->chstate |= CHS_NOTIFY_LL;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800839
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700840 spin_lock_irqsave(&cs->lock, flags);
841 if (at_state->seq_index != seq_index) {
842 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800843 goto error;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700844 }
845 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800846
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000847 for (i = 0; i < AT_NUM; ++i) {
848 kfree(bcs->commands[i]);
849 bcs->commands[i] = commands[i];
850 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800851
852 at_state->pending_commands |= PC_CID;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000853 gig_dbg(DEBUG_EVENT, "Scheduling PC_CID");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800854 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800855 return;
856
857error:
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000858 for (i = 0; i < AT_NUM; ++i) {
859 kfree(commands[i]);
860 commands[i] = NULL;
861 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800862 at_state->pending_commands |= PC_NOCID;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000863 gig_dbg(DEBUG_EVENT, "Scheduling PC_NOCID");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800864 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800865 return;
866}
867
868static void start_accept(struct at_state_t *at_state)
869{
870 struct cardstate *cs = at_state->cs;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000871 struct bc_state *bcs = at_state->bcs;
872 int i;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800873
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000874 for (i = 0; i < AT_NUM; ++i) {
875 kfree(bcs->commands[i]);
876 bcs->commands[i] = NULL;
877 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800878
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000879 bcs->commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
880 bcs->commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
881 if (!bcs->commands[AT_PROTO] || !bcs->commands[AT_ISO]) {
882 dev_err(at_state->cs->dev, "out of memory\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800883 /* error reset */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800884 at_state->pending_commands |= PC_HUP;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000885 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800886 cs->commands_pending = 1;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000887 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800888 }
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000889
890 snprintf(bcs->commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
891 snprintf(bcs->commands[AT_ISO], 9, "^SISO=%u\r", bcs->channel + 1);
892
893 at_state->pending_commands |= PC_ACCEPT;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000894 gig_dbg(DEBUG_EVENT, "Scheduling PC_ACCEPT");
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000895 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800896}
897
898static void do_start(struct cardstate *cs)
899{
900 gigaset_free_channels(cs);
901
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800902 if (cs->mstate != MS_LOCKED)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800903 schedule_init(cs, MS_INIT);
904
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700905 cs->isdn_up = 1;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000906 gigaset_isdn_start(cs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800907
908 cs->waiting = 0;
909 wake_up(&cs->waitqueue);
910}
911
912static void finish_shutdown(struct cardstate *cs)
913{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800914 if (cs->mstate != MS_LOCKED) {
915 cs->mstate = MS_UNINITIALIZED;
916 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800917 }
918
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700919 /* Tell the LL that the device is not available .. */
920 if (cs->isdn_up) {
921 cs->isdn_up = 0;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000922 gigaset_isdn_stop(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700923 }
924
Tilman Schmidt63b203b2013-01-21 11:57:21 +0000925 /* The rest is done by cleanup_cs() in process context. */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800926
927 cs->cmd_result = -ENODEV;
928 cs->waiting = 0;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800929 wake_up(&cs->waitqueue);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800930}
931
932static void do_shutdown(struct cardstate *cs)
933{
934 gigaset_block_channels(cs);
935
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800936 if (cs->mstate == MS_READY) {
937 cs->mstate = MS_SHUTDOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800938 cs->at_state.pending_commands |= PC_SHUTDOWN;
Tilman Schmidt1528b182010-02-22 13:09:52 +0000939 gig_dbg(DEBUG_EVENT, "Scheduling PC_SHUTDOWN");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800940 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800941 } else
942 finish_shutdown(cs);
943}
944
945static void do_stop(struct cardstate *cs)
946{
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700947 unsigned long flags;
948
949 spin_lock_irqsave(&cs->lock, flags);
950 cs->connected = 0;
951 spin_unlock_irqrestore(&cs->lock, flags);
952
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800953 do_shutdown(cs);
954}
955
956/* Entering cid mode or getting a cid failed:
957 * try to initialize the device and try again.
958 *
959 * channel >= 0: getting cid for the channel failed
960 * channel < 0: entering cid mode failed
961 *
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000962 * returns 0 on success, <0 on failure
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800963 */
964static int reinit_and_retry(struct cardstate *cs, int channel)
965{
966 int i;
967
968 if (--cs->retry_count <= 0)
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000969 return -EFAULT;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800970
971 for (i = 0; i < cs->channels; ++i)
972 if (cs->bcs[i].at_state.cid > 0)
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000973 return -EBUSY;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800974
975 if (channel < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700976 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800977 "Could not enter cid mode. Reinit device and try again.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800978 else {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700979 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -0800980 "Could not get a call id. Reinit device and try again.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800981 cs->bcs[channel].at_state.pending_commands |= PC_CID;
982 }
983 schedule_init(cs, MS_INIT);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000984 return 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800985}
986
987static int at_state_invalid(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700988 struct at_state_t *test_ptr)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -0800989{
990 unsigned long flags;
991 unsigned channel;
992 struct at_state_t *at_state;
993 int retval = 0;
994
995 spin_lock_irqsave(&cs->lock, flags);
996
997 if (test_ptr == &cs->at_state)
998 goto exit;
999
1000 list_for_each_entry(at_state, &cs->temp_at_states, list)
1001 if (at_state == test_ptr)
1002 goto exit;
1003
1004 for (channel = 0; channel < cs->channels; ++channel)
1005 if (&cs->bcs[channel].at_state == test_ptr)
1006 goto exit;
1007
1008 retval = 1;
1009exit:
1010 spin_unlock_irqrestore(&cs->lock, flags);
1011 return retval;
1012}
1013
1014static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001015 struct at_state_t *at_state)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001016{
1017 int retval;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001018
1019 retval = gigaset_isdn_icall(at_state);
1020 switch (retval) {
1021 case ICALL_ACCEPT:
1022 break;
1023 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001024 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001025 /* --v-- fall through --v-- */
1026 case ICALL_IGNORE:
1027 case ICALL_REJECT:
1028 /* hang up actively
1029 * Device doc says that would reject the call.
1030 * In fact it doesn't.
1031 */
1032 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001033 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001034 break;
1035 }
1036}
1037
1038static int do_lock(struct cardstate *cs)
1039{
1040 int mode;
1041 int i;
1042
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001043 switch (cs->mstate) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001044 case MS_UNINITIALIZED:
1045 case MS_READY:
1046 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1047 cs->at_state.pending_commands)
1048 return -EBUSY;
1049
1050 for (i = 0; i < cs->channels; ++i)
1051 if (cs->bcs[i].at_state.pending_commands)
1052 return -EBUSY;
1053
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00001054 if (gigaset_get_channels(cs) < 0)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001055 return -EBUSY;
1056
1057 break;
1058 case MS_LOCKED:
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001059 break;
1060 default:
1061 return -EBUSY;
1062 }
1063
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001064 mode = cs->mode;
1065 cs->mstate = MS_LOCKED;
1066 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001067
1068 return mode;
1069}
1070
1071static int do_unlock(struct cardstate *cs)
1072{
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001073 if (cs->mstate != MS_LOCKED)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001074 return -EINVAL;
1075
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001076 cs->mstate = MS_UNINITIALIZED;
1077 cs->mode = M_UNKNOWN;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001078 gigaset_free_channels(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001079 if (cs->connected)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001080 schedule_init(cs, MS_INIT);
1081
1082 return 0;
1083}
1084
1085static void do_action(int action, struct cardstate *cs,
1086 struct bc_state *bcs,
1087 struct at_state_t **p_at_state, char **pp_command,
1088 int *p_genresp, int *p_resp_code,
1089 struct event_t *ev)
1090{
1091 struct at_state_t *at_state = *p_at_state;
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001092 struct bc_state *bcs2;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001093 unsigned long flags;
1094
1095 int channel;
1096
1097 unsigned char *s, *e;
1098 int i;
1099 unsigned long val;
1100
1101 switch (action) {
1102 case ACT_NOTHING:
1103 break;
1104 case ACT_TIMEOUT:
1105 at_state->waiting = 1;
1106 break;
1107 case ACT_INIT:
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001108 cs->at_state.pending_commands &= ~PC_INIT;
1109 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001110 cs->mode = M_UNIMODEM;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001111 spin_lock_irqsave(&cs->lock, flags);
1112 if (!cs->cidmode) {
1113 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001114 gigaset_free_channels(cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001115 cs->mstate = MS_READY;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001116 break;
1117 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001118 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001119 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001120 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001121 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001122 break;
1123 case ACT_FAILINIT:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001124 dev_warn(cs->dev, "Could not initialize the device.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001125 cs->dle = 0;
1126 init_failed(cs, M_UNKNOWN);
1127 cs->cur_at_seq = SEQ_NONE;
1128 break;
1129 case ACT_CONFIGMODE:
1130 init_failed(cs, M_CONFIG);
1131 cs->cur_at_seq = SEQ_NONE;
1132 break;
1133 case ACT_SETDLE1:
1134 cs->dle = 1;
1135 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1136 cs->inbuf[0].inputstate &=
1137 ~(INS_command | INS_DLE_command);
1138 break;
1139 case ACT_SETDLE0:
1140 cs->dle = 0;
1141 cs->inbuf[0].inputstate =
1142 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1143 | INS_command;
1144 break;
1145 case ACT_CMODESET:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001146 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001147 gigaset_free_channels(cs);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001148 cs->mstate = MS_READY;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001149 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001150 cs->mode = M_CID;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001151 cs->cur_at_seq = SEQ_NONE;
1152 break;
1153 case ACT_UMODESET:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001154 cs->mode = M_UNIMODEM;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001155 cs->cur_at_seq = SEQ_NONE;
1156 break;
1157 case ACT_FAILCMODE:
1158 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001159 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001160 init_failed(cs, M_UNKNOWN);
1161 break;
1162 }
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00001163 if (reinit_and_retry(cs, -1) < 0)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001164 schedule_init(cs, MS_RECOVER);
1165 break;
1166 case ACT_FAILUMODE:
1167 cs->cur_at_seq = SEQ_NONE;
1168 schedule_init(cs, MS_RECOVER);
1169 break;
1170 case ACT_HUPMODEM:
1171 /* send "+++" (hangup in unimodem mode) */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001172 if (cs->connected) {
1173 struct cmdbuf_t *cb;
1174
1175 cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC);
1176 if (!cb) {
1177 dev_err(cs->dev, "%s: out of memory\n",
1178 __func__);
1179 return;
1180 }
1181 memcpy(cb->buf, "+++", 3);
1182 cb->len = 3;
1183 cb->offset = 0;
1184 cb->next = NULL;
1185 cb->wake_tasklet = NULL;
1186 cs->ops->write_cmd(cs, cb);
1187 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001188 break;
1189 case ACT_RING:
1190 /* get fresh AT state structure for new CID */
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001191 at_state = get_free_channel(cs, ev->parameter);
1192 if (!at_state) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001193 dev_warn(cs->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001194 "RING ignored: could not allocate channel structure\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001195 break;
1196 }
1197
1198 /* initialize AT state structure
1199 * note that bcs may be NULL if no B channel is free
1200 */
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001201 at_state->ConState = 700;
Tilman Schmidt3a0a3a62010-03-14 12:58:05 +00001202 for (i = 0; i < STR_NUM; ++i) {
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001203 kfree(at_state->str_var[i]);
1204 at_state->str_var[i] = NULL;
Tilman Schmidt3a0a3a62010-03-14 12:58:05 +00001205 }
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001206 at_state->int_var[VAR_ZCTP] = -1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001207
1208 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001209 at_state->timer_expires = RING_TIMEOUT;
1210 at_state->timer_active = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001211 spin_unlock_irqrestore(&cs->lock, flags);
1212 break;
1213 case ACT_ICALL:
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001214 handle_icall(cs, bcs, at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001215 break;
1216 case ACT_FAILSDOWN:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001217 dev_warn(cs->dev, "Could not shut down the device.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001218 /* fall through */
1219 case ACT_FAKESDOWN:
1220 case ACT_SDOWN:
1221 cs->cur_at_seq = SEQ_NONE;
1222 finish_shutdown(cs);
1223 break;
1224 case ACT_CONNECT:
1225 if (cs->onechannel) {
1226 at_state->pending_commands |= PC_DLE1;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001227 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001228 break;
1229 }
1230 bcs->chstate |= CHS_D_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001231 gigaset_isdn_connD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001232 cs->ops->init_bchannel(bcs);
1233 break;
1234 case ACT_DLE1:
1235 cs->cur_at_seq = SEQ_NONE;
1236 bcs = cs->bcs + cs->curchannel;
1237
1238 bcs->chstate |= CHS_D_UP;
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001239 gigaset_isdn_connD(bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001240 cs->ops->init_bchannel(bcs);
1241 break;
1242 case ACT_FAKEHUP:
1243 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1244 /* fall through */
1245 case ACT_DISCONNECT:
1246 cs->cur_at_seq = SEQ_NONE;
1247 at_state->cid = -1;
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001248 if (!bcs) {
1249 disconnect_nobc(p_at_state, cs);
1250 } else if (cs->onechannel && cs->dle) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001251 /* Check for other open channels not needed:
1252 * DLE only used for M10x with one B channel.
1253 */
1254 at_state->pending_commands |= PC_DLE0;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001255 cs->commands_pending = 1;
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001256 } else {
1257 disconnect_bc(at_state, cs, bcs);
1258 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001259 break;
1260 case ACT_FAKEDLE0:
1261 at_state->int_var[VAR_ZDLE] = 0;
1262 cs->dle = 0;
1263 /* fall through */
1264 case ACT_DLE0:
1265 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001266 bcs2 = cs->bcs + cs->curchannel;
1267 disconnect_bc(&bcs2->at_state, cs, bcs2);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001268 break;
1269 case ACT_ABORTHUP:
1270 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001271 dev_warn(cs->dev, "Could not hang up.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001272 at_state->cid = -1;
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001273 if (!bcs)
1274 disconnect_nobc(p_at_state, cs);
1275 else if (cs->onechannel)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001276 at_state->pending_commands |= PC_DLE0;
Tilman Schmidt698e3ed2006-06-26 00:25:34 -07001277 else
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001278 disconnect_bc(at_state, cs, bcs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001279 schedule_init(cs, MS_RECOVER);
1280 break;
1281 case ACT_FAILDLE0:
1282 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt5dcd7d82014-10-03 17:03:32 +02001283 dev_warn(cs->dev, "Error leaving DLE mode.\n");
1284 cs->dle = 0;
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001285 bcs2 = cs->bcs + cs->curchannel;
1286 disconnect_bc(&bcs2->at_state, cs, bcs2);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001287 schedule_init(cs, MS_RECOVER);
1288 break;
1289 case ACT_FAILDLE1:
1290 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001291 dev_warn(cs->dev,
1292 "Could not enter DLE mode. Trying to hang up.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001293 channel = cs->curchannel;
1294 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001295 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001296 break;
1297
1298 case ACT_CID: /* got cid; start dialing */
1299 cs->cur_at_seq = SEQ_NONE;
1300 channel = cs->curchannel;
1301 if (ev->parameter > 0 && ev->parameter <= 65535) {
1302 cs->bcs[channel].at_state.cid = ev->parameter;
1303 cs->bcs[channel].at_state.pending_commands |=
1304 PC_DIAL;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001305 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001306 break;
1307 }
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001308 /* bad cid: fall through */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001309 case ACT_FAILCID:
1310 cs->cur_at_seq = SEQ_NONE;
1311 channel = cs->curchannel;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +00001312 if (reinit_and_retry(cs, channel) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001313 dev_warn(cs->dev,
1314 "Could not get a call ID. Cannot dial.\n");
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001315 bcs2 = cs->bcs + channel;
1316 disconnect_bc(&bcs2->at_state, cs, bcs2);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001317 }
1318 break;
1319 case ACT_ABORTCID:
1320 cs->cur_at_seq = SEQ_NONE;
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001321 bcs2 = cs->bcs + cs->curchannel;
1322 disconnect_bc(&bcs2->at_state, cs, bcs2);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001323 break;
1324
1325 case ACT_DIALING:
1326 case ACT_ACCEPTED:
1327 cs->cur_at_seq = SEQ_NONE;
1328 break;
1329
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001330 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL procssng */
Tilman Schmidtb8324f92014-10-11 13:46:30 +02001331 if (bcs)
1332 disconnect_bc(at_state, cs, bcs);
1333 else
1334 disconnect_nobc(p_at_state, cs);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001335 break;
1336
1337 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1338 cs->cur_at_seq = SEQ_NONE;
1339 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001340 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001341 break;
1342
1343 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1344 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1345 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1346 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001347 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001348 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001349 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
Tilman Schmidt784d5852006-04-10 22:55:04 -07001350 are not handled properly anymore */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001351 at_state->getstring = 1;
1352 break;
1353 case ACT_SETVER:
1354 if (!ev->ptr) {
1355 *p_genresp = 1;
1356 *p_resp_code = RSP_ERROR;
1357 break;
1358 }
1359 s = ev->ptr;
1360
1361 if (!strcmp(s, "OK")) {
Tilman Schmidte055d032012-04-25 13:02:20 +00001362 /* OK without version string: assume old response */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001363 *p_genresp = 1;
Tilman Schmidte055d032012-04-25 13:02:20 +00001364 *p_resp_code = RSP_NONE;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001365 break;
1366 }
1367
1368 for (i = 0; i < 4; ++i) {
1369 val = simple_strtoul(s, (char **) &e, 10);
1370 if (val > INT_MAX || e == s)
1371 break;
1372 if (i == 3) {
1373 if (*e)
1374 break;
1375 } else if (*e != '.')
1376 break;
1377 else
1378 s = e + 1;
1379 cs->fwver[i] = val;
1380 }
1381 if (i != 4) {
1382 *p_genresp = 1;
1383 *p_resp_code = RSP_ERROR;
1384 break;
1385 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001386 cs->gotfwver = 0;
1387 break;
1388 case ACT_GOTVER:
1389 if (cs->gotfwver == 0) {
1390 cs->gotfwver = 1;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001391 gig_dbg(DEBUG_EVENT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001392 "firmware version %02d.%03d.%02d.%02d",
1393 cs->fwver[0], cs->fwver[1],
1394 cs->fwver[2], cs->fwver[3]);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001395 break;
1396 }
1397 /* fall through */
1398 case ACT_FAILVER:
1399 cs->gotfwver = -1;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001400 dev_err(cs->dev, "could not read firmware version.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001401 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001402 case ACT_ERROR:
Tilman Schmidt05eae94f2009-10-06 12:18:46 +00001403 gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d",
1404 __func__, at_state->ConState);
1405 cs->cur_at_seq = SEQ_NONE;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001406 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001407 case ACT_DEBUG:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001408 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001409 __func__, ev->type, at_state->ConState);
1410 break;
1411 case ACT_WARN:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001412 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1413 __func__, ev->type, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001414 break;
1415 case ACT_ZCAU:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001416 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1417 ev->parameter, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001418 break;
1419
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001420 /* events from the LL */
1421
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001422 case ACT_DIAL:
Tilman Schmidt846ac302014-10-11 13:46:29 +02001423 if (!ev->ptr) {
1424 *p_genresp = 1;
1425 *p_resp_code = RSP_ERROR;
1426 break;
1427 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001428 start_dial(at_state, ev->ptr, ev->parameter);
1429 break;
1430 case ACT_ACCEPT:
1431 start_accept(at_state);
1432 break;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001433 case ACT_HUP:
1434 at_state->pending_commands |= PC_HUP;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001435 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001436 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001437 break;
1438
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001439 /* hotplug events */
1440
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001441 case ACT_STOP:
1442 do_stop(cs);
1443 break;
1444 case ACT_START:
1445 do_start(cs);
1446 break;
1447
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001448 /* events from the interface */
1449
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001450 case ACT_IF_LOCK:
1451 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1452 cs->waiting = 0;
1453 wake_up(&cs->waitqueue);
1454 break;
1455 case ACT_IF_VER:
1456 if (ev->parameter != 0)
1457 cs->cmd_result = -EINVAL;
1458 else if (cs->gotfwver != 1) {
1459 cs->cmd_result = -ENOENT;
1460 } else {
1461 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1462 cs->cmd_result = 0;
1463 }
1464 cs->waiting = 0;
1465 wake_up(&cs->waitqueue);
1466 break;
1467
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001468 /* events from the proc file system */
1469
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001470 case ACT_PROC_CIDMODE:
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001471 spin_lock_irqsave(&cs->lock, flags);
1472 if (ev->parameter != cs->cidmode) {
1473 cs->cidmode = ev->parameter;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001474 if (ev->parameter) {
1475 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001476 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001477 } else {
1478 cs->at_state.pending_commands |= PC_UMMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001479 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001480 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001481 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001482 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001483 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001484 cs->waiting = 0;
1485 wake_up(&cs->waitqueue);
1486 break;
1487
Tilman Schmidtf86936f2012-04-25 13:02:20 +00001488 /* events from the hardware drivers */
1489
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001490 case ACT_NOTIFY_BC_DOWN:
1491 bchannel_down(bcs);
1492 break;
1493 case ACT_NOTIFY_BC_UP:
1494 bchannel_up(bcs);
1495 break;
1496 case ACT_SHUTDOWN:
1497 do_shutdown(cs);
1498 break;
1499
1500
1501 default:
1502 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1503 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1504 if (!*pp_command) {
1505 *p_genresp = 1;
1506 *p_resp_code = RSP_NULL;
1507 }
1508 } else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001509 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001510 }
1511}
1512
1513/* State machine to do the calling and hangup procedure */
1514static void process_event(struct cardstate *cs, struct event_t *ev)
1515{
1516 struct bc_state *bcs;
1517 char *p_command = NULL;
1518 struct reply_t *rep;
1519 int rcode;
1520 int genresp = 0;
1521 int resp_code = RSP_ERROR;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001522 struct at_state_t *at_state;
1523 int index;
1524 int curact;
1525 unsigned long flags;
1526
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001527 if (ev->cid >= 0) {
1528 at_state = at_state_from_cid(cs, ev->cid);
1529 if (!at_state) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001530 gig_dbg(DEBUG_EVENT, "event %d for invalid cid %d",
1531 ev->type, ev->cid);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001532 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001533 NULL, 0, NULL);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001534 return;
1535 }
1536 } else {
1537 at_state = ev->at_state;
1538 if (at_state_invalid(cs, at_state)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001539 gig_dbg(DEBUG_EVENT, "event for invalid at_state %p",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001540 at_state);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001541 return;
1542 }
1543 }
1544
Tilman Schmidt1528b182010-02-22 13:09:52 +00001545 gig_dbg(DEBUG_EVENT, "connection state %d, event %d",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001546 at_state->ConState, ev->type);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001547
1548 bcs = at_state->bcs;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001549
1550 /* Setting the pointer to the dial array */
1551 rep = at_state->replystruct;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001552
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001553 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001554 if (ev->type == EV_TIMEOUT) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001555 if (ev->parameter != at_state->timer_index
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001556 || !at_state->timer_active) {
1557 ev->type = RSP_NONE; /* old timeout */
Tilman Schmidt1528b182010-02-22 13:09:52 +00001558 gig_dbg(DEBUG_EVENT, "old timeout");
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001559 } else {
1560 if (at_state->waiting)
1561 gig_dbg(DEBUG_EVENT, "stopped waiting");
1562 else
1563 gig_dbg(DEBUG_EVENT, "timeout occurred");
1564 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001565 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001566 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001567
Tilman Schmidt917f5082006-04-10 22:55:00 -07001568 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1569 or at_state->str_var[STR_XXXX], set it */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001570 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1571 index = ev->type - RSP_VAR;
1572 at_state->int_var[index] = ev->parameter;
1573 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1574 index = ev->type - RSP_STR;
1575 kfree(at_state->str_var[index]);
1576 at_state->str_var[index] = ev->ptr;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001577 ev->ptr = NULL; /* prevent process_events() from
1578 deallocating ptr */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001579 }
1580
1581 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1582 at_state->getstring = 0;
1583
Tilman Schmidt917f5082006-04-10 22:55:00 -07001584 /* Search row in dial array which matches modem response and current
1585 constate */
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001586 for (;; rep++) {
1587 rcode = rep->resp_code;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001588 if (rcode == RSP_LAST) {
1589 /* found nothing...*/
Tilman Schmidt784d5852006-04-10 22:55:04 -07001590 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
Joe Perches475be4d2012-02-19 19:52:38 -08001591 "resp_code %d in ConState %d!\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001592 __func__, ev->type, at_state->ConState);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001593 return;
1594 }
1595 if ((rcode == RSP_ANY || rcode == ev->type)
Joe Perches475be4d2012-02-19 19:52:38 -08001596 && ((int) at_state->ConState >= rep->min_ConState)
1597 && (rep->max_ConState < 0
1598 || (int) at_state->ConState <= rep->max_ConState)
1599 && (rep->parameter < 0 || rep->parameter == ev->parameter))
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001600 break;
1601 }
1602
1603 p_command = rep->command;
1604
1605 at_state->waiting = 0;
1606 for (curact = 0; curact < MAXACT; ++curact) {
1607 /* The row tells us what we should do ..
1608 */
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001609 do_action(rep->action[curact], cs, bcs, &at_state, &p_command,
1610 &genresp, &resp_code, ev);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001611 if (!at_state)
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001612 /* at_state destroyed by disconnect */
1613 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001614 }
1615
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001616 /* Jump to the next con-state regarding the array */
1617 if (rep->new_ConState >= 0)
1618 at_state->ConState = rep->new_ConState;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001619
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001620 if (genresp) {
1621 spin_lock_irqsave(&cs->lock, flags);
1622 at_state->timer_expires = 0;
1623 at_state->timer_active = 0;
1624 spin_unlock_irqrestore(&cs->lock, flags);
1625 gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1626 } else {
1627 /* Send command to modem if not NULL... */
1628 if (p_command) {
1629 if (cs->connected)
1630 send_command(cs, p_command, at_state);
1631 else
1632 gigaset_add_event(cs, at_state, RSP_NODEV,
1633 NULL, 0, NULL);
1634 }
1635
1636 spin_lock_irqsave(&cs->lock, flags);
1637 if (!rep->timeout) {
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001638 at_state->timer_expires = 0;
1639 at_state->timer_active = 0;
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001640 } else if (rep->timeout > 0) { /* new timeout */
1641 at_state->timer_expires = rep->timeout * 10;
1642 at_state->timer_active = 1;
1643 ++at_state->timer_index;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001644 }
Tilman Schmidt63b203b2013-01-21 11:57:21 +00001645 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001646 }
1647}
1648
1649static void schedule_sequence(struct cardstate *cs,
1650 struct at_state_t *at_state, int sequence)
1651{
1652 cs->cur_at_seq = sequence;
1653 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1654}
1655
1656static void process_command_flags(struct cardstate *cs)
1657{
1658 struct at_state_t *at_state = NULL;
1659 struct bc_state *bcs;
1660 int i;
1661 int sequence;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001662 unsigned long flags;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001663
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001664 cs->commands_pending = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001665
1666 if (cs->cur_at_seq) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001667 gig_dbg(DEBUG_EVENT, "not searching scheduled commands: busy");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001668 return;
1669 }
1670
Tilman Schmidt1528b182010-02-22 13:09:52 +00001671 gig_dbg(DEBUG_EVENT, "searching scheduled commands");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001672
1673 sequence = SEQ_NONE;
1674
1675 /* clear pending_commands and hangup channels on shutdown */
1676 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1677 cs->at_state.pending_commands &= ~PC_CIDMODE;
1678 for (i = 0; i < cs->channels; ++i) {
1679 bcs = cs->bcs + i;
1680 at_state = &bcs->at_state;
1681 at_state->pending_commands &=
1682 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1683 if (at_state->cid > 0)
1684 at_state->pending_commands |= PC_HUP;
1685 if (at_state->pending_commands & PC_CID) {
1686 at_state->pending_commands |= PC_NOCID;
1687 at_state->pending_commands &= ~PC_CID;
1688 }
1689 }
1690 }
1691
1692 /* clear pending_commands and hangup channels on reset */
1693 if (cs->at_state.pending_commands & PC_INIT) {
1694 cs->at_state.pending_commands &= ~PC_CIDMODE;
1695 for (i = 0; i < cs->channels; ++i) {
1696 bcs = cs->bcs + i;
1697 at_state = &bcs->at_state;
1698 at_state->pending_commands &=
1699 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1700 if (at_state->cid > 0)
1701 at_state->pending_commands |= PC_HUP;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001702 if (cs->mstate == MS_RECOVER) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001703 if (at_state->pending_commands & PC_CID) {
1704 at_state->pending_commands |= PC_NOCID;
1705 at_state->pending_commands &= ~PC_CID;
1706 }
1707 }
1708 }
1709 }
1710
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001711 /* only switch back to unimodem mode if no commands are pending and
1712 * no channels are up */
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001713 spin_lock_irqsave(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001714 if (cs->at_state.pending_commands == PC_UMMODE
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001715 && !cs->cidmode
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001716 && list_empty(&cs->temp_at_states)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001717 && cs->mode == M_CID) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001718 sequence = SEQ_UMMODE;
1719 at_state = &cs->at_state;
1720 for (i = 0; i < cs->channels; ++i) {
1721 bcs = cs->bcs + i;
1722 if (bcs->at_state.pending_commands ||
1723 bcs->at_state.cid > 0) {
1724 sequence = SEQ_NONE;
1725 break;
1726 }
1727 }
1728 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001729 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001730 cs->at_state.pending_commands &= ~PC_UMMODE;
1731 if (sequence != SEQ_NONE) {
1732 schedule_sequence(cs, at_state, sequence);
1733 return;
1734 }
1735
1736 for (i = 0; i < cs->channels; ++i) {
1737 bcs = cs->bcs + i;
1738 if (bcs->at_state.pending_commands & PC_HUP) {
Tilman Schmidtd2ca8482013-01-21 11:57:20 +00001739 if (cs->dle) {
1740 cs->curchannel = bcs->channel;
1741 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1742 return;
1743 }
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001744 bcs->at_state.pending_commands &= ~PC_HUP;
1745 if (bcs->at_state.pending_commands & PC_CID) {
1746 /* not yet dialing: PC_NOCID is sufficient */
1747 bcs->at_state.pending_commands |= PC_NOCID;
1748 bcs->at_state.pending_commands &= ~PC_CID;
1749 } else {
1750 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1751 return;
1752 }
1753 }
1754 if (bcs->at_state.pending_commands & PC_NOCID) {
1755 bcs->at_state.pending_commands &= ~PC_NOCID;
1756 cs->curchannel = bcs->channel;
1757 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1758 return;
1759 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1760 bcs->at_state.pending_commands &= ~PC_DLE0;
1761 cs->curchannel = bcs->channel;
1762 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1763 return;
1764 }
1765 }
1766
1767 list_for_each_entry(at_state, &cs->temp_at_states, list)
1768 if (at_state->pending_commands & PC_HUP) {
1769 at_state->pending_commands &= ~PC_HUP;
1770 schedule_sequence(cs, at_state, SEQ_HUP);
1771 return;
1772 }
1773
1774 if (cs->at_state.pending_commands & PC_INIT) {
1775 cs->at_state.pending_commands &= ~PC_INIT;
Tilman Schmidtc35a87f2009-10-25 09:30:47 +00001776 cs->dle = 0;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001777 cs->inbuf->inputstate = INS_command;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001778 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1779 return;
1780 }
1781 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1782 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1783 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1784 return;
1785 }
1786 if (cs->at_state.pending_commands & PC_CIDMODE) {
1787 cs->at_state.pending_commands &= ~PC_CIDMODE;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001788 if (cs->mode == M_UNIMODEM) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001789 cs->retry_count = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001790 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1791 return;
1792 }
1793 }
1794
1795 for (i = 0; i < cs->channels; ++i) {
1796 bcs = cs->bcs + i;
1797 if (bcs->at_state.pending_commands & PC_DLE1) {
1798 bcs->at_state.pending_commands &= ~PC_DLE1;
1799 cs->curchannel = bcs->channel;
1800 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1801 return;
1802 }
1803 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1804 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1805 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1806 return;
1807 }
1808 if (bcs->at_state.pending_commands & PC_DIAL) {
1809 bcs->at_state.pending_commands &= ~PC_DIAL;
1810 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1811 return;
1812 }
1813 if (bcs->at_state.pending_commands & PC_CID) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001814 switch (cs->mode) {
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001815 case M_UNIMODEM:
1816 cs->at_state.pending_commands |= PC_CIDMODE;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001817 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001818 cs->commands_pending = 1;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001819 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001820 case M_UNKNOWN:
1821 schedule_init(cs, MS_INIT);
1822 return;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001823 }
1824 bcs->at_state.pending_commands &= ~PC_CID;
1825 cs->curchannel = bcs->channel;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001826 cs->retry_count = 2;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001827 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1828 return;
1829 }
1830 }
1831}
1832
1833static void process_events(struct cardstate *cs)
1834{
1835 struct event_t *ev;
1836 unsigned head, tail;
1837 int i;
1838 int check_flags = 0;
1839 int was_busy;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001840 unsigned long flags;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001841
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001842 spin_lock_irqsave(&cs->ev_lock, flags);
1843 head = cs->ev_head;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001844
1845 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001846 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001847 if (tail == head) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001848 if (!check_flags && !cs->commands_pending)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001849 break;
1850 check_flags = 0;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001851 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001852 process_command_flags(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001853 spin_lock_irqsave(&cs->ev_lock, flags);
1854 tail = cs->ev_tail;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001855 if (tail == head) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001856 if (!cs->commands_pending)
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001857 break;
1858 continue;
1859 }
1860 }
1861
1862 ev = cs->events + head;
1863 was_busy = cs->cur_at_seq != SEQ_NONE;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001864 spin_unlock_irqrestore(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001865 process_event(cs, ev);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001866 spin_lock_irqsave(&cs->ev_lock, flags);
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001867 kfree(ev->ptr);
1868 ev->ptr = NULL;
1869 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1870 check_flags = 1;
1871
1872 head = (head + 1) % MAX_EVENTS;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001873 cs->ev_head = head;
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001874 }
1875
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001876 spin_unlock_irqrestore(&cs->ev_lock, flags);
1877
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001878 if (i == 2 * MAX_EVENTS) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001879 dev_err(cs->dev,
1880 "infinite loop in process_events; aborting.\n");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001881 }
1882}
1883
1884/* tasklet scheduled on any event received from the Gigaset device
1885 * parameter:
1886 * data ISDN controller state structure
1887 */
1888void gigaset_handle_event(unsigned long data)
1889{
1890 struct cardstate *cs = (struct cardstate *) data;
1891
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001892 /* handle incoming data on control/common channel */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001893 if (cs->inbuf->head != cs->inbuf->tail) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001894 gig_dbg(DEBUG_INTR, "processing new data");
Hansjoerg Lipp14fa73a2006-03-26 01:38:30 -08001895 cs->ops->handle_input(cs->inbuf);
1896 }
1897
1898 process_events(cs);
1899}