xref: /openbmc/linux/drivers/media/usb/gspca/jeilinj.c (revision 68198dca)
1 /*
2  * Jeilinj subdriver
3  *
4  * Supports some Jeilin dual-mode cameras which use bulk transport and
5  * download raw JPEG data.
6  *
7  * Copyright (C) 2009 Theodore Kilgore
8  *
9  * Sportscam DV15 support and control settings are
10  * Copyright (C) 2011 Patrice Chotard
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  */
22 
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 
25 #define MODULE_NAME "jeilinj"
26 
27 #include <linux/slab.h>
28 #include "gspca.h"
29 #include "jpeg.h"
30 
31 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
32 MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
33 MODULE_LICENSE("GPL");
34 
35 /* Default timeouts, in ms */
36 #define JEILINJ_CMD_TIMEOUT 500
37 #define JEILINJ_CMD_DELAY 160
38 #define JEILINJ_DATA_TIMEOUT 1000
39 
40 /* Maximum transfer size to use. */
41 #define JEILINJ_MAX_TRANSFER 0x200
42 #define FRAME_HEADER_LEN 0x10
43 #define FRAME_START 0xFFFFFFFF
44 
45 enum {
46 	SAKAR_57379,
47 	SPORTSCAM_DV15,
48 };
49 
50 #define CAMQUALITY_MIN 0	/* highest cam quality */
51 #define CAMQUALITY_MAX 97	/* lowest cam quality  */
52 
53 /* Structure to hold all of our device specific stuff */
54 struct sd {
55 	struct gspca_dev gspca_dev;	/* !! must be the first item */
56 	int blocks_left;
57 	const struct v4l2_pix_format *cap_mode;
58 	struct v4l2_ctrl *freq;
59 	struct v4l2_ctrl *jpegqual;
60 	/* Driver stuff */
61 	u8 type;
62 	u8 quality;				 /* image quality */
63 #define QUALITY_MIN 35
64 #define QUALITY_MAX 85
65 #define QUALITY_DEF 85
66 	u8 jpeg_hdr[JPEG_HDR_SZ];
67 };
68 
69 struct jlj_command {
70 	unsigned char instruction[2];
71 	unsigned char ack_wanted;
72 	unsigned char delay;
73 };
74 
75 /* AFAICT these cameras will only do 320x240. */
76 static struct v4l2_pix_format jlj_mode[] = {
77 	{ 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
78 		.bytesperline = 320,
79 		.sizeimage = 320 * 240,
80 		.colorspace = V4L2_COLORSPACE_JPEG,
81 		.priv = 0},
82 	{ 640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
83 		.bytesperline = 640,
84 		.sizeimage = 640 * 480,
85 		.colorspace = V4L2_COLORSPACE_JPEG,
86 		.priv = 0}
87 };
88 
89 /*
90  * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
91  * and 0x82 for bulk transfer.
92  */
93 
94 /* All commands are two bytes only */
95 static void jlj_write2(struct gspca_dev *gspca_dev, unsigned char *command)
96 {
97 	int retval;
98 
99 	if (gspca_dev->usb_err < 0)
100 		return;
101 	memcpy(gspca_dev->usb_buf, command, 2);
102 	retval = usb_bulk_msg(gspca_dev->dev,
103 			usb_sndbulkpipe(gspca_dev->dev, 3),
104 			gspca_dev->usb_buf, 2, NULL, 500);
105 	if (retval < 0) {
106 		pr_err("command write [%02x] error %d\n",
107 		       gspca_dev->usb_buf[0], retval);
108 		gspca_dev->usb_err = retval;
109 	}
110 }
111 
112 /* Responses are one byte only */
113 static void jlj_read1(struct gspca_dev *gspca_dev, unsigned char *response)
114 {
115 	int retval;
116 
117 	if (gspca_dev->usb_err < 0)
118 		return;
119 	retval = usb_bulk_msg(gspca_dev->dev,
120 	usb_rcvbulkpipe(gspca_dev->dev, 0x84),
121 				gspca_dev->usb_buf, 1, NULL, 500);
122 	*response = gspca_dev->usb_buf[0];
123 	if (retval < 0) {
124 		pr_err("read command [%02x] error %d\n",
125 		       gspca_dev->usb_buf[0], retval);
126 		gspca_dev->usb_err = retval;
127 	}
128 }
129 
130 static void setfreq(struct gspca_dev *gspca_dev, s32 val)
131 {
132 	u8 freq_commands[][2] = {
133 		{0x71, 0x80},
134 		{0x70, 0x07}
135 	};
136 
137 	freq_commands[0][1] |= val >> 1;
138 
139 	jlj_write2(gspca_dev, freq_commands[0]);
140 	jlj_write2(gspca_dev, freq_commands[1]);
141 }
142 
143 static void setcamquality(struct gspca_dev *gspca_dev, s32 val)
144 {
145 	u8 quality_commands[][2] = {
146 		{0x71, 0x1E},
147 		{0x70, 0x06}
148 	};
149 	u8 camquality;
150 
151 	/* adapt camera quality from jpeg quality */
152 	camquality = ((QUALITY_MAX - val) * CAMQUALITY_MAX)
153 		/ (QUALITY_MAX - QUALITY_MIN);
154 	quality_commands[0][1] += camquality;
155 
156 	jlj_write2(gspca_dev, quality_commands[0]);
157 	jlj_write2(gspca_dev, quality_commands[1]);
158 }
159 
160 static void setautogain(struct gspca_dev *gspca_dev, s32 val)
161 {
162 	u8 autogain_commands[][2] = {
163 		{0x94, 0x02},
164 		{0xcf, 0x00}
165 	};
166 
167 	autogain_commands[1][1] = val << 4;
168 
169 	jlj_write2(gspca_dev, autogain_commands[0]);
170 	jlj_write2(gspca_dev, autogain_commands[1]);
171 }
172 
173 static void setred(struct gspca_dev *gspca_dev, s32 val)
174 {
175 	u8 setred_commands[][2] = {
176 		{0x94, 0x02},
177 		{0xe6, 0x00}
178 	};
179 
180 	setred_commands[1][1] = val;
181 
182 	jlj_write2(gspca_dev, setred_commands[0]);
183 	jlj_write2(gspca_dev, setred_commands[1]);
184 }
185 
186 static void setgreen(struct gspca_dev *gspca_dev, s32 val)
187 {
188 	u8 setgreen_commands[][2] = {
189 		{0x94, 0x02},
190 		{0xe7, 0x00}
191 	};
192 
193 	setgreen_commands[1][1] = val;
194 
195 	jlj_write2(gspca_dev, setgreen_commands[0]);
196 	jlj_write2(gspca_dev, setgreen_commands[1]);
197 }
198 
199 static void setblue(struct gspca_dev *gspca_dev, s32 val)
200 {
201 	u8 setblue_commands[][2] = {
202 		{0x94, 0x02},
203 		{0xe9, 0x00}
204 	};
205 
206 	setblue_commands[1][1] = val;
207 
208 	jlj_write2(gspca_dev, setblue_commands[0]);
209 	jlj_write2(gspca_dev, setblue_commands[1]);
210 }
211 
212 static int jlj_start(struct gspca_dev *gspca_dev)
213 {
214 	int i;
215 	int start_commands_size;
216 	u8 response = 0xff;
217 	struct sd *sd = (struct sd *) gspca_dev;
218 	struct jlj_command start_commands[] = {
219 		{{0x71, 0x81}, 0, 0},
220 		{{0x70, 0x05}, 0, JEILINJ_CMD_DELAY},
221 		{{0x95, 0x70}, 1, 0},
222 		{{0x71, 0x81 - gspca_dev->curr_mode}, 0, 0},
223 		{{0x70, 0x04}, 0, JEILINJ_CMD_DELAY},
224 		{{0x95, 0x70}, 1, 0},
225 		{{0x71, 0x00}, 0, 0},   /* start streaming ??*/
226 		{{0x70, 0x08}, 0, JEILINJ_CMD_DELAY},
227 		{{0x95, 0x70}, 1, 0},
228 #define SPORTSCAM_DV15_CMD_SIZE 9
229 		{{0x94, 0x02}, 0, 0},
230 		{{0xde, 0x24}, 0, 0},
231 		{{0x94, 0x02}, 0, 0},
232 		{{0xdd, 0xf0}, 0, 0},
233 		{{0x94, 0x02}, 0, 0},
234 		{{0xe3, 0x2c}, 0, 0},
235 		{{0x94, 0x02}, 0, 0},
236 		{{0xe4, 0x00}, 0, 0},
237 		{{0x94, 0x02}, 0, 0},
238 		{{0xe5, 0x00}, 0, 0},
239 		{{0x94, 0x02}, 0, 0},
240 		{{0xe6, 0x2c}, 0, 0},
241 		{{0x94, 0x03}, 0, 0},
242 		{{0xaa, 0x00}, 0, 0}
243 	};
244 
245 	sd->blocks_left = 0;
246 	/* Under Windows, USB spy shows that only the 9 first start
247 	 * commands are used for SPORTSCAM_DV15 webcam
248 	 */
249 	if (sd->type == SPORTSCAM_DV15)
250 		start_commands_size = SPORTSCAM_DV15_CMD_SIZE;
251 	else
252 		start_commands_size = ARRAY_SIZE(start_commands);
253 
254 	for (i = 0; i < start_commands_size; i++) {
255 		jlj_write2(gspca_dev, start_commands[i].instruction);
256 		if (start_commands[i].delay)
257 			msleep(start_commands[i].delay);
258 		if (start_commands[i].ack_wanted)
259 			jlj_read1(gspca_dev, &response);
260 	}
261 	setcamquality(gspca_dev, v4l2_ctrl_g_ctrl(sd->jpegqual));
262 	msleep(2);
263 	setfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->freq));
264 	if (gspca_dev->usb_err < 0)
265 		PERR("Start streaming command failed");
266 	return gspca_dev->usb_err;
267 }
268 
269 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
270 			u8 *data, int len)
271 {
272 	struct sd *sd = (struct sd *) gspca_dev;
273 	int packet_type;
274 	u32 header_marker;
275 
276 	PDEBUG(D_STREAM, "Got %d bytes out of %d for Block 0",
277 			len, JEILINJ_MAX_TRANSFER);
278 	if (len != JEILINJ_MAX_TRANSFER) {
279 		PDEBUG(D_PACK, "bad length");
280 		goto discard;
281 	}
282 	/* check if it's start of frame */
283 	header_marker = ((u32 *)data)[0];
284 	if (header_marker == FRAME_START) {
285 		sd->blocks_left = data[0x0a] - 1;
286 		PDEBUG(D_STREAM, "blocks_left = 0x%x", sd->blocks_left);
287 		/* Start a new frame, and add the JPEG header, first thing */
288 		gspca_frame_add(gspca_dev, FIRST_PACKET,
289 				sd->jpeg_hdr, JPEG_HDR_SZ);
290 		/* Toss line 0 of data block 0, keep the rest. */
291 		gspca_frame_add(gspca_dev, INTER_PACKET,
292 				data + FRAME_HEADER_LEN,
293 				JEILINJ_MAX_TRANSFER - FRAME_HEADER_LEN);
294 	} else if (sd->blocks_left > 0) {
295 		PDEBUG(D_STREAM, "%d blocks remaining for frame",
296 				sd->blocks_left);
297 		sd->blocks_left -= 1;
298 		if (sd->blocks_left == 0)
299 			packet_type = LAST_PACKET;
300 		else
301 			packet_type = INTER_PACKET;
302 		gspca_frame_add(gspca_dev, packet_type,
303 				data, JEILINJ_MAX_TRANSFER);
304 	} else
305 		goto discard;
306 	return;
307 discard:
308 	/* Discard data until a new frame starts. */
309 	gspca_dev->last_packet_type = DISCARD_PACKET;
310 }
311 
312 /* This function is called at probe time just before sd_init */
313 static int sd_config(struct gspca_dev *gspca_dev,
314 		const struct usb_device_id *id)
315 {
316 	struct cam *cam = &gspca_dev->cam;
317 	struct sd *dev  = (struct sd *) gspca_dev;
318 
319 	dev->type = id->driver_info;
320 	dev->quality = QUALITY_DEF;
321 
322 	cam->cam_mode = jlj_mode;
323 	cam->nmodes = ARRAY_SIZE(jlj_mode);
324 	cam->bulk = 1;
325 	cam->bulk_nurbs = 1;
326 	cam->bulk_size = JEILINJ_MAX_TRANSFER;
327 	return 0;
328 }
329 
330 static void sd_stopN(struct gspca_dev *gspca_dev)
331 {
332 	int i;
333 	u8 *buf;
334 	static u8 stop_commands[][2] = {
335 		{0x71, 0x00},
336 		{0x70, 0x09},
337 		{0x71, 0x80},
338 		{0x70, 0x05}
339 	};
340 
341 	for (;;) {
342 		/* get the image remaining blocks */
343 		usb_bulk_msg(gspca_dev->dev,
344 				gspca_dev->urb[0]->pipe,
345 				gspca_dev->urb[0]->transfer_buffer,
346 				JEILINJ_MAX_TRANSFER, NULL,
347 				JEILINJ_DATA_TIMEOUT);
348 
349 		/* search for 0xff 0xd9  (EOF for JPEG) */
350 		i = 0;
351 		buf = gspca_dev->urb[0]->transfer_buffer;
352 		while ((i < (JEILINJ_MAX_TRANSFER - 1)) &&
353 			((buf[i] != 0xff) || (buf[i+1] != 0xd9)))
354 			i++;
355 
356 		if (i != (JEILINJ_MAX_TRANSFER - 1))
357 			/* last remaining block found */
358 			break;
359 		}
360 
361 	for (i = 0; i < ARRAY_SIZE(stop_commands); i++)
362 		jlj_write2(gspca_dev, stop_commands[i]);
363 }
364 
365 /* this function is called at probe and resume time */
366 static int sd_init(struct gspca_dev *gspca_dev)
367 {
368 	return gspca_dev->usb_err;
369 }
370 
371 /* Set up for getting frames. */
372 static int sd_start(struct gspca_dev *gspca_dev)
373 {
374 	struct sd *dev = (struct sd *) gspca_dev;
375 
376 	/* create the JPEG header */
377 	jpeg_define(dev->jpeg_hdr, gspca_dev->pixfmt.height,
378 			gspca_dev->pixfmt.width,
379 			0x21);          /* JPEG 422 */
380 	jpeg_set_qual(dev->jpeg_hdr, dev->quality);
381 	PDEBUG(D_STREAM, "Start streaming at %dx%d",
382 		gspca_dev->pixfmt.height, gspca_dev->pixfmt.width);
383 	jlj_start(gspca_dev);
384 	return gspca_dev->usb_err;
385 }
386 
387 /* Table of supported USB devices */
388 static const struct usb_device_id device_table[] = {
389 	{USB_DEVICE(0x0979, 0x0280), .driver_info = SAKAR_57379},
390 	{USB_DEVICE(0x0979, 0x0270), .driver_info = SPORTSCAM_DV15},
391 	{}
392 };
393 
394 MODULE_DEVICE_TABLE(usb, device_table);
395 
396 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
397 {
398 	struct gspca_dev *gspca_dev =
399 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
400 	struct sd *sd = (struct sd *)gspca_dev;
401 
402 	gspca_dev->usb_err = 0;
403 
404 	if (!gspca_dev->streaming)
405 		return 0;
406 
407 	switch (ctrl->id) {
408 	case V4L2_CID_POWER_LINE_FREQUENCY:
409 		setfreq(gspca_dev, ctrl->val);
410 		break;
411 	case V4L2_CID_RED_BALANCE:
412 		setred(gspca_dev, ctrl->val);
413 		break;
414 	case V4L2_CID_GAIN:
415 		setgreen(gspca_dev, ctrl->val);
416 		break;
417 	case V4L2_CID_BLUE_BALANCE:
418 		setblue(gspca_dev, ctrl->val);
419 		break;
420 	case V4L2_CID_AUTOGAIN:
421 		setautogain(gspca_dev, ctrl->val);
422 		break;
423 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
424 		jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
425 		setcamquality(gspca_dev, ctrl->val);
426 		break;
427 	}
428 	return gspca_dev->usb_err;
429 }
430 
431 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
432 	.s_ctrl = sd_s_ctrl,
433 };
434 
435 static int sd_init_controls(struct gspca_dev *gspca_dev)
436 {
437 	struct sd *sd = (struct sd *)gspca_dev;
438 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
439 	static const struct v4l2_ctrl_config custom_autogain = {
440 		.ops = &sd_ctrl_ops,
441 		.id = V4L2_CID_AUTOGAIN,
442 		.type = V4L2_CTRL_TYPE_INTEGER,
443 		.name = "Automatic Gain (and Exposure)",
444 		.max = 3,
445 		.step = 1,
446 		.def = 0,
447 	};
448 
449 	gspca_dev->vdev.ctrl_handler = hdl;
450 	v4l2_ctrl_handler_init(hdl, 6);
451 	sd->freq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
452 			V4L2_CID_POWER_LINE_FREQUENCY,
453 			V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1,
454 			V4L2_CID_POWER_LINE_FREQUENCY_60HZ);
455 	v4l2_ctrl_new_custom(hdl, &custom_autogain, NULL);
456 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
457 			V4L2_CID_RED_BALANCE, 0, 3, 1, 2);
458 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
459 			V4L2_CID_GAIN, 0, 3, 1, 2);
460 	v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
461 			V4L2_CID_BLUE_BALANCE, 0, 3, 1, 2);
462 	sd->jpegqual = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
463 			V4L2_CID_JPEG_COMPRESSION_QUALITY,
464 			QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
465 
466 	if (hdl->error) {
467 		pr_err("Could not initialize controls\n");
468 		return hdl->error;
469 	}
470 	return 0;
471 }
472 
473 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
474 			const struct v4l2_jpegcompression *jcomp)
475 {
476 	struct sd *sd = (struct sd *) gspca_dev;
477 
478 	v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
479 	return 0;
480 }
481 
482 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
483 			struct v4l2_jpegcompression *jcomp)
484 {
485 	struct sd *sd = (struct sd *) gspca_dev;
486 
487 	memset(jcomp, 0, sizeof *jcomp);
488 	jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
489 	jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
490 			| V4L2_JPEG_MARKER_DQT;
491 	return 0;
492 }
493 
494 
495 /* sub-driver description */
496 static const struct sd_desc sd_desc_sakar_57379 = {
497 	.name   = MODULE_NAME,
498 	.config = sd_config,
499 	.init   = sd_init,
500 	.start  = sd_start,
501 	.stopN  = sd_stopN,
502 	.pkt_scan = sd_pkt_scan,
503 };
504 
505 /* sub-driver description */
506 static const struct sd_desc sd_desc_sportscam_dv15 = {
507 	.name   = MODULE_NAME,
508 	.config = sd_config,
509 	.init   = sd_init,
510 	.init_controls = sd_init_controls,
511 	.start  = sd_start,
512 	.stopN  = sd_stopN,
513 	.pkt_scan = sd_pkt_scan,
514 	.get_jcomp = sd_get_jcomp,
515 	.set_jcomp = sd_set_jcomp,
516 };
517 
518 static const struct sd_desc *sd_desc[2] = {
519 	&sd_desc_sakar_57379,
520 	&sd_desc_sportscam_dv15
521 };
522 
523 /* -- device connect -- */
524 static int sd_probe(struct usb_interface *intf,
525 		const struct usb_device_id *id)
526 {
527 	return gspca_dev_probe(intf, id,
528 			sd_desc[id->driver_info],
529 			sizeof(struct sd),
530 			THIS_MODULE);
531 }
532 
533 static struct usb_driver sd_driver = {
534 	.name       = MODULE_NAME,
535 	.id_table   = device_table,
536 	.probe      = sd_probe,
537 	.disconnect = gspca_disconnect,
538 #ifdef CONFIG_PM
539 	.suspend = gspca_suspend,
540 	.resume  = gspca_resume,
541 	.reset_resume = gspca_resume,
542 #endif
543 };
544 
545 module_usb_driver(sd_driver);
546