2 * Stuff used by all variants of the driver
4 * Copyright (c) 2001 by Stefan Eilers,
5 * Hansjoerg Lipp <hjlipp@web.de>,
6 * Tilman Schmidt <tilman@imap.cc>.
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 * =====================================================================
18 /* ========================================================== */
19 /* bit masks for pending commands */
25 #define PC_SHUTDOWN 0x020
26 #define PC_ACCEPT 0x040
28 #define PC_NOCID 0x100
29 #define PC_CIDMODE 0x200
30 #define PC_UMMODE 0x400
32 /* types of modem responses */
41 /* Possible ASCII responses */
44 //#define RSP_CONNECT 2
56 #define RSP_ZABINFO 14
57 #define RSP_ZSMLSTCHG 15
59 #define RSP_ZSAU (RSP_VAR + VAR_ZSAU)
60 #define RSP_ZDLE (RSP_VAR + VAR_ZDLE)
61 #define RSP_ZVLS (RSP_VAR + VAR_ZVLS)
62 #define RSP_ZCTP (RSP_VAR + VAR_ZCTP)
63 #define RSP_STR (RSP_VAR + VAR_NUM)
64 #define RSP_NMBR (RSP_STR + STR_NMBR)
65 #define RSP_ZCPN (RSP_STR + STR_ZCPN)
66 #define RSP_ZCON (RSP_STR + STR_ZCON)
67 #define RSP_ZBC (RSP_STR + STR_ZBC)
68 #define RSP_ZHLC (RSP_STR + STR_ZHLC)
69 #define RSP_ERROR -1 /* ERROR */
70 #define RSP_WRONG_CID -2 /* unknown cid in cmd */
71 //#define RSP_EMPTY -3
72 #define RSP_UNKNOWN -4 /* unknown response */
73 #define RSP_FAIL -5 /* internal error */
74 #define RSP_INVAL -6 /* invalid response */
77 #define RSP_STRING -20
79 //#define RSP_RETRYFAIL -22
80 //#define RSP_RETRY -23
81 //#define RSP_SKIP -24
87 /* actions for process_response */
91 #define ACT_FAILINIT 3
92 #define ACT_HUPMODEM 4
93 #define ACT_CONFIGMODE 5
97 #define ACT_FAILDLE0 9
98 #define ACT_FAILDLE1 10
101 #define ACT_FAILCID 13
103 #define ACT_FAILSDOWN 15
106 #define ACT_DIALING 18
107 #define ACT_ABORTDIAL 19
108 #define ACT_DISCONNECT 20
109 #define ACT_CONNECT 21
110 #define ACT_REMOTEREJECT 22
111 #define ACT_CONNTIMEOUT 23
112 #define ACT_REMOTEHUP 24
113 #define ACT_ABORTHUP 25
115 #define ACT_ACCEPTED 27
116 #define ACT_ABORTACCEPT 28
117 #define ACT_TIMEOUT 29
118 #define ACT_GETSTRING 30
119 #define ACT_SETVER 31
120 #define ACT_FAILVER 32
121 #define ACT_GOTVER 33
124 #define ACT_ABORTCID 36
126 #define ACT_NOTIFY_BC_DOWN 38
127 #define ACT_NOTIFY_BC_UP 39
129 #define ACT_ACCEPT 41
130 #define ACT_PROTO_L2 42
132 #define ACT_IF_LOCK 44
135 #define ACT_FAKEDLE0 47
136 #define ACT_FAKEHUP 48
137 #define ACT_FAKESDOWN 49
138 #define ACT_SHUTDOWN 50
139 #define ACT_PROC_CIDMODE 51
140 #define ACT_UMODESET 52
141 #define ACT_FAILUMODE 53
142 #define ACT_CMODESET 54
143 #define ACT_FAILCMODE 55
144 #define ACT_IF_VER 56
147 /* at command sequences */
153 #define SEQ_NOCID 350
156 #define SEQ_ACCEPT 720
157 #define SEQ_SHUTDOWN 500
158 #define SEQ_CIDMODE 10
159 #define SEQ_UMMODE 11
162 // 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid), 400: hup, 500: reset, 600: dial, 700: ring
163 struct reply_t gigaset_tab_nocid_m10x[]= /* with dle mode */
165 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
167 /* initialize device, set cid mode if possible */
168 //{RSP_INIT, -1, -1,100, 900, 0, {ACT_TEST}},
169 //{RSP_ERROR, 900,900, -1, 0, 0, {ACT_FAILINIT}},
170 //{RSP_OK, 900,900, -1, 100, INIT_TIMEOUT,
173 {RSP_INIT, -1, -1,SEQ_INIT, 100, INIT_TIMEOUT,
174 {ACT_TIMEOUT}}, /* wait until device is ready */
176 {EV_TIMEOUT, 100,100, -1, 101, 3, {0}, "Z\r"}, /* device in transparent mode? try to initialize it. */
177 {RSP_OK, 101,103, -1, 120, 5, {ACT_GETSTRING}, "+GMR\r"}, /* get version */
179 {EV_TIMEOUT, 101,101, -1, 102, 5, {0}, "Z\r"}, /* timeout => try once again. */
180 {RSP_ERROR, 101,101, -1, 102, 5, {0}, "Z\r"}, /* error => try once again. */
182 {EV_TIMEOUT, 102,102, -1, 108, 5, {ACT_SETDLE1}, "^SDLE=0\r"}, /* timeout => try again in DLE mode. */
183 {RSP_OK, 108,108, -1, 104,-1},
184 {RSP_ZDLE, 104,104, 0, 103, 5, {0}, "Z\r"},
185 {EV_TIMEOUT, 104,104, -1, 0, 0, {ACT_FAILINIT}},
186 {RSP_ERROR, 108,108, -1, 0, 0, {ACT_FAILINIT}},
188 {EV_TIMEOUT, 108,108, -1, 105, 2, {ACT_SETDLE0,
190 ACT_TIMEOUT}}, /* still timeout => connection in unimodem mode? */
191 {EV_TIMEOUT, 105,105, -1, 103, 5, {0}, "Z\r"},
193 {RSP_ERROR, 102,102, -1, 107, 5, {0}, "^GETPRE\r"}, /* ERROR on ATZ => maybe in config mode? */
194 {RSP_OK, 107,107, -1, 0, 0, {ACT_CONFIGMODE}},
195 {RSP_ERROR, 107,107, -1, 0, 0, {ACT_FAILINIT}},
196 {EV_TIMEOUT, 107,107, -1, 0, 0, {ACT_FAILINIT}},
198 {RSP_ERROR, 103,103, -1, 0, 0, {ACT_FAILINIT}},
199 {EV_TIMEOUT, 103,103, -1, 0, 0, {ACT_FAILINIT}},
201 {RSP_STRING, 120,120, -1, 121,-1, {ACT_SETVER}},
203 {EV_TIMEOUT, 120,121, -1, 0, 0, {ACT_FAILVER, ACT_INIT}},
204 {RSP_ERROR, 120,121, -1, 0, 0, {ACT_FAILVER, ACT_INIT}},
205 {RSP_OK, 121,121, -1, 0, 0, {ACT_GOTVER, ACT_INIT}},
207 {EV_TIMEOUT, 120,121, -1, 130, 5, {ACT_FAILVER}, "^SGCI=1\r"},
208 {RSP_ERROR, 120,121, -1, 130, 5, {ACT_FAILVER}, "^SGCI=1\r"},
209 {RSP_OK, 121,121, -1, 130, 5, {ACT_GOTVER}, "^SGCI=1\r"},
211 {RSP_OK, 130,130, -1, 0, 0, {ACT_INIT}},
212 {RSP_ERROR, 130,130, -1, 0, 0, {ACT_FAILINIT}},
213 {EV_TIMEOUT, 130,130, -1, 0, 0, {ACT_FAILINIT}},
217 {RSP_INIT, 0, 0,SEQ_DLE0, 201, 5, {0}, "^SDLE=0\r"},
218 {RSP_OK, 201,201, -1, 202,-1},
219 //{RSP_ZDLE, 202,202, 0, 202, 0, {ACT_ERROR}},//DELETE
220 {RSP_ZDLE, 202,202, 0, 0, 0, {ACT_DLE0}},
221 {RSP_NODEV, 200,249, -1, 0, 0, {ACT_FAKEDLE0}},
222 {RSP_ERROR, 200,249, -1, 0, 0, {ACT_FAILDLE0}},
223 {EV_TIMEOUT, 200,249, -1, 0, 0, {ACT_FAILDLE0}},
226 {RSP_INIT, 0, 0,SEQ_DLE1, 251, 5, {0}, "^SDLE=1\r"},
227 {RSP_OK, 251,251, -1, 252,-1},
228 {RSP_ZDLE, 252,252, 1, 0, 0, {ACT_DLE1}},
229 {RSP_ERROR, 250,299, -1, 0, 0, {ACT_FAILDLE1}},
230 {EV_TIMEOUT, 250,299, -1, 0, 0, {ACT_FAILDLE1}},
233 {RSP_RING, -1, -1, -1, -1,-1, {ACT_RING}},
236 //{RSP_INIT, 0, 0,300, 901, 0, {ACT_TEST}},
237 //{RSP_ERROR, 901,901, -1, 0, 0, {ACT_FAILCID}},
238 //{RSP_OK, 901,901, -1, 301, 5, {0}, "^SGCI?\r"},
240 {RSP_INIT, 0, 0,SEQ_CID, 301, 5, {0}, "^SGCI?\r"},
241 {RSP_OK, 301,301, -1, 302,-1},
242 {RSP_ZGCI, 302,302, -1, 0, 0, {ACT_CID}},
243 {RSP_ERROR, 301,349, -1, 0, 0, {ACT_FAILCID}},
244 {EV_TIMEOUT, 301,349, -1, 0, 0, {ACT_FAILCID}},
247 {RSP_INIT, 0, 0,SEQ_CIDMODE, 150, 5, {0}, "^SGCI=1\r"},
248 {RSP_OK, 150,150, -1, 0, 0, {ACT_CMODESET}},
249 {RSP_ERROR, 150,150, -1, 0, 0, {ACT_FAILCMODE}},
250 {EV_TIMEOUT, 150,150, -1, 0, 0, {ACT_FAILCMODE}},
253 //{RSP_INIT, 0, 0,SEQ_UMMODE, 160, 5, {0}, "^SGCI=0\r"},
254 {RSP_INIT, 0, 0,SEQ_UMMODE, 160, 5, {0}, "Z\r"},
255 {RSP_OK, 160,160, -1, 0, 0, {ACT_UMODESET}},
256 {RSP_ERROR, 160,160, -1, 0, 0, {ACT_FAILUMODE}},
257 {EV_TIMEOUT, 160,160, -1, 0, 0, {ACT_FAILUMODE}},
259 /* abort getting cid */
260 {RSP_INIT, 0, 0,SEQ_NOCID, 0, 0, {ACT_ABORTCID}},
264 {RSP_INIT, 0, 0,SEQ_SHUTDOWN, 503, 5, {0}, "^SGCI=0\r"},
265 {RSP_OK, 503,503, -1, 504, 5, {0}, "Z\r"},
267 {RSP_INIT, 0, 0,SEQ_SHUTDOWN, 504, 5, {0}, "Z\r"},
268 {RSP_OK, 504,504, -1, 0, 0, {ACT_SDOWN}},
269 {RSP_ERROR, 501,599, -1, 0, 0, {ACT_FAILSDOWN}},
270 {EV_TIMEOUT, 501,599, -1, 0, 0, {ACT_FAILSDOWN}},
271 {RSP_NODEV, 501,599, -1, 0, 0, {ACT_FAKESDOWN}},
273 {EV_PROC_CIDMODE,-1, -1, -1, -1,-1, {ACT_PROC_CIDMODE}}, //FIXME
274 {EV_IF_LOCK, -1, -1, -1, -1,-1, {ACT_IF_LOCK}}, //FIXME
275 {EV_IF_VER, -1, -1, -1, -1,-1, {ACT_IF_VER}}, //FIXME
276 {EV_START, -1, -1, -1, -1,-1, {ACT_START}}, //FIXME
277 {EV_STOP, -1, -1, -1, -1,-1, {ACT_STOP}}, //FIXME
278 {EV_SHUTDOWN, -1, -1, -1, -1,-1, {ACT_SHUTDOWN}}, //FIXME
281 {RSP_EMPTY, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
282 {RSP_ZCFGT, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
283 {RSP_ZCFG, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
284 {RSP_ZLOG, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
285 {RSP_ZMWI, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
286 {RSP_ZABINFO, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
287 {RSP_ZSMLSTCHG,-1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
289 {RSP_ZCAU, -1, -1, -1, -1,-1, {ACT_ZCAU}},
290 {RSP_NONE, -1, -1, -1, -1,-1, {ACT_DEBUG}},
291 {RSP_ANY, -1, -1, -1, -1,-1, {ACT_WARN}},
295 // 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring, 400: hup, 750: accepted icall
296 struct reply_t gigaset_tab_cid_m10x[] = /* for M10x */
298 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
301 {EV_DIAL, -1, -1, -1, -1,-1, {ACT_DIAL}}, //FIXME
302 {RSP_INIT, 0, 0,SEQ_DIAL, 601, 5, {ACT_CMD+AT_BC}},
303 {RSP_OK, 601,601, -1, 602, 5, {ACT_CMD+AT_HLC}},
304 {RSP_NULL, 602,602, -1, 603, 5, {ACT_CMD+AT_PROTO}},
305 {RSP_OK, 602,602, -1, 603, 5, {ACT_CMD+AT_PROTO}},
306 {RSP_OK, 603,603, -1, 604, 5, {ACT_CMD+AT_TYPE}},
307 {RSP_OK, 604,604, -1, 605, 5, {ACT_CMD+AT_MSN}},
308 {RSP_OK, 605,605, -1, 606, 5, {ACT_CMD+AT_ISO}},
309 {RSP_NULL, 605,605, -1, 606, 5, {ACT_CMD+AT_ISO}},
310 {RSP_OK, 606,606, -1, 607, 5, {0}, "+VLS=17\r"}, /* set "Endgeraetemodus" */
311 {RSP_OK, 607,607, -1, 608,-1},
312 //{RSP_ZSAU, 608,608,ZSAU_PROCEEDING, 608, 0, {ACT_ERROR}},//DELETE
313 {RSP_ZSAU, 608,608,ZSAU_PROCEEDING, 609, 5, {ACT_CMD+AT_DIAL}},
314 {RSP_OK, 609,609, -1, 650, 0, {ACT_DIALING}},
316 {RSP_ZVLS, 608,608, 17, -1,-1, {ACT_DEBUG}},
317 {RSP_ZCTP, 609,609, -1, -1,-1, {ACT_DEBUG}},
318 {RSP_ZCPN, 609,609, -1, -1,-1, {ACT_DEBUG}},
319 {RSP_ERROR, 601,609, -1, 0, 0, {ACT_ABORTDIAL}},
320 {EV_TIMEOUT, 601,609, -1, 0, 0, {ACT_ABORTDIAL}},
323 {RSP_ZCTP, 650,650, -1, -1,-1, {ACT_DEBUG}},
324 {RSP_ZCPN, 650,650, -1, -1,-1, {ACT_DEBUG}},
325 {RSP_ZSAU, 650,650,ZSAU_CALL_DELIVERED, -1,-1, {ACT_DEBUG}}, /* some devices don't send this */
327 /* connection established */
328 {RSP_ZSAU, 650,650,ZSAU_ACTIVE, 800,-1, {ACT_CONNECT}}, //FIXME -> DLE1
329 {RSP_ZSAU, 750,750,ZSAU_ACTIVE, 800,-1, {ACT_CONNECT}}, //FIXME -> DLE1
331 {EV_BC_OPEN, 800,800, -1, 800,-1, {ACT_NOTIFY_BC_UP}}, //FIXME new constate + timeout
334 {RSP_ZSAU, 650,650,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT}},
335 {RSP_ZSAU, 750,750,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP}},
336 {RSP_ZSAU, 800,800,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP}},
339 {EV_HUP, -1, -1, -1, -1,-1, {ACT_HUP}}, //FIXME
340 {RSP_INIT, -1, -1,SEQ_HUP, 401, 5, {0}, "+VLS=0\r"}, /* hang up */ //-1,-1?
341 {RSP_OK, 401,401, -1, 402, 5},
342 {RSP_ZVLS, 402,402, 0, 403, 5},
343 {RSP_ZSAU, 403,403,ZSAU_DISCONNECT_REQ, -1,-1, {ACT_DEBUG}}, /* if not remote hup */
344 //{RSP_ZSAU, 403,403,ZSAU_NULL, 401, 0, {ACT_ERROR}}, //DELETE//FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
345 {RSP_ZSAU, 403,403,ZSAU_NULL, 0, 0, {ACT_DISCONNECT}}, //FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
346 {RSP_NODEV, 401,403, -1, 0, 0, {ACT_FAKEHUP}}, //FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
347 {RSP_ERROR, 401,401, -1, 0, 0, {ACT_ABORTHUP}},
348 {EV_TIMEOUT, 401,403, -1, 0, 0, {ACT_ABORTHUP}},
350 {EV_BC_CLOSED, 0, 0, -1, 0,-1, {ACT_NOTIFY_BC_DOWN}}, //FIXME new constate + timeout
353 {RSP_ZBC, 700,700, -1, -1,-1, {0}},
354 {RSP_ZHLC, 700,700, -1, -1,-1, {0}},
355 {RSP_NMBR, 700,700, -1, -1,-1, {0}},
356 {RSP_ZCPN, 700,700, -1, -1,-1, {0}},
357 {RSP_ZCTP, 700,700, -1, -1,-1, {0}},
358 {EV_TIMEOUT, 700,700, -1, 720,720, {ACT_ICALL}},
359 {EV_BC_CLOSED,720,720, -1, 0,-1, {ACT_NOTIFY_BC_DOWN}},
362 {EV_ACCEPT, -1, -1, -1, -1,-1, {ACT_ACCEPT}}, //FIXME
363 {RSP_INIT, 720,720,SEQ_ACCEPT, 721, 5, {ACT_CMD+AT_PROTO}},
364 {RSP_OK, 721,721, -1, 722, 5, {ACT_CMD+AT_ISO}},
365 {RSP_OK, 722,722, -1, 723, 5, {0}, "+VLS=17\r"}, /* set "Endgeraetemodus" */
366 {RSP_OK, 723,723, -1, 724, 5, {0}},
367 {RSP_ZVLS, 724,724, 17, 750,50, {ACT_ACCEPTED}},
368 {RSP_ERROR, 721,729, -1, 0, 0, {ACT_ABORTACCEPT}},
369 {EV_TIMEOUT, 721,729, -1, 0, 0, {ACT_ABORTACCEPT}},
370 {RSP_ZSAU, 700,729,ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT}},
371 {RSP_ZSAU, 700,729,ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT}},
372 {RSP_ZSAU, 700,729,ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT}},
374 {EV_TIMEOUT, 750,750, -1, 0, 0, {ACT_CONNTIMEOUT}},
377 {EV_PROTO_L2, -1, -1, -1, -1,-1, {ACT_PROTO_L2}}, //FIXME
379 {RSP_ZCON, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
380 {RSP_ZCCR, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
381 {RSP_ZAOC, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
382 {RSP_ZCSTR, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
384 {RSP_ZCAU, -1, -1, -1, -1,-1, {ACT_ZCAU}},
385 {RSP_NONE, -1, -1, -1, -1,-1, {ACT_DEBUG}},
386 {RSP_ANY, -1, -1, -1, -1,-1, {ACT_WARN}},
392 static struct reply_t tab_nocid[]= /* no dle mode */ //FIXME
394 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
396 {RSP_ANY, -1, -1, -1, -1,-1, ACT_WARN, NULL},
397 {RSP_LAST,0,0,0,0,0,0}
400 static struct reply_t tab_cid[] = /* no dle mode */ //FIXME
402 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
404 {RSP_ANY, -1, -1, -1, -1,-1, ACT_WARN, NULL},
405 {RSP_LAST,0,0,0,0,0,0}
409 static struct resp_type_t resp_type[]=
411 /*{"", RSP_EMPTY, RT_NOTHING},*/
412 {"OK", RSP_OK, RT_NOTHING},
413 {"ERROR", RSP_ERROR, RT_NOTHING},
414 {"ZSAU", RSP_ZSAU, RT_ZSAU},
415 {"ZCAU", RSP_ZCAU, RT_ZCAU},
416 {"RING", RSP_RING, RT_RING},
417 {"ZGCI", RSP_ZGCI, RT_NUMBER},
418 {"ZVLS", RSP_ZVLS, RT_NUMBER},
419 {"ZCTP", RSP_ZCTP, RT_NUMBER},
420 {"ZDLE", RSP_ZDLE, RT_NUMBER},
421 {"ZCFGT", RSP_ZCFGT, RT_NUMBER},
422 {"ZCCR", RSP_ZCCR, RT_NUMBER},
423 {"ZMWI", RSP_ZMWI, RT_NUMBER},
424 {"ZHLC", RSP_ZHLC, RT_STRING},
425 {"ZBC", RSP_ZBC, RT_STRING},
426 {"NMBR", RSP_NMBR, RT_STRING},
427 {"ZCPN", RSP_ZCPN, RT_STRING},
428 {"ZCON", RSP_ZCON, RT_STRING},
429 {"ZAOC", RSP_ZAOC, RT_STRING},
430 {"ZCSTR", RSP_ZCSTR, RT_STRING},
431 {"ZCFG", RSP_ZCFG, RT_HEX},
432 {"ZLOG", RSP_ZLOG, RT_NOTHING},
433 {"ZABINFO", RSP_ZABINFO, RT_NOTHING},
434 {"ZSMLSTCHG", RSP_ZSMLSTCHG, RT_NOTHING},
439 * Get integer from char-pointer
441 static int isdn_getnum(char *p)
445 gig_dbg(DEBUG_TRANSCMD, "string: %s", p);
447 while (*p >= '0' && *p <= '9')
448 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p++) - '0');
450 v = -1; /* invalid Character */
455 * Get integer from char-pointer
457 static int isdn_gethex(char *p)
462 gig_dbg(DEBUG_TRANSCMD, "string: %s", p);
468 if (v > (INT_MAX - 15) / 16)
471 if (c >= '0' && c <= '9')
473 else if (c >= 'a' && c <= 'f')
475 else if (c >= 'A' && c <= 'F')
485 /* retrieve CID from parsed response
486 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
488 static int cid_of_response(char *s)
493 return 0; /* no CID separator */
494 cid = isdn_getnum(s);
496 return 0; /* CID not numeric */
497 if (cid < 1 || cid > 65535)
498 return -1; /* CID out of range */
500 //FIXME is ;<digit>+ at end of non-CID response really impossible?
503 /* This function will be called via task queue from the callback handler.
504 * We received a modem response and have to handle it..
506 void gigaset_handle_modem_response(struct cardstate *cs)
508 unsigned char *argv[MAX_REC_PARAMS + 1];
511 struct resp_type_t *rt;
514 unsigned next, tail, head;
515 struct event_t *event;
525 /* ignore additional LFs/CRs (M10x config mode or cx100) */
526 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[len]);
529 cs->respdata[len] = 0;
530 gig_dbg(DEBUG_TRANSCMD, "raw string: '%s'", cs->respdata);
531 argv[0] = cs->respdata;
533 if (cs->at_state.getstring) {
534 /* getstring only allowed without cid at the moment */
535 cs->at_state.getstring = 0;
540 for (i = 0; i < len; i++)
541 switch (cs->respdata[i]) {
545 if (params > MAX_REC_PARAMS) {
547 "too many parameters in response\n");
548 /* need last parameter (might be CID) */
551 argv[params++] = cs->respdata + i + 1;
555 cid = params > 1 ? cid_of_response(argv[params-1]) : 0;
557 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
562 for (j = 1; j < params; ++j)
565 gig_dbg(DEBUG_TRANSCMD, "CMD received: %s", argv[0]);
568 gig_dbg(DEBUG_TRANSCMD, "CID: %s", argv[params]);
570 gig_dbg(DEBUG_TRANSCMD, "available params: %d", params - 1);
571 for (j = 1; j < params; j++)
572 gig_dbg(DEBUG_TRANSCMD, "param %d: %s", j, argv[j]);
575 spin_lock_irqsave(&cs->ev_lock, flags);
581 while (curarg < params) {
582 next = (tail + 1) % MAX_EVENTS;
583 if (unlikely(next == head)) {
584 dev_err(cs->dev, "event queue full\n");
588 event = cs->events + tail;
589 event->at_state = NULL;
596 resp_code = RSP_STRING;
597 param_type = RT_STRING;
599 for (rt = resp_type; rt->response; ++rt)
600 if (!strcmp(argv[curarg], rt->response))
604 event->type = RSP_UNKNOWN;
606 "unknown modem response: %s\n",
611 resp_code = rt->resp_code;
612 param_type = rt->type;
616 event->type = resp_code;
618 switch (param_type) {
624 "received RING without CID!\n");
625 event->type = RSP_INVAL;
629 event->parameter = cid;
634 if (curarg >= params) {
635 event->parameter = ZSAU_NONE;
638 if (!strcmp(argv[curarg], "OUTGOING_CALL_PROCEEDING"))
639 event->parameter = ZSAU_OUTGOING_CALL_PROCEEDING;
640 else if (!strcmp(argv[curarg], "CALL_DELIVERED"))
641 event->parameter = ZSAU_CALL_DELIVERED;
642 else if (!strcmp(argv[curarg], "ACTIVE"))
643 event->parameter = ZSAU_ACTIVE;
644 else if (!strcmp(argv[curarg], "DISCONNECT_IND"))
645 event->parameter = ZSAU_DISCONNECT_IND;
646 else if (!strcmp(argv[curarg], "NULL"))
647 event->parameter = ZSAU_NULL;
648 else if (!strcmp(argv[curarg], "DISCONNECT_REQ"))
649 event->parameter = ZSAU_DISCONNECT_REQ;
651 event->parameter = ZSAU_UNKNOWN;
653 "%s: unknown parameter %s after ZSAU\n",
654 __func__, argv[curarg]);
659 if (curarg < params) {
660 event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
662 dev_err(cs->dev, "out of memory\n");
665 #ifdef CONFIG_GIGASET_DEBUG
667 gig_dbg(DEBUG_CMD, "string==NULL");
669 gig_dbg(DEBUG_CMD, "string==%s",
670 (char *) event->ptr);
674 event->parameter = -1;
675 if (curarg + 1 < params) {
676 i = isdn_gethex(argv[curarg]);
677 j = isdn_gethex(argv[curarg + 1]);
678 if (i >= 0 && i < 256 && j >= 0 && j < 256)
679 event->parameter = (unsigned) i << 8
687 if (curarg < params) {
688 if (param_type == RT_HEX)
690 isdn_gethex(argv[curarg]);
693 isdn_getnum(argv[curarg]);
696 event->parameter = -1;
697 #ifdef CONFIG_GIGASET_DEBUG
698 gig_dbg(DEBUG_CMD, "parameter==%d", event->parameter);
703 if (resp_code == RSP_ZDLE)
704 cs->dle = event->parameter;
711 spin_unlock_irqrestore(&cs->ev_lock, flags);
713 if (curarg != params)
715 "invalid number of processed parameters: %d/%d",
718 EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
721 * process closing of connection associated with given AT state structure
723 static void disconnect(struct at_state_t **at_state_p)
726 struct bc_state *bcs = (*at_state_p)->bcs;
727 struct cardstate *cs = (*at_state_p)->cs;
729 spin_lock_irqsave(&cs->lock, flags);
730 ++(*at_state_p)->seq_index;
732 /* revert to selected idle mode */
734 cs->at_state.pending_commands |= PC_UMMODE;
735 atomic_set(&cs->commands_pending, 1); //FIXME
736 gig_dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
738 spin_unlock_irqrestore(&cs->lock, flags);
741 /* B channel assigned: invoke hardware specific handler */
742 cs->ops->close_bchannel(bcs);
744 /* no B channel assigned: just deallocate */
745 spin_lock_irqsave(&cs->lock, flags);
746 list_del(&(*at_state_p)->list);
749 spin_unlock_irqrestore(&cs->lock, flags);
754 * get a free AT state structure: either one of those associated with the
755 * B channels of the Gigaset device, or if none of those is available,
756 * a newly allocated one with bcs=NULL
757 * The structure should be freed by calling disconnect() after use.
759 static inline struct at_state_t *get_free_channel(struct cardstate *cs,
761 /* cids: >0: siemens-cid
763 -1: no cid assigned yet
768 struct at_state_t *ret;
770 for (i = 0; i < cs->channels; ++i)
771 if (gigaset_get_channel(cs->bcs + i)) {
772 ret = &cs->bcs[i].at_state;
777 spin_lock_irqsave(&cs->lock, flags);
778 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
780 gigaset_at_init(ret, NULL, cs, cid);
781 list_add(&ret->list, &cs->temp_at_states);
783 spin_unlock_irqrestore(&cs->lock, flags);
787 static void init_failed(struct cardstate *cs, int mode)
790 struct at_state_t *at_state;
792 cs->at_state.pending_commands &= ~PC_INIT;
793 atomic_set(&cs->mode, mode);
794 atomic_set(&cs->mstate, MS_UNINITIALIZED);
795 gigaset_free_channels(cs);
796 for (i = 0; i < cs->channels; ++i) {
797 at_state = &cs->bcs[i].at_state;
798 if (at_state->pending_commands & PC_CID) {
799 at_state->pending_commands &= ~PC_CID;
800 at_state->pending_commands |= PC_NOCID;
801 atomic_set(&cs->commands_pending, 1);
806 static void schedule_init(struct cardstate *cs, int state)
808 if (cs->at_state.pending_commands & PC_INIT) {
809 gig_dbg(DEBUG_CMD, "not scheduling PC_INIT again");
812 atomic_set(&cs->mstate, state);
813 atomic_set(&cs->mode, M_UNKNOWN);
814 gigaset_block_channels(cs);
815 cs->at_state.pending_commands |= PC_INIT;
816 atomic_set(&cs->commands_pending, 1);
817 gig_dbg(DEBUG_CMD, "Scheduling PC_INIT");
820 /* Add "AT" to a command, add the cid, dle encode it, send the result to the
822 static void send_command(struct cardstate *cs, const char *cmd, int cid,
823 int dle, gfp_t kmallocflags)
825 size_t cmdlen, buflen;
826 char *cmdpos, *cmdbuf, *cmdtail;
828 cmdlen = strlen(cmd);
829 buflen = 11 + cmdlen;
830 if (unlikely(buflen <= cmdlen)) {
831 dev_err(cs->dev, "integer overflow in buflen\n");
835 cmdbuf = kmalloc(buflen, kmallocflags);
836 if (unlikely(!cmdbuf)) {
837 dev_err(cs->dev, "out of memory\n");
842 cmdtail = cmdpos + cmdlen;
843 memcpy(cmdpos, cmd, cmdlen);
845 if (cid > 0 && cid <= 65535) {
847 *--cmdpos = '0' + cid % 10;
865 cs->ops->write_cmd(cs, cmdpos, cmdlen, NULL);
869 static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
871 struct at_state_t *at_state;
876 return &cs->at_state;
878 for (i = 0; i < cs->channels; ++i)
879 if (cid == cs->bcs[i].at_state.cid)
880 return &cs->bcs[i].at_state;
882 spin_lock_irqsave(&cs->lock, flags);
884 list_for_each_entry(at_state, &cs->temp_at_states, list)
885 if (cid == at_state->cid) {
886 spin_unlock_irqrestore(&cs->lock, flags);
890 spin_unlock_irqrestore(&cs->lock, flags);
895 static void bchannel_down(struct bc_state *bcs)
897 if (bcs->chstate & CHS_B_UP) {
898 bcs->chstate &= ~CHS_B_UP;
899 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
902 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
903 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
904 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
907 gigaset_free_channel(bcs);
909 gigaset_bcs_reinit(bcs);
912 static void bchannel_up(struct bc_state *bcs)
914 if (!(bcs->chstate & CHS_D_UP)) {
915 dev_notice(bcs->cs->dev, "%s: D channel not up\n", __func__);
916 bcs->chstate |= CHS_D_UP;
917 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
920 if (bcs->chstate & CHS_B_UP) {
921 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
926 bcs->chstate |= CHS_B_UP;
927 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
930 static void start_dial(struct at_state_t *at_state, void *data, unsigned seq_index)
932 struct bc_state *bcs = at_state->bcs;
933 struct cardstate *cs = at_state->cs;
937 bcs->chstate |= CHS_NOTIFY_LL;
939 spin_lock_irqsave(&cs->lock, flags);
940 if (at_state->seq_index != seq_index) {
941 spin_unlock_irqrestore(&cs->lock, flags);
944 spin_unlock_irqrestore(&cs->lock, flags);
946 retval = gigaset_isdn_setup_dial(at_state, data);
951 at_state->pending_commands |= PC_CID;
952 gig_dbg(DEBUG_CMD, "Scheduling PC_CID");
953 atomic_set(&cs->commands_pending, 1);
957 at_state->pending_commands |= PC_NOCID;
958 gig_dbg(DEBUG_CMD, "Scheduling PC_NOCID");
959 atomic_set(&cs->commands_pending, 1);
963 static void start_accept(struct at_state_t *at_state)
965 struct cardstate *cs = at_state->cs;
968 retval = gigaset_isdn_setup_accept(at_state);
971 at_state->pending_commands |= PC_ACCEPT;
972 gig_dbg(DEBUG_CMD, "Scheduling PC_ACCEPT");
973 atomic_set(&cs->commands_pending, 1);
976 at_state->pending_commands |= PC_HUP;
977 gig_dbg(DEBUG_CMD, "Scheduling PC_HUP");
978 atomic_set(&cs->commands_pending, 1);
982 static void do_start(struct cardstate *cs)
984 gigaset_free_channels(cs);
986 if (atomic_read(&cs->mstate) != MS_LOCKED)
987 schedule_init(cs, MS_INIT);
990 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
991 // FIXME: not in locked mode
992 // FIXME 2: only after init sequence
995 wake_up(&cs->waitqueue);
998 static void finish_shutdown(struct cardstate *cs)
1000 if (atomic_read(&cs->mstate) != MS_LOCKED) {
1001 atomic_set(&cs->mstate, MS_UNINITIALIZED);
1002 atomic_set(&cs->mode, M_UNKNOWN);
1005 /* Tell the LL that the device is not available .. */
1008 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
1011 /* The rest is done by cleanup_cs () in user mode. */
1013 cs->cmd_result = -ENODEV;
1015 wake_up_interruptible(&cs->waitqueue);
1018 static void do_shutdown(struct cardstate *cs)
1020 gigaset_block_channels(cs);
1022 if (atomic_read(&cs->mstate) == MS_READY) {
1023 atomic_set(&cs->mstate, MS_SHUTDOWN);
1024 cs->at_state.pending_commands |= PC_SHUTDOWN;
1025 atomic_set(&cs->commands_pending, 1);
1026 gig_dbg(DEBUG_CMD, "Scheduling PC_SHUTDOWN");
1028 finish_shutdown(cs);
1031 static void do_stop(struct cardstate *cs)
1033 unsigned long flags;
1035 spin_lock_irqsave(&cs->lock, flags);
1037 spin_unlock_irqrestore(&cs->lock, flags);
1042 /* Entering cid mode or getting a cid failed:
1043 * try to initialize the device and try again.
1045 * channel >= 0: getting cid for the channel failed
1046 * channel < 0: entering cid mode failed
1048 * returns 0 on failure
1050 static int reinit_and_retry(struct cardstate *cs, int channel)
1054 if (--cs->retry_count <= 0)
1057 for (i = 0; i < cs->channels; ++i)
1058 if (cs->bcs[i].at_state.cid > 0)
1063 "Could not enter cid mode. Reinit device and try again.\n");
1066 "Could not get a call id. Reinit device and try again.\n");
1067 cs->bcs[channel].at_state.pending_commands |= PC_CID;
1069 schedule_init(cs, MS_INIT);
1073 static int at_state_invalid(struct cardstate *cs,
1074 struct at_state_t *test_ptr)
1076 unsigned long flags;
1078 struct at_state_t *at_state;
1081 spin_lock_irqsave(&cs->lock, flags);
1083 if (test_ptr == &cs->at_state)
1086 list_for_each_entry(at_state, &cs->temp_at_states, list)
1087 if (at_state == test_ptr)
1090 for (channel = 0; channel < cs->channels; ++channel)
1091 if (&cs->bcs[channel].at_state == test_ptr)
1096 spin_unlock_irqrestore(&cs->lock, flags);
1100 static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
1101 struct at_state_t **p_at_state)
1104 struct at_state_t *at_state = *p_at_state;
1106 retval = gigaset_isdn_icall(at_state);
1111 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
1112 /* --v-- fall through --v-- */
1116 * Device doc says that would reject the call.
1117 * In fact it doesn't.
1119 at_state->pending_commands |= PC_HUP;
1120 atomic_set(&cs->commands_pending, 1);
1125 static int do_lock(struct cardstate *cs)
1130 switch (atomic_read(&cs->mstate)) {
1131 case MS_UNINITIALIZED:
1133 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1134 cs->at_state.pending_commands)
1137 for (i = 0; i < cs->channels; ++i)
1138 if (cs->bcs[i].at_state.pending_commands)
1141 if (!gigaset_get_channels(cs))
1152 mode = atomic_read(&cs->mode);
1153 atomic_set(&cs->mstate, MS_LOCKED);
1154 atomic_set(&cs->mode, M_UNKNOWN);
1159 static int do_unlock(struct cardstate *cs)
1161 if (atomic_read(&cs->mstate) != MS_LOCKED)
1164 atomic_set(&cs->mstate, MS_UNINITIALIZED);
1165 atomic_set(&cs->mode, M_UNKNOWN);
1166 gigaset_free_channels(cs);
1168 schedule_init(cs, MS_INIT);
1173 static void do_action(int action, struct cardstate *cs,
1174 struct bc_state *bcs,
1175 struct at_state_t **p_at_state, char **pp_command,
1176 int *p_genresp, int *p_resp_code,
1179 struct at_state_t *at_state = *p_at_state;
1180 struct at_state_t *at_state2;
1181 unsigned long flags;
1185 unsigned char *s, *e;
1193 at_state->waiting = 1;
1196 cs->at_state.pending_commands &= ~PC_INIT;
1197 cs->cur_at_seq = SEQ_NONE;
1198 atomic_set(&cs->mode, M_UNIMODEM);
1199 spin_lock_irqsave(&cs->lock, flags);
1201 spin_unlock_irqrestore(&cs->lock, flags);
1202 gigaset_free_channels(cs);
1203 atomic_set(&cs->mstate, MS_READY);
1206 spin_unlock_irqrestore(&cs->lock, flags);
1207 cs->at_state.pending_commands |= PC_CIDMODE;
1208 atomic_set(&cs->commands_pending, 1);
1209 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1212 dev_warn(cs->dev, "Could not initialize the device.\n");
1214 init_failed(cs, M_UNKNOWN);
1215 cs->cur_at_seq = SEQ_NONE;
1217 case ACT_CONFIGMODE:
1218 init_failed(cs, M_CONFIG);
1219 cs->cur_at_seq = SEQ_NONE;
1223 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1224 cs->inbuf[0].inputstate &=
1225 ~(INS_command | INS_DLE_command);
1229 cs->inbuf[0].inputstate =
1230 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1234 if (atomic_read(&cs->mstate) == MS_INIT ||
1235 atomic_read(&cs->mstate) == MS_RECOVER) {
1236 gigaset_free_channels(cs);
1237 atomic_set(&cs->mstate, MS_READY);
1239 atomic_set(&cs->mode, M_CID);
1240 cs->cur_at_seq = SEQ_NONE;
1243 atomic_set(&cs->mode, M_UNIMODEM);
1244 cs->cur_at_seq = SEQ_NONE;
1247 cs->cur_at_seq = SEQ_NONE;
1248 if (atomic_read(&cs->mstate) == MS_INIT ||
1249 atomic_read(&cs->mstate) == MS_RECOVER) {
1250 init_failed(cs, M_UNKNOWN);
1253 if (!reinit_and_retry(cs, -1))
1254 schedule_init(cs, MS_RECOVER);
1257 cs->cur_at_seq = SEQ_NONE;
1258 schedule_init(cs, MS_RECOVER);
1261 /* send "+++" (hangup in unimodem mode) */
1262 cs->ops->write_cmd(cs, "+++", 3, NULL);
1265 /* get fresh AT state structure for new CID */
1266 at_state2 = get_free_channel(cs, ev->parameter);
1269 "RING ignored: could not allocate channel structure\n");
1273 /* initialize AT state structure
1274 * note that bcs may be NULL if no B channel is free
1276 at_state2->ConState = 700;
1277 kfree(at_state2->str_var[STR_NMBR]);
1278 at_state2->str_var[STR_NMBR] = NULL;
1279 kfree(at_state2->str_var[STR_ZCPN]);
1280 at_state2->str_var[STR_ZCPN] = NULL;
1281 kfree(at_state2->str_var[STR_ZBC]);
1282 at_state2->str_var[STR_ZBC] = NULL;
1283 kfree(at_state2->str_var[STR_ZHLC]);
1284 at_state2->str_var[STR_ZHLC] = NULL;
1285 at_state2->int_var[VAR_ZCTP] = -1;
1287 spin_lock_irqsave(&cs->lock, flags);
1288 at_state2->timer_expires = RING_TIMEOUT;
1289 at_state2->timer_active = 1;
1290 spin_unlock_irqrestore(&cs->lock, flags);
1293 handle_icall(cs, bcs, p_at_state);
1294 at_state = *p_at_state;
1297 dev_warn(cs->dev, "Could not shut down the device.\n");
1301 cs->cur_at_seq = SEQ_NONE;
1302 finish_shutdown(cs);
1305 if (cs->onechannel) {
1306 at_state->pending_commands |= PC_DLE1;
1307 atomic_set(&cs->commands_pending, 1);
1310 bcs->chstate |= CHS_D_UP;
1311 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
1312 cs->ops->init_bchannel(bcs);
1315 cs->cur_at_seq = SEQ_NONE;
1316 bcs = cs->bcs + cs->curchannel;
1318 bcs->chstate |= CHS_D_UP;
1319 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
1320 cs->ops->init_bchannel(bcs);
1323 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1325 case ACT_DISCONNECT:
1326 cs->cur_at_seq = SEQ_NONE;
1328 if (bcs && cs->onechannel && cs->dle) {
1329 /* Check for other open channels not needed:
1330 * DLE only used for M10x with one B channel.
1332 at_state->pending_commands |= PC_DLE0;
1333 atomic_set(&cs->commands_pending, 1);
1335 disconnect(p_at_state);
1336 at_state = *p_at_state;
1340 at_state->int_var[VAR_ZDLE] = 0;
1344 cs->cur_at_seq = SEQ_NONE;
1345 at_state2 = &cs->bcs[cs->curchannel].at_state;
1346 disconnect(&at_state2);
1349 cs->cur_at_seq = SEQ_NONE;
1350 dev_warn(cs->dev, "Could not hang up.\n");
1352 if (bcs && cs->onechannel)
1353 at_state->pending_commands |= PC_DLE0;
1355 disconnect(p_at_state);
1356 at_state = *p_at_state;
1358 schedule_init(cs, MS_RECOVER);
1361 cs->cur_at_seq = SEQ_NONE;
1362 dev_warn(cs->dev, "Could not leave DLE mode.\n");
1363 at_state2 = &cs->bcs[cs->curchannel].at_state;
1364 disconnect(&at_state2);
1365 schedule_init(cs, MS_RECOVER);
1368 cs->cur_at_seq = SEQ_NONE;
1370 "Could not enter DLE mode. Trying to hang up.\n");
1371 channel = cs->curchannel;
1372 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
1373 atomic_set(&cs->commands_pending, 1);
1376 case ACT_CID: /* got cid; start dialing */
1377 cs->cur_at_seq = SEQ_NONE;
1378 channel = cs->curchannel;
1379 if (ev->parameter > 0 && ev->parameter <= 65535) {
1380 cs->bcs[channel].at_state.cid = ev->parameter;
1381 cs->bcs[channel].at_state.pending_commands |=
1383 atomic_set(&cs->commands_pending, 1);
1388 cs->cur_at_seq = SEQ_NONE;
1389 channel = cs->curchannel;
1390 if (!reinit_and_retry(cs, channel)) {
1392 "Could not get a call ID. Cannot dial.\n");
1393 at_state2 = &cs->bcs[channel].at_state;
1394 disconnect(&at_state2);
1398 cs->cur_at_seq = SEQ_NONE;
1399 at_state2 = &cs->bcs[cs->curchannel].at_state;
1400 disconnect(&at_state2);
1405 cs->cur_at_seq = SEQ_NONE;
1408 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL processing */
1409 disconnect(p_at_state);
1410 at_state = *p_at_state;
1413 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1414 cs->cur_at_seq = SEQ_NONE;
1415 at_state->pending_commands |= PC_HUP;
1416 atomic_set(&cs->commands_pending, 1);
1419 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1420 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1421 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1422 at_state->pending_commands |= PC_HUP;
1423 atomic_set(&cs->commands_pending, 1);
1425 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
1426 are not handled properly anymore */
1427 at_state->getstring = 1;
1432 *p_resp_code = RSP_ERROR;
1437 if (!strcmp(s, "OK")) {
1439 *p_resp_code = RSP_ERROR;
1443 for (i = 0; i < 4; ++i) {
1444 val = simple_strtoul(s, (char **) &e, 10);
1445 if (val > INT_MAX || e == s)
1450 } else if (*e != '.')
1458 *p_resp_code = RSP_ERROR;
1461 /*at_state->getstring = 1;*/
1465 if (cs->gotfwver == 0) {
1468 "firmware version %02d.%03d.%02d.%02d",
1469 cs->fwver[0], cs->fwver[1],
1470 cs->fwver[2], cs->fwver[3]);
1476 dev_err(cs->dev, "could not read firmware version.\n");
1478 #ifdef CONFIG_GIGASET_DEBUG
1481 *p_resp_code = RSP_ERROR;
1485 static int count = 3; //2; //1;
1487 *p_resp_code = count ? RSP_ERROR : RSP_OK;
1494 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
1495 __func__, ev->type, at_state->ConState);
1498 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1499 __func__, ev->type, at_state->ConState);
1502 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1503 ev->parameter, at_state->ConState);
1506 /* events from the LL */
1508 start_dial(at_state, ev->ptr, ev->parameter);
1511 start_accept(at_state);
1514 gig_dbg(DEBUG_CMD, "set protocol to %u",
1515 (unsigned) ev->parameter);
1516 at_state->bcs->proto2 = ev->parameter;
1519 at_state->pending_commands |= PC_HUP;
1520 atomic_set(&cs->commands_pending, 1);
1521 gig_dbg(DEBUG_CMD, "Scheduling PC_HUP");
1524 /* hotplug events */
1532 /* events from the interface */ // FIXME without ACT_xxxx?
1534 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1536 wake_up(&cs->waitqueue);
1539 if (ev->parameter != 0)
1540 cs->cmd_result = -EINVAL;
1541 else if (cs->gotfwver != 1) {
1542 cs->cmd_result = -ENOENT;
1544 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1548 wake_up(&cs->waitqueue);
1551 /* events from the proc file system */ // FIXME without ACT_xxxx?
1552 case ACT_PROC_CIDMODE:
1553 spin_lock_irqsave(&cs->lock, flags);
1554 if (ev->parameter != cs->cidmode) {
1555 cs->cidmode = ev->parameter;
1556 if (ev->parameter) {
1557 cs->at_state.pending_commands |= PC_CIDMODE;
1558 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1560 cs->at_state.pending_commands |= PC_UMMODE;
1561 gig_dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
1563 atomic_set(&cs->commands_pending, 1);
1565 spin_unlock_irqrestore(&cs->lock, flags);
1567 wake_up(&cs->waitqueue);
1570 /* events from the hardware drivers */
1571 case ACT_NOTIFY_BC_DOWN:
1574 case ACT_NOTIFY_BC_UP:
1583 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1584 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1587 *p_resp_code = RSP_NULL;
1590 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
1594 /* State machine to do the calling and hangup procedure */
1595 static void process_event(struct cardstate *cs, struct event_t *ev)
1597 struct bc_state *bcs;
1598 char *p_command = NULL;
1599 struct reply_t *rep;
1602 int resp_code = RSP_ERROR;
1604 struct at_state_t *at_state;
1607 unsigned long flags;
1610 at_state = at_state_from_cid(cs, ev->cid);
1612 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
1617 at_state = ev->at_state;
1618 if (at_state_invalid(cs, at_state)) {
1619 gig_dbg(DEBUG_ANY, "event for invalid at_state %p",
1625 gig_dbg(DEBUG_CMD, "connection state %d, event %d",
1626 at_state->ConState, ev->type);
1628 bcs = at_state->bcs;
1629 sendcid = at_state->cid;
1631 /* Setting the pointer to the dial array */
1632 rep = at_state->replystruct;
1634 spin_lock_irqsave(&cs->lock, flags);
1635 if (ev->type == EV_TIMEOUT) {
1636 if (ev->parameter != at_state->timer_index
1637 || !at_state->timer_active) {
1638 ev->type = RSP_NONE; /* old timeout */
1639 gig_dbg(DEBUG_ANY, "old timeout");
1640 } else if (!at_state->waiting)
1641 gig_dbg(DEBUG_ANY, "timeout occurred");
1643 gig_dbg(DEBUG_ANY, "stopped waiting");
1645 spin_unlock_irqrestore(&cs->lock, flags);
1647 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1648 or at_state->str_var[STR_XXXX], set it */
1649 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1650 index = ev->type - RSP_VAR;
1651 at_state->int_var[index] = ev->parameter;
1652 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1653 index = ev->type - RSP_STR;
1654 kfree(at_state->str_var[index]);
1655 at_state->str_var[index] = ev->ptr;
1656 ev->ptr = NULL; /* prevent process_events() from
1660 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1661 at_state->getstring = 0;
1663 /* Search row in dial array which matches modem response and current
1666 rcode = rep->resp_code;
1667 if (rcode == RSP_LAST) {
1668 /* found nothing...*/
1669 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1670 "resp_code %d in ConState %d!\n",
1671 __func__, ev->type, at_state->ConState);
1674 if ((rcode == RSP_ANY || rcode == ev->type)
1675 && ((int) at_state->ConState >= rep->min_ConState)
1676 && (rep->max_ConState < 0
1677 || (int) at_state->ConState <= rep->max_ConState)
1678 && (rep->parameter < 0 || rep->parameter == ev->parameter))
1682 p_command = rep->command;
1684 at_state->waiting = 0;
1685 for (curact = 0; curact < MAXACT; ++curact) {
1686 /* The row tells us what we should do ..
1688 do_action(rep->action[curact], cs, bcs, &at_state, &p_command, &genresp, &resp_code, ev);
1690 break; /* may be freed after disconnect */
1694 /* Jump to the next con-state regarding the array */
1695 if (rep->new_ConState >= 0)
1696 at_state->ConState = rep->new_ConState;
1699 spin_lock_irqsave(&cs->lock, flags);
1700 at_state->timer_expires = 0; //FIXME
1701 at_state->timer_active = 0; //FIXME
1702 spin_unlock_irqrestore(&cs->lock, flags);
1703 gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1705 /* Send command to modem if not NULL... */
1706 if (p_command/*rep->command*/) {
1708 send_command(cs, p_command,
1712 gigaset_add_event(cs, at_state,
1717 spin_lock_irqsave(&cs->lock, flags);
1718 if (!rep->timeout) {
1719 at_state->timer_expires = 0;
1720 at_state->timer_active = 0;
1721 } else if (rep->timeout > 0) { /* new timeout */
1722 at_state->timer_expires = rep->timeout * 10;
1723 at_state->timer_active = 1;
1724 ++at_state->timer_index;
1726 spin_unlock_irqrestore(&cs->lock, flags);
1731 static void schedule_sequence(struct cardstate *cs,
1732 struct at_state_t *at_state, int sequence)
1734 cs->cur_at_seq = sequence;
1735 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1738 static void process_command_flags(struct cardstate *cs)
1740 struct at_state_t *at_state = NULL;
1741 struct bc_state *bcs;
1744 unsigned long flags;
1746 atomic_set(&cs->commands_pending, 0);
1748 if (cs->cur_at_seq) {
1749 gig_dbg(DEBUG_CMD, "not searching scheduled commands: busy");
1753 gig_dbg(DEBUG_CMD, "searching scheduled commands");
1755 sequence = SEQ_NONE;
1757 /* clear pending_commands and hangup channels on shutdown */
1758 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1759 cs->at_state.pending_commands &= ~PC_CIDMODE;
1760 for (i = 0; i < cs->channels; ++i) {
1762 at_state = &bcs->at_state;
1763 at_state->pending_commands &=
1764 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1765 if (at_state->cid > 0)
1766 at_state->pending_commands |= PC_HUP;
1767 if (at_state->pending_commands & PC_CID) {
1768 at_state->pending_commands |= PC_NOCID;
1769 at_state->pending_commands &= ~PC_CID;
1774 /* clear pending_commands and hangup channels on reset */
1775 if (cs->at_state.pending_commands & PC_INIT) {
1776 cs->at_state.pending_commands &= ~PC_CIDMODE;
1777 for (i = 0; i < cs->channels; ++i) {
1779 at_state = &bcs->at_state;
1780 at_state->pending_commands &=
1781 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1782 if (at_state->cid > 0)
1783 at_state->pending_commands |= PC_HUP;
1784 if (atomic_read(&cs->mstate) == MS_RECOVER) {
1785 if (at_state->pending_commands & PC_CID) {
1786 at_state->pending_commands |= PC_NOCID;
1787 at_state->pending_commands &= ~PC_CID;
1793 /* only switch back to unimodem mode, if no commands are pending and no channels are up */
1794 spin_lock_irqsave(&cs->lock, flags);
1795 if (cs->at_state.pending_commands == PC_UMMODE
1797 && list_empty(&cs->temp_at_states)
1798 && atomic_read(&cs->mode) == M_CID) {
1799 sequence = SEQ_UMMODE;
1800 at_state = &cs->at_state;
1801 for (i = 0; i < cs->channels; ++i) {
1803 if (bcs->at_state.pending_commands ||
1804 bcs->at_state.cid > 0) {
1805 sequence = SEQ_NONE;
1810 spin_unlock_irqrestore(&cs->lock, flags);
1811 cs->at_state.pending_commands &= ~PC_UMMODE;
1812 if (sequence != SEQ_NONE) {
1813 schedule_sequence(cs, at_state, sequence);
1817 for (i = 0; i < cs->channels; ++i) {
1819 if (bcs->at_state.pending_commands & PC_HUP) {
1820 bcs->at_state.pending_commands &= ~PC_HUP;
1821 if (bcs->at_state.pending_commands & PC_CID) {
1822 /* not yet dialing: PC_NOCID is sufficient */
1823 bcs->at_state.pending_commands |= PC_NOCID;
1824 bcs->at_state.pending_commands &= ~PC_CID;
1826 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1830 if (bcs->at_state.pending_commands & PC_NOCID) {
1831 bcs->at_state.pending_commands &= ~PC_NOCID;
1832 cs->curchannel = bcs->channel;
1833 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1835 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1836 bcs->at_state.pending_commands &= ~PC_DLE0;
1837 cs->curchannel = bcs->channel;
1838 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1843 list_for_each_entry(at_state, &cs->temp_at_states, list)
1844 if (at_state->pending_commands & PC_HUP) {
1845 at_state->pending_commands &= ~PC_HUP;
1846 schedule_sequence(cs, at_state, SEQ_HUP);
1850 if (cs->at_state.pending_commands & PC_INIT) {
1851 cs->at_state.pending_commands &= ~PC_INIT;
1852 cs->dle = 0; //FIXME
1853 cs->inbuf->inputstate = INS_command;
1854 //FIXME reset card state (or -> LOCK0)?
1855 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1858 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1859 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1860 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1863 if (cs->at_state.pending_commands & PC_CIDMODE) {
1864 cs->at_state.pending_commands &= ~PC_CIDMODE;
1865 if (atomic_read(&cs->mode) == M_UNIMODEM) {
1866 cs->retry_count = 1;
1867 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1872 for (i = 0; i < cs->channels; ++i) {
1874 if (bcs->at_state.pending_commands & PC_DLE1) {
1875 bcs->at_state.pending_commands &= ~PC_DLE1;
1876 cs->curchannel = bcs->channel;
1877 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1880 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1881 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1882 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1885 if (bcs->at_state.pending_commands & PC_DIAL) {
1886 bcs->at_state.pending_commands &= ~PC_DIAL;
1887 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1890 if (bcs->at_state.pending_commands & PC_CID) {
1891 switch (atomic_read(&cs->mode)) {
1893 cs->at_state.pending_commands |= PC_CIDMODE;
1894 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1895 atomic_set(&cs->commands_pending, 1);
1897 #ifdef GIG_MAYINITONDIAL
1899 schedule_init(cs, MS_INIT);
1903 bcs->at_state.pending_commands &= ~PC_CID;
1904 cs->curchannel = bcs->channel;
1906 cs->retry_count = 2;
1908 cs->retry_count = 1;
1910 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1916 static void process_events(struct cardstate *cs)
1919 unsigned head, tail;
1921 int check_flags = 0;
1923 unsigned long flags;
1925 spin_lock_irqsave(&cs->ev_lock, flags);
1928 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
1931 if (!check_flags && !atomic_read(&cs->commands_pending))
1934 spin_unlock_irqrestore(&cs->ev_lock, flags);
1935 process_command_flags(cs);
1936 spin_lock_irqsave(&cs->ev_lock, flags);
1939 if (!atomic_read(&cs->commands_pending))
1945 ev = cs->events + head;
1946 was_busy = cs->cur_at_seq != SEQ_NONE;
1947 spin_unlock_irqrestore(&cs->ev_lock, flags);
1948 process_event(cs, ev);
1949 spin_lock_irqsave(&cs->ev_lock, flags);
1952 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1955 head = (head + 1) % MAX_EVENTS;
1959 spin_unlock_irqrestore(&cs->ev_lock, flags);
1961 if (i == 2 * MAX_EVENTS) {
1963 "infinite loop in process_events; aborting.\n");
1967 /* tasklet scheduled on any event received from the Gigaset device
1969 * data ISDN controller state structure
1971 void gigaset_handle_event(unsigned long data)
1973 struct cardstate *cs = (struct cardstate *) data;
1975 /* handle incoming data on control/common channel */
1976 if (atomic_read(&cs->inbuf->head) != atomic_read(&cs->inbuf->tail)) {
1977 gig_dbg(DEBUG_INTR, "processing new data");
1978 cs->ops->handle_input(cs->inbuf);