# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher.
# This patch includes the following deltas:
#	           ChangeSet	1.476   -> 1.477  
#	drivers/usb/media/konicawc.c	1.6     -> 1.7    
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 02/08/12	spse@secret.org.uk	1.477
# [PATCH] misc fixes for konicawc driver
# 
# This patch against 2.5.31 fixes some problems in the konicawc driver
# 
# - add new video mode 160x120
# - add konicawc_camera_on/off()
# - initially send out URBs in status,data order
# - fix konicawc_isoc_irq to always resubmit status then data URBs
# - tidy up debug messages
# - make konicawc_compress_iso look for frame start on initial image
# --------------------------------------------
#
diff -Nru a/drivers/usb/media/konicawc.c b/drivers/usb/media/konicawc.c
--- a/drivers/usb/media/konicawc.c	Mon Aug 12 10:57:29 2002
+++ b/drivers/usb/media/konicawc.c	Mon Aug 12 10:57:29 2002
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * konicawc.c - konica webcam driver
  *
  * Author: Simon Evans <spse@secret.org.uk>
@@ -8,7 +6,7 @@
  * Copyright (C) 2002 Simon Evans
  *
  * Licence: GPL
- * 
+ *
  * Driver for USB webcams based on Konica chipset. This
  * chipset is used in Intel YC76 camera.
  *
@@ -18,6 +16,7 @@
 #include <linux/module.h>
 #include <linux/init.h>
 
+
 #include "usbvideo.h"
 
 #define MAX_BRIGHTNESS	108
@@ -26,9 +25,11 @@
 #define MAX_SHARPNESS	108
 #define MAX_WHITEBAL	372
 #define MAX_SPEED	6
+
+
 #define MAX_CAMERAS	1
 
-#define DRIVER_VERSION	"v1.1"
+#define DRIVER_VERSION	"v1.3"
 #define DRIVER_DESC	"Konica Webcam driver"
 
 enum ctrl_req {
@@ -41,18 +42,32 @@
 
 
 enum frame_sizes {
-	SIZE_160X136	= 0,
-	SIZE_176X144	= 1,
-	SIZE_320X240	= 2,
+	SIZE_160X120	= 0,
+	SIZE_160X136	= 1,
+	SIZE_176X144	= 2,
+	SIZE_320X240	= 3,
+	
 };
 
+#define MAX_FRAME_SIZE	SIZE_320X240
 
 static usbvideo_t *cams;
 
+#ifdef CONFIG_USB_DEBUG
+static int debug;
+#define DEBUG(n, format, arg...) \
+	if (n <= debug) {	 \
+		printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
+	}
+#else
+#define DEBUG(n, arg...)
+static const int debug = 0;
+#endif
+
+
 /* Some default values for inital camera settings,
    can be set by modprobe */
 
-static int debug;
 static enum frame_sizes size;	
 static int speed = 6;		/* Speed (fps) 0 (slowest) to 6 (fastest) */
 static int brightness =	MAX_BRIGHTNESS/2;
@@ -61,31 +76,36 @@
 static int sharpness =	MAX_SHARPNESS/2;
 static int whitebal =	3*(MAX_WHITEBAL/4);
 
-static int speed_to_interface[] = { 1, 0, 3, 2, 4, 5, 6 };
+static int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
 
 /* These FPS speeds are from the windows config box. They are
  * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
  * real fps.
  */
 
-static int speed_to_fps[3][7] = { { 24, 40, 48, 60, 72, 80, 100 },
-				  { 18, 30, 36, 45, 54, 60, 75  },
-				  { 6,  10, 12, 15, 18, 20, 25  } };
-
-
-static int camera_sizes[][2] = { { 160, 136 },
-				 { 176, 144 },
-				 { 320, 240 },
-				 { } /* List terminator */
+static int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
+			       { 24, 40, 48, 60, 72, 80, 100 },
+			       { 18, 30, 36, 45, 54, 60, 75  },
+			       { 6,  10, 12, 15, 18, 21, 25  } };
+
+struct cam_size {
+	u16	width;
+	u16	height;
+	u8	cmd;
 };
 
+static struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
+					  { 160, 136, 0xa },
+					  { 176, 144, 0x4 },
+					  { 320, 240, 0x5 } };
+
 struct konicawc {
 	u8 brightness;		/* camera uses 0 - 9, x11 for real value */
 	u8 contrast;		/* as above */
 	u8 saturation;		/* as above */
 	u8 sharpness;		/* as above */
 	u8 white_bal;		/* 0 - 33, x11 for real value */
-	u8 speed;		/* Stored as 0 - 6, used as index in speed_to_* (above) */
+	u8 speed;		/* Stored as 0 - 6, used as index in spd_to_* (above) */
 	u8 size;		/* Frame Size */
 	int height;
 	int width;
@@ -93,6 +113,10 @@
 	u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
 	struct urb *last_data_urb;
 	int lastframe;
+	int cur_frame_size;	/* number of bytes in current frame size */
+	int maxline;		/* number of lines per frame */
+	int yplanesz;		/* Number of bytes in the Y plane */
+	unsigned int buttonsts:1;
 };
 
 
