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