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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define MODULE_NAME "jl2005bcd"
23 #include <linux/workqueue.h>
24 #include <linux/slab.h>
28 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
29 MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver");
30 MODULE_LICENSE("GPL");
32 /* Default timeouts, in ms */
33 #define JL2005C_CMD_TIMEOUT 500
34 #define JL2005C_DATA_TIMEOUT 1000
36 /* Maximum transfer size to use. */
37 #define JL2005C_MAX_TRANSFER 0x200
38 #define FRAME_HEADER_LEN 16
41 /* specific webcam descriptor */
43 struct gspca_dev gspca_dev
; /* !! must be the first item */
44 unsigned char firmware_id
[6];
45 const struct v4l2_pix_format
*cap_mode
;
47 struct work_struct work_struct
;
49 int block_size
; /* block size of camera */
50 int vga
; /* 1 if vga cam, 0 if cif cam */
54 /* Camera has two resolution settings. What they are depends on model. */
55 static const struct v4l2_pix_format cif_mode
[] = {
56 {176, 144, V4L2_PIX_FMT_JL2005BCD
, V4L2_FIELD_NONE
,
58 .sizeimage
= 176 * 144,
59 .colorspace
= V4L2_COLORSPACE_SRGB
,
61 {352, 288, V4L2_PIX_FMT_JL2005BCD
, V4L2_FIELD_NONE
,
63 .sizeimage
= 352 * 288,
64 .colorspace
= V4L2_COLORSPACE_SRGB
,
68 static const struct v4l2_pix_format vga_mode
[] = {
69 {320, 240, V4L2_PIX_FMT_JL2005BCD
, V4L2_FIELD_NONE
,
71 .sizeimage
= 320 * 240,
72 .colorspace
= V4L2_COLORSPACE_SRGB
,
74 {640, 480, V4L2_PIX_FMT_JL2005BCD
, V4L2_FIELD_NONE
,
76 .sizeimage
= 640 * 480,
77 .colorspace
= V4L2_COLORSPACE_SRGB
,
82 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
83 * and 0x82 for bulk data transfer.
86 /* All commands are two bytes only */
87 static int jl2005c_write2(struct gspca_dev
*gspca_dev
, unsigned char *command
)
91 memcpy(gspca_dev
->usb_buf
, command
, 2);
92 retval
= usb_bulk_msg(gspca_dev
->dev
,
93 usb_sndbulkpipe(gspca_dev
->dev
, 3),
94 gspca_dev
->usb_buf
, 2, NULL
, 500);
96 pr_err("command write [%02x] error %d\n",
97 gspca_dev
->usb_buf
[0], retval
);
101 /* Response to a command is one byte in usb_buf[0], only if requested. */
102 static int jl2005c_read1(struct gspca_dev
*gspca_dev
)
106 retval
= usb_bulk_msg(gspca_dev
->dev
,
107 usb_rcvbulkpipe(gspca_dev
->dev
, 0x84),
108 gspca_dev
->usb_buf
, 1, NULL
, 500);
110 pr_err("read command [0x%02x] error %d\n",
111 gspca_dev
->usb_buf
[0], retval
);
115 /* Response appears in gspca_dev->usb_buf[0] */
116 static int jl2005c_read_reg(struct gspca_dev
*gspca_dev
, unsigned char reg
)
120 static u8 instruction
[2] = {0x95, 0x00};
121 /* put register to read in byte 1 */
122 instruction
[1] = reg
;
123 /* Send the read request */
124 retval
= jl2005c_write2(gspca_dev
, instruction
);
127 retval
= jl2005c_read1(gspca_dev
);
132 static int jl2005c_start_new_frame(struct gspca_dev
*gspca_dev
)
136 int frame_brightness
= 0;
138 static u8 instruction
[2] = {0x7f, 0x01};
140 retval
= jl2005c_write2(gspca_dev
, instruction
);
145 while (i
< 20 && !frame_brightness
) {
146 /* If we tried 20 times, give up. */
147 retval
= jl2005c_read_reg(gspca_dev
, 0x7e);
150 frame_brightness
= gspca_dev
->usb_buf
[0];
151 retval
= jl2005c_read_reg(gspca_dev
, 0x7d);
156 PDEBUG(D_FRAM
, "frame_brightness is 0x%02x", gspca_dev
->usb_buf
[0]);
160 static int jl2005c_write_reg(struct gspca_dev
*gspca_dev
, unsigned char reg
,
166 instruction
[0] = reg
;
167 instruction
[1] = value
;
169 retval
= jl2005c_write2(gspca_dev
, instruction
);
176 static int jl2005c_get_firmware_id(struct gspca_dev
*gspca_dev
)
178 struct sd
*sd
= (struct sd
*)gspca_dev
;
181 unsigned char regs_to_read
[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f};
183 PDEBUG(D_PROBE
, "Running jl2005c_get_firmware_id");
184 /* Read the first ID byte once for warmup */
185 retval
= jl2005c_read_reg(gspca_dev
, regs_to_read
[0]);
186 PDEBUG(D_PROBE
, "response is %02x", gspca_dev
->usb_buf
[0]);
189 /* Now actually get the ID string */
190 for (i
= 0; i
< 6; i
++) {
191 retval
= jl2005c_read_reg(gspca_dev
, regs_to_read
[i
]);
194 sd
->firmware_id
[i
] = gspca_dev
->usb_buf
[0];
196 PDEBUG(D_PROBE
, "firmware ID is %02x%02x%02x%02x%02x%02x",
206 static int jl2005c_stream_start_vga_lg
207 (struct gspca_dev
*gspca_dev
)
211 static u8 instruction
[][2] = {
220 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
222 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
230 static int jl2005c_stream_start_vga_small(struct gspca_dev
*gspca_dev
)
234 static u8 instruction
[][2] = {
243 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
245 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
253 static int jl2005c_stream_start_cif_lg(struct gspca_dev
*gspca_dev
)
257 static u8 instruction
[][2] = {
266 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
268 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
276 static int jl2005c_stream_start_cif_small(struct gspca_dev
*gspca_dev
)
280 static u8 instruction
[][2] = {
289 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
291 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
300 static int jl2005c_stop(struct gspca_dev
*gspca_dev
)
302 return jl2005c_write_reg(gspca_dev
, 0x07, 0x00);
306 * This function is called as a workqueue function and runs whenever the camera
307 * is streaming data. Because it is a workqueue function it is allowed to sleep
308 * so we can use synchronous USB calls. To avoid possible collisions with other
309 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
310 * performing USB operations using it. In practice we don't really need this
311 * as the camera doesn't provide any controls.
313 static void jl2005c_dostream(struct work_struct
*work
)
315 struct sd
*dev
= container_of(work
, struct sd
, work_struct
);
316 struct gspca_dev
*gspca_dev
= &dev
->gspca_dev
;
317 int bytes_left
= 0; /* bytes remaining in current frame. */
318 int data_len
; /* size to use for the next read. */
320 unsigned char header_sig
[2] = {0x4a, 0x4c};
326 buffer
= kmalloc(JL2005C_MAX_TRANSFER
, GFP_KERNEL
| GFP_DMA
);
328 pr_err("Couldn't allocate USB buffer\n");
332 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
334 if (gspca_dev
->frozen
)
337 /* Check if this is a new frame. If so, start the frame first */
339 mutex_lock(&gspca_dev
->usb_lock
);
340 ret
= jl2005c_start_new_frame(gspca_dev
);
341 mutex_unlock(&gspca_dev
->usb_lock
);
344 ret
= usb_bulk_msg(gspca_dev
->dev
,
345 usb_rcvbulkpipe(gspca_dev
->dev
, 0x82),
346 buffer
, JL2005C_MAX_TRANSFER
, &act_len
,
347 JL2005C_DATA_TIMEOUT
);
349 "Got %d bytes out of %d for header",
350 act_len
, JL2005C_MAX_TRANSFER
);
351 if (ret
< 0 || act_len
< JL2005C_MAX_TRANSFER
)
353 /* Check whether we actually got the first blodk */
354 if (memcmp(header_sig
, buffer
, 2) != 0) {
355 pr_err("First block is not the first block\n");
358 /* total size to fetch is byte 7, times blocksize
359 * of which we already got act_len */
360 bytes_left
= buffer
[0x07] * dev
->block_size
- act_len
;
361 PDEBUG(D_PACK
, "bytes_left = 0x%x", bytes_left
);
362 /* We keep the header. It has other information, too.*/
363 packet_type
= FIRST_PACKET
;
364 gspca_frame_add(gspca_dev
, packet_type
,
368 while (bytes_left
> 0 && gspca_dev
->present
) {
369 data_len
= bytes_left
> JL2005C_MAX_TRANSFER
?
370 JL2005C_MAX_TRANSFER
: bytes_left
;
371 ret
= usb_bulk_msg(gspca_dev
->dev
,
372 usb_rcvbulkpipe(gspca_dev
->dev
, 0x82),
373 buffer
, data_len
, &act_len
,
374 JL2005C_DATA_TIMEOUT
);
375 if (ret
< 0 || act_len
< data_len
)
378 "Got %d bytes out of %d for frame",
379 data_len
, bytes_left
);
380 bytes_left
-= data_len
;
381 if (bytes_left
== 0) {
382 packet_type
= LAST_PACKET
;
385 packet_type
= INTER_PACKET
;
386 gspca_frame_add(gspca_dev
, packet_type
,
391 if (gspca_dev
->present
) {
392 mutex_lock(&gspca_dev
->usb_lock
);
393 jl2005c_stop(gspca_dev
);
394 mutex_unlock(&gspca_dev
->usb_lock
);
402 /* This function is called at probe time */
403 static int sd_config(struct gspca_dev
*gspca_dev
,
404 const struct usb_device_id
*id
)
407 struct sd
*sd
= (struct sd
*) gspca_dev
;
409 cam
= &gspca_dev
->cam
;
410 /* We don't use the buffer gspca allocates so make it small. */
413 /* For the rest, the camera needs to be detected */
414 jl2005c_get_firmware_id(gspca_dev
);
415 /* Here are some known firmware IDs
416 * First some JL2005B cameras
417 * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
418 * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
420 * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
421 * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
422 * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
424 * Based upon this scanty evidence, we can detect a CIF camera by
425 * testing byte 0 for 0x4x.
427 if ((sd
->firmware_id
[0] & 0xf0) == 0x40) {
428 cam
->cam_mode
= cif_mode
;
429 cam
->nmodes
= ARRAY_SIZE(cif_mode
);
430 sd
->block_size
= 0x80;
432 cam
->cam_mode
= vga_mode
;
433 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
434 sd
->block_size
= 0x200;
437 INIT_WORK(&sd
->work_struct
, jl2005c_dostream
);
442 /* this function is called at probe and resume time */
443 static int sd_init(struct gspca_dev
*gspca_dev
)
448 static int sd_start(struct gspca_dev
*gspca_dev
)
451 struct sd
*sd
= (struct sd
*) gspca_dev
;
452 sd
->cap_mode
= gspca_dev
->cam
.cam_mode
;
454 switch (gspca_dev
->pixfmt
.width
) {
456 PDEBUG(D_STREAM
, "Start streaming at vga resolution");
457 jl2005c_stream_start_vga_lg(gspca_dev
);
460 PDEBUG(D_STREAM
, "Start streaming at qvga resolution");
461 jl2005c_stream_start_vga_small(gspca_dev
);
464 PDEBUG(D_STREAM
, "Start streaming at cif resolution");
465 jl2005c_stream_start_cif_lg(gspca_dev
);
468 PDEBUG(D_STREAM
, "Start streaming at qcif resolution");
469 jl2005c_stream_start_cif_small(gspca_dev
);
472 pr_err("Unknown resolution specified\n");
476 schedule_work(&sd
->work_struct
);
481 /* called on streamoff with alt==0 and on disconnect */
482 /* the usb_lock is held at entry - restore on exit */
483 static void sd_stop0(struct gspca_dev
*gspca_dev
)
485 struct sd
*dev
= (struct sd
*) gspca_dev
;
487 /* wait for the work queue to terminate */
488 mutex_unlock(&gspca_dev
->usb_lock
);
489 /* This waits for sq905c_dostream to finish */
490 flush_work(&dev
->work_struct
);
491 mutex_lock(&gspca_dev
->usb_lock
);
496 /* sub-driver description */
497 static const struct sd_desc sd_desc
= {
505 /* -- module initialisation -- */
506 static const struct usb_device_id device_table
[] = {
507 {USB_DEVICE(0x0979, 0x0227)},
510 MODULE_DEVICE_TABLE(usb
, device_table
);
512 /* -- device connect -- */
513 static int sd_probe(struct usb_interface
*intf
,
514 const struct usb_device_id
*id
)
516 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
520 static struct usb_driver sd_driver
= {
522 .id_table
= device_table
,
524 .disconnect
= gspca_disconnect
,
526 .suspend
= gspca_suspend
,
527 .resume
= gspca_resume
,
528 .reset_resume
= gspca_resume
,
532 module_usb_driver(sd_driver
);