@@ -110,42 +134,56 @@
 }
 
 
+static inline void konicawc_camera_on(uvd_t *uvd)
+{
+        DEBUG(0, "camera on");
+        konicawc_set_misc(uvd, 0x2, 1, 0x0b);
+}
+
+
+static inline void konicawc_camera_off(uvd_t *uvd)
+{
+        DEBUG(0, "camera off");
+        konicawc_set_misc(uvd, 0x2, 0, 0x0b);
+}
+
+
+static void konicawc_set_camera_size(uvd_t *uvd)
+{
+	struct konicawc *cam = (struct konicawc *)uvd->user_data;
+
+	konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
+	cam->width = camera_sizes[cam->size].width;
+	cam->height = camera_sizes[cam->size].height;
+	cam->yplanesz = cam->height * cam->width;
+	cam->cur_frame_size = (cam->yplanesz * 3) / 2;
+	cam->maxline = cam->yplanesz / 256;
+	uvd->videosize = VIDEOSIZE(cam->width, cam->height);
+}
+
+
 static int konicawc_setup_on_open(uvd_t *uvd)
 {
 	struct konicawc *cam = (struct konicawc *)uvd->user_data;
 
-	konicawc_set_misc(uvd, 0x2, 0, 0x0b);
-	dbg("setting brightness to %d (%d)", cam->brightness,
+	DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
 	    cam->brightness * 11);
 	konicawc_set_value(uvd, cam->brightness, SetBrightness);
-	dbg("setting white balance to %d (%d)", cam->white_bal,
+	DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
 	    cam->white_bal * 11);
 	konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
-	dbg("setting contrast to %d (%d)", cam->contrast,
+	DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
 	    cam->contrast * 11);
 	konicawc_set_value(uvd, cam->contrast, SetContrast);
-	dbg("setting saturation to %d (%d)", cam->saturation,
+	DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
 	    cam->saturation * 11);
 	konicawc_set_value(uvd, cam->saturation, SetSaturation);
-	dbg("setting sharpness to %d (%d)", cam->sharpness,
+	DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
 	    cam->sharpness * 11);
 	konicawc_set_value(uvd, cam->sharpness, SetSharpness);
-	dbg("setting size %d", cam->size);
-	switch(cam->size) {
-	case 0:
-		konicawc_set_misc(uvd, 0x2, 0xa, 0x08);
-		break;
-
-	case 1:
-		konicawc_set_misc(uvd, 0x2, 4, 0x08);
-		break;
-
-	case 2:
-		konicawc_set_misc(uvd, 0x2, 5, 0x08);
-		break;
-	}
-	konicawc_set_misc(uvd, 0x2, 1, 0x0b);
-	cam->lastframe = -1;
+	konicawc_set_camera_size(uvd);
+	cam->lastframe = -2;
+	cam->buttonsts = 0;
 	return 0;
 }
 
@@ -154,23 +192,25 @@
 {
 	struct konicawc *cam = (struct konicawc *)uvd->user_data;
 
-	dbg("new brightness: %d", uvd->vpic.brightness);
+	konicawc_camera_off(uvd);
+	DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
 	uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
 	if(cam->brightness != uvd->vpic.brightness / 11) {
 	   cam->brightness = uvd->vpic.brightness / 11;
-	   dbg("setting brightness to %d (%d)", cam->brightness,
+	   DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
 	       cam->brightness * 11);
 	   konicawc_set_value(uvd, cam->brightness, SetBrightness);
 	}
 
-	dbg("new contrast: %d", uvd->vpic.contrast);
+	DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
 	uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
 	if(cam->contrast != uvd->vpic.contrast / 11) {
 		cam->contrast = uvd->vpic.contrast / 11;
-		dbg("setting contrast to %d (%d)", cam->contrast,
+		DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
 		    cam->contrast * 11);
 		konicawc_set_value(uvd, cam->contrast, SetContrast);
 	}
+	konicawc_camera_on(uvd);
 }
 
 
