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>,
6 * Stefan Eilers <Eilers.Stefan@epost.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>
19 //#define GIG_M10x_STUFF_VOICE_DATA
21 /* check if byte must be stuffed/escaped
22 * I'm not sure which data should be encoded.
23 * Therefore I will go the hard way and decode every value
24 * less than 0x20, the flag sequence and the control escape char.
26 static inline int muststuff(unsigned char c)
28 if (c < PPP_TRANS) return 1;
29 if (c == PPP_FLAG) return 1;
30 if (c == PPP_ESCAPE) return 1;
31 /* other possible candidates: */
32 /* 0x91: XON with parity set */
33 /* 0x93: XOFF with parity set */
37 /* == data input =========================================================== */
39 /* process a block of received bytes in command mode (modem response)
41 * number of processed bytes
43 static inline int cmd_loop(unsigned char c, unsigned char *src, int numbytes,
44 struct inbuf_t *inbuf)
46 struct cardstate *cs = inbuf->cs;
47 unsigned cbytes = cs->cbytes;
48 int inputstate = inbuf->inputstate;
49 int startbytes = numbytes;
52 cs->respdata[cbytes] = c;
53 if (c == 10 || c == 13) {
54 gig_dbg(DEBUG_TRANSCMD, "%s: End of Command (%d Bytes)",
57 gigaset_handle_modem_response(cs); /* can change
62 !(inputstate & INS_DLE_command)) {
63 inputstate &= ~INS_command;
67 /* advance in line buffer, checking for overflow */
68 if (cbytes < MAX_RESP_SIZE - 1)
71 dev_warn(cs->dev, "response too large\n");
79 (cs->dle || inputstate & INS_DLE_command)) {
80 inputstate |= INS_DLE_char;
86 inbuf->inputstate = inputstate;
88 return startbytes - numbytes;
91 /* process a block of received bytes in lock mode (tty i/f)
93 * number of processed bytes
95 static inline int lock_loop(unsigned char *src, int numbytes,
96 struct inbuf_t *inbuf)
98 struct cardstate *cs = inbuf->cs;
100 gigaset_dbg_buffer(DEBUG_LOCKCMD, "received response",
102 gigaset_if_receive(cs, src, numbytes);
107 /* process a block of received bytes in HDLC data mode
108 * Collect HDLC frames, undoing byte stuffing and watching for DLE escapes.
109 * When a frame is complete, check the FCS and pass valid frames to the LL.
110 * If DLE is encountered, return immediately to let the caller handle it.
112 * number of processed bytes
113 * numbytes (all bytes processed) on error --FIXME
115 static inline int hdlc_loop(unsigned char c, unsigned char *src, int numbytes,
116 struct inbuf_t *inbuf)
118 struct cardstate *cs = inbuf->cs;
119 struct bc_state *bcs = inbuf->bcs;
124 struct sk_buff *compskb;
125 int startbytes = numbytes;
128 IFNULLRETVAL(bcs, numbytes);
129 inputstate = bcs->inputstate;
132 IFNULLRETVAL(skb, numbytes);
134 if (unlikely(inputstate & INS_byte_stuff)) {
135 inputstate &= ~INS_byte_stuff;
139 if (unlikely(c == PPP_ESCAPE)) {
140 if (unlikely(!numbytes)) {
141 inputstate |= INS_byte_stuff;
146 if (unlikely(c == DLE_FLAG &&
148 inbuf->inputstate & INS_DLE_command))) {
149 inbuf->inputstate |= INS_DLE_char;
150 inputstate |= INS_byte_stuff;
155 #ifdef CONFIG_GIGASET_DEBUG
156 if (unlikely(!muststuff(c)))
157 gig_dbg(DEBUG_HDLC, "byte stuffed: 0x%02x", c);
159 } else if (unlikely(c == PPP_FLAG)) {
160 if (unlikely(inputstate & INS_skip_frame)) {
161 if (!(inputstate & INS_have_data)) { /* 7E 7E */
162 #ifdef CONFIG_GIGASET_DEBUG
167 "7e----------------------------");
171 gigaset_rcv_error(NULL, cs, bcs);
172 } else if (!(inputstate & INS_have_data)) { /* 7E 7E */
173 #ifdef CONFIG_GIGASET_DEBUG
179 "7e----------------------------");
184 if (unlikely(fcs != PPP_GOODFCS)) {
186 "Packet checksum at %lu failed, "
187 "packet is corrupted (%u bytes)!\n",
188 bcs->rcvbytes, skb->len);
190 gigaset_rcv_error(compskb, cs, bcs);
193 if (likely((l = skb->len) > 2)) {
199 inputstate |= INS_skip_frame;
202 "invalid packet size (1)!\n");
204 gigaset_rcv_error(NULL,
208 if (likely(!(error ||
211 gigaset_rcv_skb(skb, cs, bcs);
221 inputstate &= ~(INS_have_data | INS_skip_frame);
222 if (unlikely(bcs->ignore)) {
223 inputstate |= INS_skip_frame;
225 } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)) {
226 skb_reserve(skb, HW_HDR_LEN);
229 "could not allocate new skb\n");
230 inputstate |= INS_skip_frame;
234 #ifdef CONFIG_GIGASET_DEBUG
235 } else if (unlikely(muststuff(c))) {
236 /* Should not happen. Possible after ZDLE=1<CR><LF>. */
237 gig_dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
243 #ifdef CONFIG_GIGASET_DEBUG
244 if (unlikely(!(inputstate & INS_have_data))) {
245 gig_dbg(DEBUG_HDLC, "7e (%d x) ================",
251 inputstate |= INS_have_data;
253 if (likely(!(inputstate & INS_skip_frame))) {
254 if (unlikely(skb->len == SBUFSIZE)) {
255 dev_warn(cs->dev, "received packet too long\n");
256 dev_kfree_skb_any(skb);
258 inputstate |= INS_skip_frame;
261 *gigaset_skb_put_quick(skb, 1) = c;
262 /* *__skb_put (skb, 1) = c; */
263 fcs = crc_ccitt_byte(fcs, c);
266 if (unlikely(!numbytes))
270 if (unlikely(c == DLE_FLAG &&
272 inbuf->inputstate & INS_DLE_command))) {
273 inbuf->inputstate |= INS_DLE_char;
277 bcs->inputstate = inputstate;
280 return startbytes - numbytes;
283 /* process a block of received bytes in transparent data mode
284 * Invert bytes, undoing byte stuffing and watching for DLE escapes.
285 * If DLE is encountered, return immediately to let the caller handle it.
287 * number of processed bytes
288 * numbytes (all bytes processed) on error --FIXME
290 static inline int iraw_loop(unsigned char c, unsigned char *src, int numbytes,
291 struct inbuf_t *inbuf)
293 struct cardstate *cs = inbuf->cs;
294 struct bc_state *bcs = inbuf->bcs;
297 int startbytes = numbytes;
299 IFNULLRETVAL(bcs, numbytes);
300 inputstate = bcs->inputstate;
302 IFNULLRETVAL(skb, numbytes);
306 inputstate |= INS_have_data;
308 if (likely(!(inputstate & INS_skip_frame))) {
309 if (unlikely(skb->len == SBUFSIZE)) {
310 //FIXME just pass skb up and allocate a new one
311 dev_warn(cs->dev, "received packet too long\n");
312 dev_kfree_skb_any(skb);
314 inputstate |= INS_skip_frame;
317 *gigaset_skb_put_quick(skb, 1) = gigaset_invtab[c];
320 if (unlikely(!numbytes))
324 if (unlikely(c == DLE_FLAG &&
326 inbuf->inputstate & INS_DLE_command))) {
327 inbuf->inputstate |= INS_DLE_char;
333 if (likely(inputstate & INS_have_data)) {
334 if (likely(!(inputstate & INS_skip_frame))) {
335 gigaset_rcv_skb(skb, cs, bcs);
337 inputstate &= ~(INS_have_data | INS_skip_frame);
338 if (unlikely(bcs->ignore)) {
339 inputstate |= INS_skip_frame;
341 } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN))
343 skb_reserve(skb, HW_HDR_LEN);
345 dev_warn(cs->dev, "could not allocate new skb\n");
346 inputstate |= INS_skip_frame;
350 bcs->inputstate = inputstate;
352 return startbytes - numbytes;
355 /* process a block of data received from the device
357 void gigaset_m10x_input(struct inbuf_t *inbuf)
359 struct cardstate *cs;
360 unsigned tail, head, numbytes;
361 unsigned char *src, c;
364 head = atomic_read(&inbuf->head);
365 tail = atomic_read(&inbuf->tail);
366 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
370 src = inbuf->data + head;
371 numbytes = (head > tail ? RBUFSIZE : tail) - head;
372 gig_dbg(DEBUG_INTR, "processing %u bytes", numbytes);
375 if (atomic_read(&cs->mstate) == MS_LOCKED) {
376 procbytes = lock_loop(src, numbytes, inbuf);
378 numbytes -= procbytes;
382 if (c == DLE_FLAG && (cs->dle ||
383 inbuf->inputstate & INS_DLE_command)) {
384 if (!(inbuf->inputstate & INS_DLE_char)) {
385 inbuf->inputstate |= INS_DLE_char;
388 /* <DLE> <DLE> => <DLE> in data stream */
389 inbuf->inputstate &= ~INS_DLE_char;
392 if (!(inbuf->inputstate & INS_DLE_char)) {
394 /* FIXME use function pointers? */
395 if (inbuf->inputstate & INS_command)
396 procbytes = cmd_loop(c, src, numbytes, inbuf);
397 else if (inbuf->bcs->proto2 == ISDN_PROTO_L2_HDLC)
398 procbytes = hdlc_loop(c, src, numbytes, inbuf);
400 procbytes = iraw_loop(c, src, numbytes, inbuf);
403 numbytes -= procbytes;
404 } else { /* DLE char */
405 inbuf->inputstate &= ~INS_DLE_char;
407 case 'X': /*begin of command*/
408 #ifdef CONFIG_GIGASET_DEBUG
409 if (inbuf->inputstate & INS_command)
411 "received <DLE> 'X' in command mode\n");
414 INS_command | INS_DLE_command;
416 case '.': /*end of command*/
417 #ifdef CONFIG_GIGASET_DEBUG
418 if (!(inbuf->inputstate & INS_command))
420 "received <DLE> '.' in hdlc mode\n");
422 inbuf->inputstate &= cs->dle ?
423 ~(INS_DLE_command|INS_command)
426 //case DLE_FLAG: /*DLE_FLAG in data stream*/ /* schon oben behandelt! */
429 "received 0x10 0x%02x!\n",
431 /* FIXME: reset driver?? */
437 /* end of buffer, check for wrap */
449 gig_dbg(DEBUG_INTR, "setting head to %u", head);
450 atomic_set(&inbuf->head, head);
455 /* == data output ========================================================== */
457 /* Encoding of a PPP packet into an octet stuffed HDLC frame
458 * with FCS, opening and closing flags.
460 * skb skb containing original packet (freed upon return)
461 * head number of headroom bytes to allocate in result skb
462 * tail number of tailroom bytes to allocate in result skb
464 * pointer to newly allocated skb containing the result frame
466 static struct sk_buff *HDLC_Encode(struct sk_buff *skb, int head, int tail)
468 struct sk_buff *hdlc_skb;
473 unsigned int stuf_cnt;
482 fcs = crc_ccitt_byte(fcs, *cp++);
484 fcs ^= 0xffff; /* complement */
486 /* size of new buffer: original size + number of stuffing bytes
487 * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
489 hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + tail + head);
494 skb_reserve(hdlc_skb, head);
496 /* Copy acknowledge request into new skb */
497 memcpy(hdlc_skb->head, skb->head, 2);
499 /* Add flag sequence in front of everything.. */
500 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
502 /* Perform byte stuffing while copying data. */
504 if (muststuff(*skb->data)) {
505 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
506 *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
508 *(skb_put(hdlc_skb, 1)) = *skb->data++;
511 /* Finally add FCS (byte stuffed) and flag sequence */
512 c = (fcs & 0x00ff); /* least significant byte first */
514 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
517 *(skb_put(hdlc_skb, 1)) = c;
519 c = ((fcs >> 8) & 0x00ff);
521 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
524 *(skb_put(hdlc_skb, 1)) = c;
526 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
532 /* Encoding of a raw packet into an octet stuffed bit inverted frame
534 * skb skb containing original packet (freed upon return)
535 * head number of headroom bytes to allocate in result skb
536 * tail number of tailroom bytes to allocate in result skb
538 * pointer to newly allocated skb containing the result frame
540 static struct sk_buff *iraw_encode(struct sk_buff *skb, int head, int tail)
542 struct sk_buff *iraw_skb;
547 /* worst case: every byte must be stuffed */
548 iraw_skb = dev_alloc_skb(2*skb->len + tail + head);
553 skb_reserve(iraw_skb, head);
558 c = gigaset_invtab[*cp++];
560 *(skb_put(iraw_skb, 1)) = c;
561 *(skb_put(iraw_skb, 1)) = c;
568 * called by common.c to queue an skb for sending
569 * and start transmission if necessary
571 * B Channel control structure
574 * number of bytes accepted for sending
575 * (skb->len if ok, 0 if out of buffer space)
576 * or error code (< 0, eg. -EINVAL)
578 int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
582 IFNULLRETVAL(bcs, -EFAULT);
583 IFNULLRETVAL(skb, -EFAULT);
586 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
587 skb = HDLC_Encode(skb, HW_HDR_LEN, 0);
589 skb = iraw_encode(skb, HW_HDR_LEN, 0);
591 dev_err(bcs->cs->dev,
592 "unable to allocate memory for encoding!\n");
596 skb_queue_tail(&bcs->squeue, skb);
597 tasklet_schedule(&bcs->cs->write_tasklet);
599 return len; /* ok so far */