Module Name:    src
Committed By:   kiyohara
Date:           Mon May 27 16:23:20 UTC 2013

Modified Files:
        src/sys/dev/ir: sir.c sir.h
        src/sys/dev/usb: ustir.c

Log Message:
Split IrDA SIR part for other SIR device.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/ir/sir.c
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/ir/sir.h
cvs rdiff -u -r1.32 -r1.33 src/sys/dev/usb/ustir.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/ir/sir.c
diff -u src/sys/dev/ir/sir.c:1.5 src/sys/dev/ir/sir.c:1.6
--- src/sys/dev/ir/sir.c:1.5	Mon Apr 28 20:23:51 2008
+++ src/sys/dev/ir/sir.c	Mon May 27 16:23:20 2013
@@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: sir.c,v 1.5 
 /*
  * 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 @@ const u_int16_t irda_fcstab[] = {
 	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 @@ const u_int16_t irda_fcstab[] = {
 #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 @@ irda_sir_frame(u_int8_t *obuf, u_int max
 	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;
+}

Index: src/sys/dev/ir/sir.h
diff -u src/sys/dev/ir/sir.h:1.7 src/sys/dev/ir/sir.h:1.8
--- src/sys/dev/ir/sir.h:1.7	Mon Apr 28 20:23:51 2008
+++ src/sys/dev/ir/sir.h	Mon May 27 16:23:20 2013
@@ -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 @@ extern const u_int16_t irda_fcstab[];
 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);

Index: src/sys/dev/usb/ustir.c
diff -u src/sys/dev/usb/ustir.c:1.32 src/sys/dev/usb/ustir.c:1.33
--- src/sys/dev/usb/ustir.c:1.32	Tue Mar  6 03:35:30 2012
+++ src/sys/dev/usb/ustir.c	Mon May 27 16:23:20 2013
@@ -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 @@ Static struct ustir_speedrec const ustir
 	{ 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 {
-	FR_IDLE,
-	FR_INPROGRESS,
-	FR_FRAMEOK,
-	FR_FRAMEBADFCS,
-	FR_FRAMEMALFORMED,
-	FR_BUFFEROVERRUN
-};
-
-struct framestate {
-	struct framedefn const *definition;
-
-	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)
-
-Static void deframe_clear(struct framestate *);
-Static void deframe_init(struct framestate *, struct framedefn const *,
-			 u_int8_t *, size_t);
-Static enum frameresult deframe_process(struct framestate *, u_int8_t const **,
-					size_t *);
-
 struct ustir_softc {
 	device_t		sc_dev;
 	usbd_device_handle	sc_udev;
@@ -230,17 +167,6 @@ Static usbd_status ustir_start_read(stru
 Static void ustir_periodic(struct ustir_softc *);
 Static void ustir_thread(void *);
 
-Static u_int32_t
-crc_ccitt_16(u_int32_t crcinit, u_int8_t const *buf, size_t blen)
-{
-	while (blen-- > 0) {
-		u_int8_t chr;
-		chr = *buf++;
-		crcinit = updateFCS(crcinit, chr);
-	}
-	return crcinit;
-}
-
 static usbd_status
 ustir_read_reg(struct ustir_softc *sc, unsigned int reg, u_int8_t *data)
 {
@@ -435,130 +361,6 @@ ustir_detach(device_t self, int flags)
 	return rv;
 }
 
-Static void
-deframe_clear(struct framestate *fstate)
-{
-	fstate->bufindex = 0;
-	fstate->fsmstate = FSTATE_END_OF_FRAME;
-	fstate->escaped = 0;
-}
-
-Static void
-deframe_init(struct framestate *fstate, struct framedefn const *definition,
-	     u_int8_t *buf, size_t buflen)
-{
-	fstate->definition = definition;
-	fstate->buffer = buf;
-	fstate->buflen = buflen;
-
-	deframe_clear(fstate);
-}
-
-Static enum frameresult
-deframe_process(struct framestate *fstate, u_int8_t const **bptr, size_t *blen)
-{
-	struct framedefn const *definition;
-	u_int8_t const *cptr;
-	u_int8_t escchr;
-	size_t ibuflen, obufindex, obuflen;
-	enum framefsmstate fsmstate;
-	enum frameresult result;
-
-	cptr = *bptr;
-	fsmstate = fstate->fsmstate;
-	definition = fstate->definition;
-	escchr = definition->esc_byte;
-	obufindex = fstate->bufindex;
-	obuflen = fstate->buflen;
-	ibuflen = *blen;
-
-	while (ibuflen-- > 0) {
-		u_int8_t chr;
-
-		chr = *cptr++;
-
-		if (fstate->escaped) {
-			fstate->escaped = 0;
-			chr ^= definition->esc_xor;
-		} else if (chr == escchr) {
-			fstate->escaped = 1;
-			continue;
-		}
-
-		switch (fsmstate) {
-		case FSTATE_IN_DATA:
-			if (chr == definition->eof_byte) {
-				fsmstate = FSTATE_IN_END;
-				fstate->state_index = definition->eof_count;
-				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) {
-				u_int32_t crc;
-				size_t fcslen;
-
-				fsmstate = FSTATE_END_OF_FRAME;
-
-				fcslen = definition->fcs_count;
-
-				if (obufindex < fcslen) {
-					result = FR_FRAMEMALFORMED;
-					goto complete;
-				}
-
-				crc = definition->
-					fcs_calc(definition->fcs_init,
-						 fstate->buffer, obufindex);
-
-				/* Remove check bytes from buffer length */
-				obufindex -= fcslen;
-
-				if (crc == definition->fcs_correct)
-					result = FR_FRAMEOK;
-				else
-					result = FR_FRAMEBADFCS;
-
-				goto complete;
-			}
-			break;
-
-		case FSTATE_END_OF_FRAME:
-			if (chr != definition->bof_byte)
-				break;
-
-			fsmstate = FSTATE_START_OF_FRAME;
-			fstate->state_index = definition->bof_count;
-			/* 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;
-}
-
 /* Returns 0 if more data required, 1 if a complete frame was extracted */
 static int
 deframe_rd_ur(struct ustir_softc *sc)
@@ -934,8 +736,7 @@ ustir_open(void *h, int flag, int mode,
 	sc->sc_params.ebofs = 0;
 	sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
 
-	deframe_init(&sc->sc_framestate, &framedef_sir, sc->sc_ur_buf,
-		     IRDA_MAX_FRAME_SIZE);
+	deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
 
 	/* Increment reference for thread */
 	sc->sc_refcnt++;

Reply via email to