@@ -180,21 +220,20 @@
 	int i, totlen = 0;
 	unsigned char *status = stsurb->transfer_buffer;
 	int keep = 0, discard = 0, bad = 0;
-	static int buttonsts = 0;
+	struct konicawc *cam = (struct konicawc *)uvd->user_data;
 
 	for (i = 0; i < dataurb->number_of_packets; i++) {
-		int button = buttonsts;
+		int button = cam->buttonsts;
 		unsigned char sts;
 		int n = dataurb->iso_frame_desc[i].actual_length;
 		int st = dataurb->iso_frame_desc[i].status;
-		cdata = dataurb->transfer_buffer + 
+		cdata = dataurb->transfer_buffer +
 			dataurb->iso_frame_desc[i].offset;
 
 		/* Detect and ignore errored packets */
 		if (st < 0) {
-			if (debug >= 1)
-				err("Data error: packet=%d. len=%d. status=%d.",
-				    i, n, st);
+			DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
+			      i, n, st);
 			uvd->stats.iso_err_count++;
 			continue;
 		}
@@ -210,8 +249,8 @@
 
 		/* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
 		 * otherwise:
-		 * bit 0 0:drop packet (padding data)
-		 *	 1 keep packet
+		 * bit 0 0: keep packet
+		 *	 1: drop packet (padding data)
 		 *
 		 * bit 4 0 button not clicked
 		 *       1 button clicked
@@ -225,10 +264,10 @@
 		
 		/* work out the button status, but dont do
 		   anything with it for now */
-		   
-		if(button != buttonsts) {
-			dbg("button: %sclicked", button ? "" : "un");
-			buttonsts = button;
+
+		if(button != cam->buttonsts) {
+			DEBUG(2, "button: %sclicked", button ? "" : "un");
+			cam->buttonsts = button;
 		}
 
 		if(sts == 0x01) { /* drop frame */
@@ -241,34 +280,52 @@
 			bad++;
 			continue;
 		}
+		if(!sts && cam->lastframe == -2) {
+			DEBUG(2, "dropping frame looking for image start");
+			continue;
+		}
 
 		keep++;
-		if(*(status+i) & 0x80) { /* frame start */
+		if(sts & 0x80) { /* frame start */
 			unsigned char marker[] = { 0, 0xff, 0, 0x00 };
-			if(debug > 1)
-				dbg("Adding Marker packet = %d, frame = %2.2x",
-				    i, *(status+i));
-			marker[3] = *(status+i) - 0x80;
-			RingQueue_Enqueue(&uvd->dp, marker, 4);			
+
+			if(cam->lastframe == -2) {
+				DEBUG(2, "found initial image");
+				cam->lastframe = -1;
+			}
+				
+			marker[3] = sts & 0x7F;
+			RingQueue_Enqueue(&uvd->dp, marker, 4);
 			totlen += 4;
 		}
+
 		totlen += n;	/* Little local accounting */
-		if(debug > 5)
-			dbg("Adding packet %d, bytes = %d", i, n);
 		RingQueue_Enqueue(&uvd->dp, cdata, n);
-
 	}
-	if(debug > 8) {
-		dbg("finished: keep = %d discard = %d bad = %d added %d bytes",
+	DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
 		    keep, discard, bad, totlen);
-	}
 	return totlen;
 }
 
 
