[PATCH] I2C: i2c-algo-pca -- gracefully handle a busy bus
[linux-2.6/next.git] / drivers / usb / media / konicawc.c
blob20ac9e1069d46cd7f0e5a1fba5d65f0302e62269
1 /*
2 * konicawc.c - konica webcam driver
4 * Author: Simon Evans <spse@secret.org.uk>
6 * Copyright (C) 2002 Simon Evans
8 * Licence: GPL
10 * Driver for USB webcams based on Konica chipset. This
11 * chipset is used in Intel YC76 camera.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/input.h>
19 #include <linux/usb_input.h>
21 #include "usbvideo.h"
23 #define MAX_BRIGHTNESS 108
24 #define MAX_CONTRAST 108
25 #define MAX_SATURATION 108
26 #define MAX_SHARPNESS 108
27 #define MAX_WHITEBAL 372
28 #define MAX_SPEED 6
31 #define MAX_CAMERAS 1
33 #define DRIVER_VERSION "v1.4"
34 #define DRIVER_DESC "Konica Webcam driver"
36 enum ctrl_req {
37 SetWhitebal = 0x01,
38 SetBrightness = 0x02,
39 SetSharpness = 0x03,
40 SetContrast = 0x04,
41 SetSaturation = 0x05,
45 enum frame_sizes {
46 SIZE_160X120 = 0,
47 SIZE_160X136 = 1,
48 SIZE_176X144 = 2,
49 SIZE_320X240 = 3,
53 #define MAX_FRAME_SIZE SIZE_320X240
55 static struct usbvideo *cams;
57 #ifdef CONFIG_USB_DEBUG
58 static int debug;
59 #define DEBUG(n, format, arg...) \
60 if (n <= debug) { \
61 printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
63 #else
64 #define DEBUG(n, arg...)
65 static const int debug = 0;
66 #endif
69 /* Some default values for initial camera settings,
70 can be set by modprobe */
72 static int size;
73 static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
74 static int brightness = MAX_BRIGHTNESS/2;
75 static int contrast = MAX_CONTRAST/2;
76 static int saturation = MAX_SATURATION/2;
77 static int sharpness = MAX_SHARPNESS/2;
78 static int whitebal = 3*(MAX_WHITEBAL/4);
80 static int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
82 /* These FPS speeds are from the windows config box. They are
83 * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
84 * real fps.
87 static int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
88 { 24, 40, 48, 60, 72, 80, 100 },
89 { 18, 30, 36, 45, 54, 60, 75 },
90 { 6, 10, 12, 15, 18, 21, 25 } };
92 struct cam_size {
93 u16 width;
94 u16 height;
95 u8 cmd;
98 static struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
99 { 160, 136, 0xa },
100 { 176, 144, 0x4 },
101 { 320, 240, 0x5 } };
103 struct konicawc {
104 u8 brightness; /* camera uses 0 - 9, x11 for real value */
105 u8 contrast; /* as above */
106 u8 saturation; /* as above */
107 u8 sharpness; /* as above */
108 u8 white_bal; /* 0 - 33, x11 for real value */
109 u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
110 u8 size; /* Frame Size */
111 int height;
112 int width;
113 struct urb *sts_urb[USBVIDEO_NUMSBUF];
114 u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
115 struct urb *last_data_urb;
116 int lastframe;
117 int cur_frame_size; /* number of bytes in current frame size */
118 int maxline; /* number of lines per frame */
119 int yplanesz; /* Number of bytes in the Y plane */
120 unsigned int buttonsts:1;
121 #ifdef CONFIG_INPUT
122 struct input_dev input;
123 char input_physname[64];
124 #endif
128 #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
129 #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
130 #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
133 static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
135 int retval = usb_control_msg(uvd->dev,
136 dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
137 request, 0x40 | dir, value, index, buf, len, 1000);
138 return retval < 0 ? retval : 0;
142 static inline void konicawc_camera_on(struct uvd *uvd)
144 DEBUG(0, "camera on");
145 konicawc_set_misc(uvd, 0x2, 1, 0x0b);
149 static inline void konicawc_camera_off(struct uvd *uvd)
151 DEBUG(0, "camera off");
152 konicawc_set_misc(uvd, 0x2, 0, 0x0b);
156 static void konicawc_set_camera_size(struct uvd *uvd)
158 struct konicawc *cam = (struct konicawc *)uvd->user_data;
160 konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
161 cam->width = camera_sizes[cam->size].width;
162 cam->height = camera_sizes[cam->size].height;
163 cam->yplanesz = cam->height * cam->width;
164 cam->cur_frame_size = (cam->yplanesz * 3) / 2;
165 cam->maxline = cam->yplanesz / 256;
166 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
170 static int konicawc_setup_on_open(struct uvd *uvd)
172 struct konicawc *cam = (struct konicawc *)uvd->user_data;
174 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
175 cam->brightness * 11);
176 konicawc_set_value(uvd, cam->brightness, SetBrightness);
177 DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
178 cam->white_bal * 11);
179 konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
180 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
181 cam->contrast * 11);
182 konicawc_set_value(uvd, cam->contrast, SetContrast);
183 DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
184 cam->saturation * 11);
185 konicawc_set_value(uvd, cam->saturation, SetSaturation);
186 DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
187 cam->sharpness * 11);
188 konicawc_set_value(uvd, cam->sharpness, SetSharpness);
189 konicawc_set_camera_size(uvd);
190 cam->lastframe = -2;
191 cam->buttonsts = 0;
192 return 0;
196 static void konicawc_adjust_picture(struct uvd *uvd)
198 struct konicawc *cam = (struct konicawc *)uvd->user_data;
200 konicawc_camera_off(uvd);
201 DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
202 uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
203 if(cam->brightness != uvd->vpic.brightness / 11) {
204 cam->brightness = uvd->vpic.brightness / 11;
205 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
206 cam->brightness * 11);
207 konicawc_set_value(uvd, cam->brightness, SetBrightness);
210 DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
211 uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
212 if(cam->contrast != uvd->vpic.contrast / 11) {
213 cam->contrast = uvd->vpic.contrast / 11;
214 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
215 cam->contrast * 11);
216 konicawc_set_value(uvd, cam->contrast, SetContrast);
218 konicawc_camera_on(uvd);
222 static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
224 char *cdata;
225 int i, totlen = 0;
226 unsigned char *status = stsurb->transfer_buffer;
227 int keep = 0, discard = 0, bad = 0;
228 struct konicawc *cam = (struct konicawc *)uvd->user_data;
230 for (i = 0; i < dataurb->number_of_packets; i++) {
231 int button = cam->buttonsts;
232 unsigned char sts;
233 int n = dataurb->iso_frame_desc[i].actual_length;
234 int st = dataurb->iso_frame_desc[i].status;
235 cdata = dataurb->transfer_buffer +
236 dataurb->iso_frame_desc[i].offset;
238 /* Detect and ignore errored packets */
239 if (st < 0) {
240 DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
241 i, n, st);
242 uvd->stats.iso_err_count++;
243 continue;
246 /* Detect and ignore empty packets */
247 if (n <= 0) {
248 uvd->stats.iso_skip_count++;
249 continue;
252 /* See what the status data said about the packet */
253 sts = *(status+stsurb->iso_frame_desc[i].offset);
255 /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
256 * otherwise:
257 * bit 0 0: keep packet
258 * 1: drop packet (padding data)
260 * bit 4 0 button not clicked
261 * 1 button clicked
262 * button is used to `take a picture' (in software)
265 if(sts < 0x80) {
266 button = !!(sts & 0x40);
267 sts &= ~0x40;
270 /* work out the button status, but don't do
271 anything with it for now */
273 if(button != cam->buttonsts) {
274 DEBUG(2, "button: %sclicked", button ? "" : "un");
275 cam->buttonsts = button;
276 #ifdef CONFIG_INPUT
277 input_report_key(&cam->input, BTN_0, cam->buttonsts);
278 input_sync(&cam->input);
279 #endif
282 if(sts == 0x01) { /* drop frame */
283 discard++;
284 continue;
287 if((sts > 0x01) && (sts < 0x80)) {
288 info("unknown status %2.2x", sts);
289 bad++;
290 continue;
292 if(!sts && cam->lastframe == -2) {
293 DEBUG(2, "dropping frame looking for image start");
294 continue;
297 keep++;
298 if(sts & 0x80) { /* frame start */
299 unsigned char marker[] = { 0, 0xff, 0, 0x00 };
301 if(cam->lastframe == -2) {
302 DEBUG(2, "found initial image");
303 cam->lastframe = -1;
306 marker[3] = sts & 0x7F;
307 RingQueue_Enqueue(&uvd->dp, marker, 4);
308 totlen += 4;
311 totlen += n; /* Little local accounting */
312 RingQueue_Enqueue(&uvd->dp, cdata, n);
314 DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
315 keep, discard, bad, totlen);
316 return totlen;
320 static void resubmit_urb(struct uvd *uvd, struct urb *urb)
322 int i, ret;
323 for (i = 0; i < FRAMES_PER_DESC; i++) {
324 urb->iso_frame_desc[i].status = 0;
326 urb->dev = uvd->dev;
327 urb->status = 0;
328 ret = usb_submit_urb(urb, GFP_ATOMIC);
329 DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
330 if(ret)
331 err("usb_submit_urb error (%d)", ret);
336 static void konicawc_isoc_irq(struct urb *urb, struct pt_regs *regs)
338 struct uvd *uvd = urb->context;
339 struct konicawc *cam = (struct konicawc *)uvd->user_data;
341 /* We don't want to do anything if we are about to be removed! */
342 if (!CAMERA_IS_OPERATIONAL(uvd))
343 return;
345 if (!uvd->streaming) {
346 DEBUG(1, "Not streaming, but interrupt!");
347 return;
350 DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
352 uvd->stats.urb_count++;
354 if (urb->transfer_buffer_length > 32) {
355 cam->last_data_urb = urb;
356 return;
358 /* Copy the data received into ring queue */
359 if(cam->last_data_urb) {
360 int len = 0;
361 if(urb->start_frame != cam->last_data_urb->start_frame)
362 err("Lost sync on frames");
363 else if (!urb->status && !cam->last_data_urb->status)
364 len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
366 resubmit_urb(uvd, cam->last_data_urb);
367 resubmit_urb(uvd, urb);
368 cam->last_data_urb = NULL;
369 uvd->stats.urb_length = len;
370 uvd->stats.data_count += len;
371 if(len)
372 RingQueue_WakeUpInterruptible(&uvd->dp);
373 return;
375 return;
379 static int konicawc_start_data(struct uvd *uvd)
381 struct usb_device *dev = uvd->dev;
382 int i, errFlag;
383 struct konicawc *cam = (struct konicawc *)uvd->user_data;
384 int pktsz;
385 struct usb_interface *intf;
386 struct usb_host_interface *interface = NULL;
388 intf = usb_ifnum_to_if(dev, uvd->iface);
389 if (intf)
390 interface = usb_altnum_to_altsetting(intf,
391 spd_to_iface[cam->speed]);
392 if (!interface)
393 return -ENXIO;
394 pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
395 DEBUG(1, "pktsz = %d", pktsz);
396 if (!CAMERA_IS_OPERATIONAL(uvd)) {
397 err("Camera is not operational");
398 return -EFAULT;
400 uvd->curframe = -1;
401 konicawc_camera_on(uvd);
402 /* Alternate interface 1 is is the biggest frame size */
403 i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
404 if (i < 0) {
405 err("usb_set_interface error");
406 uvd->last_error = i;
407 return -EBUSY;
410 /* We double buffer the Iso lists */
411 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
412 int j, k;
413 struct urb *urb = uvd->sbuf[i].urb;
414 urb->dev = dev;
415 urb->context = uvd;
416 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
417 urb->interval = 1;
418 urb->transfer_flags = URB_ISO_ASAP;
419 urb->transfer_buffer = uvd->sbuf[i].data;
420 urb->complete = konicawc_isoc_irq;
421 urb->number_of_packets = FRAMES_PER_DESC;
422 urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
423 for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
424 urb->iso_frame_desc[j].offset = k;
425 urb->iso_frame_desc[j].length = pktsz;
428 urb = cam->sts_urb[i];
429 urb->dev = dev;
430 urb->context = uvd;
431 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
432 urb->interval = 1;
433 urb->transfer_flags = URB_ISO_ASAP;
434 urb->transfer_buffer = cam->sts_buf[i];
435 urb->complete = konicawc_isoc_irq;
436 urb->number_of_packets = FRAMES_PER_DESC;
437 urb->transfer_buffer_length = FRAMES_PER_DESC;
438 for (j=0; j < FRAMES_PER_DESC; j++) {
439 urb->iso_frame_desc[j].offset = j;
440 urb->iso_frame_desc[j].length = 1;
444 cam->last_data_urb = NULL;
446 /* Submit all URBs */
447 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
448 errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
449 if (errFlag)
450 err("usb_submit_isoc(%d) ret %d", i, errFlag);
452 errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
453 if (errFlag)
454 err ("usb_submit_isoc(%d) ret %d", i, errFlag);
457 uvd->streaming = 1;
458 DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
459 return 0;
463 static void konicawc_stop_data(struct uvd *uvd)
465 int i, j;
466 struct konicawc *cam;
468 if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
469 return;
471 konicawc_camera_off(uvd);
472 uvd->streaming = 0;
473 cam = (struct konicawc *)uvd->user_data;
474 cam->last_data_urb = NULL;
476 /* Unschedule all of the iso td's */
477 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
478 usb_kill_urb(uvd->sbuf[i].urb);
479 usb_kill_urb(cam->sts_urb[i]);
482 if (!uvd->remove_pending) {
483 /* Set packet size to 0 */
484 j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
485 if (j < 0) {
486 err("usb_set_interface() error %d.", j);
487 uvd->last_error = j;
493 static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
495 struct konicawc *cam = (struct konicawc *)uvd->user_data;
496 int maxline = cam->maxline;
497 int yplanesz = cam->yplanesz;
499 assert(frame != NULL);
501 DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
502 DEBUG(3, "Frame state = %d", frame->scanstate);
504 if(frame->scanstate == ScanState_Scanning) {
505 int drop = 0;
506 int curframe;
507 int fdrops = 0;
508 DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
509 while(RingQueue_GetLength(&uvd->dp) >= 4) {
510 if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
511 (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
512 (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
513 (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
514 curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
515 if(cam->lastframe >= 0) {
516 fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
517 fdrops--;
518 if(fdrops) {
519 info("Dropped %d frames (%d -> %d)", fdrops,
520 cam->lastframe, curframe);
523 cam->lastframe = curframe;
524 frame->curline = 0;
525 frame->scanstate = ScanState_Lines;
526 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
527 break;
529 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
530 drop++;
532 if(drop)
533 DEBUG(2, "dropped %d bytes looking for new frame", drop);
536 if(frame->scanstate == ScanState_Scanning)
537 return;
539 /* Try to move data from queue into frame buffer
540 * We get data in blocks of 384 bytes made up of:
541 * 256 Y, 64 U, 64 V.
542 * This needs to be written out as a Y plane, a U plane and a V plane.
545 while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
546 /* Y */
547 RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
548 /* U */
549 RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
550 /* V */
551 RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
552 frame->seqRead_Length += 384;
553 frame->curline++;
555 /* See if we filled the frame */
556 if (frame->curline == maxline) {
557 DEBUG(5, "got whole frame");
559 frame->frameState = FrameState_Done_Hold;
560 frame->curline = 0;
561 uvd->curframe = -1;
562 uvd->stats.frame_num++;
567 static int konicawc_find_fps(int size, int fps)
569 int i;
571 fps *= 3;
572 DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
573 if(fps <= spd_to_fps[size][0])
574 return 0;
576 if(fps >= spd_to_fps[size][MAX_SPEED])
577 return MAX_SPEED;
579 for(i = 0; i < MAX_SPEED; i++) {
580 if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
581 DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
582 if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
583 return i;
584 else
585 return i+1;
588 return MAX_SPEED+1;
592 static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
594 struct konicawc *cam = (struct konicawc *)uvd->user_data;
595 int newspeed = cam->speed;
596 int newsize;
597 int x = vw->width;
598 int y = vw->height;
599 int fps = vw->flags;
601 if(x > 0 && y > 0) {
602 DEBUG(2, "trying to find size %d,%d", x, y);
603 for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
604 if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
605 break;
607 } else {
608 newsize = cam->size;
611 if(newsize > MAX_FRAME_SIZE) {
612 DEBUG(1, "couldn't find size %d,%d", x, y);
613 return -EINVAL;
616 if(fps > 0) {
617 DEBUG(1, "trying to set fps to %d", fps);
618 newspeed = konicawc_find_fps(newsize, fps);
619 DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
622 if(newspeed > MAX_SPEED)
623 return -EINVAL;
625 DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
626 if((newsize == cam->size) && (newspeed == cam->speed)) {
627 DEBUG(1, "Nothing to do");
628 return 0;
630 DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
631 camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
633 konicawc_stop_data(uvd);
634 uvd->ifaceAltActive = spd_to_iface[newspeed];
635 DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
636 cam->speed = newspeed;
638 if(cam->size != newsize) {
639 cam->size = newsize;
640 konicawc_set_camera_size(uvd);
643 /* Flush the input queue and clear any current frame in progress */
645 RingQueue_Flush(&uvd->dp);
646 cam->lastframe = -2;
647 if(uvd->curframe != -1) {
648 uvd->frame[uvd->curframe].curline = 0;
649 uvd->frame[uvd->curframe].seqRead_Length = 0;
650 uvd->frame[uvd->curframe].seqRead_Index = 0;
653 konicawc_start_data(uvd);
654 return 0;
658 static int konicawc_calculate_fps(struct uvd *uvd)
660 struct konicawc *cam = uvd->user_data;
661 return spd_to_fps[cam->size][cam->speed]/3;
665 static void konicawc_configure_video(struct uvd *uvd)
667 struct konicawc *cam = (struct konicawc *)uvd->user_data;
668 u8 buf[2];
670 memset(&uvd->vpic, 0, sizeof(uvd->vpic));
671 memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
673 RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
674 RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
675 RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
676 RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
677 RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
679 cam->brightness = brightness / 11;
680 cam->contrast = contrast / 11;
681 cam->saturation = saturation / 11;
682 cam->sharpness = sharpness / 11;
683 cam->white_bal = whitebal / 11;
685 uvd->vpic.colour = 108;
686 uvd->vpic.hue = 108;
687 uvd->vpic.brightness = brightness;
688 uvd->vpic.contrast = contrast;
689 uvd->vpic.whiteness = whitebal;
690 uvd->vpic.depth = 6;
691 uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
693 memset(&uvd->vcap, 0, sizeof(uvd->vcap));
694 strcpy(uvd->vcap.name, "Konica Webcam");
695 uvd->vcap.type = VID_TYPE_CAPTURE;
696 uvd->vcap.channels = 1;
697 uvd->vcap.audios = 0;
698 uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
699 uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
700 uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
701 uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
703 memset(&uvd->vchan, 0, sizeof(uvd->vchan));
704 uvd->vchan.flags = 0 ;
705 uvd->vchan.tuners = 0;
706 uvd->vchan.channel = 0;
707 uvd->vchan.type = VIDEO_TYPE_CAMERA;
708 strcpy(uvd->vchan.name, "Camera");
710 /* Talk to device */
711 DEBUG(1, "device init");
712 if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
713 DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
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_set_misc(uvd, 0x2, 0, 0xd))
717 DEBUG(2, "2,0,d failed");
718 DEBUG(1, "setting initial values");
722 static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
724 struct usb_device *dev = interface_to_usbdev(intf);
725 struct uvd *uvd = NULL;
726 int ix, i, nas;
727 int actInterface=-1, inactInterface=-1, maxPS=0;
728 unsigned char video_ep = 0;
730 DEBUG(1, "konicawc_probe(%p)", intf);
732 /* We don't handle multi-config cameras */
733 if (dev->descriptor.bNumConfigurations != 1)
734 return -ENODEV;
736 info("Konica Webcam (rev. 0x%04x)", le16_to_cpu(dev->descriptor.bcdDevice));
737 RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
739 /* Validate found interface: must have one ISO endpoint */
740 nas = intf->num_altsetting;
741 if (nas != 8) {
742 err("Incorrect number of alternate settings (%d) for this camera!", nas);
743 return -ENODEV;
745 /* Validate all alternate settings */
746 for (ix=0; ix < nas; ix++) {
747 const struct usb_host_interface *interface;
748 const struct usb_endpoint_descriptor *endpoint;
750 interface = &intf->altsetting[ix];
751 i = interface->desc.bAlternateSetting;
752 if (interface->desc.bNumEndpoints != 2) {
753 err("Interface %d. has %u. endpoints!",
754 interface->desc.bInterfaceNumber,
755 (unsigned)(interface->desc.bNumEndpoints));
756 return -ENODEV;
758 endpoint = &interface->endpoint[1].desc;
759 DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
760 endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
761 if (video_ep == 0)
762 video_ep = endpoint->bEndpointAddress;
763 else if (video_ep != endpoint->bEndpointAddress) {
764 err("Alternate settings have different endpoint addresses!");
765 return -ENODEV;
767 if ((endpoint->bmAttributes & 0x03) != 0x01) {
768 err("Interface %d. has non-ISO endpoint!",
769 interface->desc.bInterfaceNumber);
770 return -ENODEV;
772 if ((endpoint->bEndpointAddress & 0x80) == 0) {
773 err("Interface %d. has ISO OUT endpoint!",
774 interface->desc.bInterfaceNumber);
775 return -ENODEV;
777 if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
778 if (inactInterface < 0)
779 inactInterface = i;
780 else {
781 err("More than one inactive alt. setting!");
782 return -ENODEV;
784 } else {
785 if (i == spd_to_iface[speed]) {
786 /* This one is the requested one */
787 actInterface = i;
790 if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
791 maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
793 if(actInterface == -1) {
794 err("Cant find required endpoint");
795 return -ENODEV;
798 DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
800 uvd = usbvideo_AllocateDevice(cams);
801 if (uvd != NULL) {
802 struct konicawc *cam = (struct konicawc *)(uvd->user_data);
803 /* Here uvd is a fully allocated uvd object */
804 for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
805 cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
806 if(cam->sts_urb[i] == NULL) {
807 while(i--) {
808 usb_free_urb(cam->sts_urb[i]);
810 err("can't allocate urbs");
811 return -ENOMEM;
814 cam->speed = speed;
815 RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
816 cam->width = camera_sizes[size].width;
817 cam->height = camera_sizes[size].height;
818 cam->size = size;
820 uvd->flags = 0;
821 uvd->debug = debug;
822 uvd->dev = dev;
823 uvd->iface = intf->altsetting->desc.bInterfaceNumber;
824 uvd->ifaceAltInactive = inactInterface;
825 uvd->ifaceAltActive = actInterface;
826 uvd->video_endp = video_ep;
827 uvd->iso_packet_len = maxPS;
828 uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
829 uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
830 uvd->canvas = VIDEOSIZE(320, 240);
831 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
833 /* Initialize konicawc specific data */
834 konicawc_configure_video(uvd);
836 i = usbvideo_RegisterVideoDevice(uvd);
837 uvd->max_frame_size = (320 * 240 * 3)/2;
838 if (i != 0) {
839 err("usbvideo_RegisterVideoDevice() failed.");
840 uvd = NULL;
842 #ifdef CONFIG_INPUT
843 /* Register input device for button */
844 memset(&cam->input, 0, sizeof(struct input_dev));
845 cam->input.name = "Konicawc snapshot button";
846 cam->input.private = cam;
847 cam->input.evbit[0] = BIT(EV_KEY);
848 cam->input.keybit[LONG(BTN_0)] = BIT(BTN_0);
849 usb_to_input_id(dev, &cam->input.id);
850 input_register_device(&cam->input);
852 usb_make_path(dev, cam->input_physname, 56);
853 strcat(cam->input_physname, "/input0");
854 cam->input.phys = cam->input_physname;
855 info("konicawc: %s on %s\n", cam->input.name, cam->input.phys);
856 #endif
859 if (uvd) {
860 usb_set_intfdata (intf, uvd);
861 return 0;
863 return -EIO;
867 static void konicawc_free_uvd(struct uvd *uvd)
869 int i;
870 struct konicawc *cam = (struct konicawc *)uvd->user_data;
872 #ifdef CONFIG_INPUT
873 input_unregister_device(&cam->input);
874 #endif
875 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
876 usb_free_urb(cam->sts_urb[i]);
877 cam->sts_urb[i] = NULL;
882 static struct usb_device_id id_table[] = {
883 { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
884 { } /* Terminating entry */
888 static int __init konicawc_init(void)
890 struct usbvideo_cb cbTbl;
891 info(DRIVER_DESC " " DRIVER_VERSION);
892 memset(&cbTbl, 0, sizeof(cbTbl));
893 cbTbl.probe = konicawc_probe;
894 cbTbl.setupOnOpen = konicawc_setup_on_open;
895 cbTbl.processData = konicawc_process_isoc;
896 cbTbl.getFPS = konicawc_calculate_fps;
897 cbTbl.setVideoMode = konicawc_set_video_mode;
898 cbTbl.startDataPump = konicawc_start_data;
899 cbTbl.stopDataPump = konicawc_stop_data;
900 cbTbl.adjustPicture = konicawc_adjust_picture;
901 cbTbl.userFree = konicawc_free_uvd;
902 return usbvideo_register(
903 &cams,
904 MAX_CAMERAS,
905 sizeof(struct konicawc),
906 "konicawc",
907 &cbTbl,
908 THIS_MODULE,
909 id_table);
913 static void __exit konicawc_cleanup(void)
915 usbvideo_Deregister(&cams);
919 MODULE_DEVICE_TABLE(usb, id_table);
921 MODULE_LICENSE("GPL");
922 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
923 MODULE_DESCRIPTION(DRIVER_DESC);
924 module_param(speed, int, 0);
925 MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
926 module_param(size, int, 0);
927 MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
928 module_param(brightness, int, 0);
929 MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
930 module_param(contrast, int, 0);
931 MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
932 module_param(saturation, int, 0);
933 MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
934 module_param(sharpness, int, 0);
935 MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
936 module_param(whitebal, int, 0);
937 MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
939 #ifdef CONFIG_USB_DEBUG
940 module_param(debug, int, S_IRUGO | S_IWUSR);
941 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
942 #endif
944 module_init(konicawc_init);
945 module_exit(konicawc_cleanup);