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