16 #include <linux/export.h>
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
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
68 #define RSP_STRING -20
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
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
158 {
EV_TIMEOUT, 100, 100, -1, 101, 3, {0},
"Z\r"},
162 {
EV_TIMEOUT, 101, 101, -1, 102, 5, {0},
"Z\r"},
163 {
RSP_ERROR, 101, 101, -1, 102, 5, {0},
"Z\r"},
167 {
RSP_OK, 108, 108, -1, 104, -1},
168 {
RSP_ZDLE, 104, 104, 0, 103, 5, {0},
"Z\r"},
175 {
EV_TIMEOUT, 105, 105, -1, 103, 5, {0},
"Z\r"},
177 {
RSP_ERROR, 102, 102, -1, 107, 5, {0},
"^GETPRE\r"},
197 {
RSP_OK, 201, 201, -1, 202, -1},
205 {
RSP_OK, 251, 251, -1, 252, -1},
215 {
RSP_OK, 301, 301, -1, 302, -1},
274 {
RSP_OK, 607, 607, -1, 608, 5, {0},
"+VLS=17\r"},
275 {
RSP_OK, 608, 608, -1, 609, -1},
306 {
RSP_OK, 401, 401, -1, 402, 5},
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} },
329 {
RSP_OK, 722, 722, -1, 723, 5, {0},
"+VLS=17\r"},
330 {
RSP_OK, 723, 723, -1, 724, 5, {0} },
353 static const struct resp_type_t {
376 static const struct zsau_resp_t {
393 static int cid_of_response(
char *
s)
403 if (cid < 1 || cid > 65535)
421 const struct resp_type_t *rt;
422 const struct zsau_resp_t *zr;
450 for (i = 0; i < len; i++)
457 "too many parameters in response\n");
461 argv[params++] = cs->
respdata + i + 1;
465 cid = params > 1 ? cid_of_response(argv[params - 1]) : 0;
481 for (j = 1; j <
params; j++)
491 while (curarg < params) {
499 event->at_state =
NULL;
509 for (rt = resp_type; rt->response; ++rt)
510 if (!
strcmp(argv[curarg], rt->response))
516 "unknown modem response: '%s'\n",
521 resp_code = rt->resp_code;
522 param_type = rt->type;
526 event->type = resp_code;
528 switch (param_type) {
534 "received RING without CID!\n");
539 event->parameter =
cid;
544 if (curarg >= params) {
548 for (zr = zsau_resp; zr->str; ++zr)
549 if (!
strcmp(argv[curarg], zr->str))
551 event->parameter = zr->code;
554 "%s: unknown parameter %s after ZSAU\n",
555 __func__, argv[curarg]);
559 if (curarg < params) {
566 event->
ptr ? (
char *) event->
ptr :
"NULL");
569 event->parameter = -1;
570 if (curarg + 1 < params) {
573 i =
kstrtou8(argv[curarg++], 16, &type);
574 j =
kstrtou8(argv[curarg++], 16, &value);
575 if (i == 0 && j == 0)
576 event->parameter = (type << 8) | value;
581 if (curarg >= params ||
589 cs->
dle =
event->parameter;
596 spin_unlock_irqrestore(&cs->
ev_lock, flags);
598 if (curarg != params)
600 "invalid number of processed parameters: %d/%d",
611 struct bc_state *bcs = (*at_state_p)->bcs;
615 ++(*at_state_p)->seq_index;
623 spin_unlock_irqrestore(&cs->
lock, flags);
627 cs->
ops->close_bchannel(bcs);
639 spin_unlock_irqrestore(&cs->
lock, flags);
662 ret = &cs->
bcs[
i].at_state;
673 spin_unlock_irqrestore(&cs->
lock, flags);
687 at_state = &cs->
bcs[
i].at_state;
712 static void send_command(
struct cardstate *cs,
const char *
cmd,
int cid,
718 buflen =
strlen(cmd) + 12;
721 dev_err(cs->
dev,
"%s: out of memory\n", __func__);
724 if (cid > 0 && cid <= 65535)
726 dle ?
"\020(AT%d%s\020)" :
"AT%d%s",
730 dle ?
"\020(AT%s\020)" :
"AT%s",
735 cs->
ops->write_cmd(cs, cb);
748 if (cid == cs->
bcs[i].at_state.cid)
749 return &cs->
bcs[
i].at_state;
754 if (cid == at_state->cid) {
755 spin_unlock_irqrestore(&cs->
lock, flags);
759 spin_unlock_irqrestore(&cs->
lock, flags);
764 static void bchannel_down(
struct bc_state *bcs)
781 static void bchannel_up(
struct bc_state *bcs)
784 dev_notice(bcs->
cs->dev,
"%s: B channel already up\n",
806 spin_unlock_irqrestore(&cs->
lock, flags);
809 spin_unlock_irqrestore(&cs->
lock, flags);
832 static void start_accept(
struct at_state_t *at_state)
846 dev_err(at_state->
cs->dev,
"out of memory\n");
862 static void do_start(
struct cardstate *cs)
876 static void finish_shutdown(
struct cardstate *cs)
896 static void do_shutdown(
struct cardstate *cs)
909 static void do_stop(
struct cardstate *cs)
915 spin_unlock_irqrestore(&cs->
lock, flags);
936 if (cs->
bcs[i].at_state.cid > 0)
941 "Could not enter cid mode. Reinit device and try again.\n");
944 "Could not get a call id. Reinit device and try again.\n");
951 static int at_state_invalid(
struct cardstate *cs,
965 if (at_state == test_ptr)
968 for (channel = 0; channel < cs->
channels; ++channel)
969 if (&cs->bcs[channel].at_state == test_ptr)
974 spin_unlock_irqrestore(&cs->lock, flags);
989 dev_err(cs->dev,
"internal error: disposition=%d\n", retval);
998 cs->commands_pending = 1;
1003 static int do_lock(
struct cardstate *cs)
1016 if (cs->
bcs[i].at_state.pending_commands)
1036 static int do_unlock(
struct cardstate *cs)
1052 struct at_state_t **p_at_state,
char **pp_command,
1053 int *p_genresp,
int *p_resp_code,
1058 unsigned long flags;
1062 unsigned char *
s, *
e;
1078 spin_unlock_irqrestore(&cs->
lock, flags);
1083 spin_unlock_irqrestore(&cs->
lock, flags);
1089 dev_warn(cs->
dev,
"Could not initialize the device.\n");
1101 cs->
inbuf[0].inputstate &=
1106 cs->
inbuf[0].inputstate =
1128 if (reinit_and_retry(cs, -1) < 0)
1151 cs->
ops->write_cmd(cs, cb);
1156 at_state2 = get_free_channel(cs, ev->
parameter);
1159 "RING ignored: could not allocate channel structure\n");
1176 spin_unlock_irqrestore(&cs->
lock, flags);
1179 handle_icall(cs, bcs, p_at_state);
1182 dev_warn(cs->
dev,
"Could not shut down the device.\n");
1187 finish_shutdown(cs);
1197 cs->
ops->init_bchannel(bcs);
1205 cs->
ops->init_bchannel(bcs);
1251 "Could not enter DLE mode. Trying to hang up.\n");
1262 cs->
bcs[
channel].at_state.pending_commands |=
1271 if (reinit_and_retry(cs, channel) < 0) {
1273 "Could not get a call ID. Cannot dial.\n");
1324 for (i = 0; i < 4; ++
i) {
1331 }
else if (*e !=
'.')
1349 "firmware version %02d.%03d.%02d.%02d",
1357 dev_err(cs->
dev,
"could not read firmware version.\n");
1369 dev_warn(cs->
dev,
"%s: resp_code %d in ConState %d!\n",
1373 dev_warn(cs->
dev,
"cause code %04x in connection state %d.\n",
1383 start_accept(at_state);
1435 spin_unlock_irqrestore(&cs->
lock, flags);
1455 *pp_command = at_state->
bcs->commands[action -
ACT_CMD];
1461 dev_err(cs->
dev,
"%s: action==%d!\n", __func__, action);
1469 char *p_command =
NULL;
1478 unsigned long flags;
1481 at_state = at_state_from_cid(cs, ev->
cid);
1491 if (at_state_invalid(cs, at_state)) {
1501 bcs = at_state->
bcs;
1502 sendcid = at_state->
cid;
1513 }
else if (!at_state->
waiting)
1518 spin_unlock_irqrestore(&cs->
lock, flags);
1543 "resp_code %d in ConState %d!\n",
1558 for (curact = 0; curact <
MAXACT; ++curact) {
1561 do_action(rep->
action[curact], cs, bcs, &at_state, &p_command,
1562 &genresp, &resp_code, ev);
1576 spin_unlock_irqrestore(&cs->
lock, flags);
1583 send_command(cs, p_command,
1596 }
else if (rep->
timeout > 0) {
1601 spin_unlock_irqrestore(&cs->
lock, flags);
1606 static void schedule_sequence(
struct cardstate *cs,
1613 static void process_command_flags(
struct cardstate *cs)
1619 unsigned long flags;
1640 if (at_state->
cid > 0)
1657 if (at_state->
cid > 0)
1679 if (bcs->
at_state.pending_commands ||
1686 spin_unlock_irqrestore(&cs->
lock, flags);
1689 schedule_sequence(cs, at_state, sequence);
1720 if (at_state->pending_commands &
PC_HUP) {
1722 schedule_sequence(cs, at_state,
SEQ_HUP);
1785 static void process_events(
struct cardstate *cs)
1790 int check_flags = 0;
1792 unsigned long flags;
1803 spin_unlock_irqrestore(&cs->
ev_lock, flags);
1804 process_command_flags(cs);
1816 spin_unlock_irqrestore(&cs->
ev_lock, flags);
1817 process_event(cs, ev);
1824 head = (head + 1) % MAX_EVENTS;
1828 spin_unlock_irqrestore(&cs->
ev_lock, flags);
1830 if (i == 2 * MAX_EVENTS) {
1832 "infinite loop in process_events; aborting.\n");