1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Stuff used by all variants of the driver
5 * Copyright (c) 2001 by Stefan Eilers,
6 * Hansjoerg Lipp <hjlipp@web.de>,
7 * Tilman Schmidt <tilman@imap.cc>.
9 * =====================================================================
10 * =====================================================================
13 #include <linux/export.h>
16 /* ========================================================== */
17 /* bit masks for pending commands */
23 #define PC_SHUTDOWN 0x020
24 #define PC_ACCEPT 0x040
26 #define PC_NOCID 0x100
27 #define PC_CIDMODE 0x200
28 #define PC_UMMODE 0x400
30 /* types of modem responses */
38 /* Possible ASCII responses */
46 /* responses with values to store in at_state */
49 #define RSP_ZSAU (RSP_VAR + VAR_ZSAU)
50 #define RSP_ZDLE (RSP_VAR + VAR_ZDLE)
51 #define RSP_ZCTP (RSP_VAR + VAR_ZCTP)
53 #define RSP_STR (RSP_VAR + VAR_NUM)
54 #define RSP_NMBR (RSP_STR + STR_NMBR)
55 #define RSP_ZCPN (RSP_STR + STR_ZCPN)
56 #define RSP_ZCON (RSP_STR + STR_ZCON)
57 #define RSP_ZBC (RSP_STR + STR_ZBC)
58 #define RSP_ZHLC (RSP_STR + STR_ZHLC)
60 #define RSP_WRONG_CID -2 /* unknown cid in cmd */
61 #define RSP_INVAL -6 /* invalid response */
62 #define RSP_NODEV -9 /* device not connected */
65 #define RSP_STRING -20
71 /* actions for process_response */
75 #define ACT_FAILINIT 3
76 #define ACT_HUPMODEM 4
77 #define ACT_CONFIGMODE 5
81 #define ACT_FAILDLE0 9
82 #define ACT_FAILDLE1 10
85 #define ACT_FAILCID 13
87 #define ACT_FAILSDOWN 15
90 #define ACT_DIALING 18
91 #define ACT_ABORTDIAL 19
92 #define ACT_DISCONNECT 20
93 #define ACT_CONNECT 21
94 #define ACT_REMOTEREJECT 22
95 #define ACT_CONNTIMEOUT 23
96 #define ACT_REMOTEHUP 24
97 #define ACT_ABORTHUP 25
99 #define ACT_ACCEPTED 27
100 #define ACT_ABORTACCEPT 28
101 #define ACT_TIMEOUT 29
102 #define ACT_GETSTRING 30
103 #define ACT_SETVER 31
104 #define ACT_FAILVER 32
105 #define ACT_GOTVER 33
108 #define ACT_ABORTCID 36
110 #define ACT_NOTIFY_BC_DOWN 38
111 #define ACT_NOTIFY_BC_UP 39
113 #define ACT_ACCEPT 41
115 #define ACT_IF_LOCK 44
118 #define ACT_FAKEDLE0 47
119 #define ACT_FAKEHUP 48
120 #define ACT_FAKESDOWN 49
121 #define ACT_SHUTDOWN 50
122 #define ACT_PROC_CIDMODE 51
123 #define ACT_UMODESET 52
124 #define ACT_FAILUMODE 53
125 #define ACT_CMODESET 54
126 #define ACT_FAILCMODE 55
127 #define ACT_IF_VER 56
130 /* at command sequences */
136 #define SEQ_NOCID 350
139 #define SEQ_ACCEPT 720
140 #define SEQ_SHUTDOWN 500
141 #define SEQ_CIDMODE 10
142 #define SEQ_UMMODE 11
145 /* 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid),
146 * 400: hup, 500: reset, 600: dial, 700: ring */
147 struct reply_t gigaset_tab_nocid[] =
149 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
152 /* initialize device, set cid mode if possible */
153 {RSP_INIT, -1, -1, SEQ_INIT, 100, 1, {ACT_TIMEOUT} },
155 {EV_TIMEOUT, 100, 100, -1, 101, 3, {0}, "Z\r"},
156 {RSP_OK, 101, 103, -1, 120, 5, {ACT_GETSTRING},
159 {EV_TIMEOUT, 101, 101, -1, 102, 5, {0}, "Z\r"},
160 {RSP_ERROR, 101, 101, -1, 102, 5, {0}, "Z\r"},
162 {EV_TIMEOUT, 102, 102, -1, 108, 5, {ACT_SETDLE1},
164 {RSP_OK, 108, 108, -1, 104, -1},
165 {RSP_ZDLE, 104, 104, 0, 103, 5, {0}, "Z\r"},
166 {EV_TIMEOUT, 104, 104, -1, 0, 0, {ACT_FAILINIT} },
167 {RSP_ERROR, 108, 108, -1, 0, 0, {ACT_FAILINIT} },
169 {EV_TIMEOUT, 108, 108, -1, 105, 2, {ACT_SETDLE0,
172 {EV_TIMEOUT, 105, 105, -1, 103, 5, {0}, "Z\r"},
174 {RSP_ERROR, 102, 102, -1, 107, 5, {0}, "^GETPRE\r"},
175 {RSP_OK, 107, 107, -1, 0, 0, {ACT_CONFIGMODE} },
176 {RSP_ERROR, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
177 {EV_TIMEOUT, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
179 {RSP_ERROR, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
180 {EV_TIMEOUT, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
182 {RSP_STRING, 120, 120, -1, 121, -1, {ACT_SETVER} },
184 {EV_TIMEOUT, 120, 121, -1, 0, 0, {ACT_FAILVER,
186 {RSP_ERROR, 120, 121, -1, 0, 0, {ACT_FAILVER,
188 {RSP_OK, 121, 121, -1, 0, 0, {ACT_GOTVER,
190 {RSP_NONE, 121, 121, -1, 120, 0, {ACT_GETSTRING} },
193 {RSP_INIT, 0, 0, SEQ_DLE0, 201, 5, {0}, "^SDLE=0\r"},
194 {RSP_OK, 201, 201, -1, 202, -1},
195 {RSP_ZDLE, 202, 202, 0, 0, 0, {ACT_DLE0} },
196 {RSP_NODEV, 200, 249, -1, 0, 0, {ACT_FAKEDLE0} },
197 {RSP_ERROR, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
198 {EV_TIMEOUT, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
201 {RSP_INIT, 0, 0, SEQ_DLE1, 251, 5, {0}, "^SDLE=1\r"},
202 {RSP_OK, 251, 251, -1, 252, -1},
203 {RSP_ZDLE, 252, 252, 1, 0, 0, {ACT_DLE1} },
204 {RSP_ERROR, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
205 {EV_TIMEOUT, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
208 {RSP_RING, -1, -1, -1, -1, -1, {ACT_RING} },
211 {RSP_INIT, 0, 0, SEQ_CID, 301, 5, {0}, "^SGCI?\r"},
212 {RSP_OK, 301, 301, -1, 302, -1},
213 {RSP_ZGCI, 302, 302, -1, 0, 0, {ACT_CID} },
214 {RSP_ERROR, 301, 349, -1, 0, 0, {ACT_FAILCID} },
215 {EV_TIMEOUT, 301, 349, -1, 0, 0, {ACT_FAILCID} },
218 {RSP_INIT, 0, 0, SEQ_CIDMODE, 150, 5, {0}, "^SGCI=1\r"},
219 {RSP_OK, 150, 150, -1, 0, 0, {ACT_CMODESET} },
220 {RSP_ERROR, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
221 {EV_TIMEOUT, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
224 {RSP_INIT, 0, 0, SEQ_UMMODE, 160, 5, {0}, "Z\r"},
225 {RSP_OK, 160, 160, -1, 0, 0, {ACT_UMODESET} },
226 {RSP_ERROR, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
227 {EV_TIMEOUT, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
229 /* abort getting cid */
230 {RSP_INIT, 0, 0, SEQ_NOCID, 0, 0, {ACT_ABORTCID} },
233 {RSP_INIT, 0, 0, SEQ_SHUTDOWN, 504, 5, {0}, "Z\r"},
234 {RSP_OK, 504, 504, -1, 0, 0, {ACT_SDOWN} },
235 {RSP_ERROR, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
236 {EV_TIMEOUT, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
237 {RSP_NODEV, 501, 599, -1, 0, 0, {ACT_FAKESDOWN} },
239 {EV_PROC_CIDMODE, -1, -1, -1, -1, -1, {ACT_PROC_CIDMODE} },
240 {EV_IF_LOCK, -1, -1, -1, -1, -1, {ACT_IF_LOCK} },
241 {EV_IF_VER, -1, -1, -1, -1, -1, {ACT_IF_VER} },
242 {EV_START, -1, -1, -1, -1, -1, {ACT_START} },
243 {EV_STOP, -1, -1, -1, -1, -1, {ACT_STOP} },
244 {EV_SHUTDOWN, -1, -1, -1, -1, -1, {ACT_SHUTDOWN} },
247 {RSP_ERROR, -1, -1, -1, -1, -1, {ACT_ERROR} },
248 {RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
249 {RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
250 {RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
254 /* 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring,
255 * 400: hup, 750: accepted icall */
256 struct reply_t gigaset_tab_cid[] =
258 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
262 {EV_DIAL, -1, -1, -1, -1, -1, {ACT_DIAL} },
263 {RSP_INIT, 0, 0, SEQ_DIAL, 601, 5, {ACT_CMD + AT_BC} },
264 {RSP_OK, 601, 601, -1, 603, 5, {ACT_CMD + AT_PROTO} },
265 {RSP_OK, 603, 603, -1, 604, 5, {ACT_CMD + AT_TYPE} },
266 {RSP_OK, 604, 604, -1, 605, 5, {ACT_CMD + AT_MSN} },
267 {RSP_NULL, 605, 605, -1, 606, 5, {ACT_CMD + AT_CLIP} },
268 {RSP_OK, 605, 605, -1, 606, 5, {ACT_CMD + AT_CLIP} },
269 {RSP_NULL, 606, 606, -1, 607, 5, {ACT_CMD + AT_ISO} },
270 {RSP_OK, 606, 606, -1, 607, 5, {ACT_CMD + AT_ISO} },
271 {RSP_OK, 607, 607, -1, 608, 5, {0}, "+VLS=17\r"},
272 {RSP_OK, 608, 608, -1, 609, -1},
273 {RSP_ZSAU, 609, 609, ZSAU_PROCEEDING, 610, 5, {ACT_CMD + AT_DIAL} },
274 {RSP_OK, 610, 610, -1, 650, 0, {ACT_DIALING} },
276 {RSP_ERROR, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
277 {EV_TIMEOUT, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
279 /* optional dialing responses */
280 {EV_BC_OPEN, 650, 650, -1, 651, -1},
281 {RSP_ZVLS, 609, 651, 17, -1, -1, {ACT_DEBUG} },
282 {RSP_ZCTP, 610, 651, -1, -1, -1, {ACT_DEBUG} },
283 {RSP_ZCPN, 610, 651, -1, -1, -1, {ACT_DEBUG} },
284 {RSP_ZSAU, 650, 651, ZSAU_CALL_DELIVERED, -1, -1, {ACT_DEBUG} },
287 {RSP_ZSAU, 650, 650, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
288 {RSP_ZSAU, 651, 651, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
290 {RSP_ZSAU, 750, 750, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
291 {RSP_ZSAU, 751, 751, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
293 {EV_BC_OPEN, 800, 800, -1, 800, -1, {ACT_NOTIFY_BC_UP} },
296 {RSP_ZSAU, 650, 651, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT} },
297 {RSP_ZSAU, 750, 751, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
298 {RSP_ZSAU, 800, 800, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
301 {EV_HUP, -1, -1, -1, -1, -1, {ACT_HUP} },
302 {RSP_INIT, -1, -1, SEQ_HUP, 401, 5, {0}, "+VLS=0\r"},
303 {RSP_OK, 401, 401, -1, 402, 5},
304 {RSP_ZVLS, 402, 402, 0, 403, 5},
305 {RSP_ZSAU, 403, 403, ZSAU_DISCONNECT_REQ, -1, -1, {ACT_DEBUG} },
306 {RSP_ZSAU, 403, 403, ZSAU_NULL, 0, 0, {ACT_DISCONNECT} },
307 {RSP_NODEV, 401, 403, -1, 0, 0, {ACT_FAKEHUP} },
308 {RSP_ERROR, 401, 401, -1, 0, 0, {ACT_ABORTHUP} },
309 {EV_TIMEOUT, 401, 403, -1, 0, 0, {ACT_ABORTHUP} },
311 {EV_BC_CLOSED, 0, 0, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
314 {RSP_ZBC, 700, 700, -1, -1, -1, {0} },
315 {RSP_ZHLC, 700, 700, -1, -1, -1, {0} },
316 {RSP_NMBR, 700, 700, -1, -1, -1, {0} },
317 {RSP_ZCPN, 700, 700, -1, -1, -1, {0} },
318 {RSP_ZCTP, 700, 700, -1, -1, -1, {0} },
319 {EV_TIMEOUT, 700, 700, -1, 720, 720, {ACT_ICALL} },
320 {EV_BC_CLOSED, 720, 720, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
323 {EV_ACCEPT, -1, -1, -1, -1, -1, {ACT_ACCEPT} },
324 {RSP_INIT, 720, 720, SEQ_ACCEPT, 721, 5, {ACT_CMD + AT_PROTO} },
325 {RSP_OK, 721, 721, -1, 722, 5, {ACT_CMD + AT_ISO} },
326 {RSP_OK, 722, 722, -1, 723, 5, {0}, "+VLS=17\r"},
327 {RSP_OK, 723, 723, -1, 724, 5, {0} },
328 {RSP_ZVLS, 724, 724, 17, 750, 50, {ACT_ACCEPTED} },
329 {RSP_ERROR, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
330 {EV_TIMEOUT, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
331 {RSP_ZSAU, 700, 729, ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT} },
332 {RSP_ZSAU, 700, 729, ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT} },
333 {RSP_ZSAU, 700, 729, ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT} },
335 {EV_BC_OPEN, 750, 750, -1, 751, -1},
336 {EV_TIMEOUT, 750, 751, -1, 0, 0, {ACT_CONNTIMEOUT} },
338 /* B channel closed (general case) */
339 {EV_BC_CLOSED, -1, -1, -1, -1, -1, {ACT_NOTIFY_BC_DOWN} },
342 {RSP_ZCON, -1, -1, -1, -1, -1, {ACT_DEBUG} },
343 {RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
344 {RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
345 {RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
350 static const struct resp_type_t {
357 {"OK", RSP_OK, RT_NOTHING},
358 {"ERROR", RSP_ERROR, RT_NOTHING},
359 {"ZSAU", RSP_ZSAU, RT_ZSAU},
360 {"ZCAU", RSP_ZCAU, RT_ZCAU},
361 {"RING", RSP_RING, RT_RING},
362 {"ZGCI", RSP_ZGCI, RT_NUMBER},
363 {"ZVLS", RSP_ZVLS, RT_NUMBER},
364 {"ZCTP", RSP_ZCTP, RT_NUMBER},
365 {"ZDLE", RSP_ZDLE, RT_NUMBER},
366 {"ZHLC", RSP_ZHLC, RT_STRING},
367 {"ZBC", RSP_ZBC, RT_STRING},
368 {"NMBR", RSP_NMBR, RT_STRING},
369 {"ZCPN", RSP_ZCPN, RT_STRING},
370 {"ZCON", RSP_ZCON, RT_STRING},
374 static const struct zsau_resp_t {
380 {"OUTGOING_CALL_PROCEEDING", ZSAU_PROCEEDING},
381 {"CALL_DELIVERED", ZSAU_CALL_DELIVERED},
382 {"ACTIVE", ZSAU_ACTIVE},
383 {"DISCONNECT_IND", ZSAU_DISCONNECT_IND},
385 {"DISCONNECT_REQ", ZSAU_DISCONNECT_REQ},
389 /* check for and remove fixed string prefix
390 * If s starts with prefix terminated by a non-alphanumeric character,
391 * return pointer to the first character after that, otherwise return NULL.
393 static char *skip_prefix(char *s, const char *prefix)
396 if (*s++ != *prefix++)
403 /* queue event with CID */
404 static void add_cid_event(struct cardstate *cs, int cid, int type,
405 void *ptr, int parameter)
409 struct event_t *event;
411 gig_dbg(DEBUG_EVENT, "queueing event %d for cid %d", type, cid);
413 spin_lock_irqsave(&cs->ev_lock, flags);
416 next = (tail + 1) % MAX_EVENTS;
417 if (unlikely(next == cs->ev_head)) {
418 dev_err(cs->dev, "event queue full\n");
421 event = cs->events + tail;
426 event->parameter = parameter;
427 event->at_state = NULL;
431 spin_unlock_irqrestore(&cs->ev_lock, flags);
435 * gigaset_handle_modem_response() - process received modem response
436 * @cs: device descriptor structure.
438 * Called by asyncdata/isocdata if a block of data received from the
439 * device must be processed as a modem command response. The data is
440 * already in the cs structure.
442 void gigaset_handle_modem_response(struct cardstate *cs)
444 char *eoc, *psep, *ptr;
445 const struct resp_type_t *rt;
446 const struct zsau_resp_t *zr;
451 /* ignore additional LFs/CRs (M10x config mode or cx100) */
452 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[0]);
455 cs->respdata[cs->cbytes] = 0;
457 if (cs->at_state.getstring) {
458 /* state machine wants next line verbatim */
459 cs->at_state.getstring = 0;
460 ptr = kstrdup(cs->respdata, GFP_ATOMIC);
461 gig_dbg(DEBUG_EVENT, "string==%s", ptr ? ptr : "NULL");
462 add_cid_event(cs, 0, RSP_STRING, ptr, 0);
466 /* look up response type */
467 for (rt = resp_type; rt->response; ++rt) {
468 eoc = skip_prefix(cs->respdata, rt->response);
473 add_cid_event(cs, 0, RSP_NONE, NULL, 0);
474 gig_dbg(DEBUG_EVENT, "unknown modem response: '%s'\n",
480 psep = strrchr(cs->respdata, ';');
482 !kstrtoint(psep + 1, 10, &cid) &&
483 cid >= 1 && cid <= 65535) {
484 /* valid CID: chop it off */
487 /* no valid CID: leave unchanged */
491 gig_dbg(DEBUG_EVENT, "CMD received: %s", cs->respdata);
493 gig_dbg(DEBUG_EVENT, "CID: %d", cid);
497 /* check parameter separator */
499 goto bad_param; /* extra parameter */
501 add_cid_event(cs, cid, rt->resp_code, NULL, 0);
505 /* check parameter separator */
507 eoc = NULL; /* no parameter */
508 else if (*eoc++ != ',')
511 add_cid_event(cs, 0, rt->resp_code, NULL, cid);
513 /* process parameters as individual responses */
515 /* look up parameter type */
517 for (rt = resp_type; rt->response; ++rt) {
518 psep = skip_prefix(eoc, rt->response);
523 /* all legal parameters are of type RT_STRING */
524 if (!psep || rt->type != RT_STRING) {
526 "illegal RING parameter: '%s'\n",
531 /* skip parameter value separator */
535 /* look up end of parameter */
536 eoc = strchr(psep, ',');
540 /* retrieve parameter value */
541 ptr = kstrdup(psep, GFP_ATOMIC);
544 add_cid_event(cs, cid, rt->resp_code, ptr, 0);
549 /* check parameter separator */
552 add_cid_event(cs, cid, rt->resp_code, NULL, ZSAU_NONE);
558 /* look up parameter value */
559 for (zr = zsau_resp; zr->str; ++zr)
560 if (!strcmp(eoc, zr->str))
565 add_cid_event(cs, cid, rt->resp_code, NULL, zr->code);
569 /* check parameter separator */
573 /* retrieve parameter value */
574 ptr = kstrdup(eoc, GFP_ATOMIC);
577 add_cid_event(cs, cid, rt->resp_code, ptr, 0);
581 /* check parameter separators */
584 psep = strchr(eoc, ',');
589 /* decode parameter values */
590 if (kstrtou8(eoc, 16, &type) || kstrtou8(psep, 16, &value)) {
594 parameter = (type << 8) | value;
596 add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
600 /* check parameter separator */
604 /* decode parameter value */
605 if (kstrtoint(eoc, 10, ¶meter))
608 /* special case ZDLE: set flag before queueing event */
609 if (rt->resp_code == RSP_ZDLE)
612 add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
616 /* parameter unexpected, incomplete or malformed */
617 dev_warn(cs->dev, "bad parameter in response '%s'\n",
619 add_cid_event(cs, cid, rt->resp_code, NULL, -1);
623 dev_err(cs->dev, "%s: internal error on '%s'\n",
624 __func__, cs->respdata);
627 EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
630 * process closing of connection associated with given AT state structure
633 static void disconnect_nobc(struct at_state_t **at_state_p,
634 struct cardstate *cs)
638 spin_lock_irqsave(&cs->lock, flags);
639 ++(*at_state_p)->seq_index;
641 /* revert to selected idle mode */
643 cs->at_state.pending_commands |= PC_UMMODE;
644 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
645 cs->commands_pending = 1;
648 /* check for and deallocate temporary AT state */
649 if (!list_empty(&(*at_state_p)->list)) {
650 list_del(&(*at_state_p)->list);
655 spin_unlock_irqrestore(&cs->lock, flags);
659 * process closing of connection associated with given AT state structure
662 static void disconnect_bc(struct at_state_t *at_state,
663 struct cardstate *cs, struct bc_state *bcs)
667 spin_lock_irqsave(&cs->lock, flags);
668 ++at_state->seq_index;
670 /* revert to selected idle mode */
672 cs->at_state.pending_commands |= PC_UMMODE;
673 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
674 cs->commands_pending = 1;
676 spin_unlock_irqrestore(&cs->lock, flags);
678 /* invoke hardware specific handler */
679 cs->ops->close_bchannel(bcs);
682 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
683 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
684 gigaset_isdn_hupD(bcs);
689 * get a free AT state structure: either one of those associated with the
690 * B channels of the Gigaset device, or if none of those is available,
691 * a newly allocated one with bcs=NULL
692 * The structure should be freed by calling disconnect_nobc() after use.
694 static inline struct at_state_t *get_free_channel(struct cardstate *cs,
696 /* cids: >0: siemens-cid
698 * -1: no cid assigned yet
703 struct at_state_t *ret;
705 for (i = 0; i < cs->channels; ++i)
706 if (gigaset_get_channel(cs->bcs + i) >= 0) {
707 ret = &cs->bcs[i].at_state;
712 spin_lock_irqsave(&cs->lock, flags);
713 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
715 gigaset_at_init(ret, NULL, cs, cid);
716 list_add(&ret->list, &cs->temp_at_states);
718 spin_unlock_irqrestore(&cs->lock, flags);
722 static void init_failed(struct cardstate *cs, int mode)
725 struct at_state_t *at_state;
727 cs->at_state.pending_commands &= ~PC_INIT;
729 cs->mstate = MS_UNINITIALIZED;
730 gigaset_free_channels(cs);
731 for (i = 0; i < cs->channels; ++i) {
732 at_state = &cs->bcs[i].at_state;
733 if (at_state->pending_commands & PC_CID) {
734 at_state->pending_commands &= ~PC_CID;
735 at_state->pending_commands |= PC_NOCID;
736 cs->commands_pending = 1;
741 static void schedule_init(struct cardstate *cs, int state)
743 if (cs->at_state.pending_commands & PC_INIT) {
744 gig_dbg(DEBUG_EVENT, "not scheduling PC_INIT again");
748 cs->mode = M_UNKNOWN;
749 gigaset_block_channels(cs);
750 cs->at_state.pending_commands |= PC_INIT;
751 gig_dbg(DEBUG_EVENT, "Scheduling PC_INIT");
752 cs->commands_pending = 1;
755 /* send an AT command
756 * adding the "AT" prefix, cid and DLE encapsulation as appropriate
758 static void send_command(struct cardstate *cs, const char *cmd,
759 struct at_state_t *at_state)
761 int cid = at_state->cid;
765 buflen = strlen(cmd) + 12; /* DLE ( A T 1 2 3 4 5 <cmd> DLE ) \0 */
766 cb = kmalloc(sizeof(struct cmdbuf_t) + buflen, GFP_ATOMIC);
768 dev_err(cs->dev, "%s: out of memory\n", __func__);
771 if (cid > 0 && cid <= 65535)
772 cb->len = snprintf(cb->buf, buflen,
773 cs->dle ? "\020(AT%d%s\020)" : "AT%d%s",
776 cb->len = snprintf(cb->buf, buflen,
777 cs->dle ? "\020(AT%s\020)" : "AT%s",
781 cb->wake_tasklet = NULL;
782 cs->ops->write_cmd(cs, cb);
785 static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
787 struct at_state_t *at_state;
792 return &cs->at_state;
794 for (i = 0; i < cs->channels; ++i)
795 if (cid == cs->bcs[i].at_state.cid)
796 return &cs->bcs[i].at_state;
798 spin_lock_irqsave(&cs->lock, flags);
800 list_for_each_entry(at_state, &cs->temp_at_states, list)
801 if (cid == at_state->cid) {
802 spin_unlock_irqrestore(&cs->lock, flags);
806 spin_unlock_irqrestore(&cs->lock, flags);
811 static void bchannel_down(struct bc_state *bcs)
813 if (bcs->chstate & CHS_B_UP) {
814 bcs->chstate &= ~CHS_B_UP;
815 gigaset_isdn_hupB(bcs);
818 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
819 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
820 gigaset_isdn_hupD(bcs);
823 gigaset_free_channel(bcs);
825 gigaset_bcs_reinit(bcs);
828 static void bchannel_up(struct bc_state *bcs)
830 if (bcs->chstate & CHS_B_UP) {
831 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
836 bcs->chstate |= CHS_B_UP;
837 gigaset_isdn_connB(bcs);
840 static void start_dial(struct at_state_t *at_state, void *data,
843 struct bc_state *bcs = at_state->bcs;
844 struct cardstate *cs = at_state->cs;
845 char **commands = data;
849 bcs->chstate |= CHS_NOTIFY_LL;
851 spin_lock_irqsave(&cs->lock, flags);
852 if (at_state->seq_index != seq_index) {
853 spin_unlock_irqrestore(&cs->lock, flags);
856 spin_unlock_irqrestore(&cs->lock, flags);
858 for (i = 0; i < AT_NUM; ++i) {
859 kfree(bcs->commands[i]);
860 bcs->commands[i] = commands[i];
863 at_state->pending_commands |= PC_CID;
864 gig_dbg(DEBUG_EVENT, "Scheduling PC_CID");
865 cs->commands_pending = 1;
869 for (i = 0; i < AT_NUM; ++i) {
873 at_state->pending_commands |= PC_NOCID;
874 gig_dbg(DEBUG_EVENT, "Scheduling PC_NOCID");
875 cs->commands_pending = 1;
879 static void start_accept(struct at_state_t *at_state)
881 struct cardstate *cs = at_state->cs;
882 struct bc_state *bcs = at_state->bcs;
885 for (i = 0; i < AT_NUM; ++i) {
886 kfree(bcs->commands[i]);
887 bcs->commands[i] = NULL;
890 bcs->commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
891 bcs->commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
892 if (!bcs->commands[AT_PROTO] || !bcs->commands[AT_ISO]) {
893 dev_err(at_state->cs->dev, "out of memory\n");
895 at_state->pending_commands |= PC_HUP;
896 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
897 cs->commands_pending = 1;
901 snprintf(bcs->commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
902 snprintf(bcs->commands[AT_ISO], 9, "^SISO=%u\r", bcs->channel + 1);
904 at_state->pending_commands |= PC_ACCEPT;
905 gig_dbg(DEBUG_EVENT, "Scheduling PC_ACCEPT");
906 cs->commands_pending = 1;
909 static void do_start(struct cardstate *cs)
911 gigaset_free_channels(cs);
913 if (cs->mstate != MS_LOCKED)
914 schedule_init(cs, MS_INIT);
917 gigaset_isdn_start(cs);
920 wake_up(&cs->waitqueue);
923 static void finish_shutdown(struct cardstate *cs)
925 if (cs->mstate != MS_LOCKED) {
926 cs->mstate = MS_UNINITIALIZED;
927 cs->mode = M_UNKNOWN;
930 /* Tell the LL that the device is not available .. */
933 gigaset_isdn_stop(cs);
936 /* The rest is done by cleanup_cs() in process context. */
938 cs->cmd_result = -ENODEV;
940 wake_up(&cs->waitqueue);
943 static void do_shutdown(struct cardstate *cs)
945 gigaset_block_channels(cs);
947 if (cs->mstate == MS_READY) {
948 cs->mstate = MS_SHUTDOWN;
949 cs->at_state.pending_commands |= PC_SHUTDOWN;
950 gig_dbg(DEBUG_EVENT, "Scheduling PC_SHUTDOWN");
951 cs->commands_pending = 1;
956 static void do_stop(struct cardstate *cs)
960 spin_lock_irqsave(&cs->lock, flags);
962 spin_unlock_irqrestore(&cs->lock, flags);
967 /* Entering cid mode or getting a cid failed:
968 * try to initialize the device and try again.
970 * channel >= 0: getting cid for the channel failed
971 * channel < 0: entering cid mode failed
973 * returns 0 on success, <0 on failure
975 static int reinit_and_retry(struct cardstate *cs, int channel)
979 if (--cs->retry_count <= 0)
982 for (i = 0; i < cs->channels; ++i)
983 if (cs->bcs[i].at_state.cid > 0)
988 "Could not enter cid mode. Reinit device and try again.\n");
991 "Could not get a call id. Reinit device and try again.\n");
992 cs->bcs[channel].at_state.pending_commands |= PC_CID;
994 schedule_init(cs, MS_INIT);
998 static int at_state_invalid(struct cardstate *cs,
999 struct at_state_t *test_ptr)
1001 unsigned long flags;
1003 struct at_state_t *at_state;
1006 spin_lock_irqsave(&cs->lock, flags);
1008 if (test_ptr == &cs->at_state)
1011 list_for_each_entry(at_state, &cs->temp_at_states, list)
1012 if (at_state == test_ptr)
1015 for (channel = 0; channel < cs->channels; ++channel)
1016 if (&cs->bcs[channel].at_state == test_ptr)
1021 spin_unlock_irqrestore(&cs->lock, flags);
1025 static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
1026 struct at_state_t *at_state)
1030 retval = gigaset_isdn_icall(at_state);
1035 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
1040 * Device doc says that would reject the call.
1041 * In fact it doesn't.
1043 at_state->pending_commands |= PC_HUP;
1044 cs->commands_pending = 1;
1049 static int do_lock(struct cardstate *cs)
1054 switch (cs->mstate) {
1055 case MS_UNINITIALIZED:
1057 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1058 cs->at_state.pending_commands)
1061 for (i = 0; i < cs->channels; ++i)
1062 if (cs->bcs[i].at_state.pending_commands)
1065 if (gigaset_get_channels(cs) < 0)
1076 cs->mstate = MS_LOCKED;
1077 cs->mode = M_UNKNOWN;
1082 static int do_unlock(struct cardstate *cs)
1084 if (cs->mstate != MS_LOCKED)
1087 cs->mstate = MS_UNINITIALIZED;
1088 cs->mode = M_UNKNOWN;
1089 gigaset_free_channels(cs);
1091 schedule_init(cs, MS_INIT);
1096 static void do_action(int action, struct cardstate *cs,
1097 struct bc_state *bcs,
1098 struct at_state_t **p_at_state, char **pp_command,
1099 int *p_genresp, int *p_resp_code,
1102 struct at_state_t *at_state = *p_at_state;
1103 struct bc_state *bcs2;
1104 unsigned long flags;
1108 unsigned char *s, *e;
1116 at_state->waiting = 1;
1119 cs->at_state.pending_commands &= ~PC_INIT;
1120 cs->cur_at_seq = SEQ_NONE;
1121 cs->mode = M_UNIMODEM;
1122 spin_lock_irqsave(&cs->lock, flags);
1124 spin_unlock_irqrestore(&cs->lock, flags);
1125 gigaset_free_channels(cs);
1126 cs->mstate = MS_READY;
1129 spin_unlock_irqrestore(&cs->lock, flags);
1130 cs->at_state.pending_commands |= PC_CIDMODE;
1131 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1132 cs->commands_pending = 1;
1135 dev_warn(cs->dev, "Could not initialize the device.\n");
1137 init_failed(cs, M_UNKNOWN);
1138 cs->cur_at_seq = SEQ_NONE;
1140 case ACT_CONFIGMODE:
1141 init_failed(cs, M_CONFIG);
1142 cs->cur_at_seq = SEQ_NONE;
1146 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1147 cs->inbuf[0].inputstate &=
1148 ~(INS_command | INS_DLE_command);
1152 cs->inbuf[0].inputstate =
1153 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1157 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1158 gigaset_free_channels(cs);
1159 cs->mstate = MS_READY;
1162 cs->cur_at_seq = SEQ_NONE;
1165 cs->mode = M_UNIMODEM;
1166 cs->cur_at_seq = SEQ_NONE;
1169 cs->cur_at_seq = SEQ_NONE;
1170 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1171 init_failed(cs, M_UNKNOWN);
1174 if (reinit_and_retry(cs, -1) < 0)
1175 schedule_init(cs, MS_RECOVER);
1178 cs->cur_at_seq = SEQ_NONE;
1179 schedule_init(cs, MS_RECOVER);
1182 /* send "+++" (hangup in unimodem mode) */
1183 if (cs->connected) {
1184 struct cmdbuf_t *cb;
1186 cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC);
1188 dev_err(cs->dev, "%s: out of memory\n",
1192 memcpy(cb->buf, "+++", 3);
1196 cb->wake_tasklet = NULL;
1197 cs->ops->write_cmd(cs, cb);
1201 /* get fresh AT state structure for new CID */
1202 at_state = get_free_channel(cs, ev->parameter);
1205 "RING ignored: could not allocate channel structure\n");
1209 /* initialize AT state structure
1210 * note that bcs may be NULL if no B channel is free
1212 at_state->ConState = 700;
1213 for (i = 0; i < STR_NUM; ++i) {
1214 kfree(at_state->str_var[i]);
1215 at_state->str_var[i] = NULL;
1217 at_state->int_var[VAR_ZCTP] = -1;
1219 spin_lock_irqsave(&cs->lock, flags);
1220 at_state->timer_expires = RING_TIMEOUT;
1221 at_state->timer_active = 1;
1222 spin_unlock_irqrestore(&cs->lock, flags);
1225 handle_icall(cs, bcs, at_state);
1228 dev_warn(cs->dev, "Could not shut down the device.\n");
1232 cs->cur_at_seq = SEQ_NONE;
1233 finish_shutdown(cs);
1236 if (cs->onechannel) {
1237 at_state->pending_commands |= PC_DLE1;
1238 cs->commands_pending = 1;
1241 bcs->chstate |= CHS_D_UP;
1242 gigaset_isdn_connD(bcs);
1243 cs->ops->init_bchannel(bcs);
1246 cs->cur_at_seq = SEQ_NONE;
1247 bcs = cs->bcs + cs->curchannel;
1249 bcs->chstate |= CHS_D_UP;
1250 gigaset_isdn_connD(bcs);
1251 cs->ops->init_bchannel(bcs);
1254 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1256 case ACT_DISCONNECT:
1257 cs->cur_at_seq = SEQ_NONE;
1260 disconnect_nobc(p_at_state, cs);
1261 } else if (cs->onechannel && cs->dle) {
1262 /* Check for other open channels not needed:
1263 * DLE only used for M10x with one B channel.
1265 at_state->pending_commands |= PC_DLE0;
1266 cs->commands_pending = 1;
1268 disconnect_bc(at_state, cs, bcs);
1272 at_state->int_var[VAR_ZDLE] = 0;
1276 cs->cur_at_seq = SEQ_NONE;
1277 bcs2 = cs->bcs + cs->curchannel;
1278 disconnect_bc(&bcs2->at_state, cs, bcs2);
1281 cs->cur_at_seq = SEQ_NONE;
1282 dev_warn(cs->dev, "Could not hang up.\n");
1285 disconnect_nobc(p_at_state, cs);
1286 else if (cs->onechannel)
1287 at_state->pending_commands |= PC_DLE0;
1289 disconnect_bc(at_state, cs, bcs);
1290 schedule_init(cs, MS_RECOVER);
1293 cs->cur_at_seq = SEQ_NONE;
1294 dev_warn(cs->dev, "Error leaving DLE mode.\n");
1296 bcs2 = cs->bcs + cs->curchannel;
1297 disconnect_bc(&bcs2->at_state, cs, bcs2);
1298 schedule_init(cs, MS_RECOVER);
1301 cs->cur_at_seq = SEQ_NONE;
1303 "Could not enter DLE mode. Trying to hang up.\n");
1304 channel = cs->curchannel;
1305 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
1306 cs->commands_pending = 1;
1309 case ACT_CID: /* got cid; start dialing */
1310 cs->cur_at_seq = SEQ_NONE;
1311 channel = cs->curchannel;
1312 if (ev->parameter > 0 && ev->parameter <= 65535) {
1313 cs->bcs[channel].at_state.cid = ev->parameter;
1314 cs->bcs[channel].at_state.pending_commands |=
1316 cs->commands_pending = 1;
1319 /* fall through - bad cid */
1321 cs->cur_at_seq = SEQ_NONE;
1322 channel = cs->curchannel;
1323 if (reinit_and_retry(cs, channel) < 0) {
1325 "Could not get a call ID. Cannot dial.\n");
1326 bcs2 = cs->bcs + channel;
1327 disconnect_bc(&bcs2->at_state, cs, bcs2);
1331 cs->cur_at_seq = SEQ_NONE;
1332 bcs2 = cs->bcs + cs->curchannel;
1333 disconnect_bc(&bcs2->at_state, cs, bcs2);
1338 cs->cur_at_seq = SEQ_NONE;
1341 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL procssng */
1343 disconnect_bc(at_state, cs, bcs);
1345 disconnect_nobc(p_at_state, cs);
1348 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1349 cs->cur_at_seq = SEQ_NONE;
1350 at_state->pending_commands |= PC_HUP;
1351 cs->commands_pending = 1;
1354 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1355 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1356 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1357 at_state->pending_commands |= PC_HUP;
1358 cs->commands_pending = 1;
1360 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
1361 are not handled properly anymore */
1362 at_state->getstring = 1;
1367 *p_resp_code = RSP_ERROR;
1372 if (!strcmp(s, "OK")) {
1373 /* OK without version string: assume old response */
1375 *p_resp_code = RSP_NONE;
1379 for (i = 0; i < 4; ++i) {
1380 val = simple_strtoul(s, (char **) &e, 10);
1381 if (val > INT_MAX || e == s)
1386 } else if (*e != '.')
1394 *p_resp_code = RSP_ERROR;
1400 if (cs->gotfwver == 0) {
1402 gig_dbg(DEBUG_EVENT,
1403 "firmware version %02d.%03d.%02d.%02d",
1404 cs->fwver[0], cs->fwver[1],
1405 cs->fwver[2], cs->fwver[3]);
1411 dev_err(cs->dev, "could not read firmware version.\n");
1414 gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d",
1415 __func__, at_state->ConState);
1416 cs->cur_at_seq = SEQ_NONE;
1419 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
1420 __func__, ev->type, at_state->ConState);
1423 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1424 __func__, ev->type, at_state->ConState);
1427 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1428 ev->parameter, at_state->ConState);
1431 /* events from the LL */
1436 *p_resp_code = RSP_ERROR;
1439 start_dial(at_state, ev->ptr, ev->parameter);
1442 start_accept(at_state);
1445 at_state->pending_commands |= PC_HUP;
1446 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
1447 cs->commands_pending = 1;
1450 /* hotplug events */
1459 /* events from the interface */
1462 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1464 wake_up(&cs->waitqueue);
1467 if (ev->parameter != 0)
1468 cs->cmd_result = -EINVAL;
1469 else if (cs->gotfwver != 1) {
1470 cs->cmd_result = -ENOENT;
1472 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1476 wake_up(&cs->waitqueue);
1479 /* events from the proc file system */
1481 case ACT_PROC_CIDMODE:
1482 spin_lock_irqsave(&cs->lock, flags);
1483 if (ev->parameter != cs->cidmode) {
1484 cs->cidmode = ev->parameter;
1485 if (ev->parameter) {
1486 cs->at_state.pending_commands |= PC_CIDMODE;
1487 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1489 cs->at_state.pending_commands |= PC_UMMODE;
1490 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
1492 cs->commands_pending = 1;
1494 spin_unlock_irqrestore(&cs->lock, flags);
1496 wake_up(&cs->waitqueue);
1499 /* events from the hardware drivers */
1501 case ACT_NOTIFY_BC_DOWN:
1504 case ACT_NOTIFY_BC_UP:
1513 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1514 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1517 *p_resp_code = RSP_NULL;
1520 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
1524 /* State machine to do the calling and hangup procedure */
1525 static void process_event(struct cardstate *cs, struct event_t *ev)
1527 struct bc_state *bcs;
1528 char *p_command = NULL;
1529 struct reply_t *rep;
1532 int resp_code = RSP_ERROR;
1533 struct at_state_t *at_state;
1536 unsigned long flags;
1539 at_state = at_state_from_cid(cs, ev->cid);
1541 gig_dbg(DEBUG_EVENT, "event %d for invalid cid %d",
1543 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
1548 at_state = ev->at_state;
1549 if (at_state_invalid(cs, at_state)) {
1550 gig_dbg(DEBUG_EVENT, "event for invalid at_state %p",
1556 gig_dbg(DEBUG_EVENT, "connection state %d, event %d",
1557 at_state->ConState, ev->type);
1559 bcs = at_state->bcs;
1561 /* Setting the pointer to the dial array */
1562 rep = at_state->replystruct;
1564 spin_lock_irqsave(&cs->lock, flags);
1565 if (ev->type == EV_TIMEOUT) {
1566 if (ev->parameter != at_state->timer_index
1567 || !at_state->timer_active) {
1568 ev->type = RSP_NONE; /* old timeout */
1569 gig_dbg(DEBUG_EVENT, "old timeout");
1571 if (at_state->waiting)
1572 gig_dbg(DEBUG_EVENT, "stopped waiting");
1574 gig_dbg(DEBUG_EVENT, "timeout occurred");
1577 spin_unlock_irqrestore(&cs->lock, flags);
1579 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1580 or at_state->str_var[STR_XXXX], set it */
1581 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1582 index = ev->type - RSP_VAR;
1583 at_state->int_var[index] = ev->parameter;
1584 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1585 index = ev->type - RSP_STR;
1586 kfree(at_state->str_var[index]);
1587 at_state->str_var[index] = ev->ptr;
1588 ev->ptr = NULL; /* prevent process_events() from
1592 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1593 at_state->getstring = 0;
1595 /* Search row in dial array which matches modem response and current
1598 rcode = rep->resp_code;
1599 if (rcode == RSP_LAST) {
1600 /* found nothing...*/
1601 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1602 "resp_code %d in ConState %d!\n",
1603 __func__, ev->type, at_state->ConState);
1606 if ((rcode == RSP_ANY || rcode == ev->type)
1607 && ((int) at_state->ConState >= rep->min_ConState)
1608 && (rep->max_ConState < 0
1609 || (int) at_state->ConState <= rep->max_ConState)
1610 && (rep->parameter < 0 || rep->parameter == ev->parameter))
1614 p_command = rep->command;
1616 at_state->waiting = 0;
1617 for (curact = 0; curact < MAXACT; ++curact) {
1618 /* The row tells us what we should do ..
1620 do_action(rep->action[curact], cs, bcs, &at_state, &p_command,
1621 &genresp, &resp_code, ev);
1623 /* at_state destroyed by disconnect */
1627 /* Jump to the next con-state regarding the array */
1628 if (rep->new_ConState >= 0)
1629 at_state->ConState = rep->new_ConState;
1632 spin_lock_irqsave(&cs->lock, flags);
1633 at_state->timer_expires = 0;
1634 at_state->timer_active = 0;
1635 spin_unlock_irqrestore(&cs->lock, flags);
1636 gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1638 /* Send command to modem if not NULL... */
1641 send_command(cs, p_command, at_state);
1643 gigaset_add_event(cs, at_state, RSP_NODEV,
1647 spin_lock_irqsave(&cs->lock, flags);
1648 if (!rep->timeout) {
1649 at_state->timer_expires = 0;
1650 at_state->timer_active = 0;
1651 } else if (rep->timeout > 0) { /* new timeout */
1652 at_state->timer_expires = rep->timeout * 10;
1653 at_state->timer_active = 1;
1654 ++at_state->timer_index;
1656 spin_unlock_irqrestore(&cs->lock, flags);
1660 static void schedule_sequence(struct cardstate *cs,
1661 struct at_state_t *at_state, int sequence)
1663 cs->cur_at_seq = sequence;
1664 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1667 static void process_command_flags(struct cardstate *cs)
1669 struct at_state_t *at_state = NULL;
1670 struct bc_state *bcs;
1673 unsigned long flags;
1675 cs->commands_pending = 0;
1677 if (cs->cur_at_seq) {
1678 gig_dbg(DEBUG_EVENT, "not searching scheduled commands: busy");
1682 gig_dbg(DEBUG_EVENT, "searching scheduled commands");
1684 sequence = SEQ_NONE;
1686 /* clear pending_commands and hangup channels on shutdown */
1687 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1688 cs->at_state.pending_commands &= ~PC_CIDMODE;
1689 for (i = 0; i < cs->channels; ++i) {
1691 at_state = &bcs->at_state;
1692 at_state->pending_commands &=
1693 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1694 if (at_state->cid > 0)
1695 at_state->pending_commands |= PC_HUP;
1696 if (at_state->pending_commands & PC_CID) {
1697 at_state->pending_commands |= PC_NOCID;
1698 at_state->pending_commands &= ~PC_CID;
1703 /* clear pending_commands and hangup channels on reset */
1704 if (cs->at_state.pending_commands & PC_INIT) {
1705 cs->at_state.pending_commands &= ~PC_CIDMODE;
1706 for (i = 0; i < cs->channels; ++i) {
1708 at_state = &bcs->at_state;
1709 at_state->pending_commands &=
1710 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1711 if (at_state->cid > 0)
1712 at_state->pending_commands |= PC_HUP;
1713 if (cs->mstate == MS_RECOVER) {
1714 if (at_state->pending_commands & PC_CID) {
1715 at_state->pending_commands |= PC_NOCID;
1716 at_state->pending_commands &= ~PC_CID;
1722 /* only switch back to unimodem mode if no commands are pending and
1723 * no channels are up */
1724 spin_lock_irqsave(&cs->lock, flags);
1725 if (cs->at_state.pending_commands == PC_UMMODE
1727 && list_empty(&cs->temp_at_states)
1728 && cs->mode == M_CID) {
1729 sequence = SEQ_UMMODE;
1730 at_state = &cs->at_state;
1731 for (i = 0; i < cs->channels; ++i) {
1733 if (bcs->at_state.pending_commands ||
1734 bcs->at_state.cid > 0) {
1735 sequence = SEQ_NONE;
1740 spin_unlock_irqrestore(&cs->lock, flags);
1741 cs->at_state.pending_commands &= ~PC_UMMODE;
1742 if (sequence != SEQ_NONE) {
1743 schedule_sequence(cs, at_state, sequence);
1747 for (i = 0; i < cs->channels; ++i) {
1749 if (bcs->at_state.pending_commands & PC_HUP) {
1751 cs->curchannel = bcs->channel;
1752 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1755 bcs->at_state.pending_commands &= ~PC_HUP;
1756 if (bcs->at_state.pending_commands & PC_CID) {
1757 /* not yet dialing: PC_NOCID is sufficient */
1758 bcs->at_state.pending_commands |= PC_NOCID;
1759 bcs->at_state.pending_commands &= ~PC_CID;
1761 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1765 if (bcs->at_state.pending_commands & PC_NOCID) {
1766 bcs->at_state.pending_commands &= ~PC_NOCID;
1767 cs->curchannel = bcs->channel;
1768 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1770 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1771 bcs->at_state.pending_commands &= ~PC_DLE0;
1772 cs->curchannel = bcs->channel;
1773 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1778 list_for_each_entry(at_state, &cs->temp_at_states, list)
1779 if (at_state->pending_commands & PC_HUP) {
1780 at_state->pending_commands &= ~PC_HUP;
1781 schedule_sequence(cs, at_state, SEQ_HUP);
1785 if (cs->at_state.pending_commands & PC_INIT) {
1786 cs->at_state.pending_commands &= ~PC_INIT;
1788 cs->inbuf->inputstate = INS_command;
1789 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1792 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1793 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1794 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1797 if (cs->at_state.pending_commands & PC_CIDMODE) {
1798 cs->at_state.pending_commands &= ~PC_CIDMODE;
1799 if (cs->mode == M_UNIMODEM) {
1800 cs->retry_count = 1;
1801 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1806 for (i = 0; i < cs->channels; ++i) {
1808 if (bcs->at_state.pending_commands & PC_DLE1) {
1809 bcs->at_state.pending_commands &= ~PC_DLE1;
1810 cs->curchannel = bcs->channel;
1811 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1814 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1815 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1816 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1819 if (bcs->at_state.pending_commands & PC_DIAL) {
1820 bcs->at_state.pending_commands &= ~PC_DIAL;
1821 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1824 if (bcs->at_state.pending_commands & PC_CID) {
1827 cs->at_state.pending_commands |= PC_CIDMODE;
1828 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1829 cs->commands_pending = 1;
1832 schedule_init(cs, MS_INIT);
1835 bcs->at_state.pending_commands &= ~PC_CID;
1836 cs->curchannel = bcs->channel;
1837 cs->retry_count = 2;
1838 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1844 static void process_events(struct cardstate *cs)
1847 unsigned head, tail;
1849 int check_flags = 0;
1851 unsigned long flags;
1853 spin_lock_irqsave(&cs->ev_lock, flags);
1856 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
1859 if (!check_flags && !cs->commands_pending)
1862 spin_unlock_irqrestore(&cs->ev_lock, flags);
1863 process_command_flags(cs);
1864 spin_lock_irqsave(&cs->ev_lock, flags);
1867 if (!cs->commands_pending)
1873 ev = cs->events + head;
1874 was_busy = cs->cur_at_seq != SEQ_NONE;
1875 spin_unlock_irqrestore(&cs->ev_lock, flags);
1876 process_event(cs, ev);
1877 spin_lock_irqsave(&cs->ev_lock, flags);
1880 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1883 head = (head + 1) % MAX_EVENTS;
1887 spin_unlock_irqrestore(&cs->ev_lock, flags);
1889 if (i == 2 * MAX_EVENTS) {
1891 "infinite loop in process_events; aborting.\n");
1895 /* tasklet scheduled on any event received from the Gigaset device
1897 * data ISDN controller state structure
1899 void gigaset_handle_event(unsigned long data)
1901 struct cardstate *cs = (struct cardstate *) data;
1903 /* handle incoming data on control/common channel */
1904 if (cs->inbuf->head != cs->inbuf->tail) {
1905 gig_dbg(DEBUG_INTR, "processing new data");
1906 cs->ops->handle_input(cs->inbuf);