xref: /linux/drivers/media/usb/gspca/jl2005bcd.c (revision 9cebfe6504488198b012e746bc6b313f88b95439)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Jeilin JL2005B/C/D library
4  *
5  * Copyright (C) 2011 Theodore Kilgore <kilgota@auburn.edu>
6  */
7 
8 #define MODULE_NAME "jl2005bcd"
9 
10 #include <linux/workqueue.h>
11 #include <linux/slab.h>
12 #include "gspca.h"
13 
14 
15 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
16 MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver");
17 MODULE_LICENSE("GPL");
18 
19 /* Default timeouts, in ms */
20 #define JL2005C_CMD_TIMEOUT 500
21 #define JL2005C_DATA_TIMEOUT 1000
22 
23 /* Maximum transfer size to use. */
24 #define JL2005C_MAX_TRANSFER 0x200
25 #define FRAME_HEADER_LEN 16
26 
27 
28 /* specific webcam descriptor */
29 struct sd {
30 	struct gspca_dev gspca_dev;  /* !! must be the first item */
31 	unsigned char firmware_id[6];
32 	const struct v4l2_pix_format *cap_mode;
33 	/* Driver stuff */
34 	struct work_struct work_struct;
35 	u8 frame_brightness;
36 	int block_size;	/* block size of camera */
37 	int vga;	/* 1 if vga cam, 0 if cif cam */
38 };
39 
40 
41 /* Camera has two resolution settings. What they are depends on model. */
42 static const struct v4l2_pix_format cif_mode[] = {
43 	{176, 144, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
44 		.bytesperline = 176,
45 		.sizeimage = 176 * 144,
46 		.colorspace = V4L2_COLORSPACE_SRGB,
47 		.priv = 0},
48 	{352, 288, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
49 		.bytesperline = 352,
50 		.sizeimage = 352 * 288,
51 		.colorspace = V4L2_COLORSPACE_SRGB,
52 		.priv = 0},
53 };
54 
55 static const struct v4l2_pix_format vga_mode[] = {
56 	{320, 240, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
57 		.bytesperline = 320,
58 		.sizeimage = 320 * 240,
59 		.colorspace = V4L2_COLORSPACE_SRGB,
60 		.priv = 0},
61 	{640, 480, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
62 		.bytesperline = 640,
63 		.sizeimage = 640 * 480,
64 		.colorspace = V4L2_COLORSPACE_SRGB,
65 		.priv = 0},
66 };
67 
68 /*
69  * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
70  * and 0x82 for bulk data transfer.
71  */
72 
73 /* All commands are two bytes only */
74 static int jl2005c_write2(struct gspca_dev *gspca_dev, unsigned char *command)
75 {
76 	int retval;
77 
78 	memcpy(gspca_dev->usb_buf, command, 2);
79 	retval = usb_bulk_msg(gspca_dev->dev,
80 			usb_sndbulkpipe(gspca_dev->dev, 3),
81 			gspca_dev->usb_buf, 2, NULL, 500);
82 	if (retval < 0)
83 		pr_err("command write [%02x] error %d\n",
84 		       gspca_dev->usb_buf[0], retval);
85 	return retval;
86 }
87 
88 /* Response to a command is one byte in usb_buf[0], only if requested. */
89 static int jl2005c_read1(struct gspca_dev *gspca_dev)
90 {
91 	int retval;
92 
93 	retval = usb_bulk_msg(gspca_dev->dev,
94 				usb_rcvbulkpipe(gspca_dev->dev, 0x84),
95 				gspca_dev->usb_buf, 1, NULL, 500);
96 	if (retval < 0)
97 		pr_err("read command [0x%02x] error %d\n",
98 		       gspca_dev->usb_buf[0], retval);
99 	return retval;
100 }
101 
102 /* Response appears in gspca_dev->usb_buf[0] */
103 static int jl2005c_read_reg(struct gspca_dev *gspca_dev, unsigned char reg)
104 {
105 	int retval;
106 
107 	static u8 instruction[2] = {0x95, 0x00};
108 	/* put register to read in byte 1 */
109 	instruction[1] = reg;
110 	/* Send the read request */
111 	retval = jl2005c_write2(gspca_dev, instruction);
112 	if (retval < 0)
113 		return retval;
114 	retval = jl2005c_read1(gspca_dev);
115 
116 	return retval;
117 }
118 
119 static int jl2005c_start_new_frame(struct gspca_dev *gspca_dev)
120 {
121 	int i;
122 	int retval;
123 	int frame_brightness = 0;
124 
125 	static u8 instruction[2] = {0x7f, 0x01};
126 
127 	retval = jl2005c_write2(gspca_dev, instruction);
128 	if (retval < 0)
129 		return retval;
130 
131 	i = 0;
132 	while (i < 20 && !frame_brightness) {
133 		/* If we tried 20 times, give up. */
134 		retval = jl2005c_read_reg(gspca_dev, 0x7e);
135 		if (retval < 0)
136 			return retval;
137 		frame_brightness = gspca_dev->usb_buf[0];
138 		retval = jl2005c_read_reg(gspca_dev, 0x7d);
139 		if (retval < 0)
140 			return retval;
141 		i++;
142 	}
143 	gspca_dbg(gspca_dev, D_FRAM, "frame_brightness is 0x%02x\n",
144 		  gspca_dev->usb_buf[0]);
145 	return retval;
146 }
147 
148 static int jl2005c_write_reg(struct gspca_dev *gspca_dev, unsigned char reg,
149 						    unsigned char value)
150 {
151 	u8 instruction[2];
152 
153 	instruction[0] = reg;
154 	instruction[1] = value;
155 
156 	return jl2005c_write2(gspca_dev, instruction);
157 }
158 
159 static int jl2005c_get_firmware_id(struct gspca_dev *gspca_dev)
160 {
161 	struct sd *sd = (struct sd *)gspca_dev;
162 	int i = 0;
163 	int retval;
164 	static const unsigned char regs_to_read[] = {
165 		0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f
166 	};
167 
168 	gspca_dbg(gspca_dev, D_PROBE, "Running jl2005c_get_firmware_id\n");
169 	/* Read the first ID byte once for warmup */
170 	retval = jl2005c_read_reg(gspca_dev, regs_to_read[0]);
171 	gspca_dbg(gspca_dev, D_PROBE, "response is %02x\n",
172 		  gspca_dev->usb_buf[0]);
173 	if (retval < 0)
174 		return retval;
175 	/* Now actually get the ID string */
176 	for (i = 0; i < 6; i++) {
177 		retval = jl2005c_read_reg(gspca_dev, regs_to_read[i]);
178 		if (retval < 0)
179 			return retval;
180 		sd->firmware_id[i] = gspca_dev->usb_buf[0];
181 	}
182 	gspca_dbg(gspca_dev, D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x\n",
183 		  sd->firmware_id[0],
184 		  sd->firmware_id[1],
185 		  sd->firmware_id[2],
186 		  sd->firmware_id[3],
187 		  sd->firmware_id[4],
188 		  sd->firmware_id[5]);
189 	return 0;
190 }
191 
192 static int jl2005c_stream_start_vga_lg
193 		    (struct gspca_dev *gspca_dev)
194 {
195 	int i;
196 	int retval = -1;
197 	static u8 instruction[][2] = {
198 		{0x05, 0x00},
199 		{0x7c, 0x00},
200 		{0x7d, 0x18},
201 		{0x02, 0x00},
202 		{0x01, 0x00},
203 		{0x04, 0x52},
204 	};
205 
206 	for (i = 0; i < ARRAY_SIZE(instruction); i++) {
207 		msleep(60);
208 		retval = jl2005c_write2(gspca_dev, instruction[i]);
209 		if (retval < 0)
210 			return retval;
211 	}
212 	msleep(60);
213 	return retval;
214 }
215 
216 static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev)
217 {
218 	int i;
219 	int retval = -1;
220 	static u8 instruction[][2] = {
221 		{0x06, 0x00},
222 		{0x7c, 0x00},
223 		{0x7d, 0x1a},
224 		{0x02, 0x00},
225 		{0x01, 0x00},
226 		{0x04, 0x52},
227 	};
228 
229 	for (i = 0; i < ARRAY_SIZE(instruction); i++) {
230 		msleep(60);
231 		retval = jl2005c_write2(gspca_dev, instruction[i]);
232 		if (retval < 0)
233 			return retval;
234 	}
235 	msleep(60);
236 	return retval;
237 }
238 
239 static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev)
240 {
241 	int i;
242 	int retval = -1;
243 	static u8 instruction[][2] = {
244 		{0x05, 0x00},
245 		{0x7c, 0x00},
246 		{0x7d, 0x30},
247 		{0x02, 0x00},
248 		{0x01, 0x00},
249 		{0x04, 0x42},
250 	};
251 
252 	for (i = 0; i < ARRAY_SIZE(instruction); i++) {
253 		msleep(60);
254 		retval = jl2005c_write2(gspca_dev, instruction[i]);
255 		if (retval < 0)
256 			return retval;
257 	}
258 	msleep(60);
259 	return retval;
260 }
261 
262 static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev)
263 {
264 	int i;
265 	int retval = -1;
266 	static u8 instruction[][2] = {
267 		{0x06, 0x00},
268 		{0x7c, 0x00},
269 		{0x7d, 0x32},
270 		{0x02, 0x00},
271 		{0x01, 0x00},
272 		{0x04, 0x42},
273 	};
274 
275 	for (i = 0; i < ARRAY_SIZE(instruction); i++) {
276 		msleep(60);
277 		retval = jl2005c_write2(gspca_dev, instruction[i]);
278 		if (retval < 0)
279 			return retval;
280 	}
281 	msleep(60);
282 	return retval;
283 }
284 
285 
286 static int jl2005c_stop(struct gspca_dev *gspca_dev)
287 {
288 	return jl2005c_write_reg(gspca_dev, 0x07, 0x00);
289 }
290 
291 /*
292  * This function is called as a workqueue function and runs whenever the camera
293  * is streaming data. Because it is a workqueue function it is allowed to sleep
294  * so we can use synchronous USB calls. To avoid possible collisions with other
295  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
296  * performing USB operations using it. In practice we don't really need this
297  * as the camera doesn't provide any controls.
298  */
299 static void jl2005c_dostream(struct work_struct *work)
300 {
301 	struct sd *dev = container_of(work, struct sd, work_struct);
302 	struct gspca_dev *gspca_dev = &dev->gspca_dev;
303 	int bytes_left = 0; /* bytes remaining in current frame. */
304 	int data_len;   /* size to use for the next read. */
305 	int header_read = 0;
306 	unsigned char header_sig[2] = {0x4a, 0x4c};
307 	int act_len;
308 	int packet_type;
309 	int ret;
310 	u8 *buffer;
311 
312 	buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL);
313 	if (!buffer) {
314 		pr_err("Couldn't allocate USB buffer\n");
315 		goto quit_stream;
316 	}
317 
318 	while (gspca_dev->present && gspca_dev->streaming) {
319 #ifdef CONFIG_PM
320 		if (gspca_dev->frozen)
321 			break;
322 #endif
323 		/* Check if this is a new frame. If so, start the frame first */
324 		if (!header_read) {
325 			mutex_lock(&gspca_dev->usb_lock);
326 			ret = jl2005c_start_new_frame(gspca_dev);
327 			mutex_unlock(&gspca_dev->usb_lock);
328 			if (ret < 0)
329 				goto quit_stream;
330 			ret = usb_bulk_msg(gspca_dev->dev,
331 				usb_rcvbulkpipe(gspca_dev->dev, 0x82),
332 				buffer, JL2005C_MAX_TRANSFER, &act_len,
333 				JL2005C_DATA_TIMEOUT);
334 			gspca_dbg(gspca_dev, D_PACK,
335 				  "Got %d bytes out of %d for header\n",
336 				  act_len, JL2005C_MAX_TRANSFER);
337 			if (ret < 0 || act_len < JL2005C_MAX_TRANSFER)
338 				goto quit_stream;
339 			/* Check whether we actually got the first blodk */
340 			if (memcmp(header_sig, buffer, 2) != 0) {
341 				pr_err("First block is not the first block\n");
342 				goto quit_stream;
343 			}
344 			/* total size to fetch is byte 7, times blocksize
345 			 * of which we already got act_len */
346 			bytes_left = buffer[0x07] * dev->block_size - act_len;
347 			gspca_dbg(gspca_dev, D_PACK, "bytes_left = 0x%x\n",
348 				  bytes_left);
349 			/* We keep the header. It has other information, too.*/
350 			packet_type = FIRST_PACKET;
351 			gspca_frame_add(gspca_dev, packet_type,
352 					buffer, act_len);
353 			header_read = 1;
354 		}
355 		while (bytes_left > 0 && gspca_dev->present) {
356 			data_len = bytes_left > JL2005C_MAX_TRANSFER ?
357 				JL2005C_MAX_TRANSFER : bytes_left;
358 			ret = usb_bulk_msg(gspca_dev->dev,
359 				usb_rcvbulkpipe(gspca_dev->dev, 0x82),
360 				buffer, data_len, &act_len,
361 				JL2005C_DATA_TIMEOUT);
362 			if (ret < 0 || act_len < data_len)
363 				goto quit_stream;
364 			gspca_dbg(gspca_dev, D_PACK,
365 				  "Got %d bytes out of %d for frame\n",
366 				  data_len, bytes_left);
367 			bytes_left -= data_len;
368 			if (bytes_left == 0) {
369 				packet_type = LAST_PACKET;
370 				header_read = 0;
371 			} else
372 				packet_type = INTER_PACKET;
373 			gspca_frame_add(gspca_dev, packet_type,
374 					buffer, data_len);
375 		}
376 	}
377 quit_stream:
378 	if (gspca_dev->present) {
379 		mutex_lock(&gspca_dev->usb_lock);
380 		jl2005c_stop(gspca_dev);
381 		mutex_unlock(&gspca_dev->usb_lock);
382 	}
383 	kfree(buffer);
384 }
385 
386 
387 
388 
389 /* This function is called at probe time */
390 static int sd_config(struct gspca_dev *gspca_dev,
391 			const struct usb_device_id *id)
392 {
393 	struct cam *cam;
394 	struct sd *sd = (struct sd *) gspca_dev;
395 
396 	cam = &gspca_dev->cam;
397 	/* We don't use the buffer gspca allocates so make it small. */
398 	cam->bulk_size = 64;
399 	cam->bulk = 1;
400 	/* For the rest, the camera needs to be detected */
401 	jl2005c_get_firmware_id(gspca_dev);
402 	/* Here are some known firmware IDs
403 	 * First some JL2005B cameras
404 	 * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2}	Sakar KidzCam
405 	 * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2}	No-name JL2005B
406 	 * JL2005C cameras
407 	 * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8}	Argus DC-1512
408 	 * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8}	ICarly
409 	 * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4}	Jazz
410 	 *
411 	 * Based upon this scanty evidence, we can detect a CIF camera by
412 	 * testing byte 0 for 0x4x.
413 	 */
414 	if ((sd->firmware_id[0] & 0xf0) == 0x40) {
415 		cam->cam_mode	= cif_mode;
416 		cam->nmodes	= ARRAY_SIZE(cif_mode);
417 		sd->block_size	= 0x80;
418 	} else {
419 		cam->cam_mode	= vga_mode;
420 		cam->nmodes	= ARRAY_SIZE(vga_mode);
421 		sd->block_size	= 0x200;
422 	}
423 
424 	INIT_WORK(&sd->work_struct, jl2005c_dostream);
425 
426 	return 0;
427 }
428 
429 /* this function is called at probe and resume time */
430 static int sd_init(struct gspca_dev *gspca_dev)
431 {
432 	return 0;
433 }
434 
435 static int sd_start(struct gspca_dev *gspca_dev)
436 {
437 
438 	struct sd *sd = (struct sd *) gspca_dev;
439 	sd->cap_mode = gspca_dev->cam.cam_mode;
440 
441 	switch (gspca_dev->pixfmt.width) {
442 	case 640:
443 		gspca_dbg(gspca_dev, D_STREAM, "Start streaming at vga resolution\n");
444 		jl2005c_stream_start_vga_lg(gspca_dev);
445 		break;
446 	case 320:
447 		gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qvga resolution\n");
448 		jl2005c_stream_start_vga_small(gspca_dev);
449 		break;
450 	case 352:
451 		gspca_dbg(gspca_dev, D_STREAM, "Start streaming at cif resolution\n");
452 		jl2005c_stream_start_cif_lg(gspca_dev);
453 		break;
454 	case 176:
455 		gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qcif resolution\n");
456 		jl2005c_stream_start_cif_small(gspca_dev);
457 		break;
458 	default:
459 		pr_err("Unknown resolution specified\n");
460 		return -1;
461 	}
462 
463 	schedule_work(&sd->work_struct);
464 
465 	return 0;
466 }
467 
468 /* called on streamoff with alt==0 and on disconnect */
469 /* the usb_lock is held at entry - restore on exit */
470 static void sd_stop0(struct gspca_dev *gspca_dev)
471 {
472 	struct sd *dev = (struct sd *) gspca_dev;
473 
474 	/* wait for the work queue to terminate */
475 	mutex_unlock(&gspca_dev->usb_lock);
476 	/* This waits for sq905c_dostream to finish */
477 	flush_work(&dev->work_struct);
478 	mutex_lock(&gspca_dev->usb_lock);
479 }
480 
481 
482 
483 /* sub-driver description */
484 static const struct sd_desc sd_desc = {
485 	.name = MODULE_NAME,
486 	.config = sd_config,
487 	.init = sd_init,
488 	.start = sd_start,
489 	.stop0 = sd_stop0,
490 };
491 
492 /* -- module initialisation -- */
493 static const struct usb_device_id device_table[] = {
494 	{USB_DEVICE(0x0979, 0x0227)},
495 	{}
496 };
497 MODULE_DEVICE_TABLE(usb, device_table);
498 
499 /* -- device connect -- */
500 static int sd_probe(struct usb_interface *intf,
501 				const struct usb_device_id *id)
502 {
503 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
504 				THIS_MODULE);
505 }
506 
507 static struct usb_driver sd_driver = {
508 	.name = MODULE_NAME,
509 	.id_table = device_table,
510 	.probe = sd_probe,
511 	.disconnect = gspca_disconnect,
512 #ifdef CONFIG_PM
513 	.suspend = gspca_suspend,
514 	.resume = gspca_resume,
515 	.reset_resume = gspca_resume,
516 #endif
517 };
518 
519 module_usb_driver(sd_driver);
520