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