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