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 */
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_UNKNOWN -4 /* unknown response */
72 #define RSP_FAIL -5 /* internal error */
73 #define RSP_INVAL -6 /* invalid response */
76 #define RSP_STRING -20
78 #define RSP_RETRYFAIL -22
86 /* actions for process_response */
90 #define ACT_FAILINIT 3
91 #define ACT_HUPMODEM 4
92 #define ACT_CONFIGMODE 5
96 #define ACT_FAILDLE0 9
97 #define ACT_FAILDLE1 10
100 #define ACT_FAILCID 13
102 #define ACT_FAILSDOWN 15
105 #define ACT_DIALING 18
106 #define ACT_ABORTDIAL 19
107 #define ACT_DISCONNECT 20
108 #define ACT_CONNECT 21
109 #define ACT_REMOTEREJECT 22
110 #define ACT_CONNTIMEOUT 23
111 #define ACT_REMOTEHUP 24
112 #define ACT_ABORTHUP 25
114 #define ACT_ACCEPTED 27
115 #define ACT_ABORTACCEPT 28
116 #define ACT_TIMEOUT 29
117 #define ACT_GETSTRING 30
118 #define ACT_SETVER 31
119 #define ACT_FAILVER 32
120 #define ACT_GOTVER 33
123 #define ACT_ABORTCID 36
125 #define ACT_NOTIFY_BC_DOWN 38
126 #define ACT_NOTIFY_BC_UP 39
128 #define ACT_ACCEPT 41
130 #define ACT_IF_LOCK 44
133 #define ACT_FAKEDLE0 47
134 #define ACT_FAKEHUP 48
135 #define ACT_FAKESDOWN 49
136 #define ACT_SHUTDOWN 50
137 #define ACT_PROC_CIDMODE 51
138 #define ACT_UMODESET 52
139 #define ACT_FAILUMODE 53
140 #define ACT_CMODESET 54
141 #define ACT_FAILCMODE 55
142 #define ACT_IF_VER 56
145 /* at command sequences */
151 #define SEQ_NOCID 350
154 #define SEQ_ACCEPT 720
155 #define SEQ_SHUTDOWN 500
156 #define SEQ_CIDMODE 10
157 #define SEQ_UMMODE 11
160 /* 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid),
161 * 400: hup, 500: reset, 600: dial, 700: ring */
162 struct reply_t gigaset_tab_nocid
[] =
164 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
167 /* initialize device, set cid mode if possible */
168 {RSP_INIT
, -1, -1, SEQ_INIT
, 100, 1, {ACT_TIMEOUT
} },
170 {EV_TIMEOUT
, 100, 100, -1, 101, 3, {0}, "Z\r"},
171 {RSP_OK
, 101, 103, -1, 120, 5, {ACT_GETSTRING
},
174 {EV_TIMEOUT
, 101, 101, -1, 102, 5, {0}, "Z\r"},
175 {RSP_ERROR
, 101, 101, -1, 102, 5, {0}, "Z\r"},
177 {EV_TIMEOUT
, 102, 102, -1, 108, 5, {ACT_SETDLE1
},
179 {RSP_OK
, 108, 108, -1, 104, -1},
180 {RSP_ZDLE
, 104, 104, 0, 103, 5, {0}, "Z\r"},
181 {EV_TIMEOUT
, 104, 104, -1, 0, 0, {ACT_FAILINIT
} },
182 {RSP_ERROR
, 108, 108, -1, 0, 0, {ACT_FAILINIT
} },
184 {EV_TIMEOUT
, 108, 108, -1, 105, 2, {ACT_SETDLE0
,
187 {EV_TIMEOUT
, 105, 105, -1, 103, 5, {0}, "Z\r"},
189 {RSP_ERROR
, 102, 102, -1, 107, 5, {0}, "^GETPRE\r"},
190 {RSP_OK
, 107, 107, -1, 0, 0, {ACT_CONFIGMODE
} },
191 {RSP_ERROR
, 107, 107, -1, 0, 0, {ACT_FAILINIT
} },
192 {EV_TIMEOUT
, 107, 107, -1, 0, 0, {ACT_FAILINIT
} },
194 {RSP_ERROR
, 103, 103, -1, 0, 0, {ACT_FAILINIT
} },
195 {EV_TIMEOUT
, 103, 103, -1, 0, 0, {ACT_FAILINIT
} },
197 {RSP_STRING
, 120, 120, -1, 121, -1, {ACT_SETVER
} },
199 {EV_TIMEOUT
, 120, 121, -1, 0, 0, {ACT_FAILVER
,
201 {RSP_ERROR
, 120, 121, -1, 0, 0, {ACT_FAILVER
,
203 {RSP_OK
, 121, 121, -1, 0, 0, {ACT_GOTVER
,
207 {RSP_INIT
, 0, 0, SEQ_DLE0
, 201, 5, {0}, "^SDLE=0\r"},
208 {RSP_OK
, 201, 201, -1, 202, -1},
209 {RSP_ZDLE
, 202, 202, 0, 0, 0, {ACT_DLE0
} },
210 {RSP_NODEV
, 200, 249, -1, 0, 0, {ACT_FAKEDLE0
} },
211 {RSP_ERROR
, 200, 249, -1, 0, 0, {ACT_FAILDLE0
} },
212 {EV_TIMEOUT
, 200, 249, -1, 0, 0, {ACT_FAILDLE0
} },
215 {RSP_INIT
, 0, 0, SEQ_DLE1
, 251, 5, {0}, "^SDLE=1\r"},
216 {RSP_OK
, 251, 251, -1, 252, -1},
217 {RSP_ZDLE
, 252, 252, 1, 0, 0, {ACT_DLE1
} },
218 {RSP_ERROR
, 250, 299, -1, 0, 0, {ACT_FAILDLE1
} },
219 {EV_TIMEOUT
, 250, 299, -1, 0, 0, {ACT_FAILDLE1
} },
222 {RSP_RING
, -1, -1, -1, -1, -1, {ACT_RING
} },
225 {RSP_INIT
, 0, 0, SEQ_CID
, 301, 5, {0}, "^SGCI?\r"},
226 {RSP_OK
, 301, 301, -1, 302, -1},
227 {RSP_ZGCI
, 302, 302, -1, 0, 0, {ACT_CID
} },
228 {RSP_ERROR
, 301, 349, -1, 0, 0, {ACT_FAILCID
} },
229 {EV_TIMEOUT
, 301, 349, -1, 0, 0, {ACT_FAILCID
} },
232 {RSP_INIT
, 0, 0, SEQ_CIDMODE
, 150, 5, {0}, "^SGCI=1\r"},
233 {RSP_OK
, 150, 150, -1, 0, 0, {ACT_CMODESET
} },
234 {RSP_ERROR
, 150, 150, -1, 0, 0, {ACT_FAILCMODE
} },
235 {EV_TIMEOUT
, 150, 150, -1, 0, 0, {ACT_FAILCMODE
} },
238 {RSP_INIT
, 0, 0, SEQ_UMMODE
, 160, 5, {0}, "Z\r"},
239 {RSP_OK
, 160, 160, -1, 0, 0, {ACT_UMODESET
} },
240 {RSP_ERROR
, 160, 160, -1, 0, 0, {ACT_FAILUMODE
} },
241 {EV_TIMEOUT
, 160, 160, -1, 0, 0, {ACT_FAILUMODE
} },
243 /* abort getting cid */
244 {RSP_INIT
, 0, 0, SEQ_NOCID
, 0, 0, {ACT_ABORTCID
} },
247 {RSP_INIT
, 0, 0, SEQ_SHUTDOWN
, 504, 5, {0}, "Z\r"},
248 {RSP_OK
, 504, 504, -1, 0, 0, {ACT_SDOWN
} },
249 {RSP_ERROR
, 501, 599, -1, 0, 0, {ACT_FAILSDOWN
} },
250 {EV_TIMEOUT
, 501, 599, -1, 0, 0, {ACT_FAILSDOWN
} },
251 {RSP_NODEV
, 501, 599, -1, 0, 0, {ACT_FAKESDOWN
} },
253 {EV_PROC_CIDMODE
, -1, -1, -1, -1, -1, {ACT_PROC_CIDMODE
} },
254 {EV_IF_LOCK
, -1, -1, -1, -1, -1, {ACT_IF_LOCK
} },
255 {EV_IF_VER
, -1, -1, -1, -1, -1, {ACT_IF_VER
} },
256 {EV_START
, -1, -1, -1, -1, -1, {ACT_START
} },
257 {EV_STOP
, -1, -1, -1, -1, -1, {ACT_STOP
} },
258 {EV_SHUTDOWN
, -1, -1, -1, -1, -1, {ACT_SHUTDOWN
} },
261 {RSP_ERROR
, -1, -1, -1, -1, -1, {ACT_ERROR
} },
262 {RSP_ZCFGT
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
263 {RSP_ZCFG
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
264 {RSP_ZLOG
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
265 {RSP_ZMWI
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
266 {RSP_ZABINFO
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
267 {RSP_ZSMLSTCHG
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
269 {RSP_ZCAU
, -1, -1, -1, -1, -1, {ACT_ZCAU
} },
270 {RSP_NONE
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
271 {RSP_ANY
, -1, -1, -1, -1, -1, {ACT_WARN
} },
275 /* 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring,
276 * 400: hup, 750: accepted icall */
277 struct reply_t gigaset_tab_cid
[] =
279 /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout,
283 {EV_DIAL
, -1, -1, -1, -1, -1, {ACT_DIAL
} },
284 {RSP_INIT
, 0, 0, SEQ_DIAL
, 601, 5, {ACT_CMD
+AT_BC
} },
285 {RSP_OK
, 601, 601, -1, 602, 5, {ACT_CMD
+AT_HLC
} },
286 {RSP_NULL
, 602, 602, -1, 603, 5, {ACT_CMD
+AT_PROTO
} },
287 {RSP_OK
, 602, 602, -1, 603, 5, {ACT_CMD
+AT_PROTO
} },
288 {RSP_OK
, 603, 603, -1, 604, 5, {ACT_CMD
+AT_TYPE
} },
289 {RSP_OK
, 604, 604, -1, 605, 5, {ACT_CMD
+AT_MSN
} },
290 {RSP_NULL
, 605, 605, -1, 606, 5, {ACT_CMD
+AT_CLIP
} },
291 {RSP_OK
, 605, 605, -1, 606, 5, {ACT_CMD
+AT_CLIP
} },
292 {RSP_NULL
, 606, 606, -1, 607, 5, {ACT_CMD
+AT_ISO
} },
293 {RSP_OK
, 606, 606, -1, 607, 5, {ACT_CMD
+AT_ISO
} },
294 {RSP_OK
, 607, 607, -1, 608, 5, {0}, "+VLS=17\r"},
295 {RSP_OK
, 608, 608, -1, 609, -1},
296 {RSP_ZSAU
, 609, 609, ZSAU_PROCEEDING
, 610, 5, {ACT_CMD
+AT_DIAL
} },
297 {RSP_OK
, 610, 610, -1, 650, 0, {ACT_DIALING
} },
299 {RSP_ERROR
, 601, 610, -1, 0, 0, {ACT_ABORTDIAL
} },
300 {EV_TIMEOUT
, 601, 610, -1, 0, 0, {ACT_ABORTDIAL
} },
302 /* optional dialing responses */
303 {EV_BC_OPEN
, 650, 650, -1, 651, -1},
304 {RSP_ZVLS
, 609, 651, 17, -1, -1, {ACT_DEBUG
} },
305 {RSP_ZCTP
, 610, 651, -1, -1, -1, {ACT_DEBUG
} },
306 {RSP_ZCPN
, 610, 651, -1, -1, -1, {ACT_DEBUG
} },
307 {RSP_ZSAU
, 650, 651, ZSAU_CALL_DELIVERED
, -1, -1, {ACT_DEBUG
} },
310 {RSP_ZSAU
, 650, 650, ZSAU_ACTIVE
, 800, -1, {ACT_CONNECT
} },
311 {RSP_ZSAU
, 651, 651, ZSAU_ACTIVE
, 800, -1, {ACT_CONNECT
,
313 {RSP_ZSAU
, 750, 750, ZSAU_ACTIVE
, 800, -1, {ACT_CONNECT
} },
314 {RSP_ZSAU
, 751, 751, ZSAU_ACTIVE
, 800, -1, {ACT_CONNECT
,
316 {EV_BC_OPEN
, 800, 800, -1, 800, -1, {ACT_NOTIFY_BC_UP
} },
319 {RSP_ZSAU
, 650, 651, ZSAU_DISCONNECT_IND
, 0, 0, {ACT_REMOTEREJECT
} },
320 {RSP_ZSAU
, 750, 751, ZSAU_DISCONNECT_IND
, 0, 0, {ACT_REMOTEHUP
} },
321 {RSP_ZSAU
, 800, 800, ZSAU_DISCONNECT_IND
, 0, 0, {ACT_REMOTEHUP
} },
324 {EV_HUP
, -1, -1, -1, -1, -1, {ACT_HUP
} },
325 {RSP_INIT
, -1, -1, SEQ_HUP
, 401, 5, {0}, "+VLS=0\r"},
326 {RSP_OK
, 401, 401, -1, 402, 5},
327 {RSP_ZVLS
, 402, 402, 0, 403, 5},
328 {RSP_ZSAU
, 403, 403, ZSAU_DISCONNECT_REQ
, -1, -1, {ACT_DEBUG
} },
329 {RSP_ZSAU
, 403, 403, ZSAU_NULL
, 0, 0, {ACT_DISCONNECT
} },
330 {RSP_NODEV
, 401, 403, -1, 0, 0, {ACT_FAKEHUP
} },
331 {RSP_ERROR
, 401, 401, -1, 0, 0, {ACT_ABORTHUP
} },
332 {EV_TIMEOUT
, 401, 403, -1, 0, 0, {ACT_ABORTHUP
} },
334 {EV_BC_CLOSED
, 0, 0, -1, 0, -1, {ACT_NOTIFY_BC_DOWN
} },
337 {RSP_ZBC
, 700, 700, -1, -1, -1, {0} },
338 {RSP_ZHLC
, 700, 700, -1, -1, -1, {0} },
339 {RSP_NMBR
, 700, 700, -1, -1, -1, {0} },
340 {RSP_ZCPN
, 700, 700, -1, -1, -1, {0} },
341 {RSP_ZCTP
, 700, 700, -1, -1, -1, {0} },
342 {EV_TIMEOUT
, 700, 700, -1, 720, 720, {ACT_ICALL
} },
343 {EV_BC_CLOSED
, 720, 720, -1, 0, -1, {ACT_NOTIFY_BC_DOWN
} },
346 {EV_ACCEPT
, -1, -1, -1, -1, -1, {ACT_ACCEPT
} },
347 {RSP_INIT
, 720, 720, SEQ_ACCEPT
, 721, 5, {ACT_CMD
+AT_PROTO
} },
348 {RSP_OK
, 721, 721, -1, 722, 5, {ACT_CMD
+AT_ISO
} },
349 {RSP_OK
, 722, 722, -1, 723, 5, {0}, "+VLS=17\r"},
350 {RSP_OK
, 723, 723, -1, 724, 5, {0} },
351 {RSP_ZVLS
, 724, 724, 17, 750, 50, {ACT_ACCEPTED
} },
352 {RSP_ERROR
, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT
} },
353 {EV_TIMEOUT
, 721, 729, -1, 0, 0, {ACT_ABORTACCEPT
} },
354 {RSP_ZSAU
, 700, 729, ZSAU_NULL
, 0, 0, {ACT_ABORTACCEPT
} },
355 {RSP_ZSAU
, 700, 729, ZSAU_ACTIVE
, 0, 0, {ACT_ABORTACCEPT
} },
356 {RSP_ZSAU
, 700, 729, ZSAU_DISCONNECT_IND
, 0, 0, {ACT_ABORTACCEPT
} },
358 {EV_BC_OPEN
, 750, 750, -1, 751, -1},
359 {EV_TIMEOUT
, 750, 751, -1, 0, 0, {ACT_CONNTIMEOUT
} },
361 /* B channel closed (general case) */
362 {EV_BC_CLOSED
, -1, -1, -1, -1, -1, {ACT_NOTIFY_BC_DOWN
} },
365 {RSP_ZCON
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
366 {RSP_ZCCR
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
367 {RSP_ZAOC
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
368 {RSP_ZCSTR
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
370 {RSP_ZCAU
, -1, -1, -1, -1, -1, {ACT_ZCAU
} },
371 {RSP_NONE
, -1, -1, -1, -1, -1, {ACT_DEBUG
} },
372 {RSP_ANY
, -1, -1, -1, -1, -1, {ACT_WARN
} },
377 static const struct resp_type_t
{
378 unsigned char *response
;
383 {"OK", RSP_OK
, RT_NOTHING
},
384 {"ERROR", RSP_ERROR
, RT_NOTHING
},
385 {"ZSAU", RSP_ZSAU
, RT_ZSAU
},
386 {"ZCAU", RSP_ZCAU
, RT_ZCAU
},
387 {"RING", RSP_RING
, RT_RING
},
388 {"ZGCI", RSP_ZGCI
, RT_NUMBER
},
389 {"ZVLS", RSP_ZVLS
, RT_NUMBER
},
390 {"ZCTP", RSP_ZCTP
, RT_NUMBER
},
391 {"ZDLE", RSP_ZDLE
, RT_NUMBER
},
392 {"ZCFGT", RSP_ZCFGT
, RT_NUMBER
},
393 {"ZCCR", RSP_ZCCR
, RT_NUMBER
},
394 {"ZMWI", RSP_ZMWI
, RT_NUMBER
},
395 {"ZHLC", RSP_ZHLC
, RT_STRING
},
396 {"ZBC", RSP_ZBC
, RT_STRING
},
397 {"NMBR", RSP_NMBR
, RT_STRING
},
398 {"ZCPN", RSP_ZCPN
, RT_STRING
},
399 {"ZCON", RSP_ZCON
, RT_STRING
},
400 {"ZAOC", RSP_ZAOC
, RT_STRING
},
401 {"ZCSTR", RSP_ZCSTR
, RT_STRING
},
402 {"ZCFG", RSP_ZCFG
, RT_HEX
},
403 {"ZLOG", RSP_ZLOG
, RT_NOTHING
},
404 {"ZABINFO", RSP_ZABINFO
, RT_NOTHING
},
405 {"ZSMLSTCHG", RSP_ZSMLSTCHG
, RT_NOTHING
},
409 static const struct zsau_resp_t
{
414 {"OUTGOING_CALL_PROCEEDING", ZSAU_OUTGOING_CALL_PROCEEDING
},
415 {"CALL_DELIVERED", ZSAU_CALL_DELIVERED
},
416 {"ACTIVE", ZSAU_ACTIVE
},
417 {"DISCONNECT_IND", ZSAU_DISCONNECT_IND
},
419 {"DISCONNECT_REQ", ZSAU_DISCONNECT_REQ
},
424 * Get integer from char-pointer
426 static int isdn_getnum(char *p
)
430 gig_dbg(DEBUG_EVENT
, "string: %s", p
);
432 while (*p
>= '0' && *p
<= '9')
433 v
= ((v
< 0) ? 0 : (v
* 10)) + (int) ((*p
++) - '0');
435 v
= -1; /* invalid Character */
440 * Get integer from char-pointer
442 static int isdn_gethex(char *p
)
447 gig_dbg(DEBUG_EVENT
, "string: %s", p
);
453 if (v
> (INT_MAX
- 15) / 16)
456 if (c
>= '0' && c
<= '9')
458 else if (c
>= 'a' && c
<= 'f')
460 else if (c
>= 'A' && c
<= 'F')
470 /* retrieve CID from parsed response
471 * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
473 static int cid_of_response(char *s
)
478 return 0; /* no CID separator */
479 cid
= isdn_getnum(s
);
481 return 0; /* CID not numeric */
482 if (cid
< 1 || cid
> 65535)
483 return -1; /* CID out of range */
488 * gigaset_handle_modem_response() - process received modem response
489 * @cs: device descriptor structure.
491 * Called by asyncdata/isocdata if a block of data received from the
492 * device must be processed as a modem command response. The data is
493 * already in the cs structure.
495 void gigaset_handle_modem_response(struct cardstate
*cs
)
497 unsigned char *argv
[MAX_REC_PARAMS
+ 1];
500 const struct resp_type_t
*rt
;
501 const struct zsau_resp_t
*zr
;
504 unsigned next
, tail
, head
;
505 struct event_t
*event
;
515 /* ignore additional LFs/CRs (M10x config mode or cx100) */
516 gig_dbg(DEBUG_MCMD
, "skipped EOL [%02X]", cs
->respdata
[len
]);
519 cs
->respdata
[len
] = 0;
520 argv
[0] = cs
->respdata
;
522 if (cs
->at_state
.getstring
) {
523 /* getstring only allowed without cid at the moment */
524 cs
->at_state
.getstring
= 0;
529 for (i
= 0; i
< len
; i
++)
530 switch (cs
->respdata
[i
]) {
534 if (params
> MAX_REC_PARAMS
) {
536 "too many parameters in response\n");
537 /* need last parameter (might be CID) */
540 argv
[params
++] = cs
->respdata
+ i
+ 1;
544 cid
= params
> 1 ? cid_of_response(argv
[params
-1]) : 0;
546 gigaset_add_event(cs
, &cs
->at_state
, RSP_INVAL
,
551 for (j
= 1; j
< params
; ++j
)
554 gig_dbg(DEBUG_EVENT
, "CMD received: %s", argv
[0]);
557 gig_dbg(DEBUG_EVENT
, "CID: %s", argv
[params
]);
559 gig_dbg(DEBUG_EVENT
, "available params: %d", params
- 1);
560 for (j
= 1; j
< params
; j
++)
561 gig_dbg(DEBUG_EVENT
, "param %d: %s", j
, argv
[j
]);
564 spin_lock_irqsave(&cs
->ev_lock
, flags
);
570 while (curarg
< params
) {
571 next
= (tail
+ 1) % MAX_EVENTS
;
572 if (unlikely(next
== head
)) {
573 dev_err(cs
->dev
, "event queue full\n");
577 event
= cs
->events
+ tail
;
578 event
->at_state
= NULL
;
585 resp_code
= RSP_STRING
;
586 param_type
= RT_STRING
;
588 for (rt
= resp_type
; rt
->response
; ++rt
)
589 if (!strcmp(argv
[curarg
], rt
->response
))
593 event
->type
= RSP_UNKNOWN
;
595 "unknown modem response: %s\n",
600 resp_code
= rt
->resp_code
;
601 param_type
= rt
->type
;
605 event
->type
= resp_code
;
607 switch (param_type
) {
613 "received RING without CID!\n");
614 event
->type
= RSP_INVAL
;
618 event
->parameter
= cid
;
623 if (curarg
>= params
) {
624 event
->parameter
= ZSAU_NONE
;
627 for (zr
= zsau_resp
; zr
->str
; ++zr
)
628 if (!strcmp(argv
[curarg
], zr
->str
))
630 event
->parameter
= zr
->code
;
633 "%s: unknown parameter %s after ZSAU\n",
634 __func__
, argv
[curarg
]);
638 if (curarg
< params
) {
639 event
->ptr
= kstrdup(argv
[curarg
], GFP_ATOMIC
);
641 dev_err(cs
->dev
, "out of memory\n");
644 gig_dbg(DEBUG_EVENT
, "string==%s",
645 event
->ptr
? (char *) event
->ptr
: "NULL");
648 event
->parameter
= -1;
649 if (curarg
+ 1 < params
) {
650 i
= isdn_gethex(argv
[curarg
]);
651 j
= isdn_gethex(argv
[curarg
+ 1]);
652 if (i
>= 0 && i
< 256 && j
>= 0 && j
< 256)
653 event
->parameter
= (unsigned) i
<< 8
661 if (curarg
< params
) {
662 if (param_type
== RT_HEX
)
664 isdn_gethex(argv
[curarg
]);
667 isdn_getnum(argv
[curarg
]);
670 event
->parameter
= -1;
671 gig_dbg(DEBUG_EVENT
, "parameter==%d", event
->parameter
);
675 if (resp_code
== RSP_ZDLE
)
676 cs
->dle
= event
->parameter
;
683 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
685 if (curarg
!= params
)
687 "invalid number of processed parameters: %d/%d",
690 EXPORT_SYMBOL_GPL(gigaset_handle_modem_response
);
693 * process closing of connection associated with given AT state structure
695 static void disconnect(struct at_state_t
**at_state_p
)
698 struct bc_state
*bcs
= (*at_state_p
)->bcs
;
699 struct cardstate
*cs
= (*at_state_p
)->cs
;
701 spin_lock_irqsave(&cs
->lock
, flags
);
702 ++(*at_state_p
)->seq_index
;
704 /* revert to selected idle mode */
706 cs
->at_state
.pending_commands
|= PC_UMMODE
;
707 gig_dbg(DEBUG_EVENT
, "Scheduling PC_UMMODE");
708 cs
->commands_pending
= 1;
710 spin_unlock_irqrestore(&cs
->lock
, flags
);
713 /* B channel assigned: invoke hardware specific handler */
714 cs
->ops
->close_bchannel(bcs
);
716 if (bcs
->chstate
& (CHS_D_UP
| CHS_NOTIFY_LL
)) {
717 bcs
->chstate
&= ~(CHS_D_UP
| CHS_NOTIFY_LL
);
718 gigaset_isdn_hupD(bcs
);
721 /* no B channel assigned: just deallocate */
722 spin_lock_irqsave(&cs
->lock
, flags
);
723 list_del(&(*at_state_p
)->list
);
726 spin_unlock_irqrestore(&cs
->lock
, flags
);
731 * get a free AT state structure: either one of those associated with the
732 * B channels of the Gigaset device, or if none of those is available,
733 * a newly allocated one with bcs=NULL
734 * The structure should be freed by calling disconnect() after use.
736 static inline struct at_state_t
*get_free_channel(struct cardstate
*cs
,
738 /* cids: >0: siemens-cid
740 -1: no cid assigned yet
745 struct at_state_t
*ret
;
747 for (i
= 0; i
< cs
->channels
; ++i
)
748 if (gigaset_get_channel(cs
->bcs
+ i
)) {
749 ret
= &cs
->bcs
[i
].at_state
;
754 spin_lock_irqsave(&cs
->lock
, flags
);
755 ret
= kmalloc(sizeof(struct at_state_t
), GFP_ATOMIC
);
757 gigaset_at_init(ret
, NULL
, cs
, cid
);
758 list_add(&ret
->list
, &cs
->temp_at_states
);
760 spin_unlock_irqrestore(&cs
->lock
, flags
);
764 static void init_failed(struct cardstate
*cs
, int mode
)
767 struct at_state_t
*at_state
;
769 cs
->at_state
.pending_commands
&= ~PC_INIT
;
771 cs
->mstate
= MS_UNINITIALIZED
;
772 gigaset_free_channels(cs
);
773 for (i
= 0; i
< cs
->channels
; ++i
) {
774 at_state
= &cs
->bcs
[i
].at_state
;
775 if (at_state
->pending_commands
& PC_CID
) {
776 at_state
->pending_commands
&= ~PC_CID
;
777 at_state
->pending_commands
|= PC_NOCID
;
778 cs
->commands_pending
= 1;
783 static void schedule_init(struct cardstate
*cs
, int state
)
785 if (cs
->at_state
.pending_commands
& PC_INIT
) {
786 gig_dbg(DEBUG_EVENT
, "not scheduling PC_INIT again");
790 cs
->mode
= M_UNKNOWN
;
791 gigaset_block_channels(cs
);
792 cs
->at_state
.pending_commands
|= PC_INIT
;
793 gig_dbg(DEBUG_EVENT
, "Scheduling PC_INIT");
794 cs
->commands_pending
= 1;
797 /* Add "AT" to a command, add the cid, dle encode it, send the result to the
799 static void send_command(struct cardstate
*cs
, const char *cmd
, int cid
,
800 int dle
, gfp_t kmallocflags
)
802 size_t cmdlen
, buflen
;
803 char *cmdpos
, *cmdbuf
, *cmdtail
;
805 cmdlen
= strlen(cmd
);
806 buflen
= 11 + cmdlen
;
807 if (unlikely(buflen
<= cmdlen
)) {
808 dev_err(cs
->dev
, "integer overflow in buflen\n");
812 cmdbuf
= kmalloc(buflen
, kmallocflags
);
813 if (unlikely(!cmdbuf
)) {
814 dev_err(cs
->dev
, "out of memory\n");
819 cmdtail
= cmdpos
+ cmdlen
;
820 memcpy(cmdpos
, cmd
, cmdlen
);
822 if (cid
> 0 && cid
<= 65535) {
824 *--cmdpos
= '0' + cid
% 10;
842 cs
->ops
->write_cmd(cs
, cmdpos
, cmdlen
, NULL
);
846 static struct at_state_t
*at_state_from_cid(struct cardstate
*cs
, int cid
)
848 struct at_state_t
*at_state
;
853 return &cs
->at_state
;
855 for (i
= 0; i
< cs
->channels
; ++i
)
856 if (cid
== cs
->bcs
[i
].at_state
.cid
)
857 return &cs
->bcs
[i
].at_state
;
859 spin_lock_irqsave(&cs
->lock
, flags
);
861 list_for_each_entry(at_state
, &cs
->temp_at_states
, list
)
862 if (cid
== at_state
->cid
) {
863 spin_unlock_irqrestore(&cs
->lock
, flags
);
867 spin_unlock_irqrestore(&cs
->lock
, flags
);
872 static void bchannel_down(struct bc_state
*bcs
)
874 if (bcs
->chstate
& CHS_B_UP
) {
875 bcs
->chstate
&= ~CHS_B_UP
;
876 gigaset_isdn_hupB(bcs
);
879 if (bcs
->chstate
& (CHS_D_UP
| CHS_NOTIFY_LL
)) {
880 bcs
->chstate
&= ~(CHS_D_UP
| CHS_NOTIFY_LL
);
881 gigaset_isdn_hupD(bcs
);
884 gigaset_free_channel(bcs
);
886 gigaset_bcs_reinit(bcs
);
889 static void bchannel_up(struct bc_state
*bcs
)
891 if (bcs
->chstate
& CHS_B_UP
) {
892 dev_notice(bcs
->cs
->dev
, "%s: B channel already up\n",
897 bcs
->chstate
|= CHS_B_UP
;
898 gigaset_isdn_connB(bcs
);
901 static void start_dial(struct at_state_t
*at_state
, void *data
,
904 struct bc_state
*bcs
= at_state
->bcs
;
905 struct cardstate
*cs
= at_state
->cs
;
906 char **commands
= data
;
910 bcs
->chstate
|= CHS_NOTIFY_LL
;
912 spin_lock_irqsave(&cs
->lock
, flags
);
913 if (at_state
->seq_index
!= seq_index
) {
914 spin_unlock_irqrestore(&cs
->lock
, flags
);
917 spin_unlock_irqrestore(&cs
->lock
, flags
);
919 for (i
= 0; i
< AT_NUM
; ++i
) {
920 kfree(bcs
->commands
[i
]);
921 bcs
->commands
[i
] = commands
[i
];
924 at_state
->pending_commands
|= PC_CID
;
925 gig_dbg(DEBUG_EVENT
, "Scheduling PC_CID");
926 cs
->commands_pending
= 1;
930 for (i
= 0; i
< AT_NUM
; ++i
) {
934 at_state
->pending_commands
|= PC_NOCID
;
935 gig_dbg(DEBUG_EVENT
, "Scheduling PC_NOCID");
936 cs
->commands_pending
= 1;
940 static void start_accept(struct at_state_t
*at_state
)
942 struct cardstate
*cs
= at_state
->cs
;
943 struct bc_state
*bcs
= at_state
->bcs
;
946 for (i
= 0; i
< AT_NUM
; ++i
) {
947 kfree(bcs
->commands
[i
]);
948 bcs
->commands
[i
] = NULL
;
951 bcs
->commands
[AT_PROTO
] = kmalloc(9, GFP_ATOMIC
);
952 bcs
->commands
[AT_ISO
] = kmalloc(9, GFP_ATOMIC
);
953 if (!bcs
->commands
[AT_PROTO
] || !bcs
->commands
[AT_ISO
]) {
954 dev_err(at_state
->cs
->dev
, "out of memory\n");
956 at_state
->pending_commands
|= PC_HUP
;
957 gig_dbg(DEBUG_EVENT
, "Scheduling PC_HUP");
958 cs
->commands_pending
= 1;
962 snprintf(bcs
->commands
[AT_PROTO
], 9, "^SBPR=%u\r", bcs
->proto2
);
963 snprintf(bcs
->commands
[AT_ISO
], 9, "^SISO=%u\r", bcs
->channel
+ 1);
965 at_state
->pending_commands
|= PC_ACCEPT
;
966 gig_dbg(DEBUG_EVENT
, "Scheduling PC_ACCEPT");
967 cs
->commands_pending
= 1;
970 static void do_start(struct cardstate
*cs
)
972 gigaset_free_channels(cs
);
974 if (cs
->mstate
!= MS_LOCKED
)
975 schedule_init(cs
, MS_INIT
);
978 gigaset_isdn_start(cs
);
981 wake_up(&cs
->waitqueue
);
984 static void finish_shutdown(struct cardstate
*cs
)
986 if (cs
->mstate
!= MS_LOCKED
) {
987 cs
->mstate
= MS_UNINITIALIZED
;
988 cs
->mode
= M_UNKNOWN
;
991 /* Tell the LL that the device is not available .. */
994 gigaset_isdn_stop(cs
);
997 /* The rest is done by cleanup_cs () in user mode. */
999 cs
->cmd_result
= -ENODEV
;
1001 wake_up(&cs
->waitqueue
);
1004 static void do_shutdown(struct cardstate
*cs
)
1006 gigaset_block_channels(cs
);
1008 if (cs
->mstate
== MS_READY
) {
1009 cs
->mstate
= MS_SHUTDOWN
;
1010 cs
->at_state
.pending_commands
|= PC_SHUTDOWN
;
1011 gig_dbg(DEBUG_EVENT
, "Scheduling PC_SHUTDOWN");
1012 cs
->commands_pending
= 1;
1014 finish_shutdown(cs
);
1017 static void do_stop(struct cardstate
*cs
)
1019 unsigned long flags
;
1021 spin_lock_irqsave(&cs
->lock
, flags
);
1023 spin_unlock_irqrestore(&cs
->lock
, flags
);
1028 /* Entering cid mode or getting a cid failed:
1029 * try to initialize the device and try again.
1031 * channel >= 0: getting cid for the channel failed
1032 * channel < 0: entering cid mode failed
1034 * returns 0 on failure
1036 static int reinit_and_retry(struct cardstate
*cs
, int channel
)
1040 if (--cs
->retry_count
<= 0)
1043 for (i
= 0; i
< cs
->channels
; ++i
)
1044 if (cs
->bcs
[i
].at_state
.cid
> 0)
1049 "Could not enter cid mode. Reinit device and try again.\n");
1052 "Could not get a call id. Reinit device and try again.\n");
1053 cs
->bcs
[channel
].at_state
.pending_commands
|= PC_CID
;
1055 schedule_init(cs
, MS_INIT
);
1059 static int at_state_invalid(struct cardstate
*cs
,
1060 struct at_state_t
*test_ptr
)
1062 unsigned long flags
;
1064 struct at_state_t
*at_state
;
1067 spin_lock_irqsave(&cs
->lock
, flags
);
1069 if (test_ptr
== &cs
->at_state
)
1072 list_for_each_entry(at_state
, &cs
->temp_at_states
, list
)
1073 if (at_state
== test_ptr
)
1076 for (channel
= 0; channel
< cs
->channels
; ++channel
)
1077 if (&cs
->bcs
[channel
].at_state
== test_ptr
)
1082 spin_unlock_irqrestore(&cs
->lock
, flags
);
1086 static void handle_icall(struct cardstate
*cs
, struct bc_state
*bcs
,
1087 struct at_state_t
**p_at_state
)
1090 struct at_state_t
*at_state
= *p_at_state
;
1092 retval
= gigaset_isdn_icall(at_state
);
1097 dev_err(cs
->dev
, "internal error: disposition=%d\n", retval
);
1098 /* --v-- fall through --v-- */
1102 * Device doc says that would reject the call.
1103 * In fact it doesn't.
1105 at_state
->pending_commands
|= PC_HUP
;
1106 cs
->commands_pending
= 1;
1111 static int do_lock(struct cardstate
*cs
)
1116 switch (cs
->mstate
) {
1117 case MS_UNINITIALIZED
:
1119 if (cs
->cur_at_seq
|| !list_empty(&cs
->temp_at_states
) ||
1120 cs
->at_state
.pending_commands
)
1123 for (i
= 0; i
< cs
->channels
; ++i
)
1124 if (cs
->bcs
[i
].at_state
.pending_commands
)
1127 if (!gigaset_get_channels(cs
))
1138 cs
->mstate
= MS_LOCKED
;
1139 cs
->mode
= M_UNKNOWN
;
1144 static int do_unlock(struct cardstate
*cs
)
1146 if (cs
->mstate
!= MS_LOCKED
)
1149 cs
->mstate
= MS_UNINITIALIZED
;
1150 cs
->mode
= M_UNKNOWN
;
1151 gigaset_free_channels(cs
);
1153 schedule_init(cs
, MS_INIT
);
1158 static void do_action(int action
, struct cardstate
*cs
,
1159 struct bc_state
*bcs
,
1160 struct at_state_t
**p_at_state
, char **pp_command
,
1161 int *p_genresp
, int *p_resp_code
,
1164 struct at_state_t
*at_state
= *p_at_state
;
1165 struct at_state_t
*at_state2
;
1166 unsigned long flags
;
1170 unsigned char *s
, *e
;
1178 at_state
->waiting
= 1;
1181 cs
->at_state
.pending_commands
&= ~PC_INIT
;
1182 cs
->cur_at_seq
= SEQ_NONE
;
1183 cs
->mode
= M_UNIMODEM
;
1184 spin_lock_irqsave(&cs
->lock
, flags
);
1186 spin_unlock_irqrestore(&cs
->lock
, flags
);
1187 gigaset_free_channels(cs
);
1188 cs
->mstate
= MS_READY
;
1191 spin_unlock_irqrestore(&cs
->lock
, flags
);
1192 cs
->at_state
.pending_commands
|= PC_CIDMODE
;
1193 gig_dbg(DEBUG_EVENT
, "Scheduling PC_CIDMODE");
1194 cs
->commands_pending
= 1;
1197 dev_warn(cs
->dev
, "Could not initialize the device.\n");
1199 init_failed(cs
, M_UNKNOWN
);
1200 cs
->cur_at_seq
= SEQ_NONE
;
1202 case ACT_CONFIGMODE
:
1203 init_failed(cs
, M_CONFIG
);
1204 cs
->cur_at_seq
= SEQ_NONE
;
1208 /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
1209 cs
->inbuf
[0].inputstate
&=
1210 ~(INS_command
| INS_DLE_command
);
1214 cs
->inbuf
[0].inputstate
=
1215 (cs
->inbuf
[0].inputstate
& ~INS_DLE_command
)
1219 if (cs
->mstate
== MS_INIT
|| cs
->mstate
== MS_RECOVER
) {
1220 gigaset_free_channels(cs
);
1221 cs
->mstate
= MS_READY
;
1224 cs
->cur_at_seq
= SEQ_NONE
;
1227 cs
->mode
= M_UNIMODEM
;
1228 cs
->cur_at_seq
= SEQ_NONE
;
1231 cs
->cur_at_seq
= SEQ_NONE
;
1232 if (cs
->mstate
== MS_INIT
|| cs
->mstate
== MS_RECOVER
) {
1233 init_failed(cs
, M_UNKNOWN
);
1236 if (!reinit_and_retry(cs
, -1))
1237 schedule_init(cs
, MS_RECOVER
);
1240 cs
->cur_at_seq
= SEQ_NONE
;
1241 schedule_init(cs
, MS_RECOVER
);
1244 /* send "+++" (hangup in unimodem mode) */
1246 cs
->ops
->write_cmd(cs
, "+++", 3, NULL
);
1249 /* get fresh AT state structure for new CID */
1250 at_state2
= get_free_channel(cs
, ev
->parameter
);
1253 "RING ignored: could not allocate channel structure\n");
1257 /* initialize AT state structure
1258 * note that bcs may be NULL if no B channel is free
1260 at_state2
->ConState
= 700;
1261 kfree(at_state2
->str_var
[STR_NMBR
]);
1262 at_state2
->str_var
[STR_NMBR
] = NULL
;
1263 kfree(at_state2
->str_var
[STR_ZCPN
]);
1264 at_state2
->str_var
[STR_ZCPN
] = NULL
;
1265 kfree(at_state2
->str_var
[STR_ZBC
]);
1266 at_state2
->str_var
[STR_ZBC
] = NULL
;
1267 kfree(at_state2
->str_var
[STR_ZHLC
]);
1268 at_state2
->str_var
[STR_ZHLC
] = NULL
;
1269 at_state2
->int_var
[VAR_ZCTP
] = -1;
1271 spin_lock_irqsave(&cs
->lock
, flags
);
1272 at_state2
->timer_expires
= RING_TIMEOUT
;
1273 at_state2
->timer_active
= 1;
1274 spin_unlock_irqrestore(&cs
->lock
, flags
);
1277 handle_icall(cs
, bcs
, p_at_state
);
1280 dev_warn(cs
->dev
, "Could not shut down the device.\n");
1284 cs
->cur_at_seq
= SEQ_NONE
;
1285 finish_shutdown(cs
);
1288 if (cs
->onechannel
) {
1289 at_state
->pending_commands
|= PC_DLE1
;
1290 cs
->commands_pending
= 1;
1293 bcs
->chstate
|= CHS_D_UP
;
1294 gigaset_isdn_connD(bcs
);
1295 cs
->ops
->init_bchannel(bcs
);
1298 cs
->cur_at_seq
= SEQ_NONE
;
1299 bcs
= cs
->bcs
+ cs
->curchannel
;
1301 bcs
->chstate
|= CHS_D_UP
;
1302 gigaset_isdn_connD(bcs
);
1303 cs
->ops
->init_bchannel(bcs
);
1306 at_state
->int_var
[VAR_ZSAU
] = ZSAU_NULL
;
1308 case ACT_DISCONNECT
:
1309 cs
->cur_at_seq
= SEQ_NONE
;
1311 if (bcs
&& cs
->onechannel
&& cs
->dle
) {
1312 /* Check for other open channels not needed:
1313 * DLE only used for M10x with one B channel.
1315 at_state
->pending_commands
|= PC_DLE0
;
1316 cs
->commands_pending
= 1;
1318 disconnect(p_at_state
);
1321 at_state
->int_var
[VAR_ZDLE
] = 0;
1325 cs
->cur_at_seq
= SEQ_NONE
;
1326 at_state2
= &cs
->bcs
[cs
->curchannel
].at_state
;
1327 disconnect(&at_state2
);
1330 cs
->cur_at_seq
= SEQ_NONE
;
1331 dev_warn(cs
->dev
, "Could not hang up.\n");
1333 if (bcs
&& cs
->onechannel
)
1334 at_state
->pending_commands
|= PC_DLE0
;
1336 disconnect(p_at_state
);
1337 schedule_init(cs
, MS_RECOVER
);
1340 cs
->cur_at_seq
= SEQ_NONE
;
1341 dev_warn(cs
->dev
, "Could not leave DLE mode.\n");
1342 at_state2
= &cs
->bcs
[cs
->curchannel
].at_state
;
1343 disconnect(&at_state2
);
1344 schedule_init(cs
, MS_RECOVER
);
1347 cs
->cur_at_seq
= SEQ_NONE
;
1349 "Could not enter DLE mode. Trying to hang up.\n");
1350 channel
= cs
->curchannel
;
1351 cs
->bcs
[channel
].at_state
.pending_commands
|= PC_HUP
;
1352 cs
->commands_pending
= 1;
1355 case ACT_CID
: /* got cid; start dialing */
1356 cs
->cur_at_seq
= SEQ_NONE
;
1357 channel
= cs
->curchannel
;
1358 if (ev
->parameter
> 0 && ev
->parameter
<= 65535) {
1359 cs
->bcs
[channel
].at_state
.cid
= ev
->parameter
;
1360 cs
->bcs
[channel
].at_state
.pending_commands
|=
1362 cs
->commands_pending
= 1;
1367 cs
->cur_at_seq
= SEQ_NONE
;
1368 channel
= cs
->curchannel
;
1369 if (!reinit_and_retry(cs
, channel
)) {
1371 "Could not get a call ID. Cannot dial.\n");
1372 at_state2
= &cs
->bcs
[channel
].at_state
;
1373 disconnect(&at_state2
);
1377 cs
->cur_at_seq
= SEQ_NONE
;
1378 at_state2
= &cs
->bcs
[cs
->curchannel
].at_state
;
1379 disconnect(&at_state2
);
1384 cs
->cur_at_seq
= SEQ_NONE
;
1387 case ACT_ABORTACCEPT
: /* hangup/error/timeout during ICALL procssng */
1388 disconnect(p_at_state
);
1391 case ACT_ABORTDIAL
: /* error/timeout during dial preparation */
1392 cs
->cur_at_seq
= SEQ_NONE
;
1393 at_state
->pending_commands
|= PC_HUP
;
1394 cs
->commands_pending
= 1;
1397 case ACT_REMOTEREJECT
: /* DISCONNECT_IND after dialling */
1398 case ACT_CONNTIMEOUT
: /* timeout waiting for ZSAU=ACTIVE */
1399 case ACT_REMOTEHUP
: /* DISCONNECT_IND with established connection */
1400 at_state
->pending_commands
|= PC_HUP
;
1401 cs
->commands_pending
= 1;
1403 case ACT_GETSTRING
: /* warning: RING, ZDLE, ...
1404 are not handled properly anymore */
1405 at_state
->getstring
= 1;
1410 *p_resp_code
= RSP_ERROR
;
1415 if (!strcmp(s
, "OK")) {
1417 *p_resp_code
= RSP_ERROR
;
1421 for (i
= 0; i
< 4; ++i
) {
1422 val
= simple_strtoul(s
, (char **) &e
, 10);
1423 if (val
> INT_MAX
|| e
== s
)
1428 } else if (*e
!= '.')
1436 *p_resp_code
= RSP_ERROR
;
1439 /*at_state->getstring = 1;*/
1443 if (cs
->gotfwver
== 0) {
1445 gig_dbg(DEBUG_EVENT
,
1446 "firmware version %02d.%03d.%02d.%02d",
1447 cs
->fwver
[0], cs
->fwver
[1],
1448 cs
->fwver
[2], cs
->fwver
[3]);
1454 dev_err(cs
->dev
, "could not read firmware version.\n");
1457 gig_dbg(DEBUG_ANY
, "%s: ERROR response in ConState %d",
1458 __func__
, at_state
->ConState
);
1459 cs
->cur_at_seq
= SEQ_NONE
;
1462 gig_dbg(DEBUG_ANY
, "%s: resp_code %d in ConState %d",
1463 __func__
, ev
->type
, at_state
->ConState
);
1466 dev_warn(cs
->dev
, "%s: resp_code %d in ConState %d!\n",
1467 __func__
, ev
->type
, at_state
->ConState
);
1470 dev_warn(cs
->dev
, "cause code %04x in connection state %d.\n",
1471 ev
->parameter
, at_state
->ConState
);
1474 /* events from the LL */
1476 start_dial(at_state
, ev
->ptr
, ev
->parameter
);
1479 start_accept(at_state
);
1482 at_state
->pending_commands
|= PC_HUP
;
1483 gig_dbg(DEBUG_EVENT
, "Scheduling PC_HUP");
1484 cs
->commands_pending
= 1;
1487 /* hotplug events */
1495 /* events from the interface */
1497 cs
->cmd_result
= ev
->parameter
? do_lock(cs
) : do_unlock(cs
);
1499 wake_up(&cs
->waitqueue
);
1502 if (ev
->parameter
!= 0)
1503 cs
->cmd_result
= -EINVAL
;
1504 else if (cs
->gotfwver
!= 1) {
1505 cs
->cmd_result
= -ENOENT
;
1507 memcpy(ev
->arg
, cs
->fwver
, sizeof cs
->fwver
);
1511 wake_up(&cs
->waitqueue
);
1514 /* events from the proc file system */
1515 case ACT_PROC_CIDMODE
:
1516 spin_lock_irqsave(&cs
->lock
, flags
);
1517 if (ev
->parameter
!= cs
->cidmode
) {
1518 cs
->cidmode
= ev
->parameter
;
1519 if (ev
->parameter
) {
1520 cs
->at_state
.pending_commands
|= PC_CIDMODE
;
1521 gig_dbg(DEBUG_EVENT
, "Scheduling PC_CIDMODE");
1523 cs
->at_state
.pending_commands
|= PC_UMMODE
;
1524 gig_dbg(DEBUG_EVENT
, "Scheduling PC_UMMODE");
1526 cs
->commands_pending
= 1;
1528 spin_unlock_irqrestore(&cs
->lock
, flags
);
1530 wake_up(&cs
->waitqueue
);
1533 /* events from the hardware drivers */
1534 case ACT_NOTIFY_BC_DOWN
:
1537 case ACT_NOTIFY_BC_UP
:
1546 if (action
>= ACT_CMD
&& action
< ACT_CMD
+ AT_NUM
) {
1547 *pp_command
= at_state
->bcs
->commands
[action
- ACT_CMD
];
1550 *p_resp_code
= RSP_NULL
;
1553 dev_err(cs
->dev
, "%s: action==%d!\n", __func__
, action
);
1557 /* State machine to do the calling and hangup procedure */
1558 static void process_event(struct cardstate
*cs
, struct event_t
*ev
)
1560 struct bc_state
*bcs
;
1561 char *p_command
= NULL
;
1562 struct reply_t
*rep
;
1565 int resp_code
= RSP_ERROR
;
1567 struct at_state_t
*at_state
;
1570 unsigned long flags
;
1573 at_state
= at_state_from_cid(cs
, ev
->cid
);
1575 gig_dbg(DEBUG_EVENT
, "event %d for invalid cid %d",
1577 gigaset_add_event(cs
, &cs
->at_state
, RSP_WRONG_CID
,
1582 at_state
= ev
->at_state
;
1583 if (at_state_invalid(cs
, at_state
)) {
1584 gig_dbg(DEBUG_EVENT
, "event for invalid at_state %p",
1590 gig_dbg(DEBUG_EVENT
, "connection state %d, event %d",
1591 at_state
->ConState
, ev
->type
);
1593 bcs
= at_state
->bcs
;
1594 sendcid
= at_state
->cid
;
1596 /* Setting the pointer to the dial array */
1597 rep
= at_state
->replystruct
;
1599 spin_lock_irqsave(&cs
->lock
, flags
);
1600 if (ev
->type
== EV_TIMEOUT
) {
1601 if (ev
->parameter
!= at_state
->timer_index
1602 || !at_state
->timer_active
) {
1603 ev
->type
= RSP_NONE
; /* old timeout */
1604 gig_dbg(DEBUG_EVENT
, "old timeout");
1605 } else if (!at_state
->waiting
)
1606 gig_dbg(DEBUG_EVENT
, "timeout occurred");
1608 gig_dbg(DEBUG_EVENT
, "stopped waiting");
1610 spin_unlock_irqrestore(&cs
->lock
, flags
);
1612 /* if the response belongs to a variable in at_state->int_var[VAR_XXXX]
1613 or at_state->str_var[STR_XXXX], set it */
1614 if (ev
->type
>= RSP_VAR
&& ev
->type
< RSP_VAR
+ VAR_NUM
) {
1615 index
= ev
->type
- RSP_VAR
;
1616 at_state
->int_var
[index
] = ev
->parameter
;
1617 } else if (ev
->type
>= RSP_STR
&& ev
->type
< RSP_STR
+ STR_NUM
) {
1618 index
= ev
->type
- RSP_STR
;
1619 kfree(at_state
->str_var
[index
]);
1620 at_state
->str_var
[index
] = ev
->ptr
;
1621 ev
->ptr
= NULL
; /* prevent process_events() from
1625 if (ev
->type
== EV_TIMEOUT
|| ev
->type
== RSP_STRING
)
1626 at_state
->getstring
= 0;
1628 /* Search row in dial array which matches modem response and current
1631 rcode
= rep
->resp_code
;
1632 if (rcode
== RSP_LAST
) {
1633 /* found nothing...*/
1634 dev_warn(cs
->dev
, "%s: rcode=RSP_LAST: "
1635 "resp_code %d in ConState %d!\n",
1636 __func__
, ev
->type
, at_state
->ConState
);
1639 if ((rcode
== RSP_ANY
|| rcode
== ev
->type
)
1640 && ((int) at_state
->ConState
>= rep
->min_ConState
)
1641 && (rep
->max_ConState
< 0
1642 || (int) at_state
->ConState
<= rep
->max_ConState
)
1643 && (rep
->parameter
< 0 || rep
->parameter
== ev
->parameter
))
1647 p_command
= rep
->command
;
1649 at_state
->waiting
= 0;
1650 for (curact
= 0; curact
< MAXACT
; ++curact
) {
1651 /* The row tells us what we should do ..
1653 do_action(rep
->action
[curact
], cs
, bcs
, &at_state
, &p_command
,
1654 &genresp
, &resp_code
, ev
);
1656 break; /* may be freed after disconnect */
1660 /* Jump to the next con-state regarding the array */
1661 if (rep
->new_ConState
>= 0)
1662 at_state
->ConState
= rep
->new_ConState
;
1665 spin_lock_irqsave(&cs
->lock
, flags
);
1666 at_state
->timer_expires
= 0;
1667 at_state
->timer_active
= 0;
1668 spin_unlock_irqrestore(&cs
->lock
, flags
);
1669 gigaset_add_event(cs
, at_state
, resp_code
,
1672 /* Send command to modem if not NULL... */
1675 send_command(cs
, p_command
,
1679 gigaset_add_event(cs
, at_state
,
1684 spin_lock_irqsave(&cs
->lock
, flags
);
1685 if (!rep
->timeout
) {
1686 at_state
->timer_expires
= 0;
1687 at_state
->timer_active
= 0;
1688 } else if (rep
->timeout
> 0) { /* new timeout */
1689 at_state
->timer_expires
= rep
->timeout
* 10;
1690 at_state
->timer_active
= 1;
1691 ++at_state
->timer_index
;
1693 spin_unlock_irqrestore(&cs
->lock
, flags
);
1698 static void schedule_sequence(struct cardstate
*cs
,
1699 struct at_state_t
*at_state
, int sequence
)
1701 cs
->cur_at_seq
= sequence
;
1702 gigaset_add_event(cs
, at_state
, RSP_INIT
, NULL
, sequence
, NULL
);
1705 static void process_command_flags(struct cardstate
*cs
)
1707 struct at_state_t
*at_state
= NULL
;
1708 struct bc_state
*bcs
;
1711 unsigned long flags
;
1713 cs
->commands_pending
= 0;
1715 if (cs
->cur_at_seq
) {
1716 gig_dbg(DEBUG_EVENT
, "not searching scheduled commands: busy");
1720 gig_dbg(DEBUG_EVENT
, "searching scheduled commands");
1722 sequence
= SEQ_NONE
;
1724 /* clear pending_commands and hangup channels on shutdown */
1725 if (cs
->at_state
.pending_commands
& PC_SHUTDOWN
) {
1726 cs
->at_state
.pending_commands
&= ~PC_CIDMODE
;
1727 for (i
= 0; i
< cs
->channels
; ++i
) {
1729 at_state
= &bcs
->at_state
;
1730 at_state
->pending_commands
&=
1731 ~(PC_DLE1
| PC_ACCEPT
| PC_DIAL
);
1732 if (at_state
->cid
> 0)
1733 at_state
->pending_commands
|= PC_HUP
;
1734 if (at_state
->pending_commands
& PC_CID
) {
1735 at_state
->pending_commands
|= PC_NOCID
;
1736 at_state
->pending_commands
&= ~PC_CID
;
1741 /* clear pending_commands and hangup channels on reset */
1742 if (cs
->at_state
.pending_commands
& PC_INIT
) {
1743 cs
->at_state
.pending_commands
&= ~PC_CIDMODE
;
1744 for (i
= 0; i
< cs
->channels
; ++i
) {
1746 at_state
= &bcs
->at_state
;
1747 at_state
->pending_commands
&=
1748 ~(PC_DLE1
| PC_ACCEPT
| PC_DIAL
);
1749 if (at_state
->cid
> 0)
1750 at_state
->pending_commands
|= PC_HUP
;
1751 if (cs
->mstate
== MS_RECOVER
) {
1752 if (at_state
->pending_commands
& PC_CID
) {
1753 at_state
->pending_commands
|= PC_NOCID
;
1754 at_state
->pending_commands
&= ~PC_CID
;
1760 /* only switch back to unimodem mode if no commands are pending and
1761 * no channels are up */
1762 spin_lock_irqsave(&cs
->lock
, flags
);
1763 if (cs
->at_state
.pending_commands
== PC_UMMODE
1765 && list_empty(&cs
->temp_at_states
)
1766 && cs
->mode
== M_CID
) {
1767 sequence
= SEQ_UMMODE
;
1768 at_state
= &cs
->at_state
;
1769 for (i
= 0; i
< cs
->channels
; ++i
) {
1771 if (bcs
->at_state
.pending_commands
||
1772 bcs
->at_state
.cid
> 0) {
1773 sequence
= SEQ_NONE
;
1778 spin_unlock_irqrestore(&cs
->lock
, flags
);
1779 cs
->at_state
.pending_commands
&= ~PC_UMMODE
;
1780 if (sequence
!= SEQ_NONE
) {
1781 schedule_sequence(cs
, at_state
, sequence
);
1785 for (i
= 0; i
< cs
->channels
; ++i
) {
1787 if (bcs
->at_state
.pending_commands
& PC_HUP
) {
1788 bcs
->at_state
.pending_commands
&= ~PC_HUP
;
1789 if (bcs
->at_state
.pending_commands
& PC_CID
) {
1790 /* not yet dialing: PC_NOCID is sufficient */
1791 bcs
->at_state
.pending_commands
|= PC_NOCID
;
1792 bcs
->at_state
.pending_commands
&= ~PC_CID
;
1794 schedule_sequence(cs
, &bcs
->at_state
, SEQ_HUP
);
1798 if (bcs
->at_state
.pending_commands
& PC_NOCID
) {
1799 bcs
->at_state
.pending_commands
&= ~PC_NOCID
;
1800 cs
->curchannel
= bcs
->channel
;
1801 schedule_sequence(cs
, &cs
->at_state
, SEQ_NOCID
);
1803 } else if (bcs
->at_state
.pending_commands
& PC_DLE0
) {
1804 bcs
->at_state
.pending_commands
&= ~PC_DLE0
;
1805 cs
->curchannel
= bcs
->channel
;
1806 schedule_sequence(cs
, &cs
->at_state
, SEQ_DLE0
);
1811 list_for_each_entry(at_state
, &cs
->temp_at_states
, list
)
1812 if (at_state
->pending_commands
& PC_HUP
) {
1813 at_state
->pending_commands
&= ~PC_HUP
;
1814 schedule_sequence(cs
, at_state
, SEQ_HUP
);
1818 if (cs
->at_state
.pending_commands
& PC_INIT
) {
1819 cs
->at_state
.pending_commands
&= ~PC_INIT
;
1821 cs
->inbuf
->inputstate
= INS_command
;
1822 schedule_sequence(cs
, &cs
->at_state
, SEQ_INIT
);
1825 if (cs
->at_state
.pending_commands
& PC_SHUTDOWN
) {
1826 cs
->at_state
.pending_commands
&= ~PC_SHUTDOWN
;
1827 schedule_sequence(cs
, &cs
->at_state
, SEQ_SHUTDOWN
);
1830 if (cs
->at_state
.pending_commands
& PC_CIDMODE
) {
1831 cs
->at_state
.pending_commands
&= ~PC_CIDMODE
;
1832 if (cs
->mode
== M_UNIMODEM
) {
1833 cs
->retry_count
= 1;
1834 schedule_sequence(cs
, &cs
->at_state
, SEQ_CIDMODE
);
1839 for (i
= 0; i
< cs
->channels
; ++i
) {
1841 if (bcs
->at_state
.pending_commands
& PC_DLE1
) {
1842 bcs
->at_state
.pending_commands
&= ~PC_DLE1
;
1843 cs
->curchannel
= bcs
->channel
;
1844 schedule_sequence(cs
, &cs
->at_state
, SEQ_DLE1
);
1847 if (bcs
->at_state
.pending_commands
& PC_ACCEPT
) {
1848 bcs
->at_state
.pending_commands
&= ~PC_ACCEPT
;
1849 schedule_sequence(cs
, &bcs
->at_state
, SEQ_ACCEPT
);
1852 if (bcs
->at_state
.pending_commands
& PC_DIAL
) {
1853 bcs
->at_state
.pending_commands
&= ~PC_DIAL
;
1854 schedule_sequence(cs
, &bcs
->at_state
, SEQ_DIAL
);
1857 if (bcs
->at_state
.pending_commands
& PC_CID
) {
1860 cs
->at_state
.pending_commands
|= PC_CIDMODE
;
1861 gig_dbg(DEBUG_EVENT
, "Scheduling PC_CIDMODE");
1862 cs
->commands_pending
= 1;
1864 #ifdef GIG_MAYINITONDIAL
1866 schedule_init(cs
, MS_INIT
);
1870 bcs
->at_state
.pending_commands
&= ~PC_CID
;
1871 cs
->curchannel
= bcs
->channel
;
1873 cs
->retry_count
= 2;
1875 cs
->retry_count
= 1;
1877 schedule_sequence(cs
, &cs
->at_state
, SEQ_CID
);
1883 static void process_events(struct cardstate
*cs
)
1886 unsigned head
, tail
;
1888 int check_flags
= 0;
1890 unsigned long flags
;
1892 spin_lock_irqsave(&cs
->ev_lock
, flags
);
1895 for (i
= 0; i
< 2 * MAX_EVENTS
; ++i
) {
1898 if (!check_flags
&& !cs
->commands_pending
)
1901 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
1902 process_command_flags(cs
);
1903 spin_lock_irqsave(&cs
->ev_lock
, flags
);
1906 if (!cs
->commands_pending
)
1912 ev
= cs
->events
+ head
;
1913 was_busy
= cs
->cur_at_seq
!= SEQ_NONE
;
1914 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
1915 process_event(cs
, ev
);
1916 spin_lock_irqsave(&cs
->ev_lock
, flags
);
1919 if (was_busy
&& cs
->cur_at_seq
== SEQ_NONE
)
1922 head
= (head
+ 1) % MAX_EVENTS
;
1926 spin_unlock_irqrestore(&cs
->ev_lock
, flags
);
1928 if (i
== 2 * MAX_EVENTS
) {
1930 "infinite loop in process_events; aborting.\n");
1934 /* tasklet scheduled on any event received from the Gigaset device
1936 * data ISDN controller state structure
1938 void gigaset_handle_event(unsigned long data
)
1940 struct cardstate
*cs
= (struct cardstate
*) data
;
1942 /* handle incoming data on control/common channel */
1943 if (cs
->inbuf
->head
!= cs
->inbuf
->tail
) {
1944 gig_dbg(DEBUG_INTR
, "processing new data");
1945 cs
->ops
->handle_input(cs
->inbuf
);