+static void resubmit_urb(uvd_t *uvd, struct urb *urb)
+{
+        int i, ret;
+        for (i = 0; i < FRAMES_PER_DESC; i++) {
+                urb->iso_frame_desc[i].status = 0;
+        }
+        urb->dev = uvd->dev;
+        urb->status = 0;
+        ret = usb_submit_urb(urb, GFP_KERNEL);
+	DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
+        if(ret)
+                err("usb_submit_urb error (%d)", ret);
+
+}
+
+
 static void konicawc_isoc_irq(struct urb *urb)
 {
-	int i, ret, len = 0;
 	uvd_t *uvd = urb->context;
 	struct konicawc *cam = (struct konicawc *)uvd->user_data;
 
@@ -277,42 +334,35 @@
 		return;
 
 	if (!uvd->streaming) {
-		if (debug >= 1)
-			info("Not streaming, but interrupt!");
+		DEBUG(1, "Not streaming, but interrupt!");
 		return;
 	}
 
-	if (urb->actual_length > 32) {
-		cam->last_data_urb = urb;
-		goto urb_done_with;
-	}
+	DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
 
 	uvd->stats.urb_count++;
-	if (urb->actual_length <= 0)
-		goto urb_done_with;
 
+	if (urb->transfer_buffer_length > 32) {
+		cam->last_data_urb = urb;
+		return;
+	}
 	/* Copy the data received into ring queue */
 	if(cam->last_data_urb) {
-		len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
-		for (i = 0; i < FRAMES_PER_DESC; i++) {
-			cam->last_data_urb->iso_frame_desc[i].status = 0;
-		}
+		int len = 0;
+		if(urb->start_frame != cam->last_data_urb->start_frame)
+			err("Lost sync on frames");
+		else if (!urb->status && !cam->last_data_urb->status)
+			len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
+
+		resubmit_urb(uvd, urb);
+		resubmit_urb(uvd, cam->last_data_urb);
 		cam->last_data_urb = NULL;
+		uvd->stats.urb_length = len;
+		uvd->stats.data_count += len;
+		if(len)
+			RingQueue_WakeUpInterruptible(&uvd->dp);
+		return;
 	}
-	uvd->stats.urb_length = len;
-	uvd->stats.data_count += len;
-	if(len)
-		RingQueue_WakeUpInterruptible(&uvd->dp);
-
-urb_done_with:
-	for (i = 0; i < FRAMES_PER_DESC; i++) {
-		urb->iso_frame_desc[i].status = 0;
-	}
-	urb->dev = uvd->dev;
-	urb->status = 0;
-	ret = usb_submit_urb(urb, GFP_KERNEL);
-	if(ret)
-		err("usb_submit_urb error (%d)", ret);
 	return;
 }
 
@@ -322,13 +372,18 @@
 	struct usb_device *dev = uvd->dev;
 	int i, errFlag;
 	struct konicawc *cam = (struct konicawc *)uvd->user_data;
+	int pktsz;
+	struct usb_interface_descriptor *interface;
 
+	interface = &dev->actconfig->interface[uvd->iface].altsetting[spd_to_iface[cam->speed]];
+	pktsz = interface->endpoint[1].wMaxPacketSize;
+	DEBUG(1, "pktsz = %d", pktsz);
 	if (!CAMERA_IS_OPERATIONAL(uvd)) {
 		err("Camera is not operational");
 		return -EFAULT;
 	}
 	uvd->curframe = -1;
-
+	konicawc_camera_on(uvd);
 	/* Alternate interface 1 is is the biggest frame size */
 	i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
 	if (i < 0) {
@@ -349,10 +404,10 @@
 		urb->transfer_buffer = uvd->sbuf[i].data;
 		urb->complete = konicawc_isoc_irq;
 		urb->number_of_packets = FRAMES_PER_DESC;
-		urb->transfer_buffer_length = uvd->iso_packet_len * FRAMES_PER_DESC;
-		for (j=k=0; j < FRAMES_PER_DESC; j++, k += uvd->iso_packet_len) {
+		urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
+		for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
 			urb->iso_frame_desc[j].offset = k;
-			urb->iso_frame_desc[j].length = uvd->iso_packet_len;
+			urb->iso_frame_desc[j].length = pktsz;
 		}
 
 		urb = cam->sts_urb[i];
@@ -375,18 +430,17 @@
 	
 	/* Submit all URBs */
 	for (i=0; i < USBVIDEO_NUMSBUF; i++) {
-		errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
-		if (errFlag)
-			err ("usb_submit_isoc(%d) ret %d", i, errFlag);
-
 		errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
 		if (errFlag)
 			err("usb_submit_isoc(%d) ret %d", i, errFlag);
+
+		errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
+		if (errFlag)
+			err ("usb_submit_isoc(%d) ret %d", i, errFlag);
 	}
 
 	uvd->streaming = 1;
-	if (debug > 1)
-		dbg("streaming=1 video_endp=$%02x", uvd->video_endp);
+	DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
 	return 0;
 }
 
@@ -399,6 +453,8 @@
 	if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
 		return;
 
+	konicawc_camera_off(uvd);
+	uvd->streaming = 0;
 	cam = (struct konicawc *)uvd->user_data;
 	cam->last_data_urb = NULL;
 
