2 * Jeilin JL2005B/C/D library
4 * Copyright (C) 2011 Theodore Kilgore <kilgota@auburn.edu>
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
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.
17 #define MODULE_NAME "jl2005bcd"
19 #include <linux/workqueue.h>
20 #include <linux/slab.h>
24 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
25 MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver");
26 MODULE_LICENSE("GPL");
28 /* Default timeouts, in ms */
29 #define JL2005C_CMD_TIMEOUT 500
30 #define JL2005C_DATA_TIMEOUT 1000
32 /* Maximum transfer size to use. */
33 #define JL2005C_MAX_TRANSFER 0x200
34 #define FRAME_HEADER_LEN 16
37 /* specific webcam descriptor */
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;
43 struct work_struct work_struct;
45 int block_size; /* block size of camera */
46 int vga; /* 1 if vga cam, 0 if cif cam */
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,
54 .sizeimage = 176 * 144,
55 .colorspace = V4L2_COLORSPACE_SRGB,
57 {352, 288, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
59 .sizeimage = 352 * 288,
60 .colorspace = V4L2_COLORSPACE_SRGB,
64 static const struct v4l2_pix_format vga_mode[] = {
65 {320, 240, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
67 .sizeimage = 320 * 240,
68 .colorspace = V4L2_COLORSPACE_SRGB,
70 {640, 480, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
72 .sizeimage = 640 * 480,
73 .colorspace = V4L2_COLORSPACE_SRGB,
78 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
79 * and 0x82 for bulk data transfer.
82 /* All commands are two bytes only */
83 static int jl2005c_write2(struct gspca_dev *gspca_dev, unsigned char *command)
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);
92 pr_err("command write [%02x] error %d\n",
93 gspca_dev->usb_buf[0], retval);
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)
102 retval = usb_bulk_msg(gspca_dev->dev,
103 usb_rcvbulkpipe(gspca_dev->dev, 0x84),
104 gspca_dev->usb_buf, 1, NULL, 500);
106 pr_err("read command [0x%02x] error %d\n",
107 gspca_dev->usb_buf[0], retval);
111 /* Response appears in gspca_dev->usb_buf[0] */
112 static int jl2005c_read_reg(struct gspca_dev *gspca_dev, unsigned char reg)
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);
123 retval = jl2005c_read1(gspca_dev);
128 static int jl2005c_start_new_frame(struct gspca_dev *gspca_dev)
132 int frame_brightness = 0;
134 static u8 instruction[2] = {0x7f, 0x01};
136 retval = jl2005c_write2(gspca_dev, instruction);
141 while (i < 20 && !frame_brightness) {
142 /* If we tried 20 times, give up. */
143 retval = jl2005c_read_reg(gspca_dev, 0x7e);
146 frame_brightness = gspca_dev->usb_buf[0];
147 retval = jl2005c_read_reg(gspca_dev, 0x7d);
152 gspca_dbg(gspca_dev, D_FRAM, "frame_brightness is 0x%02x\n",
153 gspca_dev->usb_buf[0]);
157 static int jl2005c_write_reg(struct gspca_dev *gspca_dev, unsigned char reg,
163 instruction[0] = reg;
164 instruction[1] = value;
166 retval = jl2005c_write2(gspca_dev, instruction);
173 static int jl2005c_get_firmware_id(struct gspca_dev *gspca_dev)
175 struct sd *sd = (struct sd *)gspca_dev;
178 unsigned char regs_to_read[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f};
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]);
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]);
192 sd->firmware_id[i] = gspca_dev->usb_buf[0];
194 gspca_dbg(gspca_dev, D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x\n",
204 static int jl2005c_stream_start_vga_lg
205 (struct gspca_dev *gspca_dev)
209 static u8 instruction[][2] = {
218 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
220 retval = jl2005c_write2(gspca_dev, instruction[i]);
228 static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev)
232 static u8 instruction[][2] = {
241 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
243 retval = jl2005c_write2(gspca_dev, instruction[i]);
251 static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev)
255 static u8 instruction[][2] = {
264 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
266 retval = jl2005c_write2(gspca_dev, instruction[i]);
274 static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev)
278 static u8 instruction[][2] = {
287 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
289 retval = jl2005c_write2(gspca_dev, instruction[i]);
298 static int jl2005c_stop(struct gspca_dev *gspca_dev)
300 return jl2005c_write_reg(gspca_dev, 0x07, 0x00);
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.
311 static void jl2005c_dostream(struct work_struct *work)
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. */
318 unsigned char header_sig[2] = {0x4a, 0x4c};
324 buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL);
326 pr_err("Couldn't allocate USB buffer\n");
330 while (gspca_dev->present && gspca_dev->streaming) {
332 if (gspca_dev->frozen)
335 /* Check if this is a new frame. If so, start the frame first */
337 mutex_lock(&gspca_dev->usb_lock);
338 ret = jl2005c_start_new_frame(gspca_dev);
339 mutex_unlock(&gspca_dev->usb_lock);
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)
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");
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",
361 /* We keep the header. It has other information, too.*/
362 packet_type = FIRST_PACKET;
363 gspca_frame_add(gspca_dev, packet_type,
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)
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;
384 packet_type = INTER_PACKET;
385 gspca_frame_add(gspca_dev, packet_type,
390 if (gspca_dev->present) {
391 mutex_lock(&gspca_dev->usb_lock);
392 jl2005c_stop(gspca_dev);
393 mutex_unlock(&gspca_dev->usb_lock);
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)
406 struct sd *sd = (struct sd *) gspca_dev;
408 cam = &gspca_dev->cam;
409 /* We don't use the buffer gspca allocates so make it small. */
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
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
423 * Based upon this scanty evidence, we can detect a CIF camera by
424 * testing byte 0 for 0x4x.
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;
431 cam->cam_mode = vga_mode;
432 cam->nmodes = ARRAY_SIZE(vga_mode);
433 sd->block_size = 0x200;
436 INIT_WORK(&sd->work_struct, jl2005c_dostream);
441 /* this function is called at probe and resume time */
442 static int sd_init(struct gspca_dev *gspca_dev)
447 static int sd_start(struct gspca_dev *gspca_dev)
450 struct sd *sd = (struct sd *) gspca_dev;
451 sd->cap_mode = gspca_dev->cam.cam_mode;
453 switch (gspca_dev->pixfmt.width) {
455 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at vga resolution\n");
456 jl2005c_stream_start_vga_lg(gspca_dev);
459 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qvga resolution\n");
460 jl2005c_stream_start_vga_small(gspca_dev);
463 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at cif resolution\n");
464 jl2005c_stream_start_cif_lg(gspca_dev);
467 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qcif resolution\n");
468 jl2005c_stream_start_cif_small(gspca_dev);
471 pr_err("Unknown resolution specified\n");
475 schedule_work(&sd->work_struct);
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)
484 struct sd *dev = (struct sd *) gspca_dev;
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);
495 /* sub-driver description */
496 static const struct sd_desc sd_desc = {
504 /* -- module initialisation -- */
505 static const struct usb_device_id device_table[] = {
506 {USB_DEVICE(0x0979, 0x0227)},
509 MODULE_DEVICE_TABLE(usb, device_table);
511 /* -- device connect -- */
512 static int sd_probe(struct usb_interface *intf,
513 const struct usb_device_id *id)
515 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
519 static struct usb_driver sd_driver = {
521 .id_table = device_table,
523 .disconnect = gspca_disconnect,
525 .suspend = gspca_suspend,
526 .resume = gspca_resume,
527 .reset_resume = gspca_resume,
531 module_usb_driver(sd_driver);