]> Pileus Git - ~andy/linux/blob - drivers/media/video/usbvideo/quickcam_messenger.c
Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/drzeus/mmc
[~andy/linux] / drivers / media / video / usbvideo / quickcam_messenger.c
1 /*
2  * Driver for Logitech Quickcam Messenger usb video camera
3  * Copyright (C) Jaya Kumar
4  *
5  * This work was sponsored by CIS(M) Sdn Bhd.
6  * History:
7  * 05/08/2006 - Jaya Kumar
8  * I wrote this based on the konicawc by Simon Evans.
9  * -
10  * Full credit for reverse engineering and creating an initial
11  * working linux driver for the VV6422 goes to the qce-ga project by
12  * Tuukka Toivonen, Jochen Hoenicke, Peter McConnell,
13  * Cristiano De Michele, Georg Acher, Jean-Frederic Clere as well as
14  * others.
15  * ---
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
29  *
30  */
31
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/input.h>
36 #include <linux/usb/input.h>
37
38 #include "usbvideo.h"
39 #include "quickcam_messenger.h"
40
41 /*
42  * Version Information
43  */
44
45 #ifdef CONFIG_USB_DEBUG
46 static int debug;
47 #define DEBUG(n, format, arg...) \
48         if (n <= debug) {        \
49                 printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
50         }
51 #else
52 #define DEBUG(n, arg...)
53 static const int debug = 0;
54 #endif
55
56 #define DRIVER_VERSION "v0.01"
57 #define DRIVER_DESC "Logitech Quickcam Messenger USB"
58
59 #define USB_LOGITECH_VENDOR_ID  0x046D
60 #define USB_QCM_PRODUCT_ID      0x08F0
61
62 #define MAX_CAMERAS     1
63
64 #define MAX_COLOUR      32768
65 #define MAX_HUE         32768
66 #define MAX_BRIGHTNESS  32768
67 #define MAX_CONTRAST    32768
68 #define MAX_WHITENESS   32768
69
70 static int size = SIZE_320X240;
71 static int colour = MAX_COLOUR;
72 static int hue = MAX_HUE;
73 static int brightness = MAX_BRIGHTNESS;
74 static int contrast =   MAX_CONTRAST;
75 static int whiteness =  MAX_WHITENESS;
76
77 static struct usbvideo *cams;
78
79 static struct usb_device_id qcm_table [] = {
80         { USB_DEVICE(USB_LOGITECH_VENDOR_ID, USB_QCM_PRODUCT_ID) },
81         { }
82 };
83 MODULE_DEVICE_TABLE(usb, qcm_table);
84
85 #ifdef CONFIG_INPUT
86 static void qcm_register_input(struct qcm *cam, struct usb_device *dev)
87 {
88         struct input_dev *input_dev;
89
90         usb_make_path(dev, cam->input_physname, sizeof(cam->input_physname));
91         strncat(cam->input_physname, "/input0", sizeof(cam->input_physname));
92
93         cam->input = input_dev = input_allocate_device();
94         if (!input_dev) {
95                 warn("insufficient mem for cam input device");
96                 return;
97         }
98
99         input_dev->name = "QCM button";
100         input_dev->phys = cam->input_physname;
101         usb_to_input_id(dev, &input_dev->id);
102         input_dev->cdev.dev = &dev->dev;
103
104         input_dev->evbit[0] = BIT(EV_KEY);
105         input_dev->keybit[LONG(BTN_0)] = BIT(BTN_0);
106
107         input_dev->private = cam;
108
109         input_register_device(cam->input);
110 }
111
112 static void qcm_unregister_input(struct qcm *cam)
113 {
114         if (cam->input) {
115                 input_unregister_device(cam->input);
116                 cam->input = NULL;
117         }
118 }
119
120 static void qcm_report_buttonstat(struct qcm *cam)
121 {
122         if (cam->input) {
123                 input_report_key(cam->input, BTN_0, cam->button_sts);
124                 input_sync(cam->input);
125         }
126 }
127
128 static void qcm_int_irq(struct urb *urb)
129 {
130         int ret;
131         struct uvd *uvd = urb->context;
132         struct qcm *cam;
133
134         if (!CAMERA_IS_OPERATIONAL(uvd))
135                 return;
136
137         if (!uvd->streaming)
138                 return;
139
140         uvd->stats.urb_count++;
141
142         if (urb->status < 0)
143                 uvd->stats.iso_err_count++;
144         else {
145                 if (urb->actual_length > 0 ) {
146                         cam = (struct qcm *) uvd->user_data;
147                         if (cam->button_sts_buf == 0x88)
148                                 cam->button_sts = 0x0;
149                         else if (cam->button_sts_buf == 0x80)
150                                 cam->button_sts = 0x1;
151                         qcm_report_buttonstat(cam);
152                 }
153         }
154
155         ret = usb_submit_urb(urb, GFP_ATOMIC);
156         if (ret < 0)
157                 err("usb_submit_urb error (%d)", ret);
158 }
159
160 static int qcm_setup_input_int(struct qcm *cam, struct uvd *uvd)
161 {
162         int errflag;
163         usb_fill_int_urb(cam->button_urb, uvd->dev,
164                         usb_rcvintpipe(uvd->dev, uvd->video_endp + 1),
165                         &cam->button_sts_buf,
166                         1,
167                         qcm_int_irq,
168                         uvd, 16);
169
170         errflag = usb_submit_urb(cam->button_urb, GFP_KERNEL);
171         if (errflag)
172                 err ("usb_submit_int ret %d", errflag);
173         return errflag;
174 }
175
176 static void qcm_stop_int_data(struct qcm *cam)
177 {
178         usb_kill_urb(cam->button_urb);
179 }
180
181 static int qcm_alloc_int_urb(struct qcm *cam)
182 {
183         cam->button_urb = usb_alloc_urb(0, GFP_KERNEL);
184
185         if (!cam->button_urb)
186                 return -ENOMEM;
187
188         return 0;
189 }
190
191 static void qcm_free_int(struct qcm *cam)
192 {
193         usb_free_urb(cam->button_urb);
194 }
195 #endif /* CONFIG_INPUT */
196
197 static int qcm_stv_setb(struct usb_device *dev, u16 reg, u8 val)
198 {
199         int ret;
200
201         /* we'll wait up to 3 slices but no more */
202         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
203                 0x04, USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
204                 reg, 0, &val, 1, 3*HZ);
205         return ret;
206 }
207
208 static int qcm_stv_setw(struct usb_device *dev, u16 reg, u16 val)
209 {
210         int ret;
211
212         /* we'll wait up to 3 slices but no more */
213         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
214                 0x04, USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
215                 reg, 0, &val, 2, 3*HZ);
216         return ret;
217 }
218
219 static int qcm_stv_getw(struct usb_device *dev, unsigned short reg,
220                                                         __le16 *val)
221 {
222         int ret;
223
224         /* we'll wait up to 3 slices but no more */
225         ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
226                 0x04, USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
227                 reg, 0, val, 2, 3*HZ);
228         return ret;
229 }
230
231 static int qcm_camera_on(struct uvd *uvd)
232 {
233         int ret;
234         CHECK_RET(ret, qcm_stv_setb(uvd->dev, STV_ISO_ENABLE, 0x01));
235         return 0;
236 }
237
238 static int qcm_camera_off(struct uvd *uvd)
239 {
240         int ret;
241         CHECK_RET(ret, qcm_stv_setb(uvd->dev, STV_ISO_ENABLE, 0x00));
242         return 0;
243 }
244
245 static void qcm_hsv2rgb(u16 hue, u16 sat, u16 val, u16 *r, u16 *g, u16 *b)
246 {
247         unsigned int segment, valsat;
248         signed int   h = (signed int) hue;
249         unsigned int s = (sat - 32768) * 2;     /* rescale */
250         unsigned int v = val;
251         unsigned int p;
252
253         /*
254         the registers controling gain are 8 bit of which
255         we affect only the last 4 bits with our gain.
256         we know that if saturation is 0, (unsaturated) then
257         we're grayscale (center axis of the colour cone) so
258         we set rgb=value. we use a formula obtained from
259         wikipedia to map the cone to the RGB plane. it's
260         as follows for the human value case of h=0..360,
261         s=0..1, v=0..1
262         h_i = h/60 % 6 , f = h/60 - h_i , p = v(1-s)
263         q = v(1 - f*s) , t = v(1 - (1-f)s)
264         h_i==0 => r=v , g=t, b=p
265         h_i==1 => r=q , g=v, b=p
266         h_i==2 => r=p , g=v, b=t
267         h_i==3 => r=p , g=q, b=v
268         h_i==4 => r=t , g=p, b=v
269         h_i==5 => r=v , g=p, b=q
270         the bottom side (the point) and the stuff just up
271         of that is black so we simplify those two cases.
272         */
273         if (sat < 32768) {
274                 /* anything less than this is unsaturated */
275                 *r = val;
276                 *g = val;
277                 *b = val;
278                 return;
279         }
280         if (val <= (0xFFFF/8)) {
281                 /* anything less than this is black */
282                 *r = 0;
283                 *g = 0;
284                 *b = 0;
285                 return;
286         }
287
288         /* the rest of this code is copying tukkat's
289         implementation of the hsv2rgb conversion as taken
290         from qc-usb-messenger code. the 10923 is 0xFFFF/6
291         to divide the cone into 6 sectors.  */
292
293         segment = (h + 10923) & 0xFFFF;
294         segment = segment*3 >> 16;              /* 0..2: 0=R, 1=G, 2=B */
295         hue -= segment * 21845;                 /* -10923..10923 */
296         h = hue;
297         h *= 3;
298         valsat = v*s >> 16;                     /* 0..65534 */
299         p = v - valsat;
300         if (h >= 0) {
301                 unsigned int t = v - (valsat * (32769 - h) >> 15);
302                 switch (segment) {
303                 case 0: /* R-> */
304                         *r = v;
305                         *g = t;
306                         *b = p;
307                         break;
308                 case 1: /* G-> */
309                         *r = p;
310                         *g = v;
311                         *b = t;
312                         break;
313                 case 2: /* B-> */
314                         *r = t;
315                         *g = p;
316                         *b = v;
317                         break;
318                 }
319         } else {
320                 unsigned int q = v - (valsat * (32769 + h) >> 15);
321                 switch (segment) {
322                 case 0: /* ->R */
323                         *r = v;
324                         *g = p;
325                         *b = q;
326                         break;
327                 case 1: /* ->G */
328                         *r = q;
329                         *g = v;
330                         *b = p;
331                         break;
332                 case 2: /* ->B */
333                         *r = p;
334                         *g = q;
335                         *b = v;
336                         break;
337                 }
338         }
339 }
340
341 static int qcm_sensor_set_gains(struct uvd *uvd, u16 hue,
342         u16 saturation, u16 value)
343 {
344         int ret;
345         u16 r=0,g=0,b=0;
346
347         /* this code is based on qc-usb-messenger */
348         qcm_hsv2rgb(hue, saturation, value, &r, &g, &b);
349
350         r >>= 12;
351         g >>= 12;
352         b >>= 12;
353
354         /* min val is 8 */
355         r = max((u16) 8, r);
356         g = max((u16) 8, g);
357         b = max((u16) 8, b);
358
359         r |= 0x30;
360         g |= 0x30;
361         b |= 0x30;
362
363         /* set the r,g,b gain registers */
364         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x0509, r));
365         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x050A, g));
366         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x050B, b));
367
368         /* doing as qc-usb did */
369         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x050C, 0x2A));
370         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x050D, 0x01));
371         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x143F, 0x01));
372
373         return 0;
374 }
375
376 static int qcm_sensor_set_exposure(struct uvd *uvd, int exposure)
377 {
378         int ret;
379         int formedval;
380
381         /* calculation was from qc-usb-messenger driver */
382         formedval = ( exposure >> 12 );
383
384         /* max value for formedval is 14 */
385         formedval = min(formedval, 14);
386
387         CHECK_RET(ret, qcm_stv_setb(uvd->dev,
388                         0x143A, 0xF0 | formedval));
389         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x143F, 0x01));
390         return 0;
391 }
392
393 static int qcm_sensor_setlevels(struct uvd *uvd, int brightness, int contrast,
394                                         int hue, int colour)
395 {
396         int ret;
397         /* brightness is exposure, contrast is gain, colour is saturation */
398         CHECK_RET(ret,
399                 qcm_sensor_set_exposure(uvd, brightness));
400         CHECK_RET(ret, qcm_sensor_set_gains(uvd, hue, colour, contrast));
401
402         return 0;
403 }
404
405 static int qcm_sensor_setsize(struct uvd *uvd, u8 size)
406 {
407         int ret;
408
409         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x1505, size));
410         return 0;
411 }
412
413 static int qcm_sensor_set_shutter(struct uvd *uvd, int whiteness)
414 {
415         int ret;
416         /* some rescaling as done by the qc-usb-messenger code */
417         if (whiteness > 0xC000)
418                 whiteness = 0xC000 + (whiteness & 0x3FFF)*8;
419
420         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x143D,
421                                 (whiteness >> 8) & 0xFF));
422         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x143E,
423                                 (whiteness >> 16) & 0x03));
424         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x143F, 0x01));
425
426         return 0;
427 }
428
429 static int qcm_sensor_init(struct uvd *uvd)
430 {
431         struct qcm *cam = (struct qcm *) uvd->user_data;
432         int ret;
433         int i;
434
435         for (i=0; i < sizeof(regval_table)/sizeof(regval_table[0]) ; i++) {
436                 CHECK_RET(ret, qcm_stv_setb(uvd->dev,
437                                         regval_table[i].reg,
438                                         regval_table[i].val));
439         }
440
441         CHECK_RET(ret, qcm_stv_setw(uvd->dev, 0x15c1,
442                                 cpu_to_le16(ISOC_PACKET_SIZE)));
443         CHECK_RET(ret, qcm_stv_setb(uvd->dev, 0x15c3, 0x08));
444         CHECK_RET(ret, ret = qcm_stv_setb(uvd->dev, 0x143f, 0x01));
445
446         CHECK_RET(ret, qcm_stv_setb(uvd->dev, STV_ISO_ENABLE, 0x00));
447
448         CHECK_RET(ret, qcm_sensor_setsize(uvd, camera_sizes[cam->size].cmd));
449
450         CHECK_RET(ret, qcm_sensor_setlevels(uvd, uvd->vpic.brightness,
451                         uvd->vpic.contrast, uvd->vpic.hue, uvd->vpic.colour));
452
453         CHECK_RET(ret, qcm_sensor_set_shutter(uvd, uvd->vpic.whiteness));
454         CHECK_RET(ret, qcm_sensor_setsize(uvd, camera_sizes[cam->size].cmd));
455
456         return 0;
457 }
458
459 static int qcm_set_camera_size(struct uvd *uvd)
460 {
461         int ret;
462         struct qcm *cam = (struct qcm *) uvd->user_data;
463
464         CHECK_RET(ret, qcm_sensor_setsize(uvd, camera_sizes[cam->size].cmd));
465         cam->width = camera_sizes[cam->size].width;
466         cam->height = camera_sizes[cam->size].height;
467         uvd->videosize = VIDEOSIZE(cam->width, cam->height);
468
469         return 0;
470 }
471
472 static int qcm_setup_on_open(struct uvd *uvd)
473 {
474         int ret;
475
476         CHECK_RET(ret, qcm_sensor_set_gains(uvd, uvd->vpic.hue,
477                                 uvd->vpic.colour, uvd->vpic.contrast));
478         CHECK_RET(ret, qcm_sensor_set_exposure(uvd, uvd->vpic.brightness));
479         CHECK_RET(ret, qcm_sensor_set_shutter(uvd, uvd->vpic.whiteness));
480         CHECK_RET(ret, qcm_set_camera_size(uvd));
481         CHECK_RET(ret, qcm_camera_on(uvd));
482         return 0;
483 }
484
485 static void qcm_adjust_picture(struct uvd *uvd)
486 {
487         int ret;
488         struct qcm *cam = (struct qcm *) uvd->user_data;
489
490         ret = qcm_camera_off(uvd);
491         if (ret) {
492                 err("can't turn camera off. abandoning pic adjustment");
493                 return;
494         }
495
496         /* if there's been a change in contrast, hue, or
497         colour then we need to recalculate hsv in order
498         to update gains */
499         if ((cam->contrast != uvd->vpic.contrast) ||
500                 (cam->hue != uvd->vpic.hue) ||
501                 (cam->colour != uvd->vpic.colour)) {
502                 cam->contrast = uvd->vpic.contrast;
503                 cam->hue = uvd->vpic.hue;
504                 cam->colour = uvd->vpic.colour;
505                 ret = qcm_sensor_set_gains(uvd, cam->hue, cam->colour,
506                                                 cam->contrast);
507                 if (ret) {
508                         err("can't set gains. abandoning pic adjustment");
509                         return;
510                 }
511         }
512
513         if (cam->brightness != uvd->vpic.brightness) {
514                 cam->brightness = uvd->vpic.brightness;
515                 ret = qcm_sensor_set_exposure(uvd, cam->brightness);
516                 if (ret) {
517                         err("can't set exposure. abandoning pic adjustment");
518                         return;
519                 }
520         }
521
522         if (cam->whiteness != uvd->vpic.whiteness) {
523                 cam->whiteness = uvd->vpic.whiteness;
524                 qcm_sensor_set_shutter(uvd, cam->whiteness);
525                 if (ret) {
526                         err("can't set shutter. abandoning pic adjustment");
527                         return;
528                 }
529         }
530
531         ret = qcm_camera_on(uvd);
532         if (ret) {
533                 err("can't reenable camera. pic adjustment failed");
534                 return;
535         }
536 }
537
538 static int qcm_process_frame(struct uvd *uvd, u8 *cdata, int framelen)
539 {
540         int datalen;
541         int totaldata;
542         struct framehdr {
543                 __be16 id;
544                 __be16 len;
545         };
546         struct framehdr *fhdr;
547
548         totaldata = 0;
549         while (framelen) {
550                 fhdr = (struct framehdr *) cdata;
551                 datalen = be16_to_cpu(fhdr->len);
552                 framelen -= 4;
553                 cdata += 4;
554
555                 if ((fhdr->id) == cpu_to_be16(0x8001)) {
556                         RingQueue_Enqueue(&uvd->dp, marker, 4);
557                         totaldata += 4;
558                         continue;
559                 }
560                 if ((fhdr->id & cpu_to_be16(0xFF00)) == cpu_to_be16(0x0200)) {
561                         RingQueue_Enqueue(&uvd->dp, cdata, datalen);
562                         totaldata += datalen;
563                 }
564                 framelen -= datalen;
565                 cdata += datalen;
566         }
567         return totaldata;
568 }
569
570 static int qcm_compress_iso(struct uvd *uvd, struct urb *dataurb)
571 {
572         int totlen;
573         int i;
574         unsigned char *cdata;
575
576         totlen=0;
577         for (i = 0; i < dataurb->number_of_packets; i++) {
578                 int n = dataurb->iso_frame_desc[i].actual_length;
579                 int st = dataurb->iso_frame_desc[i].status;
580
581                 cdata = dataurb->transfer_buffer +
582                         dataurb->iso_frame_desc[i].offset;
583
584                 if (st < 0) {
585                         warn("Data error: packet=%d. len=%d. status=%d.",
586                               i, n, st);
587                         uvd->stats.iso_err_count++;
588                         continue;
589                 }
590                 if (!n)
591                         continue;
592
593                 totlen += qcm_process_frame(uvd, cdata, n);
594         }
595         return totlen;
596 }
597
598 static void resubmit_urb(struct uvd *uvd, struct urb *urb)
599 {
600         int ret;
601
602         urb->dev = uvd->dev;
603         ret = usb_submit_urb(urb, GFP_ATOMIC);
604         if (ret)
605                 err("usb_submit_urb error (%d)", ret);
606 }
607
608 static void qcm_isoc_irq(struct urb *urb)
609 {
610         int len;
611         struct uvd *uvd = urb->context;
612
613         if (!CAMERA_IS_OPERATIONAL(uvd))
614                 return;
615
616         if (!uvd->streaming)
617                 return;
618
619         uvd->stats.urb_count++;
620
621         if (!urb->actual_length) {
622                 resubmit_urb(uvd, urb);
623                 return;
624         }
625
626         len = qcm_compress_iso(uvd, urb);
627         resubmit_urb(uvd, urb);
628         uvd->stats.urb_length = len;
629         uvd->stats.data_count += len;
630         if (len)
631                 RingQueue_WakeUpInterruptible(&uvd->dp);
632 }
633
634 static int qcm_start_data(struct uvd *uvd)
635 {
636         struct qcm *cam = (struct qcm *) uvd->user_data;
637         int i;
638         int errflag;
639         int pktsz;
640         int err;
641
642         pktsz = uvd->iso_packet_len;
643         if (!CAMERA_IS_OPERATIONAL(uvd)) {
644                 err("Camera is not operational");
645                 return -EFAULT;
646         }
647
648         err = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltActive);
649         if (err < 0) {
650                 err("usb_set_interface error");
651                 uvd->last_error = err;
652                 return -EBUSY;
653         }
654
655         for (i=0; i < USBVIDEO_NUMSBUF; i++) {
656                 int j, k;
657                 struct urb *urb = uvd->sbuf[i].urb;
658                 urb->dev = uvd->dev;
659                 urb->context = uvd;
660                 urb->pipe = usb_rcvisocpipe(uvd->dev, uvd->video_endp);
661                 urb->interval = 1;
662                 urb->transfer_flags = URB_ISO_ASAP;
663                 urb->transfer_buffer = uvd->sbuf[i].data;
664                 urb->complete = qcm_isoc_irq;
665                 urb->number_of_packets = FRAMES_PER_DESC;
666                 urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
667                 for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
668                         urb->iso_frame_desc[j].offset = k;
669                         urb->iso_frame_desc[j].length = pktsz;
670                 }
671         }
672
673         uvd->streaming = 1;
674         uvd->curframe = -1;
675         for (i=0; i < USBVIDEO_NUMSBUF; i++) {
676                 errflag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
677                 if (errflag)
678                         err ("usb_submit_isoc(%d) ret %d", i, errflag);
679         }
680
681         CHECK_RET(err, qcm_setup_input_int(cam, uvd));
682         CHECK_RET(err, qcm_camera_on(uvd));
683         return 0;
684 }
685
686 static void qcm_stop_data(struct uvd *uvd)
687 {
688         struct qcm *cam = (struct qcm *) uvd->user_data;
689         int i, j;
690         int ret;
691
692         if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
693                 return;
694
695         ret = qcm_camera_off(uvd);
696         if (ret)
697                 warn("couldn't turn the cam off.");
698
699         uvd->streaming = 0;
700
701         /* Unschedule all of the iso td's */
702         for (i=0; i < USBVIDEO_NUMSBUF; i++)
703                 usb_kill_urb(uvd->sbuf[i].urb);
704
705         qcm_stop_int_data(cam);
706
707         if (!uvd->remove_pending) {
708                 /* Set packet size to 0 */
709                 j = usb_set_interface(uvd->dev, uvd->iface,
710                                         uvd->ifaceAltInactive);
711                 if (j < 0) {
712                         err("usb_set_interface() error %d.", j);
713                         uvd->last_error = j;
714                 }
715         }
716 }
717
718 static void qcm_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
719 {
720         struct qcm *cam = (struct qcm *) uvd->user_data;
721         int x;
722         struct rgb *rgbL0;
723         struct rgb *rgbL1;
724         struct bayL0 *bayL0;
725         struct bayL1 *bayL1;
726         int hor,ver,hordel,verdel;
727         assert(frame != NULL);
728
729         switch (cam->size) {
730         case SIZE_160X120:
731                 hor = 162; ver = 124; hordel = 1; verdel = 2;
732                 break;
733         case SIZE_320X240:
734         default:
735                 hor = 324; ver = 248; hordel = 2; verdel = 4;
736                 break;
737         }
738
739         if (frame->scanstate == ScanState_Scanning) {
740                 while (RingQueue_GetLength(&uvd->dp) >=
741                          4 + (hor*verdel + hordel)) {
742                         if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
743                             (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
744                             (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
745                             (RING_QUEUE_PEEK(&uvd->dp, 3) == 0xff)) {
746                                 frame->curline = 0;
747                                 frame->scanstate = ScanState_Lines;
748                                 frame->frameState = FrameState_Grabbing;
749                                 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
750                         /*
751                         * if we're starting, we need to discard the first
752                         * 4 lines of y bayer data
753                         * and the first 2 gr elements of x bayer data
754                         */
755                                 RING_QUEUE_DEQUEUE_BYTES(&uvd->dp,
756                                                         (hor*verdel + hordel));
757                                 break;
758                         }
759                         RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
760                 }
761         }
762
763         if (frame->scanstate == ScanState_Scanning)
764                 return;
765
766         /* now we can start processing bayer data so long as we have at least
767         * 2 lines worth of data. this is the simplest demosaicing method that
768         * I could think of. I use each 2x2 bayer element without interpolation
769         * to generate 4 rgb pixels.
770         */
771         while ( frame->curline < cam->height &&
772                 (RingQueue_GetLength(&uvd->dp) >= hor*2)) {
773                 /* get 2 lines of bayer for demosaicing
774                  * into 2 lines of RGB */
775                 RingQueue_Dequeue(&uvd->dp, cam->scratch, hor*2);
776                 bayL0 = (struct bayL0 *) cam->scratch;
777                 bayL1 = (struct bayL1 *) (cam->scratch + hor);
778                 /* frame->curline is the rgb y line */
779                 rgbL0 = (struct rgb *)
780                                 ( frame->data + (cam->width*3*frame->curline));
781                 /* w/2 because we're already doing 2 pixels */
782                 rgbL1 = rgbL0 + (cam->width/2);
783
784                 for (x=0; x < cam->width; x+=2) {
785                         rgbL0->r = bayL0->r;
786                         rgbL0->g = bayL0->g;
787                         rgbL0->b = bayL1->b;
788
789                         rgbL0->r2 = bayL0->r;
790                         rgbL0->g2 = bayL1->g;
791                         rgbL0->b2 = bayL1->b;
792
793                         rgbL1->r = bayL0->r;
794                         rgbL1->g = bayL1->g;
795                         rgbL1->b = bayL1->b;
796
797                         rgbL1->r2 = bayL0->r;
798                         rgbL1->g2 = bayL1->g;
799                         rgbL1->b2 = bayL1->b;
800
801                         rgbL0++;
802                         rgbL1++;
803
804                         bayL0++;
805                         bayL1++;
806                 }
807
808                 frame->seqRead_Length += cam->width*3*2;
809                 frame->curline += 2;
810         }
811         /* See if we filled the frame */
812         if (frame->curline == cam->height) {
813                 frame->frameState = FrameState_Done_Hold;
814                 frame->curline = 0;
815                 uvd->curframe = -1;
816                 uvd->stats.frame_num++;
817         }
818 }
819
820 /* taken from konicawc */
821 static int qcm_set_video_mode(struct uvd *uvd, struct video_window *vw)
822 {
823         int ret;
824         int newsize;
825         int oldsize;
826         int x = vw->width;
827         int y = vw->height;
828         struct qcm *cam = (struct qcm *) uvd->user_data;
829
830         if (x > 0 && y > 0) {
831                 DEBUG(2, "trying to find size %d,%d", x, y);
832                 for (newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
833                         if ((camera_sizes[newsize].width == x) &&
834                                 (camera_sizes[newsize].height == y))
835                                 break;
836                 }
837         } else
838                 newsize = cam->size;
839
840         if (newsize > MAX_FRAME_SIZE) {
841                 DEBUG(1, "couldn't find size %d,%d", x, y);
842                 return -EINVAL;
843         }
844
845         if (newsize == cam->size) {
846                 DEBUG(1, "Nothing to do");
847                 return 0;
848         }
849
850         qcm_stop_data(uvd);
851
852         if (cam->size != newsize) {
853                 oldsize = cam->size;
854                 cam->size = newsize;
855                 ret = qcm_set_camera_size(uvd);
856                 if (ret) {
857                         err("Couldn't set camera size, err=%d",ret);
858                         /* restore the original size */
859                         cam->size = oldsize;
860                         return ret;
861                 }
862         }
863
864         /* Flush the input queue and clear any current frame in progress */
865
866         RingQueue_Flush(&uvd->dp);
867         if (uvd->curframe != -1) {
868                 uvd->frame[uvd->curframe].curline = 0;
869                 uvd->frame[uvd->curframe].seqRead_Length = 0;
870                 uvd->frame[uvd->curframe].seqRead_Index = 0;
871         }
872
873         CHECK_RET(ret, qcm_start_data(uvd));
874         return 0;
875 }
876
877 static int qcm_configure_video(struct uvd *uvd)
878 {
879         int ret;
880         memset(&uvd->vpic, 0, sizeof(uvd->vpic));
881         memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
882
883         uvd->vpic.colour = colour;
884         uvd->vpic.hue = hue;
885         uvd->vpic.brightness = brightness;
886         uvd->vpic.contrast = contrast;
887         uvd->vpic.whiteness = whiteness;
888         uvd->vpic.depth = 24;
889         uvd->vpic.palette = VIDEO_PALETTE_RGB24;
890
891         memset(&uvd->vcap, 0, sizeof(uvd->vcap));
892         strcpy(uvd->vcap.name, "QCM USB Camera");
893         uvd->vcap.type = VID_TYPE_CAPTURE;
894         uvd->vcap.channels = 1;
895         uvd->vcap.audios = 0;
896
897         uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
898         uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
899         uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
900         uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
901
902         memset(&uvd->vchan, 0, sizeof(uvd->vchan));
903         uvd->vchan.flags = 0 ;
904         uvd->vchan.tuners = 0;
905         uvd->vchan.channel = 0;
906         uvd->vchan.type = VIDEO_TYPE_CAMERA;
907         strcpy(uvd->vchan.name, "Camera");
908
909         CHECK_RET(ret, qcm_sensor_init(uvd));
910         return 0;
911 }
912
913 static int qcm_probe(struct usb_interface *intf,
914                         const struct usb_device_id *devid)
915 {
916         int err;
917         struct uvd *uvd;
918         struct usb_device *dev = interface_to_usbdev(intf);
919         struct qcm *cam;
920         size_t buffer_size;
921         unsigned char video_ep;
922         struct usb_host_interface *interface;
923         struct usb_endpoint_descriptor *endpoint;
924         int i,j;
925         unsigned int ifacenum, ifacenum_inact=0;
926         __le16 sensor_id;
927
928         /* we don't support multiconfig cams */
929         if (dev->descriptor.bNumConfigurations != 1)
930                 return -ENODEV;
931
932         /* first check for the video interface and not
933         * the audio interface */
934         interface = &intf->cur_altsetting[0];
935         if ((interface->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
936                 || (interface->desc.bInterfaceSubClass !=
937                         USB_CLASS_VENDOR_SPEC))
938                 return -ENODEV;
939
940         /*
941         walk through each endpoint in each setting in the interface
942         stop when we find the one that's an isochronous IN endpoint.
943         */
944         for (i=0; i < intf->num_altsetting; i++) {
945                 interface = &intf->cur_altsetting[i];
946                 ifacenum = interface->desc.bAlternateSetting;
947                 /* walk the end points */
948                 for (j=0; j < interface->desc.bNumEndpoints; j++) {
949                         endpoint = &interface->endpoint[j].desc;
950
951                         if ((endpoint->bEndpointAddress &
952                                 USB_ENDPOINT_DIR_MASK) != USB_DIR_IN)
953                                 continue; /* not input then not good */
954
955                         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
956                         if (!buffer_size) {
957                                 ifacenum_inact = ifacenum;
958                                 continue; /* 0 pkt size is not what we want */
959                         }
960
961                         if ((endpoint->bmAttributes &
962                                 USB_ENDPOINT_XFERTYPE_MASK) ==
963                                 USB_ENDPOINT_XFER_ISOC) {
964                                 video_ep = endpoint->bEndpointAddress;
965                                 /* break out of the search */
966                                 goto good_videoep;
967                         }
968                 }
969         }
970         /* failed out since nothing useful was found */
971         err("No suitable endpoint was found\n");
972         return -ENODEV;
973
974 good_videoep:
975         /* disable isochronous stream before doing anything else */
976         err = qcm_stv_setb(dev, STV_ISO_ENABLE, 0);
977         if (err < 0) {
978                 err("Failed to disable sensor stream");
979                 return -EIO;
980         }
981
982         /*
983         Check that this is the same unknown sensor that is known to work. This
984         sensor is suspected to be the ST VV6422C001. I'll check the same value
985         that the qc-usb driver checks. This value is probably not even the
986         sensor ID since it matches the USB dev ID. Oh well. If it doesn't
987         match, it's probably a diff sensor so exit and apologize.
988         */
989         err = qcm_stv_getw(dev, CMOS_SENSOR_IDREV, &sensor_id);
990         if (err < 0) {
991                 err("Couldn't read sensor values. Err %d\n",err);
992                 return err;
993         }
994         if (sensor_id != cpu_to_le16(0x08F0)) {
995                 err("Sensor ID %x != %x. Unsupported. Sorry\n",
996                         le16_to_cpu(sensor_id), (0x08F0));
997                 return -ENODEV;
998         }
999
1000         uvd = usbvideo_AllocateDevice(cams);
1001         if (!uvd)
1002                 return -ENOMEM;
1003
1004         cam = (struct qcm *) uvd->user_data;
1005
1006         /* buf for doing demosaicing */
1007         cam->scratch = kmalloc(324*2, GFP_KERNEL);
1008         if (!cam->scratch) /* uvd freed in dereg */
1009                 return -ENOMEM;
1010
1011         /* yes, if we fail after here, cam->scratch gets freed
1012         by qcm_free_uvd */
1013
1014         err = qcm_alloc_int_urb(cam);
1015         if (err < 0)
1016                 return err;
1017
1018         /* yes, if we fail after here, int urb gets freed
1019         by qcm_free_uvd */
1020
1021         RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
1022         cam->width = camera_sizes[size].width;
1023         cam->height = camera_sizes[size].height;
1024         cam->size = size;
1025
1026         uvd->debug = debug;
1027         uvd->flags = 0;
1028         uvd->dev = dev;
1029         uvd->iface = intf->altsetting->desc.bInterfaceNumber;
1030         uvd->ifaceAltActive = ifacenum;
1031         uvd->ifaceAltInactive = ifacenum_inact;
1032         uvd->video_endp = video_ep;
1033         uvd->iso_packet_len = buffer_size;
1034         uvd->paletteBits = 1L << VIDEO_PALETTE_RGB24;
1035         uvd->defaultPalette = VIDEO_PALETTE_RGB24;
1036         uvd->canvas = VIDEOSIZE(320, 240);
1037         uvd->videosize = VIDEOSIZE(cam->width, cam->height);
1038         err = qcm_configure_video(uvd);
1039         if (err) {
1040                 err("failed to configure video settings");
1041                 return err;
1042         }
1043
1044         err = usbvideo_RegisterVideoDevice(uvd);
1045         if (err) { /* the uvd gets freed in Deregister */
1046                 err("usbvideo_RegisterVideoDevice() failed.");
1047                 return err;
1048         }
1049
1050         uvd->max_frame_size = (320 * 240 * 3);
1051         qcm_register_input(cam, dev);
1052         usb_set_intfdata(intf, uvd);
1053         return 0;
1054 }
1055
1056 static void qcm_free_uvd(struct uvd *uvd)
1057 {
1058         struct qcm *cam = (struct qcm *) uvd->user_data;
1059
1060         kfree(cam->scratch);
1061         qcm_unregister_input(cam);
1062         qcm_free_int(cam);
1063 }
1064
1065 static struct usbvideo_cb qcm_driver = {
1066         .probe =                qcm_probe,
1067         .setupOnOpen =          qcm_setup_on_open,
1068         .processData =          qcm_process_isoc,
1069         .setVideoMode =         qcm_set_video_mode,
1070         .startDataPump =        qcm_start_data,
1071         .stopDataPump =         qcm_stop_data,
1072         .adjustPicture =        qcm_adjust_picture,
1073         .userFree =             qcm_free_uvd
1074 };
1075
1076 static int __init qcm_init(void)
1077 {
1078         info(DRIVER_DESC " " DRIVER_VERSION);
1079
1080         return usbvideo_register(
1081                 &cams,
1082                 MAX_CAMERAS,
1083                 sizeof(struct qcm),
1084                 "QCM",
1085                 &qcm_driver,
1086                 THIS_MODULE,
1087                 qcm_table);
1088 }
1089
1090 static void __exit qcm_exit(void)
1091 {
1092         usbvideo_Deregister(&cams);
1093 }
1094
1095 module_param(size, int, 0);
1096 MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 320x240");
1097 module_param(colour, int, 0);
1098 MODULE_PARM_DESC(colour, "Initial colour");
1099 module_param(hue, int, 0);
1100 MODULE_PARM_DESC(hue, "Initial hue");
1101 module_param(brightness, int, 0);
1102 MODULE_PARM_DESC(brightness, "Initial brightness");
1103 module_param(contrast, int, 0);
1104 MODULE_PARM_DESC(contrast, "Initial contrast");
1105 module_param(whiteness, int, 0);
1106 MODULE_PARM_DESC(whiteness, "Initial whiteness");
1107
1108 #ifdef CONFIG_USB_DEBUG
1109 module_param(debug, int, S_IRUGO | S_IWUSR);
1110 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
1111 #endif
1112
1113 module_init(qcm_init);
1114 module_exit(qcm_exit);
1115
1116 MODULE_LICENSE("GPL");
1117 MODULE_AUTHOR("Jaya Kumar");
1118 MODULE_DESCRIPTION("QCM USB Camera");
1119 MODULE_SUPPORTED_DEVICE("QCM USB Camera");