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