4 * Supports some Jeilin dual-mode cameras which use bulk transport and
5 * download raw JPEG data.
7 * Copyright (C) 2009 Theodore Kilgore
9 * Sportscam DV15 support and control settings are
10 * Copyright (C) 2011 Patrice Chotard
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define MODULE_NAME "jeilinj"
29 #include <linux/slab.h>
33 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
34 MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
35 MODULE_LICENSE("GPL");
37 /* Default timeouts, in ms */
38 #define JEILINJ_CMD_TIMEOUT 500
39 #define JEILINJ_CMD_DELAY 160
40 #define JEILINJ_DATA_TIMEOUT 1000
42 /* Maximum transfer size to use. */
43 #define JEILINJ_MAX_TRANSFER 0x200
44 #define FRAME_HEADER_LEN 0x10
45 #define FRAME_START 0xFFFFFFFF
52 #define CAMQUALITY_MIN 0 /* highest cam quality */
53 #define CAMQUALITY_MAX 97 /* lowest cam quality */
61 NCTRLS
/* number of controls */
64 /* Structure to hold all of our device specific stuff */
66 struct gspca_dev gspca_dev
; /* !! must be the first item */
67 struct gspca_ctrl ctrls
[NCTRLS
];
69 const struct v4l2_pix_format
*cap_mode
;
72 u8 quality
; /* image quality */
73 #define QUALITY_MIN 35
74 #define QUALITY_MAX 85
75 #define QUALITY_DEF 85
76 u8 jpeg_hdr
[JPEG_HDR_SZ
];
80 unsigned char instruction
[2];
81 unsigned char ack_wanted
;
85 /* AFAICT these cameras will only do 320x240. */
86 static struct v4l2_pix_format jlj_mode
[] = {
87 { 320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
89 .sizeimage
= 320 * 240,
90 .colorspace
= V4L2_COLORSPACE_JPEG
,
92 { 640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
94 .sizeimage
= 640 * 480,
95 .colorspace
= V4L2_COLORSPACE_JPEG
,
100 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
101 * and 0x82 for bulk transfer.
104 /* All commands are two bytes only */
105 static void jlj_write2(struct gspca_dev
*gspca_dev
, unsigned char *command
)
109 if (gspca_dev
->usb_err
< 0)
111 memcpy(gspca_dev
->usb_buf
, command
, 2);
112 retval
= usb_bulk_msg(gspca_dev
->dev
,
113 usb_sndbulkpipe(gspca_dev
->dev
, 3),
114 gspca_dev
->usb_buf
, 2, NULL
, 500);
116 err("command write [%02x] error %d",
117 gspca_dev
->usb_buf
[0], retval
);
118 gspca_dev
->usb_err
= retval
;
122 /* Responses are one byte only */
123 static void jlj_read1(struct gspca_dev
*gspca_dev
, unsigned char response
)
127 if (gspca_dev
->usb_err
< 0)
129 retval
= usb_bulk_msg(gspca_dev
->dev
,
130 usb_rcvbulkpipe(gspca_dev
->dev
, 0x84),
131 gspca_dev
->usb_buf
, 1, NULL
, 500);
132 response
= gspca_dev
->usb_buf
[0];
134 err("read command [%02x] error %d",
135 gspca_dev
->usb_buf
[0], retval
);
136 gspca_dev
->usb_err
= retval
;
140 static void setfreq(struct gspca_dev
*gspca_dev
)
142 struct sd
*sd
= (struct sd
*) gspca_dev
;
143 u8 freq_commands
[][2] = {
148 freq_commands
[0][1] |= (sd
->ctrls
[LIGHTFREQ
].val
>> 1);
150 jlj_write2(gspca_dev
, freq_commands
[0]);
151 jlj_write2(gspca_dev
, freq_commands
[1]);
154 static void setcamquality(struct gspca_dev
*gspca_dev
)
156 struct sd
*sd
= (struct sd
*) gspca_dev
;
157 u8 quality_commands
[][2] = {
163 /* adapt camera quality from jpeg quality */
164 camquality
= ((QUALITY_MAX
- sd
->quality
) * CAMQUALITY_MAX
)
165 / (QUALITY_MAX
- QUALITY_MIN
);
166 quality_commands
[0][1] += camquality
;
168 jlj_write2(gspca_dev
, quality_commands
[0]);
169 jlj_write2(gspca_dev
, quality_commands
[1]);
172 static void setautogain(struct gspca_dev
*gspca_dev
)
174 struct sd
*sd
= (struct sd
*) gspca_dev
;
175 u8 autogain_commands
[][2] = {
180 autogain_commands
[1][1] = (sd
->ctrls
[AUTOGAIN
].val
<< 4);
182 jlj_write2(gspca_dev
, autogain_commands
[0]);
183 jlj_write2(gspca_dev
, autogain_commands
[1]);
186 static void setred(struct gspca_dev
*gspca_dev
)
188 struct sd
*sd
= (struct sd
*) gspca_dev
;
189 u8 setred_commands
[][2] = {
194 setred_commands
[1][1] = sd
->ctrls
[RED
].val
;
196 jlj_write2(gspca_dev
, setred_commands
[0]);
197 jlj_write2(gspca_dev
, setred_commands
[1]);
200 static void setgreen(struct gspca_dev
*gspca_dev
)
202 struct sd
*sd
= (struct sd
*) gspca_dev
;
203 u8 setgreen_commands
[][2] = {
208 setgreen_commands
[1][1] = sd
->ctrls
[GREEN
].val
;
210 jlj_write2(gspca_dev
, setgreen_commands
[0]);
211 jlj_write2(gspca_dev
, setgreen_commands
[1]);
214 static void setblue(struct gspca_dev
*gspca_dev
)
216 struct sd
*sd
= (struct sd
*) gspca_dev
;
217 u8 setblue_commands
[][2] = {
222 setblue_commands
[1][1] = sd
->ctrls
[BLUE
].val
;
224 jlj_write2(gspca_dev
, setblue_commands
[0]);
225 jlj_write2(gspca_dev
, setblue_commands
[1]);
228 static const struct ctrl sd_ctrls
[NCTRLS
] = {
231 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
232 .type
= V4L2_CTRL_TYPE_MENU
,
233 .name
= "Light frequency filter",
234 .minimum
= V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
, /* 1 */
235 .maximum
= V4L2_CID_POWER_LINE_FREQUENCY_60HZ
, /* 2 */
237 .default_value
= V4L2_CID_POWER_LINE_FREQUENCY_60HZ
,
239 .set_control
= setfreq
243 .id
= V4L2_CID_AUTOGAIN
,
244 .type
= V4L2_CTRL_TYPE_INTEGER
,
245 .name
= "Automatic Gain (and Exposure)",
249 #define AUTOGAIN_DEF 0
250 .default_value
= AUTOGAIN_DEF
,
252 .set_control
= setautogain
256 .id
= V4L2_CID_RED_BALANCE
,
257 .type
= V4L2_CTRL_TYPE_INTEGER
,
258 .name
= "red balance",
262 #define RED_BALANCE_DEF 2
263 .default_value
= RED_BALANCE_DEF
,
265 .set_control
= setred
271 .type
= V4L2_CTRL_TYPE_INTEGER
,
272 .name
= "green balance",
276 #define GREEN_BALANCE_DEF 2
277 .default_value
= GREEN_BALANCE_DEF
,
279 .set_control
= setgreen
283 .id
= V4L2_CID_BLUE_BALANCE
,
284 .type
= V4L2_CTRL_TYPE_INTEGER
,
285 .name
= "blue balance",
289 #define BLUE_BALANCE_DEF 2
290 .default_value
= BLUE_BALANCE_DEF
,
292 .set_control
= setblue
296 static int jlj_start(struct gspca_dev
*gspca_dev
)
299 int start_commands_size
;
301 struct sd
*sd
= (struct sd
*) gspca_dev
;
302 struct jlj_command start_commands
[] = {
303 {{0x71, 0x81}, 0, 0},
304 {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY
},
305 {{0x95, 0x70}, 1, 0},
306 {{0x71, 0x81 - gspca_dev
->curr_mode
}, 0, 0},
307 {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY
},
308 {{0x95, 0x70}, 1, 0},
309 {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
310 {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY
},
311 {{0x95, 0x70}, 1, 0},
312 #define SPORTSCAM_DV15_CMD_SIZE 9
313 {{0x94, 0x02}, 0, 0},
314 {{0xde, 0x24}, 0, 0},
315 {{0x94, 0x02}, 0, 0},
316 {{0xdd, 0xf0}, 0, 0},
317 {{0x94, 0x02}, 0, 0},
318 {{0xe3, 0x2c}, 0, 0},
319 {{0x94, 0x02}, 0, 0},
320 {{0xe4, 0x00}, 0, 0},
321 {{0x94, 0x02}, 0, 0},
322 {{0xe5, 0x00}, 0, 0},
323 {{0x94, 0x02}, 0, 0},
324 {{0xe6, 0x2c}, 0, 0},
325 {{0x94, 0x03}, 0, 0},
330 /* Under Windows, USB spy shows that only the 9 first start
331 * commands are used for SPORTSCAM_DV15 webcam
333 if (sd
->type
== SPORTSCAM_DV15
)
334 start_commands_size
= SPORTSCAM_DV15_CMD_SIZE
;
336 start_commands_size
= ARRAY_SIZE(start_commands
);
338 for (i
= 0; i
< start_commands_size
; i
++) {
339 jlj_write2(gspca_dev
, start_commands
[i
].instruction
);
340 if (start_commands
[i
].delay
)
341 msleep(start_commands
[i
].delay
);
342 if (start_commands
[i
].ack_wanted
)
343 jlj_read1(gspca_dev
, response
);
345 setcamquality(gspca_dev
);
348 if (gspca_dev
->usb_err
< 0)
349 PDEBUG(D_ERR
, "Start streaming command failed");
350 return gspca_dev
->usb_err
;
353 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
356 struct sd
*sd
= (struct sd
*) gspca_dev
;
360 PDEBUG(D_STREAM
, "Got %d bytes out of %d for Block 0",
361 len
, JEILINJ_MAX_TRANSFER
);
362 if (len
!= JEILINJ_MAX_TRANSFER
) {
363 PDEBUG(D_PACK
, "bad length");
366 /* check if it's start of frame */
367 header_marker
= ((u32
*)data
)[0];
368 if (header_marker
== FRAME_START
) {
369 sd
->blocks_left
= data
[0x0a] - 1;
370 PDEBUG(D_STREAM
, "blocks_left = 0x%x", sd
->blocks_left
);
371 /* Start a new frame, and add the JPEG header, first thing */
372 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
373 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
374 /* Toss line 0 of data block 0, keep the rest. */
375 gspca_frame_add(gspca_dev
, INTER_PACKET
,
376 data
+ FRAME_HEADER_LEN
,
377 JEILINJ_MAX_TRANSFER
- FRAME_HEADER_LEN
);
378 } else if (sd
->blocks_left
> 0) {
379 PDEBUG(D_STREAM
, "%d blocks remaining for frame",
381 sd
->blocks_left
-= 1;
382 if (sd
->blocks_left
== 0)
383 packet_type
= LAST_PACKET
;
385 packet_type
= INTER_PACKET
;
386 gspca_frame_add(gspca_dev
, packet_type
,
387 data
, JEILINJ_MAX_TRANSFER
);
392 /* Discard data until a new frame starts. */
393 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
396 /* This function is called at probe time just before sd_init */
397 static int sd_config(struct gspca_dev
*gspca_dev
,
398 const struct usb_device_id
*id
)
400 struct cam
*cam
= &gspca_dev
->cam
;
401 struct sd
*dev
= (struct sd
*) gspca_dev
;
403 dev
->type
= id
->driver_info
;
404 gspca_dev
->cam
.ctrls
= dev
->ctrls
;
405 dev
->quality
= QUALITY_DEF
;
406 dev
->ctrls
[LIGHTFREQ
].def
= V4L2_CID_POWER_LINE_FREQUENCY_60HZ
;
407 dev
->ctrls
[RED
].def
= RED_BALANCE_DEF
;
408 dev
->ctrls
[GREEN
].def
= GREEN_BALANCE_DEF
;
409 dev
->ctrls
[BLUE
].def
= BLUE_BALANCE_DEF
;
411 "JEILINJ camera detected"
412 " (vid/pid 0x%04X:0x%04X)", id
->idVendor
, id
->idProduct
);
413 cam
->cam_mode
= jlj_mode
;
414 cam
->nmodes
= ARRAY_SIZE(jlj_mode
);
417 cam
->bulk_size
= JEILINJ_MAX_TRANSFER
;
421 static void sd_stopN(struct gspca_dev
*gspca_dev
)
425 u8 stop_commands
[][2] = {
433 /* get the image remaining blocks */
434 usb_bulk_msg(gspca_dev
->dev
,
435 gspca_dev
->urb
[0]->pipe
,
436 gspca_dev
->urb
[0]->transfer_buffer
,
437 JEILINJ_MAX_TRANSFER
, NULL
,
438 JEILINJ_DATA_TIMEOUT
);
440 /* search for 0xff 0xd9 (EOF for JPEG) */
442 buf
= gspca_dev
->urb
[0]->transfer_buffer
;
443 while ((i
< (JEILINJ_MAX_TRANSFER
- 1)) &&
444 ((buf
[i
] != 0xff) || (buf
[i
+1] != 0xd9)))
447 if (i
!= (JEILINJ_MAX_TRANSFER
- 1))
448 /* last remaining block found */
452 for (i
= 0; i
< ARRAY_SIZE(stop_commands
); i
++)
453 jlj_write2(gspca_dev
, stop_commands
[i
]);
456 /* this function is called at probe and resume time */
457 static int sd_init(struct gspca_dev
*gspca_dev
)
459 return gspca_dev
->usb_err
;
462 /* Set up for getting frames. */
463 static int sd_start(struct gspca_dev
*gspca_dev
)
465 struct sd
*dev
= (struct sd
*) gspca_dev
;
467 /* create the JPEG header */
468 jpeg_define(dev
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
469 0x21); /* JPEG 422 */
470 jpeg_set_qual(dev
->jpeg_hdr
, dev
->quality
);
471 PDEBUG(D_STREAM
, "Start streaming at %dx%d",
472 gspca_dev
->height
, gspca_dev
->width
);
473 jlj_start(gspca_dev
);
474 return gspca_dev
->usb_err
;
477 /* Table of supported USB devices */
478 static const struct usb_device_id device_table
[] = {
479 {USB_DEVICE(0x0979, 0x0280), .driver_info
= SAKAR_57379
},
480 {USB_DEVICE(0x0979, 0x0270), .driver_info
= SPORTSCAM_DV15
},
484 MODULE_DEVICE_TABLE(usb
, device_table
);
486 static int sd_querymenu(struct gspca_dev
*gspca_dev
,
487 struct v4l2_querymenu
*menu
)
490 case V4L2_CID_POWER_LINE_FREQUENCY
:
491 switch (menu
->index
) {
492 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
493 strcpy((char *) menu
->name
, "disable");
495 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
496 strcpy((char *) menu
->name
, "50 Hz");
498 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
499 strcpy((char *) menu
->name
, "60 Hz");
507 static int sd_set_jcomp(struct gspca_dev
*gspca_dev
,
508 struct v4l2_jpegcompression
*jcomp
)
510 struct sd
*sd
= (struct sd
*) gspca_dev
;
512 if (jcomp
->quality
< QUALITY_MIN
)
513 sd
->quality
= QUALITY_MIN
;
514 else if (jcomp
->quality
> QUALITY_MAX
)
515 sd
->quality
= QUALITY_MAX
;
517 sd
->quality
= jcomp
->quality
;
518 if (gspca_dev
->streaming
) {
519 jpeg_set_qual(sd
->jpeg_hdr
, sd
->quality
);
520 setcamquality(gspca_dev
);
525 static int sd_get_jcomp(struct gspca_dev
*gspca_dev
,
526 struct v4l2_jpegcompression
*jcomp
)
528 struct sd
*sd
= (struct sd
*) gspca_dev
;
530 memset(jcomp
, 0, sizeof *jcomp
);
531 jcomp
->quality
= sd
->quality
;
532 jcomp
->jpeg_markers
= V4L2_JPEG_MARKER_DHT
533 | V4L2_JPEG_MARKER_DQT
;
538 /* sub-driver description */
539 static const struct sd_desc sd_desc_sakar_57379
= {
545 .pkt_scan
= sd_pkt_scan
,
548 /* sub-driver description */
549 static const struct sd_desc sd_desc_sportscam_dv15
= {
555 .pkt_scan
= sd_pkt_scan
,
557 .nctrls
= ARRAY_SIZE(sd_ctrls
),
558 .querymenu
= sd_querymenu
,
559 .get_jcomp
= sd_get_jcomp
,
560 .set_jcomp
= sd_set_jcomp
,
563 static const struct sd_desc
*sd_desc
[2] = {
564 &sd_desc_sakar_57379
,
565 &sd_desc_sportscam_dv15
568 /* -- device connect -- */
569 static int sd_probe(struct usb_interface
*intf
,
570 const struct usb_device_id
*id
)
572 return gspca_dev_probe(intf
, id
,
573 sd_desc
[id
->driver_info
],
578 static struct usb_driver sd_driver
= {
580 .id_table
= device_table
,
582 .disconnect
= gspca_disconnect
,
584 .suspend
= gspca_suspend
,
585 .resume
= gspca_resume
,
589 /* -- module insert / remove -- */
590 static int __init
sd_mod_init(void)
592 return usb_register(&sd_driver
);
595 static void __exit
sd_mod_exit(void)
597 usb_deregister(&sd_driver
);
600 module_init(sd_mod_init
);
601 module_exit(sd_mod_exit
);