V4L/DVB (6006): tuner: move last_div to tuner-simple private data
[wrt350n-kernel.git] / drivers / media / video / usbvideo / konicawc.c
blob491505d6fdeea14d4c7b9934aa656666d83eaa6e
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/usb/input.h>
20 #include "usbvideo.h"
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
30 #define MAX_CAMERAS 1
32 #define DRIVER_VERSION "v1.4"
33 #define DRIVER_DESC "Konica Webcam driver"
35 enum ctrl_req {
36 SetWhitebal = 0x01,
37 SetBrightness = 0x02,
38 SetSharpness = 0x03,
39 SetContrast = 0x04,
40 SetSaturation = 0x05,
44 enum frame_sizes {
45 SIZE_160X120 = 0,
46 SIZE_160X136 = 1,
47 SIZE_176X144 = 2,
48 SIZE_320X240 = 3,
52 #define MAX_FRAME_SIZE SIZE_320X240
54 static struct usbvideo *cams;
56 #ifdef CONFIG_USB_DEBUG
57 static int debug;
58 #define DEBUG(n, format, arg...) \
59 if (n <= debug) { \
60 printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
62 #else
63 #define DEBUG(n, arg...)
64 static const int debug = 0;
65 #endif
68 /* Some default values for initial camera settings,
69 can be set by modprobe */
71 static int size;
72 static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
73 static int brightness = MAX_BRIGHTNESS/2;
74 static int contrast = MAX_CONTRAST/2;
75 static int saturation = MAX_SATURATION/2;
76 static int sharpness = MAX_SHARPNESS/2;
77 static int whitebal = 3*(MAX_WHITEBAL/4);
79 static const int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
81 /* These FPS speeds are from the windows config box. They are
82 * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
83 * real fps.
86 static const 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 } };
91 struct cam_size {
92 u16 width;
93 u16 height;
94 u8 cmd;
97 static const struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
98 { 160, 136, 0xa },
99 { 176, 144, 0x4 },
100 { 320, 240, 0x5 } };
102 struct 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;
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 char input_physname[64];
123 #endif
127 #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
128 #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
129 #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
132 static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
134 int retval = usb_control_msg(uvd->dev,
135 dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
136 request, 0x40 | dir, value, index, buf, len, 1000);
137 return retval < 0 ? retval : 0;
141 static inline void konicawc_camera_on(struct uvd *uvd)
143 DEBUG(0, "camera on");
144 konicawc_set_misc(uvd, 0x2, 1, 0x0b);
148 static inline void konicawc_camera_off(struct uvd *uvd)
150 DEBUG(0, "camera off");
151 konicawc_set_misc(uvd, 0x2, 0, 0x0b);
155 static void konicawc_set_camera_size(struct uvd *uvd)
157 struct konicawc *cam = (struct konicawc *)uvd->user_data;
159 konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
160 cam->width = camera_sizes[cam->size].width;
161 cam->height = camera_sizes[cam->size].height;
162 cam->yplanesz = cam->height * cam->width;
163 cam->cur_frame_size = (cam->yplanesz * 3) / 2;
164 cam->maxline = cam->yplanesz / 256;
165 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
169 static int konicawc_setup_on_open(struct uvd *uvd)
171 struct konicawc *cam = (struct konicawc *)uvd->user_data;
173 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
174 cam->brightness * 11);
175 konicawc_set_value(uvd, cam->brightness, SetBrightness);
176 DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
177 cam->white_bal * 11);
178 konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
179 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
180 cam->contrast * 11);
181 konicawc_set_value(uvd, cam->contrast, SetContrast);
182 DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
183 cam->saturation * 11);
184 konicawc_set_value(uvd, cam->saturation, SetSaturation);
185 DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
186 cam->sharpness * 11);
187 konicawc_set_value(uvd, cam->sharpness, SetSharpness);
188 konicawc_set_camera_size(uvd);
189 cam->lastframe = -2;
190 cam->buttonsts = 0;
191 return 0;
195 static void konicawc_adjust_picture(struct uvd *uvd)
197 struct konicawc *cam = (struct konicawc *)uvd->user_data;
199 konicawc_camera_off(uvd);
200 DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
201 uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
202 if(cam->brightness != uvd->vpic.brightness / 11) {
203 cam->brightness = uvd->vpic.brightness / 11;
204 DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
205 cam->brightness * 11);
206 konicawc_set_value(uvd, cam->brightness, SetBrightness);
209 DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
210 uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
211 if(cam->contrast != uvd->vpic.contrast / 11) {
212 cam->contrast = uvd->vpic.contrast / 11;
213 DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
214 cam->contrast * 11);
215 konicawc_set_value(uvd, cam->contrast, SetContrast);
217 konicawc_camera_on(uvd);
220 #ifdef CONFIG_INPUT
222 static void konicawc_register_input(struct konicawc *cam, struct usb_device *dev)
224 struct input_dev *input_dev;
225 int error;
227 usb_make_path(dev, cam->input_physname, sizeof(cam->input_physname));
228 strncat(cam->input_physname, "/input0", sizeof(cam->input_physname));
230 cam->input = input_dev = input_allocate_device();
231 if (!input_dev) {
232 warn("Not enough memory for camera's input device\n");
233 return;
236 input_dev->name = "Konicawc snapshot button";
237 input_dev->phys = cam->input_physname;
238 usb_to_input_id(dev, &input_dev->id);
239 input_dev->dev.parent = &dev->dev;
241 input_dev->evbit[0] = BIT(EV_KEY);
242 input_dev->keybit[LONG(BTN_0)] = BIT(BTN_0);
244 input_dev->private = cam;
246 error = input_register_device(cam->input);
247 if (error) {
248 warn("Failed to register camera's input device, err: %d\n",
249 error);
250 input_free_device(cam->input);
251 cam->input = NULL;
255 static void konicawc_unregister_input(struct konicawc *cam)
257 if (cam->input) {
258 input_unregister_device(cam->input);
259 cam->input = NULL;
263 static void konicawc_report_buttonstat(struct konicawc *cam)
265 if (cam->input) {
266 input_report_key(cam->input, BTN_0, cam->buttonsts);
267 input_sync(cam->input);
271 #else
273 static inline void konicawc_register_input(struct konicawc *cam, struct usb_device *dev) { }
274 static inline void konicawc_unregister_input(struct konicawc *cam) { }
275 static inline void konicawc_report_buttonstat(struct konicawc *cam) { }
277 #endif /* CONFIG_INPUT */
279 static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
281 char *cdata;
282 int i, totlen = 0;
283 unsigned char *status = stsurb->transfer_buffer;
284 int keep = 0, discard = 0, bad = 0;
285 struct konicawc *cam = (struct konicawc *)uvd->user_data;
287 for (i = 0; i < dataurb->number_of_packets; i++) {
288 int button = cam->buttonsts;
289 unsigned char sts;
290 int n = dataurb->iso_frame_desc[i].actual_length;
291 int st = dataurb->iso_frame_desc[i].status;
292 cdata = dataurb->transfer_buffer +
293 dataurb->iso_frame_desc[i].offset;
295 /* Detect and ignore errored packets */
296 if (st < 0) {
297 DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
298 i, n, st);
299 uvd->stats.iso_err_count++;
300 continue;
303 /* Detect and ignore empty packets */
304 if (n <= 0) {
305 uvd->stats.iso_skip_count++;
306 continue;
309 /* See what the status data said about the packet */
310 sts = *(status+stsurb->iso_frame_desc[i].offset);
312 /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
313 * otherwise:
314 * bit 0 0: keep packet
315 * 1: drop packet (padding data)
317 * bit 4 0 button not clicked
318 * 1 button clicked
319 * button is used to `take a picture' (in software)
322 if(sts < 0x80) {
323 button = !!(sts & 0x40);
324 sts &= ~0x40;
327 /* work out the button status, but don't do
328 anything with it for now */
330 if(button != cam->buttonsts) {
331 DEBUG(2, "button: %sclicked", button ? "" : "un");
332 cam->buttonsts = button;
333 konicawc_report_buttonstat(cam);
336 if(sts == 0x01) { /* drop frame */
337 discard++;
338 continue;
341 if((sts > 0x01) && (sts < 0x80)) {
342 info("unknown status %2.2x", sts);
343 bad++;
344 continue;
346 if(!sts && cam->lastframe == -2) {
347 DEBUG(2, "dropping frame looking for image start");
348 continue;
351 keep++;
352 if(sts & 0x80) { /* frame start */
353 unsigned char marker[] = { 0, 0xff, 0, 0x00 };
355 if(cam->lastframe == -2) {
356 DEBUG(2, "found initial image");
357 cam->lastframe = -1;
360 marker[3] = sts & 0x7F;
361 RingQueue_Enqueue(&uvd->dp, marker, 4);
362 totlen += 4;
365 totlen += n; /* Little local accounting */
366 RingQueue_Enqueue(&uvd->dp, cdata, n);
368 DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
369 keep, discard, bad, totlen);
370 return totlen;
374 static void resubmit_urb(struct uvd *uvd, struct urb *urb)
376 int i, ret;
377 for (i = 0; i < FRAMES_PER_DESC; i++) {
378 urb->iso_frame_desc[i].status = 0;
380 urb->dev = uvd->dev;
381 urb->status = 0;
382 ret = usb_submit_urb(urb, GFP_ATOMIC);
383 DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
384 if(ret)
385 err("usb_submit_urb error (%d)", ret);
390 static void konicawc_isoc_irq(struct urb *urb)
392 struct uvd *uvd = urb->context;
393 struct konicawc *cam = (struct konicawc *)uvd->user_data;
395 /* We don't want to do anything if we are about to be removed! */
396 if (!CAMERA_IS_OPERATIONAL(uvd))
397 return;
399 if (!uvd->streaming) {
400 DEBUG(1, "Not streaming, but interrupt!");
401 return;
404 DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
406 uvd->stats.urb_count++;
408 if (urb->transfer_buffer_length > 32) {
409 cam->last_data_urb = urb;
410 return;
412 /* Copy the data received into ring queue */
413 if(cam->last_data_urb) {
414 int len = 0;
415 if(urb->start_frame != cam->last_data_urb->start_frame)
416 err("Lost sync on frames");
417 else if (!urb->status && !cam->last_data_urb->status)
418 len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
420 resubmit_urb(uvd, cam->last_data_urb);
421 resubmit_urb(uvd, urb);
422 cam->last_data_urb = NULL;
423 uvd->stats.urb_length = len;
424 uvd->stats.data_count += len;
425 if(len)
426 RingQueue_WakeUpInterruptible(&uvd->dp);
427 return;
429 return;
433 static int konicawc_start_data(struct uvd *uvd)
435 struct usb_device *dev = uvd->dev;
436 int i, errFlag;
437 struct konicawc *cam = (struct konicawc *)uvd->user_data;
438 int pktsz;
439 struct usb_interface *intf;
440 struct usb_host_interface *interface = NULL;
442 intf = usb_ifnum_to_if(dev, uvd->iface);
443 if (intf)
444 interface = usb_altnum_to_altsetting(intf,
445 spd_to_iface[cam->speed]);
446 if (!interface)
447 return -ENXIO;
448 pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
449 DEBUG(1, "pktsz = %d", pktsz);
450 if (!CAMERA_IS_OPERATIONAL(uvd)) {
451 err("Camera is not operational");
452 return -EFAULT;
454 uvd->curframe = -1;
455 konicawc_camera_on(uvd);
456 /* Alternate interface 1 is is the biggest frame size */
457 i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
458 if (i < 0) {
459 err("usb_set_interface error");
460 uvd->last_error = i;
461 return -EBUSY;
464 /* We double buffer the Iso lists */
465 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
466 int j, k;
467 struct urb *urb = uvd->sbuf[i].urb;
468 urb->dev = dev;
469 urb->context = uvd;
470 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
471 urb->interval = 1;
472 urb->transfer_flags = URB_ISO_ASAP;
473 urb->transfer_buffer = uvd->sbuf[i].data;
474 urb->complete = konicawc_isoc_irq;
475 urb->number_of_packets = FRAMES_PER_DESC;
476 urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
477 for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
478 urb->iso_frame_desc[j].offset = k;
479 urb->iso_frame_desc[j].length = pktsz;
482 urb = cam->sts_urb[i];
483 urb->dev = dev;
484 urb->context = uvd;
485 urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
486 urb->interval = 1;
487 urb->transfer_flags = URB_ISO_ASAP;
488 urb->transfer_buffer = cam->sts_buf[i];
489 urb->complete = konicawc_isoc_irq;
490 urb->number_of_packets = FRAMES_PER_DESC;
491 urb->transfer_buffer_length = FRAMES_PER_DESC;
492 for (j=0; j < FRAMES_PER_DESC; j++) {
493 urb->iso_frame_desc[j].offset = j;
494 urb->iso_frame_desc[j].length = 1;
498 cam->last_data_urb = NULL;
500 /* Submit all URBs */
501 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
502 errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
503 if (errFlag)
504 err("usb_submit_isoc(%d) ret %d", i, errFlag);
506 errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
507 if (errFlag)
508 err ("usb_submit_isoc(%d) ret %d", i, errFlag);
511 uvd->streaming = 1;
512 DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
513 return 0;
517 static void konicawc_stop_data(struct uvd *uvd)
519 int i, j;
520 struct konicawc *cam;
522 if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
523 return;
525 konicawc_camera_off(uvd);
526 uvd->streaming = 0;
527 cam = (struct konicawc *)uvd->user_data;
528 cam->last_data_urb = NULL;
530 /* Unschedule all of the iso td's */
531 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
532 usb_kill_urb(uvd->sbuf[i].urb);
533 usb_kill_urb(cam->sts_urb[i]);
536 if (!uvd->remove_pending) {
537 /* Set packet size to 0 */
538 j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
539 if (j < 0) {
540 err("usb_set_interface() error %d.", j);
541 uvd->last_error = j;
547 static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
549 struct konicawc *cam = (struct konicawc *)uvd->user_data;
550 int maxline = cam->maxline;
551 int yplanesz = cam->yplanesz;
553 assert(frame != NULL);
555 DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
556 DEBUG(3, "Frame state = %d", frame->scanstate);
558 if(frame->scanstate == ScanState_Scanning) {
559 int drop = 0;
560 int curframe;
561 int fdrops = 0;
562 DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
563 while(RingQueue_GetLength(&uvd->dp) >= 4) {
564 if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
565 (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
566 (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
567 (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
568 curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
569 if(cam->lastframe >= 0) {
570 fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
571 fdrops--;
572 if(fdrops) {
573 info("Dropped %d frames (%d -> %d)", fdrops,
574 cam->lastframe, curframe);
577 cam->lastframe = curframe;
578 frame->curline = 0;
579 frame->scanstate = ScanState_Lines;
580 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
581 break;
583 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
584 drop++;
586 if(drop)
587 DEBUG(2, "dropped %d bytes looking for new frame", drop);
590 if(frame->scanstate == ScanState_Scanning)
591 return;
593 /* Try to move data from queue into frame buffer
594 * We get data in blocks of 384 bytes made up of:
595 * 256 Y, 64 U, 64 V.
596 * This needs to be written out as a Y plane, a U plane and a V plane.
599 while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
600 /* Y */
601 RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
602 /* U */
603 RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
604 /* V */
605 RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
606 frame->seqRead_Length += 384;
607 frame->curline++;
609 /* See if we filled the frame */
610 if (frame->curline == maxline) {
611 DEBUG(5, "got whole frame");
613 frame->frameState = FrameState_Done_Hold;
614 frame->curline = 0;
615 uvd->curframe = -1;
616 uvd->stats.frame_num++;
621 static int konicawc_find_fps(int size, int fps)
623 int i;
625 fps *= 3;
626 DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
627 if(fps <= spd_to_fps[size][0])
628 return 0;
630 if(fps >= spd_to_fps[size][MAX_SPEED])
631 return MAX_SPEED;
633 for(i = 0; i < MAX_SPEED; i++) {
634 if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
635 DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
636 if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
637 return i;
638 else
639 return i+1;
642 return MAX_SPEED+1;
646 static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
648 struct konicawc *cam = (struct konicawc *)uvd->user_data;
649 int newspeed = cam->speed;
650 int newsize;
651 int x = vw->width;
652 int y = vw->height;
653 int fps = vw->flags;
655 if(x > 0 && y > 0) {
656 DEBUG(2, "trying to find size %d,%d", x, y);
657 for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
658 if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
659 break;
661 } else {
662 newsize = cam->size;
665 if(newsize > MAX_FRAME_SIZE) {
666 DEBUG(1, "couldn't find size %d,%d", x, y);
667 return -EINVAL;
670 if(fps > 0) {
671 DEBUG(1, "trying to set fps to %d", fps);
672 newspeed = konicawc_find_fps(newsize, fps);
673 DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
676 if(newspeed > MAX_SPEED)
677 return -EINVAL;
679 DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
680 if((newsize == cam->size) && (newspeed == cam->speed)) {
681 DEBUG(1, "Nothing to do");
682 return 0;
684 DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
685 camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
687 konicawc_stop_data(uvd);
688 uvd->ifaceAltActive = spd_to_iface[newspeed];
689 DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
690 cam->speed = newspeed;
692 if(cam->size != newsize) {
693 cam->size = newsize;
694 konicawc_set_camera_size(uvd);
697 /* Flush the input queue and clear any current frame in progress */
699 RingQueue_Flush(&uvd->dp);
700 cam->lastframe = -2;
701 if(uvd->curframe != -1) {
702 uvd->frame[uvd->curframe].curline = 0;
703 uvd->frame[uvd->curframe].seqRead_Length = 0;
704 uvd->frame[uvd->curframe].seqRead_Index = 0;
707 konicawc_start_data(uvd);
708 return 0;
712 static int konicawc_calculate_fps(struct uvd *uvd)
714 struct konicawc *cam = uvd->user_data;
715 return spd_to_fps[cam->size][cam->speed]/3;
719 static void konicawc_configure_video(struct uvd *uvd)
721 struct konicawc *cam = (struct konicawc *)uvd->user_data;
722 u8 buf[2];
724 memset(&uvd->vpic, 0, sizeof(uvd->vpic));
725 memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
727 RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
728 RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
729 RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
730 RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
731 RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
733 cam->brightness = brightness / 11;
734 cam->contrast = contrast / 11;
735 cam->saturation = saturation / 11;
736 cam->sharpness = sharpness / 11;
737 cam->white_bal = whitebal / 11;
739 uvd->vpic.colour = 108;
740 uvd->vpic.hue = 108;
741 uvd->vpic.brightness = brightness;
742 uvd->vpic.contrast = contrast;
743 uvd->vpic.whiteness = whitebal;
744 uvd->vpic.depth = 6;
745 uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
747 memset(&uvd->vcap, 0, sizeof(uvd->vcap));
748 strcpy(uvd->vcap.name, "Konica Webcam");
749 uvd->vcap.type = VID_TYPE_CAPTURE;
750 uvd->vcap.channels = 1;
751 uvd->vcap.audios = 0;
752 uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
753 uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
754 uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
755 uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
757 memset(&uvd->vchan, 0, sizeof(uvd->vchan));
758 uvd->vchan.flags = 0 ;
759 uvd->vchan.tuners = 0;
760 uvd->vchan.channel = 0;
761 uvd->vchan.type = VIDEO_TYPE_CAMERA;
762 strcpy(uvd->vchan.name, "Camera");
764 /* Talk to device */
765 DEBUG(1, "device init");
766 if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
767 DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
768 if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
769 DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
770 if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
771 DEBUG(2, "2,0,d failed");
772 DEBUG(1, "setting initial values");
775 static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
777 struct usb_device *dev = interface_to_usbdev(intf);
778 struct uvd *uvd = NULL;
779 int ix, i, nas;
780 int actInterface=-1, inactInterface=-1, maxPS=0;
781 unsigned char video_ep = 0;
783 DEBUG(1, "konicawc_probe(%p)", intf);
785 /* We don't handle multi-config cameras */
786 if (dev->descriptor.bNumConfigurations != 1)
787 return -ENODEV;
789 info("Konica Webcam (rev. 0x%04x)", le16_to_cpu(dev->descriptor.bcdDevice));
790 RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
792 /* Validate found interface: must have one ISO endpoint */
793 nas = intf->num_altsetting;
794 if (nas != 8) {
795 err("Incorrect number of alternate settings (%d) for this camera!", nas);
796 return -ENODEV;
798 /* Validate all alternate settings */
799 for (ix=0; ix < nas; ix++) {
800 const struct usb_host_interface *interface;
801 const struct usb_endpoint_descriptor *endpoint;
803 interface = &intf->altsetting[ix];
804 i = interface->desc.bAlternateSetting;
805 if (interface->desc.bNumEndpoints != 2) {
806 err("Interface %d. has %u. endpoints!",
807 interface->desc.bInterfaceNumber,
808 (unsigned)(interface->desc.bNumEndpoints));
809 return -ENODEV;
811 endpoint = &interface->endpoint[1].desc;
812 DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
813 endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
814 if (video_ep == 0)
815 video_ep = endpoint->bEndpointAddress;
816 else if (video_ep != endpoint->bEndpointAddress) {
817 err("Alternate settings have different endpoint addresses!");
818 return -ENODEV;
820 if ((endpoint->bmAttributes & 0x03) != 0x01) {
821 err("Interface %d. has non-ISO endpoint!",
822 interface->desc.bInterfaceNumber);
823 return -ENODEV;
825 if ((endpoint->bEndpointAddress & 0x80) == 0) {
826 err("Interface %d. has ISO OUT endpoint!",
827 interface->desc.bInterfaceNumber);
828 return -ENODEV;
830 if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
831 if (inactInterface < 0)
832 inactInterface = i;
833 else {
834 err("More than one inactive alt. setting!");
835 return -ENODEV;
837 } else {
838 if (i == spd_to_iface[speed]) {
839 /* This one is the requested one */
840 actInterface = i;
843 if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
844 maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
846 if(actInterface == -1) {
847 err("Cant find required endpoint");
848 return -ENODEV;
851 DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
853 uvd = usbvideo_AllocateDevice(cams);
854 if (uvd != NULL) {
855 struct konicawc *cam = (struct konicawc *)(uvd->user_data);
856 /* Here uvd is a fully allocated uvd object */
857 for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
858 cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
859 if(cam->sts_urb[i] == NULL) {
860 while(i--) {
861 usb_free_urb(cam->sts_urb[i]);
863 err("can't allocate urbs");
864 return -ENOMEM;
867 cam->speed = speed;
868 RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
869 cam->width = camera_sizes[size].width;
870 cam->height = camera_sizes[size].height;
871 cam->size = size;
873 uvd->flags = 0;
874 uvd->debug = debug;
875 uvd->dev = dev;
876 uvd->iface = intf->altsetting->desc.bInterfaceNumber;
877 uvd->ifaceAltInactive = inactInterface;
878 uvd->ifaceAltActive = actInterface;
879 uvd->video_endp = video_ep;
880 uvd->iso_packet_len = maxPS;
881 uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
882 uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
883 uvd->canvas = VIDEOSIZE(320, 240);
884 uvd->videosize = VIDEOSIZE(cam->width, cam->height);
886 /* Initialize konicawc specific data */
887 konicawc_configure_video(uvd);
889 i = usbvideo_RegisterVideoDevice(uvd);
890 uvd->max_frame_size = (320 * 240 * 3)/2;
891 if (i != 0) {
892 err("usbvideo_RegisterVideoDevice() failed.");
893 uvd = NULL;
896 konicawc_register_input(cam, dev);
899 if (uvd) {
900 usb_set_intfdata (intf, uvd);
901 return 0;
903 return -EIO;
907 static void konicawc_free_uvd(struct uvd *uvd)
909 int i;
910 struct konicawc *cam = (struct konicawc *)uvd->user_data;
912 konicawc_unregister_input(cam);
914 for (i = 0; i < USBVIDEO_NUMSBUF; i++) {
915 usb_free_urb(cam->sts_urb[i]);
916 cam->sts_urb[i] = NULL;
921 static struct usb_device_id id_table[] = {
922 { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
923 { } /* Terminating entry */
927 static int __init konicawc_init(void)
929 struct usbvideo_cb cbTbl;
930 info(DRIVER_DESC " " DRIVER_VERSION);
931 memset(&cbTbl, 0, sizeof(cbTbl));
932 cbTbl.probe = konicawc_probe;
933 cbTbl.setupOnOpen = konicawc_setup_on_open;
934 cbTbl.processData = konicawc_process_isoc;
935 cbTbl.getFPS = konicawc_calculate_fps;
936 cbTbl.setVideoMode = konicawc_set_video_mode;
937 cbTbl.startDataPump = konicawc_start_data;
938 cbTbl.stopDataPump = konicawc_stop_data;
939 cbTbl.adjustPicture = konicawc_adjust_picture;
940 cbTbl.userFree = konicawc_free_uvd;
941 return usbvideo_register(
942 &cams,
943 MAX_CAMERAS,
944 sizeof(struct konicawc),
945 "konicawc",
946 &cbTbl,
947 THIS_MODULE,
948 id_table);
952 static void __exit konicawc_cleanup(void)
954 usbvideo_Deregister(&cams);
958 MODULE_DEVICE_TABLE(usb, id_table);
960 MODULE_LICENSE("GPL");
961 MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
962 MODULE_DESCRIPTION(DRIVER_DESC);
963 module_param(speed, int, 0);
964 MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
965 module_param(size, int, 0);
966 MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
967 module_param(brightness, int, 0);
968 MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
969 module_param(contrast, int, 0);
970 MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
971 module_param(saturation, int, 0);
972 MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
973 module_param(sharpness, int, 0);
974 MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
975 module_param(whitebal, int, 0);
976 MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
978 #ifdef CONFIG_USB_DEBUG
979 module_param(debug, int, S_IRUGO | S_IWUSR);
980 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
981 #endif
983 module_init(konicawc_init);
984 module_exit(konicawc_cleanup);