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