2 * Common data handling layer for ser_gigaset and usb_gigaset
4 * Copyright (c) 2005 by Tilman Schmidt <tilman@imap.cc>,
5 * Hansjoerg Lipp <hjlipp@web.de>,
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 * =====================================================================
17 #include <linux/crc-ccitt.h>
18 #include <linux/bitrev.h>
20 //#define GIG_M10x_STUFF_VOICE_DATA
22 /* check if byte must be stuffed/escaped
23 * I'm not sure which data should be encoded.
24 * Therefore I will go the hard way and decode every value
25 * less than 0x20, the flag sequence and the control escape char.
27 static inline int muststuff(unsigned char c)
29 if (c < PPP_TRANS) return 1;
30 if (c == PPP_FLAG) return 1;
31 if (c == PPP_ESCAPE) return 1;
32 /* other possible candidates: */
33 /* 0x91: XON with parity set */
34 /* 0x93: XOFF with parity set */
38 /* == data input =========================================================== */
40 /* process a block of received bytes in command mode (modem response)
42 * number of processed bytes
44 static inline int cmd_loop(unsigned char c, unsigned char *src, int numbytes,
45 struct inbuf_t *inbuf)
47 struct cardstate *cs = inbuf->cs;
48 unsigned cbytes = cs->cbytes;
49 int inputstate = inbuf->inputstate;
50 int startbytes = numbytes;
53 cs->respdata[cbytes] = c;
54 if (c == 10 || c == 13) {
55 gig_dbg(DEBUG_TRANSCMD, "%s: End of Command (%d Bytes)",
58 gigaset_handle_modem_response(cs); /* can change
63 !(inputstate & INS_DLE_command)) {
64 inputstate &= ~INS_command;
68 /* advance in line buffer, checking for overflow */
69 if (cbytes < MAX_RESP_SIZE - 1)
72 dev_warn(cs->dev, "response too large\n");
80 (cs->dle || inputstate & INS_DLE_command)) {
81 inputstate |= INS_DLE_char;
87 inbuf->inputstate = inputstate;
89 return startbytes - numbytes;
92 /* process a block of received bytes in lock mode (tty i/f)
94 * number of processed bytes
96 static inline int lock_loop(unsigned char *src, int numbytes,
97 struct inbuf_t *inbuf)
99 struct cardstate *cs = inbuf->cs;
101 gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response",
103 gigaset_if_receive(cs, src, numbytes);
108 /* process a block of received bytes in HDLC data mode
109 * Collect HDLC frames, undoing byte stuffing and watching for DLE escapes.
110 * When a frame is complete, check the FCS and pass valid frames to the LL.
111 * If DLE is encountered, return immediately to let the caller handle it.
113 * number of processed bytes
114 * numbytes (all bytes processed) on error --FIXME
116 static inline int hdlc_loop(unsigned char c, unsigned char *src, int numbytes,
117 struct inbuf_t *inbuf)
119 struct cardstate *cs = inbuf->cs;
120 struct bc_state *bcs = inbuf->bcs;
121 int inputstate = bcs->inputstate;
122 __u16 fcs = bcs->fcs;
123 struct sk_buff *skb = bcs->skb;
125 struct sk_buff *compskb;
126 int startbytes = numbytes;
129 if (unlikely(inputstate & INS_byte_stuff)) {
130 inputstate &= ~INS_byte_stuff;
134 if (unlikely(c == PPP_ESCAPE)) {
135 if (unlikely(!numbytes)) {
136 inputstate |= INS_byte_stuff;
141 if (unlikely(c == DLE_FLAG &&
143 inbuf->inputstate & INS_DLE_command))) {
144 inbuf->inputstate |= INS_DLE_char;
145 inputstate |= INS_byte_stuff;
150 #ifdef CONFIG_GIGASET_DEBUG
151 if (unlikely(!muststuff(c)))
152 gig_dbg(DEBUG_HDLC, "byte stuffed: 0x%02x", c);
154 } else if (unlikely(c == PPP_FLAG)) {
155 if (unlikely(inputstate & INS_skip_frame)) {
156 if (!(inputstate & INS_have_data)) { /* 7E 7E */
157 #ifdef CONFIG_GIGASET_DEBUG
162 "7e----------------------------");
166 gigaset_rcv_error(NULL, cs, bcs);
167 } else if (!(inputstate & INS_have_data)) { /* 7E 7E */
168 #ifdef CONFIG_GIGASET_DEBUG
174 "7e----------------------------");
179 if (unlikely(fcs != PPP_GOODFCS)) {
181 "Packet checksum at %lu failed, "
182 "packet is corrupted (%u bytes)!\n",
183 bcs->rcvbytes, skb->len);
185 gigaset_rcv_error(compskb, cs, bcs);
188 if (likely((l = skb->len) > 2)) {
194 inputstate |= INS_skip_frame;
197 "invalid packet size (1)!\n");
199 gigaset_rcv_error(NULL,
203 if (likely(!(error ||
206 gigaset_rcv_skb(skb, cs, bcs);
216 inputstate &= ~(INS_have_data | INS_skip_frame);
217 if (unlikely(bcs->ignore)) {
218 inputstate |= INS_skip_frame;
220 } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)) {
221 skb_reserve(skb, HW_HDR_LEN);
224 "could not allocate new skb\n");
225 inputstate |= INS_skip_frame;
229 #ifdef CONFIG_GIGASET_DEBUG
230 } else if (unlikely(muststuff(c))) {
231 /* Should not happen. Possible after ZDLE=1<CR><LF>. */
232 gig_dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
238 #ifdef CONFIG_GIGASET_DEBUG
239 if (unlikely(!(inputstate & INS_have_data))) {
240 gig_dbg(DEBUG_HDLC, "7e (%d x) ================",
246 inputstate |= INS_have_data;
248 if (likely(!(inputstate & INS_skip_frame))) {
249 if (unlikely(skb->len == SBUFSIZE)) {
250 dev_warn(cs->dev, "received packet too long\n");
251 dev_kfree_skb_any(skb);
253 inputstate |= INS_skip_frame;
256 *__skb_put(skb, 1) = c;
257 fcs = crc_ccitt_byte(fcs, c);
260 if (unlikely(!numbytes))
264 if (unlikely(c == DLE_FLAG &&
266 inbuf->inputstate & INS_DLE_command))) {
267 inbuf->inputstate |= INS_DLE_char;
271 bcs->inputstate = inputstate;
274 return startbytes - numbytes;
277 /* process a block of received bytes in transparent data mode
278 * Invert bytes, undoing byte stuffing and watching for DLE escapes.
279 * If DLE is encountered, return immediately to let the caller handle it.
281 * number of processed bytes
282 * numbytes (all bytes processed) on error --FIXME
284 static inline int iraw_loop(unsigned char c, unsigned char *src, int numbytes,
285 struct inbuf_t *inbuf)
287 struct cardstate *cs = inbuf->cs;
288 struct bc_state *bcs = inbuf->bcs;
289 int inputstate = bcs->inputstate;
290 struct sk_buff *skb = bcs->skb;
291 int startbytes = numbytes;
295 inputstate |= INS_have_data;
297 if (likely(!(inputstate & INS_skip_frame))) {
298 if (unlikely(skb->len == SBUFSIZE)) {
299 //FIXME just pass skb up and allocate a new one
300 dev_warn(cs->dev, "received packet too long\n");
301 dev_kfree_skb_any(skb);
303 inputstate |= INS_skip_frame;
306 *__skb_put(skb, 1) = bitrev8(c);
309 if (unlikely(!numbytes))
313 if (unlikely(c == DLE_FLAG &&
315 inbuf->inputstate & INS_DLE_command))) {
316 inbuf->inputstate |= INS_DLE_char;
322 if (likely(inputstate & INS_have_data)) {
323 if (likely(!(inputstate & INS_skip_frame))) {
324 gigaset_rcv_skb(skb, cs, bcs);
326 inputstate &= ~(INS_have_data | INS_skip_frame);
327 if (unlikely(bcs->ignore)) {
328 inputstate |= INS_skip_frame;
330 } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN))
332 skb_reserve(skb, HW_HDR_LEN);
334 dev_warn(cs->dev, "could not allocate new skb\n");
335 inputstate |= INS_skip_frame;
339 bcs->inputstate = inputstate;
341 return startbytes - numbytes;
344 /* process a block of data received from the device
346 void gigaset_m10x_input(struct inbuf_t *inbuf)
348 struct cardstate *cs;
349 unsigned tail, head, numbytes;
350 unsigned char *src, c;
355 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
359 src = inbuf->data + head;
360 numbytes = (head > tail ? RBUFSIZE : tail) - head;
361 gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
364 if (cs->mstate == MS_LOCKED) {
365 procbytes = lock_loop(src, numbytes, inbuf);
367 numbytes -= procbytes;
371 if (c == DLE_FLAG && (cs->dle ||
372 inbuf->inputstate & INS_DLE_command)) {
373 if (!(inbuf->inputstate & INS_DLE_char)) {
374 inbuf->inputstate |= INS_DLE_char;
377 /* <DLE> <DLE> => <DLE> in data stream */
378 inbuf->inputstate &= ~INS_DLE_char;
381 if (!(inbuf->inputstate & INS_DLE_char)) {
383 /* FIXME use function pointers? */
384 if (inbuf->inputstate & INS_command)
385 procbytes = cmd_loop(c, src, numbytes, inbuf);
386 else if (inbuf->bcs->proto2 == ISDN_PROTO_L2_HDLC)
387 procbytes = hdlc_loop(c, src, numbytes, inbuf);
389 procbytes = iraw_loop(c, src, numbytes, inbuf);
392 numbytes -= procbytes;
393 } else { /* DLE char */
394 inbuf->inputstate &= ~INS_DLE_char;
396 case 'X': /*begin of command*/
397 #ifdef CONFIG_GIGASET_DEBUG
398 if (inbuf->inputstate & INS_command)
400 "received <DLE> 'X' in command mode\n");
403 INS_command | INS_DLE_command;
405 case '.': /*end of command*/
406 #ifdef CONFIG_GIGASET_DEBUG
407 if (!(inbuf->inputstate & INS_command))
409 "received <DLE> '.' in hdlc mode\n");
411 inbuf->inputstate &= cs->dle ?
412 ~(INS_DLE_command|INS_command)
415 //case DLE_FLAG: /*DLE_FLAG in data stream*/ /* schon oben behandelt! */
418 "received 0x10 0x%02x!\n",
420 /* FIXME: reset driver?? */
426 /* end of buffer, check for wrap */
438 gig_dbg(DEBUG_INTR, "setting head to %u", head);
442 EXPORT_SYMBOL_GPL(gigaset_m10x_input);
445 /* == data output ========================================================== */
447 /* Encoding of a PPP packet into an octet stuffed HDLC frame
448 * with FCS, opening and closing flags.
450 * skb skb containing original packet (freed upon return)
451 * head number of headroom bytes to allocate in result skb
452 * tail number of tailroom bytes to allocate in result skb
454 * pointer to newly allocated skb containing the result frame
456 static struct sk_buff *HDLC_Encode(struct sk_buff *skb, int head, int tail)
458 struct sk_buff *hdlc_skb;
463 unsigned int stuf_cnt;
472 fcs = crc_ccitt_byte(fcs, *cp++);
474 fcs ^= 0xffff; /* complement */
476 /* size of new buffer: original size + number of stuffing bytes
477 * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
479 hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + tail + head);
484 skb_reserve(hdlc_skb, head);
486 /* Copy acknowledge request into new skb */
487 memcpy(hdlc_skb->head, skb->head, 2);
489 /* Add flag sequence in front of everything.. */
490 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
492 /* Perform byte stuffing while copying data. */
494 if (muststuff(*skb->data)) {
495 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
496 *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
498 *(skb_put(hdlc_skb, 1)) = *skb->data++;
501 /* Finally add FCS (byte stuffed) and flag sequence */
502 c = (fcs & 0x00ff); /* least significant byte first */
504 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
507 *(skb_put(hdlc_skb, 1)) = c;
509 c = ((fcs >> 8) & 0x00ff);
511 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
514 *(skb_put(hdlc_skb, 1)) = c;
516 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
522 /* Encoding of a raw packet into an octet stuffed bit inverted frame
524 * skb skb containing original packet (freed upon return)
525 * head number of headroom bytes to allocate in result skb
526 * tail number of tailroom bytes to allocate in result skb
528 * pointer to newly allocated skb containing the result frame
530 static struct sk_buff *iraw_encode(struct sk_buff *skb, int head, int tail)
532 struct sk_buff *iraw_skb;
537 /* worst case: every byte must be stuffed */
538 iraw_skb = dev_alloc_skb(2*skb->len + tail + head);
543 skb_reserve(iraw_skb, head);
550 *(skb_put(iraw_skb, 1)) = c;
551 *(skb_put(iraw_skb, 1)) = c;
558 * called by common.c to queue an skb for sending
559 * and start transmission if necessary
561 * B Channel control structure
564 * number of bytes accepted for sending
565 * (skb->len if ok, 0 if out of buffer space)
566 * or error code (< 0, eg. -EINVAL)
568 int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
570 unsigned len = skb->len;
573 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
574 skb = HDLC_Encode(skb, HW_HDR_LEN, 0);
576 skb = iraw_encode(skb, HW_HDR_LEN, 0);
578 dev_err(bcs->cs->dev,
579 "unable to allocate memory for encoding!\n");
583 skb_queue_tail(&bcs->squeue, skb);
584 spin_lock_irqsave(&bcs->cs->lock, flags);
585 if (bcs->cs->connected)
586 tasklet_schedule(&bcs->cs->write_tasklet);
587 spin_unlock_irqrestore(&bcs->cs->lock, flags);
589 return len; /* ok so far */
591 EXPORT_SYMBOL_GPL(gigaset_m10x_send_skb);