Index: m5602_v4l2.c
===================================================================
--- m5602_v4l2.c	(revision 403)
+++ m5602_v4l2.c	(working copy)
@@ -126,6 +126,7 @@
 {
 	struct m5602_camera *cam = urb->context;
 	struct m5602_frame_t *f;
+	u8 current_frame_num = 0;
 	unsigned long lock_flags;
 	int i, err = 0;
 
@@ -188,16 +189,30 @@
 		if (len < 12)
 			continue;
 
-		/* Frame delimiter: ff xx xx xx ff ff */
-		if (pos[0] == 0xff && pos[4] == 0xff && pos[5] == 0xff) {
-			PDEBUG(DBG_DATA, "Frame delimiter detected");
+		/* Frame delimiter: ff bf xx yy ff ff, xx and yy is frame number */
+		if (pos[0] == 0xff && pos[1] == 0xbf && pos[2] == pos[3] && pos[4] == 0xff && pos[5] == 0xff) {
 
+			current_frame_num = pos[2];
+			PDEBUG(DBG_DATA, "Frame delimiter %d detected ",current_frame_num);
+			
 			/* Remove the extra fluff appended on each header */
 			pos += 6;
 			len -= 6;
 
 			switch (f->state) {
 			case FRAME_GRABBING:
+				
+				/* test for damaged frames */
+				if (current_frame_num == f->framenum) {	
+					if ((f->buf.bytesused + len) <=
+						(M5602_DEFAULT_FRAME_WIDTH *
+						M5602_DEFAULT_FRAME_HEIGHT)) {
+							memcpy(f->rawdata + f->buf.bytesused,pos, len);
+							f->buf.bytesused += len;
+						}
+					break;
+				}
+				
 				f->buf.bytesused =
 					cam->sensor->current_fmt.sizeimage;
 				do_gettimeofday(&f->buf.timestamp);
@@ -206,11 +221,13 @@
 				PDEBUG(DBG_V4L2, "Frame %d captured, contains "
 						 "%d bytes", f->framenum,
 						 f->buf.bytesused);
-
+				
 				spin_lock_irqsave(&cam->queue_lock, lock_flags);
 
 				/* Add the current frame to the outqueue */
+				
 				list_move_tail(&f->frame, &cam->outqueue);
+				
 
 				if (list_empty(&cam->inqueue)) {
 					cam->frame_current = NULL;
@@ -229,16 +246,17 @@
 				wake_up_interruptible(&cam->wait_frame);
 
 				/* Start with the new frame */
-				PDEBUG(DBG_DATA, "Starting frame %d, "
-					"copying %d bytes",
-					f->framenum, len);
 
 				memcpy(f->rawdata, pos, len);
-				f->buf.sequence = ++cam->frame_count;
+				f->buf.sequence = current_frame_num; 
 				f->framenum = f->buf.sequence;
 				f->buf.bytesused = len;
 				f->state = FRAME_GRABBING;
 
+				PDEBUG(DBG_DATA, "Starting frame %d, "
+						"copying %d bytes",
+	  					f->framenum, len);
+				
 				cam->frame_current = f;
 				break;
 
@@ -248,7 +266,7 @@
 				       "copying %d bytes", f->framenum, len);
 
 				memcpy(f->rawdata, pos, len);
-				f->buf.sequence  = ++cam->frame_count;
+				f->buf.sequence = current_frame_num; 
 				f->framenum = f->buf.sequence;
 				f->buf.bytesused = len;
 				f->state = FRAME_GRABBING;
@@ -522,7 +540,7 @@
 	cam->stream = STREAM_OFF;
 	cam->nbuffers = 0;
 	cam->frame_count = 0;
-
+	
 	m5602_empty_framequeues(cam);
 
 	PDEBUG(DBG_V4L2, "Video device /dev/video%d is open", cam->vdev->minor);
Index: m5602_bridge.h
===================================================================
--- m5602_bridge.h	(revision 403)
+++ m5602_bridge.h	(working copy)
@@ -165,7 +165,7 @@
 	struct list_head frame;
 
 	unsigned char *rawdata;
-	int framenum;
+	u8 framenum;
 	struct v4l2_buffer buf;
 	enum m5602_frame_state state;
 	unsigned long vma_use_count;
