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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #define MODULE_NAME "jeilinj"
31 #include <linux/slab.h>
35 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
36 MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
37 MODULE_LICENSE("GPL");
39 /* Default timeouts, in ms */
40 #define JEILINJ_CMD_TIMEOUT 500
41 #define JEILINJ_CMD_DELAY 160
42 #define JEILINJ_DATA_TIMEOUT 1000
44 /* Maximum transfer size to use. */
45 #define JEILINJ_MAX_TRANSFER 0x200
46 #define FRAME_HEADER_LEN 0x10
47 #define FRAME_START 0xFFFFFFFF
54 #define CAMQUALITY_MIN 0 /* highest cam quality */
55 #define CAMQUALITY_MAX 97 /* lowest cam quality */
57 /* Structure to hold all of our device specific stuff */
59 struct gspca_dev gspca_dev
; /* !! must be the first item */
61 const struct v4l2_pix_format
*cap_mode
;
62 struct v4l2_ctrl
*freq
;
63 struct v4l2_ctrl
*jpegqual
;
66 u8 quality
; /* image quality */
67 #define QUALITY_MIN 35
68 #define QUALITY_MAX 85
69 #define QUALITY_DEF 85
70 u8 jpeg_hdr
[JPEG_HDR_SZ
];
74 unsigned char instruction
[2];
75 unsigned char ack_wanted
;
79 /* AFAICT these cameras will only do 320x240. */
80 static struct v4l2_pix_format jlj_mode
[] = {
81 { 320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
83 .sizeimage
= 320 * 240,
84 .colorspace
= V4L2_COLORSPACE_JPEG
,
86 { 640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
88 .sizeimage
= 640 * 480,
89 .colorspace
= V4L2_COLORSPACE_JPEG
,
94 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
95 * and 0x82 for bulk transfer.
98 /* All commands are two bytes only */
99 static void jlj_write2(struct gspca_dev
*gspca_dev
, unsigned char *command
)
103 if (gspca_dev
->usb_err
< 0)
105 memcpy(gspca_dev
->usb_buf
, command
, 2);
106 retval
= usb_bulk_msg(gspca_dev
->dev
,
107 usb_sndbulkpipe(gspca_dev
->dev
, 3),
108 gspca_dev
->usb_buf
, 2, NULL
, 500);
110 pr_err("command write [%02x] error %d\n",
111 gspca_dev
->usb_buf
[0], retval
);
112 gspca_dev
->usb_err
= retval
;
116 /* Responses are one byte only */
117 static void jlj_read1(struct gspca_dev
*gspca_dev
, unsigned char response
)
121 if (gspca_dev
->usb_err
< 0)
123 retval
= usb_bulk_msg(gspca_dev
->dev
,
124 usb_rcvbulkpipe(gspca_dev
->dev
, 0x84),
125 gspca_dev
->usb_buf
, 1, NULL
, 500);
126 response
= gspca_dev
->usb_buf
[0];
128 pr_err("read command [%02x] error %d\n",
129 gspca_dev
->usb_buf
[0], retval
);
130 gspca_dev
->usb_err
= retval
;
134 static void setfreq(struct gspca_dev
*gspca_dev
, s32 val
)
136 u8 freq_commands
[][2] = {
141 freq_commands
[0][1] |= val
>> 1;
143 jlj_write2(gspca_dev
, freq_commands
[0]);
144 jlj_write2(gspca_dev
, freq_commands
[1]);
147 static void setcamquality(struct gspca_dev
*gspca_dev
, s32 val
)
149 u8 quality_commands
[][2] = {
155 /* adapt camera quality from jpeg quality */
156 camquality
= ((QUALITY_MAX
- val
) * CAMQUALITY_MAX
)
157 / (QUALITY_MAX
- QUALITY_MIN
);
158 quality_commands
[0][1] += camquality
;
160 jlj_write2(gspca_dev
, quality_commands
[0]);
161 jlj_write2(gspca_dev
, quality_commands
[1]);
164 static void setautogain(struct gspca_dev
*gspca_dev
, s32 val
)
166 u8 autogain_commands
[][2] = {
171 autogain_commands
[1][1] = val
<< 4;
173 jlj_write2(gspca_dev
, autogain_commands
[0]);
174 jlj_write2(gspca_dev
, autogain_commands
[1]);
177 static void setred(struct gspca_dev
*gspca_dev
, s32 val
)
179 u8 setred_commands
[][2] = {
184 setred_commands
[1][1] = val
;
186 jlj_write2(gspca_dev
, setred_commands
[0]);
187 jlj_write2(gspca_dev
, setred_commands
[1]);
190 static void setgreen(struct gspca_dev
*gspca_dev
, s32 val
)
192 u8 setgreen_commands
[][2] = {
197 setgreen_commands
[1][1] = val
;
199 jlj_write2(gspca_dev
, setgreen_commands
[0]);
200 jlj_write2(gspca_dev
, setgreen_commands
[1]);
203 static void setblue(struct gspca_dev
*gspca_dev
, s32 val
)
205 u8 setblue_commands
[][2] = {
210 setblue_commands
[1][1] = val
;
212 jlj_write2(gspca_dev
, setblue_commands
[0]);
213 jlj_write2(gspca_dev
, setblue_commands
[1]);
216 static int jlj_start(struct gspca_dev
*gspca_dev
)
219 int start_commands_size
;
221 struct sd
*sd
= (struct sd
*) gspca_dev
;
222 struct jlj_command start_commands
[] = {
223 {{0x71, 0x81}, 0, 0},
224 {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY
},
225 {{0x95, 0x70}, 1, 0},
226 {{0x71, 0x81 - gspca_dev
->curr_mode
}, 0, 0},
227 {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY
},
228 {{0x95, 0x70}, 1, 0},
229 {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
230 {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY
},
231 {{0x95, 0x70}, 1, 0},
232 #define SPORTSCAM_DV15_CMD_SIZE 9
233 {{0x94, 0x02}, 0, 0},
234 {{0xde, 0x24}, 0, 0},
235 {{0x94, 0x02}, 0, 0},
236 {{0xdd, 0xf0}, 0, 0},
237 {{0x94, 0x02}, 0, 0},
238 {{0xe3, 0x2c}, 0, 0},
239 {{0x94, 0x02}, 0, 0},
240 {{0xe4, 0x00}, 0, 0},
241 {{0x94, 0x02}, 0, 0},
242 {{0xe5, 0x00}, 0, 0},
243 {{0x94, 0x02}, 0, 0},
244 {{0xe6, 0x2c}, 0, 0},
245 {{0x94, 0x03}, 0, 0},
250 /* Under Windows, USB spy shows that only the 9 first start
251 * commands are used for SPORTSCAM_DV15 webcam
253 if (sd
->type
== SPORTSCAM_DV15
)
254 start_commands_size
= SPORTSCAM_DV15_CMD_SIZE
;
256 start_commands_size
= ARRAY_SIZE(start_commands
);
258 for (i
= 0; i
< start_commands_size
; i
++) {
259 jlj_write2(gspca_dev
, start_commands
[i
].instruction
);
260 if (start_commands
[i
].delay
)
261 msleep(start_commands
[i
].delay
);
262 if (start_commands
[i
].ack_wanted
)
263 jlj_read1(gspca_dev
, response
);
265 setcamquality(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->jpegqual
));
267 setfreq(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->freq
));
268 if (gspca_dev
->usb_err
< 0)
269 PDEBUG(D_ERR
, "Start streaming command failed");
270 return gspca_dev
->usb_err
;
273 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
276 struct sd
*sd
= (struct sd
*) gspca_dev
;
280 PDEBUG(D_STREAM
, "Got %d bytes out of %d for Block 0",
281 len
, JEILINJ_MAX_TRANSFER
);
282 if (len
!= JEILINJ_MAX_TRANSFER
) {
283 PDEBUG(D_PACK
, "bad length");
286 /* check if it's start of frame */
287 header_marker
= ((u32
*)data
)[0];
288 if (header_marker
== FRAME_START
) {
289 sd
->blocks_left
= data
[0x0a] - 1;
290 PDEBUG(D_STREAM
, "blocks_left = 0x%x", sd
->blocks_left
);
291 /* Start a new frame, and add the JPEG header, first thing */
292 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
293 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
294 /* Toss line 0 of data block 0, keep the rest. */
295 gspca_frame_add(gspca_dev
, INTER_PACKET
,
296 data
+ FRAME_HEADER_LEN
,
297 JEILINJ_MAX_TRANSFER
- FRAME_HEADER_LEN
);
298 } else if (sd
->blocks_left
> 0) {
299 PDEBUG(D_STREAM
, "%d blocks remaining for frame",
301 sd
->blocks_left
-= 1;
302 if (sd
->blocks_left
== 0)
303 packet_type
= LAST_PACKET
;
305 packet_type
= INTER_PACKET
;
306 gspca_frame_add(gspca_dev
, packet_type
,
307 data
, JEILINJ_MAX_TRANSFER
);
312 /* Discard data until a new frame starts. */
313 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
316 /* This function is called at probe time just before sd_init */
317 static int sd_config(struct gspca_dev
*gspca_dev
,
318 const struct usb_device_id
*id
)
320 struct cam
*cam
= &gspca_dev
->cam
;
321 struct sd
*dev
= (struct sd
*) gspca_dev
;
323 dev
->type
= id
->driver_info
;
324 dev
->quality
= QUALITY_DEF
;
326 cam
->cam_mode
= jlj_mode
;
327 cam
->nmodes
= ARRAY_SIZE(jlj_mode
);
330 cam
->bulk_size
= JEILINJ_MAX_TRANSFER
;
334 static void sd_stopN(struct gspca_dev
*gspca_dev
)
338 static u8 stop_commands
[][2] = {
346 /* get the image remaining blocks */
347 usb_bulk_msg(gspca_dev
->dev
,
348 gspca_dev
->urb
[0]->pipe
,
349 gspca_dev
->urb
[0]->transfer_buffer
,
350 JEILINJ_MAX_TRANSFER
, NULL
,
351 JEILINJ_DATA_TIMEOUT
);
353 /* search for 0xff 0xd9 (EOF for JPEG) */
355 buf
= gspca_dev
->urb
[0]->transfer_buffer
;
356 while ((i
< (JEILINJ_MAX_TRANSFER
- 1)) &&
357 ((buf
[i
] != 0xff) || (buf
[i
+1] != 0xd9)))
360 if (i
!= (JEILINJ_MAX_TRANSFER
- 1))
361 /* last remaining block found */
365 for (i
= 0; i
< ARRAY_SIZE(stop_commands
); i
++)
366 jlj_write2(gspca_dev
, stop_commands
[i
]);
369 /* this function is called at probe and resume time */
370 static int sd_init(struct gspca_dev
*gspca_dev
)
372 return gspca_dev
->usb_err
;
375 /* Set up for getting frames. */
376 static int sd_start(struct gspca_dev
*gspca_dev
)
378 struct sd
*dev
= (struct sd
*) gspca_dev
;
380 /* create the JPEG header */
381 jpeg_define(dev
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
382 0x21); /* JPEG 422 */
383 jpeg_set_qual(dev
->jpeg_hdr
, dev
->quality
);
384 PDEBUG(D_STREAM
, "Start streaming at %dx%d",
385 gspca_dev
->height
, gspca_dev
->width
);
386 jlj_start(gspca_dev
);
387 return gspca_dev
->usb_err
;
390 /* Table of supported USB devices */
391 static const struct usb_device_id device_table
[] = {
392 {USB_DEVICE(0x0979, 0x0280), .driver_info
= SAKAR_57379
},
393 {USB_DEVICE(0x0979, 0x0270), .driver_info
= SPORTSCAM_DV15
},
397 MODULE_DEVICE_TABLE(usb
, device_table
);
399 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
401 struct gspca_dev
*gspca_dev
=
402 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
403 struct sd
*sd
= (struct sd
*)gspca_dev
;
405 gspca_dev
->usb_err
= 0;
407 if (!gspca_dev
->streaming
)
411 case V4L2_CID_POWER_LINE_FREQUENCY
:
412 setfreq(gspca_dev
, ctrl
->val
);
414 case V4L2_CID_RED_BALANCE
:
415 setred(gspca_dev
, ctrl
->val
);
418 setgreen(gspca_dev
, ctrl
->val
);
420 case V4L2_CID_BLUE_BALANCE
:
421 setblue(gspca_dev
, ctrl
->val
);
423 case V4L2_CID_AUTOGAIN
:
424 setautogain(gspca_dev
, ctrl
->val
);
426 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
427 jpeg_set_qual(sd
->jpeg_hdr
, ctrl
->val
);
428 setcamquality(gspca_dev
, ctrl
->val
);
431 return gspca_dev
->usb_err
;
434 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
438 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
440 struct sd
*sd
= (struct sd
*)gspca_dev
;
441 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
442 static const struct v4l2_ctrl_config custom_autogain
= {
444 .id
= V4L2_CID_AUTOGAIN
,
445 .type
= V4L2_CTRL_TYPE_INTEGER
,
446 .name
= "Automatic Gain (and Exposure)",
452 gspca_dev
->vdev
.ctrl_handler
= hdl
;
453 v4l2_ctrl_handler_init(hdl
, 6);
454 sd
->freq
= v4l2_ctrl_new_std_menu(hdl
, &sd_ctrl_ops
,
455 V4L2_CID_POWER_LINE_FREQUENCY
,
456 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
, 1,
457 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
);
458 v4l2_ctrl_new_custom(hdl
, &custom_autogain
, NULL
);
459 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
460 V4L2_CID_RED_BALANCE
, 0, 3, 1, 2);
461 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
462 V4L2_CID_GAIN
, 0, 3, 1, 2);
463 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
464 V4L2_CID_BLUE_BALANCE
, 0, 3, 1, 2);
465 sd
->jpegqual
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
466 V4L2_CID_JPEG_COMPRESSION_QUALITY
,
467 QUALITY_MIN
, QUALITY_MAX
, 1, QUALITY_DEF
);
470 pr_err("Could not initialize controls\n");
476 static int sd_set_jcomp(struct gspca_dev
*gspca_dev
,
477 struct v4l2_jpegcompression
*jcomp
)
479 struct sd
*sd
= (struct sd
*) gspca_dev
;
481 v4l2_ctrl_s_ctrl(sd
->jpegqual
, jcomp
->quality
);
485 static int sd_get_jcomp(struct gspca_dev
*gspca_dev
,
486 struct v4l2_jpegcompression
*jcomp
)
488 struct sd
*sd
= (struct sd
*) gspca_dev
;
490 memset(jcomp
, 0, sizeof *jcomp
);
491 jcomp
->quality
= v4l2_ctrl_g_ctrl(sd
->jpegqual
);
492 jcomp
->jpeg_markers
= V4L2_JPEG_MARKER_DHT
493 | V4L2_JPEG_MARKER_DQT
;
498 /* sub-driver description */
499 static const struct sd_desc sd_desc_sakar_57379
= {
505 .pkt_scan
= sd_pkt_scan
,
508 /* sub-driver description */
509 static const struct sd_desc sd_desc_sportscam_dv15
= {
513 .init_controls
= sd_init_controls
,
516 .pkt_scan
= sd_pkt_scan
,
517 .get_jcomp
= sd_get_jcomp
,
518 .set_jcomp
= sd_set_jcomp
,
521 static const struct sd_desc
*sd_desc
[2] = {
522 &sd_desc_sakar_57379
,
523 &sd_desc_sportscam_dv15
526 /* -- device connect -- */
527 static int sd_probe(struct usb_interface
*intf
,
528 const struct usb_device_id
*id
)
530 return gspca_dev_probe(intf
, id
,
531 sd_desc
[id
->driver_info
],
536 static struct usb_driver sd_driver
= {
538 .id_table
= device_table
,
540 .disconnect
= gspca_disconnect
,
542 .suspend
= gspca_suspend
,
543 .resume
= gspca_resume
,
544 .reset_resume
= gspca_resume
,
548 module_usb_driver(sd_driver
);