xref: /netbsd-src/sys/dev/ir/sir.h (revision 07f928ec11c1a205e1825eac53d8b542f067cdc4)
1 /*	$NetBSD: sir.h,v 1.8 2013/05/27 16:23:20 kiyohara Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) and Tommy Bohlin
9  * (tommy@gatespace.com).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Framing originally written by Tommy Bohlin.
35  */
36 
37 /* Protocol constants */
38 #define SIR_EXTRA_BOF            0xff
39 #define SIR_BOF                  0xc0
40 #define SIR_CE                   0x7d
41 #define SIR_EOF                  0xc1
42 
43 #define SIR_ESC_BIT              0x20
44 
45 enum framefsmstate {
46 	FSTATE_END_OF_FRAME,
47 	FSTATE_START_OF_FRAME,
48 	FSTATE_IN_DATA,
49 	FSTATE_IN_END
50 };
51 
52 enum frameresult {
53 	FR_IDLE,
54 	FR_INPROGRESS,
55 	FR_FRAMEOK,
56 	FR_FRAMEBADFCS,
57 	FR_FRAMEMALFORMED,
58 	FR_BUFFEROVERRUN
59 };
60 
61 struct framestate {
62 	u_int8_t *buffer;
63 	size_t buflen;
64 	size_t bufindex;
65 
66 	enum framefsmstate fsmstate;
67 	u_int escaped;
68 	u_int state_index;
69 };
70 
71 #define deframe_isclear(fs) ((fs)->fsmstate == FSTATE_END_OF_FRAME)
72 
73 int irda_sir_frame(u_int8_t *, u_int, struct uio *, u_int);
74 void deframe_init(struct framestate *, u_int8_t *, size_t);
75 void deframe_clear(struct framestate *);
76 enum frameresult deframe_process(struct framestate *, u_int8_t const **,
77 				 size_t *);
78 
79 /*
80  * CRC computation
81  */
82 #define INITFCS 0xffff
83 #define GOODFCS 0xf0b8
84 
85 extern const u_int16_t irda_fcstab[];
86 
updateFCS(u_int16_t fcs,int c)87 static __inline u_int16_t updateFCS(u_int16_t fcs, int c) {
88 	return (fcs >> 8) ^ irda_fcstab[(fcs^c) & 0xff];
89 }
90 
91 u_int32_t crc_ccitt_16(u_int32_t, u_int8_t const*, size_t);
92