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 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 warn("response too large");
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)))
158 "byte stuffed: 0x%02x", c);
160 } else if (unlikely(c == PPP_FLAG)) {
161 if (unlikely(inputstate & INS_skip_frame)) {
162 if (!(inputstate & INS_have_data)) { /* 7E 7E */
163 //dbg(DEBUG_HDLC, "(7e)7e------------------------");
164 #ifdef CONFIG_GIGASET_DEBUG
169 "7e----------------------------");
173 gigaset_rcv_error(NULL, cs, bcs);
174 } else if (!(inputstate & INS_have_data)) { /* 7E 7E */
175 //dbg(DEBUG_HDLC, "(7e)7e------------------------");
176 #ifdef CONFIG_GIGASET_DEBUG
182 "7e----------------------------");
187 if (unlikely(fcs != PPP_GOODFCS)) {
188 err("Packet checksum at %lu failed, "
189 "packet is corrupted (%u bytes)!",
190 bcs->rcvbytes, skb->len);
192 gigaset_rcv_error(compskb, cs, bcs);
195 if (likely((l = skb->len) > 2)) {
201 inputstate |= INS_skip_frame;
203 err("invalid packet size (1)!");
205 gigaset_rcv_error(NULL, cs, bcs);
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);
228 warn("could not allocate new skb");
229 inputstate |= INS_skip_frame;
233 #ifdef CONFIG_GIGASET_DEBUG
234 } else if (unlikely(muststuff(c))) {
235 /* Should not happen. Possible after ZDLE=1<CR><LF>. */
236 dbg(DEBUG_HDLC, "not byte stuffed: 0x%02x", c);
242 #ifdef CONFIG_GIGASET_DEBUG
243 if (unlikely(!(inputstate & INS_have_data))) {
245 "7e (%d x) ================", bcs->emptycount);
250 inputstate |= INS_have_data;
252 if (likely(!(inputstate & INS_skip_frame))) {
253 if (unlikely(skb->len == SBUFSIZE)) {
254 warn("received packet too long");
255 dev_kfree_skb_any(skb);
257 inputstate |= INS_skip_frame;
260 *gigaset_skb_put_quick(skb, 1) = c;
261 /* *__skb_put (skb, 1) = c; */
262 fcs = crc_ccitt_byte(fcs, c);
265 if (unlikely(!numbytes))
269 if (unlikely(c == DLE_FLAG &&
271 inbuf->inputstate & INS_DLE_command))) {
272 inbuf->inputstate |= INS_DLE_char;
276 bcs->inputstate = inputstate;
279 return startbytes - numbytes;
282 /* process a block of received bytes in transparent data mode
283 * Invert bytes, undoing byte stuffing and watching for DLE escapes.
284 * If DLE is encountered, return immediately to let the caller handle it.
286 * number of processed bytes
287 * numbytes (all bytes processed) on error --FIXME
289 static inline int iraw_loop(unsigned char c, unsigned char *src, int numbytes,
290 struct inbuf_t *inbuf)
292 struct cardstate *cs = inbuf->cs;
293 struct bc_state *bcs = inbuf->bcs;
296 int startbytes = numbytes;
298 IFNULLRETVAL(bcs, numbytes);
299 inputstate = bcs->inputstate;
301 IFNULLRETVAL(skb, numbytes);
305 inputstate |= INS_have_data;
307 if (likely(!(inputstate & INS_skip_frame))) {
308 if (unlikely(skb->len == SBUFSIZE)) {
309 //FIXME just pass skb up and allocate a new one
310 warn("received packet too long");
311 dev_kfree_skb_any(skb);
313 inputstate |= INS_skip_frame;
316 *gigaset_skb_put_quick(skb, 1) = gigaset_invtab[c];
319 if (unlikely(!numbytes))
323 if (unlikely(c == DLE_FLAG &&
325 inbuf->inputstate & INS_DLE_command))) {
326 inbuf->inputstate |= INS_DLE_char;
332 if (likely(inputstate & INS_have_data)) {
333 if (likely(!(inputstate & INS_skip_frame))) {
334 gigaset_rcv_skb(skb, cs, bcs);
336 inputstate &= ~(INS_have_data | INS_skip_frame);
337 if (unlikely(bcs->ignore)) {
338 inputstate |= INS_skip_frame;
340 } else if (likely((skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN))
342 skb_reserve(skb, HW_HDR_LEN);
344 warn("could not allocate new skb");
345 inputstate |= INS_skip_frame;
349 bcs->inputstate = inputstate;
351 return startbytes - numbytes;
354 /* process a block of data received from the device
356 void gigaset_m10x_input(struct inbuf_t *inbuf)
358 struct cardstate *cs;
359 unsigned tail, head, numbytes;
360 unsigned char *src, c;
363 head = atomic_read(&inbuf->head);
364 tail = atomic_read(&inbuf->tail);
365 dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
369 src = inbuf->data + head;
370 numbytes = (head > tail ? RBUFSIZE : tail) - head;
371 dbg(DEBUG_INTR, "processing %u bytes", numbytes);
374 if (atomic_read(&cs->mstate) == MS_LOCKED) {
375 procbytes = lock_loop(src, numbytes, inbuf);
377 numbytes -= procbytes;
381 if (c == DLE_FLAG && (cs->dle ||
382 inbuf->inputstate & INS_DLE_command)) {
383 if (!(inbuf->inputstate & INS_DLE_char)) {
384 inbuf->inputstate |= INS_DLE_char;
387 /* <DLE> <DLE> => <DLE> in data stream */
388 inbuf->inputstate &= ~INS_DLE_char;
391 if (!(inbuf->inputstate & INS_DLE_char)) {
393 /* FIXME use function pointers? */
394 if (inbuf->inputstate & INS_command)
395 procbytes = cmd_loop(c, src, numbytes, inbuf);
396 else if (inbuf->bcs->proto2 == ISDN_PROTO_L2_HDLC)
397 procbytes = hdlc_loop(c, src, numbytes, inbuf);
399 procbytes = iraw_loop(c, src, numbytes, inbuf);
402 numbytes -= procbytes;
403 } else { /* DLE-char */
404 inbuf->inputstate &= ~INS_DLE_char;
406 case 'X': /*begin of command*/
407 #ifdef CONFIG_GIGASET_DEBUG
408 if (inbuf->inputstate & INS_command)
409 err("received <DLE> 'X' in command mode");
412 INS_command | INS_DLE_command;
414 case '.': /*end of command*/
415 #ifdef CONFIG_GIGASET_DEBUG
416 if (!(inbuf->inputstate & INS_command))
417 err("received <DLE> '.' in hdlc mode");
419 inbuf->inputstate &= cs->dle ?
420 ~(INS_DLE_command|INS_command)
423 //case DLE_FLAG: /*DLE_FLAG in data stream*/ /* schon oben behandelt! */
425 err("received 0x10 0x%02x!", (int) c);
426 /* FIXME: reset driver?? */
432 /* end of buffer, check for wrap */
444 dbg(DEBUG_INTR, "setting head to %u", head);
445 atomic_set(&inbuf->head, head);
450 /* == data output ========================================================== */
452 /* Encoding of a PPP packet into an octet stuffed HDLC frame
453 * with FCS, opening and closing flags.
455 * skb skb containing original packet (freed upon return)
456 * head number of headroom bytes to allocate in result skb
457 * tail number of tailroom bytes to allocate in result skb
459 * pointer to newly allocated skb containing the result frame
461 static struct sk_buff *HDLC_Encode(struct sk_buff *skb, int head, int tail)
463 struct sk_buff *hdlc_skb;
468 unsigned int stuf_cnt;
477 fcs = crc_ccitt_byte(fcs, *cp++);
479 fcs ^= 0xffff; /* complement */
481 /* size of new buffer: original size + number of stuffing bytes
482 * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
484 hdlc_skb = dev_alloc_skb(skb->len + stuf_cnt + 6 + tail + head);
486 err("unable to allocate memory for HDLC encoding!");
490 skb_reserve(hdlc_skb, head);
492 /* Copy acknowledge request into new skb */
493 memcpy(hdlc_skb->head, skb->head, 2);
495 /* Add flag sequence in front of everything.. */
496 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
498 /* Perform byte stuffing while copying data. */
500 if (muststuff(*skb->data)) {
501 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
502 *(skb_put(hdlc_skb, 1)) = (*skb->data++) ^ PPP_TRANS;
504 *(skb_put(hdlc_skb, 1)) = *skb->data++;
507 /* Finally add FCS (byte stuffed) and flag sequence */
508 c = (fcs & 0x00ff); /* least significant byte first */
510 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
513 *(skb_put(hdlc_skb, 1)) = c;
515 c = ((fcs >> 8) & 0x00ff);
517 *(skb_put(hdlc_skb, 1)) = PPP_ESCAPE;
520 *(skb_put(hdlc_skb, 1)) = c;
522 *(skb_put(hdlc_skb, 1)) = PPP_FLAG;
528 /* Encoding of a raw packet into an octet stuffed bit inverted frame
530 * skb skb containing original packet (freed upon return)
531 * head number of headroom bytes to allocate in result skb
532 * tail number of tailroom bytes to allocate in result skb
534 * pointer to newly allocated skb containing the result frame
536 static struct sk_buff *iraw_encode(struct sk_buff *skb, int head, int tail)
538 struct sk_buff *iraw_skb;
543 /* worst case: every byte must be stuffed */
544 iraw_skb = dev_alloc_skb(2*skb->len + tail + head);
546 err("unable to allocate memory for HDLC encoding!");
550 skb_reserve(iraw_skb, head);
555 c = gigaset_invtab[*cp++];
557 *(skb_put(iraw_skb, 1)) = c;
558 *(skb_put(iraw_skb, 1)) = c;
565 * called by common.c to queue an skb for sending
566 * and start transmission if necessary
568 * B Channel control structure
571 * number of bytes accepted for sending
572 * (skb->len if ok, 0 if out of buffer space)
573 * or error code (< 0, eg. -EINVAL)
575 int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb)
579 IFNULLRETVAL(bcs, -EFAULT);
580 IFNULLRETVAL(skb, -EFAULT);
583 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
584 skb = HDLC_Encode(skb, HW_HDR_LEN, 0);
586 skb = iraw_encode(skb, HW_HDR_LEN, 0);
590 skb_queue_tail(&bcs->squeue, skb);
591 tasklet_schedule(&bcs->cs->write_tasklet);
593 return len; /* ok so far */