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