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 /* check if byte must be stuffed/escaped
21 * I'm not sure which data should be encoded.
22 * Therefore I will go the hard way and decode every value
23 * less than 0x20, the flag sequence and the control escape char.
25 static inline int muststuff(unsigned char c
)
27 if (c
< PPP_TRANS
) return 1;
28 if (c
== PPP_FLAG
) return 1;
29 if (c
== PPP_ESCAPE
) return 1;
30 /* other possible candidates: */
31 /* 0x91: XON with parity set */
32 /* 0x93: XOFF with parity set */
36 /* == data input =========================================================== */
38 /* process a block of received bytes in command mode (modem response)
40 * number of processed bytes
42 static inline int cmd_loop(unsigned char c
, unsigned char *src
, int numbytes
,
43 struct inbuf_t
*inbuf
)
45 struct cardstate
*cs
= inbuf
->cs
;
46 unsigned cbytes
= cs
->cbytes
;
47 int inputstate
= inbuf
->inputstate
;
48 int startbytes
= numbytes
;
51 cs
->respdata
[cbytes
] = c
;
52 if (c
== 10 || c
== 13) {
53 gig_dbg(DEBUG_TRANSCMD
, "%s: End of Command (%d Bytes)",
56 gigaset_handle_modem_response(cs
); /* can change
61 !(inputstate
& INS_DLE_command
)) {
62 inputstate
&= ~INS_command
;
66 /* advance in line buffer, checking for overflow */
67 if (cbytes
< MAX_RESP_SIZE
- 1)
70 dev_warn(cs
->dev
, "response too large\n");
78 (cs
->dle
|| inputstate
& INS_DLE_command
)) {
79 inputstate
|= INS_DLE_char
;
85 inbuf
->inputstate
= inputstate
;
87 return startbytes
- numbytes
;
90 /* process a block of received bytes in lock mode (tty i/f)
92 * number of processed bytes
94 static inline int lock_loop(unsigned char *src
, int numbytes
,
95 struct inbuf_t
*inbuf
)
97 struct cardstate
*cs
= inbuf
->cs
;
99 gigaset_dbg_buffer(DEBUG_LOCKCMD
, "received response",
101 gigaset_if_receive(cs
, src
, numbytes
);
106 /* process a block of received bytes in HDLC data mode
107 * Collect HDLC frames, undoing byte stuffing and watching for DLE escapes.
108 * When a frame is complete, check the FCS and pass valid frames to the LL.
109 * If DLE is encountered, return immediately to let the caller handle it.
111 * number of processed bytes
112 * numbytes (all bytes processed) on error --FIXME
114 static inline int hdlc_loop(unsigned char c
, unsigned char *src
, int numbytes
,
115 struct inbuf_t
*inbuf
)
117 struct cardstate
*cs
= inbuf
->cs
;
118 struct bc_state
*bcs
= inbuf
->bcs
;
119 int inputstate
= bcs
->inputstate
;
120 __u16 fcs
= bcs
->fcs
;
121 struct sk_buff
*skb
= bcs
->skb
;
123 struct sk_buff
*compskb
;
124 int startbytes
= numbytes
;
127 if (unlikely(inputstate
& INS_byte_stuff
)) {
128 inputstate
&= ~INS_byte_stuff
;
132 if (unlikely(c
== PPP_ESCAPE
)) {
133 if (unlikely(!numbytes
)) {
134 inputstate
|= INS_byte_stuff
;
139 if (unlikely(c
== DLE_FLAG
&&
141 inbuf
->inputstate
& INS_DLE_command
))) {
142 inbuf
->inputstate
|= INS_DLE_char
;
143 inputstate
|= INS_byte_stuff
;
148 if (unlikely(!muststuff(c
)))
149 gig_dbg(DEBUG_HDLC
, "byte stuffed: 0x%02x", c
);
150 } else if (unlikely(c
== PPP_FLAG
)) {
151 if (unlikely(inputstate
& INS_skip_frame
)) {
152 #ifdef CONFIG_GIGASET_DEBUG
153 if (!(inputstate
& INS_have_data
)) { /* 7E 7E */
157 "7e----------------------------");
162 gigaset_rcv_error(NULL
, cs
, bcs
);
163 } else if (!(inputstate
& INS_have_data
)) { /* 7E 7E */
164 #ifdef CONFIG_GIGASET_DEBUG
170 "7e----------------------------");
175 if (unlikely(fcs
!= PPP_GOODFCS
)) {
177 "Packet checksum at %lu failed, "
178 "packet is corrupted (%u bytes)!\n",
179 bcs
->rcvbytes
, skb
->len
);
181 gigaset_rcv_error(compskb
, cs
, bcs
);
184 if (likely((l
= skb
->len
) > 2)) {
190 inputstate
|= INS_skip_frame
;
193 "invalid packet size (1)!\n");
195 gigaset_rcv_error(NULL
,
199 if (likely(!(error
||
202 gigaset_rcv_skb(skb
, cs
, bcs
);
212 inputstate
&= ~(INS_have_data
| INS_skip_frame
);
213 if (unlikely(bcs
->ignore
)) {
214 inputstate
|= INS_skip_frame
;
216 } else if (likely((skb
= dev_alloc_skb(SBUFSIZE
+ HW_HDR_LEN
)) != NULL
)) {
217 skb_reserve(skb
, HW_HDR_LEN
);
220 "could not allocate new skb\n");
221 inputstate
|= INS_skip_frame
;
225 } else if (unlikely(muststuff(c
))) {
226 /* Should not happen. Possible after ZDLE=1<CR><LF>. */
227 gig_dbg(DEBUG_HDLC
, "not byte stuffed: 0x%02x", c
);
232 #ifdef CONFIG_GIGASET_DEBUG
233 if (unlikely(!(inputstate
& INS_have_data
))) {
234 gig_dbg(DEBUG_HDLC
, "7e (%d x) ================",
240 inputstate
|= INS_have_data
;
242 if (likely(!(inputstate
& INS_skip_frame
))) {
243 if (unlikely(skb
->len
== SBUFSIZE
)) {
244 dev_warn(cs
->dev
, "received packet too long\n");
245 dev_kfree_skb_any(skb
);
247 inputstate
|= INS_skip_frame
;
250 *__skb_put(skb
, 1) = c
;
251 fcs
= crc_ccitt_byte(fcs
, c
);
254 if (unlikely(!numbytes
))
258 if (unlikely(c
== DLE_FLAG
&&
260 inbuf
->inputstate
& INS_DLE_command
))) {
261 inbuf
->inputstate
|= INS_DLE_char
;
265 bcs
->inputstate
= inputstate
;
268 return startbytes
- numbytes
;
271 /* process a block of received bytes in transparent data mode
272 * Invert bytes, undoing byte stuffing and watching for DLE escapes.
273 * If DLE is encountered, return immediately to let the caller handle it.
275 * number of processed bytes
276 * numbytes (all bytes processed) on error --FIXME
278 static inline int iraw_loop(unsigned char c
, unsigned char *src
, int numbytes
,
279 struct inbuf_t
*inbuf
)
281 struct cardstate
*cs
= inbuf
->cs
;
282 struct bc_state
*bcs
= inbuf
->bcs
;
283 int inputstate
= bcs
->inputstate
;
284 struct sk_buff
*skb
= bcs
->skb
;
285 int startbytes
= numbytes
;
289 inputstate
|= INS_have_data
;
291 if (likely(!(inputstate
& INS_skip_frame
))) {
292 if (unlikely(skb
->len
== SBUFSIZE
)) {
293 //FIXME just pass skb up and allocate a new one
294 dev_warn(cs
->dev
, "received packet too long\n");
295 dev_kfree_skb_any(skb
);
297 inputstate
|= INS_skip_frame
;
300 *__skb_put(skb
, 1) = bitrev8(c
);
303 if (unlikely(!numbytes
))
307 if (unlikely(c
== DLE_FLAG
&&
309 inbuf
->inputstate
& INS_DLE_command
))) {
310 inbuf
->inputstate
|= INS_DLE_char
;
316 if (likely(inputstate
& INS_have_data
)) {
317 if (likely(!(inputstate
& INS_skip_frame
))) {
318 gigaset_rcv_skb(skb
, cs
, bcs
);
320 inputstate
&= ~(INS_have_data
| INS_skip_frame
);
321 if (unlikely(bcs
->ignore
)) {
322 inputstate
|= INS_skip_frame
;
324 } else if (likely((skb
= dev_alloc_skb(SBUFSIZE
+ HW_HDR_LEN
))
326 skb_reserve(skb
, HW_HDR_LEN
);
328 dev_warn(cs
->dev
, "could not allocate new skb\n");
329 inputstate
|= INS_skip_frame
;
333 bcs
->inputstate
= inputstate
;
335 return startbytes
- numbytes
;
338 /* process a block of data received from the device
340 void gigaset_m10x_input(struct inbuf_t
*inbuf
)
342 struct cardstate
*cs
;
343 unsigned tail
, head
, numbytes
;
344 unsigned char *src
, c
;
349 gig_dbg(DEBUG_INTR
, "buffer state: %u -> %u", head
, tail
);
353 src
= inbuf
->data
+ head
;
354 numbytes
= (head
> tail
? RBUFSIZE
: tail
) - head
;
355 gig_dbg(DEBUG_INTR
, "processing %u bytes", numbytes
);
358 if (cs
->mstate
== MS_LOCKED
) {
359 procbytes
= lock_loop(src
, numbytes
, inbuf
);
361 numbytes
-= procbytes
;
365 if (c
== DLE_FLAG
&& (cs
->dle
||
366 inbuf
->inputstate
& INS_DLE_command
)) {
367 if (!(inbuf
->inputstate
& INS_DLE_char
)) {
368 inbuf
->inputstate
|= INS_DLE_char
;
371 /* <DLE> <DLE> => <DLE> in data stream */
372 inbuf
->inputstate
&= ~INS_DLE_char
;
375 if (!(inbuf
->inputstate
& INS_DLE_char
)) {
377 /* FIXME use function pointers? */
378 if (inbuf
->inputstate
& INS_command
)
379 procbytes
= cmd_loop(c
, src
, numbytes
, inbuf
);
380 else if (inbuf
->bcs
->proto2
== ISDN_PROTO_L2_HDLC
)
381 procbytes
= hdlc_loop(c
, src
, numbytes
, inbuf
);
383 procbytes
= iraw_loop(c
, src
, numbytes
, inbuf
);
386 numbytes
-= procbytes
;
387 } else { /* DLE char */
388 inbuf
->inputstate
&= ~INS_DLE_char
;
390 case 'X': /*begin of command*/
391 if (inbuf
->inputstate
& INS_command
)
393 "received <DLE> 'X' in command mode\n");
395 INS_command
| INS_DLE_command
;
397 case '.': /*end of command*/
398 if (!(inbuf
->inputstate
& INS_command
))
400 "received <DLE> '.' in hdlc mode\n");
401 inbuf
->inputstate
&= cs
->dle
?
402 ~(INS_DLE_command
|INS_command
)
405 //case DLE_FLAG: /*DLE_FLAG in data stream*/ /* schon oben behandelt! */
408 "received 0x10 0x%02x!\n",
410 /* FIXME: reset driver?? */
416 /* end of buffer, check for wrap */
428 gig_dbg(DEBUG_INTR
, "setting head to %u", head
);
432 EXPORT_SYMBOL_GPL(gigaset_m10x_input
);
435 /* == data output ========================================================== */
437 /* Encoding of a PPP packet into an octet stuffed HDLC frame
438 * with FCS, opening and closing flags.
440 * skb skb containing original packet (freed upon return)
441 * head number of headroom bytes to allocate in result skb
442 * tail number of tailroom bytes to allocate in result skb
444 * pointer to newly allocated skb containing the result frame
446 static struct sk_buff
*HDLC_Encode(struct sk_buff
*skb
, int head
, int tail
)
448 struct sk_buff
*hdlc_skb
;
453 unsigned int stuf_cnt
;
462 fcs
= crc_ccitt_byte(fcs
, *cp
++);
464 fcs
^= 0xffff; /* complement */
466 /* size of new buffer: original size + number of stuffing bytes
467 * + 2 bytes FCS + 2 stuffing bytes for FCS (if needed) + 2 flag bytes
469 hdlc_skb
= dev_alloc_skb(skb
->len
+ stuf_cnt
+ 6 + tail
+ head
);
474 skb_reserve(hdlc_skb
, head
);
476 /* Copy acknowledge request into new skb */
477 memcpy(hdlc_skb
->head
, skb
->head
, 2);
479 /* Add flag sequence in front of everything.. */
480 *(skb_put(hdlc_skb
, 1)) = PPP_FLAG
;
482 /* Perform byte stuffing while copying data. */
484 if (muststuff(*skb
->data
)) {
485 *(skb_put(hdlc_skb
, 1)) = PPP_ESCAPE
;
486 *(skb_put(hdlc_skb
, 1)) = (*skb
->data
++) ^ PPP_TRANS
;
488 *(skb_put(hdlc_skb
, 1)) = *skb
->data
++;
491 /* Finally add FCS (byte stuffed) and flag sequence */
492 c
= (fcs
& 0x00ff); /* least significant byte first */
494 *(skb_put(hdlc_skb
, 1)) = PPP_ESCAPE
;
497 *(skb_put(hdlc_skb
, 1)) = c
;
499 c
= ((fcs
>> 8) & 0x00ff);
501 *(skb_put(hdlc_skb
, 1)) = PPP_ESCAPE
;
504 *(skb_put(hdlc_skb
, 1)) = c
;
506 *(skb_put(hdlc_skb
, 1)) = PPP_FLAG
;
512 /* Encoding of a raw packet into an octet stuffed bit inverted frame
514 * skb skb containing original packet (freed upon return)
515 * head number of headroom bytes to allocate in result skb
516 * tail number of tailroom bytes to allocate in result skb
518 * pointer to newly allocated skb containing the result frame
520 static struct sk_buff
*iraw_encode(struct sk_buff
*skb
, int head
, int tail
)
522 struct sk_buff
*iraw_skb
;
527 /* worst case: every byte must be stuffed */
528 iraw_skb
= dev_alloc_skb(2*skb
->len
+ tail
+ head
);
533 skb_reserve(iraw_skb
, head
);
540 *(skb_put(iraw_skb
, 1)) = c
;
541 *(skb_put(iraw_skb
, 1)) = c
;
548 * called by common.c to queue an skb for sending
549 * and start transmission if necessary
551 * B Channel control structure
554 * number of bytes accepted for sending
555 * (skb->len if ok, 0 if out of buffer space)
556 * or error code (< 0, eg. -EINVAL)
558 int gigaset_m10x_send_skb(struct bc_state
*bcs
, struct sk_buff
*skb
)
560 unsigned len
= skb
->len
;
563 if (bcs
->proto2
== ISDN_PROTO_L2_HDLC
)
564 skb
= HDLC_Encode(skb
, HW_HDR_LEN
, 0);
566 skb
= iraw_encode(skb
, HW_HDR_LEN
, 0);
568 dev_err(bcs
->cs
->dev
,
569 "unable to allocate memory for encoding!\n");
573 skb_queue_tail(&bcs
->squeue
, skb
);
574 spin_lock_irqsave(&bcs
->cs
->lock
, flags
);
575 if (bcs
->cs
->connected
)
576 tasklet_schedule(&bcs
->cs
->write_tasklet
);
577 spin_unlock_irqrestore(&bcs
->cs
->lock
, flags
);
579 return len
; /* ok so far */
581 EXPORT_SYMBOL_GPL(gigaset_m10x_send_skb
);