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 PDEBUG(D_FRAM, "frame_brightness is 0x%02x", gspca_dev->usb_buf[0]);
156 static int jl2005c_write_reg(struct gspca_dev *gspca_dev, unsigned char reg,
162 instruction[0] = reg;
163 instruction[1] = value;
165 retval = jl2005c_write2(gspca_dev, instruction);
172 static int jl2005c_get_firmware_id(struct gspca_dev *gspca_dev)
174 struct sd *sd = (struct sd *)gspca_dev;
177 unsigned char regs_to_read[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f};
179 PDEBUG(D_PROBE, "Running jl2005c_get_firmware_id");
180 /* Read the first ID byte once for warmup */
181 retval = jl2005c_read_reg(gspca_dev, regs_to_read[0]);
182 PDEBUG(D_PROBE, "response is %02x", gspca_dev->usb_buf[0]);
185 /* Now actually get the ID string */
186 for (i = 0; i < 6; i++) {
187 retval = jl2005c_read_reg(gspca_dev, regs_to_read[i]);
190 sd->firmware_id[i] = gspca_dev->usb_buf[0];
192 PDEBUG(D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x",
202 static int jl2005c_stream_start_vga_lg
203 (struct gspca_dev *gspca_dev)
207 static u8 instruction[][2] = {
216 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
218 retval = jl2005c_write2(gspca_dev, instruction[i]);
226 static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev)
230 static u8 instruction[][2] = {
239 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
241 retval = jl2005c_write2(gspca_dev, instruction[i]);
249 static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev)
253 static u8 instruction[][2] = {
262 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
264 retval = jl2005c_write2(gspca_dev, instruction[i]);
272 static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev)
276 static u8 instruction[][2] = {
285 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
287 retval = jl2005c_write2(gspca_dev, instruction[i]);
296 static int jl2005c_stop(struct gspca_dev *gspca_dev)
298 return jl2005c_write_reg(gspca_dev, 0x07, 0x00);
302 * This function is called as a workqueue function and runs whenever the camera
303 * is streaming data. Because it is a workqueue function it is allowed to sleep
304 * so we can use synchronous USB calls. To avoid possible collisions with other
305 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
306 * performing USB operations using it. In practice we don't really need this
307 * as the camera doesn't provide any controls.
309 static void jl2005c_dostream(struct work_struct *work)
311 struct sd *dev = container_of(work, struct sd, work_struct);
312 struct gspca_dev *gspca_dev = &dev->gspca_dev;
313 int bytes_left = 0; /* bytes remaining in current frame. */
314 int data_len; /* size to use for the next read. */
316 unsigned char header_sig[2] = {0x4a, 0x4c};
322 buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
324 pr_err("Couldn't allocate USB buffer\n");
328 while (gspca_dev->present && gspca_dev->streaming) {
330 if (gspca_dev->frozen)
333 /* Check if this is a new frame. If so, start the frame first */
335 mutex_lock(&gspca_dev->usb_lock);
336 ret = jl2005c_start_new_frame(gspca_dev);
337 mutex_unlock(&gspca_dev->usb_lock);
340 ret = usb_bulk_msg(gspca_dev->dev,
341 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
342 buffer, JL2005C_MAX_TRANSFER, &act_len,
343 JL2005C_DATA_TIMEOUT);
345 "Got %d bytes out of %d for header",
346 act_len, JL2005C_MAX_TRANSFER);
347 if (ret < 0 || act_len < JL2005C_MAX_TRANSFER)
349 /* Check whether we actually got the first blodk */
350 if (memcmp(header_sig, buffer, 2) != 0) {
351 pr_err("First block is not the first block\n");
354 /* total size to fetch is byte 7, times blocksize
355 * of which we already got act_len */
356 bytes_left = buffer[0x07] * dev->block_size - act_len;
357 PDEBUG(D_PACK, "bytes_left = 0x%x", bytes_left);
358 /* We keep the header. It has other information, too.*/
359 packet_type = FIRST_PACKET;
360 gspca_frame_add(gspca_dev, packet_type,
364 while (bytes_left > 0 && gspca_dev->present) {
365 data_len = bytes_left > JL2005C_MAX_TRANSFER ?
366 JL2005C_MAX_TRANSFER : bytes_left;
367 ret = usb_bulk_msg(gspca_dev->dev,
368 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
369 buffer, data_len, &act_len,
370 JL2005C_DATA_TIMEOUT);
371 if (ret < 0 || act_len < data_len)
374 "Got %d bytes out of %d for frame",
375 data_len, bytes_left);
376 bytes_left -= data_len;
377 if (bytes_left == 0) {
378 packet_type = LAST_PACKET;
381 packet_type = INTER_PACKET;
382 gspca_frame_add(gspca_dev, packet_type,
387 if (gspca_dev->present) {
388 mutex_lock(&gspca_dev->usb_lock);
389 jl2005c_stop(gspca_dev);
390 mutex_unlock(&gspca_dev->usb_lock);
398 /* This function is called at probe time */
399 static int sd_config(struct gspca_dev *gspca_dev,
400 const struct usb_device_id *id)
403 struct sd *sd = (struct sd *) gspca_dev;
405 cam = &gspca_dev->cam;
406 /* We don't use the buffer gspca allocates so make it small. */
409 /* For the rest, the camera needs to be detected */
410 jl2005c_get_firmware_id(gspca_dev);
411 /* Here are some known firmware IDs
412 * First some JL2005B cameras
413 * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
414 * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
416 * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
417 * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
418 * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
420 * Based upon this scanty evidence, we can detect a CIF camera by
421 * testing byte 0 for 0x4x.
423 if ((sd->firmware_id[0] & 0xf0) == 0x40) {
424 cam->cam_mode = cif_mode;
425 cam->nmodes = ARRAY_SIZE(cif_mode);
426 sd->block_size = 0x80;
428 cam->cam_mode = vga_mode;
429 cam->nmodes = ARRAY_SIZE(vga_mode);
430 sd->block_size = 0x200;
433 INIT_WORK(&sd->work_struct, jl2005c_dostream);
438 /* this function is called at probe and resume time */
439 static int sd_init(struct gspca_dev *gspca_dev)
444 static int sd_start(struct gspca_dev *gspca_dev)
447 struct sd *sd = (struct sd *) gspca_dev;
448 sd->cap_mode = gspca_dev->cam.cam_mode;
450 switch (gspca_dev->pixfmt.width) {
452 PDEBUG(D_STREAM, "Start streaming at vga resolution");
453 jl2005c_stream_start_vga_lg(gspca_dev);
456 PDEBUG(D_STREAM, "Start streaming at qvga resolution");
457 jl2005c_stream_start_vga_small(gspca_dev);
460 PDEBUG(D_STREAM, "Start streaming at cif resolution");
461 jl2005c_stream_start_cif_lg(gspca_dev);
464 PDEBUG(D_STREAM, "Start streaming at qcif resolution");
465 jl2005c_stream_start_cif_small(gspca_dev);
468 pr_err("Unknown resolution specified\n");
472 schedule_work(&sd->work_struct);
477 /* called on streamoff with alt==0 and on disconnect */
478 /* the usb_lock is held at entry - restore on exit */
479 static void sd_stop0(struct gspca_dev *gspca_dev)
481 struct sd *dev = (struct sd *) gspca_dev;
483 /* wait for the work queue to terminate */
484 mutex_unlock(&gspca_dev->usb_lock);
485 /* This waits for sq905c_dostream to finish */
486 flush_work(&dev->work_struct);
487 mutex_lock(&gspca_dev->usb_lock);
492 /* sub-driver description */
493 static const struct sd_desc sd_desc = {
501 /* -- module initialisation -- */
502 static const struct usb_device_id device_table[] = {
503 {USB_DEVICE(0x0979, 0x0227)},
506 MODULE_DEVICE_TABLE(usb, device_table);
508 /* -- device connect -- */
509 static int sd_probe(struct usb_interface *intf,
510 const struct usb_device_id *id)
512 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
516 static struct usb_driver sd_driver = {
518 .id_table = device_table,
520 .disconnect = gspca_disconnect,
522 .suspend = gspca_suspend,
523 .resume = gspca_resume,
524 .reset_resume = gspca_resume,
528 module_usb_driver(sd_driver);