Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6.git] / drivers / media / video / gspca / se401.c
1 /*
2  * GSPCA Endpoints (formerly known as AOX) se401 USB Camera sub Driver
3  *
4  * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
5  *
6  * Based on the v4l1 se401 driver which is:
7  *
8  * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  */
25
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #define MODULE_NAME "se401"
29
30 #define BULK_SIZE 4096
31 #define PACKET_SIZE 1024
32 #define READ_REQ_SIZE 64
33 #define MAX_MODES ((READ_REQ_SIZE - 6) / 4)
34 /* The se401 compression algorithm uses a fixed quant factor, which
35    can be configured by setting the high nibble of the SE401_OPERATINGMODE
36    feature. This needs to exactly match what is in libv4l! */
37 #define SE401_QUANT_FACT 8
38
39 #include <linux/input.h>
40 #include <linux/slab.h>
41 #include "gspca.h"
42 #include "se401.h"
43
44 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
45 MODULE_DESCRIPTION("Endpoints se401");
46 MODULE_LICENSE("GPL");
47
48 /* controls */
49 enum e_ctrl {
50         BRIGHTNESS,
51         GAIN,
52         EXPOSURE,
53         FREQ,
54         NCTRL   /* number of controls */
55 };
56
57 /* exposure change state machine states */
58 enum {
59         EXPO_CHANGED,
60         EXPO_DROP_FRAME,
61         EXPO_NO_CHANGE,
62 };
63
64 /* specific webcam descriptor */
65 struct sd {
66         struct gspca_dev gspca_dev;     /* !! must be the first item */
67         struct gspca_ctrl ctrls[NCTRL];
68         struct v4l2_pix_format fmts[MAX_MODES];
69         int pixels_read;
70         int packet_read;
71         u8 packet[PACKET_SIZE];
72         u8 restart_stream;
73         u8 button_state;
74         u8 resetlevel;
75         u8 resetlevel_frame_count;
76         int resetlevel_adjust_dir;
77         int expo_change_state;
78 };
79
80 static void setbrightness(struct gspca_dev *gspca_dev);
81 static void setgain(struct gspca_dev *gspca_dev);
82 static void setexposure(struct gspca_dev *gspca_dev);
83
84 static const struct ctrl sd_ctrls[NCTRL] = {
85 [BRIGHTNESS] = {
86                 {
87                         .id      = V4L2_CID_BRIGHTNESS,
88                         .type    = V4L2_CTRL_TYPE_INTEGER,
89                         .name    = "Brightness",
90                         .minimum = 0,
91                         .maximum = 255,
92                         .step    = 1,
93                         .default_value = 15,
94                 },
95                 .set_control = setbrightness
96         },
97 [GAIN] = {
98                 {
99                         .id      = V4L2_CID_GAIN,
100                         .type    = V4L2_CTRL_TYPE_INTEGER,
101                         .name    = "Gain",
102                         .minimum = 0,
103                         .maximum = 50, /* Really 63 but > 50 is not pretty */
104                         .step    = 1,
105                         .default_value = 25,
106                 },
107                 .set_control = setgain
108         },
109 [EXPOSURE] = {
110                 {
111                         .id = V4L2_CID_EXPOSURE,
112                         .type = V4L2_CTRL_TYPE_INTEGER,
113                         .name = "Exposure",
114                         .minimum = 0,
115                         .maximum = 32767,
116                         .step = 1,
117                         .default_value = 15000,
118                 },
119                 .set_control = setexposure
120         },
121 [FREQ] = {
122                 {
123                         .id      = V4L2_CID_POWER_LINE_FREQUENCY,
124                         .type    = V4L2_CTRL_TYPE_MENU,
125                         .name    = "Light frequency filter",
126                         .minimum = 0,
127                         .maximum = 2,
128                         .step    = 1,
129                         .default_value = 0,
130                 },
131                 .set_control = setexposure
132         },
133 };
134
135 static void se401_write_req(struct gspca_dev *gspca_dev, u16 req, u16 value,
136                             int silent)
137 {
138         int err;
139
140         if (gspca_dev->usb_err < 0)
141                 return;
142
143         err = usb_control_msg(gspca_dev->dev,
144                               usb_sndctrlpipe(gspca_dev->dev, 0), req,
145                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
146                               value, 0, NULL, 0, 1000);
147         if (err < 0) {
148                 if (!silent)
149                         pr_err("write req failed req %#04x val %#04x error %d\n",
150                                req, value, err);
151                 gspca_dev->usb_err = err;
152         }
153 }
154
155 static void se401_read_req(struct gspca_dev *gspca_dev, u16 req, int silent)
156 {
157         int err;
158
159         if (gspca_dev->usb_err < 0)
160                 return;
161
162         if (USB_BUF_SZ < READ_REQ_SIZE) {
163                 pr_err("USB_BUF_SZ too small!!\n");
164                 gspca_dev->usb_err = -ENOBUFS;
165                 return;
166         }
167
168         err = usb_control_msg(gspca_dev->dev,
169                               usb_rcvctrlpipe(gspca_dev->dev, 0), req,
170                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
171                               0, 0, gspca_dev->usb_buf, READ_REQ_SIZE, 1000);
172         if (err < 0) {
173                 if (!silent)
174                         pr_err("read req failed req %#04x error %d\n",
175                                req, err);
176                 gspca_dev->usb_err = err;
177         }
178 }
179
180 static void se401_set_feature(struct gspca_dev *gspca_dev,
181                               u16 selector, u16 param)
182 {
183         int err;
184
185         if (gspca_dev->usb_err < 0)
186                 return;
187
188         err = usb_control_msg(gspca_dev->dev,
189                               usb_sndctrlpipe(gspca_dev->dev, 0),
190                               SE401_REQ_SET_EXT_FEATURE,
191                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
192                               param, selector, NULL, 0, 1000);
193         if (err < 0) {
194                 pr_err("set feature failed sel %#04x param %#04x error %d\n",
195                        selector, param, err);
196                 gspca_dev->usb_err = err;
197         }
198 }
199
200 static int se401_get_feature(struct gspca_dev *gspca_dev, u16 selector)
201 {
202         int err;
203
204         if (gspca_dev->usb_err < 0)
205                 return gspca_dev->usb_err;
206
207         if (USB_BUF_SZ < 2) {
208                 pr_err("USB_BUF_SZ too small!!\n");
209                 gspca_dev->usb_err = -ENOBUFS;
210                 return gspca_dev->usb_err;
211         }
212
213         err = usb_control_msg(gspca_dev->dev,
214                               usb_rcvctrlpipe(gspca_dev->dev, 0),
215                               SE401_REQ_GET_EXT_FEATURE,
216                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
217                               0, selector, gspca_dev->usb_buf, 2, 1000);
218         if (err < 0) {
219                 pr_err("get feature failed sel %#04x error %d\n",
220                        selector, err);
221                 gspca_dev->usb_err = err;
222                 return err;
223         }
224         return gspca_dev->usb_buf[0] | (gspca_dev->usb_buf[1] << 8);
225 }
226
227 static void setbrightness(struct gspca_dev *gspca_dev)
228 {
229         struct sd *sd = (struct sd *) gspca_dev;
230
231         if (gspca_dev->ctrl_dis & (1 << BRIGHTNESS))
232                 return;
233
234         /* HDG: this does not seem to do anything on my cam */
235         se401_write_req(gspca_dev, SE401_REQ_SET_BRT,
236                         sd->ctrls[BRIGHTNESS].val, 0);
237 }
238
239 static void setgain(struct gspca_dev *gspca_dev)
240 {
241         struct sd *sd = (struct sd *) gspca_dev;
242         u16 gain = 63 - sd->ctrls[GAIN].val;
243
244         /* red color gain */
245         se401_set_feature(gspca_dev, HV7131_REG_ARCG, gain);
246         /* green color gain */
247         se401_set_feature(gspca_dev, HV7131_REG_AGCG, gain);
248         /* blue color gain */
249         se401_set_feature(gspca_dev, HV7131_REG_ABCG, gain);
250 }
251
252 static void setexposure(struct gspca_dev *gspca_dev)
253 {
254         struct sd *sd = (struct sd *) gspca_dev;
255         int integration = sd->ctrls[EXPOSURE].val << 6;
256         u8 expose_h, expose_m, expose_l;
257
258         /* Do this before the set_feature calls, for proper timing wrt
259            the interrupt driven pkt_scan. Note we may still race but that
260            is not a big issue, the expo change state machine is merely for
261            avoiding underexposed frames getting send out, if one sneaks
262            through so be it */
263         sd->expo_change_state = EXPO_CHANGED;
264
265         if (sd->ctrls[FREQ].val == V4L2_CID_POWER_LINE_FREQUENCY_50HZ)
266                 integration = integration - integration % 106667;
267         if (sd->ctrls[FREQ].val == V4L2_CID_POWER_LINE_FREQUENCY_60HZ)
268                 integration = integration - integration % 88889;
269
270         expose_h = (integration >> 16);
271         expose_m = (integration >> 8);
272         expose_l = integration;
273
274         /* integration time low */
275         se401_set_feature(gspca_dev, HV7131_REG_TITL, expose_l);
276         /* integration time mid */
277         se401_set_feature(gspca_dev, HV7131_REG_TITM, expose_m);
278         /* integration time high */
279         se401_set_feature(gspca_dev, HV7131_REG_TITU, expose_h);
280 }
281
282 static int sd_config(struct gspca_dev *gspca_dev,
283                         const struct usb_device_id *id)
284 {
285         struct sd *sd = (struct sd *)gspca_dev;
286         struct cam *cam = &gspca_dev->cam;
287         u8 *cd = gspca_dev->usb_buf;
288         int i, j, n;
289         int widths[MAX_MODES], heights[MAX_MODES];
290
291         /* Read the camera descriptor */
292         se401_read_req(gspca_dev, SE401_REQ_GET_CAMERA_DESCRIPTOR, 1);
293         if (gspca_dev->usb_err) {
294                 /* Sometimes after being idle for a while the se401 won't
295                    respond and needs a good kicking  */
296                 usb_reset_device(gspca_dev->dev);
297                 gspca_dev->usb_err = 0;
298                 se401_read_req(gspca_dev, SE401_REQ_GET_CAMERA_DESCRIPTOR, 0);
299         }
300
301         /* Some cameras start with their LED on */
302         se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 0, 0);
303         if (gspca_dev->usb_err)
304                 return gspca_dev->usb_err;
305
306         if (cd[1] != 0x41) {
307                 pr_err("Wrong descriptor type\n");
308                 return -ENODEV;
309         }
310
311         if (!(cd[2] & SE401_FORMAT_BAYER)) {
312                 pr_err("Bayer format not supported!\n");
313                 return -ENODEV;
314         }
315
316         if (cd[3])
317                 pr_info("ExtraFeatures: %d\n", cd[3]);
318
319         n = cd[4] | (cd[5] << 8);
320         if (n > MAX_MODES) {
321                 pr_err("Too many frame sizes\n");
322                 return -ENODEV;
323         }
324
325         for (i = 0; i < n ; i++) {
326                 widths[i] = cd[6 + i * 4 + 0] | (cd[6 + i * 4 + 1] << 8);
327                 heights[i] = cd[6 + i * 4 + 2] | (cd[6 + i * 4 + 3] << 8);
328         }
329
330         for (i = 0; i < n ; i++) {
331                 sd->fmts[i].width = widths[i];
332                 sd->fmts[i].height = heights[i];
333                 sd->fmts[i].field = V4L2_FIELD_NONE;
334                 sd->fmts[i].colorspace = V4L2_COLORSPACE_SRGB;
335                 sd->fmts[i].priv = 1;
336
337                 /* janggu compression only works for 1/4th or 1/16th res */
338                 for (j = 0; j < n; j++) {
339                         if (widths[j] / 2 == widths[i] &&
340                             heights[j] / 2 == heights[i]) {
341                                 sd->fmts[i].priv = 2;
342                                 break;
343                         }
344                 }
345                 /* 1/16th if available too is better then 1/4th, because
346                    we then use a larger area of the sensor */
347                 for (j = 0; j < n; j++) {
348                         if (widths[j] / 4 == widths[i] &&
349                             heights[j] / 4 == heights[i]) {
350                                 sd->fmts[i].priv = 4;
351                                 break;
352                         }
353                 }
354
355                 if (sd->fmts[i].priv == 1) {
356                         /* Not a 1/4th or 1/16th res, use bayer */
357                         sd->fmts[i].pixelformat = V4L2_PIX_FMT_SBGGR8;
358                         sd->fmts[i].bytesperline = widths[i];
359                         sd->fmts[i].sizeimage = widths[i] * heights[i];
360                         pr_info("Frame size: %dx%d bayer\n",
361                                 widths[i], heights[i]);
362                 } else {
363                         /* Found a match use janggu compression */
364                         sd->fmts[i].pixelformat = V4L2_PIX_FMT_SE401;
365                         sd->fmts[i].bytesperline = 0;
366                         sd->fmts[i].sizeimage = widths[i] * heights[i] * 3;
367                         pr_info("Frame size: %dx%d 1/%dth janggu\n",
368                                 widths[i], heights[i],
369                                 sd->fmts[i].priv * sd->fmts[i].priv);
370                 }
371         }
372
373         cam->cam_mode = sd->fmts;
374         cam->nmodes = n;
375         cam->bulk = 1;
376         cam->bulk_size = BULK_SIZE;
377         cam->bulk_nurbs = 4;
378         cam->ctrls = sd->ctrls;
379         sd->resetlevel = 0x2d; /* Set initial resetlevel */
380
381         /* See if the camera supports brightness */
382         se401_read_req(gspca_dev, SE401_REQ_GET_BRT, 1);
383         if (gspca_dev->usb_err) {
384                 gspca_dev->ctrl_dis = (1 << BRIGHTNESS);
385                 gspca_dev->usb_err = 0;
386         }
387
388         return 0;
389 }
390
391 /* this function is called at probe and resume time */
392 static int sd_init(struct gspca_dev *gspca_dev)
393 {
394         return 0;
395 }
396
397 /* function called at start time before URB creation */
398 static int sd_isoc_init(struct gspca_dev *gspca_dev)
399 {
400         gspca_dev->alt = 1;     /* Ignore the bogus isoc alt settings */
401
402         return gspca_dev->usb_err;
403 }
404
405 /* -- start the camera -- */
406 static int sd_start(struct gspca_dev *gspca_dev)
407 {
408         struct sd *sd = (struct sd *)gspca_dev;
409         int mult = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
410         int mode = 0;
411
412         se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 1, 1);
413         if (gspca_dev->usb_err) {
414                 /* Sometimes after being idle for a while the se401 won't
415                    respond and needs a good kicking  */
416                 usb_reset_device(gspca_dev->dev);
417                 gspca_dev->usb_err = 0;
418                 se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 1, 0);
419         }
420         se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 1, 0);
421
422         se401_set_feature(gspca_dev, HV7131_REG_MODE_B, 0x05);
423
424         /* set size + mode */
425         se401_write_req(gspca_dev, SE401_REQ_SET_WIDTH,
426                         gspca_dev->width * mult, 0);
427         se401_write_req(gspca_dev, SE401_REQ_SET_HEIGHT,
428                         gspca_dev->height * mult, 0);
429         /*
430          * HDG: disabled this as it does not seem to do anything
431          * se401_write_req(gspca_dev, SE401_REQ_SET_OUTPUT_MODE,
432          *                 SE401_FORMAT_BAYER, 0);
433          */
434
435         switch (mult) {
436         case 1: /* Raw bayer */
437                 mode = 0x03; break;
438         case 2: /* 1/4th janggu */
439                 mode = SE401_QUANT_FACT << 4; break;
440         case 4: /* 1/16th janggu */
441                 mode = (SE401_QUANT_FACT << 4) | 0x02; break;
442         }
443         se401_set_feature(gspca_dev, SE401_OPERATINGMODE, mode);
444
445         setbrightness(gspca_dev);
446         setgain(gspca_dev);
447         setexposure(gspca_dev);
448         se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel);
449
450         sd->packet_read = 0;
451         sd->pixels_read = 0;
452         sd->restart_stream = 0;
453         sd->resetlevel_frame_count = 0;
454         sd->resetlevel_adjust_dir = 0;
455         sd->expo_change_state = EXPO_NO_CHANGE;
456
457         se401_write_req(gspca_dev, SE401_REQ_START_CONTINUOUS_CAPTURE, 0, 0);
458
459         return gspca_dev->usb_err;
460 }
461
462 static void sd_stopN(struct gspca_dev *gspca_dev)
463 {
464         se401_write_req(gspca_dev, SE401_REQ_STOP_CONTINUOUS_CAPTURE, 0, 0);
465         se401_write_req(gspca_dev, SE401_REQ_LED_CONTROL, 0, 0);
466         se401_write_req(gspca_dev, SE401_REQ_CAMERA_POWER, 0, 0);
467 }
468
469 static void sd_dq_callback(struct gspca_dev *gspca_dev)
470 {
471         struct sd *sd = (struct sd *)gspca_dev;
472         unsigned int ahrc, alrc;
473         int oldreset, adjust_dir;
474
475         /* Restart the stream if requested do so by pkt_scan */
476         if (sd->restart_stream) {
477                 sd_stopN(gspca_dev);
478                 sd_start(gspca_dev);
479                 sd->restart_stream = 0;
480         }
481
482         /* Automatically adjust sensor reset level
483            Hyundai have some really nice docs about this and other sensor
484            related stuff on their homepage: www.hei.co.kr */
485         sd->resetlevel_frame_count++;
486         if (sd->resetlevel_frame_count < 20)
487                 return;
488
489         /* For some reason this normally read-only register doesn't get reset
490            to zero after reading them just once... */
491         se401_get_feature(gspca_dev, HV7131_REG_HIREFNOH);
492         se401_get_feature(gspca_dev, HV7131_REG_HIREFNOL);
493         se401_get_feature(gspca_dev, HV7131_REG_LOREFNOH);
494         se401_get_feature(gspca_dev, HV7131_REG_LOREFNOL);
495         ahrc = 256*se401_get_feature(gspca_dev, HV7131_REG_HIREFNOH) +
496             se401_get_feature(gspca_dev, HV7131_REG_HIREFNOL);
497         alrc = 256*se401_get_feature(gspca_dev, HV7131_REG_LOREFNOH) +
498             se401_get_feature(gspca_dev, HV7131_REG_LOREFNOL);
499
500         /* Not an exact science, but it seems to work pretty well... */
501         oldreset = sd->resetlevel;
502         if (alrc > 10) {
503                 while (alrc >= 10 && sd->resetlevel < 63) {
504                         sd->resetlevel++;
505                         alrc /= 2;
506                 }
507         } else if (ahrc > 20) {
508                 while (ahrc >= 20 && sd->resetlevel > 0) {
509                         sd->resetlevel--;
510                         ahrc /= 2;
511                 }
512         }
513         /* Detect ping-pong-ing and halve adjustment to avoid overshoot */
514         if (sd->resetlevel > oldreset)
515                 adjust_dir = 1;
516         else
517                 adjust_dir = -1;
518         if (sd->resetlevel_adjust_dir &&
519             sd->resetlevel_adjust_dir != adjust_dir)
520                 sd->resetlevel = oldreset + (sd->resetlevel - oldreset) / 2;
521
522         if (sd->resetlevel != oldreset) {
523                 sd->resetlevel_adjust_dir = adjust_dir;
524                 se401_set_feature(gspca_dev, HV7131_REG_ARLV, sd->resetlevel);
525         }
526
527         sd->resetlevel_frame_count = 0;
528 }
529
530 static void sd_complete_frame(struct gspca_dev *gspca_dev, u8 *data, int len)
531 {
532         struct sd *sd = (struct sd *)gspca_dev;
533
534         switch (sd->expo_change_state) {
535         case EXPO_CHANGED:
536                 /* The exposure was changed while this frame
537                    was being send, so this frame is ok */
538                 sd->expo_change_state = EXPO_DROP_FRAME;
539                 break;
540         case EXPO_DROP_FRAME:
541                 /* The exposure was changed while this frame
542                    was being captured, drop it! */
543                 gspca_dev->last_packet_type = DISCARD_PACKET;
544                 sd->expo_change_state = EXPO_NO_CHANGE;
545                 break;
546         case EXPO_NO_CHANGE:
547                 break;
548         }
549         gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
550 }
551
552 static void sd_pkt_scan_janggu(struct gspca_dev *gspca_dev, u8 *data, int len)
553 {
554         struct sd *sd = (struct sd *)gspca_dev;
555         int imagesize = gspca_dev->width * gspca_dev->height;
556         int i, plen, bits, pixels, info, count;
557
558         if (sd->restart_stream)
559                 return;
560
561         /* Sometimes a 1024 bytes garbage bulk packet is send between frames */
562         if (gspca_dev->last_packet_type == LAST_PACKET && len == 1024) {
563                 gspca_dev->last_packet_type = DISCARD_PACKET;
564                 return;
565         }
566
567         i = 0;
568         while (i < len) {
569                 /* Read header if not already be present from prev bulk pkt */
570                 if (sd->packet_read < 4) {
571                         count = 4 - sd->packet_read;
572                         if (count > len - i)
573                                 count = len - i;
574                         memcpy(&sd->packet[sd->packet_read], &data[i], count);
575                         sd->packet_read += count;
576                         i += count;
577                         if (sd->packet_read < 4)
578                                 break;
579                 }
580                 bits   = sd->packet[3] + (sd->packet[2] << 8);
581                 pixels = sd->packet[1] + ((sd->packet[0] & 0x3f) << 8);
582                 info   = (sd->packet[0] & 0xc0) >> 6;
583                 plen   = ((bits + 47) >> 4) << 1;
584                 /* Sanity checks */
585                 if (plen > 1024) {
586                         pr_err("invalid packet len %d restarting stream\n",
587                                plen);
588                         goto error;
589                 }
590                 if (info == 3) {
591                         pr_err("unknown frame info value restarting stream\n");
592                         goto error;
593                 }
594
595                 /* Read (remainder of) packet contents */
596                 count = plen - sd->packet_read;
597                 if (count > len - i)
598                         count = len - i;
599                 memcpy(&sd->packet[sd->packet_read], &data[i], count);
600                 sd->packet_read += count;
601                 i += count;
602                 if (sd->packet_read < plen)
603                         break;
604
605                 sd->pixels_read += pixels;
606                 sd->packet_read = 0;
607
608                 switch (info) {
609                 case 0: /* Frame data */
610                         gspca_frame_add(gspca_dev, INTER_PACKET, sd->packet,
611                                         plen);
612                         break;
613                 case 1: /* EOF */
614                         if (sd->pixels_read != imagesize) {
615                                 pr_err("frame size %d expected %d\n",
616                                        sd->pixels_read, imagesize);
617                                 goto error;
618                         }
619                         sd_complete_frame(gspca_dev, sd->packet, plen);
620                         return; /* Discard the rest of the bulk packet !! */
621                 case 2: /* SOF */
622                         gspca_frame_add(gspca_dev, FIRST_PACKET, sd->packet,
623                                         plen);
624                         sd->pixels_read = pixels;
625                         break;
626                 }
627         }
628         return;
629
630 error:
631         sd->restart_stream = 1;
632         /* Give userspace a 0 bytes frame, so our dq callback gets
633            called and it can restart the stream */
634         gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
635         gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
636 }
637
638 static void sd_pkt_scan_bayer(struct gspca_dev *gspca_dev, u8 *data, int len)
639 {
640         struct cam *cam = &gspca_dev->cam;
641         int imagesize = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
642
643         if (gspca_dev->image_len == 0) {
644                 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
645                 return;
646         }
647
648         if (gspca_dev->image_len + len >= imagesize) {
649                 sd_complete_frame(gspca_dev, data, len);
650                 return;
651         }
652
653         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
654 }
655
656 static void sd_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
657 {
658         int mult = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
659
660         if (len == 0)
661                 return;
662
663         if (mult == 1) /* mult == 1 means raw bayer */
664                 sd_pkt_scan_bayer(gspca_dev, data, len);
665         else
666                 sd_pkt_scan_janggu(gspca_dev, data, len);
667 }
668
669 static int sd_querymenu(struct gspca_dev *gspca_dev,
670                         struct v4l2_querymenu *menu)
671 {
672         switch (menu->id) {
673         case V4L2_CID_POWER_LINE_FREQUENCY:
674                 switch (menu->index) {
675                 case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
676                         strcpy((char *) menu->name, "NoFliker");
677                         return 0;
678                 case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
679                         strcpy((char *) menu->name, "50 Hz");
680                         return 0;
681                 case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
682                         strcpy((char *) menu->name, "60 Hz");
683                         return 0;
684                 }
685                 break;
686         }
687         return -EINVAL;
688 }
689
690 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
691 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev, u8 *data, int len)
692 {
693         struct sd *sd = (struct sd *)gspca_dev;
694         u8 state;
695
696         if (len != 2)
697                 return -EINVAL;
698
699         switch (data[0]) {
700         case 0:
701         case 1:
702                 state = data[0];
703                 break;
704         default:
705                 return -EINVAL;
706         }
707         if (sd->button_state != state) {
708                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
709                 input_sync(gspca_dev->input_dev);
710                 sd->button_state = state;
711         }
712
713         return 0;
714 }
715 #endif
716
717 /* sub-driver description */
718 static const struct sd_desc sd_desc = {
719         .name = MODULE_NAME,
720         .ctrls = sd_ctrls,
721         .nctrls = ARRAY_SIZE(sd_ctrls),
722         .config = sd_config,
723         .init = sd_init,
724         .isoc_init = sd_isoc_init,
725         .start = sd_start,
726         .stopN = sd_stopN,
727         .dq_callback = sd_dq_callback,
728         .pkt_scan = sd_pkt_scan,
729         .querymenu = sd_querymenu,
730 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
731         .int_pkt_scan = sd_int_pkt_scan,
732 #endif
733 };
734
735 /* -- module initialisation -- */
736 static const struct usb_device_id device_table[] = {
737         {USB_DEVICE(0x03e8, 0x0004)}, /* Endpoints/Aox SE401 */
738         {USB_DEVICE(0x0471, 0x030b)}, /* Philips PCVC665K */
739         {USB_DEVICE(0x047d, 0x5001)}, /* Kensington 67014 */
740         {USB_DEVICE(0x047d, 0x5002)}, /* Kensington 6701(5/7) */
741         {USB_DEVICE(0x047d, 0x5003)}, /* Kensington 67016 */
742         {}
743 };
744 MODULE_DEVICE_TABLE(usb, device_table);
745
746 /* -- device connect -- */
747 static int sd_probe(struct usb_interface *intf,
748                         const struct usb_device_id *id)
749 {
750         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
751                                 THIS_MODULE);
752 }
753
754 static int sd_pre_reset(struct usb_interface *intf)
755 {
756         return 0;
757 }
758
759 static int sd_post_reset(struct usb_interface *intf)
760 {
761         return 0;
762 }
763
764 static struct usb_driver sd_driver = {
765         .name = MODULE_NAME,
766         .id_table = device_table,
767         .probe = sd_probe,
768         .disconnect = gspca_disconnect,
769 #ifdef CONFIG_PM
770         .suspend = gspca_suspend,
771         .resume = gspca_resume,
772 #endif
773         .pre_reset = sd_pre_reset,
774         .post_reset = sd_post_reset,
775 };
776
777 module_usb_driver(sd_driver);