4 * Supports some Jeilin dual-mode cameras which use bulk transport and
5 * download raw JPEG data.
7 * Copyright (C) 2009 Theodore Kilgore
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define MODULE_NAME "jeilinj"
26 #include <linux/workqueue.h>
30 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
31 MODULE_DESCRIPTION("GSPCA/JEILINJ USB Camera Driver");
32 MODULE_LICENSE("GPL");
34 /* Default timeouts, in ms */
35 #define JEILINJ_CMD_TIMEOUT 500
36 #define JEILINJ_DATA_TIMEOUT 1000
38 /* Maximum transfer size to use. */
39 #define JEILINJ_MAX_TRANSFER 0x200
41 #define FRAME_HEADER_LEN 0x10
43 /* Structure to hold all of our device specific stuff */
45 struct gspca_dev gspca_dev
; /* !! must be the first item */
46 const struct v4l2_pix_format
*cap_mode
;
48 struct work_struct work_struct
;
49 struct workqueue_struct
*work_thread
;
50 u8 quality
; /* image quality */
51 u8 jpegqual
; /* webcam quality */
56 unsigned char instruction
[2];
57 unsigned char ack_wanted
;
60 /* AFAICT these cameras will only do 320x240. */
61 static struct v4l2_pix_format jlj_mode
[] = {
62 { 320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
64 .sizeimage
= 320 * 240,
65 .colorspace
= V4L2_COLORSPACE_JPEG
,
70 * cam uses endpoint 0x03 to send commands, 0x84 for read commands,
71 * and 0x82 for bulk transfer.
74 /* All commands are two bytes only */
75 static int jlj_write2(struct gspca_dev
*gspca_dev
, unsigned char *command
)
79 memcpy(gspca_dev
->usb_buf
, command
, 2);
80 retval
= usb_bulk_msg(gspca_dev
->dev
,
81 usb_sndbulkpipe(gspca_dev
->dev
, 3),
82 gspca_dev
->usb_buf
, 2, NULL
, 500);
84 PDEBUG(D_ERR
, "command write [%02x] error %d",
85 gspca_dev
->usb_buf
[0], retval
);
89 /* Responses are one byte only */
90 static int jlj_read1(struct gspca_dev
*gspca_dev
, unsigned char response
)
94 retval
= usb_bulk_msg(gspca_dev
->dev
,
95 usb_rcvbulkpipe(gspca_dev
->dev
, 0x84),
96 gspca_dev
->usb_buf
, 1, NULL
, 500);
97 response
= gspca_dev
->usb_buf
[0];
99 PDEBUG(D_ERR
, "read command [%02x] error %d",
100 gspca_dev
->usb_buf
[0], retval
);
104 static int jlj_start(struct gspca_dev
*gspca_dev
)
109 struct jlj_command start_commands
[] = {
138 for (i
= 0; i
< ARRAY_SIZE(start_commands
); i
++) {
139 retval
= jlj_write2(gspca_dev
, start_commands
[i
].instruction
);
142 if (start_commands
[i
].ack_wanted
)
143 retval
= jlj_read1(gspca_dev
, response
);
147 PDEBUG(D_ERR
, "jlj_start retval is %d", retval
);
151 static int jlj_stop(struct gspca_dev
*gspca_dev
)
155 struct jlj_command stop_commands
[] = {
161 for (i
= 0; i
< ARRAY_SIZE(stop_commands
); i
++) {
162 retval
= jlj_write2(gspca_dev
, stop_commands
[i
].instruction
);
169 /* This function is called as a workqueue function and runs whenever the camera
170 * is streaming data. Because it is a workqueue function it is allowed to sleep
171 * so we can use synchronous USB calls. To avoid possible collisions with other
172 * threads attempting to use the camera's USB interface the gspca usb_lock is
173 * used when performing the one USB control operation inside the workqueue,
174 * which tells the camera to close the stream. In practice the only thing
175 * which needs to be protected against is the usb_set_interface call that
176 * gspca makes during stream_off. Otherwise the camera doesn't provide any
177 * controls that the user could try to change.
180 static void jlj_dostream(struct work_struct
*work
)
182 struct sd
*dev
= container_of(work
, struct sd
, work_struct
);
183 struct gspca_dev
*gspca_dev
= &dev
->gspca_dev
;
184 int blocks_left
; /* 0x200-sized blocks remaining in current frame. */
191 buffer
= kmalloc(JEILINJ_MAX_TRANSFER
, GFP_KERNEL
| GFP_DMA
);
193 PDEBUG(D_ERR
, "Couldn't allocate USB buffer");
196 while (gspca_dev
->present
&& gspca_dev
->streaming
) {
198 * Now request data block 0. Line 0 reports the size
199 * to download, in blocks of size 0x200, and also tells the
200 * "actual" data size, in bytes, which seems best to ignore.
202 ret
= usb_bulk_msg(gspca_dev
->dev
,
203 usb_rcvbulkpipe(gspca_dev
->dev
, 0x82),
204 buffer
, JEILINJ_MAX_TRANSFER
, &act_len
,
205 JEILINJ_DATA_TIMEOUT
);
207 "Got %d bytes out of %d for Block 0",
208 act_len
, JEILINJ_MAX_TRANSFER
);
209 if (ret
< 0 || act_len
< FRAME_HEADER_LEN
)
211 size_in_blocks
= buffer
[0x0a];
212 blocks_left
= buffer
[0x0a] - 1;
213 PDEBUG(D_STREAM
, "blocks_left = 0x%x", blocks_left
);
215 /* Start a new frame, and add the JPEG header, first thing */
216 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
217 dev
->jpeg_hdr
, JPEG_HDR_SZ
);
218 /* Toss line 0 of data block 0, keep the rest. */
219 gspca_frame_add(gspca_dev
, INTER_PACKET
,
220 buffer
+ FRAME_HEADER_LEN
,
221 JEILINJ_MAX_TRANSFER
- FRAME_HEADER_LEN
);
223 while (blocks_left
> 0) {
224 if (!gspca_dev
->present
)
226 ret
= usb_bulk_msg(gspca_dev
->dev
,
227 usb_rcvbulkpipe(gspca_dev
->dev
, 0x82),
228 buffer
, JEILINJ_MAX_TRANSFER
, &act_len
,
229 JEILINJ_DATA_TIMEOUT
);
230 if (ret
< 0 || act_len
< JEILINJ_MAX_TRANSFER
)
233 "%d blocks remaining for frame", blocks_left
);
235 if (blocks_left
== 0)
236 packet_type
= LAST_PACKET
;
238 packet_type
= INTER_PACKET
;
239 gspca_frame_add(gspca_dev
, packet_type
,
240 buffer
, JEILINJ_MAX_TRANSFER
);
244 mutex_lock(&gspca_dev
->usb_lock
);
245 if (gspca_dev
->present
)
247 mutex_unlock(&gspca_dev
->usb_lock
);
251 /* This function is called at probe time just before sd_init */
252 static int sd_config(struct gspca_dev
*gspca_dev
,
253 const struct usb_device_id
*id
)
255 struct cam
*cam
= &gspca_dev
->cam
;
256 struct sd
*dev
= (struct sd
*) gspca_dev
;
261 "JEILINJ camera detected"
262 " (vid/pid 0x%04X:0x%04X)", id
->idVendor
, id
->idProduct
);
263 cam
->cam_mode
= jlj_mode
;
266 /* We don't use the buffer gspca allocates so make it small. */
268 INIT_WORK(&dev
->work_struct
, jlj_dostream
);
272 /* called on streamoff with alt==0 and on disconnect */
273 /* the usb_lock is held at entry - restore on exit */
274 static void sd_stop0(struct gspca_dev
*gspca_dev
)
276 struct sd
*dev
= (struct sd
*) gspca_dev
;
278 /* wait for the work queue to terminate */
279 mutex_unlock(&gspca_dev
->usb_lock
);
280 /* This waits for jlj_dostream to finish */
281 destroy_workqueue(dev
->work_thread
);
282 dev
->work_thread
= NULL
;
283 mutex_lock(&gspca_dev
->usb_lock
);
284 kfree(dev
->jpeg_hdr
);
287 /* this function is called at probe and resume time */
288 static int sd_init(struct gspca_dev
*gspca_dev
)
293 /* Set up for getting frames. */
294 static int sd_start(struct gspca_dev
*gspca_dev
)
296 struct sd
*dev
= (struct sd
*) gspca_dev
;
299 /* create the JPEG header */
300 dev
->jpeg_hdr
= kmalloc(JPEG_HDR_SZ
, GFP_KERNEL
);
301 if (dev
->jpeg_hdr
== NULL
)
303 jpeg_define(dev
->jpeg_hdr
, gspca_dev
->height
, gspca_dev
->width
,
304 0x21); /* JPEG 422 */
305 jpeg_set_qual(dev
->jpeg_hdr
, dev
->quality
);
306 PDEBUG(D_STREAM
, "Start streaming at 320x240");
307 ret
= jlj_start(gspca_dev
);
309 PDEBUG(D_ERR
, "Start streaming command failed");
312 /* Start the workqueue function to do the streaming */
313 dev
->work_thread
= create_singlethread_workqueue(MODULE_NAME
);
314 queue_work(dev
->work_thread
, &dev
->work_struct
);
319 /* Table of supported USB devices */
320 static const __devinitdata
struct usb_device_id device_table
[] = {
321 {USB_DEVICE(0x0979, 0x0280)},
325 MODULE_DEVICE_TABLE(usb
, device_table
);
327 /* sub-driver description */
328 static const struct sd_desc sd_desc
= {
336 /* -- device connect -- */
337 static int sd_probe(struct usb_interface
*intf
,
338 const struct usb_device_id
*id
)
340 return gspca_dev_probe(intf
, id
,
346 static struct usb_driver sd_driver
= {
348 .id_table
= device_table
,
350 .disconnect
= gspca_disconnect
,
352 .suspend
= gspca_suspend
,
353 .resume
= gspca_resume
,
357 /* -- module insert / remove -- */
358 static int __init
sd_mod_init(void)
362 ret
= usb_register(&sd_driver
);
365 PDEBUG(D_PROBE
, "registered");
369 static void __exit
sd_mod_exit(void)
371 usb_deregister(&sd_driver
);
372 PDEBUG(D_PROBE
, "deregistered");
375 module_init(sd_mod_init
);
376 module_exit(sd_mod_exit
);