Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/dev Split IrDA SIR part for other SIR device.
details: https://anonhg.NetBSD.org/src/rev/50d6476ad20f
branches: trunk
changeset: 786978:50d6476ad20f
user: kiyohara <kiyohara%NetBSD.org@localhost>
date: Mon May 27 16:23:20 2013 +0000
description:
Split IrDA SIR part for other SIR device.
diffstat:
sys/dev/ir/sir.c | 144 ++++++++++++++++++++++++++++++++++--
sys/dev/ir/sir.h | 36 ++++++++-
sys/dev/usb/ustir.c | 205 +---------------------------------------------------
3 files changed, 174 insertions(+), 211 deletions(-)
diffs (truncated from 490 to 300 lines):
diff -r b574eeb1ee33 -r 50d6476ad20f sys/dev/ir/sir.c
--- a/sys/dev/ir/sir.c Mon May 27 07:37:20 2013 +0000
+++ b/sys/dev/ir/sir.c Mon May 27 16:23:20 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sir.c,v 1.5 2008/04/28 20:23:51 martin Exp $ */
+/* $NetBSD: sir.c,v 1.6 2013/05/27 16:23:20 kiyohara Exp $ */
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sir.c,v 1.5 2008/04/28 20:23:51 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sir.c,v 1.6 2013/05/27 16:23:20 kiyohara Exp $");
#include <sys/param.h>
#include <sys/conf.h>
@@ -46,7 +46,7 @@
/*
* CRC computation table
*/
-const u_int16_t irda_fcstab[] = {
+const uint16_t irda_fcstab[] = {
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
@@ -81,7 +81,6 @@
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
};
-
#define MAX_IRDA_FRAME 5000 /* XXX what is it? */
#define PUTC(c) if (p < end) *p++ = (c)
@@ -96,15 +95,15 @@
#define CHUNK 512
int
-irda_sir_frame(u_int8_t *obuf, u_int maxlen, struct uio *uio, u_int ebofs)
+irda_sir_frame(uint8_t *obuf, u_int maxlen, struct uio *uio, u_int ebofs)
{
- u_int8_t ibuf[CHUNK];
- u_int8_t *p, *end, *cp;
+ uint8_t ibuf[CHUNK];
+ uint8_t *p, *end, *cp;
size_t n;
int error;
int i;
int c;
- u_int16_t ofcs;
+ uint16_t ofcs;
p = obuf;
end = p + maxlen;
@@ -142,3 +141,132 @@
else
return (-EINVAL);
}
+
+void
+deframe_init(struct framestate *fstate, uint8_t *buf, size_t buflen)
+{
+
+ fstate->buffer = buf;
+ fstate->buflen = buflen;
+
+ deframe_clear(fstate);
+}
+
+void
+deframe_clear(struct framestate *fstate)
+{
+
+ fstate->bufindex = 0;
+ fstate->fsmstate = FSTATE_END_OF_FRAME;
+ fstate->escaped = 0;
+}
+
+enum frameresult
+deframe_process(struct framestate *fstate, uint8_t const **bptr, size_t *blen)
+{
+ uint8_t const *cptr;
+ size_t ibuflen, obufindex, obuflen;
+ enum framefsmstate fsmstate;
+ enum frameresult result;
+
+ cptr = *bptr;
+ fsmstate = fstate->fsmstate;
+ obufindex = fstate->bufindex;
+ obuflen = fstate->buflen;
+ ibuflen = *blen;
+
+ while (ibuflen-- > 0) {
+ uint8_t chr;
+
+ chr = *cptr++;
+
+ if (fstate->escaped) {
+ fstate->escaped = 0;
+ chr ^= SIR_ESC_BIT;
+ } else if (chr == SIR_CE) {
+ fstate->escaped = 1;
+ continue;
+ }
+
+ switch (fsmstate) {
+ case FSTATE_IN_DATA:
+ if (chr == SIR_EOF) {
+ fsmstate = FSTATE_IN_END;
+ fstate->state_index = 1;
+ goto state_in_end;
+ }
+ if (obufindex >= obuflen) {
+ result = FR_BUFFEROVERRUN;
+ fsmstate = FSTATE_END_OF_FRAME;
+ goto complete;
+ }
+ fstate->buffer[obufindex++] = chr;
+ break;
+
+ state_in_end:
+ /* FALLTHROUGH */
+
+ case FSTATE_IN_END:
+ if (--fstate->state_index == 0) {
+ uint32_t crc;
+ const size_t fcslen = 2;
+
+ fsmstate = FSTATE_END_OF_FRAME;
+
+ if (obufindex < fcslen) {
+ result = FR_FRAMEMALFORMED;
+ goto complete;
+ }
+
+ crc = crc_ccitt_16(INITFCS, fstate->buffer,
+ obufindex);
+
+ /* Remove check bytes from buffer length */
+ obufindex -= fcslen;
+
+ if (crc == GOODFCS)
+ result = FR_FRAMEOK;
+ else
+ result = FR_FRAMEBADFCS;
+
+ goto complete;
+ }
+ break;
+
+ case FSTATE_END_OF_FRAME:
+ if (chr != SIR_BOF)
+ break;
+
+ fsmstate = FSTATE_START_OF_FRAME;
+ fstate->state_index = 1;
+ /* FALLTHROUGH */
+ case FSTATE_START_OF_FRAME:
+ if (--fstate->state_index == 0) {
+ fsmstate = FSTATE_IN_DATA;
+ obufindex = 0;
+ }
+ break;
+ }
+ }
+
+ result = (fsmstate == FSTATE_END_OF_FRAME) ? FR_IDLE : FR_INPROGRESS;
+
+ complete:
+ fstate->bufindex = obufindex;
+ fstate->fsmstate = fsmstate;
+ *blen = ibuflen;
+
+ return result;
+}
+
+uint32_t
+crc_ccitt_16(uint32_t crcinit, uint8_t const *buf, size_t blen)
+{
+
+ while (blen-- > 0) {
+ uint8_t chr;
+ chr = *buf++;
+ crcinit = updateFCS(crcinit, chr);
+ }
+ return crcinit;
+}
diff -r b574eeb1ee33 -r 50d6476ad20f sys/dev/ir/sir.h
--- a/sys/dev/ir/sir.h Mon May 27 07:37:20 2013 +0000
+++ b/sys/dev/ir/sir.h Mon May 27 16:23:20 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sir.h,v 1.7 2008/04/28 20:23:51 martin Exp $ */
+/* $NetBSD: sir.h,v 1.8 2013/05/27 16:23:20 kiyohara Exp $ */
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -42,7 +42,39 @@
#define SIR_ESC_BIT 0x20
+enum framefsmstate {
+ FSTATE_END_OF_FRAME,
+ FSTATE_START_OF_FRAME,
+ FSTATE_IN_DATA,
+ FSTATE_IN_END
+};
+
+enum frameresult {
+ FR_IDLE,
+ FR_INPROGRESS,
+ FR_FRAMEOK,
+ FR_FRAMEBADFCS,
+ FR_FRAMEMALFORMED,
+ FR_BUFFEROVERRUN
+};
+
+struct framestate {
+ u_int8_t *buffer;
+ size_t buflen;
+ size_t bufindex;
+
+ enum framefsmstate fsmstate;
+ u_int escaped;
+ u_int state_index;
+};
+
+#define deframe_isclear(fs) ((fs)->fsmstate == FSTATE_END_OF_FRAME)
+
int irda_sir_frame(u_int8_t *, u_int, struct uio *, u_int);
+void deframe_init(struct framestate *, u_int8_t *, size_t);
+void deframe_clear(struct framestate *);
+enum frameresult deframe_process(struct framestate *, u_int8_t const **,
+ size_t *);
/*
* CRC computation
@@ -55,3 +87,5 @@
static __inline u_int16_t updateFCS(u_int16_t fcs, int c) {
return (fcs >> 8) ^ irda_fcstab[(fcs^c) & 0xff];
}
+
+u_int32_t crc_ccitt_16(u_int32_t, u_int8_t const*, size_t);
diff -r b574eeb1ee33 -r 50d6476ad20f sys/dev/usb/ustir.c
--- a/sys/dev/usb/ustir.c Mon May 27 07:37:20 2013 +0000
+++ b/sys/dev/usb/ustir.c Mon May 27 16:23:20 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ustir.c,v 1.32 2012/03/06 03:35:30 mrg Exp $ */
+/* $NetBSD: ustir.c,v 1.33 2013/05/27 16:23:20 kiyohara Exp $ */
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.32 2012/03/06 03:35:30 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.33 2013/05/27 16:23:20 kiyohara Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -88,69 +88,6 @@
{ 2400, STIR_BRMODE_2400 }
};
-struct framedefn {
- unsigned int bof_count;
- u_int8_t bof_byte;
-
- u_int8_t esc_byte;
- u_int8_t esc_xor;
-
- unsigned int eof_count;
- u_int8_t eof_byte;
-
- unsigned int fcs_count;
- u_int32_t fcs_init;
- u_int32_t fcs_correct;
-
- u_int32_t (*fcs_calc)(u_int32_t, u_int8_t const*, size_t);
-};
-
-Static u_int32_t crc_ccitt_16(u_int32_t, u_int8_t const*, size_t);
-
-struct framedefn const framedef_sir = {
- 1, 0xc0,
- 0x7d, 0x20,
- 1, 0xc1,
- 2, INITFCS, GOODFCS,
- crc_ccitt_16
-};
-
-enum framefsmstate {
- FSTATE_END_OF_FRAME,
- FSTATE_START_OF_FRAME,
- FSTATE_IN_DATA,
- FSTATE_IN_END
-};
-
-enum frameresult {
Home |
Main Index |
Thread Index |
Old Index