]>
Commit | Line | Data |
---|---|---|
fe3449a4 TK |
1 | /* |
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. | |
fe3449a4 TK |
15 | */ |
16 | ||
17 | #define MODULE_NAME "jl2005bcd" | |
18 | ||
19 | #include <linux/workqueue.h> | |
20 | #include <linux/slab.h> | |
21 | #include "gspca.h" | |
22 | ||
23 | ||
24 | MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>"); | |
25 | MODULE_DESCRIPTION("JL2005B/C/D USB Camera Driver"); | |
26 | MODULE_LICENSE("GPL"); | |
27 | ||
28 | /* Default timeouts, in ms */ | |
29 | #define JL2005C_CMD_TIMEOUT 500 | |
30 | #define JL2005C_DATA_TIMEOUT 1000 | |
31 | ||
32 | /* Maximum transfer size to use. */ | |
33 | #define JL2005C_MAX_TRANSFER 0x200 | |
34 | #define FRAME_HEADER_LEN 16 | |
35 | ||
36 | ||
37 | /* specific webcam descriptor */ | |
38 | struct sd { | |
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; | |
42 | /* Driver stuff */ | |
43 | struct work_struct work_struct; | |
fe3449a4 TK |
44 | u8 frame_brightness; |
45 | int block_size; /* block size of camera */ | |
46 | int vga; /* 1 if vga cam, 0 if cif cam */ | |
47 | }; | |
48 | ||
49 | ||
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, | |
53 | .bytesperline = 176, | |
54 | .sizeimage = 176 * 144, | |
55 | .colorspace = V4L2_COLORSPACE_SRGB, | |
56 | .priv = 0}, | |
57 | {352, 288, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE, | |
58 | .bytesperline = 352, | |
59 | .sizeimage = 352 * 288, | |
60 | .colorspace = V4L2_COLORSPACE_SRGB, | |
61 | .priv = 0}, | |
62 | }; | |
63 | ||
64 | static const struct v4l2_pix_format vga_mode[] = { | |
65 | {320, 240, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE, | |
66 | .bytesperline = 320, | |
67 | .sizeimage = 320 * 240, | |
68 | .colorspace = V4L2_COLORSPACE_SRGB, | |
69 | .priv = 0}, | |
70 | {640, 480, V4L2_PIX_FMT_JL2005BCD, V4L2_FIELD_NONE, | |
71 | .bytesperline = 640, | |
72 | .sizeimage = 640 * 480, | |
73 | .colorspace = V4L2_COLORSPACE_SRGB, | |
74 | .priv = 0}, | |
75 | }; | |
76 | ||
77 | /* | |
78 | * cam uses endpoint 0x03 to send commands, 0x84 for read commands, | |
79 | * and 0x82 for bulk data transfer. | |
80 | */ | |
81 | ||
82 | /* All commands are two bytes only */ | |
83 | static int jl2005c_write2(struct gspca_dev *gspca_dev, unsigned char *command) | |
84 | { | |
85 | int retval; | |
86 | ||
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); | |
91 | if (retval < 0) | |
92 | pr_err("command write [%02x] error %d\n", | |
93 | gspca_dev->usb_buf[0], retval); | |
94 | return retval; | |
95 | } | |
96 | ||
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) | |
99 | { | |
100 | int retval; | |
101 | ||
102 | retval = usb_bulk_msg(gspca_dev->dev, | |
103 | usb_rcvbulkpipe(gspca_dev->dev, 0x84), | |
104 | gspca_dev->usb_buf, 1, NULL, 500); | |
105 | if (retval < 0) | |
106 | pr_err("read command [0x%02x] error %d\n", | |
107 | gspca_dev->usb_buf[0], retval); | |
108 | return retval; | |
109 | } | |
110 | ||
111 | /* Response appears in gspca_dev->usb_buf[0] */ | |
112 | static int jl2005c_read_reg(struct gspca_dev *gspca_dev, unsigned char reg) | |
113 | { | |
114 | int retval; | |
115 | ||
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); | |
121 | if (retval < 0) | |
122 | return retval; | |
123 | retval = jl2005c_read1(gspca_dev); | |
124 | ||
125 | return retval; | |
126 | } | |
127 | ||
128 | static int jl2005c_start_new_frame(struct gspca_dev *gspca_dev) | |
129 | { | |
130 | int i; | |
131 | int retval; | |
132 | int frame_brightness = 0; | |
133 | ||
134 | static u8 instruction[2] = {0x7f, 0x01}; | |
135 | ||
136 | retval = jl2005c_write2(gspca_dev, instruction); | |
137 | if (retval < 0) | |
138 | return retval; | |
139 | ||
140 | i = 0; | |
141 | while (i < 20 && !frame_brightness) { | |
142 | /* If we tried 20 times, give up. */ | |
143 | retval = jl2005c_read_reg(gspca_dev, 0x7e); | |
144 | if (retval < 0) | |
145 | return retval; | |
146 | frame_brightness = gspca_dev->usb_buf[0]; | |
147 | retval = jl2005c_read_reg(gspca_dev, 0x7d); | |
148 | if (retval < 0) | |
149 | return retval; | |
150 | i++; | |
151 | } | |
152 | PDEBUG(D_FRAM, "frame_brightness is 0x%02x", gspca_dev->usb_buf[0]); | |
153 | return retval; | |
154 | } | |
155 | ||
156 | static int jl2005c_write_reg(struct gspca_dev *gspca_dev, unsigned char reg, | |
157 | unsigned char value) | |
158 | { | |
159 | int retval; | |
160 | u8 instruction[2]; | |
161 | ||
162 | instruction[0] = reg; | |
163 | instruction[1] = value; | |
164 | ||
165 | retval = jl2005c_write2(gspca_dev, instruction); | |
166 | if (retval < 0) | |
167 | return retval; | |
168 | ||
169 | return retval; | |
170 | } | |
171 | ||
172 | static int jl2005c_get_firmware_id(struct gspca_dev *gspca_dev) | |
173 | { | |
174 | struct sd *sd = (struct sd *)gspca_dev; | |
175 | int i = 0; | |
176 | int retval = -1; | |
177 | unsigned char regs_to_read[] = {0x57, 0x02, 0x03, 0x5d, 0x5e, 0x5f}; | |
178 | ||
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]); | |
183 | if (retval < 0) | |
184 | return retval; | |
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]); | |
188 | if (retval < 0) | |
189 | return retval; | |
190 | sd->firmware_id[i] = gspca_dev->usb_buf[0]; | |
191 | } | |
192 | PDEBUG(D_PROBE, "firmware ID is %02x%02x%02x%02x%02x%02x", | |
193 | sd->firmware_id[0], | |
194 | sd->firmware_id[1], | |
195 | sd->firmware_id[2], | |
196 | sd->firmware_id[3], | |
197 | sd->firmware_id[4], | |
198 | sd->firmware_id[5]); | |
199 | return 0; | |
200 | } | |
201 | ||
202 | static int jl2005c_stream_start_vga_lg | |
203 | (struct gspca_dev *gspca_dev) | |
204 | { | |
205 | int i; | |
206 | int retval = -1; | |
207 | static u8 instruction[][2] = { | |
208 | {0x05, 0x00}, | |
209 | {0x7c, 0x00}, | |
210 | {0x7d, 0x18}, | |
211 | {0x02, 0x00}, | |
212 | {0x01, 0x00}, | |
213 | {0x04, 0x52}, | |
214 | }; | |
215 | ||
216 | for (i = 0; i < ARRAY_SIZE(instruction); i++) { | |
217 | msleep(60); | |
218 | retval = jl2005c_write2(gspca_dev, instruction[i]); | |
219 | if (retval < 0) | |
220 | return retval; | |
221 | } | |
222 | msleep(60); | |
223 | return retval; | |
224 | } | |
225 | ||
226 | static int jl2005c_stream_start_vga_small(struct gspca_dev *gspca_dev) | |
227 | { | |
228 | int i; | |
229 | int retval = -1; | |
230 | static u8 instruction[][2] = { | |
231 | {0x06, 0x00}, | |
232 | {0x7c, 0x00}, | |
233 | {0x7d, 0x1a}, | |
234 | {0x02, 0x00}, | |
235 | {0x01, 0x00}, | |
236 | {0x04, 0x52}, | |
237 | }; | |
238 | ||
239 | for (i = 0; i < ARRAY_SIZE(instruction); i++) { | |
240 | msleep(60); | |
241 | retval = jl2005c_write2(gspca_dev, instruction[i]); | |
242 | if (retval < 0) | |
243 | return retval; | |
244 | } | |
245 | msleep(60); | |
246 | return retval; | |
247 | } | |
248 | ||
249 | static int jl2005c_stream_start_cif_lg(struct gspca_dev *gspca_dev) | |
250 | { | |
251 | int i; | |
252 | int retval = -1; | |
253 | static u8 instruction[][2] = { | |
254 | {0x05, 0x00}, | |
255 | {0x7c, 0x00}, | |
256 | {0x7d, 0x30}, | |
257 | {0x02, 0x00}, | |
258 | {0x01, 0x00}, | |
259 | {0x04, 0x42}, | |
260 | }; | |
261 | ||
262 | for (i = 0; i < ARRAY_SIZE(instruction); i++) { | |
263 | msleep(60); | |
264 | retval = jl2005c_write2(gspca_dev, instruction[i]); | |
265 | if (retval < 0) | |
266 | return retval; | |
267 | } | |
268 | msleep(60); | |
269 | return retval; | |
270 | } | |
271 | ||
272 | static int jl2005c_stream_start_cif_small(struct gspca_dev *gspca_dev) | |
273 | { | |
274 | int i; | |
275 | int retval = -1; | |
276 | static u8 instruction[][2] = { | |
277 | {0x06, 0x00}, | |
278 | {0x7c, 0x00}, | |
279 | {0x7d, 0x32}, | |
280 | {0x02, 0x00}, | |
281 | {0x01, 0x00}, | |
282 | {0x04, 0x42}, | |
283 | }; | |
284 | ||
285 | for (i = 0; i < ARRAY_SIZE(instruction); i++) { | |
286 | msleep(60); | |
287 | retval = jl2005c_write2(gspca_dev, instruction[i]); | |
288 | if (retval < 0) | |
289 | return retval; | |
290 | } | |
291 | msleep(60); | |
292 | return retval; | |
293 | } | |
294 | ||
295 | ||
296 | static int jl2005c_stop(struct gspca_dev *gspca_dev) | |
297 | { | |
29a8d979 | 298 | return jl2005c_write_reg(gspca_dev, 0x07, 0x00); |
fe3449a4 TK |
299 | } |
300 | ||
844db450 HG |
301 | /* |
302 | * This function is called as a workqueue function and runs whenever the camera | |
fe3449a4 TK |
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 | |
844db450 HG |
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. | |
fe3449a4 TK |
308 | */ |
309 | static void jl2005c_dostream(struct work_struct *work) | |
310 | { | |
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. */ | |
315 | int header_read = 0; | |
316 | unsigned char header_sig[2] = {0x4a, 0x4c}; | |
317 | int act_len; | |
318 | int packet_type; | |
319 | int ret; | |
320 | u8 *buffer; | |
321 | ||
322 | buffer = kmalloc(JL2005C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA); | |
323 | if (!buffer) { | |
324 | pr_err("Couldn't allocate USB buffer\n"); | |
325 | goto quit_stream; | |
326 | } | |
327 | ||
345321dc | 328 | while (gspca_dev->present && gspca_dev->streaming) { |
4ad34da0 HV |
329 | #ifdef CONFIG_PM |
330 | if (gspca_dev->frozen) | |
331 | break; | |
332 | #endif | |
fe3449a4 TK |
333 | /* Check if this is a new frame. If so, start the frame first */ |
334 | if (!header_read) { | |
335 | mutex_lock(&gspca_dev->usb_lock); | |
336 | ret = jl2005c_start_new_frame(gspca_dev); | |
337 | mutex_unlock(&gspca_dev->usb_lock); | |
338 | if (ret < 0) | |
339 | goto quit_stream; | |
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); | |
344 | PDEBUG(D_PACK, | |
345 | "Got %d bytes out of %d for header", | |
346 | act_len, JL2005C_MAX_TRANSFER); | |
347 | if (ret < 0 || act_len < JL2005C_MAX_TRANSFER) | |
348 | goto quit_stream; | |
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"); | |
352 | goto quit_stream; | |
353 | } | |
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, | |
361 | buffer, act_len); | |
362 | header_read = 1; | |
363 | } | |
345321dc | 364 | while (bytes_left > 0 && gspca_dev->present) { |
fe3449a4 TK |
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) | |
372 | goto quit_stream; | |
373 | PDEBUG(D_PACK, | |
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; | |
379 | header_read = 0; | |
380 | } else | |
381 | packet_type = INTER_PACKET; | |
382 | gspca_frame_add(gspca_dev, packet_type, | |
383 | buffer, data_len); | |
384 | } | |
385 | } | |
386 | quit_stream: | |
345321dc | 387 | if (gspca_dev->present) { |
fe3449a4 TK |
388 | mutex_lock(&gspca_dev->usb_lock); |
389 | jl2005c_stop(gspca_dev); | |
390 | mutex_unlock(&gspca_dev->usb_lock); | |
391 | } | |
392 | kfree(buffer); | |
393 | } | |
394 | ||
395 | ||
396 | ||
397 | ||
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) | |
401 | { | |
402 | struct cam *cam; | |
403 | struct sd *sd = (struct sd *) gspca_dev; | |
404 | ||
405 | cam = &gspca_dev->cam; | |
406 | /* We don't use the buffer gspca allocates so make it small. */ | |
407 | cam->bulk_size = 64; | |
408 | cam->bulk = 1; | |
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 | |
415 | * JL2005C cameras | |
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 | |
419 | * | |
420 | * Based upon this scanty evidence, we can detect a CIF camera by | |
421 | * testing byte 0 for 0x4x. | |
422 | */ | |
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; | |
427 | } else { | |
428 | cam->cam_mode = vga_mode; | |
429 | cam->nmodes = ARRAY_SIZE(vga_mode); | |
430 | sd->block_size = 0x200; | |
431 | } | |
432 | ||
433 | INIT_WORK(&sd->work_struct, jl2005c_dostream); | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
438 | /* this function is called at probe and resume time */ | |
439 | static int sd_init(struct gspca_dev *gspca_dev) | |
440 | { | |
441 | return 0; | |
442 | } | |
443 | ||
444 | static int sd_start(struct gspca_dev *gspca_dev) | |
445 | { | |
446 | ||
447 | struct sd *sd = (struct sd *) gspca_dev; | |
448 | sd->cap_mode = gspca_dev->cam.cam_mode; | |
449 | ||
1966bc2a | 450 | switch (gspca_dev->pixfmt.width) { |
fe3449a4 TK |
451 | case 640: |
452 | PDEBUG(D_STREAM, "Start streaming at vga resolution"); | |
453 | jl2005c_stream_start_vga_lg(gspca_dev); | |
454 | break; | |
455 | case 320: | |
456 | PDEBUG(D_STREAM, "Start streaming at qvga resolution"); | |
457 | jl2005c_stream_start_vga_small(gspca_dev); | |
458 | break; | |
459 | case 352: | |
460 | PDEBUG(D_STREAM, "Start streaming at cif resolution"); | |
461 | jl2005c_stream_start_cif_lg(gspca_dev); | |
462 | break; | |
463 | case 176: | |
464 | PDEBUG(D_STREAM, "Start streaming at qcif resolution"); | |
465 | jl2005c_stream_start_cif_small(gspca_dev); | |
466 | break; | |
467 | default: | |
468 | pr_err("Unknown resolution specified\n"); | |
469 | return -1; | |
470 | } | |
471 | ||
e5964689 | 472 | schedule_work(&sd->work_struct); |
fe3449a4 TK |
473 | |
474 | return 0; | |
475 | } | |
476 | ||
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) | |
480 | { | |
481 | struct sd *dev = (struct sd *) gspca_dev; | |
482 | ||
483 | /* wait for the work queue to terminate */ | |
484 | mutex_unlock(&gspca_dev->usb_lock); | |
485 | /* This waits for sq905c_dostream to finish */ | |
e5964689 | 486 | flush_work(&dev->work_struct); |
fe3449a4 TK |
487 | mutex_lock(&gspca_dev->usb_lock); |
488 | } | |
489 | ||
490 | ||
491 | ||
492 | /* sub-driver description */ | |
493 | static const struct sd_desc sd_desc = { | |
494 | .name = MODULE_NAME, | |
fe3449a4 TK |
495 | .config = sd_config, |
496 | .init = sd_init, | |
497 | .start = sd_start, | |
498 | .stop0 = sd_stop0, | |
499 | }; | |
500 | ||
501 | /* -- module initialisation -- */ | |
ec063351 | 502 | static const struct usb_device_id device_table[] = { |
fe3449a4 TK |
503 | {USB_DEVICE(0x0979, 0x0227)}, |
504 | {} | |
505 | }; | |
506 | MODULE_DEVICE_TABLE(usb, device_table); | |
507 | ||
508 | /* -- device connect -- */ | |
509 | static int sd_probe(struct usb_interface *intf, | |
510 | const struct usb_device_id *id) | |
511 | { | |
512 | return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), | |
513 | THIS_MODULE); | |
514 | } | |
515 | ||
516 | static struct usb_driver sd_driver = { | |
517 | .name = MODULE_NAME, | |
518 | .id_table = device_table, | |
519 | .probe = sd_probe, | |
520 | .disconnect = gspca_disconnect, | |
521 | #ifdef CONFIG_PM | |
522 | .suspend = gspca_suspend, | |
523 | .resume = gspca_resume, | |
8bb58964 | 524 | .reset_resume = gspca_resume, |
fe3449a4 TK |
525 | #endif |
526 | }; | |
527 | ||
e52ec680 | 528 | module_usb_driver(sd_driver); |