Commit c9231f82 authored by David S. Miller's avatar David S. Miller

Merge branch 'gigaset_modem_response'

Tilman Schmidt says:

====================
isdn/gigaset: restructure modem response parser

This series of patches restructures the Gigaset ISDN driver's
modem response parser to improve code readability and conform
better to the device's specification and actual behaviour.
Could you please merge these through net-next?
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 558d51fa 4656e0a3
...@@ -389,22 +389,49 @@ zsau_resp[] = ...@@ -389,22 +389,49 @@ zsau_resp[] =
{NULL, ZSAU_UNKNOWN} {NULL, ZSAU_UNKNOWN}
}; };
/* retrieve CID from parsed response /* check for and remove fixed string prefix
* returns 0 if no CID, -1 if invalid CID, or CID value 1..65535 * If s starts with prefix terminated by a non-alphanumeric character,
* return pointer to the first character after that, otherwise return NULL.
*/ */
static int cid_of_response(char *s) static char *skip_prefix(char *s, const char *prefix)
{ {
int cid; while (*prefix)
int rc; if (*s++ != *prefix++)
return NULL;
if (s[-1] != ';') if (isalnum(*s))
return 0; /* no CID separator */ return NULL;
rc = kstrtoint(s, 10, &cid); return s;
if (rc) }
return 0; /* CID not numeric */
if (cid < 1 || cid > 65535) /* queue event with CID */
return -1; /* CID out of range */ static void add_cid_event(struct cardstate *cs, int cid, int type,
return cid; void *ptr, int parameter)
{
unsigned long flags;
unsigned next, tail;
struct event_t *event;
gig_dbg(DEBUG_EVENT, "queueing event %d for cid %d", type, cid);
spin_lock_irqsave(&cs->ev_lock, flags);
tail = cs->ev_tail;
next = (tail + 1) % MAX_EVENTS;
if (unlikely(next == cs->ev_head)) {
dev_err(cs->dev, "event queue full\n");
kfree(ptr);
} else {
event = cs->events + tail;
event->type = type;
event->cid = cid;
event->ptr = ptr;
event->arg = NULL;
event->parameter = parameter;
event->at_state = NULL;
cs->ev_tail = next;
}
spin_unlock_irqrestore(&cs->ev_lock, flags);
} }
/** /**
...@@ -417,190 +444,188 @@ static int cid_of_response(char *s) ...@@ -417,190 +444,188 @@ static int cid_of_response(char *s)
*/ */
void gigaset_handle_modem_response(struct cardstate *cs) void gigaset_handle_modem_response(struct cardstate *cs)
{ {
unsigned char *argv[MAX_REC_PARAMS + 1]; char *eoc, *psep, *ptr;
int params;
int i, j;
const struct resp_type_t *rt; const struct resp_type_t *rt;
const struct zsau_resp_t *zr; const struct zsau_resp_t *zr;
int curarg; int cid, parameter;
unsigned long flags; u8 type, value;
unsigned next, tail, head;
struct event_t *event; if (!cs->cbytes) {
int resp_code;
int param_type;
int abort;
size_t len;
int cid;
int rawstring;
len = cs->cbytes;
if (!len) {
/* ignore additional LFs/CRs (M10x config mode or cx100) */ /* ignore additional LFs/CRs (M10x config mode or cx100) */
gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[0]); gig_dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[0]);
return; return;
} }
cs->respdata[len] = 0; cs->respdata[cs->cbytes] = 0;
argv[0] = cs->respdata;
params = 1;
if (cs->at_state.getstring) { if (cs->at_state.getstring) {
/* getstring only allowed without cid at the moment */ /* state machine wants next line verbatim */
cs->at_state.getstring = 0; cs->at_state.getstring = 0;
rawstring = 1; ptr = kstrdup(cs->respdata, GFP_ATOMIC);
cid = 0; gig_dbg(DEBUG_EVENT, "string==%s", ptr ? ptr : "NULL");
} else { add_cid_event(cs, 0, RSP_STRING, ptr, 0);
/* parse line */ return;
for (i = 0; i < len; i++) }
switch (cs->respdata[i]) {
case ';':
case ',':
case '=':
if (params > MAX_REC_PARAMS) {
dev_warn(cs->dev,
"too many parameters in response\n");
/* need last parameter (might be CID) */
params--;
}
argv[params++] = cs->respdata + i + 1;
}
rawstring = 0;
cid = params > 1 ? cid_of_response(argv[params - 1]) : 0;
if (cid < 0) {
gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
NULL, 0, NULL);
return;
}
for (j = 1; j < params; ++j) /* look up response type */
argv[j][-1] = 0; for (rt = resp_type; rt->response; ++rt) {
eoc = skip_prefix(cs->respdata, rt->response);
if (eoc)
break;
}
if (!rt->response) {
add_cid_event(cs, 0, RSP_NONE, NULL, 0);
gig_dbg(DEBUG_EVENT, "unknown modem response: '%s'\n",
cs->respdata);
return;
}
gig_dbg(DEBUG_EVENT, "CMD received: %s", argv[0]); /* check for CID */
if (cid) { psep = strrchr(cs->respdata, ';');
--params; if (psep &&
gig_dbg(DEBUG_EVENT, "CID: %s", argv[params]); !kstrtoint(psep + 1, 10, &cid) &&
} cid >= 1 && cid <= 65535) {
gig_dbg(DEBUG_EVENT, "available params: %d", params - 1); /* valid CID: chop it off */
for (j = 1; j < params; j++) *psep = 0;
gig_dbg(DEBUG_EVENT, "param %d: %s", j, argv[j]); } else {
/* no valid CID: leave unchanged */
cid = 0;
} }
spin_lock_irqsave(&cs->ev_lock, flags); gig_dbg(DEBUG_EVENT, "CMD received: %s", cs->respdata);
head = cs->ev_head; if (cid)
tail = cs->ev_tail; gig_dbg(DEBUG_EVENT, "CID: %d", cid);
abort = 1; switch (rt->type) {
curarg = 0; case RT_NOTHING:
while (curarg < params) { /* check parameter separator */
next = (tail + 1) % MAX_EVENTS; if (*eoc)
if (unlikely(next == head)) { goto bad_param; /* extra parameter */
dev_err(cs->dev, "event queue full\n");
break;
}
event = cs->events + tail; add_cid_event(cs, cid, rt->resp_code, NULL, 0);
event->at_state = NULL; break;
event->cid = cid;
event->ptr = NULL;
event->arg = NULL;
tail = next;
if (rawstring) { case RT_RING:
resp_code = RSP_STRING; /* check parameter separator */
param_type = RT_STRING; if (!*eoc)
} else { eoc = NULL; /* no parameter */
for (rt = resp_type; rt->response; ++rt) else if (*eoc++ != ',')
if (!strcmp(argv[curarg], rt->response)) goto bad_param;
add_cid_event(cs, 0, rt->resp_code, NULL, cid);
/* process parameters as individual responses */
while (eoc) {
/* look up parameter type */
psep = NULL;
for (rt = resp_type; rt->response; ++rt) {
psep = skip_prefix(eoc, rt->response);
if (psep)
break; break;
}
if (!rt->response) { /* all legal parameters are of type RT_STRING */
event->type = RSP_NONE; if (!psep || rt->type != RT_STRING) {
gig_dbg(DEBUG_EVENT, dev_warn(cs->dev,
"unknown modem response: '%s'\n", "illegal RING parameter: '%s'\n",
argv[curarg]); eoc);
break; return;
} }
resp_code = rt->resp_code; /* skip parameter value separator */
param_type = rt->type; if (*psep++ != '=')
++curarg; goto bad_param;
}
event->type = resp_code; /* look up end of parameter */
eoc = strchr(psep, ',');
if (eoc)
*eoc++ = 0;
switch (param_type) { /* retrieve parameter value */
case RT_NOTHING: ptr = kstrdup(psep, GFP_ATOMIC);
break;
case RT_RING: /* queue event */
if (!cid) { add_cid_event(cs, cid, rt->resp_code, ptr, 0);
dev_err(cs->dev, }
"received RING without CID!\n"); break;
event->type = RSP_INVAL;
abort = 1; case RT_ZSAU:
} else { /* check parameter separator */
event->cid = 0; if (!*eoc) {
event->parameter = cid; /* no parameter */
abort = 0; add_cid_event(cs, cid, rt->resp_code, NULL, ZSAU_NONE);
}
break; break;
case RT_ZSAU: }
if (curarg >= params) { if (*eoc++ != '=')
event->parameter = ZSAU_NONE; goto bad_param;
/* look up parameter value */
for (zr = zsau_resp; zr->str; ++zr)
if (!strcmp(eoc, zr->str))
break; break;
} if (!zr->str)
for (zr = zsau_resp; zr->str; ++zr) goto bad_param;
if (!strcmp(argv[curarg], zr->str))
break; add_cid_event(cs, cid, rt->resp_code, NULL, zr->code);
event->parameter = zr->code; break;
if (!zr->str)
dev_warn(cs->dev, case RT_STRING:
"%s: unknown parameter %s after ZSAU\n", /* check parameter separator */
__func__, argv[curarg]); if (*eoc++ != '=')
++curarg; goto bad_param;
break;
case RT_STRING: /* retrieve parameter value */
if (curarg < params) { ptr = kstrdup(eoc, GFP_ATOMIC);
event->ptr = kstrdup(argv[curarg], GFP_ATOMIC);
if (!event->ptr) /* queue event */
dev_err(cs->dev, "out of memory\n"); add_cid_event(cs, cid, rt->resp_code, ptr, 0);
++curarg; break;
}
gig_dbg(DEBUG_EVENT, "string==%s", case RT_ZCAU:
event->ptr ? (char *) event->ptr : "NULL"); /* check parameter separators */
break; if (*eoc++ != '=')
case RT_ZCAU: goto bad_param;
event->parameter = -1; psep = strchr(eoc, ',');
if (curarg + 1 < params) { if (!psep)
u8 type, value; goto bad_param;
*psep++ = 0;
i = kstrtou8(argv[curarg++], 16, &type);
j = kstrtou8(argv[curarg++], 16, &value); /* decode parameter values */
if (i == 0 && j == 0) if (kstrtou8(eoc, 16, &type) || kstrtou8(psep, 16, &value)) {
event->parameter = (type << 8) | value; *--psep = ',';
} else goto bad_param;
curarg = params - 1;
break;
case RT_NUMBER:
if (curarg >= params ||
kstrtoint(argv[curarg++], 10, &event->parameter))
event->parameter = -1;
gig_dbg(DEBUG_EVENT, "parameter==%d", event->parameter);
break;
} }
parameter = (type << 8) | value;
if (resp_code == RSP_ZDLE) add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
cs->dle = event->parameter; break;
if (abort) case RT_NUMBER:
break; /* check parameter separator */
} if (*eoc++ != '=')
goto bad_param;
cs->ev_tail = tail; /* decode parameter value */
spin_unlock_irqrestore(&cs->ev_lock, flags); if (kstrtoint(eoc, 10, &parameter))
goto bad_param;
/* special case ZDLE: set flag before queueing event */
if (rt->resp_code == RSP_ZDLE)
cs->dle = parameter;
if (curarg != params) add_cid_event(cs, cid, rt->resp_code, NULL, parameter);
gig_dbg(DEBUG_EVENT, break;
"invalid number of processed parameters: %d/%d",
curarg, params); bad_param:
/* parameter unexpected, incomplete or malformed */
dev_warn(cs->dev, "bad parameter in response '%s'\n",
cs->respdata);
add_cid_event(cs, cid, rt->resp_code, NULL, -1);
break;
default:
dev_err(cs->dev, "%s: internal error on '%s'\n",
__func__, cs->respdata);
}
} }
EXPORT_SYMBOL_GPL(gigaset_handle_modem_response); EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment