1/*
2 * konicawc.c - konica webcam driver
3 *
4 * Author: Simon Evans <spse@secret.org.uk>
5 *
6 * Copyright (C) 2002 Simon Evans
7 *
8 * Licence: GPL
9 *
10 * Driver for USB webcams based on Konica chipset. This
11 * chipset is used in Intel YC76 camera.
12 *
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/input.h>
19
20#include "usbvideo.h"
21
22#define MAX_BRIGHTNESS	108
23#define MAX_CONTRAST	108
24#define MAX_SATURATION	108
25#define MAX_SHARPNESS	108
26#define MAX_WHITEBAL	372
27#define MAX_SPEED	6
28
29
30#define MAX_CAMERAS	1
31
32#define DRIVER_VERSION	"v1.4"
33#define DRIVER_DESC	"Konica Webcam driver"
34
35enum ctrl_req {
36	SetWhitebal	= 0x01,
37	SetBrightness	= 0x02,
38        SetSharpness	= 0x03,
39	SetContrast	= 0x04,
40	SetSaturation	= 0x05,
41};
42
43
44enum frame_sizes {
45	SIZE_160X120	= 0,
46	SIZE_160X136	= 1,
47	SIZE_176X144	= 2,
48	SIZE_320X240	= 3,
49
50};
51
52#define MAX_FRAME_SIZE	SIZE_320X240
53
54static struct usbvideo *cams;
55
56#ifdef CONFIG_USB_DEBUG
57static int debug;
58#define DEBUG(n, format, arg...) \
59	if (n <= debug) {	 \
60		printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
61	}
62#else
63#define DEBUG(n, arg...)
64static const int debug = 0;
65#endif
66
67
68/* Some default values for inital camera settings,
69   can be set by modprobe */
70
71static enum frame_sizes size;
72static int speed = 6;		/* Speed (fps) 0 (slowest) to 6 (fastest) */
73static int brightness =	MAX_BRIGHTNESS/2;
74static int contrast =	MAX_CONTRAST/2;
75static int saturation =	MAX_SATURATION/2;
76static int sharpness =	MAX_SHARPNESS/2;
77static int whitebal =	3*(MAX_WHITEBAL/4);
78
79static int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
80
81/* These FPS speeds are from the windows config box. They are
82 * indexed on size (0-3) and speed (0-6). Divide by 3 to get the
83 * real fps.
84 */
85
86static int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
87			       { 24, 40, 48, 60, 72, 80, 100 },
88			       { 18, 30, 36, 45, 54, 60, 75  },
89			       { 6,  10, 12, 15, 18, 21, 25  } };
90
91struct cam_size {
92	u16	width;
93	u16	height;
94	u8	cmd;
95};
96
97static struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
98					  { 160, 136, 0xa },
99					  { 176, 144, 0x4 },
100					  { 320, 240, 0x5 } };
101
102struct konicawc {
103	u8 brightness;		/* camera uses 0 - 9, x11 for real value */
104	u8 contrast;		/* as above */
105	u8 saturation;		/* as above */
106	u8 sharpness;		/* as above */
107	u8 white_bal;		/* 0 - 33, x11 for real value */
108	u8 speed;		/* Stored as 0 - 6, used as index in spd_to_* (above) */
109	u8 size;		/* Frame Size */
110	int height;
111	int width;
112	struct urb *sts_urb[USBVIDEO_NUMSBUF];
113	u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
114	struct urb *last_data_urb, *last_sts_urb;
115	int lastframe;
116	int cur_frame_size;	/* number of bytes in current frame size */
117	int maxline;		/* number of lines per frame */
118	int yplanesz;		/* Number of bytes in the Y plane */
119	unsigned int buttonsts:1;
120#ifdef CONFIG_INPUT
121	struct input_dev input;
122#endif
123};
124
125
126#define konicawc_set_misc(uvd, req, value, index)		konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
127#define konicawc_get_misc(uvd, req, value, index, buf, sz)	konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
128#define konicawc_set_value(uvd, value, index)			konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
129
130
131static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
132{
133        int retval = usb_control_msg(uvd->dev,
134		dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
135		    request, 0x40 | dir, value, index, buf, len, HZ);
136        return retval < 0 ? retval : 0;
137}
138
139
140static inline void konicawc_camera_on(struct uvd *uvd)
141{
142        DEBUG(0, "camera on");
143        konicawc_set_misc(uvd, 0x2, 1, 0x0b);
144}
145
146
147static inline void konicawc_camera_off(struct uvd *uvd)
148{
149        DEBUG(0, "camera off");
150        konicawc_set_misc(uvd, 0x2, 0, 0x0b);
151}
152
153
154static void konicawc_set_camera_size(struct uvd *uvd)
155{
156	struct konicawc *cam = (struct konicawc *)uvd->user_data;
157
158	konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
159	cam->width = camera_sizes[cam->size].width;
160	cam->height = camera_sizes[cam->size].height;
161	cam->yplanesz = cam->height * cam->width;
162	cam->cur_frame_size = (cam->yplanesz * 3) / 2;
163	cam->maxline = cam->yplanesz / 256;
164	uvd->videosize = VIDEOSIZE(cam->width, cam->height);
165}
166
167
168static int konicawc_setup_on_open(struct uvd *uvd)
169{
170	struct konicawc *cam = (struct konicawc *)uvd->user_data;
171
172	DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
173	    cam->brightness * 11);
174	konicawc_set_value(uvd, cam->brightness, SetBrightness);
175	DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
176	    cam->white_bal * 11);
177	konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
178	DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
179	    cam->contrast * 11);
180	konicawc_set_value(uvd, cam->contrast, SetContrast);
181	DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
182	    cam->saturation * 11);
183	konicawc_set_value(uvd, cam->saturation, SetSaturation);
184	DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
185	    cam->sharpness * 11);
186	konicawc_set_value(uvd, cam->sharpness, SetSharpness);
187	konicawc_set_camera_size(uvd);
188	cam->lastframe = -2;
189	cam->buttonsts = 0;
190	return 0;
191}
192
193
194static void konicawc_adjust_picture(struct uvd *uvd)
195{
196	struct konicawc *cam = (struct konicawc *)uvd->user_data;
197
198	konicawc_camera_off(uvd);
199	DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
200	uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
201	if(cam->brightness != uvd->vpic.brightness / 11) {
202	   cam->brightness = uvd->vpic.brightness / 11;
203	   DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
204	       cam->brightness * 11);
205	   konicawc_set_value(uvd, cam->brightness, SetBrightness);
206	}
207
208	DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
209	uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
210	if(cam->contrast != uvd->vpic.contrast / 11) {
211		cam->contrast = uvd->vpic.contrast / 11;
212		DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
213		    cam->contrast * 11);
214		konicawc_set_value(uvd, cam->contrast, SetContrast);
215	}
216	konicawc_camera_on(uvd);
217}
218
219
220static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
221{
222	char *cdata;
223	int i, totlen = 0;
224	unsigned char *status = stsurb->transfer_buffer;
225	int keep = 0, discard = 0, bad = 0;
226	struct konicawc *cam = (struct konicawc *)uvd->user_data;
227
228	for (i = 0; i < dataurb->number_of_packets; i++) {
229		int button = cam->buttonsts;
230		unsigned char sts;
231		int n = dataurb->iso_frame_desc[i].actual_length;
232		int st = dataurb->iso_frame_desc[i].status;
233		cdata = dataurb->transfer_buffer +
234			dataurb->iso_frame_desc[i].offset;
235
236		/* Detect and ignore errored packets */
237		if (st < 0) {
238			DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
239			      i, n, st);
240			uvd->stats.iso_err_count++;
241			continue;
242		}
243
244		/* Detect and ignore empty packets */
245		if (n <= 0) {
246			uvd->stats.iso_skip_count++;
247			continue;
248		}
249
250		/* See what the status data said about the packet */
251		sts = *(status+stsurb->iso_frame_desc[i].offset);
252
253		/* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
254		 * otherwise:
255		 * bit 0 0: keep packet
256		 *	 1: drop packet (padding data)
257		 *
258		 * bit 4 0 button not clicked
259		 *       1 button clicked
260		 * button is used to `take a picture' (in software)
261		 */
262
263		if(sts < 0x80) {
264			button = !!(sts & 0x40);
265			sts &= ~0x40;
266		}
267
268		/* work out the button status, but dont do
269		   anything with it for now */
270
271		if(button != cam->buttonsts) {
272			DEBUG(2, "button: %sclicked", button ? "" : "un");
273			cam->buttonsts = button;
274#ifdef CONFIG_INPUT
275			input_report_key(&cam->input, BTN_0, cam->buttonsts);
276#endif
277		}
278
279		if(sts == 0x01) { /* drop frame */
280			discard++;
281			continue;
282		}
283
284		if((sts > 0x01) && (sts < 0x80)) {
285			info("unknown status %2.2x", sts);
286			bad++;
287			continue;
288		}
289		if(!sts && cam->lastframe == -2) {
290			DEBUG(2, "dropping frame looking for image start");
291			continue;
292		}
293
294		keep++;
295		if(sts & 0x80) { /* frame start */
296			unsigned char marker[] = { 0, 0xff, 0, 0x00 };
297
298			if(cam->lastframe == -2) {
299				DEBUG(2, "found initial image");
300				cam->lastframe = -1;
301			}
302
303			marker[3] = sts & 0x7F;
304			RingQueue_Enqueue(&uvd->dp, marker, 4);
305			totlen += 4;
306		}
307
308		totlen += n;	/* Little local accounting */
309		RingQueue_Enqueue(&uvd->dp, cdata, n);
310	}
311	DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
312		    keep, discard, bad, totlen);
313	return totlen;
314}
315
316
317static void resubmit_urb(struct uvd *uvd, struct urb *urb)
318{
319        int i, ret;
320        for (i = 0; i < FRAMES_PER_DESC; i++) {
321                urb->iso_frame_desc[i].status = 0;
322        }
323        urb->dev = uvd->dev;
324        urb->status = 0;
325        ret = usb_submit_urb(urb);
326	DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
327        if(ret)
328                err("usb_submit_urb error (%d)", ret);
329
330}
331
332
333static void konicawc_isoc_irq(struct urb *urb)
334{
335	struct uvd *uvd = urb->context;
336	struct konicawc *cam = (struct konicawc *)uvd->user_data;
337
338	/* We don't want to do anything if we are about to be removed! */
339	if (!CAMERA_IS_OPERATIONAL(uvd))
340		return;
341
342	if (!uvd->streaming) {
343		DEBUG(1, "Not streaming, but interrupt!");
344		return;
345	}
346
347	DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
348
349	uvd->stats.urb_count++;
350
351	if (urb->transfer_buffer_length > 32) {
352		cam->last_data_urb = urb;
353	} else {
354		cam->last_sts_urb = urb;
355	}
356	/* Copy the data received into ring queue */
357	if(cam->last_data_urb && cam->last_sts_urb) {
358		int len = 0;
359		if(cam->last_sts_urb->start_frame != cam->last_data_urb->start_frame)
360			err("Lost sync on frames (%d != %d)",
361			    cam->last_sts_urb->start_frame, cam->last_data_urb->start_frame);
362		else if (!cam->last_sts_urb->status && !cam->last_data_urb->status)
363			len = konicawc_compress_iso(uvd, cam->last_data_urb,
364						    cam->last_sts_urb);
365
366		resubmit_urb(uvd, cam->last_sts_urb);
367		resubmit_urb(uvd, cam->last_data_urb);
368		cam->last_data_urb = NULL;
369		cam->last_sts_urb = NULL;
370		uvd->stats.urb_length = len;
371		uvd->stats.data_count += len;
372		if(len)
373			RingQueue_WakeUpInterruptible(&uvd->dp);
374		return;
375	}
376	return;
377}
378
379
380static int konicawc_start_data(struct uvd *uvd)
381{
382	struct usb_device *dev = uvd->dev;
383	int i, errFlag;
384	struct konicawc *cam = (struct konicawc *)uvd->user_data;
385	int pktsz;
386	struct usb_interface_descriptor *interface;
387
388	interface = &dev->actconfig->interface[uvd->iface].altsetting[spd_to_iface[cam->speed]];
389	pktsz = interface->endpoint[1].wMaxPacketSize;
390	DEBUG(1, "pktsz = %d", pktsz);
391	if (!CAMERA_IS_OPERATIONAL(uvd)) {
392		err("Camera is not operational");
393		return -EFAULT;
394	}
395	uvd->curframe = -1;
396	konicawc_camera_on(uvd);
397	/* Alternate interface 1 is is the biggest frame size */
398	i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
399	if (i < 0) {
400		err("usb_set_interface error");
401		uvd->last_error = i;
402		return -EBUSY;
403	}
404
405	/* We double buffer the Iso lists */
406	for (i=0; i < USBVIDEO_NUMSBUF; i++) {
407		int j, k;
408		struct urb *urb = uvd->sbuf[i].urb;
409		urb->dev = dev;
410		urb->context = uvd;
411		urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
412		urb->interval = 1;
413		urb->transfer_flags = USB_ISO_ASAP;
414		urb->transfer_buffer = uvd->sbuf[i].data;
415		urb->complete = konicawc_isoc_irq;
416		urb->number_of_packets = FRAMES_PER_DESC;
417		urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
418		for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
419			urb->iso_frame_desc[j].offset = k;
420			urb->iso_frame_desc[j].length = pktsz;
421		}
422
423		urb = cam->sts_urb[i];
424		urb->dev = dev;
425		urb->context = uvd;
426		urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
427		urb->interval = 1;
428		urb->transfer_flags = USB_ISO_ASAP;
429		urb->transfer_buffer = cam->sts_buf[i];
430		urb->complete = konicawc_isoc_irq;
431		urb->number_of_packets = FRAMES_PER_DESC;
432		urb->transfer_buffer_length = FRAMES_PER_DESC;
433		for (j=0; j < FRAMES_PER_DESC; j++) {
434			urb->iso_frame_desc[j].offset = j;
435			urb->iso_frame_desc[j].length = 1;
436		}
437	}
438
439	cam->last_data_urb = NULL;
440	cam->last_sts_urb = NULL;
441
442	/* Submit all URBs */
443	for (i=0; i < USBVIDEO_NUMSBUF; i++) {
444		errFlag = usb_submit_urb(cam->sts_urb[i]);
445		if (errFlag)
446			err("usb_submit_isoc(%d) ret %d", i, errFlag);
447
448		errFlag = usb_submit_urb(uvd->sbuf[i].urb);
449		if (errFlag)
450			err ("usb_submit_isoc(%d) ret %d", i, errFlag);
451	}
452
453	uvd->streaming = 1;
454	DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
455	return 0;
456}
457
458
459static void konicawc_stop_data(struct uvd *uvd)
460{
461	int i, j;
462	struct konicawc *cam;
463
464	if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
465		return;
466
467	konicawc_camera_off(uvd);
468	uvd->streaming = 0;
469	cam = (struct konicawc *)uvd->user_data;
470	cam->last_data_urb = NULL;
471	cam->last_sts_urb = NULL;
472
473	/* Unschedule all of the iso td's */
474	for (i=0; i < USBVIDEO_NUMSBUF; i++) {
475		j = usb_unlink_urb(uvd->sbuf[i].urb);
476		if (j < 0)
477			err("usb_unlink_urb() error %d.", j);
478
479		j = usb_unlink_urb(cam->sts_urb[i]);
480		if (j < 0)
481			err("usb_unlink_urb() error %d.", j);
482	}
483
484	if (!uvd->remove_pending) {
485		/* Set packet size to 0 */
486		j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
487		if (j < 0) {
488			err("usb_set_interface() error %d.", j);
489			uvd->last_error = j;
490		}
491	}
492}
493
494
495static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
496{
497	struct konicawc *cam = (struct konicawc *)uvd->user_data;
498	int maxline = cam->maxline;
499	int yplanesz = cam->yplanesz;
500
501	assert(frame != NULL);
502
503	DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
504	DEBUG(3, "Frame state = %d", frame->scanstate);
505
506	if(frame->scanstate == ScanState_Scanning) {
507		int drop = 0;
508		int curframe;
509		int fdrops = 0;
510		DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
511		while(RingQueue_GetLength(&uvd->dp) >= 4) {
512			if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
513			    (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
514			    (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
515			    (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
516				curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
517				if(cam->lastframe >= 0) {
518					fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
519					fdrops--;
520					if(fdrops) {
521						info("Dropped %d frames (%d -> %d)", fdrops,
522						     cam->lastframe, curframe);
523					}
524				}
525				cam->lastframe = curframe;
526				frame->curline = 0;
527				frame->scanstate = ScanState_Lines;
528				RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
529				break;
530			}
531			RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
532			drop++;
533		}
534		if(drop)
535			DEBUG(2, "dropped %d bytes looking for new frame", drop);
536	}
537
538	if(frame->scanstate == ScanState_Scanning)
539		return;
540
541	/* Try to move data from queue into frame buffer
542	 * We get data in blocks of 384 bytes made up of:
543	 * 256 Y, 64 U, 64 V.
544	 * This needs to be written out as a Y plane, a U plane and a V plane.
545	 */
546
547	while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
548		/* Y */
549		RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
550		/* U */
551		RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
552		/* V */
553		RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
554		frame->seqRead_Length += 384;
555		frame->curline++;
556	}
557	/* See if we filled the frame */
558	if (frame->curline == maxline) {
559		DEBUG(5, "got whole frame");
560
561		frame->frameState = FrameState_Done_Hold;
562		frame->curline = 0;
563		uvd->curframe = -1;
564		uvd->stats.frame_num++;
565	}
566}
567
568
569static int konicawc_find_fps(int size, int fps)
570{
571	int i;
572
573	fps *= 3;
574	DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
575	if(fps <= spd_to_fps[size][0])
576		return 0;
577
578	if(fps >= spd_to_fps[size][MAX_SPEED])
579		return MAX_SPEED;
580
581	for(i = 0; i < MAX_SPEED; i++) {
582		if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
583			DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
584			if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
585				return i;
586			else
587				return i+1;
588		}
589	}
590	return MAX_SPEED+1;
591}
592
593
594static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
595{
596	struct konicawc *cam = (struct konicawc *)uvd->user_data;
597	int newspeed = cam->speed;
598	int newsize;
599	int x = vw->width;
600	int y = vw->height;
601	int fps = vw->flags;
602
603	if(x > 0 && y > 0) {
604		DEBUG(2, "trying to find size %d,%d", x, y);
605		for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
606			if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
607				break;
608		}
609	} else {
610		newsize = cam->size;
611	}
612
613	if(newsize > MAX_FRAME_SIZE) {
614		DEBUG(1, "couldnt find size %d,%d", x, y);
615		return -EINVAL;
616	}
617
618	if(fps > 0) {
619		DEBUG(1, "trying to set fps to %d", fps);
620		newspeed = konicawc_find_fps(newsize, fps);
621		DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
622	}
623
624	if(newspeed > MAX_SPEED)
625		return -EINVAL;
626
627	DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
628	if((newsize == cam->size) && (newspeed == cam->speed)) {
629		DEBUG(1, "Nothing to do");
630		return 0;
631	}
632	DEBUG(0, "setting to  %dx%d @ %d fps", camera_sizes[newsize].width,
633	     camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
634
635	konicawc_stop_data(uvd);
636	uvd->ifaceAltActive = spd_to_iface[newspeed];
637	DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
638	cam->speed = newspeed;
639
640	if(cam->size != newsize) {
641		cam->size = newsize;
642		konicawc_set_camera_size(uvd);
643	}
644
645	/* Flush the input queue and clear any current frame in progress */
646
647	RingQueue_Flush(&uvd->dp);
648	cam->lastframe = -2;
649	if(uvd->curframe != -1) {
650	  uvd->frame[uvd->curframe].curline = 0;
651	  uvd->frame[uvd->curframe].seqRead_Length = 0;
652	  uvd->frame[uvd->curframe].seqRead_Index = 0;
653	}
654
655	konicawc_start_data(uvd);
656	return 0;
657}
658
659
660static int konicawc_calculate_fps(struct uvd *uvd)
661{
662	struct konicawc *cam = uvd->user_data;
663	return spd_to_fps[cam->size][cam->speed]/3;
664}
665
666
667static void konicawc_configure_video(struct uvd *uvd)
668{
669	struct konicawc *cam = (struct konicawc *)uvd->user_data;
670	u8 buf[2];
671
672	memset(&uvd->vpic, 0, sizeof(uvd->vpic));
673	memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
674
675	RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
676	RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
677	RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
678	RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
679	RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
680
681	cam->brightness = brightness / 11;
682	cam->contrast = contrast / 11;
683	cam->saturation = saturation / 11;
684	cam->sharpness = sharpness / 11;
685	cam->white_bal = whitebal / 11;
686
687	uvd->vpic.colour = 108;
688	uvd->vpic.hue = 108;
689	uvd->vpic.brightness = brightness;
690	uvd->vpic.contrast = contrast;
691	uvd->vpic.whiteness = whitebal;
692	uvd->vpic.depth = 6;
693	uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
694
695	memset(&uvd->vcap, 0, sizeof(uvd->vcap));
696	strcpy(uvd->vcap.name, "Konica Webcam");
697	uvd->vcap.type = VID_TYPE_CAPTURE;
698	uvd->vcap.channels = 1;
699	uvd->vcap.audios = 0;
700	uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
701	uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
702	uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
703	uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
704
705	memset(&uvd->vchan, 0, sizeof(uvd->vchan));
706	uvd->vchan.flags = 0 ;
707	uvd->vchan.tuners = 0;
708	uvd->vchan.channel = 0;
709	uvd->vchan.type = VIDEO_TYPE_CAMERA;
710	strcpy(uvd->vchan.name, "Camera");
711
712	/* Talk to device */
713	DEBUG(1, "device init");
714	if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
715		DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
716	if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
717		DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
718	if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
719		DEBUG(2, "2,0,d failed");
720	DEBUG(1, "setting initial values");
721}
722
723
724static void *konicawc_probe(struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *devid)
725{
726	struct uvd *uvd = NULL;
727	int i, nas;
728	int actInterface=-1, inactInterface=-1, maxPS=0;
729	unsigned char video_ep = 0;
730
731	DEBUG(1, "konicawc_probe(%p,%u.)", dev, ifnum);
732
733	/* We don't handle multi-config cameras */
734	if (dev->descriptor.bNumConfigurations != 1)
735		return NULL;
736
737	info("Konica Webcam (rev. 0x%04x)", dev->descriptor.bcdDevice);
738	RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
739
740	/* Validate found interface: must have one ISO endpoint */
741	nas = dev->actconfig->interface[ifnum].num_altsetting;
742	if (nas != 8) {
743		err("Incorrect number of alternate settings (%d) for this camera!", nas);
744		return NULL;
745	}
746	/* Validate all alternate settings */
747	for (i=0; i < nas; i++) {
748		const struct usb_interface_descriptor *interface;
749		const struct usb_endpoint_descriptor *endpoint;
750
751		interface = &dev->actconfig->interface[ifnum].altsetting[i];
752		if (interface->bNumEndpoints != 2) {
753			err("Interface %d. has %u. endpoints!",
754			    ifnum, (unsigned)(interface->bNumEndpoints));
755			return NULL;
756		}
757		endpoint = &interface->endpoint[1];
758		DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
759		    endpoint->bEndpointAddress, endpoint->wMaxPacketSize);
760		if (video_ep == 0)
761			video_ep = endpoint->bEndpointAddress;
762		else if (video_ep != endpoint->bEndpointAddress) {
763			err("Alternate settings have different endpoint addresses!");
764			return NULL;
765		}
766		if ((endpoint->bmAttributes & 0x03) != 0x01) {
767			err("Interface %d. has non-ISO endpoint!", ifnum);
768			return NULL;
769		}
770		if ((endpoint->bEndpointAddress & 0x80) == 0) {
771			err("Interface %d. has ISO OUT endpoint!", ifnum);
772			return NULL;
773		}
774		if (endpoint->wMaxPacketSize == 0) {
775			if (inactInterface < 0)
776				inactInterface = i;
777			else {
778				err("More than one inactive alt. setting!");
779				return NULL;
780			}
781		} else {
782			if (i == spd_to_iface[speed]) {
783				/* This one is the requested one */
784				actInterface = i;
785			}
786		}
787		if(endpoint->wMaxPacketSize > maxPS)
788			maxPS = endpoint->wMaxPacketSize;
789	}
790	if(actInterface == -1) {
791		err("Cant find required endpoint");
792		return NULL;
793	}
794
795	DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
796
797	/* Code below may sleep, need to lock module while we are here */
798	MOD_INC_USE_COUNT;
799	uvd = usbvideo_AllocateDevice(cams);
800	if (uvd != NULL) {
801		struct konicawc *cam = (struct konicawc *)(uvd->user_data);
802		/* Here uvd is a fully allocated uvd object */
803		for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
804			cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC);
805			if(cam->sts_urb[i] == NULL) {
806				while(i--) {
807					usb_free_urb(cam->sts_urb[i]);
808				}
809				err("cant allocate urbs");
810				return NULL;
811			}
812		}
813		cam->speed = speed;
814		RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
815		cam->width = camera_sizes[size].width;
816		cam->height = camera_sizes[size].height;
817		cam->size = size;
818
819		uvd->flags = 0;
820		uvd->debug = debug;
821		uvd->dev = dev;
822		uvd->iface = ifnum;
823		uvd->ifaceAltInactive = inactInterface;
824		uvd->ifaceAltActive = actInterface;
825		uvd->video_endp = video_ep;
826		uvd->iso_packet_len = maxPS;
827		uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
828		uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
829		uvd->canvas = VIDEOSIZE(320, 240);
830		uvd->videosize = VIDEOSIZE(cam->width, cam->height);
831
832		/* Initialize konicawc specific data */
833		konicawc_configure_video(uvd);
834
835		i = usbvideo_RegisterVideoDevice(uvd);
836		uvd->max_frame_size = (320 * 240 * 3)/2;
837		if (i != 0) {
838			err("usbvideo_RegisterVideoDevice() failed.");
839			uvd = NULL;
840		}
841#ifdef CONFIG_INPUT
842		/* Register input device for button */
843		memset(&cam->input, 0, sizeof(struct input_dev));
844		cam->input.name = "Konicawc snapshot button";
845		cam->input.private = cam;
846		cam->input.evbit[0] = BIT(EV_KEY);
847		cam->input.keybit[LONG(BTN_0)] = BIT(BTN_0);
848		cam->input.idbus = BUS_USB;
849		cam->input.idvendor = dev->descriptor.idVendor;
850		cam->input.idproduct = dev->descriptor.idProduct;
851		cam->input.idversion = dev->descriptor.bcdDevice;
852		input_register_device(&cam->input);
853#endif
854	}
855	MOD_DEC_USE_COUNT;
856	return uvd;
857}
858
859
860static void konicawc_free_uvd(struct uvd *uvd)
861{
862	int i;
863	struct konicawc *cam = (struct konicawc *)uvd->user_data;
864
865#ifdef CONFIG_INPUT
866	input_unregister_device(&cam->input);
867#endif
868	for (i=0; i < USBVIDEO_NUMSBUF; i++) {
869		usb_free_urb(cam->sts_urb[i]);
870		cam->sts_urb[i] = NULL;
871	}
872}
873
874
875static struct usb_device_id id_table[] = {
876	{ USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
877	{ }  /* Terminating entry */
878};
879
880
881static int __init konicawc_init(void)
882{
883	struct usbvideo_cb cbTbl;
884	info(DRIVER_DESC " " DRIVER_VERSION);
885	memset(&cbTbl, 0, sizeof(cbTbl));
886	cbTbl.probe = konicawc_probe;
887	cbTbl.setupOnOpen = konicawc_setup_on_open;
888	cbTbl.processData = konicawc_process_isoc;
889	cbTbl.getFPS = konicawc_calculate_fps;
890	cbTbl.setVideoMode = konicawc_set_video_mode;
891	cbTbl.startDataPump = konicawc_start_data;
892	cbTbl.stopDataPump = konicawc_stop_data;
893	cbTbl.adjustPicture = konicawc_adjust_picture;
894	cbTbl.userFree = konicawc_free_uvd;
895	return usbvideo_register(
896		&cams,
897		MAX_CAMERAS,
898		sizeof(struct konicawc),
899		"konicawc",
900		&cbTbl,
901		THIS_MODULE,
902		id_table);
903}
904
905
906static void __exit konicawc_cleanup(void)
907{
908	usbvideo_Deregister(&cams);
909}
910
911
912MODULE_DEVICE_TABLE(usb, id_table);
913
914MODULE_LICENSE("GPL");
915MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
916MODULE_DESCRIPTION(DRIVER_DESC);
917MODULE_PARM(speed, "i");
918MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
919MODULE_PARM(size, "i");
920MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
921MODULE_PARM(brightness, "i");
922MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
923MODULE_PARM(contrast, "i");
924MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
925MODULE_PARM(saturation, "i");
926MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
927MODULE_PARM(sharpness, "i");
928MODULE_PARM_DESC(sharpness, "Initial sharpness 0 - 108");
929MODULE_PARM(whitebal, "i");
930MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
931
932#ifdef CONFIG_USB_DEBUG
933MODULE_PARM(debug, "i");
934MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
935#endif
936
937module_init(konicawc_init);
938module_exit(konicawc_cleanup);
939