blob: 5b481fa430992a7b37b9d14f9df108498f0c4d86 [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * Jeilin JL2005B/C/D library
3 *
4 * Copyright (C) 2011 Theodore Kilgore <kilgota@auburn.edu>
5 *
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
9 * any later version.
10 *
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.
15 *
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
19 */
20
21#define MODULE_NAME "jl2005bcd"
22
23#include <linux/workqueue.h>
24#include <linux/slab.h>
25#include "gspca.h"
26
27
28MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
29MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver");
30MODULE_LICENSE("GPL");
31
32/* Default timeouts, in ms */
33#define JL2005C_CMD_TIMEOUT 500
34#define JL2005C_DATA_TIMEOUT 1000
35
36/* Maximum transfer size to use. */
37#define JL2005C_MAX_TRANSFER 0x200
38#define FRAME_HEADER_LEN 16
39
40
41/* specific webcam descriptor */
42struct sd {
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;
46 /* Driver stuff */
47 struct work_struct work_struct;
48 struct workqueue_struct *work_thread;
49 u8 frame_brightness;
50 int block_size; /* block size of camera */
51 int vga; /* 1 if vga cam, 0 if cif cam */
52};
53
54
55/* Camera has two resolution settings. What they are depends on model. */
56static const struct v4l2_pix_format cif_mode[] = {
57 {176, 144, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
58 .bytesperline = 176,
59 .sizeimage = 176 * 144,
60 .colorspace = V4L2_COLORSPACE_SRGB,
61 .priv = 0},
62 {352, 288, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
63 .bytesperline = 352,
64 .sizeimage = 352 * 288,
65 .colorspace = V4L2_COLORSPACE_SRGB,
66 .priv = 0},
67};
68
69static const struct v4l2_pix_format vga_mode[] = {
70 {320, 240, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
71 .bytesperline = 320,
72 .sizeimage = 320 * 240,
73 .colorspace = V4L2_COLORSPACE_SRGB,
74 .priv = 0},
75 {640, 480, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE,
76 .bytesperline = 640,
77 .sizeimage = 640 * 480,
78 .colorspace = V4L2_COLORSPACE_SRGB,
79 .priv = 0},
80};
81
82/*
83 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
84 * and 0x82 for bulk data transfer.
85 */
86
87/* All commands are two bytes only */
88static int jl2005c_write2(struct gspca_dev *gspca_dev, unsigned char *command)
89{
90 int retval;
91
92 memcpy(gspca_dev->usb_buf, command, 2);
93 retval = usb_bulk_msg(gspca_dev->dev,
94 usb_sndbulkpipe(gspca_dev->dev, 3),
95 gspca_dev->usb_buf, 2, NULL, 500);
96 if (retval < 0)
97 pr_err("command write [%02x] error %d\n",
98 gspca_dev->usb_buf[0], retval);
99 return retval;
100}
101
102/* Response to a command is one byte in usb_buf[0], only if requested. */
103static int jl2005c_read1(struct gspca_dev *gspca_dev)
104{
105 int retval;
106
107 retval = usb_bulk_msg(gspca_dev->dev,
108 usb_rcvbulkpipe(gspca_dev->dev, 0x84),
109 gspca_dev->usb_buf, 1, NULL, 500);
110 if (retval < 0)
111 pr_err("read command [0x%02x] error %d\n",
112 gspca_dev->usb_buf[0], retval);
113 return retval;
114}
115
116/* Response appears in gspca_dev->usb_buf[0] */
117static int jl2005c_read_reg(struct gspca_dev *gspca_dev, unsigned char reg)
118{
119 int retval;
120
121 static u8 instruction[2] = {0x95, 0x00};
122 /* put register to read in byte 1 */
123 instruction[1] = reg;
124 /* Send the read request */
125 retval = jl2005c_write2(gspca_dev, instruction);
126 if (retval < 0)
127 return retval;
128 retval = jl2005c_read1(gspca_dev);
129
130 return retval;
131}
132
133static int jl2005c_start_new_frame(struct gspca_dev *gspca_dev)
134{
135 int i;
136 int retval;
137 int frame_brightness = 0;
138
139 static u8 instruction[2] = {0x7f, 0x01};
140
141 retval = jl2005c_write2(gspca_dev, instruction);
142 if (retval < 0)
143 return retval;
144
145 i = 0;
146 while (i < 20 && !frame_brightness) {
147 /* If we tried 20 times, give up. */
148 retval = jl2005c_read_reg(gspca_dev, 0x7e);
149 if (retval < 0)
150 return retval;
151 frame_brightness = gspca_dev->usb_buf[0];
152 retval = jl2005c_read_reg(gspca_dev, 0x7d);
153 if (retval < 0)
154 return retval;
155 i++;
156 }
157 PDEBUG(D_FRAM, "frame_brightness is 0x%02x", gspca_dev->usb_buf[0]);
158 return retval;
159}
160
161static int jl2005c_write_reg(struct gspca_dev *gspca_dev, unsigned char reg,
162 unsigned char value)
163{
164 int retval;
165 u8 instruction[2];
166
167 instruction[0] = reg;
168 instruction[1] = value;
169
170 retval = jl2005c_write2(gspca_dev, instruction);
171 if (retval < 0)
172 return retval;
173
174 return retval;
175}
176
177static int jl2005c_get_firmware_id(struct gspca_dev *gspca_dev)
178{
179 struct sd *sd = (struct sd *)gspca_dev;
180 int i = 0;
181 int retval = -1;
182 unsigned char regs_to_read[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f};
183
184 PDEBUG(D_PROBE, "Running jl2005c_get_firmware_id");
185 /* Read the first ID byte once for warmup */
186 retval = jl2005c_read_reg(gspca_dev, regs_to_read[0]);
187 PDEBUG(D_PROBE, "response is %02x", gspca_dev->usb_buf[0]);
188 if (retval < 0)
189 return retval;
190 /* Now actually get the ID string */
191 for (i = 0; i < 6; i++) {
192 retval = jl2005c_read_reg(gspca_dev, regs_to_read[i]);
193 if (retval < 0)
194 return retval;
195 sd->firmware_id[i] = gspca_dev->usb_buf[0];
196 }
197 PDEBUG(D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x",
198 sd->firmware_id[0],
199 sd->firmware_id[1],
200 sd->firmware_id[2],
201 sd->firmware_id[3],
202 sd->firmware_id[4],
203 sd->firmware_id[5]);
204 return 0;
205}
206
207static int jl2005c_stream_start_vga_lg
208 (struct gspca_dev *gspca_dev)
209{
210 int i;
211 int retval = -1;
212 static u8 instruction[][2] = {
213 {0x05, 0x00},
214 {0x7c, 0x00},
215 {0x7d, 0x18},
216 {0x02, 0x00},
217 {0x01, 0x00},
218 {0x04, 0x52},
219 };
220
221 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
222 msleep(60);
223 retval = jl2005c_write2(gspca_dev, instruction[i]);
224 if (retval < 0)
225 return retval;
226 }
227 msleep(60);
228 return retval;
229}
230
231static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev)
232{
233 int i;
234 int retval = -1;
235 static u8 instruction[][2] = {
236 {0x06, 0x00},
237 {0x7c, 0x00},
238 {0x7d, 0x1a},
239 {0x02, 0x00},
240 {0x01, 0x00},
241 {0x04, 0x52},
242 };
243
244 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
245 msleep(60);
246 retval = jl2005c_write2(gspca_dev, instruction[i]);
247 if (retval < 0)
248 return retval;
249 }
250 msleep(60);
251 return retval;
252}
253
254static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev)
255{
256 int i;
257 int retval = -1;
258 static u8 instruction[][2] = {
259 {0x05, 0x00},
260 {0x7c, 0x00},
261 {0x7d, 0x30},
262 {0x02, 0x00},
263 {0x01, 0x00},
264 {0x04, 0x42},
265 };
266
267 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
268 msleep(60);
269 retval = jl2005c_write2(gspca_dev, instruction[i]);
270 if (retval < 0)
271 return retval;
272 }
273 msleep(60);
274 return retval;
275}
276
277static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev)
278{
279 int i;
280 int retval = -1;
281 static u8 instruction[][2] = {
282 {0x06, 0x00},
283 {0x7c, 0x00},
284 {0x7d, 0x32},
285 {0x02, 0x00},
286 {0x01, 0x00},
287 {0x04, 0x42},
288 };
289
290 for (i = 0; i < ARRAY_SIZE(instruction); i++) {
291 msleep(60);
292 retval = jl2005c_write2(gspca_dev, instruction[i]);
293 if (retval < 0)
294 return retval;
295 }
296 msleep(60);
297 return retval;
298}
299
300
301static int jl2005c_stop(struct gspca_dev *gspca_dev)
302{
303 int retval;
304
305 retval = jl2005c_write_reg(gspca_dev, 0x07, 0x00);
306 return retval;
307}
308
309/*
310 * This function is called as a workqueue function and runs whenever the camera
311 * is streaming data. Because it is a workqueue function it is allowed to sleep
312 * so we can use synchronous USB calls. To avoid possible collisions with other
313 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
314 * performing USB operations using it. In practice we don't really need this
315 * as the camera doesn't provide any controls.
316 */
317static void jl2005c_dostream(struct work_struct *work)
318{
319 struct sd *dev = container_of(work, struct sd, work_struct);
320 struct gspca_dev *gspca_dev = &dev->gspca_dev;
321 int bytes_left = 0; /* bytes remaining in current frame. */
322 int data_len; /* size to use for the next read. */
323 int header_read = 0;
324 unsigned char header_sig[2] = {0x4a, 0x4c};
325 int act_len;
326 int packet_type;
327 int ret;
328 u8 *buffer;
329
330 buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
331 if (!buffer) {
332 pr_err("Couldn't allocate USB buffer\n");
333 goto quit_stream;
334 }
335
336 while (gspca_dev->present && gspca_dev->streaming) {
337#ifdef CONFIG_PM
338 if (gspca_dev->frozen)
339 break;
340#endif
341 /* Check if this is a new frame. If so, start the frame first */
342 if (!header_read) {
343 mutex_lock(&gspca_dev->usb_lock);
344 ret = jl2005c_start_new_frame(gspca_dev);
345 mutex_unlock(&gspca_dev->usb_lock);
346 if (ret < 0)
347 goto quit_stream;
348 ret = usb_bulk_msg(gspca_dev->dev,
349 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
350 buffer, JL2005C_MAX_TRANSFER, &act_len,
351 JL2005C_DATA_TIMEOUT);
352 PDEBUG(D_PACK,
353 "Got %d bytes out of %d for header",
354 act_len, JL2005C_MAX_TRANSFER);
355 if (ret < 0 || act_len < JL2005C_MAX_TRANSFER)
356 goto quit_stream;
357 /* Check whether we actually got the first blodk */
358 if (memcmp(header_sig, buffer, 2) != 0) {
359 pr_err("First block is not the first block\n");
360 goto quit_stream;
361 }
362 /* total size to fetch is byte 7, times blocksize
363 * of which we already got act_len */
364 bytes_left = buffer[0x07] * dev->block_size - act_len;
365 PDEBUG(D_PACK, "bytes_left = 0x%x", bytes_left);
366 /* We keep the header. It has other information, too.*/
367 packet_type = FIRST_PACKET;
368 gspca_frame_add(gspca_dev, packet_type,
369 buffer, act_len);
370 header_read = 1;
371 }
372 while (bytes_left > 0 && gspca_dev->present) {
373 data_len = bytes_left > JL2005C_MAX_TRANSFER ?
374 JL2005C_MAX_TRANSFER : bytes_left;
375 ret = usb_bulk_msg(gspca_dev->dev,
376 usb_rcvbulkpipe(gspca_dev->dev, 0x82),
377 buffer, data_len, &act_len,
378 JL2005C_DATA_TIMEOUT);
379 if (ret < 0 || act_len < data_len)
380 goto quit_stream;
381 PDEBUG(D_PACK,
382 "Got %d bytes out of %d for frame",
383 data_len, bytes_left);
384 bytes_left -= data_len;
385 if (bytes_left == 0) {
386 packet_type = LAST_PACKET;
387 header_read = 0;
388 } else
389 packet_type = INTER_PACKET;
390 gspca_frame_add(gspca_dev, packet_type,
391 buffer, data_len);
392 }
393 }
394quit_stream:
395 if (gspca_dev->present) {
396 mutex_lock(&gspca_dev->usb_lock);
397 jl2005c_stop(gspca_dev);
398 mutex_unlock(&gspca_dev->usb_lock);
399 }
400 kfree(buffer);
401}
402
403
404
405
406/* This function is called at probe time */
407static int sd_config(struct gspca_dev *gspca_dev,
408 const struct usb_device_id *id)
409{
410 struct cam *cam;
411 struct sd *sd = (struct sd *) gspca_dev;
412
413 cam = &gspca_dev->cam;
414 /* We don't use the buffer gspca allocates so make it small. */
415 cam->bulk_size = 64;
416 cam->bulk = 1;
417 /* For the rest, the camera needs to be detected */
418 jl2005c_get_firmware_id(gspca_dev);
419 /* Here are some known firmware IDs
420 * First some JL2005B cameras
421 * {0x41, 0x07, 0x04, 0x2c, 0xe8, 0xf2} Sakar KidzCam
422 * {0x45, 0x02, 0x08, 0xb9, 0x00, 0xd2} No-name JL2005B
423 * JL2005C cameras
424 * {0x01, 0x0c, 0x16, 0x10, 0xf8, 0xc8} Argus DC-1512
425 * {0x12, 0x04, 0x03, 0xc0, 0x00, 0xd8} ICarly
426 * {0x86, 0x08, 0x05, 0x02, 0x00, 0xd4} Jazz
427 *
428 * Based upon this scanty evidence, we can detect a CIF camera by
429 * testing byte 0 for 0x4x.
430 */
431 if ((sd->firmware_id[0] & 0xf0) == 0x40) {
432 cam->cam_mode = cif_mode;
433 cam->nmodes = ARRAY_SIZE(cif_mode);
434 sd->block_size = 0x80;
435 } else {
436 cam->cam_mode = vga_mode;
437 cam->nmodes = ARRAY_SIZE(vga_mode);
438 sd->block_size = 0x200;
439 }
440
441 INIT_WORK(&sd->work_struct, jl2005c_dostream);
442
443 return 0;
444}
445
446/* this function is called at probe and resume time */
447static int sd_init(struct gspca_dev *gspca_dev)
448{
449 return 0;
450}
451
452static int sd_start(struct gspca_dev *gspca_dev)
453{
454
455 struct sd *sd = (struct sd *) gspca_dev;
456 sd->cap_mode = gspca_dev->cam.cam_mode;
457
458 switch (gspca_dev->pixfmt.width) {
459 case 640:
460 PDEBUG(D_STREAM, "Start streaming at vga resolution");
461 jl2005c_stream_start_vga_lg(gspca_dev);
462 break;
463 case 320:
464 PDEBUG(D_STREAM, "Start streaming at qvga resolution");
465 jl2005c_stream_start_vga_small(gspca_dev);
466 break;
467 case 352:
468 PDEBUG(D_STREAM, "Start streaming at cif resolution");
469 jl2005c_stream_start_cif_lg(gspca_dev);
470 break;
471 case 176:
472 PDEBUG(D_STREAM, "Start streaming at qcif resolution");
473 jl2005c_stream_start_cif_small(gspca_dev);
474 break;
475 default:
476 pr_err("Unknown resolution specified\n");
477 return -1;
478 }
479
480 /* Start the workqueue function to do the streaming */
481 sd->work_thread = create_singlethread_workqueue(MODULE_NAME);
482 queue_work(sd->work_thread, &sd->work_struct);
483
484 return 0;
485}
486
487/* called on streamoff with alt==0 and on disconnect */
488/* the usb_lock is held at entry - restore on exit */
489static void sd_stop0(struct gspca_dev *gspca_dev)
490{
491 struct sd *dev = (struct sd *) gspca_dev;
492
493 /* wait for the work queue to terminate */
494 mutex_unlock(&gspca_dev->usb_lock);
495 /* This waits for sq905c_dostream to finish */
496 destroy_workqueue(dev->work_thread);
497 dev->work_thread = NULL;
498 mutex_lock(&gspca_dev->usb_lock);
499}
500
501
502
503/* sub-driver description */
504static const struct sd_desc sd_desc = {
505 .name = MODULE_NAME,
506 .config = sd_config,
507 .init = sd_init,
508 .start = sd_start,
509 .stop0 = sd_stop0,
510};
511
512/* -- module initialisation -- */
513static const struct usb_device_id device_table[] = {
514 {USB_DEVICE(0x0979, 0x0227)},
515 {}
516};
517MODULE_DEVICE_TABLE(usb, device_table);
518
519/* -- device connect -- */
520static int sd_probe(struct usb_interface *intf,
521 const struct usb_device_id *id)
522{
523 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
524 THIS_MODULE);
525}
526
527static struct usb_driver sd_driver = {
528 .name = MODULE_NAME,
529 .id_table = device_table,
530 .probe = sd_probe,
531 .disconnect = gspca_disconnect,
532#ifdef CONFIG_PM
533 .suspend = gspca_suspend,
534 .resume = gspca_resume,
535 .reset_resume = gspca_resume,
536#endif
537};
538
539module_usb_driver(sd_driver);