GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / media / usb / gspca / w996Xcf.c
1 /**
2  *
3  * GSPCA sub driver for W996[78]CF JPEG USB Dual Mode Camera Chip.
4  *
5  * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
6  *
7  * This module is adapted from the in kernel v4l1 w9968cf driver:
8  *
9  * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  *
25  */
26
27 /* Note this is not a stand alone driver, it gets included in ov519.c, this
28    is a bit of a hack, but it needs the driver code for a lot of different
29    ov sensors which is already present in ov519.c (the old v4l1 driver used
30    the ovchipcam framework). When we have the time we really should move
31    the sensor drivers to v4l2 sub drivers, and properly split of this
32    driver from ov519.c */
33
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36 #define W9968CF_I2C_BUS_DELAY    4 /* delay in us for I2C bit r/w operations */
37
38 #define Y_QUANTABLE (&sd->jpeg_hdr[JPEG_QT0_OFFSET])
39 #define UV_QUANTABLE (&sd->jpeg_hdr[JPEG_QT1_OFFSET])
40
41 static const struct v4l2_pix_format w9968cf_vga_mode[] = {
42         {160, 120, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
43                 .bytesperline = 160 * 2,
44                 .sizeimage = 160 * 120 * 2,
45                 .colorspace = V4L2_COLORSPACE_JPEG},
46         {176, 144, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE,
47                 .bytesperline = 176 * 2,
48                 .sizeimage = 176 * 144 * 2,
49                 .colorspace = V4L2_COLORSPACE_JPEG},
50         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
51                 .bytesperline = 320 * 2,
52                 .sizeimage = 320 * 240 * 2,
53                 .colorspace = V4L2_COLORSPACE_JPEG},
54         {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
55                 .bytesperline = 352 * 2,
56                 .sizeimage = 352 * 288 * 2,
57                 .colorspace = V4L2_COLORSPACE_JPEG},
58         {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
59                 .bytesperline = 640 * 2,
60                 .sizeimage = 640 * 480 * 2,
61                 .colorspace = V4L2_COLORSPACE_JPEG},
62 };
63
64 static void reg_w(struct sd *sd, u16 index, u16 value);
65
66 /*--------------------------------------------------------------------------
67   Write 64-bit data to the fast serial bus registers.
68   Return 0 on success, -1 otherwise.
69   --------------------------------------------------------------------------*/
70 static void w9968cf_write_fsb(struct sd *sd, u16* data)
71 {
72         struct usb_device *udev = sd->gspca_dev.dev;
73         u16 value;
74         int ret;
75
76         if (sd->gspca_dev.usb_err < 0)
77                 return;
78
79         value = *data++;
80         memcpy(sd->gspca_dev.usb_buf, data, 6);
81
82         /* Avoid things going to fast for the bridge with a xhci host */
83         udelay(150);
84         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0,
85                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
86                               value, 0x06, sd->gspca_dev.usb_buf, 6, 500);
87         if (ret < 0) {
88                 pr_err("Write FSB registers failed (%d)\n", ret);
89                 sd->gspca_dev.usb_err = ret;
90         }
91 }
92
93 /*--------------------------------------------------------------------------
94   Write data to the serial bus control register.
95   Return 0 on success, a negative number otherwise.
96   --------------------------------------------------------------------------*/
97 static void w9968cf_write_sb(struct sd *sd, u16 value)
98 {
99         int ret;
100
101         if (sd->gspca_dev.usb_err < 0)
102                 return;
103
104         /* Avoid things going to fast for the bridge with a xhci host */
105         udelay(150);
106
107         /* We don't use reg_w here, as that would cause all writes when
108            bitbanging i2c to be logged, making the logs impossible to read */
109         ret = usb_control_msg(sd->gspca_dev.dev,
110                 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
111                 0,
112                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
113                 value, 0x01, NULL, 0, 500);
114
115         udelay(W9968CF_I2C_BUS_DELAY);
116
117         if (ret < 0) {
118                 pr_err("Write SB reg [01] %04x failed\n", value);
119                 sd->gspca_dev.usb_err = ret;
120         }
121 }
122
123 /*--------------------------------------------------------------------------
124   Read data from the serial bus control register.
125   Return 0 on success, a negative number otherwise.
126   --------------------------------------------------------------------------*/
127 static int w9968cf_read_sb(struct sd *sd)
128 {
129         int ret;
130
131         if (sd->gspca_dev.usb_err < 0)
132                 return -1;
133
134         /* Avoid things going to fast for the bridge with a xhci host */
135         udelay(150);
136
137         /* We don't use reg_r here, as the w9968cf is special and has 16
138            bit registers instead of 8 bit */
139         ret = usb_control_msg(sd->gspca_dev.dev,
140                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
141                         1,
142                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
143                         0, 0x01, sd->gspca_dev.usb_buf, 2, 500);
144         if (ret >= 0) {
145                 ret = sd->gspca_dev.usb_buf[0] |
146                       (sd->gspca_dev.usb_buf[1] << 8);
147         } else {
148                 pr_err("Read SB reg [01] failed\n");
149                 sd->gspca_dev.usb_err = ret;
150                 /*
151                  * Make sure the buffer is zeroed to avoid uninitialized
152                  * values.
153                  */
154                 memset(sd->gspca_dev.usb_buf, 0, 2);
155         }
156
157         udelay(W9968CF_I2C_BUS_DELAY);
158
159         return ret;
160 }
161
162 /*--------------------------------------------------------------------------
163   Upload quantization tables for the JPEG compression.
164   This function is called by w9968cf_start_transfer().
165   Return 0 on success, a negative number otherwise.
166   --------------------------------------------------------------------------*/
167 static void w9968cf_upload_quantizationtables(struct sd *sd)
168 {
169         u16 a, b;
170         int i, j;
171
172         reg_w(sd, 0x39, 0x0010); /* JPEG clock enable */
173
174         for (i = 0, j = 0; i < 32; i++, j += 2) {
175                 a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j + 1]) << 8);
176                 b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j + 1]) << 8);
177                 reg_w(sd, 0x40 + i, a);
178                 reg_w(sd, 0x60 + i, b);
179         }
180         reg_w(sd, 0x39, 0x0012); /* JPEG encoder enable */
181 }
182
183 /****************************************************************************
184  * Low-level I2C I/O functions.                                             *
185  * The adapter supports the following I2C transfer functions:               *
186  * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only)           *
187  * i2c_adap_read_byte_data()                                                *
188  * i2c_adap_read_byte()                                                     *
189  ****************************************************************************/
190
191 static void w9968cf_smbus_start(struct sd *sd)
192 {
193         w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */
194         w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */
195 }
196
197 static void w9968cf_smbus_stop(struct sd *sd)
198 {
199         w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */
200         w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */
201         w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */
202 }
203
204 static void w9968cf_smbus_write_byte(struct sd *sd, u8 v)
205 {
206         u8 bit;
207         int sda;
208
209         for (bit = 0 ; bit < 8 ; bit++) {
210                 sda = (v & 0x80) ? 2 : 0;
211                 v <<= 1;
212                 /* SDE=1, SDA=sda, SCL=0 */
213                 w9968cf_write_sb(sd, 0x10 | sda);
214                 /* SDE=1, SDA=sda, SCL=1 */
215                 w9968cf_write_sb(sd, 0x11 | sda);
216                 /* SDE=1, SDA=sda, SCL=0 */
217                 w9968cf_write_sb(sd, 0x10 | sda);
218         }
219 }
220
221 static void w9968cf_smbus_read_byte(struct sd *sd, u8 *v)
222 {
223         u8 bit;
224
225         /* No need to ensure SDA is high as we are always called after
226            read_ack which ends with SDA high */
227         *v = 0;
228         for (bit = 0 ; bit < 8 ; bit++) {
229                 *v <<= 1;
230                 /* SDE=1, SDA=1, SCL=1 */
231                 w9968cf_write_sb(sd, 0x0013);
232                 *v |= (w9968cf_read_sb(sd) & 0x0008) ? 1 : 0;
233                 /* SDE=1, SDA=1, SCL=0 */
234                 w9968cf_write_sb(sd, 0x0012);
235         }
236 }
237
238 static void w9968cf_smbus_write_nack(struct sd *sd)
239 {
240         /* No need to ensure SDA is high as we are always called after
241            read_byte which ends with SDA high */
242         w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */
243         w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */
244 }
245
246 static void w9968cf_smbus_read_ack(struct sd *sd)
247 {
248         struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
249         int sda;
250
251         /* Ensure SDA is high before raising clock to avoid a spurious stop */
252         w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */
253         w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */
254         sda = w9968cf_read_sb(sd);
255         w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */
256         if (sda >= 0 && (sda & 0x08)) {
257                 PDEBUG(D_USBI, "Did not receive i2c ACK");
258                 sd->gspca_dev.usb_err = -EIO;
259         }
260 }
261
262 /* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */
263 static void w9968cf_i2c_w(struct sd *sd, u8 reg, u8 value)
264 {
265         struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
266         u16* data = (u16 *)sd->gspca_dev.usb_buf;
267
268         data[0] = 0x082f | ((sd->sensor_addr & 0x80) ? 0x1500 : 0x0);
269         data[0] |= (sd->sensor_addr & 0x40) ? 0x4000 : 0x0;
270         data[1] = 0x2082 | ((sd->sensor_addr & 0x40) ? 0x0005 : 0x0);
271         data[1] |= (sd->sensor_addr & 0x20) ? 0x0150 : 0x0;
272         data[1] |= (sd->sensor_addr & 0x10) ? 0x5400 : 0x0;
273         data[2] = 0x8208 | ((sd->sensor_addr & 0x08) ? 0x0015 : 0x0);
274         data[2] |= (sd->sensor_addr & 0x04) ? 0x0540 : 0x0;
275         data[2] |= (sd->sensor_addr & 0x02) ? 0x5000 : 0x0;
276         data[3] = 0x1d20 | ((sd->sensor_addr & 0x02) ? 0x0001 : 0x0);
277         data[3] |= (sd->sensor_addr & 0x01) ? 0x0054 : 0x0;
278
279         w9968cf_write_fsb(sd, data);
280
281         data[0] = 0x8208 | ((reg & 0x80) ? 0x0015 : 0x0);
282         data[0] |= (reg & 0x40) ? 0x0540 : 0x0;
283         data[0] |= (reg & 0x20) ? 0x5000 : 0x0;
284         data[1] = 0x0820 | ((reg & 0x20) ? 0x0001 : 0x0);
285         data[1] |= (reg & 0x10) ? 0x0054 : 0x0;
286         data[1] |= (reg & 0x08) ? 0x1500 : 0x0;
287         data[1] |= (reg & 0x04) ? 0x4000 : 0x0;
288         data[2] = 0x2082 | ((reg & 0x04) ? 0x0005 : 0x0);
289         data[2] |= (reg & 0x02) ? 0x0150 : 0x0;
290         data[2] |= (reg & 0x01) ? 0x5400 : 0x0;
291         data[3] = 0x001d;
292
293         w9968cf_write_fsb(sd, data);
294
295         data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0);
296         data[0] |= (value & 0x40) ? 0x0540 : 0x0;
297         data[0] |= (value & 0x20) ? 0x5000 : 0x0;
298         data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0);
299         data[1] |= (value & 0x10) ? 0x0054 : 0x0;
300         data[1] |= (value & 0x08) ? 0x1500 : 0x0;
301         data[1] |= (value & 0x04) ? 0x4000 : 0x0;
302         data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0);
303         data[2] |= (value & 0x02) ? 0x0150 : 0x0;
304         data[2] |= (value & 0x01) ? 0x5400 : 0x0;
305         data[3] = 0xfe1d;
306
307         w9968cf_write_fsb(sd, data);
308
309         PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
310 }
311
312 /* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */
313 static int w9968cf_i2c_r(struct sd *sd, u8 reg)
314 {
315         struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
316         int ret = 0;
317         u8 value;
318
319         /* Fast serial bus data control disable */
320         w9968cf_write_sb(sd, 0x0013); /* don't change ! */
321
322         w9968cf_smbus_start(sd);
323         w9968cf_smbus_write_byte(sd, sd->sensor_addr);
324         w9968cf_smbus_read_ack(sd);
325         w9968cf_smbus_write_byte(sd, reg);
326         w9968cf_smbus_read_ack(sd);
327         w9968cf_smbus_stop(sd);
328         w9968cf_smbus_start(sd);
329         w9968cf_smbus_write_byte(sd, sd->sensor_addr + 1);
330         w9968cf_smbus_read_ack(sd);
331         w9968cf_smbus_read_byte(sd, &value);
332         /* signal we don't want to read anymore, the v4l1 driver used to
333            send an ack here which is very wrong! (and then fixed
334            the issues this gave by retrying reads) */
335         w9968cf_smbus_write_nack(sd);
336         w9968cf_smbus_stop(sd);
337
338         /* Fast serial bus data control re-enable */
339         w9968cf_write_sb(sd, 0x0030);
340
341         if (sd->gspca_dev.usb_err >= 0) {
342                 ret = value;
343                 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
344         } else
345                 PERR("i2c read [0x%02x] failed", reg);
346
347         return ret;
348 }
349
350 /*--------------------------------------------------------------------------
351   Turn on the LED on some webcams. A beep should be heard too.
352   Return 0 on success, a negative number otherwise.
353   --------------------------------------------------------------------------*/
354 static void w9968cf_configure(struct sd *sd)
355 {
356         reg_w(sd, 0x00, 0xff00); /* power-down */
357         reg_w(sd, 0x00, 0xbf17); /* reset everything */
358         reg_w(sd, 0x00, 0xbf10); /* normal operation */
359         reg_w(sd, 0x01, 0x0010); /* serial bus, SDS high */
360         reg_w(sd, 0x01, 0x0000); /* serial bus, SDS low */
361         reg_w(sd, 0x01, 0x0010); /* ..high 'beep-beep' */
362         reg_w(sd, 0x01, 0x0030); /* Set sda scl to FSB mode */
363
364         sd->stopped = 1;
365 }
366
367 static void w9968cf_init(struct sd *sd)
368 {
369         unsigned long hw_bufsize = sd->sif ? (352 * 288 * 2) : (640 * 480 * 2),
370                       y0 = 0x0000,
371                       u0 = y0 + hw_bufsize / 2,
372                       v0 = u0 + hw_bufsize / 4,
373                       y1 = v0 + hw_bufsize / 4,
374                       u1 = y1 + hw_bufsize / 2,
375                       v1 = u1 + hw_bufsize / 4;
376
377         reg_w(sd, 0x00, 0xff00); /* power off */
378         reg_w(sd, 0x00, 0xbf10); /* power on */
379
380         reg_w(sd, 0x03, 0x405d); /* DRAM timings */
381         reg_w(sd, 0x04, 0x0030); /* SDRAM timings */
382
383         reg_w(sd, 0x20, y0 & 0xffff); /* Y buf.0, low */
384         reg_w(sd, 0x21, y0 >> 16);    /* Y buf.0, high */
385         reg_w(sd, 0x24, u0 & 0xffff); /* U buf.0, low */
386         reg_w(sd, 0x25, u0 >> 16);    /* U buf.0, high */
387         reg_w(sd, 0x28, v0 & 0xffff); /* V buf.0, low */
388         reg_w(sd, 0x29, v0 >> 16);    /* V buf.0, high */
389
390         reg_w(sd, 0x22, y1 & 0xffff); /* Y buf.1, low */
391         reg_w(sd, 0x23, y1 >> 16);    /* Y buf.1, high */
392         reg_w(sd, 0x26, u1 & 0xffff); /* U buf.1, low */
393         reg_w(sd, 0x27, u1 >> 16);    /* U buf.1, high */
394         reg_w(sd, 0x2a, v1 & 0xffff); /* V buf.1, low */
395         reg_w(sd, 0x2b, v1 >> 16);    /* V buf.1, high */
396
397         reg_w(sd, 0x32, y1 & 0xffff); /* JPEG buf 0 low */
398         reg_w(sd, 0x33, y1 >> 16);    /* JPEG buf 0 high */
399
400         reg_w(sd, 0x34, y1 & 0xffff); /* JPEG buf 1 low */
401         reg_w(sd, 0x35, y1 >> 16);    /* JPEG bug 1 high */
402
403         reg_w(sd, 0x36, 0x0000);/* JPEG restart interval */
404         reg_w(sd, 0x37, 0x0804);/*JPEG VLE FIFO threshold*/
405         reg_w(sd, 0x38, 0x0000);/* disable hw up-scaling */
406         reg_w(sd, 0x3f, 0x0000); /* JPEG/MCTL test data */
407 }
408
409 static void w9968cf_set_crop_window(struct sd *sd)
410 {
411         int start_cropx, start_cropy,  x, y, fw, fh, cw, ch,
412             max_width, max_height;
413
414         if (sd->sif) {
415                 max_width  = 352;
416                 max_height = 288;
417         } else {
418                 max_width  = 640;
419                 max_height = 480;
420         }
421
422         if (sd->sensor == SEN_OV7620) {
423                 /*
424                  * Sigh, this is dependend on the clock / framerate changes
425                  * made by the frequency control, sick.
426                  *
427                  * Note we cannot use v4l2_ctrl_g_ctrl here, as we get called
428                  * from ov519.c:setfreq() with the ctrl lock held!
429                  */
430                 if (sd->freq->val == 1) {
431                         start_cropx = 277;
432                         start_cropy = 37;
433                 } else {
434                         start_cropx = 105;
435                         start_cropy = 37;
436                 }
437         } else {
438                 start_cropx = 320;
439                 start_cropy = 35;
440         }
441
442         /* Work around to avoid FP arithmetics */
443         #define SC(x) ((x) << 10)
444
445         /* Scaling factors */
446         fw = SC(sd->gspca_dev.pixfmt.width) / max_width;
447         fh = SC(sd->gspca_dev.pixfmt.height) / max_height;
448
449         cw = (fw >= fh) ? max_width : SC(sd->gspca_dev.pixfmt.width) / fh;
450         ch = (fw >= fh) ? SC(sd->gspca_dev.pixfmt.height) / fw : max_height;
451
452         sd->sensor_width = max_width;
453         sd->sensor_height = max_height;
454
455         x = (max_width - cw) / 2;
456         y = (max_height - ch) / 2;
457
458         reg_w(sd, 0x10, start_cropx + x);
459         reg_w(sd, 0x11, start_cropy + y);
460         reg_w(sd, 0x12, start_cropx + x + cw);
461         reg_w(sd, 0x13, start_cropy + y + ch);
462 }
463
464 static void w9968cf_mode_init_regs(struct sd *sd)
465 {
466         int val, vs_polarity, hs_polarity;
467
468         w9968cf_set_crop_window(sd);
469
470         reg_w(sd, 0x14, sd->gspca_dev.pixfmt.width);
471         reg_w(sd, 0x15, sd->gspca_dev.pixfmt.height);
472
473         /* JPEG width & height */
474         reg_w(sd, 0x30, sd->gspca_dev.pixfmt.width);
475         reg_w(sd, 0x31, sd->gspca_dev.pixfmt.height);
476
477         /* Y & UV frame buffer strides (in WORD) */
478         if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
479             V4L2_PIX_FMT_JPEG) {
480                 reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width / 2);
481                 reg_w(sd, 0x2d, sd->gspca_dev.pixfmt.width / 4);
482         } else
483                 reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width);
484
485         reg_w(sd, 0x00, 0xbf17); /* reset everything */
486         reg_w(sd, 0x00, 0xbf10); /* normal operation */
487
488         /* Transfer size in WORDS (for UYVY format only) */
489         val = sd->gspca_dev.pixfmt.width * sd->gspca_dev.pixfmt.height;
490         reg_w(sd, 0x3d, val & 0xffff); /* low bits */
491         reg_w(sd, 0x3e, val >> 16);    /* high bits */
492
493         if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
494             V4L2_PIX_FMT_JPEG) {
495                 /* We may get called multiple times (usb isoc bw negotiat.) */
496                 jpeg_define(sd->jpeg_hdr, sd->gspca_dev.pixfmt.height,
497                             sd->gspca_dev.pixfmt.width, 0x22); /* JPEG 420 */
498                 jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
499                 w9968cf_upload_quantizationtables(sd);
500                 v4l2_ctrl_grab(sd->jpegqual, true);
501         }
502
503         /* Video Capture Control Register */
504         if (sd->sensor == SEN_OV7620) {
505                 /* Seems to work around a bug in the image sensor */
506                 vs_polarity = 1;
507                 hs_polarity = 1;
508         } else {
509                 vs_polarity = 1;
510                 hs_polarity = 0;
511         }
512
513         val = (vs_polarity << 12) | (hs_polarity << 11);
514
515         /* NOTE: We may not have enough memory to do double buffering while
516            doing compression (amount of memory differs per model cam).
517            So we use the second image buffer also as jpeg stream buffer
518            (see w9968cf_init), and disable double buffering. */
519         if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat ==
520             V4L2_PIX_FMT_JPEG) {
521                 /* val |= 0x0002; YUV422P */
522                 val |= 0x0003; /* YUV420P */
523         } else
524                 val |= 0x0080; /* Enable HW double buffering */
525
526         /* val |= 0x0020; enable clamping */
527         /* val |= 0x0008; enable (1-2-1) filter */
528         /* val |= 0x000c; enable (2-3-6-3-2) filter */
529
530         val |= 0x8000; /* capt. enable */
531
532         reg_w(sd, 0x16, val);
533
534         sd->gspca_dev.empty_packet = 0;
535 }
536
537 static void w9968cf_stop0(struct sd *sd)
538 {
539         v4l2_ctrl_grab(sd->jpegqual, false);
540         reg_w(sd, 0x39, 0x0000); /* disable JPEG encoder */
541         reg_w(sd, 0x16, 0x0000); /* stop video capture */
542 }
543
544 /* The w9968cf docs say that a 0 sized packet means EOF (and also SOF
545    for the next frame). This seems to simply not be true when operating
546    in JPEG mode, in this case there may be empty packets within the
547    frame. So in JPEG mode use the JPEG SOI marker to detect SOF.
548
549    Note to make things even more interesting the w9968cf sends *PLANAR* jpeg,
550    to be precise it sends: SOI, SOF, DRI, SOS, Y-data, SOS, U-data, SOS,
551    V-data, EOI. */
552 static void w9968cf_pkt_scan(struct gspca_dev *gspca_dev,
553                         u8 *data,                       /* isoc packet */
554                         int len)                        /* iso packet length */
555 {
556         struct sd *sd = (struct sd *) gspca_dev;
557
558         if (w9968cf_vga_mode[gspca_dev->curr_mode].pixelformat ==
559             V4L2_PIX_FMT_JPEG) {
560                 if (len >= 2 &&
561                     data[0] == 0xff &&
562                     data[1] == 0xd8) {
563                         gspca_frame_add(gspca_dev, LAST_PACKET,
564                                         NULL, 0);
565                         gspca_frame_add(gspca_dev, FIRST_PACKET,
566                                         sd->jpeg_hdr, JPEG_HDR_SZ);
567                         /* Strip the ff d8, our own header (which adds
568                            huffman and quantization tables) already has this */
569                         len -= 2;
570                         data += 2;
571                 }
572         } else {
573                 /* In UYVY mode an empty packet signals EOF */
574                 if (gspca_dev->empty_packet) {
575                         gspca_frame_add(gspca_dev, LAST_PACKET,
576                                                 NULL, 0);
577                         gspca_frame_add(gspca_dev, FIRST_PACKET,
578                                         NULL, 0);
579                         gspca_dev->empty_packet = 0;
580                 }
581         }
582         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
583 }