1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Common data handling layer for ser_gigaset and usb_gigaset
5 * Copyright (c) 2005 by Tilman Schmidt <tilman@imap.cc>,
6 * Hansjoerg Lipp <hjlipp@web.de>,
9 * =====================================================================
10 * =====================================================================
14 #include <linux/crc-ccitt.h>
15 #include <linux/bitrev.h>
16 #include <linux/export.h>
18 /* check if byte must be stuffed/escaped
19 * I'm not sure which data should be encoded.
20 * Therefore I will go the hard way and encode every value
21 * less than 0x20, the flag sequence and the control escape char.
23 static inline int muststuff(unsigned char c)
25 if (c < PPP_TRANS) return 1;
26 if (c == PPP_FLAG) return 1;
27 if (c == PPP_ESCAPE) return 1;
28 /* other possible candidates: */
29 /* 0x91: XON with parity set */
30 /* 0x93: XOFF with parity set */
34 /* == data input =========================================================== */
36 /* process a block of received bytes in command mode
37 * (mstate != MS_LOCKED && (inputstate & INS_command))
38 * Append received bytes to the command response buffer and forward them
39 * line by line to the response handler. Exit whenever a mode/state change
40 * might have occurred.
41 * Note: Received lines may be terminated by CR, LF, or CR LF, which will be
42 * removed before passing the line to the response handler.
44 * number of processed bytes
46 static unsigned cmd_loop(unsigned numbytes, struct inbuf_t *inbuf)
48 unsigned char *src = inbuf->data + inbuf->head;
49 struct cardstate *cs = inbuf->cs;
50 unsigned cbytes = cs->cbytes;
51 unsigned procbytes = 0;
54 while (procbytes < numbytes) {
60 if (cbytes == 0 && cs->respdata[0] == '\r') {
61 /* collapse LF with preceding CR */
67 /* end of message line, pass to response handler */
68 if (cbytes >= MAX_RESP_SIZE) {
69 dev_warn(cs->dev, "response too large (%d)\n",
71 cbytes = MAX_RESP_SIZE;
74 gigaset_dbg_buffer(DEBUG_TRANSCMD, "received response",
75 cbytes, cs->respdata);
76 gigaset_handle_modem_response(cs);
79 /* store EOL byte for CRLF collapsing */
82 /* cs->dle may have changed */
83 if (cs->dle && !(inbuf->inputstate & INS_DLE_command))
84 inbuf->inputstate &= ~INS_command;
86 /* return for reevaluating state */
90 if (inbuf->inputstate & INS_DLE_char) {
91 /* quoted DLE: clear quote flag */
92 inbuf->inputstate &= ~INS_DLE_char;
94 (inbuf->inputstate & INS_DLE_command)) {
95 /* DLE escape, pass up for handling */
96 inbuf->inputstate |= INS_DLE_char;
99 /* quoted or not in DLE mode: treat as regular data */
102 /* append to line buffer if possible */
103 if (cbytes < MAX_RESP_SIZE)
104 cs->respdata[cbytes] = c;
113 /* process a block of received bytes in lock mode
114 * All received bytes are passed unmodified to the tty i/f.
116 * number of processed bytes
118 static unsigned lock_loop(unsigned numbytes, struct inbuf_t *inbuf)
120 unsigned char *src = inbuf->data + inbuf->head;
122 gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response", numbytes, src);
123 gigaset_if_receive(inbuf->cs, src, numbytes);
127 /* process a block of received bytes in HDLC data mode
128 * (mstate != MS_LOCKED && !(inputstate & INS_command) && proto2 == L2_HDLC)
129 * Collect HDLC frames, undoing byte stuffing and watching for DLE escapes.
130 * When a frame is complete, check the FCS and pass valid frames to the LL.
131 * If DLE is encountered, return immediately to let the caller handle it.
133 * number of processed bytes
135 static unsigned hdlc_loop(unsigned numbytes, struct inbuf_t *inbuf)
137 struct cardstate *cs = inbuf->cs;
138 struct bc_state *bcs = cs->bcs;
139 int inputstate = bcs->inputstate;
140 __u16 fcs = bcs->rx_fcs;
141 struct sk_buff *skb = bcs->rx_skb;
142 unsigned char *src = inbuf->data + inbuf->head;
143 unsigned procbytes = 0;
146 if (inputstate & INS_byte_stuff) {
149 inputstate &= ~INS_byte_stuff;
153 while (procbytes < numbytes) {
157 if (inputstate & INS_DLE_char) {
158 /* quoted DLE: clear quote flag */
159 inputstate &= ~INS_DLE_char;
160 } else if (cs->dle || (inputstate & INS_DLE_command)) {
161 /* DLE escape, pass up for handling */
162 inputstate |= INS_DLE_char;
167 if (c == PPP_ESCAPE) {
168 /* byte stuffing indicator: pull in next byte */
169 if (procbytes >= numbytes) {
170 /* end of buffer, save for later processing */
171 inputstate |= INS_byte_stuff;
178 if (inputstate & INS_DLE_char) {
179 /* quoted DLE: clear quote flag */
180 inputstate &= ~INS_DLE_char;
181 } else if (cs->dle ||
182 (inputstate & INS_DLE_command)) {
183 /* DLE escape, pass up for handling */
185 INS_DLE_char | INS_byte_stuff;
190 #ifdef CONFIG_GIGASET_DEBUG
192 gig_dbg(DEBUG_HDLC, "byte stuffed: 0x%02x", c);
194 } else if (c == PPP_FLAG) {
195 /* end of frame: process content if any */
196 if (inputstate & INS_have_data) {
198 "7e----------------------------");
200 /* check and pass received frame */
203 gigaset_isdn_rcv_err(bcs);
204 } else if (skb->len < 2) {
205 /* frame too short for FCS */
207 "short frame (%d)\n",
209 gigaset_isdn_rcv_err(bcs);
210 dev_kfree_skb_any(skb);
211 } else if (fcs != PPP_GOODFCS) {
212 /* frame check error */
214 "Checksum failed, %u bytes corrupted!\n",
216 gigaset_isdn_rcv_err(bcs);
217 dev_kfree_skb_any(skb);
220 __skb_trim(skb, skb->len - 2);
221 gigaset_skb_rcvd(bcs, skb);
224 /* prepare reception of next frame */
225 inputstate &= ~INS_have_data;
226 skb = gigaset_new_rx_skb(bcs);
228 /* empty frame (7E 7E) */
229 #ifdef CONFIG_GIGASET_DEBUG
234 gigaset_isdn_rcv_err(bcs);
235 skb = gigaset_new_rx_skb(bcs);
241 #ifdef CONFIG_GIGASET_DEBUG
242 } else if (muststuff(c)) {
243 /* Should not happen. Possible after ZDLE=1<CR><LF>. */
244 gig_dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
248 /* regular data byte, append to skb */
249 #ifdef CONFIG_GIGASET_DEBUG
250 if (!(inputstate & INS_have_data)) {
251 gig_dbg(DEBUG_HDLC, "7e (%d x) ================",
256 inputstate |= INS_have_data;
258 if (skb->len >= bcs->rx_bufsize) {
259 dev_warn(cs->dev, "received packet too long\n");
260 dev_kfree_skb_any(skb);
261 /* skip remainder of packet */
262 bcs->rx_skb = skb = NULL;
264 __skb_put_u8(skb, c);
265 fcs = crc_ccitt_byte(fcs, c);
270 bcs->inputstate = inputstate;
275 /* process a block of received bytes in transparent data mode
276 * (mstate != MS_LOCKED && !(inputstate & INS_command) && proto2 != L2_HDLC)
277 * Invert bytes, undoing byte stuffing and watching for DLE escapes.
278 * If DLE is encountered, return immediately to let the caller handle it.
280 * number of processed bytes
282 static unsigned iraw_loop(unsigned numbytes, struct inbuf_t *inbuf)
284 struct cardstate *cs = inbuf->cs;
285 struct bc_state *bcs = cs->bcs;
286 int inputstate = bcs->inputstate;
287 struct sk_buff *skb = bcs->rx_skb;
288 unsigned char *src = inbuf->data + inbuf->head;
289 unsigned procbytes = 0;
293 /* skip this block */
294 gigaset_new_rx_skb(bcs);
298 while (procbytes < numbytes && skb->len < bcs->rx_bufsize) {
303 if (inputstate & INS_DLE_char) {
304 /* quoted DLE: clear quote flag */
305 inputstate &= ~INS_DLE_char;
306 } else if (cs->dle || (inputstate & INS_DLE_command)) {
307 /* DLE escape, pass up for handling */
308 inputstate |= INS_DLE_char;
313 /* regular data byte: append to current skb */
314 inputstate |= INS_have_data;
315 __skb_put_u8(skb, bitrev8(c));
319 if (inputstate & INS_have_data) {
320 gigaset_skb_rcvd(bcs, skb);
321 inputstate &= ~INS_have_data;
322 gigaset_new_rx_skb(bcs);
325 bcs->inputstate = inputstate;
329 /* process DLE escapes
330 * Called whenever a DLE sequence might be encountered in the input stream.
331 * Either processes the entire DLE sequence or, if that isn't possible,
332 * notes the fact that an initial DLE has been received in the INS_DLE_char
333 * inputstate flag and resumes processing of the sequence on the next call.
335 static void handle_dle(struct inbuf_t *inbuf)
337 struct cardstate *cs = inbuf->cs;
339 if (cs->mstate == MS_LOCKED)
340 return; /* no DLE processing in lock mode */
342 if (!(inbuf->inputstate & INS_DLE_char)) {
344 if (inbuf->data[inbuf->head] == DLE_FLAG &&
345 (cs->dle || inbuf->inputstate & INS_DLE_command)) {
346 /* start of DLE sequence */
348 if (inbuf->head == inbuf->tail ||
349 inbuf->head == RBUFSIZE) {
350 /* end of buffer, save for later processing */
351 inbuf->inputstate |= INS_DLE_char;
355 /* regular data byte */
360 /* consume pending DLE */
361 inbuf->inputstate &= ~INS_DLE_char;
363 switch (inbuf->data[inbuf->head]) {
364 case 'X': /* begin of event message */
365 if (inbuf->inputstate & INS_command)
367 "received <DLE>X in command mode\n");
368 inbuf->inputstate |= INS_command | INS_DLE_command;
369 inbuf->head++; /* byte consumed */
371 case '.': /* end of event message */
372 if (!(inbuf->inputstate & INS_DLE_command))
374 "received <DLE>. without <DLE>X\n");
375 inbuf->inputstate &= ~INS_DLE_command;
376 /* return to data mode if in DLE mode */
378 inbuf->inputstate &= ~INS_command;
379 inbuf->head++; /* byte consumed */
381 case DLE_FLAG: /* DLE in data stream */
383 inbuf->inputstate |= INS_DLE_char;
384 if (!(cs->dle || inbuf->inputstate & INS_DLE_command))
386 "received <DLE><DLE> not in DLE mode\n");
387 break; /* quoted byte left in buffer */
389 dev_notice(cs->dev, "received <DLE><%02x>\n",
390 inbuf->data[inbuf->head]);
391 /* quoted byte left in buffer */
396 * gigaset_m10x_input() - process a block of data received from the device
397 * @inbuf: received data and device descriptor structure.
399 * Called by hardware module {ser,usb}_gigaset with a block of received
400 * bytes. Separates the bytes received over the serial data channel into
401 * user data and command replies (locked/unlocked) according to the
402 * current state of the interface.
404 void gigaset_m10x_input(struct inbuf_t *inbuf)
406 struct cardstate *cs = inbuf->cs;
407 unsigned numbytes, procbytes;
409 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", inbuf->head, inbuf->tail);
411 while (inbuf->head != inbuf->tail) {
412 /* check for DLE escape */
415 /* process a contiguous block of bytes */
416 numbytes = (inbuf->head > inbuf->tail ?
417 RBUFSIZE : inbuf->tail) - inbuf->head;
418 gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
420 * numbytes may be 0 if handle_dle() ate the last byte.
421 * This does no harm, *_loop() will just return 0 immediately.
424 if (cs->mstate == MS_LOCKED)
425 procbytes = lock_loop(numbytes, inbuf);
426 else if (inbuf->inputstate & INS_command)
427 procbytes = cmd_loop(numbytes, inbuf);
428 else if (cs->bcs->proto2 == L2_HDLC)
429 procbytes = hdlc_loop(numbytes, inbuf);
431 procbytes = iraw_loop(numbytes, inbuf);
432 inbuf->head += procbytes;
434 /* check for buffer wraparound */
435 if (inbuf->head >= RBUFSIZE)
438 gig_dbg(DEBUG_INTR, "head set to %u", inbuf->head);
441 EXPORT_SYMBOL_GPL(gigaset_m10x_input);
444 /* == data output ========================================================== */
447 * Encode a data packet into an octet stuffed HDLC frame with FCS,
448 * opening and closing flags, preserving headroom data.
450 * skb skb containing original packet (freed upon return)
452 * pointer to newly allocated skb containing the result frame
453 * and the original link layer header, NULL on error
455 static struct sk_buff *HDLC_Encode(struct sk_buff *skb)
457 struct sk_buff *hdlc_skb;
462 unsigned int stuf_cnt;
471 fcs = crc_ccitt_byte(fcs, *cp++);
473 fcs ^= 0xffff; /* complement */
475 /* size of new buffer: original size + number of stuffing bytes
476 * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
477 * + room for link layer header
479 hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + skb->mac_len);
481 dev_kfree_skb_any(skb);
485 /* Copy link layer header into new skb */
486 skb_reset_mac_header(hdlc_skb);
487 skb_reserve(hdlc_skb, skb->mac_len);
488 memcpy(skb_mac_header(hdlc_skb), skb_mac_header(skb), skb->mac_len);
489 hdlc_skb->mac_len = skb->mac_len;
491 /* Add flag sequence in front of everything.. */
492 skb_put_u8(hdlc_skb, PPP_FLAG);
494 /* Perform byte stuffing while copying data. */
496 if (muststuff(*skb->data)) {
497 skb_put_u8(hdlc_skb, PPP_ESCAPE);
498 skb_put_u8(hdlc_skb, (*skb->data++) ^ PPP_TRANS);
500 skb_put_u8(hdlc_skb, *skb->data++);
503 /* Finally add FCS (byte stuffed) and flag sequence */
504 c = (fcs & 0x00ff); /* least significant byte first */
506 skb_put_u8(hdlc_skb, PPP_ESCAPE);
509 skb_put_u8(hdlc_skb, c);
511 c = ((fcs >> 8) & 0x00ff);
513 skb_put_u8(hdlc_skb, PPP_ESCAPE);
516 skb_put_u8(hdlc_skb, c);
518 skb_put_u8(hdlc_skb, PPP_FLAG);
520 dev_kfree_skb_any(skb);
525 * Encode a data packet into an octet stuffed raw bit inverted frame,
526 * preserving headroom data.
528 * skb skb containing original packet (freed upon return)
530 * pointer to newly allocated skb containing the result frame
531 * and the original link layer header, NULL on error
533 static struct sk_buff *iraw_encode(struct sk_buff *skb)
535 struct sk_buff *iraw_skb;
540 /* size of new buffer (worst case = every byte must be stuffed):
541 * 2 * original size + room for link layer header
543 iraw_skb = dev_alloc_skb(2 * skb->len + skb->mac_len);
545 dev_kfree_skb_any(skb);
549 /* copy link layer header into new skb */
550 skb_reset_mac_header(iraw_skb);
551 skb_reserve(iraw_skb, skb->mac_len);
552 memcpy(skb_mac_header(iraw_skb), skb_mac_header(skb), skb->mac_len);
553 iraw_skb->mac_len = skb->mac_len;
555 /* copy and stuff data */
561 skb_put_u8(iraw_skb, c);
562 skb_put_u8(iraw_skb, c);
564 dev_kfree_skb_any(skb);
569 * gigaset_m10x_send_skb() - queue an skb for sending
570 * @bcs: B channel descriptor structure.
571 * @skb: data to send.
573 * Called by LL to encode and queue an skb for sending, and start
574 * transmission if necessary.
575 * Once the payload data has been transmitted completely, gigaset_skb_sent()
576 * will be called with the skb's link layer header preserved.
579 * number of bytes accepted for sending (skb->len) if ok,
580 * error code < 0 (eg. -ENOMEM) on error
582 int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
584 struct cardstate *cs = bcs->cs;
585 unsigned len = skb->len;
588 if (bcs->proto2 == L2_HDLC)
589 skb = HDLC_Encode(skb);
591 skb = iraw_encode(skb);
594 "unable to allocate memory for encoding!\n");
598 skb_queue_tail(&bcs->squeue, skb);
599 spin_lock_irqsave(&cs->lock, flags);
601 tasklet_schedule(&cs->write_tasklet);
602 spin_unlock_irqrestore(&cs->lock, flags);
604 return len; /* ok so far */
606 EXPORT_SYMBOL_GPL(gigaset_m10x_send_skb);