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[] =
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}},
208 {RSP_INIT, 0, 0,SEQ_DLE0, 201, 5, {0}, "^SDLE=0\r"},
209 {RSP_OK, 201,201, -1, 202,-1},
210 //{RSP_ZDLE, 202,202, 0, 202, 0, {ACT_ERROR}},//DELETE
211 {RSP_ZDLE, 202,202, 0, 0, 0, {ACT_DLE0}},
212 {RSP_NODEV, 200,249, -1, 0, 0, {ACT_FAKEDLE0}},
213 {RSP_ERROR, 200,249, -1, 0, 0, {ACT_FAILDLE0}},
214 {EV_TIMEOUT, 200,249, -1, 0, 0, {ACT_FAILDLE0}},
217 {RSP_INIT, 0, 0,SEQ_DLE1, 251, 5, {0}, "^SDLE=1\r"},
218 {RSP_OK, 251,251, -1, 252,-1},
219 {RSP_ZDLE, 252,252, 1, 0, 0, {ACT_DLE1}},
220 {RSP_ERROR, 250,299, -1, 0, 0, {ACT_FAILDLE1}},
221 {EV_TIMEOUT, 250,299, -1, 0, 0, {ACT_FAILDLE1}},
224 {RSP_RING, -1, -1, -1, -1,-1, {ACT_RING}},
227 //{RSP_INIT, 0, 0,300, 901, 0, {ACT_TEST}},
228 //{RSP_ERROR, 901,901, -1, 0, 0, {ACT_FAILCID}},
229 //{RSP_OK, 901,901, -1, 301, 5, {0}, "^SGCI?\r"},
231 {RSP_INIT, 0, 0,SEQ_CID, 301, 5, {0}, "^SGCI?\r"},
232 {RSP_OK, 301,301, -1, 302,-1},
233 {RSP_ZGCI, 302,302, -1, 0, 0, {ACT_CID}},
234 {RSP_ERROR, 301,349, -1, 0, 0, {ACT_FAILCID}},
235 {EV_TIMEOUT, 301,349, -1, 0, 0, {ACT_FAILCID}},
238 {RSP_INIT, 0, 0,SEQ_CIDMODE, 150, 5, {0}, "^SGCI=1\r"},
239 {RSP_OK, 150,150, -1, 0, 0, {ACT_CMODESET}},
240 {RSP_ERROR, 150,150, -1, 0, 0, {ACT_FAILCMODE}},
241 {EV_TIMEOUT, 150,150, -1, 0, 0, {ACT_FAILCMODE}},
244 //{RSP_INIT, 0, 0,SEQ_UMMODE, 160, 5, {0}, "^SGCI=0\r"},
245 {RSP_INIT, 0, 0,SEQ_UMMODE, 160, 5, {0}, "Z\r"},
246 {RSP_OK, 160,160, -1, 0, 0, {ACT_UMODESET}},
247 {RSP_ERROR, 160,160, -1, 0, 0, {ACT_FAILUMODE}},
248 {EV_TIMEOUT, 160,160, -1, 0, 0, {ACT_FAILUMODE}},
250 /* abort getting cid */
251 {RSP_INIT, 0, 0,SEQ_NOCID, 0, 0, {ACT_ABORTCID}},
254 {RSP_INIT, 0, 0,SEQ_SHUTDOWN, 504, 5, {0}, "Z\r"},
255 {RSP_OK, 504,504, -1, 0, 0, {ACT_SDOWN}},
256 {RSP_ERROR, 501,599, -1, 0, 0, {ACT_FAILSDOWN}},
257 {EV_TIMEOUT, 501,599, -1, 0, 0, {ACT_FAILSDOWN}},
258 {RSP_NODEV, 501,599, -1, 0, 0, {ACT_FAKESDOWN}},
260 {EV_PROC_CIDMODE,-1, -1, -1, -1,-1, {ACT_PROC_CIDMODE}}, //FIXME
261 {EV_IF_LOCK, -1, -1, -1, -1,-1, {ACT_IF_LOCK}}, //FIXME
262 {EV_IF_VER, -1, -1, -1, -1,-1, {ACT_IF_VER}}, //FIXME
263 {EV_START, -1, -1, -1, -1,-1, {ACT_START}}, //FIXME
264 {EV_STOP, -1, -1, -1, -1,-1, {ACT_STOP}}, //FIXME
265 {EV_SHUTDOWN, -1, -1, -1, -1,-1, {ACT_SHUTDOWN}}, //FIXME
268 {RSP_EMPTY, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
269 {RSP_ZCFGT, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
270 {RSP_ZCFG, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
271 {RSP_ZLOG, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
272 {RSP_ZMWI, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
273 {RSP_ZABINFO, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
274 {RSP_ZSMLSTCHG,-1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
276 {RSP_ZCAU, -1, -1, -1, -1,-1, {ACT_ZCAU}},
277 {RSP_NONE, -1, -1, -1, -1,-1, {ACT_DEBUG}},
278 {RSP_ANY, -1, -1, -1, -1,-1, {ACT_WARN}},
282 // 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring, 400: hup, 750: accepted icall
283 struct reply_t gigaset_tab_cid[] =
285 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
288 {EV_DIAL, -1, -1, -1, -1,-1, {ACT_DIAL}}, //FIXME
289 {RSP_INIT, 0, 0,SEQ_DIAL, 601, 5, {ACT_CMD+AT_BC}},
290 {RSP_OK, 601,601, -1, 602, 5, {ACT_CMD+AT_HLC}},
291 {RSP_NULL, 602,602, -1, 603, 5, {ACT_CMD+AT_PROTO}},
292 {RSP_OK, 602,602, -1, 603, 5, {ACT_CMD+AT_PROTO}},
293 {RSP_OK, 603,603, -1, 604, 5, {ACT_CMD+AT_TYPE}},
294 {RSP_OK, 604,604, -1, 605, 5, {ACT_CMD+AT_MSN}},
295 {RSP_OK, 605,605, -1, 606, 5, {ACT_CMD+AT_ISO}},
296 {RSP_NULL, 605,605, -1, 606, 5, {ACT_CMD+AT_ISO}},
297 {RSP_OK, 606,606, -1, 607, 5, {0}, "+VLS=17\r"}, /* set "Endgeraetemodus" */
298 {RSP_OK, 607,607, -1, 608,-1},
299 //{RSP_ZSAU, 608,608,ZSAU_PROCEEDING, 608, 0, {ACT_ERROR}},//DELETE
300 {RSP_ZSAU, 608,608,ZSAU_PROCEEDING, 609, 5, {ACT_CMD+AT_DIAL}},
301 {RSP_OK, 609,609, -1, 650, 0, {ACT_DIALING}},
303 {RSP_ZVLS, 608,608, 17, -1,-1, {ACT_DEBUG}},
304 {RSP_ZCTP, 609,609, -1, -1,-1, {ACT_DEBUG}},
305 {RSP_ZCPN, 609,609, -1, -1,-1, {ACT_DEBUG}},
306 {RSP_ERROR, 601,609, -1, 0, 0, {ACT_ABORTDIAL}},
307 {EV_TIMEOUT, 601,609, -1, 0, 0, {ACT_ABORTDIAL}},
310 {RSP_ZCTP, 650,650, -1, -1,-1, {ACT_DEBUG}},
311 {RSP_ZCPN, 650,650, -1, -1,-1, {ACT_DEBUG}},
312 {RSP_ZSAU, 650,650,ZSAU_CALL_DELIVERED, -1,-1, {ACT_DEBUG}}, /* some devices don't send this */
314 /* connection established */
315 {RSP_ZSAU, 650,650,ZSAU_ACTIVE, 800,-1, {ACT_CONNECT}}, //FIXME -> DLE1
316 {RSP_ZSAU, 750,750,ZSAU_ACTIVE, 800,-1, {ACT_CONNECT}}, //FIXME -> DLE1
318 {EV_BC_OPEN, 800,800, -1, 800,-1, {ACT_NOTIFY_BC_UP}}, //FIXME new constate + timeout
321 {RSP_ZSAU, 650,650,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT}},
322 {RSP_ZSAU, 750,750,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP}},
323 {RSP_ZSAU, 800,800,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP}},
326 {EV_HUP, -1, -1, -1, -1,-1, {ACT_HUP}}, //FIXME
327 {RSP_INIT, -1, -1,SEQ_HUP, 401, 5, {0}, "+VLS=0\r"}, /* hang up */ //-1,-1?
328 {RSP_OK, 401,401, -1, 402, 5},
329 {RSP_ZVLS, 402,402, 0, 403, 5},
330 {RSP_ZSAU, 403,403,ZSAU_DISCONNECT_REQ, -1,-1, {ACT_DEBUG}}, /* if not remote hup */
331 //{RSP_ZSAU, 403,403,ZSAU_NULL, 401, 0, {ACT_ERROR}}, //DELETE//FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
332 {RSP_ZSAU, 403,403,ZSAU_NULL, 0, 0, {ACT_DISCONNECT}}, //FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
333 {RSP_NODEV, 401,403, -1, 0, 0, {ACT_FAKEHUP}}, //FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
334 {RSP_ERROR, 401,401, -1, 0, 0, {ACT_ABORTHUP}},
335 {EV_TIMEOUT, 401,403, -1, 0, 0, {ACT_ABORTHUP}},
337 {EV_BC_CLOSED, 0, 0, -1, 0,-1, {ACT_NOTIFY_BC_DOWN}}, //FIXME new constate + timeout
340 {RSP_ZBC, 700,700, -1, -1,-1, {0}},
341 {RSP_ZHLC, 700,700, -1, -1,-1, {0}},
342 {RSP_NMBR, 700,700, -1, -1,-1, {0}},
343 {RSP_ZCPN, 700,700, -1, -1,-1, {0}},
344 {RSP_ZCTP, 700,700, -1, -1,-1, {0}},
345 {EV_TIMEOUT, 700,700, -1, 720,720, {ACT_ICALL}},
346 {EV_BC_CLOSED,720,720, -1, 0,-1, {ACT_NOTIFY_BC_DOWN}},
349 {EV_ACCEPT, -1, -1, -1, -1,-1, {ACT_ACCEPT}}, //FIXME
350 {RSP_INIT, 720,720,SEQ_ACCEPT, 721, 5, {ACT_CMD+AT_PROTO}},
351 {RSP_OK, 721,721, -1, 722, 5, {ACT_CMD+AT_ISO}},
352 {RSP_OK, 722,722, -1, 723, 5, {0}, "+VLS=17\r"}, /* set "Endgeraetemodus" */
353 {RSP_OK, 723,723, -1, 724, 5, {0}},
354 {RSP_ZVLS, 724,724, 17, 750,50, {ACT_ACCEPTED}},
355 {RSP_ERROR, 721,729, -1, 0, 0, {ACT_ABORTACCEPT}},
356 {EV_TIMEOUT, 721,729, -1, 0, 0, {ACT_ABORTACCEPT}},
357 {RSP_ZSAU, 700,729,ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT}},
358 {RSP_ZSAU, 700,729,ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT}},
359 {RSP_ZSAU, 700,729,ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT}},
361 {EV_TIMEOUT, 750,750, -1, 0, 0, {ACT_CONNTIMEOUT}},
363 /* B channel closed (general case) */
364 {EV_BC_CLOSED, -1, -1, -1, -1,-1, {ACT_NOTIFY_BC_DOWN}}, //FIXME
367 {EV_PROTO_L2, -1, -1, -1, -1,-1, {ACT_PROTO_L2}}, //FIXME
369 {RSP_ZCON, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
370 {RSP_ZCCR, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
371 {RSP_ZAOC, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
372 {RSP_ZCSTR, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
374 {RSP_ZCAU, -1, -1, -1, -1,-1, {ACT_ZCAU}},
375 {RSP_NONE, -1, -1, -1, -1,-1, {ACT_DEBUG}},
376 {RSP_ANY, -1, -1, -1, -1,-1, {ACT_WARN}},
381 static const struct resp_type_t resp_type[] =
383 /*{"", RSP_EMPTY, RT_NOTHING},*/
384 {"OK", RSP_OK, RT_NOTHING},
385 {"ERROR", RSP_ERROR, RT_NOTHING},
386 {"ZSAU", RSP_ZSAU, RT_ZSAU},
387 {"ZCAU", RSP_ZCAU, RT_ZCAU},
388 {"RING", RSP_RING, RT_RING},
389 {"ZGCI", RSP_ZGCI, RT_NUMBER},
390 {"ZVLS", RSP_ZVLS, RT_NUMBER},
391 {"ZCTP", RSP_ZCTP, RT_NUMBER},
392 {"ZDLE", RSP_ZDLE, RT_NUMBER},
393 {"ZCFGT", RSP_ZCFGT, RT_NUMBER},
394 {"ZCCR", RSP_ZCCR, RT_NUMBER},
395 {"ZMWI", RSP_ZMWI, RT_NUMBER},
396 {"ZHLC", RSP_ZHLC, RT_STRING},
397 {"ZBC", RSP_ZBC, RT_STRING},
398 {"NMBR", RSP_NMBR, RT_STRING},
399 {"ZCPN", RSP_ZCPN, RT_STRING},
400 {"ZCON", RSP_ZCON, RT_STRING},
401 {"ZAOC", RSP_ZAOC, RT_STRING},
402 {"ZCSTR", RSP_ZCSTR, RT_STRING},
403 {"ZCFG", RSP_ZCFG, RT_HEX},
404 {"ZLOG", RSP_ZLOG, RT_NOTHING},
405 {"ZABINFO", RSP_ZABINFO, RT_NOTHING},
406 {"ZSMLSTCHG", RSP_ZSMLSTCHG, RT_NOTHING},
411 * Get integer from char-pointer
413 static int isdn_getnum(char *p)
417 gig_dbg(DEBUG_TRANSCMD, "string: %s", p);
419 while (*p >= '0' && *p <= '9')
420 v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p++) - '0');
422 v = -1; /* invalid Character */
427 * Get integer from char-pointer
429 static int isdn_gethex(char *p)
434 gig_dbg(DEBUG_TRANSCMD, "string: %s", p);
440 if (v > (INT_MAX - 15) / 16)
443 if (c >= '0' && c <= '9')
445 else if (c >= 'a' && c <= 'f')
447 else if (c >= 'A' && c <= 'F')
457 /* retrieve CID from parsed response
458 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
460 static int cid_of_response(char *s)
465 return 0; /* no CID separator */
466 cid = isdn_getnum(s);
468 return 0; /* CID not numeric */
469 if (cid < 1 || cid > 65535)
470 return -1; /* CID out of range */
472 //FIXME is ;<digit>+ at end of non-CID response really impossible?
475 /* This function will be called via task queue from the callback handler.
476 * We received a modem response and have to handle it..
478 void gigaset_handle_modem_response(struct cardstate *cs)
480 unsigned char *argv[MAX_REC_PARAMS + 1];
483 const struct resp_type_t *rt;
486 unsigned next, tail, head;
487 struct event_t *event;
497 /* ignore additional LFs/CRs (M10x config mode or cx100) */
498 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[len]);
501 cs->respdata[len] = 0;
502 gig_dbg(DEBUG_TRANSCMD, "raw string: '%s'", cs->respdata);
503 argv[0] = cs->respdata;
505 if (cs->at_state.getstring) {
506 /* getstring only allowed without cid at the moment */
507 cs->at_state.getstring = 0;
512 for (i = 0; i < len; i++)
513 switch (cs->respdata[i]) {
517 if (params > MAX_REC_PARAMS) {
519 "too many parameters in response\n");
520 /* need last parameter (might be CID) */
523 argv[params++] = cs->respdata + i + 1;
527 cid = params > 1 ? cid_of_response(argv[params-1]) : 0;
529 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
534 for (j = 1; j < params; ++j)
537 gig_dbg(DEBUG_TRANSCMD, "CMD received: %s", argv[0]);
540 gig_dbg(DEBUG_TRANSCMD, "CID: %s", argv[params]);
542 gig_dbg(DEBUG_TRANSCMD, "available params: %d", params - 1);
543 for (j = 1; j < params; j++)
544 gig_dbg(DEBUG_TRANSCMD, "param %d: %s", j, argv[j]);
547 spin_lock_irqsave(&cs->ev_lock, flags);
553 while (curarg < params) {
554 next = (tail + 1) % MAX_EVENTS;
555 if (unlikely(next == head)) {
556 dev_err(cs->dev, "event queue full\n");
560 event = cs->events + tail;
561 event->at_state = NULL;
568 resp_code = RSP_STRING;
569 param_type = RT_STRING;
571 for (rt = resp_type; rt->response; ++rt)
572 if (!strcmp(argv[curarg], rt->response))
576 event->type = RSP_UNKNOWN;
578 "unknown modem response: %s\n",
583 resp_code = rt->resp_code;
584 param_type = rt->type;
588 event->type = resp_code;
590 switch (param_type) {
596 "received RING without CID!\n");
597 event->type = RSP_INVAL;
601 event->parameter = cid;
606 if (curarg >= params) {
607 event->parameter = ZSAU_NONE;
610 if (!strcmp(argv[curarg], "OUTGOING_CALL_PROCEEDING"))
611 event->parameter = ZSAU_OUTGOING_CALL_PROCEEDING;
612 else if (!strcmp(argv[curarg], "CALL_DELIVERED"))
613 event->parameter = ZSAU_CALL_DELIVERED;
614 else if (!strcmp(argv[curarg], "ACTIVE"))
615 event->parameter = ZSAU_ACTIVE;
616 else if (!strcmp(argv[curarg], "DISCONNECT_IND"))
617 event->parameter = ZSAU_DISCONNECT_IND;
618 else if (!strcmp(argv[curarg], "NULL"))
619 event->parameter = ZSAU_NULL;
620 else if (!strcmp(argv[curarg], "DISCONNECT_REQ"))
621 event->parameter = ZSAU_DISCONNECT_REQ;
623 event->parameter = ZSAU_UNKNOWN;
625 "%s: unknown parameter %s after ZSAU\n",
626 __func__, argv[curarg]);
631 if (curarg < params) {
632 event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
634 dev_err(cs->dev, "out of memory\n");
637 gig_dbg(DEBUG_CMD, "string==%s",
638 event->ptr ? (char *) event->ptr : "NULL");
641 event->parameter = -1;
642 if (curarg + 1 < params) {
643 i = isdn_gethex(argv[curarg]);
644 j = isdn_gethex(argv[curarg + 1]);
645 if (i >= 0 && i < 256 && j >= 0 && j < 256)
646 event->parameter = (unsigned) i << 8
654 if (curarg < params) {
655 if (param_type == RT_HEX)
657 isdn_gethex(argv[curarg]);
660 isdn_getnum(argv[curarg]);
663 event->parameter = -1;
664 gig_dbg(DEBUG_CMD, "parameter==%d", event->parameter);
668 if (resp_code == RSP_ZDLE)
669 cs->dle = event->parameter;
676 spin_unlock_irqrestore(&cs->ev_lock, flags);
678 if (curarg != params)
680 "invalid number of processed parameters: %d/%d",
683 EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
686 * process closing of connection associated with given AT state structure
688 static void disconnect(struct at_state_t **at_state_p)
691 struct bc_state *bcs = (*at_state_p)->bcs;
692 struct cardstate *cs = (*at_state_p)->cs;
694 spin_lock_irqsave(&cs->lock, flags);
695 ++(*at_state_p)->seq_index;
697 /* revert to selected idle mode */
699 cs->at_state.pending_commands |= PC_UMMODE;
700 cs->commands_pending = 1;
701 gig_dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
703 spin_unlock_irqrestore(&cs->lock, flags);
706 /* B channel assigned: invoke hardware specific handler */
707 cs->ops->close_bchannel(bcs);
709 /* no B channel assigned: just deallocate */
710 spin_lock_irqsave(&cs->lock, flags);
711 list_del(&(*at_state_p)->list);
714 spin_unlock_irqrestore(&cs->lock, flags);
719 * get a free AT state structure: either one of those associated with the
720 * B channels of the Gigaset device, or if none of those is available,
721 * a newly allocated one with bcs=NULL
722 * The structure should be freed by calling disconnect() after use.
724 static inline struct at_state_t *get_free_channel(struct cardstate *cs,
726 /* cids: >0: siemens-cid
728 -1: no cid assigned yet
733 struct at_state_t *ret;
735 for (i = 0; i < cs->channels; ++i)
736 if (gigaset_get_channel(cs->bcs + i)) {
737 ret = &cs->bcs[i].at_state;
742 spin_lock_irqsave(&cs->lock, flags);
743 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
745 gigaset_at_init(ret, NULL, cs, cid);
746 list_add(&ret->list, &cs->temp_at_states);
748 spin_unlock_irqrestore(&cs->lock, flags);
752 static void init_failed(struct cardstate *cs, int mode)
755 struct at_state_t *at_state;
757 cs->at_state.pending_commands &= ~PC_INIT;
759 cs->mstate = MS_UNINITIALIZED;
760 gigaset_free_channels(cs);
761 for (i = 0; i < cs->channels; ++i) {
762 at_state = &cs->bcs[i].at_state;
763 if (at_state->pending_commands & PC_CID) {
764 at_state->pending_commands &= ~PC_CID;
765 at_state->pending_commands |= PC_NOCID;
766 cs->commands_pending = 1;
771 static void schedule_init(struct cardstate *cs, int state)
773 if (cs->at_state.pending_commands & PC_INIT) {
774 gig_dbg(DEBUG_CMD, "not scheduling PC_INIT again");
778 cs->mode = M_UNKNOWN;
779 gigaset_block_channels(cs);
780 cs->at_state.pending_commands |= PC_INIT;
781 cs->commands_pending = 1;
782 gig_dbg(DEBUG_CMD, "Scheduling PC_INIT");
785 /* Add "AT" to a command, add the cid, dle encode it, send the result to the
787 static void send_command(struct cardstate *cs, const char *cmd, int cid,
788 int dle, gfp_t kmallocflags)
790 size_t cmdlen, buflen;
791 char *cmdpos, *cmdbuf, *cmdtail;
793 cmdlen = strlen(cmd);
794 buflen = 11 + cmdlen;
795 if (unlikely(buflen <= cmdlen)) {
796 dev_err(cs->dev, "integer overflow in buflen\n");
800 cmdbuf = kmalloc(buflen, kmallocflags);
801 if (unlikely(!cmdbuf)) {
802 dev_err(cs->dev, "out of memory\n");
807 cmdtail = cmdpos + cmdlen;
808 memcpy(cmdpos, cmd, cmdlen);
810 if (cid > 0 && cid <= 65535) {
812 *--cmdpos = '0' + cid % 10;
830 cs->ops->write_cmd(cs, cmdpos, cmdlen, NULL);
834 static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
836 struct at_state_t *at_state;
841 return &cs->at_state;
843 for (i = 0; i < cs->channels; ++i)
844 if (cid == cs->bcs[i].at_state.cid)
845 return &cs->bcs[i].at_state;
847 spin_lock_irqsave(&cs->lock, flags);
849 list_for_each_entry(at_state, &cs->temp_at_states, list)
850 if (cid == at_state->cid) {
851 spin_unlock_irqrestore(&cs->lock, flags);
855 spin_unlock_irqrestore(&cs->lock, flags);
860 static void bchannel_down(struct bc_state *bcs)
862 if (bcs->chstate & CHS_B_UP) {
863 bcs->chstate &= ~CHS_B_UP;
864 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
867 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
868 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
869 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
872 gigaset_free_channel(bcs);
874 gigaset_bcs_reinit(bcs);
877 static void bchannel_up(struct bc_state *bcs)
879 if (!(bcs->chstate & CHS_D_UP)) {
880 dev_notice(bcs->cs->dev, "%s: D channel not up\n", __func__);
881 bcs->chstate |= CHS_D_UP;
882 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
885 if (bcs->chstate & CHS_B_UP) {
886 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
891 bcs->chstate |= CHS_B_UP;
892 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
895 static void start_dial(struct at_state_t *at_state, void *data, unsigned seq_index)
897 struct bc_state *bcs = at_state->bcs;
898 struct cardstate *cs = at_state->cs;
902 bcs->chstate |= CHS_NOTIFY_LL;
904 spin_lock_irqsave(&cs->lock, flags);
905 if (at_state->seq_index != seq_index) {
906 spin_unlock_irqrestore(&cs->lock, flags);
909 spin_unlock_irqrestore(&cs->lock, flags);
911 retval = gigaset_isdn_setup_dial(at_state, data);
916 at_state->pending_commands |= PC_CID;
917 gig_dbg(DEBUG_CMD, "Scheduling PC_CID");
918 cs->commands_pending = 1;
922 at_state->pending_commands |= PC_NOCID;
923 gig_dbg(DEBUG_CMD, "Scheduling PC_NOCID");
924 cs->commands_pending = 1;
928 static void start_accept(struct at_state_t *at_state)
930 struct cardstate *cs = at_state->cs;
933 retval = gigaset_isdn_setup_accept(at_state);
936 at_state->pending_commands |= PC_ACCEPT;
937 gig_dbg(DEBUG_CMD, "Scheduling PC_ACCEPT");
938 cs->commands_pending = 1;
941 at_state->pending_commands |= PC_HUP;
942 gig_dbg(DEBUG_CMD, "Scheduling PC_HUP");
943 cs->commands_pending = 1;
947 static void do_start(struct cardstate *cs)
949 gigaset_free_channels(cs);
951 if (cs->mstate != MS_LOCKED)
952 schedule_init(cs, MS_INIT);
955 gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
956 // FIXME: not in locked mode
957 // FIXME 2: only after init sequence
960 wake_up(&cs->waitqueue);
963 static void finish_shutdown(struct cardstate *cs)
965 if (cs->mstate != MS_LOCKED) {
966 cs->mstate = MS_UNINITIALIZED;
967 cs->mode = M_UNKNOWN;
970 /* Tell the LL that the device is not available .. */
973 gigaset_i4l_cmd(cs, ISDN_STAT_STOP);
976 /* The rest is done by cleanup_cs () in user mode. */
978 cs->cmd_result = -ENODEV;
980 wake_up(&cs->waitqueue);
983 static void do_shutdown(struct cardstate *cs)
985 gigaset_block_channels(cs);
987 if (cs->mstate == MS_READY) {
988 cs->mstate = MS_SHUTDOWN;
989 cs->at_state.pending_commands |= PC_SHUTDOWN;
990 cs->commands_pending = 1;
991 gig_dbg(DEBUG_CMD, "Scheduling PC_SHUTDOWN");
996 static void do_stop(struct cardstate *cs)
1000 spin_lock_irqsave(&cs->lock, flags);
1002 spin_unlock_irqrestore(&cs->lock, flags);
1007 /* Entering cid mode or getting a cid failed:
1008 * try to initialize the device and try again.
1010 * channel >= 0: getting cid for the channel failed
1011 * channel < 0: entering cid mode failed
1013 * returns 0 on failure
1015 static int reinit_and_retry(struct cardstate *cs, int channel)
1019 if (--cs->retry_count <= 0)
1022 for (i = 0; i < cs->channels; ++i)
1023 if (cs->bcs[i].at_state.cid > 0)
1028 "Could not enter cid mode. Reinit device and try again.\n");
1031 "Could not get a call id. Reinit device and try again.\n");
1032 cs->bcs[channel].at_state.pending_commands |= PC_CID;
1034 schedule_init(cs, MS_INIT);
1038 static int at_state_invalid(struct cardstate *cs,
1039 struct at_state_t *test_ptr)
1041 unsigned long flags;
1043 struct at_state_t *at_state;
1046 spin_lock_irqsave(&cs->lock, flags);
1048 if (test_ptr == &cs->at_state)
1051 list_for_each_entry(at_state, &cs->temp_at_states, list)
1052 if (at_state == test_ptr)
1055 for (channel = 0; channel < cs->channels; ++channel)
1056 if (&cs->bcs[channel].at_state == test_ptr)
1061 spin_unlock_irqrestore(&cs->lock, flags);
1065 static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
1066 struct at_state_t **p_at_state)
1069 struct at_state_t *at_state = *p_at_state;
1071 retval = gigaset_isdn_icall(at_state);
1076 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
1077 /* --v-- fall through --v-- */
1081 * Device doc says that would reject the call.
1082 * In fact it doesn't.
1084 at_state->pending_commands |= PC_HUP;
1085 cs->commands_pending = 1;
1090 static int do_lock(struct cardstate *cs)
1095 switch (cs->mstate) {
1096 case MS_UNINITIALIZED:
1098 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1099 cs->at_state.pending_commands)
1102 for (i = 0; i < cs->channels; ++i)
1103 if (cs->bcs[i].at_state.pending_commands)
1106 if (!gigaset_get_channels(cs))
1118 cs->mstate = MS_LOCKED;
1119 cs->mode = M_UNKNOWN;
1124 static int do_unlock(struct cardstate *cs)
1126 if (cs->mstate != MS_LOCKED)
1129 cs->mstate = MS_UNINITIALIZED;
1130 cs->mode = M_UNKNOWN;
1131 gigaset_free_channels(cs);
1133 schedule_init(cs, MS_INIT);
1138 static void do_action(int action, struct cardstate *cs,
1139 struct bc_state *bcs,
1140 struct at_state_t **p_at_state, char **pp_command,
1141 int *p_genresp, int *p_resp_code,
1144 struct at_state_t *at_state = *p_at_state;
1145 struct at_state_t *at_state2;
1146 unsigned long flags;
1150 unsigned char *s, *e;
1158 at_state->waiting = 1;
1161 cs->at_state.pending_commands &= ~PC_INIT;
1162 cs->cur_at_seq = SEQ_NONE;
1163 cs->mode = M_UNIMODEM;
1164 spin_lock_irqsave(&cs->lock, flags);
1166 spin_unlock_irqrestore(&cs->lock, flags);
1167 gigaset_free_channels(cs);
1168 cs->mstate = MS_READY;
1171 spin_unlock_irqrestore(&cs->lock, flags);
1172 cs->at_state.pending_commands |= PC_CIDMODE;
1173 cs->commands_pending = 1;
1174 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1177 dev_warn(cs->dev, "Could not initialize the device.\n");
1179 init_failed(cs, M_UNKNOWN);
1180 cs->cur_at_seq = SEQ_NONE;
1182 case ACT_CONFIGMODE:
1183 init_failed(cs, M_CONFIG);
1184 cs->cur_at_seq = SEQ_NONE;
1188 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1189 cs->inbuf[0].inputstate &=
1190 ~(INS_command | INS_DLE_command);
1194 cs->inbuf[0].inputstate =
1195 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1199 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1200 gigaset_free_channels(cs);
1201 cs->mstate = MS_READY;
1204 cs->cur_at_seq = SEQ_NONE;
1207 cs->mode = M_UNIMODEM;
1208 cs->cur_at_seq = SEQ_NONE;
1211 cs->cur_at_seq = SEQ_NONE;
1212 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1213 init_failed(cs, M_UNKNOWN);
1216 if (!reinit_and_retry(cs, -1))
1217 schedule_init(cs, MS_RECOVER);
1220 cs->cur_at_seq = SEQ_NONE;
1221 schedule_init(cs, MS_RECOVER);
1224 /* send "+++" (hangup in unimodem mode) */
1226 cs->ops->write_cmd(cs, "+++", 3, NULL);
1229 /* get fresh AT state structure for new CID */
1230 at_state2 = get_free_channel(cs, ev->parameter);
1233 "RING ignored: could not allocate channel structure\n");
1237 /* initialize AT state structure
1238 * note that bcs may be NULL if no B channel is free
1240 at_state2->ConState = 700;
1241 kfree(at_state2->str_var[STR_NMBR]);
1242 at_state2->str_var[STR_NMBR] = NULL;
1243 kfree(at_state2->str_var[STR_ZCPN]);
1244 at_state2->str_var[STR_ZCPN] = NULL;
1245 kfree(at_state2->str_var[STR_ZBC]);
1246 at_state2->str_var[STR_ZBC] = NULL;
1247 kfree(at_state2->str_var[STR_ZHLC]);
1248 at_state2->str_var[STR_ZHLC] = NULL;
1249 at_state2->int_var[VAR_ZCTP] = -1;
1251 spin_lock_irqsave(&cs->lock, flags);
1252 at_state2->timer_expires = RING_TIMEOUT;
1253 at_state2->timer_active = 1;
1254 spin_unlock_irqrestore(&cs->lock, flags);
1257 handle_icall(cs, bcs, p_at_state);
1260 dev_warn(cs->dev, "Could not shut down the device.\n");
1264 cs->cur_at_seq = SEQ_NONE;
1265 finish_shutdown(cs);
1268 if (cs->onechannel) {
1269 at_state->pending_commands |= PC_DLE1;
1270 cs->commands_pending = 1;
1273 bcs->chstate |= CHS_D_UP;
1274 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
1275 cs->ops->init_bchannel(bcs);
1278 cs->cur_at_seq = SEQ_NONE;
1279 bcs = cs->bcs + cs->curchannel;
1281 bcs->chstate |= CHS_D_UP;
1282 gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
1283 cs->ops->init_bchannel(bcs);
1286 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1288 case ACT_DISCONNECT:
1289 cs->cur_at_seq = SEQ_NONE;
1291 if (bcs && cs->onechannel && cs->dle) {
1292 /* Check for other open channels not needed:
1293 * DLE only used for M10x with one B channel.
1295 at_state->pending_commands |= PC_DLE0;
1296 cs->commands_pending = 1;
1298 disconnect(p_at_state);
1301 at_state->int_var[VAR_ZDLE] = 0;
1305 cs->cur_at_seq = SEQ_NONE;
1306 at_state2 = &cs->bcs[cs->curchannel].at_state;
1307 disconnect(&at_state2);
1310 cs->cur_at_seq = SEQ_NONE;
1311 dev_warn(cs->dev, "Could not hang up.\n");
1313 if (bcs && cs->onechannel)
1314 at_state->pending_commands |= PC_DLE0;
1316 disconnect(p_at_state);
1317 schedule_init(cs, MS_RECOVER);
1320 cs->cur_at_seq = SEQ_NONE;
1321 dev_warn(cs->dev, "Could not leave DLE mode.\n");
1322 at_state2 = &cs->bcs[cs->curchannel].at_state;
1323 disconnect(&at_state2);
1324 schedule_init(cs, MS_RECOVER);
1327 cs->cur_at_seq = SEQ_NONE;
1329 "Could not enter DLE mode. Trying to hang up.\n");
1330 channel = cs->curchannel;
1331 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
1332 cs->commands_pending = 1;
1335 case ACT_CID: /* got cid; start dialing */
1336 cs->cur_at_seq = SEQ_NONE;
1337 channel = cs->curchannel;
1338 if (ev->parameter > 0 && ev->parameter <= 65535) {
1339 cs->bcs[channel].at_state.cid = ev->parameter;
1340 cs->bcs[channel].at_state.pending_commands |=
1342 cs->commands_pending = 1;
1347 cs->cur_at_seq = SEQ_NONE;
1348 channel = cs->curchannel;
1349 if (!reinit_and_retry(cs, channel)) {
1351 "Could not get a call ID. Cannot dial.\n");
1352 at_state2 = &cs->bcs[channel].at_state;
1353 disconnect(&at_state2);
1357 cs->cur_at_seq = SEQ_NONE;
1358 at_state2 = &cs->bcs[cs->curchannel].at_state;
1359 disconnect(&at_state2);
1364 cs->cur_at_seq = SEQ_NONE;
1367 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL processing */
1368 disconnect(p_at_state);
1371 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1372 cs->cur_at_seq = SEQ_NONE;
1373 at_state->pending_commands |= PC_HUP;
1374 cs->commands_pending = 1;
1377 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1378 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1379 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1380 at_state->pending_commands |= PC_HUP;
1381 cs->commands_pending = 1;
1383 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
1384 are not handled properly anymore */
1385 at_state->getstring = 1;
1390 *p_resp_code = RSP_ERROR;
1395 if (!strcmp(s, "OK")) {
1397 *p_resp_code = RSP_ERROR;
1401 for (i = 0; i < 4; ++i) {
1402 val = simple_strtoul(s, (char **) &e, 10);
1403 if (val > INT_MAX || e == s)
1408 } else if (*e != '.')
1416 *p_resp_code = RSP_ERROR;
1419 /*at_state->getstring = 1;*/
1423 if (cs->gotfwver == 0) {
1426 "firmware version %02d.%03d.%02d.%02d",
1427 cs->fwver[0], cs->fwver[1],
1428 cs->fwver[2], cs->fwver[3]);
1434 dev_err(cs->dev, "could not read firmware version.\n");
1436 #ifdef CONFIG_GIGASET_DEBUG
1439 *p_resp_code = RSP_ERROR;
1443 static int count = 3; //2; //1;
1445 *p_resp_code = count ? RSP_ERROR : RSP_OK;
1452 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
1453 __func__, ev->type, at_state->ConState);
1456 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1457 __func__, ev->type, at_state->ConState);
1460 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1461 ev->parameter, at_state->ConState);
1464 /* events from the LL */
1466 start_dial(at_state, ev->ptr, ev->parameter);
1469 start_accept(at_state);
1472 gig_dbg(DEBUG_CMD, "set protocol to %u",
1473 (unsigned) ev->parameter);
1474 at_state->bcs->proto2 = ev->parameter;
1477 at_state->pending_commands |= PC_HUP;
1478 cs->commands_pending = 1;
1479 gig_dbg(DEBUG_CMD, "Scheduling PC_HUP");
1482 /* hotplug events */
1490 /* events from the interface */ // FIXME without ACT_xxxx?
1492 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1494 wake_up(&cs->waitqueue);
1497 if (ev->parameter != 0)
1498 cs->cmd_result = -EINVAL;
1499 else if (cs->gotfwver != 1) {
1500 cs->cmd_result = -ENOENT;
1502 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1506 wake_up(&cs->waitqueue);
1509 /* events from the proc file system */ // FIXME without ACT_xxxx?
1510 case ACT_PROC_CIDMODE:
1511 spin_lock_irqsave(&cs->lock, flags);
1512 if (ev->parameter != cs->cidmode) {
1513 cs->cidmode = ev->parameter;
1514 if (ev->parameter) {
1515 cs->at_state.pending_commands |= PC_CIDMODE;
1516 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1518 cs->at_state.pending_commands |= PC_UMMODE;
1519 gig_dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
1521 cs->commands_pending = 1;
1523 spin_unlock_irqrestore(&cs->lock, flags);
1525 wake_up(&cs->waitqueue);
1528 /* events from the hardware drivers */
1529 case ACT_NOTIFY_BC_DOWN:
1532 case ACT_NOTIFY_BC_UP:
1541 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1542 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1545 *p_resp_code = RSP_NULL;
1548 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
1552 /* State machine to do the calling and hangup procedure */
1553 static void process_event(struct cardstate *cs, struct event_t *ev)
1555 struct bc_state *bcs;
1556 char *p_command = NULL;
1557 struct reply_t *rep;
1560 int resp_code = RSP_ERROR;
1562 struct at_state_t *at_state;
1565 unsigned long flags;
1568 at_state = at_state_from_cid(cs, ev->cid);
1570 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
1575 at_state = ev->at_state;
1576 if (at_state_invalid(cs, at_state)) {
1577 gig_dbg(DEBUG_ANY, "event for invalid at_state %p",
1583 gig_dbg(DEBUG_CMD, "connection state %d, event %d",
1584 at_state->ConState, ev->type);
1586 bcs = at_state->bcs;
1587 sendcid = at_state->cid;
1589 /* Setting the pointer to the dial array */
1590 rep = at_state->replystruct;
1592 spin_lock_irqsave(&cs->lock, flags);
1593 if (ev->type == EV_TIMEOUT) {
1594 if (ev->parameter != at_state->timer_index
1595 || !at_state->timer_active) {
1596 ev->type = RSP_NONE; /* old timeout */
1597 gig_dbg(DEBUG_ANY, "old timeout");
1598 } else if (!at_state->waiting)
1599 gig_dbg(DEBUG_ANY, "timeout occurred");
1601 gig_dbg(DEBUG_ANY, "stopped waiting");
1603 spin_unlock_irqrestore(&cs->lock, flags);
1605 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1606 or at_state->str_var[STR_XXXX], set it */
1607 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1608 index = ev->type - RSP_VAR;
1609 at_state->int_var[index] = ev->parameter;
1610 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1611 index = ev->type - RSP_STR;
1612 kfree(at_state->str_var[index]);
1613 at_state->str_var[index] = ev->ptr;
1614 ev->ptr = NULL; /* prevent process_events() from
1618 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1619 at_state->getstring = 0;
1621 /* Search row in dial array which matches modem response and current
1624 rcode = rep->resp_code;
1625 if (rcode == RSP_LAST) {
1626 /* found nothing...*/
1627 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1628 "resp_code %d in ConState %d!\n",
1629 __func__, ev->type, at_state->ConState);
1632 if ((rcode == RSP_ANY || rcode == ev->type)
1633 && ((int) at_state->ConState >= rep->min_ConState)
1634 && (rep->max_ConState < 0
1635 || (int) at_state->ConState <= rep->max_ConState)
1636 && (rep->parameter < 0 || rep->parameter == ev->parameter))
1640 p_command = rep->command;
1642 at_state->waiting = 0;
1643 for (curact = 0; curact < MAXACT; ++curact) {
1644 /* The row tells us what we should do ..
1646 do_action(rep->action[curact], cs, bcs, &at_state, &p_command, &genresp, &resp_code, ev);
1648 break; /* may be freed after disconnect */
1652 /* Jump to the next con-state regarding the array */
1653 if (rep->new_ConState >= 0)
1654 at_state->ConState = rep->new_ConState;
1657 spin_lock_irqsave(&cs->lock, flags);
1658 at_state->timer_expires = 0; //FIXME
1659 at_state->timer_active = 0; //FIXME
1660 spin_unlock_irqrestore(&cs->lock, flags);
1661 gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1663 /* Send command to modem if not NULL... */
1664 if (p_command/*rep->command*/) {
1666 send_command(cs, p_command,
1670 gigaset_add_event(cs, at_state,
1675 spin_lock_irqsave(&cs->lock, flags);
1676 if (!rep->timeout) {
1677 at_state->timer_expires = 0;
1678 at_state->timer_active = 0;
1679 } else if (rep->timeout > 0) { /* new timeout */
1680 at_state->timer_expires = rep->timeout * 10;
1681 at_state->timer_active = 1;
1682 ++at_state->timer_index;
1684 spin_unlock_irqrestore(&cs->lock, flags);
1689 static void schedule_sequence(struct cardstate *cs,
1690 struct at_state_t *at_state, int sequence)
1692 cs->cur_at_seq = sequence;
1693 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1696 static void process_command_flags(struct cardstate *cs)
1698 struct at_state_t *at_state = NULL;
1699 struct bc_state *bcs;
1702 unsigned long flags;
1704 cs->commands_pending = 0;
1706 if (cs->cur_at_seq) {
1707 gig_dbg(DEBUG_CMD, "not searching scheduled commands: busy");
1711 gig_dbg(DEBUG_CMD, "searching scheduled commands");
1713 sequence = SEQ_NONE;
1715 /* clear pending_commands and hangup channels on shutdown */
1716 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1717 cs->at_state.pending_commands &= ~PC_CIDMODE;
1718 for (i = 0; i < cs->channels; ++i) {
1720 at_state = &bcs->at_state;
1721 at_state->pending_commands &=
1722 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1723 if (at_state->cid > 0)
1724 at_state->pending_commands |= PC_HUP;
1725 if (at_state->pending_commands & PC_CID) {
1726 at_state->pending_commands |= PC_NOCID;
1727 at_state->pending_commands &= ~PC_CID;
1732 /* clear pending_commands and hangup channels on reset */
1733 if (cs->at_state.pending_commands & PC_INIT) {
1734 cs->at_state.pending_commands &= ~PC_CIDMODE;
1735 for (i = 0; i < cs->channels; ++i) {
1737 at_state = &bcs->at_state;
1738 at_state->pending_commands &=
1739 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1740 if (at_state->cid > 0)
1741 at_state->pending_commands |= PC_HUP;
1742 if (cs->mstate == MS_RECOVER) {
1743 if (at_state->pending_commands & PC_CID) {
1744 at_state->pending_commands |= PC_NOCID;
1745 at_state->pending_commands &= ~PC_CID;
1751 /* only switch back to unimodem mode, if no commands are pending and no channels are up */
1752 spin_lock_irqsave(&cs->lock, flags);
1753 if (cs->at_state.pending_commands == PC_UMMODE
1755 && list_empty(&cs->temp_at_states)
1756 && cs->mode == M_CID) {
1757 sequence = SEQ_UMMODE;
1758 at_state = &cs->at_state;
1759 for (i = 0; i < cs->channels; ++i) {
1761 if (bcs->at_state.pending_commands ||
1762 bcs->at_state.cid > 0) {
1763 sequence = SEQ_NONE;
1768 spin_unlock_irqrestore(&cs->lock, flags);
1769 cs->at_state.pending_commands &= ~PC_UMMODE;
1770 if (sequence != SEQ_NONE) {
1771 schedule_sequence(cs, at_state, sequence);
1775 for (i = 0; i < cs->channels; ++i) {
1777 if (bcs->at_state.pending_commands & PC_HUP) {
1778 bcs->at_state.pending_commands &= ~PC_HUP;
1779 if (bcs->at_state.pending_commands & PC_CID) {
1780 /* not yet dialing: PC_NOCID is sufficient */
1781 bcs->at_state.pending_commands |= PC_NOCID;
1782 bcs->at_state.pending_commands &= ~PC_CID;
1784 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1788 if (bcs->at_state.pending_commands & PC_NOCID) {
1789 bcs->at_state.pending_commands &= ~PC_NOCID;
1790 cs->curchannel = bcs->channel;
1791 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1793 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1794 bcs->at_state.pending_commands &= ~PC_DLE0;
1795 cs->curchannel = bcs->channel;
1796 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1801 list_for_each_entry(at_state, &cs->temp_at_states, list)
1802 if (at_state->pending_commands & PC_HUP) {
1803 at_state->pending_commands &= ~PC_HUP;
1804 schedule_sequence(cs, at_state, SEQ_HUP);
1808 if (cs->at_state.pending_commands & PC_INIT) {
1809 cs->at_state.pending_commands &= ~PC_INIT;
1810 cs->dle = 0; //FIXME
1811 cs->inbuf->inputstate = INS_command;
1812 //FIXME reset card state (or -> LOCK0)?
1813 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1816 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1817 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1818 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1821 if (cs->at_state.pending_commands & PC_CIDMODE) {
1822 cs->at_state.pending_commands &= ~PC_CIDMODE;
1823 if (cs->mode == M_UNIMODEM) {
1824 cs->retry_count = 1;
1825 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1830 for (i = 0; i < cs->channels; ++i) {
1832 if (bcs->at_state.pending_commands & PC_DLE1) {
1833 bcs->at_state.pending_commands &= ~PC_DLE1;
1834 cs->curchannel = bcs->channel;
1835 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1838 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1839 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1840 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1843 if (bcs->at_state.pending_commands & PC_DIAL) {
1844 bcs->at_state.pending_commands &= ~PC_DIAL;
1845 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1848 if (bcs->at_state.pending_commands & PC_CID) {
1851 cs->at_state.pending_commands |= PC_CIDMODE;
1852 gig_dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
1853 cs->commands_pending = 1;
1855 #ifdef GIG_MAYINITONDIAL
1857 schedule_init(cs, MS_INIT);
1861 bcs->at_state.pending_commands &= ~PC_CID;
1862 cs->curchannel = bcs->channel;
1864 cs->retry_count = 2;
1866 cs->retry_count = 1;
1868 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1874 static void process_events(struct cardstate *cs)
1877 unsigned head, tail;
1879 int check_flags = 0;
1881 unsigned long flags;
1883 spin_lock_irqsave(&cs->ev_lock, flags);
1886 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
1889 if (!check_flags && !cs->commands_pending)
1892 spin_unlock_irqrestore(&cs->ev_lock, flags);
1893 process_command_flags(cs);
1894 spin_lock_irqsave(&cs->ev_lock, flags);
1897 if (!cs->commands_pending)
1903 ev = cs->events + head;
1904 was_busy = cs->cur_at_seq != SEQ_NONE;
1905 spin_unlock_irqrestore(&cs->ev_lock, flags);
1906 process_event(cs, ev);
1907 spin_lock_irqsave(&cs->ev_lock, flags);
1910 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1913 head = (head + 1) % MAX_EVENTS;
1917 spin_unlock_irqrestore(&cs->ev_lock, flags);
1919 if (i == 2 * MAX_EVENTS) {
1921 "infinite loop in process_events; aborting.\n");
1925 /* tasklet scheduled on any event received from the Gigaset device
1927 * data ISDN controller state structure
1929 void gigaset_handle_event(unsigned long data)
1931 struct cardstate *cs = (struct cardstate *) data;
1933 /* handle incoming data on control/common channel */
1934 if (cs->inbuf->head != cs->inbuf->tail) {
1935 gig_dbg(DEBUG_INTR, "processing new data");
1936 cs->ops->handle_input(cs->inbuf);