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 */
63 NCTRLS
/* number of controls */
66 /* Structure to hold all of our device specific stuff */
68 struct gspca_dev gspca_dev
; /* !! must be the first item */
69 struct gspca_ctrl ctrls
[NCTRLS
];
71 const struct v4l2_pix_format
*cap_mode
;
74 u8 quality
; /* image quality */
75 #define QUALITY_MIN 35
76 #define QUALITY_MAX 85
77 #define QUALITY_DEF 85
78 u8 jpeg_hdr
[JPEG_HDR_SZ
];
82 unsigned char instruction
[2];
83 unsigned char ack_wanted
;
87 /* AFAICT these cameras will only do 320x240. */
88 static struct v4l2_pix_format jlj_mode
[] = {
89 { 320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
91 .sizeimage
= 320 * 240,
92 .colorspace
= V4L2_COLORSPACE_JPEG
,
94 { 640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
96 .sizeimage
= 640 * 480,
97 .colorspace
= V4L2_COLORSPACE_JPEG
,
102 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
103 * and 0x82 for bulk transfer.
106 /* All commands are two bytes only */
107 static void jlj_write2(struct gspca_dev
*gspca_dev
, unsigned char *command
)
111 if (gspca_dev
->usb_err
< 0)
113 memcpy(gspca_dev
->usb_buf
, command
, 2);
114 retval
= usb_bulk_msg(gspca_dev
->dev
,
115 usb_sndbulkpipe(gspca_dev
->dev
, 3),
116 gspca_dev
->usb_buf
, 2, NULL
, 500);
118 pr_err("command write [%02x] error %d\n",
119 gspca_dev
->usb_buf
[0], retval
);
120 gspca_dev
->usb_err
= retval
;
124 /* Responses are one byte only */
125 static void jlj_read1(struct gspca_dev
*gspca_dev
, unsigned char response
)
129 if (gspca_dev
->usb_err
< 0)
131 retval
= usb_bulk_msg(gspca_dev
->dev
,
132 usb_rcvbulkpipe(gspca_dev
->dev
, 0x84),
133 gspca_dev
->usb_buf
, 1, NULL
, 500);
134 response
= gspca_dev
->usb_buf
[0];
136 pr_err("read command [%02x] error %d\n",
137 gspca_dev
->usb_buf
[0], retval
);
138 gspca_dev
->usb_err
= retval
;
142 static void setfreq(struct gspca_dev
*gspca_dev
)
144 struct sd
*sd
= (struct sd
*) gspca_dev
;
145 u8 freq_commands
[][2] = {
150 freq_commands
[0][1] |= (sd
->ctrls
[LIGHTFREQ
].val
>> 1);
152 jlj_write2(gspca_dev
, freq_commands
[0]);
153 jlj_write2(gspca_dev
, freq_commands
[1]);
156 static void setcamquality(struct gspca_dev
*gspca_dev
)
158 struct sd
*sd
= (struct sd
*) gspca_dev
;
159 u8 quality_commands
[][2] = {
165 /* adapt camera quality from jpeg quality */
166 camquality
= ((QUALITY_MAX
- sd
->quality
) * CAMQUALITY_MAX
)
167 / (QUALITY_MAX
- QUALITY_MIN
);
168 quality_commands
[0][1] += camquality
;
170 jlj_write2(gspca_dev
, quality_commands
[0]);
171 jlj_write2(gspca_dev
, quality_commands
[1]);
174 static void setautogain(struct gspca_dev
*gspca_dev
)
176 struct sd
*sd
= (struct sd
*) gspca_dev
;
177 u8 autogain_commands
[][2] = {
182 autogain_commands
[1][1] = (sd
->ctrls
[AUTOGAIN
].val
<< 4);
184 jlj_write2(gspca_dev
, autogain_commands
[0]);
185 jlj_write2(gspca_dev
, autogain_commands
[1]);
188 static void setred(struct gspca_dev
*gspca_dev
)
190 struct sd
*sd
= (struct sd
*) gspca_dev
;
191 u8 setred_commands
[][2] = {
196 setred_commands
[1][1] = sd
->ctrls
[RED
].val
;
198 jlj_write2(gspca_dev
, setred_commands
[0]);
199 jlj_write2(gspca_dev
, setred_commands
[1]);
202 static void setgreen(struct gspca_dev
*gspca_dev
)
204 struct sd
*sd
= (struct sd
*) gspca_dev
;
205 u8 setgreen_commands
[][2] = {
210 setgreen_commands
[1][1] = sd
->ctrls
[GREEN
].val
;
212 jlj_write2(gspca_dev
, setgreen_commands
[0]);
213 jlj_write2(gspca_dev
, setgreen_commands
[1]);
216 static void setblue(struct gspca_dev
*gspca_dev
)
218 struct sd
*sd
= (struct sd
*) gspca_dev
;
219 u8 setblue_commands
[][2] = {
224 setblue_commands
[1][1] = sd
->ctrls
[BLUE
].val
;
226 jlj_write2(gspca_dev
, setblue_commands
[0]);
227 jlj_write2(gspca_dev
, setblue_commands
[1]);
230 static const struct ctrl sd_ctrls
[NCTRLS
] = {
233 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
234 .type
= V4L2_CTRL_TYPE_MENU
,
235 .name
= "Light frequency filter",
236 .minimum
= V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
, /* 1 */
237 .maximum
= V4L2_CID_POWER_LINE_FREQUENCY_60HZ
, /* 2 */
239 .default_value
= V4L2_CID_POWER_LINE_FREQUENCY_60HZ
,
241 .set_control
= setfreq
245 .id
= V4L2_CID_AUTOGAIN
,
246 .type
= V4L2_CTRL_TYPE_INTEGER
,
247 .name
= "Automatic Gain (and Exposure)",
251 #define AUTOGAIN_DEF 0
252 .default_value
= AUTOGAIN_DEF
,
254 .set_control
= setautogain
258 .id
= V4L2_CID_RED_BALANCE
,
259 .type
= V4L2_CTRL_TYPE_INTEGER
,
260 .name
= "red balance",
264 #define RED_BALANCE_DEF 2
265 .default_value
= RED_BALANCE_DEF
,
267 .set_control
= setred
273 .type
= V4L2_CTRL_TYPE_INTEGER
,
274 .name
= "green balance",
278 #define GREEN_BALANCE_DEF 2
279 .default_value
= GREEN_BALANCE_DEF
,
281 .set_control
= setgreen
285 .id
= V4L2_CID_BLUE_BALANCE
,
286 .type
= V4L2_CTRL_TYPE_INTEGER
,
287 .name
= "blue balance",
291 #define BLUE_BALANCE_DEF 2
292 .default_value
= BLUE_BALANCE_DEF
,
294 .set_control
= setblue
298 static int jlj_start(struct gspca_dev
*gspca_dev
)
301 int start_commands_size
;
303 struct sd
*sd
= (struct sd
*) gspca_dev
;
304 struct jlj_command start_commands
[] = {
305 {{0x71, 0x81}, 0, 0},
306 {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY
},
307 {{0x95, 0x70}, 1, 0},
308 {{0x71, 0x81 - gspca_dev
->curr_mode
}, 0, 0},
309 {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY
},
310 {{0x95, 0x70}, 1, 0},
311 {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
312 {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY
},
313 {{0x95, 0x70}, 1, 0},
314 #define SPORTSCAM_DV15_CMD_SIZE 9
315 {{0x94, 0x02}, 0, 0},
316 {{0xde, 0x24}, 0, 0},
317 {{0x94, 0x02}, 0, 0},
318 {{0xdd, 0xf0}, 0, 0},
319 {{0x94, 0x02}, 0, 0},
320 {{0xe3, 0x2c}, 0, 0},
321 {{0x94, 0x02}, 0, 0},
322 {{0xe4, 0x00}, 0, 0},
323 {{0x94, 0x02}, 0, 0},
324 {{0xe5, 0x00}, 0, 0},
325 {{0x94, 0x02}, 0, 0},
326 {{0xe6, 0x2c}, 0, 0},
327 {{0x94, 0x03}, 0, 0},
332 /* Under Windows, USB spy shows that only the 9 first start
333 * commands are used for SPORTSCAM_DV15 webcam
335 if (sd
->type
== SPORTSCAM_DV15
)
336 start_commands_size
= SPORTSCAM_DV15_CMD_SIZE
;
338 start_commands_size
= ARRAY_SIZE(start_commands
);
340 for (i
= 0; i
< start_commands_size
; i
++) {
341 jlj_write2(gspca_dev
, start_commands
[i
].instruction
);
342 if (start_commands
[i
].delay
)
343 msleep(start_commands
[i
].delay
);
344 if (start_commands
[i
].ack_wanted
)
345 jlj_read1(gspca_dev
, response
);
347 setcamquality(gspca_dev
);
350 if (gspca_dev
->usb_err
< 0)
351 PDEBUG(D_ERR
, "Start streaming command failed");
352 return gspca_dev
->usb_err
;
355 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
358 struct sd
*sd
= (struct sd
*) gspca_dev
;
362 PDEBUG(D_STREAM
, "Got %d bytes out of %d for Block 0",
363 len
, JEILINJ_MAX_TRANSFER
);
364 if (len
!= JEILINJ_MAX_TRANSFER
) {
365 PDEBUG(D_PACK
, "bad length");
368 /* check if it's start of frame */
369 header_marker
= ((u32
*)data
)[0];
370 if (header_marker
== FRAME_START
) {
371 sd
->blocks_left
= data
[0x0a] - 1;
372 PDEBUG(D_STREAM
, "blocks_left = 0x%x", sd
->blocks_left
);
373 /* Start a new frame, and add the JPEG header, first thing */
374 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
375 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
376 /* Toss line 0 of data block 0, keep the rest. */
377 gspca_frame_add(gspca_dev
, INTER_PACKET
,
378 data
+ FRAME_HEADER_LEN
,
379 JEILINJ_MAX_TRANSFER
- FRAME_HEADER_LEN
);
380 } else if (sd
->blocks_left
> 0) {
381 PDEBUG(D_STREAM
, "%d blocks remaining for frame",
383 sd
->blocks_left
-= 1;
384 if (sd
->blocks_left
== 0)
385 packet_type
= LAST_PACKET
;
387 packet_type
= INTER_PACKET
;
388 gspca_frame_add(gspca_dev
, packet_type
,
389 data
, JEILINJ_MAX_TRANSFER
);
394 /* Discard data until a new frame starts. */
395 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
398 /* This function is called at probe time just before sd_init */
399 static int sd_config(struct gspca_dev
*gspca_dev
,
400 const struct usb_device_id
*id
)
402 struct cam
*cam
= &gspca_dev
->cam
;
403 struct sd
*dev
= (struct sd
*) gspca_dev
;
405 dev
->type
= id
->driver_info
;
406 gspca_dev
->cam
.ctrls
= dev
->ctrls
;
407 dev
->quality
= QUALITY_DEF
;
409 cam
->cam_mode
= jlj_mode
;
410 cam
->nmodes
= ARRAY_SIZE(jlj_mode
);
413 cam
->bulk_size
= JEILINJ_MAX_TRANSFER
;
417 static void sd_stopN(struct gspca_dev
*gspca_dev
)
421 static u8 stop_commands
[][2] = {
429 /* get the image remaining blocks */
430 usb_bulk_msg(gspca_dev
->dev
,
431 gspca_dev
->urb
[0]->pipe
,
432 gspca_dev
->urb
[0]->transfer_buffer
,
433 JEILINJ_MAX_TRANSFER
, NULL
,
434 JEILINJ_DATA_TIMEOUT
);
436 /* search for 0xff 0xd9 (EOF for JPEG) */
438 buf
= gspca_dev
->urb
[0]->transfer_buffer
;
439 while ((i
< (JEILINJ_MAX_TRANSFER
- 1)) &&
440 ((buf
[i
] != 0xff) || (buf
[i
+1] != 0xd9)))
443 if (i
!= (JEILINJ_MAX_TRANSFER
- 1))
444 /* last remaining block found */
448 for (i
= 0; i
< ARRAY_SIZE(stop_commands
); i
++)
449 jlj_write2(gspca_dev
, stop_commands
[i
]);
452 /* this function is called at probe and resume time */
453 static int sd_init(struct gspca_dev
*gspca_dev
)
455 return gspca_dev
->usb_err
;
458 /* Set up for getting frames. */
459 static int sd_start(struct gspca_dev
*gspca_dev
)
461 struct sd
*dev
= (struct sd
*) gspca_dev
;
463 /* create the JPEG header */
464 jpeg_define(dev
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
465 0x21); /* JPEG 422 */
466 jpeg_set_qual(dev
->jpeg_hdr
, dev
->quality
);
467 PDEBUG(D_STREAM
, "Start streaming at %dx%d",
468 gspca_dev
->height
, gspca_dev
->width
);
469 jlj_start(gspca_dev
);
470 return gspca_dev
->usb_err
;
473 /* Table of supported USB devices */
474 static const struct usb_device_id device_table
[] = {
475 {USB_DEVICE(0x0979, 0x0280), .driver_info
= SAKAR_57379
},
476 {USB_DEVICE(0x0979, 0x0270), .driver_info
= SPORTSCAM_DV15
},
480 MODULE_DEVICE_TABLE(usb
, device_table
);
482 static int sd_querymenu(struct gspca_dev
*gspca_dev
,
483 struct v4l2_querymenu
*menu
)
486 case V4L2_CID_POWER_LINE_FREQUENCY
:
487 switch (menu
->index
) {
488 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
489 strcpy((char *) menu
->name
, "disable");
491 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
492 strcpy((char *) menu
->name
, "50 Hz");
494 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
495 strcpy((char *) menu
->name
, "60 Hz");
503 static int sd_set_jcomp(struct gspca_dev
*gspca_dev
,
504 struct v4l2_jpegcompression
*jcomp
)
506 struct sd
*sd
= (struct sd
*) gspca_dev
;
508 if (jcomp
->quality
< QUALITY_MIN
)
509 sd
->quality
= QUALITY_MIN
;
510 else if (jcomp
->quality
> QUALITY_MAX
)
511 sd
->quality
= QUALITY_MAX
;
513 sd
->quality
= jcomp
->quality
;
514 if (gspca_dev
->streaming
) {
515 jpeg_set_qual(sd
->jpeg_hdr
, sd
->quality
);
516 setcamquality(gspca_dev
);
521 static int sd_get_jcomp(struct gspca_dev
*gspca_dev
,
522 struct v4l2_jpegcompression
*jcomp
)
524 struct sd
*sd
= (struct sd
*) gspca_dev
;
526 memset(jcomp
, 0, sizeof *jcomp
);
527 jcomp
->quality
= sd
->quality
;
528 jcomp
->jpeg_markers
= V4L2_JPEG_MARKER_DHT
529 | V4L2_JPEG_MARKER_DQT
;
534 /* sub-driver description */
535 static const struct sd_desc sd_desc_sakar_57379
= {
541 .pkt_scan
= sd_pkt_scan
,
544 /* sub-driver description */
545 static const struct sd_desc sd_desc_sportscam_dv15
= {
551 .pkt_scan
= sd_pkt_scan
,
553 .nctrls
= ARRAY_SIZE(sd_ctrls
),
554 .querymenu
= sd_querymenu
,
555 .get_jcomp
= sd_get_jcomp
,
556 .set_jcomp
= sd_set_jcomp
,
559 static const struct sd_desc
*sd_desc
[2] = {
560 &sd_desc_sakar_57379
,
561 &sd_desc_sportscam_dv15
564 /* -- device connect -- */
565 static int sd_probe(struct usb_interface
*intf
,
566 const struct usb_device_id
*id
)
568 return gspca_dev_probe(intf
, id
,
569 sd_desc
[id
->driver_info
],
574 static struct usb_driver sd_driver
= {
576 .id_table
= device_table
,
578 .disconnect
= gspca_disconnect
,
580 .suspend
= gspca_suspend
,
581 .resume
= gspca_resume
,
585 module_usb_driver(sd_driver
);