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 * =====================================================================
16 #include <linux/export.h>
19 /* ========================================================== */
20 /* bit masks for pending commands */
26 #define PC_SHUTDOWN 0x020
27 #define PC_ACCEPT 0x040
29 #define PC_NOCID 0x100
30 #define PC_CIDMODE 0x200
31 #define PC_UMMODE 0x400
33 /* types of modem responses */
41 /* Possible ASCII responses */
49 /* responses with values to store in at_state */
52 #define RSP_ZSAU (RSP_VAR + VAR_ZSAU)
53 #define RSP_ZDLE (RSP_VAR + VAR_ZDLE)
54 #define RSP_ZCTP (RSP_VAR + VAR_ZCTP)
56 #define RSP_STR (RSP_VAR + VAR_NUM)
57 #define RSP_NMBR (RSP_STR + STR_NMBR)
58 #define RSP_ZCPN (RSP_STR + STR_ZCPN)
59 #define RSP_ZCON (RSP_STR + STR_ZCON)
60 #define RSP_ZBC (RSP_STR + STR_ZBC)
61 #define RSP_ZHLC (RSP_STR + STR_ZHLC)
63 #define RSP_WRONG_CID -2 /* unknown cid in cmd */
64 #define RSP_INVAL -6 /* invalid response */
65 #define RSP_NODEV -9 /* device not connected */
68 #define RSP_STRING -20
74 /* actions for process_response */
78 #define ACT_FAILINIT 3
79 #define ACT_HUPMODEM 4
80 #define ACT_CONFIGMODE 5
84 #define ACT_FAILDLE0 9
85 #define ACT_FAILDLE1 10
88 #define ACT_FAILCID 13
90 #define ACT_FAILSDOWN 15
93 #define ACT_DIALING 18
94 #define ACT_ABORTDIAL 19
95 #define ACT_DISCONNECT 20
96 #define ACT_CONNECT 21
97 #define ACT_REMOTEREJECT 22
98 #define ACT_CONNTIMEOUT 23
99 #define ACT_REMOTEHUP 24
100 #define ACT_ABORTHUP 25
102 #define ACT_ACCEPTED 27
103 #define ACT_ABORTACCEPT 28
104 #define ACT_TIMEOUT 29
105 #define ACT_GETSTRING 30
106 #define ACT_SETVER 31
107 #define ACT_FAILVER 32
108 #define ACT_GOTVER 33
111 #define ACT_ABORTCID 36
113 #define ACT_NOTIFY_BC_DOWN 38
114 #define ACT_NOTIFY_BC_UP 39
116 #define ACT_ACCEPT 41
118 #define ACT_IF_LOCK 44
121 #define ACT_FAKEDLE0 47
122 #define ACT_FAKEHUP 48
123 #define ACT_FAKESDOWN 49
124 #define ACT_SHUTDOWN 50
125 #define ACT_PROC_CIDMODE 51
126 #define ACT_UMODESET 52
127 #define ACT_FAILUMODE 53
128 #define ACT_CMODESET 54
129 #define ACT_FAILCMODE 55
130 #define ACT_IF_VER 56
133 /* at command sequences */
139 #define SEQ_NOCID 350
142 #define SEQ_ACCEPT 720
143 #define SEQ_SHUTDOWN 500
144 #define SEQ_CIDMODE 10
145 #define SEQ_UMMODE 11
148 /* 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid),
149 * 400: hup, 500: reset, 600: dial, 700: ring */
150 struct reply_t gigaset_tab_nocid[] =
152 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
155 /* initialize device, set cid mode if possible */
156 {RSP_INIT, -1, -1, SEQ_INIT, 100, 1, {ACT_TIMEOUT} },
158 {EV_TIMEOUT, 100, 100, -1, 101, 3, {0}, "Z\r"},
159 {RSP_OK, 101, 103, -1, 120, 5, {ACT_GETSTRING},
162 {EV_TIMEOUT, 101, 101, -1, 102, 5, {0}, "Z\r"},
163 {RSP_ERROR, 101, 101, -1, 102, 5, {0}, "Z\r"},
165 {EV_TIMEOUT, 102, 102, -1, 108, 5, {ACT_SETDLE1},
167 {RSP_OK, 108, 108, -1, 104, -1},
168 {RSP_ZDLE, 104, 104, 0, 103, 5, {0}, "Z\r"},
169 {EV_TIMEOUT, 104, 104, -1, 0, 0, {ACT_FAILINIT} },
170 {RSP_ERROR, 108, 108, -1, 0, 0, {ACT_FAILINIT} },
172 {EV_TIMEOUT, 108, 108, -1, 105, 2, {ACT_SETDLE0,
175 {EV_TIMEOUT, 105, 105, -1, 103, 5, {0}, "Z\r"},
177 {RSP_ERROR, 102, 102, -1, 107, 5, {0}, "^GETPRE\r"},
178 {RSP_OK, 107, 107, -1, 0, 0, {ACT_CONFIGMODE} },
179 {RSP_ERROR, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
180 {EV_TIMEOUT, 107, 107, -1, 0, 0, {ACT_FAILINIT} },
182 {RSP_ERROR, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
183 {EV_TIMEOUT, 103, 103, -1, 0, 0, {ACT_FAILINIT} },
185 {RSP_STRING, 120, 120, -1, 121, -1, {ACT_SETVER} },
187 {EV_TIMEOUT, 120, 121, -1, 0, 0, {ACT_FAILVER,
189 {RSP_ERROR, 120, 121, -1, 0, 0, {ACT_FAILVER,
191 {RSP_OK, 121, 121, -1, 0, 0, {ACT_GOTVER,
193 {RSP_NONE, 121, 121, -1, 120, 0, {ACT_GETSTRING} },
196 {RSP_INIT, 0, 0, SEQ_DLE0, 201, 5, {0}, "^SDLE=0\r"},
197 {RSP_OK, 201, 201, -1, 202, -1},
198 {RSP_ZDLE, 202, 202, 0, 0, 0, {ACT_DLE0} },
199 {RSP_NODEV, 200, 249, -1, 0, 0, {ACT_FAKEDLE0} },
200 {RSP_ERROR, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
201 {EV_TIMEOUT, 200, 249, -1, 0, 0, {ACT_FAILDLE0} },
204 {RSP_INIT, 0, 0, SEQ_DLE1, 251, 5, {0}, "^SDLE=1\r"},
205 {RSP_OK, 251, 251, -1, 252, -1},
206 {RSP_ZDLE, 252, 252, 1, 0, 0, {ACT_DLE1} },
207 {RSP_ERROR, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
208 {EV_TIMEOUT, 250, 299, -1, 0, 0, {ACT_FAILDLE1} },
211 {RSP_RING, -1, -1, -1, -1, -1, {ACT_RING} },
214 {RSP_INIT, 0, 0, SEQ_CID, 301, 5, {0}, "^SGCI?\r"},
215 {RSP_OK, 301, 301, -1, 302, -1},
216 {RSP_ZGCI, 302, 302, -1, 0, 0, {ACT_CID} },
217 {RSP_ERROR, 301, 349, -1, 0, 0, {ACT_FAILCID} },
218 {EV_TIMEOUT, 301, 349, -1, 0, 0, {ACT_FAILCID} },
221 {RSP_INIT, 0, 0, SEQ_CIDMODE, 150, 5, {0}, "^SGCI=1\r"},
222 {RSP_OK, 150, 150, -1, 0, 0, {ACT_CMODESET} },
223 {RSP_ERROR, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
224 {EV_TIMEOUT, 150, 150, -1, 0, 0, {ACT_FAILCMODE} },
227 {RSP_INIT, 0, 0, SEQ_UMMODE, 160, 5, {0}, "Z\r"},
228 {RSP_OK, 160, 160, -1, 0, 0, {ACT_UMODESET} },
229 {RSP_ERROR, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
230 {EV_TIMEOUT, 160, 160, -1, 0, 0, {ACT_FAILUMODE} },
232 /* abort getting cid */
233 {RSP_INIT, 0, 0, SEQ_NOCID, 0, 0, {ACT_ABORTCID} },
236 {RSP_INIT, 0, 0, SEQ_SHUTDOWN, 504, 5, {0}, "Z\r"},
237 {RSP_OK, 504, 504, -1, 0, 0, {ACT_SDOWN} },
238 {RSP_ERROR, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
239 {EV_TIMEOUT, 501, 599, -1, 0, 0, {ACT_FAILSDOWN} },
240 {RSP_NODEV, 501, 599, -1, 0, 0, {ACT_FAKESDOWN} },
242 {EV_PROC_CIDMODE, -1, -1, -1, -1, -1, {ACT_PROC_CIDMODE} },
243 {EV_IF_LOCK, -1, -1, -1, -1, -1, {ACT_IF_LOCK} },
244 {EV_IF_VER, -1, -1, -1, -1, -1, {ACT_IF_VER} },
245 {EV_START, -1, -1, -1, -1, -1, {ACT_START} },
246 {EV_STOP, -1, -1, -1, -1, -1, {ACT_STOP} },
247 {EV_SHUTDOWN, -1, -1, -1, -1, -1, {ACT_SHUTDOWN} },
250 {RSP_ERROR, -1, -1, -1, -1, -1, {ACT_ERROR} },
251 {RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
252 {RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
253 {RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
257 /* 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring,
258 * 400: hup, 750: accepted icall */
259 struct reply_t gigaset_tab_cid[] =
261 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
265 {EV_DIAL, -1, -1, -1, -1, -1, {ACT_DIAL} },
266 {RSP_INIT, 0, 0, SEQ_DIAL, 601, 5, {ACT_CMD + AT_BC} },
267 {RSP_OK, 601, 601, -1, 603, 5, {ACT_CMD + AT_PROTO} },
268 {RSP_OK, 603, 603, -1, 604, 5, {ACT_CMD + AT_TYPE} },
269 {RSP_OK, 604, 604, -1, 605, 5, {ACT_CMD + AT_MSN} },
270 {RSP_NULL, 605, 605, -1, 606, 5, {ACT_CMD + AT_CLIP} },
271 {RSP_OK, 605, 605, -1, 606, 5, {ACT_CMD + AT_CLIP} },
272 {RSP_NULL, 606, 606, -1, 607, 5, {ACT_CMD + AT_ISO} },
273 {RSP_OK, 606, 606, -1, 607, 5, {ACT_CMD + AT_ISO} },
274 {RSP_OK, 607, 607, -1, 608, 5, {0}, "+VLS=17\r"},
275 {RSP_OK, 608, 608, -1, 609, -1},
276 {RSP_ZSAU, 609, 609, ZSAU_PROCEEDING, 610, 5, {ACT_CMD + AT_DIAL} },
277 {RSP_OK, 610, 610, -1, 650, 0, {ACT_DIALING} },
279 {RSP_ERROR, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
280 {EV_TIMEOUT, 601, 610, -1, 0, 0, {ACT_ABORTDIAL} },
282 /* optional dialing responses */
283 {EV_BC_OPEN, 650, 650, -1, 651, -1},
284 {RSP_ZVLS, 609, 651, 17, -1, -1, {ACT_DEBUG} },
285 {RSP_ZCTP, 610, 651, -1, -1, -1, {ACT_DEBUG} },
286 {RSP_ZCPN, 610, 651, -1, -1, -1, {ACT_DEBUG} },
287 {RSP_ZSAU, 650, 651, ZSAU_CALL_DELIVERED, -1, -1, {ACT_DEBUG} },
290 {RSP_ZSAU, 650, 650, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
291 {RSP_ZSAU, 651, 651, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
293 {RSP_ZSAU, 750, 750, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT} },
294 {RSP_ZSAU, 751, 751, ZSAU_ACTIVE, 800, -1, {ACT_CONNECT,
296 {EV_BC_OPEN, 800, 800, -1, 800, -1, {ACT_NOTIFY_BC_UP} },
299 {RSP_ZSAU, 650, 651, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT} },
300 {RSP_ZSAU, 750, 751, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
301 {RSP_ZSAU, 800, 800, ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP} },
304 {EV_HUP, -1, -1, -1, -1, -1, {ACT_HUP} },
305 {RSP_INIT, -1, -1, SEQ_HUP, 401, 5, {0}, "+VLS=0\r"},
306 {RSP_OK, 401, 401, -1, 402, 5},
307 {RSP_ZVLS, 402, 402, 0, 403, 5},
308 {RSP_ZSAU, 403, 403, ZSAU_DISCONNECT_REQ, -1, -1, {ACT_DEBUG} },
309 {RSP_ZSAU, 403, 403, ZSAU_NULL, 0, 0, {ACT_DISCONNECT} },
310 {RSP_NODEV, 401, 403, -1, 0, 0, {ACT_FAKEHUP} },
311 {RSP_ERROR, 401, 401, -1, 0, 0, {ACT_ABORTHUP} },
312 {EV_TIMEOUT, 401, 403, -1, 0, 0, {ACT_ABORTHUP} },
314 {EV_BC_CLOSED, 0, 0, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
317 {RSP_ZBC, 700, 700, -1, -1, -1, {0} },
318 {RSP_ZHLC, 700, 700, -1, -1, -1, {0} },
319 {RSP_NMBR, 700, 700, -1, -1, -1, {0} },
320 {RSP_ZCPN, 700, 700, -1, -1, -1, {0} },
321 {RSP_ZCTP, 700, 700, -1, -1, -1, {0} },
322 {EV_TIMEOUT, 700, 700, -1, 720, 720, {ACT_ICALL} },
323 {EV_BC_CLOSED, 720, 720, -1, 0, -1, {ACT_NOTIFY_BC_DOWN} },
326 {EV_ACCEPT, -1, -1, -1, -1, -1, {ACT_ACCEPT} },
327 {RSP_INIT, 720, 720, SEQ_ACCEPT, 721, 5, {ACT_CMD + AT_PROTO} },
328 {RSP_OK, 721, 721, -1, 722, 5, {ACT_CMD + AT_ISO} },
329 {RSP_OK, 722, 722, -1, 723, 5, {0}, "+VLS=17\r"},
330 {RSP_OK, 723, 723, -1, 724, 5, {0} },
331 {RSP_ZVLS, 724, 724, 17, 750, 50, {ACT_ACCEPTED} },
332 {RSP_ERROR, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
333 {EV_TIMEOUT, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT} },
334 {RSP_ZSAU, 700, 729, ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT} },
335 {RSP_ZSAU, 700, 729, ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT} },
336 {RSP_ZSAU, 700, 729, ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT} },
338 {EV_BC_OPEN, 750, 750, -1, 751, -1},
339 {EV_TIMEOUT, 750, 751, -1, 0, 0, {ACT_CONNTIMEOUT} },
341 /* B channel closed (general case) */
342 {EV_BC_CLOSED, -1, -1, -1, -1, -1, {ACT_NOTIFY_BC_DOWN} },
345 {RSP_ZCON, -1, -1, -1, -1, -1, {ACT_DEBUG} },
346 {RSP_ZCAU, -1, -1, -1, -1, -1, {ACT_ZCAU} },
347 {RSP_NONE, -1, -1, -1, -1, -1, {ACT_DEBUG} },
348 {RSP_ANY, -1, -1, -1, -1, -1, {ACT_WARN} },
353 static const struct resp_type_t {
360 {"OK", RSP_OK, RT_NOTHING},
361 {"ERROR", RSP_ERROR, RT_NOTHING},
362 {"ZSAU", RSP_ZSAU, RT_ZSAU},
363 {"ZCAU", RSP_ZCAU, RT_ZCAU},
364 {"RING", RSP_RING, RT_RING},
365 {"ZGCI", RSP_ZGCI, RT_NUMBER},
366 {"ZVLS", RSP_ZVLS, RT_NUMBER},
367 {"ZCTP", RSP_ZCTP, RT_NUMBER},
368 {"ZDLE", RSP_ZDLE, RT_NUMBER},
369 {"ZHLC", RSP_ZHLC, RT_STRING},
370 {"ZBC", RSP_ZBC, RT_STRING},
371 {"NMBR", RSP_NMBR, RT_STRING},
372 {"ZCPN", RSP_ZCPN, RT_STRING},
373 {"ZCON", RSP_ZCON, RT_STRING},
377 static const struct zsau_resp_t {
383 {"OUTGOING_CALL_PROCEEDING", ZSAU_PROCEEDING},
384 {"CALL_DELIVERED", ZSAU_CALL_DELIVERED},
385 {"ACTIVE", ZSAU_ACTIVE},
386 {"DISCONNECT_IND", ZSAU_DISCONNECT_IND},
388 {"DISCONNECT_REQ", ZSAU_DISCONNECT_REQ},
392 /* retrieve CID from parsed response
393 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
395 static int cid_of_response(char *s)
401 return 0; /* no CID separator */
402 rc = kstrtoint(s, 10, &cid);
404 return 0; /* CID not numeric */
405 if (cid < 1 || cid > 65535)
406 return -1; /* CID out of range */
411 * gigaset_handle_modem_response() - process received modem response
412 * @cs: device descriptor structure.
414 * Called by asyncdata/isocdata if a block of data received from the
415 * device must be processed as a modem command response. The data is
416 * already in the cs structure.
418 void gigaset_handle_modem_response(struct cardstate *cs)
420 unsigned char *argv[MAX_REC_PARAMS + 1];
423 const struct resp_type_t *rt;
424 const struct zsau_resp_t *zr;
427 unsigned next, tail, head;
428 struct event_t *event;
438 /* ignore additional LFs/CRs (M10x config mode or cx100) */
439 gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[0]);
442 cs->respdata[len] = 0;
443 argv[0] = cs->respdata;
445 if (cs->at_state.getstring) {
446 /* getstring only allowed without cid at the moment */
447 cs->at_state.getstring = 0;
452 for (i = 0; i < len; i++)
453 switch (cs->respdata[i]) {
457 if (params > MAX_REC_PARAMS) {
459 "too many parameters in response\n");
460 /* need last parameter (might be CID) */
463 argv[params++] = cs->respdata + i + 1;
467 cid = params > 1 ? cid_of_response(argv[params - 1]) : 0;
469 gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
474 for (j = 1; j < params; ++j)
477 gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]);
480 gig_dbg(DEBUG_EVENT, "CID: %s", argv[params]);
482 gig_dbg(DEBUG_EVENT, "available params: %d", params - 1);
483 for (j = 1; j < params; j++)
484 gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]);
487 spin_lock_irqsave(&cs->ev_lock, flags);
493 while (curarg < params) {
494 next = (tail + 1) % MAX_EVENTS;
495 if (unlikely(next == head)) {
496 dev_err(cs->dev, "event queue full\n");
500 event = cs->events + tail;
501 event->at_state = NULL;
508 resp_code = RSP_STRING;
509 param_type = RT_STRING;
511 for (rt = resp_type; rt->response; ++rt)
512 if (!strcmp(argv[curarg], rt->response))
516 event->type = RSP_NONE;
518 "unknown modem response: '%s'\n",
523 resp_code = rt->resp_code;
524 param_type = rt->type;
528 event->type = resp_code;
530 switch (param_type) {
536 "received RING without CID!\n");
537 event->type = RSP_INVAL;
541 event->parameter = cid;
546 if (curarg >= params) {
547 event->parameter = ZSAU_NONE;
550 for (zr = zsau_resp; zr->str; ++zr)
551 if (!strcmp(argv[curarg], zr->str))
553 event->parameter = zr->code;
556 "%s: unknown parameter %s after ZSAU\n",
557 __func__, argv[curarg]);
561 if (curarg < params) {
562 event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
564 dev_err(cs->dev, "out of memory\n");
567 gig_dbg(DEBUG_EVENT, "string==%s",
568 event->ptr ? (char *) event->ptr : "NULL");
571 event->parameter = -1;
572 if (curarg + 1 < params) {
575 i = kstrtou8(argv[curarg++], 16, &type);
576 j = kstrtou8(argv[curarg++], 16, &value);
577 if (i == 0 && j == 0)
578 event->parameter = (type << 8) | value;
583 if (curarg >= params ||
584 kstrtoint(argv[curarg++], 10, &event->parameter))
585 event->parameter = -1;
586 gig_dbg(DEBUG_EVENT, "parameter==%d", event->parameter);
590 if (resp_code == RSP_ZDLE)
591 cs->dle = event->parameter;
598 spin_unlock_irqrestore(&cs->ev_lock, flags);
600 if (curarg != params)
602 "invalid number of processed parameters: %d/%d",
605 EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
608 * process closing of connection associated with given AT state structure
610 static void disconnect(struct at_state_t **at_state_p)
613 struct bc_state *bcs = (*at_state_p)->bcs;
614 struct cardstate *cs = (*at_state_p)->cs;
616 spin_lock_irqsave(&cs->lock, flags);
617 ++(*at_state_p)->seq_index;
619 /* revert to selected idle mode */
621 cs->at_state.pending_commands |= PC_UMMODE;
622 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
623 cs->commands_pending = 1;
625 spin_unlock_irqrestore(&cs->lock, flags);
628 /* B channel assigned: invoke hardware specific handler */
629 cs->ops->close_bchannel(bcs);
631 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
632 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
633 gigaset_isdn_hupD(bcs);
636 /* no B channel assigned: just deallocate */
637 spin_lock_irqsave(&cs->lock, flags);
638 list_del(&(*at_state_p)->list);
641 spin_unlock_irqrestore(&cs->lock, flags);
646 * get a free AT state structure: either one of those associated with the
647 * B channels of the Gigaset device, or if none of those is available,
648 * a newly allocated one with bcs=NULL
649 * The structure should be freed by calling disconnect() after use.
651 static inline struct at_state_t *get_free_channel(struct cardstate *cs,
653 /* cids: >0: siemens-cid
655 * -1: no cid assigned yet
660 struct at_state_t *ret;
662 for (i = 0; i < cs->channels; ++i)
663 if (gigaset_get_channel(cs->bcs + i) >= 0) {
664 ret = &cs->bcs[i].at_state;
669 spin_lock_irqsave(&cs->lock, flags);
670 ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
672 gigaset_at_init(ret, NULL, cs, cid);
673 list_add(&ret->list, &cs->temp_at_states);
675 spin_unlock_irqrestore(&cs->lock, flags);
679 static void init_failed(struct cardstate *cs, int mode)
682 struct at_state_t *at_state;
684 cs->at_state.pending_commands &= ~PC_INIT;
686 cs->mstate = MS_UNINITIALIZED;
687 gigaset_free_channels(cs);
688 for (i = 0; i < cs->channels; ++i) {
689 at_state = &cs->bcs[i].at_state;
690 if (at_state->pending_commands & PC_CID) {
691 at_state->pending_commands &= ~PC_CID;
692 at_state->pending_commands |= PC_NOCID;
693 cs->commands_pending = 1;
698 static void schedule_init(struct cardstate *cs, int state)
700 if (cs->at_state.pending_commands & PC_INIT) {
701 gig_dbg(DEBUG_EVENT, "not scheduling PC_INIT again");
705 cs->mode = M_UNKNOWN;
706 gigaset_block_channels(cs);
707 cs->at_state.pending_commands |= PC_INIT;
708 gig_dbg(DEBUG_EVENT, "Scheduling PC_INIT");
709 cs->commands_pending = 1;
712 /* send an AT command
713 * adding the "AT" prefix, cid and DLE encapsulation as appropriate
715 static void send_command(struct cardstate *cs, const char *cmd,
716 struct at_state_t *at_state)
718 int cid = at_state->cid;
722 buflen = strlen(cmd) + 12; /* DLE ( A T 1 2 3 4 5 <cmd> DLE ) \0 */
723 cb = kmalloc(sizeof(struct cmdbuf_t) + buflen, GFP_ATOMIC);
725 dev_err(cs->dev, "%s: out of memory\n", __func__);
728 if (cid > 0 && cid <= 65535)
729 cb->len = snprintf(cb->buf, buflen,
730 cs->dle ? "\020(AT%d%s\020)" : "AT%d%s",
733 cb->len = snprintf(cb->buf, buflen,
734 cs->dle ? "\020(AT%s\020)" : "AT%s",
738 cb->wake_tasklet = NULL;
739 cs->ops->write_cmd(cs, cb);
742 static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
744 struct at_state_t *at_state;
749 return &cs->at_state;
751 for (i = 0; i < cs->channels; ++i)
752 if (cid == cs->bcs[i].at_state.cid)
753 return &cs->bcs[i].at_state;
755 spin_lock_irqsave(&cs->lock, flags);
757 list_for_each_entry(at_state, &cs->temp_at_states, list)
758 if (cid == at_state->cid) {
759 spin_unlock_irqrestore(&cs->lock, flags);
763 spin_unlock_irqrestore(&cs->lock, flags);
768 static void bchannel_down(struct bc_state *bcs)
770 if (bcs->chstate & CHS_B_UP) {
771 bcs->chstate &= ~CHS_B_UP;
772 gigaset_isdn_hupB(bcs);
775 if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
776 bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
777 gigaset_isdn_hupD(bcs);
780 gigaset_free_channel(bcs);
782 gigaset_bcs_reinit(bcs);
785 static void bchannel_up(struct bc_state *bcs)
787 if (bcs->chstate & CHS_B_UP) {
788 dev_notice(bcs->cs->dev, "%s: B channel already up\n",
793 bcs->chstate |= CHS_B_UP;
794 gigaset_isdn_connB(bcs);
797 static void start_dial(struct at_state_t *at_state, void *data,
800 struct bc_state *bcs = at_state->bcs;
801 struct cardstate *cs = at_state->cs;
802 char **commands = data;
806 bcs->chstate |= CHS_NOTIFY_LL;
808 spin_lock_irqsave(&cs->lock, flags);
809 if (at_state->seq_index != seq_index) {
810 spin_unlock_irqrestore(&cs->lock, flags);
813 spin_unlock_irqrestore(&cs->lock, flags);
815 for (i = 0; i < AT_NUM; ++i) {
816 kfree(bcs->commands[i]);
817 bcs->commands[i] = commands[i];
820 at_state->pending_commands |= PC_CID;
821 gig_dbg(DEBUG_EVENT, "Scheduling PC_CID");
822 cs->commands_pending = 1;
826 for (i = 0; i < AT_NUM; ++i) {
830 at_state->pending_commands |= PC_NOCID;
831 gig_dbg(DEBUG_EVENT, "Scheduling PC_NOCID");
832 cs->commands_pending = 1;
836 static void start_accept(struct at_state_t *at_state)
838 struct cardstate *cs = at_state->cs;
839 struct bc_state *bcs = at_state->bcs;
842 for (i = 0; i < AT_NUM; ++i) {
843 kfree(bcs->commands[i]);
844 bcs->commands[i] = NULL;
847 bcs->commands[AT_PROTO] = kmalloc(9, GFP_ATOMIC);
848 bcs->commands[AT_ISO] = kmalloc(9, GFP_ATOMIC);
849 if (!bcs->commands[AT_PROTO] || !bcs->commands[AT_ISO]) {
850 dev_err(at_state->cs->dev, "out of memory\n");
852 at_state->pending_commands |= PC_HUP;
853 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
854 cs->commands_pending = 1;
858 snprintf(bcs->commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
859 snprintf(bcs->commands[AT_ISO], 9, "^SISO=%u\r", bcs->channel + 1);
861 at_state->pending_commands |= PC_ACCEPT;
862 gig_dbg(DEBUG_EVENT, "Scheduling PC_ACCEPT");
863 cs->commands_pending = 1;
866 static void do_start(struct cardstate *cs)
868 gigaset_free_channels(cs);
870 if (cs->mstate != MS_LOCKED)
871 schedule_init(cs, MS_INIT);
874 gigaset_isdn_start(cs);
877 wake_up(&cs->waitqueue);
880 static void finish_shutdown(struct cardstate *cs)
882 if (cs->mstate != MS_LOCKED) {
883 cs->mstate = MS_UNINITIALIZED;
884 cs->mode = M_UNKNOWN;
887 /* Tell the LL that the device is not available .. */
890 gigaset_isdn_stop(cs);
893 /* The rest is done by cleanup_cs() in process context. */
895 cs->cmd_result = -ENODEV;
897 wake_up(&cs->waitqueue);
900 static void do_shutdown(struct cardstate *cs)
902 gigaset_block_channels(cs);
904 if (cs->mstate == MS_READY) {
905 cs->mstate = MS_SHUTDOWN;
906 cs->at_state.pending_commands |= PC_SHUTDOWN;
907 gig_dbg(DEBUG_EVENT, "Scheduling PC_SHUTDOWN");
908 cs->commands_pending = 1;
913 static void do_stop(struct cardstate *cs)
917 spin_lock_irqsave(&cs->lock, flags);
919 spin_unlock_irqrestore(&cs->lock, flags);
924 /* Entering cid mode or getting a cid failed:
925 * try to initialize the device and try again.
927 * channel >= 0: getting cid for the channel failed
928 * channel < 0: entering cid mode failed
930 * returns 0 on success, <0 on failure
932 static int reinit_and_retry(struct cardstate *cs, int channel)
936 if (--cs->retry_count <= 0)
939 for (i = 0; i < cs->channels; ++i)
940 if (cs->bcs[i].at_state.cid > 0)
945 "Could not enter cid mode. Reinit device and try again.\n");
948 "Could not get a call id. Reinit device and try again.\n");
949 cs->bcs[channel].at_state.pending_commands |= PC_CID;
951 schedule_init(cs, MS_INIT);
955 static int at_state_invalid(struct cardstate *cs,
956 struct at_state_t *test_ptr)
960 struct at_state_t *at_state;
963 spin_lock_irqsave(&cs->lock, flags);
965 if (test_ptr == &cs->at_state)
968 list_for_each_entry(at_state, &cs->temp_at_states, list)
969 if (at_state == test_ptr)
972 for (channel = 0; channel < cs->channels; ++channel)
973 if (&cs->bcs[channel].at_state == test_ptr)
978 spin_unlock_irqrestore(&cs->lock, flags);
982 static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
983 struct at_state_t *at_state)
987 retval = gigaset_isdn_icall(at_state);
992 dev_err(cs->dev, "internal error: disposition=%d\n", retval);
993 /* --v-- fall through --v-- */
997 * Device doc says that would reject the call.
998 * In fact it doesn't.
1000 at_state->pending_commands |= PC_HUP;
1001 cs->commands_pending = 1;
1006 static int do_lock(struct cardstate *cs)
1011 switch (cs->mstate) {
1012 case MS_UNINITIALIZED:
1014 if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
1015 cs->at_state.pending_commands)
1018 for (i = 0; i < cs->channels; ++i)
1019 if (cs->bcs[i].at_state.pending_commands)
1022 if (gigaset_get_channels(cs) < 0)
1033 cs->mstate = MS_LOCKED;
1034 cs->mode = M_UNKNOWN;
1039 static int do_unlock(struct cardstate *cs)
1041 if (cs->mstate != MS_LOCKED)
1044 cs->mstate = MS_UNINITIALIZED;
1045 cs->mode = M_UNKNOWN;
1046 gigaset_free_channels(cs);
1048 schedule_init(cs, MS_INIT);
1053 static void do_action(int action, struct cardstate *cs,
1054 struct bc_state *bcs,
1055 struct at_state_t **p_at_state, char **pp_command,
1056 int *p_genresp, int *p_resp_code,
1059 struct at_state_t *at_state = *p_at_state;
1060 struct at_state_t *at_state2;
1061 unsigned long flags;
1065 unsigned char *s, *e;
1073 at_state->waiting = 1;
1076 cs->at_state.pending_commands &= ~PC_INIT;
1077 cs->cur_at_seq = SEQ_NONE;
1078 cs->mode = M_UNIMODEM;
1079 spin_lock_irqsave(&cs->lock, flags);
1081 spin_unlock_irqrestore(&cs->lock, flags);
1082 gigaset_free_channels(cs);
1083 cs->mstate = MS_READY;
1086 spin_unlock_irqrestore(&cs->lock, flags);
1087 cs->at_state.pending_commands |= PC_CIDMODE;
1088 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1089 cs->commands_pending = 1;
1092 dev_warn(cs->dev, "Could not initialize the device.\n");
1094 init_failed(cs, M_UNKNOWN);
1095 cs->cur_at_seq = SEQ_NONE;
1097 case ACT_CONFIGMODE:
1098 init_failed(cs, M_CONFIG);
1099 cs->cur_at_seq = SEQ_NONE;
1103 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1104 cs->inbuf[0].inputstate &=
1105 ~(INS_command | INS_DLE_command);
1109 cs->inbuf[0].inputstate =
1110 (cs->inbuf[0].inputstate & ~INS_DLE_command)
1114 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1115 gigaset_free_channels(cs);
1116 cs->mstate = MS_READY;
1119 cs->cur_at_seq = SEQ_NONE;
1122 cs->mode = M_UNIMODEM;
1123 cs->cur_at_seq = SEQ_NONE;
1126 cs->cur_at_seq = SEQ_NONE;
1127 if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) {
1128 init_failed(cs, M_UNKNOWN);
1131 if (reinit_and_retry(cs, -1) < 0)
1132 schedule_init(cs, MS_RECOVER);
1135 cs->cur_at_seq = SEQ_NONE;
1136 schedule_init(cs, MS_RECOVER);
1139 /* send "+++" (hangup in unimodem mode) */
1140 if (cs->connected) {
1141 struct cmdbuf_t *cb;
1143 cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC);
1145 dev_err(cs->dev, "%s: out of memory\n",
1149 memcpy(cb->buf, "+++", 3);
1153 cb->wake_tasklet = NULL;
1154 cs->ops->write_cmd(cs, cb);
1158 /* get fresh AT state structure for new CID */
1159 at_state2 = get_free_channel(cs, ev->parameter);
1162 "RING ignored: could not allocate channel structure\n");
1166 /* initialize AT state structure
1167 * note that bcs may be NULL if no B channel is free
1169 at_state2->ConState = 700;
1170 for (i = 0; i < STR_NUM; ++i) {
1171 kfree(at_state2->str_var[i]);
1172 at_state2->str_var[i] = NULL;
1174 at_state2->int_var[VAR_ZCTP] = -1;
1176 spin_lock_irqsave(&cs->lock, flags);
1177 at_state2->timer_expires = RING_TIMEOUT;
1178 at_state2->timer_active = 1;
1179 spin_unlock_irqrestore(&cs->lock, flags);
1182 handle_icall(cs, bcs, at_state);
1185 dev_warn(cs->dev, "Could not shut down the device.\n");
1189 cs->cur_at_seq = SEQ_NONE;
1190 finish_shutdown(cs);
1193 if (cs->onechannel) {
1194 at_state->pending_commands |= PC_DLE1;
1195 cs->commands_pending = 1;
1198 bcs->chstate |= CHS_D_UP;
1199 gigaset_isdn_connD(bcs);
1200 cs->ops->init_bchannel(bcs);
1203 cs->cur_at_seq = SEQ_NONE;
1204 bcs = cs->bcs + cs->curchannel;
1206 bcs->chstate |= CHS_D_UP;
1207 gigaset_isdn_connD(bcs);
1208 cs->ops->init_bchannel(bcs);
1211 at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
1213 case ACT_DISCONNECT:
1214 cs->cur_at_seq = SEQ_NONE;
1216 if (bcs && cs->onechannel && cs->dle) {
1217 /* Check for other open channels not needed:
1218 * DLE only used for M10x with one B channel.
1220 at_state->pending_commands |= PC_DLE0;
1221 cs->commands_pending = 1;
1223 disconnect(p_at_state);
1226 at_state->int_var[VAR_ZDLE] = 0;
1230 cs->cur_at_seq = SEQ_NONE;
1231 at_state2 = &cs->bcs[cs->curchannel].at_state;
1232 disconnect(&at_state2);
1235 cs->cur_at_seq = SEQ_NONE;
1236 dev_warn(cs->dev, "Could not hang up.\n");
1238 if (bcs && cs->onechannel)
1239 at_state->pending_commands |= PC_DLE0;
1241 disconnect(p_at_state);
1242 schedule_init(cs, MS_RECOVER);
1245 cs->cur_at_seq = SEQ_NONE;
1246 dev_warn(cs->dev, "Could not leave DLE mode.\n");
1247 at_state2 = &cs->bcs[cs->curchannel].at_state;
1248 disconnect(&at_state2);
1249 schedule_init(cs, MS_RECOVER);
1252 cs->cur_at_seq = SEQ_NONE;
1254 "Could not enter DLE mode. Trying to hang up.\n");
1255 channel = cs->curchannel;
1256 cs->bcs[channel].at_state.pending_commands |= PC_HUP;
1257 cs->commands_pending = 1;
1260 case ACT_CID: /* got cid; start dialing */
1261 cs->cur_at_seq = SEQ_NONE;
1262 channel = cs->curchannel;
1263 if (ev->parameter > 0 && ev->parameter <= 65535) {
1264 cs->bcs[channel].at_state.cid = ev->parameter;
1265 cs->bcs[channel].at_state.pending_commands |=
1267 cs->commands_pending = 1;
1270 /* bad cid: fall through */
1272 cs->cur_at_seq = SEQ_NONE;
1273 channel = cs->curchannel;
1274 if (reinit_and_retry(cs, channel) < 0) {
1276 "Could not get a call ID. Cannot dial.\n");
1277 at_state2 = &cs->bcs[channel].at_state;
1278 disconnect(&at_state2);
1282 cs->cur_at_seq = SEQ_NONE;
1283 at_state2 = &cs->bcs[cs->curchannel].at_state;
1284 disconnect(&at_state2);
1289 cs->cur_at_seq = SEQ_NONE;
1292 case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL procssng */
1293 disconnect(p_at_state);
1296 case ACT_ABORTDIAL: /* error/timeout during dial preparation */
1297 cs->cur_at_seq = SEQ_NONE;
1298 at_state->pending_commands |= PC_HUP;
1299 cs->commands_pending = 1;
1302 case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
1303 case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
1304 case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
1305 at_state->pending_commands |= PC_HUP;
1306 cs->commands_pending = 1;
1308 case ACT_GETSTRING: /* warning: RING, ZDLE, ...
1309 are not handled properly anymore */
1310 at_state->getstring = 1;
1315 *p_resp_code = RSP_ERROR;
1320 if (!strcmp(s, "OK")) {
1321 /* OK without version string: assume old response */
1323 *p_resp_code = RSP_NONE;
1327 for (i = 0; i < 4; ++i) {
1328 val = simple_strtoul(s, (char **) &e, 10);
1329 if (val > INT_MAX || e == s)
1334 } else if (*e != '.')
1342 *p_resp_code = RSP_ERROR;
1348 if (cs->gotfwver == 0) {
1350 gig_dbg(DEBUG_EVENT,
1351 "firmware version %02d.%03d.%02d.%02d",
1352 cs->fwver[0], cs->fwver[1],
1353 cs->fwver[2], cs->fwver[3]);
1359 dev_err(cs->dev, "could not read firmware version.\n");
1362 gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d",
1363 __func__, at_state->ConState);
1364 cs->cur_at_seq = SEQ_NONE;
1367 gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
1368 __func__, ev->type, at_state->ConState);
1371 dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n",
1372 __func__, ev->type, at_state->ConState);
1375 dev_warn(cs->dev, "cause code %04x in connection state %d.\n",
1376 ev->parameter, at_state->ConState);
1379 /* events from the LL */
1382 start_dial(at_state, ev->ptr, ev->parameter);
1385 start_accept(at_state);
1388 at_state->pending_commands |= PC_HUP;
1389 gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP");
1390 cs->commands_pending = 1;
1393 /* hotplug events */
1402 /* events from the interface */
1405 cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
1407 wake_up(&cs->waitqueue);
1410 if (ev->parameter != 0)
1411 cs->cmd_result = -EINVAL;
1412 else if (cs->gotfwver != 1) {
1413 cs->cmd_result = -ENOENT;
1415 memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
1419 wake_up(&cs->waitqueue);
1422 /* events from the proc file system */
1424 case ACT_PROC_CIDMODE:
1425 spin_lock_irqsave(&cs->lock, flags);
1426 if (ev->parameter != cs->cidmode) {
1427 cs->cidmode = ev->parameter;
1428 if (ev->parameter) {
1429 cs->at_state.pending_commands |= PC_CIDMODE;
1430 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1432 cs->at_state.pending_commands |= PC_UMMODE;
1433 gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE");
1435 cs->commands_pending = 1;
1437 spin_unlock_irqrestore(&cs->lock, flags);
1439 wake_up(&cs->waitqueue);
1442 /* events from the hardware drivers */
1444 case ACT_NOTIFY_BC_DOWN:
1447 case ACT_NOTIFY_BC_UP:
1456 if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
1457 *pp_command = at_state->bcs->commands[action - ACT_CMD];
1460 *p_resp_code = RSP_NULL;
1463 dev_err(cs->dev, "%s: action==%d!\n", __func__, action);
1467 /* State machine to do the calling and hangup procedure */
1468 static void process_event(struct cardstate *cs, struct event_t *ev)
1470 struct bc_state *bcs;
1471 char *p_command = NULL;
1472 struct reply_t *rep;
1475 int resp_code = RSP_ERROR;
1476 struct at_state_t *at_state;
1479 unsigned long flags;
1482 at_state = at_state_from_cid(cs, ev->cid);
1484 gig_dbg(DEBUG_EVENT, "event %d for invalid cid %d",
1486 gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
1491 at_state = ev->at_state;
1492 if (at_state_invalid(cs, at_state)) {
1493 gig_dbg(DEBUG_EVENT, "event for invalid at_state %p",
1499 gig_dbg(DEBUG_EVENT, "connection state %d, event %d",
1500 at_state->ConState, ev->type);
1502 bcs = at_state->bcs;
1504 /* Setting the pointer to the dial array */
1505 rep = at_state->replystruct;
1507 spin_lock_irqsave(&cs->lock, flags);
1508 if (ev->type == EV_TIMEOUT) {
1509 if (ev->parameter != at_state->timer_index
1510 || !at_state->timer_active) {
1511 ev->type = RSP_NONE; /* old timeout */
1512 gig_dbg(DEBUG_EVENT, "old timeout");
1514 if (at_state->waiting)
1515 gig_dbg(DEBUG_EVENT, "stopped waiting");
1517 gig_dbg(DEBUG_EVENT, "timeout occurred");
1520 spin_unlock_irqrestore(&cs->lock, flags);
1522 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1523 or at_state->str_var[STR_XXXX], set it */
1524 if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
1525 index = ev->type - RSP_VAR;
1526 at_state->int_var[index] = ev->parameter;
1527 } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
1528 index = ev->type - RSP_STR;
1529 kfree(at_state->str_var[index]);
1530 at_state->str_var[index] = ev->ptr;
1531 ev->ptr = NULL; /* prevent process_events() from
1535 if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
1536 at_state->getstring = 0;
1538 /* Search row in dial array which matches modem response and current
1541 rcode = rep->resp_code;
1542 if (rcode == RSP_LAST) {
1543 /* found nothing...*/
1544 dev_warn(cs->dev, "%s: rcode=RSP_LAST: "
1545 "resp_code %d in ConState %d!\n",
1546 __func__, ev->type, at_state->ConState);
1549 if ((rcode == RSP_ANY || rcode == ev->type)
1550 && ((int) at_state->ConState >= rep->min_ConState)
1551 && (rep->max_ConState < 0
1552 || (int) at_state->ConState <= rep->max_ConState)
1553 && (rep->parameter < 0 || rep->parameter == ev->parameter))
1557 p_command = rep->command;
1559 at_state->waiting = 0;
1560 for (curact = 0; curact < MAXACT; ++curact) {
1561 /* The row tells us what we should do ..
1563 do_action(rep->action[curact], cs, bcs, &at_state, &p_command,
1564 &genresp, &resp_code, ev);
1566 /* at_state destroyed by disconnect */
1570 /* Jump to the next con-state regarding the array */
1571 if (rep->new_ConState >= 0)
1572 at_state->ConState = rep->new_ConState;
1575 spin_lock_irqsave(&cs->lock, flags);
1576 at_state->timer_expires = 0;
1577 at_state->timer_active = 0;
1578 spin_unlock_irqrestore(&cs->lock, flags);
1579 gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
1581 /* Send command to modem if not NULL... */
1584 send_command(cs, p_command, at_state);
1586 gigaset_add_event(cs, at_state, RSP_NODEV,
1590 spin_lock_irqsave(&cs->lock, flags);
1591 if (!rep->timeout) {
1592 at_state->timer_expires = 0;
1593 at_state->timer_active = 0;
1594 } else if (rep->timeout > 0) { /* new timeout */
1595 at_state->timer_expires = rep->timeout * 10;
1596 at_state->timer_active = 1;
1597 ++at_state->timer_index;
1599 spin_unlock_irqrestore(&cs->lock, flags);
1603 static void schedule_sequence(struct cardstate *cs,
1604 struct at_state_t *at_state, int sequence)
1606 cs->cur_at_seq = sequence;
1607 gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
1610 static void process_command_flags(struct cardstate *cs)
1612 struct at_state_t *at_state = NULL;
1613 struct bc_state *bcs;
1616 unsigned long flags;
1618 cs->commands_pending = 0;
1620 if (cs->cur_at_seq) {
1621 gig_dbg(DEBUG_EVENT, "not searching scheduled commands: busy");
1625 gig_dbg(DEBUG_EVENT, "searching scheduled commands");
1627 sequence = SEQ_NONE;
1629 /* clear pending_commands and hangup channels on shutdown */
1630 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1631 cs->at_state.pending_commands &= ~PC_CIDMODE;
1632 for (i = 0; i < cs->channels; ++i) {
1634 at_state = &bcs->at_state;
1635 at_state->pending_commands &=
1636 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1637 if (at_state->cid > 0)
1638 at_state->pending_commands |= PC_HUP;
1639 if (at_state->pending_commands & PC_CID) {
1640 at_state->pending_commands |= PC_NOCID;
1641 at_state->pending_commands &= ~PC_CID;
1646 /* clear pending_commands and hangup channels on reset */
1647 if (cs->at_state.pending_commands & PC_INIT) {
1648 cs->at_state.pending_commands &= ~PC_CIDMODE;
1649 for (i = 0; i < cs->channels; ++i) {
1651 at_state = &bcs->at_state;
1652 at_state->pending_commands &=
1653 ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
1654 if (at_state->cid > 0)
1655 at_state->pending_commands |= PC_HUP;
1656 if (cs->mstate == MS_RECOVER) {
1657 if (at_state->pending_commands & PC_CID) {
1658 at_state->pending_commands |= PC_NOCID;
1659 at_state->pending_commands &= ~PC_CID;
1665 /* only switch back to unimodem mode if no commands are pending and
1666 * no channels are up */
1667 spin_lock_irqsave(&cs->lock, flags);
1668 if (cs->at_state.pending_commands == PC_UMMODE
1670 && list_empty(&cs->temp_at_states)
1671 && cs->mode == M_CID) {
1672 sequence = SEQ_UMMODE;
1673 at_state = &cs->at_state;
1674 for (i = 0; i < cs->channels; ++i) {
1676 if (bcs->at_state.pending_commands ||
1677 bcs->at_state.cid > 0) {
1678 sequence = SEQ_NONE;
1683 spin_unlock_irqrestore(&cs->lock, flags);
1684 cs->at_state.pending_commands &= ~PC_UMMODE;
1685 if (sequence != SEQ_NONE) {
1686 schedule_sequence(cs, at_state, sequence);
1690 for (i = 0; i < cs->channels; ++i) {
1692 if (bcs->at_state.pending_commands & PC_HUP) {
1694 cs->curchannel = bcs->channel;
1695 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1698 bcs->at_state.pending_commands &= ~PC_HUP;
1699 if (bcs->at_state.pending_commands & PC_CID) {
1700 /* not yet dialing: PC_NOCID is sufficient */
1701 bcs->at_state.pending_commands |= PC_NOCID;
1702 bcs->at_state.pending_commands &= ~PC_CID;
1704 schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
1708 if (bcs->at_state.pending_commands & PC_NOCID) {
1709 bcs->at_state.pending_commands &= ~PC_NOCID;
1710 cs->curchannel = bcs->channel;
1711 schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
1713 } else if (bcs->at_state.pending_commands & PC_DLE0) {
1714 bcs->at_state.pending_commands &= ~PC_DLE0;
1715 cs->curchannel = bcs->channel;
1716 schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
1721 list_for_each_entry(at_state, &cs->temp_at_states, list)
1722 if (at_state->pending_commands & PC_HUP) {
1723 at_state->pending_commands &= ~PC_HUP;
1724 schedule_sequence(cs, at_state, SEQ_HUP);
1728 if (cs->at_state.pending_commands & PC_INIT) {
1729 cs->at_state.pending_commands &= ~PC_INIT;
1731 cs->inbuf->inputstate = INS_command;
1732 schedule_sequence(cs, &cs->at_state, SEQ_INIT);
1735 if (cs->at_state.pending_commands & PC_SHUTDOWN) {
1736 cs->at_state.pending_commands &= ~PC_SHUTDOWN;
1737 schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
1740 if (cs->at_state.pending_commands & PC_CIDMODE) {
1741 cs->at_state.pending_commands &= ~PC_CIDMODE;
1742 if (cs->mode == M_UNIMODEM) {
1743 cs->retry_count = 1;
1744 schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
1749 for (i = 0; i < cs->channels; ++i) {
1751 if (bcs->at_state.pending_commands & PC_DLE1) {
1752 bcs->at_state.pending_commands &= ~PC_DLE1;
1753 cs->curchannel = bcs->channel;
1754 schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
1757 if (bcs->at_state.pending_commands & PC_ACCEPT) {
1758 bcs->at_state.pending_commands &= ~PC_ACCEPT;
1759 schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
1762 if (bcs->at_state.pending_commands & PC_DIAL) {
1763 bcs->at_state.pending_commands &= ~PC_DIAL;
1764 schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
1767 if (bcs->at_state.pending_commands & PC_CID) {
1770 cs->at_state.pending_commands |= PC_CIDMODE;
1771 gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE");
1772 cs->commands_pending = 1;
1775 schedule_init(cs, MS_INIT);
1778 bcs->at_state.pending_commands &= ~PC_CID;
1779 cs->curchannel = bcs->channel;
1780 cs->retry_count = 2;
1781 schedule_sequence(cs, &cs->at_state, SEQ_CID);
1787 static void process_events(struct cardstate *cs)
1790 unsigned head, tail;
1792 int check_flags = 0;
1794 unsigned long flags;
1796 spin_lock_irqsave(&cs->ev_lock, flags);
1799 for (i = 0; i < 2 * MAX_EVENTS; ++i) {
1802 if (!check_flags && !cs->commands_pending)
1805 spin_unlock_irqrestore(&cs->ev_lock, flags);
1806 process_command_flags(cs);
1807 spin_lock_irqsave(&cs->ev_lock, flags);
1810 if (!cs->commands_pending)
1816 ev = cs->events + head;
1817 was_busy = cs->cur_at_seq != SEQ_NONE;
1818 spin_unlock_irqrestore(&cs->ev_lock, flags);
1819 process_event(cs, ev);
1820 spin_lock_irqsave(&cs->ev_lock, flags);
1823 if (was_busy && cs->cur_at_seq == SEQ_NONE)
1826 head = (head + 1) % MAX_EVENTS;
1830 spin_unlock_irqrestore(&cs->ev_lock, flags);
1832 if (i == 2 * MAX_EVENTS) {
1834 "infinite loop in process_events; aborting.\n");
1838 /* tasklet scheduled on any event received from the Gigaset device
1840 * data ISDN controller state structure
1842 void gigaset_handle_event(unsigned long data)
1844 struct cardstate *cs = (struct cardstate *) data;
1846 /* handle incoming data on control/common channel */
1847 if (cs->inbuf->head != cs->inbuf->tail) {
1848 gig_dbg(DEBUG_INTR, "processing new data");
1849 cs->ops->handle_input(cs->inbuf);