1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Supports some Jeilin dual-mode cameras which use bulk transport and
6 * download raw JPEG data.
8 * Copyright (C) 2009 Theodore Kilgore
10 * Sportscam DV15 support and control settings are
11 * Copyright (C) 2011 Patrice Chotard
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #define MODULE_NAME "jeilinj"
18 #include <linux/slab.h>
22 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
23 MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
24 MODULE_LICENSE("GPL");
26 /* Default timeouts, in ms */
27 #define JEILINJ_CMD_TIMEOUT 500
28 #define JEILINJ_CMD_DELAY 160
29 #define JEILINJ_DATA_TIMEOUT 1000
31 /* Maximum transfer size to use. */
32 #define JEILINJ_MAX_TRANSFER 0x200
33 #define FRAME_HEADER_LEN 0x10
34 #define FRAME_START 0xFFFFFFFF
41 #define CAMQUALITY_MIN 0 /* highest cam quality */
42 #define CAMQUALITY_MAX 97 /* lowest cam quality */
44 /* Structure to hold all of our device specific stuff */
46 struct gspca_dev gspca_dev
; /* !! must be the first item */
48 const struct v4l2_pix_format
*cap_mode
;
49 struct v4l2_ctrl
*freq
;
50 struct v4l2_ctrl
*jpegqual
;
53 u8 quality
; /* image quality */
54 #define QUALITY_MIN 35
55 #define QUALITY_MAX 85
56 #define QUALITY_DEF 85
57 u8 jpeg_hdr
[JPEG_HDR_SZ
];
61 unsigned char instruction
[2];
62 unsigned char ack_wanted
;
66 /* AFAICT these cameras will only do 320x240. */
67 static struct v4l2_pix_format jlj_mode
[] = {
68 { 320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
70 .sizeimage
= 320 * 240,
71 .colorspace
= V4L2_COLORSPACE_JPEG
,
73 { 640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
75 .sizeimage
= 640 * 480,
76 .colorspace
= V4L2_COLORSPACE_JPEG
,
81 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
82 * and 0x82 for bulk transfer.
85 /* All commands are two bytes only */
86 static void jlj_write2(struct gspca_dev
*gspca_dev
, unsigned char *command
)
90 if (gspca_dev
->usb_err
< 0)
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);
97 pr_err("command write [%02x] error %d\n",
98 gspca_dev
->usb_buf
[0], retval
);
99 gspca_dev
->usb_err
= retval
;
103 /* Responses are one byte only */
104 static void jlj_read1(struct gspca_dev
*gspca_dev
, unsigned char *response
)
108 if (gspca_dev
->usb_err
< 0)
110 retval
= usb_bulk_msg(gspca_dev
->dev
,
111 usb_rcvbulkpipe(gspca_dev
->dev
, 0x84),
112 gspca_dev
->usb_buf
, 1, NULL
, 500);
113 *response
= gspca_dev
->usb_buf
[0];
115 pr_err("read command [%02x] error %d\n",
116 gspca_dev
->usb_buf
[0], retval
);
117 gspca_dev
->usb_err
= retval
;
121 static void setfreq(struct gspca_dev
*gspca_dev
, s32 val
)
123 u8 freq_commands
[][2] = {
128 freq_commands
[0][1] |= val
>> 1;
130 jlj_write2(gspca_dev
, freq_commands
[0]);
131 jlj_write2(gspca_dev
, freq_commands
[1]);
134 static void setcamquality(struct gspca_dev
*gspca_dev
, s32 val
)
136 u8 quality_commands
[][2] = {
142 /* adapt camera quality from jpeg quality */
143 camquality
= ((QUALITY_MAX
- val
) * CAMQUALITY_MAX
)
144 / (QUALITY_MAX
- QUALITY_MIN
);
145 quality_commands
[0][1] += camquality
;
147 jlj_write2(gspca_dev
, quality_commands
[0]);
148 jlj_write2(gspca_dev
, quality_commands
[1]);
151 static void setautogain(struct gspca_dev
*gspca_dev
, s32 val
)
153 u8 autogain_commands
[][2] = {
158 autogain_commands
[1][1] = val
<< 4;
160 jlj_write2(gspca_dev
, autogain_commands
[0]);
161 jlj_write2(gspca_dev
, autogain_commands
[1]);
164 static void setred(struct gspca_dev
*gspca_dev
, s32 val
)
166 u8 setred_commands
[][2] = {
171 setred_commands
[1][1] = val
;
173 jlj_write2(gspca_dev
, setred_commands
[0]);
174 jlj_write2(gspca_dev
, setred_commands
[1]);
177 static void setgreen(struct gspca_dev
*gspca_dev
, s32 val
)
179 u8 setgreen_commands
[][2] = {
184 setgreen_commands
[1][1] = val
;
186 jlj_write2(gspca_dev
, setgreen_commands
[0]);
187 jlj_write2(gspca_dev
, setgreen_commands
[1]);
190 static void setblue(struct gspca_dev
*gspca_dev
, s32 val
)
192 u8 setblue_commands
[][2] = {
197 setblue_commands
[1][1] = val
;
199 jlj_write2(gspca_dev
, setblue_commands
[0]);
200 jlj_write2(gspca_dev
, setblue_commands
[1]);
203 static int jlj_start(struct gspca_dev
*gspca_dev
)
206 int start_commands_size
;
208 struct sd
*sd
= (struct sd
*) gspca_dev
;
209 struct jlj_command start_commands
[] = {
210 {{0x71, 0x81}, 0, 0},
211 {{0x70, 0x05}, 0, JEILINJ_CMD_DELAY
},
212 {{0x95, 0x70}, 1, 0},
213 {{0x71, 0x81 - gspca_dev
->curr_mode
}, 0, 0},
214 {{0x70, 0x04}, 0, JEILINJ_CMD_DELAY
},
215 {{0x95, 0x70}, 1, 0},
216 {{0x71, 0x00}, 0, 0}, /* start streaming ??*/
217 {{0x70, 0x08}, 0, JEILINJ_CMD_DELAY
},
218 {{0x95, 0x70}, 1, 0},
219 #define SPORTSCAM_DV15_CMD_SIZE 9
220 {{0x94, 0x02}, 0, 0},
221 {{0xde, 0x24}, 0, 0},
222 {{0x94, 0x02}, 0, 0},
223 {{0xdd, 0xf0}, 0, 0},
224 {{0x94, 0x02}, 0, 0},
225 {{0xe3, 0x2c}, 0, 0},
226 {{0x94, 0x02}, 0, 0},
227 {{0xe4, 0x00}, 0, 0},
228 {{0x94, 0x02}, 0, 0},
229 {{0xe5, 0x00}, 0, 0},
230 {{0x94, 0x02}, 0, 0},
231 {{0xe6, 0x2c}, 0, 0},
232 {{0x94, 0x03}, 0, 0},
237 /* Under Windows, USB spy shows that only the 9 first start
238 * commands are used for SPORTSCAM_DV15 webcam
240 if (sd
->type
== SPORTSCAM_DV15
)
241 start_commands_size
= SPORTSCAM_DV15_CMD_SIZE
;
243 start_commands_size
= ARRAY_SIZE(start_commands
);
245 for (i
= 0; i
< start_commands_size
; i
++) {
246 jlj_write2(gspca_dev
, start_commands
[i
].instruction
);
247 if (start_commands
[i
].delay
)
248 msleep(start_commands
[i
].delay
);
249 if (start_commands
[i
].ack_wanted
)
250 jlj_read1(gspca_dev
, &response
);
252 setcamquality(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->jpegqual
));
254 setfreq(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->freq
));
255 if (gspca_dev
->usb_err
< 0)
256 gspca_err(gspca_dev
, "Start streaming command failed\n");
257 return gspca_dev
->usb_err
;
260 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
263 struct sd
*sd
= (struct sd
*) gspca_dev
;
267 gspca_dbg(gspca_dev
, D_STREAM
, "Got %d bytes out of %d for Block 0\n",
268 len
, JEILINJ_MAX_TRANSFER
);
269 if (len
!= JEILINJ_MAX_TRANSFER
) {
270 gspca_dbg(gspca_dev
, D_PACK
, "bad length\n");
273 /* check if it's start of frame */
274 header_marker
= ((u32
*)data
)[0];
275 if (header_marker
== FRAME_START
) {
276 sd
->blocks_left
= data
[0x0a] - 1;
277 gspca_dbg(gspca_dev
, D_STREAM
, "blocks_left = 0x%x\n",
279 /* Start a new frame, and add the JPEG header, first thing */
280 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
281 sd
->jpeg_hdr
, JPEG_HDR_SZ
);
282 /* Toss line 0 of data block 0, keep the rest. */
283 gspca_frame_add(gspca_dev
, INTER_PACKET
,
284 data
+ FRAME_HEADER_LEN
,
285 JEILINJ_MAX_TRANSFER
- FRAME_HEADER_LEN
);
286 } else if (sd
->blocks_left
> 0) {
287 gspca_dbg(gspca_dev
, D_STREAM
, "%d blocks remaining for frame\n",
289 sd
->blocks_left
-= 1;
290 if (sd
->blocks_left
== 0)
291 packet_type
= LAST_PACKET
;
293 packet_type
= INTER_PACKET
;
294 gspca_frame_add(gspca_dev
, packet_type
,
295 data
, JEILINJ_MAX_TRANSFER
);
300 /* Discard data until a new frame starts. */
301 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
304 /* This function is called at probe time just before sd_init */
305 static int sd_config(struct gspca_dev
*gspca_dev
,
306 const struct usb_device_id
*id
)
308 struct cam
*cam
= &gspca_dev
->cam
;
309 struct sd
*dev
= (struct sd
*) gspca_dev
;
311 dev
->type
= id
->driver_info
;
312 dev
->quality
= QUALITY_DEF
;
314 cam
->cam_mode
= jlj_mode
;
315 cam
->nmodes
= ARRAY_SIZE(jlj_mode
);
318 cam
->bulk_size
= JEILINJ_MAX_TRANSFER
;
322 static void sd_stopN(struct gspca_dev
*gspca_dev
)
326 static u8 stop_commands
[][2] = {
334 /* get the image remaining blocks */
335 usb_bulk_msg(gspca_dev
->dev
,
336 gspca_dev
->urb
[0]->pipe
,
337 gspca_dev
->urb
[0]->transfer_buffer
,
338 JEILINJ_MAX_TRANSFER
, NULL
,
339 JEILINJ_DATA_TIMEOUT
);
341 /* search for 0xff 0xd9 (EOF for JPEG) */
343 buf
= gspca_dev
->urb
[0]->transfer_buffer
;
344 while ((i
< (JEILINJ_MAX_TRANSFER
- 1)) &&
345 ((buf
[i
] != 0xff) || (buf
[i
+1] != 0xd9)))
348 if (i
!= (JEILINJ_MAX_TRANSFER
- 1))
349 /* last remaining block found */
353 for (i
= 0; i
< ARRAY_SIZE(stop_commands
); i
++)
354 jlj_write2(gspca_dev
, stop_commands
[i
]);
357 /* this function is called at probe and resume time */
358 static int sd_init(struct gspca_dev
*gspca_dev
)
360 return gspca_dev
->usb_err
;
363 /* Set up for getting frames. */
364 static int sd_start(struct gspca_dev
*gspca_dev
)
366 struct sd
*dev
= (struct sd
*) gspca_dev
;
368 /* create the JPEG header */
369 jpeg_define(dev
->jpeg_hdr
, gspca_dev
->pixfmt
.height
,
370 gspca_dev
->pixfmt
.width
,
371 0x21); /* JPEG 422 */
372 jpeg_set_qual(dev
->jpeg_hdr
, dev
->quality
);
373 gspca_dbg(gspca_dev
, D_STREAM
, "Start streaming at %dx%d\n",
374 gspca_dev
->pixfmt
.height
, gspca_dev
->pixfmt
.width
);
375 jlj_start(gspca_dev
);
376 return gspca_dev
->usb_err
;
379 /* Table of supported USB devices */
380 static const struct usb_device_id device_table
[] = {
381 {USB_DEVICE(0x0979, 0x0280), .driver_info
= SAKAR_57379
},
382 {USB_DEVICE(0x0979, 0x0270), .driver_info
= SPORTSCAM_DV15
},
386 MODULE_DEVICE_TABLE(usb
, device_table
);
388 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
390 struct gspca_dev
*gspca_dev
=
391 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
392 struct sd
*sd
= (struct sd
*)gspca_dev
;
394 gspca_dev
->usb_err
= 0;
396 if (!gspca_dev
->streaming
)
400 case V4L2_CID_POWER_LINE_FREQUENCY
:
401 setfreq(gspca_dev
, ctrl
->val
);
403 case V4L2_CID_RED_BALANCE
:
404 setred(gspca_dev
, ctrl
->val
);
407 setgreen(gspca_dev
, ctrl
->val
);
409 case V4L2_CID_BLUE_BALANCE
:
410 setblue(gspca_dev
, ctrl
->val
);
412 case V4L2_CID_AUTOGAIN
:
413 setautogain(gspca_dev
, ctrl
->val
);
415 case V4L2_CID_JPEG_COMPRESSION_QUALITY
:
416 jpeg_set_qual(sd
->jpeg_hdr
, ctrl
->val
);
417 setcamquality(gspca_dev
, ctrl
->val
);
420 return gspca_dev
->usb_err
;
423 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
427 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
429 struct sd
*sd
= (struct sd
*)gspca_dev
;
430 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
431 static const struct v4l2_ctrl_config custom_autogain
= {
433 .id
= V4L2_CID_AUTOGAIN
,
434 .type
= V4L2_CTRL_TYPE_INTEGER
,
435 .name
= "Automatic Gain (and Exposure)",
441 gspca_dev
->vdev
.ctrl_handler
= hdl
;
442 v4l2_ctrl_handler_init(hdl
, 6);
443 sd
->freq
= v4l2_ctrl_new_std_menu(hdl
, &sd_ctrl_ops
,
444 V4L2_CID_POWER_LINE_FREQUENCY
,
445 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
, 1,
446 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
);
447 v4l2_ctrl_new_custom(hdl
, &custom_autogain
, NULL
);
448 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
449 V4L2_CID_RED_BALANCE
, 0, 3, 1, 2);
450 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
451 V4L2_CID_GAIN
, 0, 3, 1, 2);
452 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
453 V4L2_CID_BLUE_BALANCE
, 0, 3, 1, 2);
454 sd
->jpegqual
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
455 V4L2_CID_JPEG_COMPRESSION_QUALITY
,
456 QUALITY_MIN
, QUALITY_MAX
, 1, QUALITY_DEF
);
459 pr_err("Could not initialize controls\n");
465 static int sd_set_jcomp(struct gspca_dev
*gspca_dev
,
466 const struct v4l2_jpegcompression
*jcomp
)
468 struct sd
*sd
= (struct sd
*) gspca_dev
;
470 v4l2_ctrl_s_ctrl(sd
->jpegqual
, jcomp
->quality
);
474 static int sd_get_jcomp(struct gspca_dev
*gspca_dev
,
475 struct v4l2_jpegcompression
*jcomp
)
477 struct sd
*sd
= (struct sd
*) gspca_dev
;
479 memset(jcomp
, 0, sizeof *jcomp
);
480 jcomp
->quality
= v4l2_ctrl_g_ctrl(sd
->jpegqual
);
481 jcomp
->jpeg_markers
= V4L2_JPEG_MARKER_DHT
482 | V4L2_JPEG_MARKER_DQT
;
487 /* sub-driver description */
488 static const struct sd_desc sd_desc_sakar_57379
= {
494 .pkt_scan
= sd_pkt_scan
,
497 /* sub-driver description */
498 static const struct sd_desc sd_desc_sportscam_dv15
= {
502 .init_controls
= sd_init_controls
,
505 .pkt_scan
= sd_pkt_scan
,
506 .get_jcomp
= sd_get_jcomp
,
507 .set_jcomp
= sd_set_jcomp
,
510 static const struct sd_desc
*sd_desc
[2] = {
511 &sd_desc_sakar_57379
,
512 &sd_desc_sportscam_dv15
515 /* -- device connect -- */
516 static int sd_probe(struct usb_interface
*intf
,
517 const struct usb_device_id
*id
)
519 return gspca_dev_probe(intf
, id
,
520 sd_desc
[id
->driver_info
],
525 static struct usb_driver sd_driver
= {
527 .id_table
= device_table
,
529 .disconnect
= gspca_disconnect
,
531 .suspend
= gspca_suspend
,
532 .resume
= gspca_resume
,
533 .reset_resume
= gspca_resume
,
537 module_usb_driver(sd_driver
);