@@ -413,8 +469,6 @@
 			err("usb_unlink_urb() error %d.", j);
 	}
 
-	uvd->streaming = 0;
-
 	if (!uvd->remove_pending) {
 		/* Set packet size to 0 */
 		j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
@@ -428,42 +482,33 @@
 
 static void konicawc_process_isoc(uvd_t *uvd, usbvideo_frame_t *frame)
 {	
-	int n;
-	int maxline, yplanesz;
 	struct konicawc *cam = (struct konicawc *)uvd->user_data;
-	assert(uvd != NULL);
+	int maxline = cam->maxline;
+	int yplanesz = cam->yplanesz;
+
 	assert(frame != NULL);
 
-	maxline = (cam->height * cam->width * 3) / (2 * 384);
-	yplanesz = cam->height * cam->width;
-	if(debug > 5)
-		dbg("maxline = %d yplanesz = %d", maxline, yplanesz);
-	
-	if(debug > 3)
-		dbg("Frame state = %d", frame->scanstate);
+	DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
+	DEBUG(3, "Frame state = %d", frame->scanstate);
 
 	if(frame->scanstate == ScanState_Scanning) {
 		int drop = 0;
 		int curframe;
 		int fdrops = 0;
-		if(debug > 3)
-			dbg("Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
+		DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
 		while(RingQueue_GetLength(&uvd->dp) >= 4) {
 			if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
 			    (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
 			    (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
 			    (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
 				curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
-				if(cam->lastframe != -1) {
-					if(curframe < cam->lastframe) {
-						fdrops = (curframe + 0x80) - cam->lastframe;
-					} else {
-						fdrops = curframe - cam->lastframe;
-					}
+				if(cam->lastframe >= 0) {
+					fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
 					fdrops--;
-					if(fdrops)
+					if(fdrops) {
 						info("Dropped %d frames (%d -> %d)", fdrops,
 						     cam->lastframe, curframe);
+					}
 				}
 				cam->lastframe = curframe;
 				frame->curline = 0;
@@ -474,18 +519,20 @@
 			RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
 			drop++;
 		}
+		if(drop)
+			DEBUG(2, "dropped %d bytes looking for new frame", drop);
 	}
 
 	if(frame->scanstate == ScanState_Scanning)
 		return;
 		
-	/* Try to move data from queue into frame buffer 
+	/* Try to move data from queue into frame buffer
 	 * We get data in blocks of 384 bytes made up of:
 	 * 256 Y, 64 U, 64 V.
 	 * This needs to be written out as a Y plane, a U plane and a V plane.
 	 */
 		
-	while ( frame->curline < maxline && (n = RingQueue_GetLength(&uvd->dp)) >= 384) {
+	while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
 		/* Y */
 		RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
 		/* U */
@@ -497,8 +544,7 @@
 	}
 	/* See if we filled the frame */
 	if (frame->curline == maxline) {
-		if(debug > 5)
-			dbg("got whole frame");
+		DEBUG(5, "got whole frame");
 
 		frame->frameState = FrameState_Done_Hold;
 		frame->curline = 0;
@@ -510,10 +556,8 @@
 
 static int konicawc_calculate_fps(uvd_t *uvd)
 {
-	struct konicawc *t = uvd->user_data;
-	dbg("fps = %d", speed_to_fps[t->size][t->speed]/3);
-
-	return speed_to_fps[t->size][t->speed]/3;
+	struct konicawc *cam = uvd->user_data;
+	return spd_to_fps[cam->size][cam->speed]/3;
 }
 
 
@@ -550,10 +594,10 @@
 	uvd->vcap.type = VID_TYPE_CAPTURE;
 	uvd->vcap.channels = 1;
 	uvd->vcap.audios = 0;
-	uvd->vcap.minwidth = camera_sizes[cam->size][0];
-	uvd->vcap.minheight = camera_sizes[cam->size][1];
-	uvd->vcap.maxwidth = camera_sizes[cam->size][0];
-	uvd->vcap.maxheight = camera_sizes[cam->size][1];
+	uvd->vcap.minwidth = camera_sizes[cam->size].width;
+	uvd->vcap.minheight = camera_sizes[cam->size].height;
+	uvd->vcap.maxwidth = camera_sizes[cam->size].width;
+	uvd->vcap.maxheight = camera_sizes[cam->size].height;
 
 	memset(&uvd->vchan, 0, sizeof(uvd->vchan));
 	uvd->vchan.flags = 0 ;
@@ -563,15 +607,14 @@
 	strcpy(uvd->vchan.name, "Camera");
 
 	/* Talk to device */
-	dbg("device init");
+	DEBUG(1, "device init");
 	if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
-		dbg("3,10 -> %2.2x %2.2x", buf[0], buf[1]);
+		DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
 	if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
-		dbg("3,10 -> %2.2x %2.2x", buf[0], buf[1]);
+		DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
 	if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
-		dbg("2,0,d failed");
-	dbg("setting initial values");
-
+		DEBUG(2, "2,0,d failed");
+	DEBUG(1, "setting initial values");
 }
 
 
@@ -582,8 +625,7 @@
 	int actInterface=-1, inactInterface=-1, maxPS=0;
 	unsigned char video_ep = 0;
 	
-	if (debug >= 1)
-		dbg("konicawc_probe(%p,%u.)", dev, ifnum);
+	DEBUG(1, "konicawc_probe(%p,%u.)", dev, ifnum);
 
 	/* We don't handle multi-config cameras */
 	if (dev->descriptor.bNumConfigurations != 1)
@@ -594,10 +636,8 @@
 
 	/* Validate found interface: must have one ISO endpoint */
 	nas = dev->actconfig->interface[ifnum].num_altsetting;
-	if (debug > 0)
-		info("Number of alternate settings=%d.", nas);
-	if (nas < 8) {
-		err("Too few alternate settings for this camera!");
+	if (nas != 8) {
+		err("Incorrect number of alternate settings (%d) for this camera!", nas);
 		return NULL;
 	}
 	/* Validate all alternate settings */
@@ -612,7 +652,7 @@
 			return NULL;
 		}
 		endpoint = &interface->endpoint[1];
-		dbg("found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
+		DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
 		    endpoint->bEndpointAddress, endpoint->wMaxPacketSize);
 		if (video_ep == 0)
 			video_ep = endpoint->bEndpointAddress;
@@ -636,22 +676,20 @@
 				return NULL;
 			}
 		} else {
-			if (i == speed_to_interface[speed]) {
+			if (i == spd_to_iface[speed]) {
 				/* This one is the requested one */
 				actInterface = i;
-				maxPS = endpoint->wMaxPacketSize;
-				if (debug > 0) {
-					info("Selecting requested active setting=%d. maxPS=%d.",
-					     i, maxPS);
-				}
 			}
 		}
+		if(endpoint->wMaxPacketSize > maxPS)
+			maxPS = endpoint->wMaxPacketSize;
 	}
 	if(actInterface == -1) {
 		err("Cant find required endpoint");
 		return NULL;
 	}
 
+	DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
 
 	/* Code below may sleep, need to lock module while we are here */
 	MOD_INC_USE_COUNT;
@@ -670,26 +708,10 @@
 			}
 		}
 		cam->speed = speed;
-		switch(size) {
-		case SIZE_160X136:
-		default:
-			cam->height = 136;
-			cam->width = 160;
-			cam->size = SIZE_160X136;
-			break;
-
-		case SIZE_176X144:
-			cam->height = 144;
-			cam->width = 176;
-			cam->size = SIZE_176X144;
-			break;
-
-		case SIZE_320X240:
-			cam->height = 240;
-			cam->width = 320;
-			cam->size = SIZE_320X240;
-			break;
-		}
+		RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
+		cam->width = camera_sizes[size].width;
+		cam->height = camera_sizes[size].height;
+		cam->size = size;
 
 		uvd->flags = 0;
 		uvd->debug = debug;
@@ -773,9 +795,9 @@
 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
 MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_PARM(speed, "i");
-MODULE_PARM_DESC(speed, "FPS speed: 0 (slowest) - 6 (fastest)");
+MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
 MODULE_PARM(size, "i");
-MODULE_PARM_DESC(size, "Frame Size 0: 160x136 1: 176x144 2: 320x240");
+MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
 MODULE_PARM(brightness, "i");
 MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
 MODULE_PARM(contrast, "i");
@@ -786,7 +808,11 @@
 MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
 MODULE_PARM(whitebal, "i");
 MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
+
+#ifdef CONFIG_USB_DEBUG
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
+#endif
+
 module_init(konicawc_init);
 module_exit(konicawc_cleanup);
