1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Jeilin JL2005B/C/D library
5 * Copyright (C) 2011 Theodore Kilgore <kilgota@auburn.edu>
8 #define MODULE_NAME "jl2005bcd"
10 #include <linux/workqueue.h>
11 #include <linux/slab.h>
15 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
16 MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver");
17 MODULE_LICENSE("GPL");
19 /* Default timeouts, in ms */
20 #define JL2005C_CMD_TIMEOUT 500
21 #define JL2005C_DATA_TIMEOUT 1000
23 /* Maximum transfer size to use. */
24 #define JL2005C_MAX_TRANSFER 0x200
25 #define FRAME_HEADER_LEN 16
28 /* specific webcam descriptor */
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
;
34 struct work_struct work_struct
;
36 int block_size
; /* block size of camera */
37 int vga
; /* 1 if vga cam, 0 if cif cam */
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
,
45 .sizeimage
= 176 * 144,
46 .colorspace
= V4L2_COLORSPACE_SRGB
,
48 {352, 288, V4L2_PIX_FMT_JL2005BCD
, V4L2_FIELD_NONE
,
50 .sizeimage
= 352 * 288,
51 .colorspace
= V4L2_COLORSPACE_SRGB
,
55 static const struct v4l2_pix_format vga_mode
[] = {
56 {320, 240, V4L2_PIX_FMT_JL2005BCD
, V4L2_FIELD_NONE
,
58 .sizeimage
= 320 * 240,
59 .colorspace
= V4L2_COLORSPACE_SRGB
,
61 {640, 480, V4L2_PIX_FMT_JL2005BCD
, V4L2_FIELD_NONE
,
63 .sizeimage
= 640 * 480,
64 .colorspace
= V4L2_COLORSPACE_SRGB
,
69 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
70 * and 0x82 for bulk data transfer.
73 /* All commands are two bytes only */
74 static int jl2005c_write2(struct gspca_dev
*gspca_dev
, unsigned char *command
)
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);
83 pr_err("command write [%02x] error %d\n",
84 gspca_dev
->usb_buf
[0], retval
);
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
)
93 retval
= usb_bulk_msg(gspca_dev
->dev
,
94 usb_rcvbulkpipe(gspca_dev
->dev
, 0x84),
95 gspca_dev
->usb_buf
, 1, NULL
, 500);
97 pr_err("read command [0x%02x] error %d\n",
98 gspca_dev
->usb_buf
[0], retval
);
102 /* Response appears in gspca_dev->usb_buf[0] */
103 static int jl2005c_read_reg(struct gspca_dev
*gspca_dev
, unsigned char reg
)
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
);
114 retval
= jl2005c_read1(gspca_dev
);
119 static int jl2005c_start_new_frame(struct gspca_dev
*gspca_dev
)
123 int frame_brightness
= 0;
125 static u8 instruction
[2] = {0x7f, 0x01};
127 retval
= jl2005c_write2(gspca_dev
, instruction
);
132 while (i
< 20 && !frame_brightness
) {
133 /* If we tried 20 times, give up. */
134 retval
= jl2005c_read_reg(gspca_dev
, 0x7e);
137 frame_brightness
= gspca_dev
->usb_buf
[0];
138 retval
= jl2005c_read_reg(gspca_dev
, 0x7d);
143 gspca_dbg(gspca_dev
, D_FRAM
, "frame_brightness is 0x%02x\n",
144 gspca_dev
->usb_buf
[0]);
148 static int jl2005c_write_reg(struct gspca_dev
*gspca_dev
, unsigned char reg
,
154 instruction
[0] = reg
;
155 instruction
[1] = value
;
157 retval
= jl2005c_write2(gspca_dev
, instruction
);
164 static int jl2005c_get_firmware_id(struct gspca_dev
*gspca_dev
)
166 struct sd
*sd
= (struct sd
*)gspca_dev
;
169 unsigned char regs_to_read
[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f};
171 gspca_dbg(gspca_dev
, D_PROBE
, "Running jl2005c_get_firmware_id\n");
172 /* Read the first ID byte once for warmup */
173 retval
= jl2005c_read_reg(gspca_dev
, regs_to_read
[0]);
174 gspca_dbg(gspca_dev
, D_PROBE
, "response is %02x\n",
175 gspca_dev
->usb_buf
[0]);
178 /* Now actually get the ID string */
179 for (i
= 0; i
< 6; i
++) {
180 retval
= jl2005c_read_reg(gspca_dev
, regs_to_read
[i
]);
183 sd
->firmware_id
[i
] = gspca_dev
->usb_buf
[0];
185 gspca_dbg(gspca_dev
, D_PROBE
, "firmware ID is %02x%02x%02x%02x%02x%02x\n",
195 static int jl2005c_stream_start_vga_lg
196 (struct gspca_dev
*gspca_dev
)
200 static u8 instruction
[][2] = {
209 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
211 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
219 static int jl2005c_stream_start_vga_small(struct gspca_dev
*gspca_dev
)
223 static u8 instruction
[][2] = {
232 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
234 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
242 static int jl2005c_stream_start_cif_lg(struct gspca_dev
*gspca_dev
)
246 static u8 instruction
[][2] = {
255 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
257 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
265 static int jl2005c_stream_start_cif_small(struct gspca_dev
*gspca_dev
)
269 static u8 instruction
[][2] = {
278 for (i
= 0; i
< ARRAY_SIZE(instruction
); i
++) {
280 retval
= jl2005c_write2(gspca_dev
, instruction
[i
]);
289 static int jl2005c_stop(struct gspca_dev
*gspca_dev
)
291 return jl2005c_write_reg(gspca_dev
, 0x07, 0x00);
295 * This function is called as a workqueue function and runs whenever the camera
296 * is streaming data. Because it is a workqueue function it is allowed to sleep
297 * so we can use synchronous USB calls. To avoid possible collisions with other
298 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
299 * performing USB operations using it. In practice we don't really need this
300 * as the camera doesn't provide any controls.
302 static void jl2005c_dostream(struct work_struct
*work
)
304 struct sd
*dev
= container_of(work
, struct sd
, work_struct
);
305 struct gspca_dev
*gspca_dev
= &dev
->gspca_dev
;
306 int bytes_left
= 0; /* bytes remaining in current frame. */
307 int data_len
; /* size to use for the next read. */
309 unsigned char header_sig
[2] = {0x4a, 0x4c};
315 buffer
= kmalloc(JL2005C_MAX_TRANSFER
, GFP_KERNEL
);
317 pr_err("Couldn't allocate USB buffer\n");
321 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
323 if (gspca_dev
->frozen
)
326 /* Check if this is a new frame. If so, start the frame first */
328 mutex_lock(&gspca_dev
->usb_lock
);
329 ret
= jl2005c_start_new_frame(gspca_dev
);
330 mutex_unlock(&gspca_dev
->usb_lock
);
333 ret
= usb_bulk_msg(gspca_dev
->dev
,
334 usb_rcvbulkpipe(gspca_dev
->dev
, 0x82),
335 buffer
, JL2005C_MAX_TRANSFER
, &act_len
,
336 JL2005C_DATA_TIMEOUT
);
337 gspca_dbg(gspca_dev
, D_PACK
,
338 "Got %d bytes out of %d for header\n",
339 act_len
, JL2005C_MAX_TRANSFER
);
340 if (ret
< 0 || act_len
< JL2005C_MAX_TRANSFER
)
342 /* Check whether we actually got the first blodk */
343 if (memcmp(header_sig
, buffer
, 2) != 0) {
344 pr_err("First block is not the first block\n");
347 /* total size to fetch is byte 7, times blocksize
348 * of which we already got act_len */
349 bytes_left
= buffer
[0x07] * dev
->block_size
- act_len
;
350 gspca_dbg(gspca_dev
, D_PACK
, "bytes_left = 0x%x\n",
352 /* We keep the header. It has other information, too.*/
353 packet_type
= FIRST_PACKET
;
354 gspca_frame_add(gspca_dev
, packet_type
,
358 while (bytes_left
> 0 && gspca_dev
->present
) {
359 data_len
= bytes_left
> JL2005C_MAX_TRANSFER
?
360 JL2005C_MAX_TRANSFER
: bytes_left
;
361 ret
= usb_bulk_msg(gspca_dev
->dev
,
362 usb_rcvbulkpipe(gspca_dev
->dev
, 0x82),
363 buffer
, data_len
, &act_len
,
364 JL2005C_DATA_TIMEOUT
);
365 if (ret
< 0 || act_len
< data_len
)
367 gspca_dbg(gspca_dev
, D_PACK
,
368 "Got %d bytes out of %d for frame\n",
369 data_len
, bytes_left
);
370 bytes_left
-= data_len
;
371 if (bytes_left
== 0) {
372 packet_type
= LAST_PACKET
;
375 packet_type
= INTER_PACKET
;
376 gspca_frame_add(gspca_dev
, packet_type
,
381 if (gspca_dev
->present
) {
382 mutex_lock(&gspca_dev
->usb_lock
);
383 jl2005c_stop(gspca_dev
);
384 mutex_unlock(&gspca_dev
->usb_lock
);
392 /* This function is called at probe time */
393 static int sd_config(struct gspca_dev
*gspca_dev
,
394 const struct usb_device_id
*id
)
397 struct sd
*sd
= (struct sd
*) gspca_dev
;
399 cam
= &gspca_dev
->cam
;
400 /* We don't use the buffer gspca allocates so make it small. */
403 /* For the rest, the camera needs to be detected */
404 jl2005c_get_firmware_id(gspca_dev
);
405 /* Here are some known firmware IDs
406 * First some JL2005B cameras
407 * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
408 * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
410 * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
411 * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
412 * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
414 * Based upon this scanty evidence, we can detect a CIF camera by
415 * testing byte 0 for 0x4x.
417 if ((sd
->firmware_id
[0] & 0xf0) == 0x40) {
418 cam
->cam_mode
= cif_mode
;
419 cam
->nmodes
= ARRAY_SIZE(cif_mode
);
420 sd
->block_size
= 0x80;
422 cam
->cam_mode
= vga_mode
;
423 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
424 sd
->block_size
= 0x200;
427 INIT_WORK(&sd
->work_struct
, jl2005c_dostream
);
432 /* this function is called at probe and resume time */
433 static int sd_init(struct gspca_dev
*gspca_dev
)
438 static int sd_start(struct gspca_dev
*gspca_dev
)
441 struct sd
*sd
= (struct sd
*) gspca_dev
;
442 sd
->cap_mode
= gspca_dev
->cam
.cam_mode
;
444 switch (gspca_dev
->pixfmt
.width
) {
446 gspca_dbg(gspca_dev
, D_STREAM
, "Start streaming at vga resolution\n");
447 jl2005c_stream_start_vga_lg(gspca_dev
);
450 gspca_dbg(gspca_dev
, D_STREAM
, "Start streaming at qvga resolution\n");
451 jl2005c_stream_start_vga_small(gspca_dev
);
454 gspca_dbg(gspca_dev
, D_STREAM
, "Start streaming at cif resolution\n");
455 jl2005c_stream_start_cif_lg(gspca_dev
);
458 gspca_dbg(gspca_dev
, D_STREAM
, "Start streaming at qcif resolution\n");
459 jl2005c_stream_start_cif_small(gspca_dev
);
462 pr_err("Unknown resolution specified\n");
466 schedule_work(&sd
->work_struct
);
471 /* called on streamoff with alt==0 and on disconnect */
472 /* the usb_lock is held at entry - restore on exit */
473 static void sd_stop0(struct gspca_dev
*gspca_dev
)
475 struct sd
*dev
= (struct sd
*) gspca_dev
;
477 /* wait for the work queue to terminate */
478 mutex_unlock(&gspca_dev
->usb_lock
);
479 /* This waits for sq905c_dostream to finish */
480 flush_work(&dev
->work_struct
);
481 mutex_lock(&gspca_dev
->usb_lock
);
486 /* sub-driver description */
487 static const struct sd_desc sd_desc
= {
495 /* -- module initialisation -- */
496 static const struct usb_device_id device_table
[] = {
497 {USB_DEVICE(0x0979, 0x0227)},
500 MODULE_DEVICE_TABLE(usb
, device_table
);
502 /* -- device connect -- */
503 static int sd_probe(struct usb_interface
*intf
,
504 const struct usb_device_id
*id
)
506 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
510 static struct usb_driver sd_driver
= {
512 .id_table
= device_table
,
514 .disconnect
= gspca_disconnect
,
516 .suspend
= gspca_suspend
,
517 .resume
= gspca_resume
,
518 .reset_resume
= gspca_resume
,
522 module_usb_driver(sd_driver
);