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 static const unsigned char regs_to_read[] = {
170 0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f
173 gspca_dbg(gspca_dev, D_PROBE, "Running jl2005c_get_firmware_id\n");
174 /* Read the first ID byte once for warmup */
175 retval = jl2005c_read_reg(gspca_dev, regs_to_read[0]);
176 gspca_dbg(gspca_dev, D_PROBE, "response is %02x\n",
177 gspca_dev->usb_buf[0]);
180 /* Now actually get the ID string */
181 for (i = 0; i < 6; i++) {
182 retval = jl2005c_read_reg(gspca_dev, regs_to_read[i]);
185 sd->firmware_id[i] = gspca_dev->usb_buf[0];
187 gspca_dbg(gspca_dev, D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x\n",
197 static int jl2005c_stream_start_vga_lg
198 (struct gspca_dev *gspca_dev)
202 static u8 instruction[][2] = {
211 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
213 retval = jl2005c_write2(gspca_dev, instruction[i]);
221 static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev)
225 static u8 instruction[][2] = {
234 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
236 retval = jl2005c_write2(gspca_dev, instruction[i]);
244 static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev)
248 static u8 instruction[][2] = {
257 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
259 retval = jl2005c_write2(gspca_dev, instruction[i]);
267 static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev)
271 static u8 instruction[][2] = {
280 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
282 retval = jl2005c_write2(gspca_dev, instruction[i]);
291 static int jl2005c_stop(struct gspca_dev *gspca_dev)
293 return jl2005c_write_reg(gspca_dev, 0x07, 0x00);
297 * This function is called as a workqueue function and runs whenever the camera
298 * is streaming data. Because it is a workqueue function it is allowed to sleep
299 * so we can use synchronous USB calls. To avoid possible collisions with other
300 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
301 * performing USB operations using it. In practice we don't really need this
302 * as the camera doesn't provide any controls.
304 static void jl2005c_dostream(struct work_struct *work)
306 struct sd *dev = container_of(work, struct sd, work_struct);
307 struct gspca_dev *gspca_dev = &dev->gspca_dev;
308 int bytes_left = 0; /* bytes remaining in current frame. */
309 int data_len; /* size to use for the next read. */
311 unsigned char header_sig[2] = {0x4a, 0x4c};
317 buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL);
319 pr_err("Couldn't allocate USB buffer\n");
323 while (gspca_dev->present && gspca_dev->streaming) {
325 if (gspca_dev->frozen)
328 /* Check if this is a new frame. If so, start the frame first */
330 mutex_lock(&gspca_dev->usb_lock);
331 ret = jl2005c_start_new_frame(gspca_dev);
332 mutex_unlock(&gspca_dev->usb_lock);
335 ret = usb_bulk_msg(gspca_dev->dev,
336 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
337 buffer, JL2005C_MAX_TRANSFER, &act_len,
338 JL2005C_DATA_TIMEOUT);
339 gspca_dbg(gspca_dev, D_PACK,
340 "Got %d bytes out of %d for header\n",
341 act_len, JL2005C_MAX_TRANSFER);
342 if (ret < 0 || act_len < JL2005C_MAX_TRANSFER)
344 /* Check whether we actually got the first blodk */
345 if (memcmp(header_sig, buffer, 2) != 0) {
346 pr_err("First block is not the first block\n");
349 /* total size to fetch is byte 7, times blocksize
350 * of which we already got act_len */
351 bytes_left = buffer[0x07] * dev->block_size - act_len;
352 gspca_dbg(gspca_dev, D_PACK, "bytes_left = 0x%x\n",
354 /* We keep the header. It has other information, too.*/
355 packet_type = FIRST_PACKET;
356 gspca_frame_add(gspca_dev, packet_type,
360 while (bytes_left > 0 && gspca_dev->present) {
361 data_len = bytes_left > JL2005C_MAX_TRANSFER ?
362 JL2005C_MAX_TRANSFER : bytes_left;
363 ret = usb_bulk_msg(gspca_dev->dev,
364 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
365 buffer, data_len, &act_len,
366 JL2005C_DATA_TIMEOUT);
367 if (ret < 0 || act_len < data_len)
369 gspca_dbg(gspca_dev, D_PACK,
370 "Got %d bytes out of %d for frame\n",
371 data_len, bytes_left);
372 bytes_left -= data_len;
373 if (bytes_left == 0) {
374 packet_type = LAST_PACKET;
377 packet_type = INTER_PACKET;
378 gspca_frame_add(gspca_dev, packet_type,
383 if (gspca_dev->present) {
384 mutex_lock(&gspca_dev->usb_lock);
385 jl2005c_stop(gspca_dev);
386 mutex_unlock(&gspca_dev->usb_lock);
394 /* This function is called at probe time */
395 static int sd_config(struct gspca_dev *gspca_dev,
396 const struct usb_device_id *id)
399 struct sd *sd = (struct sd *) gspca_dev;
401 cam = &gspca_dev->cam;
402 /* We don't use the buffer gspca allocates so make it small. */
405 /* For the rest, the camera needs to be detected */
406 jl2005c_get_firmware_id(gspca_dev);
407 /* Here are some known firmware IDs
408 * First some JL2005B cameras
409 * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
410 * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
412 * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
413 * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
414 * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
416 * Based upon this scanty evidence, we can detect a CIF camera by
417 * testing byte 0 for 0x4x.
419 if ((sd->firmware_id[0] & 0xf0) == 0x40) {
420 cam->cam_mode = cif_mode;
421 cam->nmodes = ARRAY_SIZE(cif_mode);
422 sd->block_size = 0x80;
424 cam->cam_mode = vga_mode;
425 cam->nmodes = ARRAY_SIZE(vga_mode);
426 sd->block_size = 0x200;
429 INIT_WORK(&sd->work_struct, jl2005c_dostream);
434 /* this function is called at probe and resume time */
435 static int sd_init(struct gspca_dev *gspca_dev)
440 static int sd_start(struct gspca_dev *gspca_dev)
443 struct sd *sd = (struct sd *) gspca_dev;
444 sd->cap_mode = gspca_dev->cam.cam_mode;
446 switch (gspca_dev->pixfmt.width) {
448 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at vga resolution\n");
449 jl2005c_stream_start_vga_lg(gspca_dev);
452 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qvga resolution\n");
453 jl2005c_stream_start_vga_small(gspca_dev);
456 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at cif resolution\n");
457 jl2005c_stream_start_cif_lg(gspca_dev);
460 gspca_dbg(gspca_dev, D_STREAM, "Start streaming at qcif resolution\n");
461 jl2005c_stream_start_cif_small(gspca_dev);
464 pr_err("Unknown resolution specified\n");
468 schedule_work(&sd->work_struct);
473 /* called on streamoff with alt==0 and on disconnect */
474 /* the usb_lock is held at entry - restore on exit */
475 static void sd_stop0(struct gspca_dev *gspca_dev)
477 struct sd *dev = (struct sd *) gspca_dev;
479 /* wait for the work queue to terminate */
480 mutex_unlock(&gspca_dev->usb_lock);
481 /* This waits for sq905c_dostream to finish */
482 flush_work(&dev->work_struct);
483 mutex_lock(&gspca_dev->usb_lock);
488 /* sub-driver description */
489 static const struct sd_desc sd_desc = {
497 /* -- module initialisation -- */
498 static const struct usb_device_id device_table[] = {
499 {USB_DEVICE(0x0979, 0x0227)},
502 MODULE_DEVICE_TABLE(usb, device_table);
504 /* -- device connect -- */
505 static int sd_probe(struct usb_interface *intf,
506 const struct usb_device_id *id)
508 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
512 static struct usb_driver sd_driver = {
514 .id_table = device_table,
516 .disconnect = gspca_disconnect,
518 .suspend = gspca_suspend,
519 .resume = gspca_resume,
520 .reset_resume = gspca_resume,
524 module_usb_driver(sd_driver);