4 * Copyright (C) 2008, 2009 Adam Baker and Theodore Kilgore
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 * History and Acknowledgments
20 * The original Linux driver for SQ905 based cameras was written by
21 * Marcell Lengyel and furter developed by many other contributors
22 * and is available from http://sourceforge.net/projects/sqcam/
24 * This driver takes advantage of the reverse engineering work done for
25 * that driver and for libgphoto2 but shares no code with them.
27 * This driver has used as a base the finepix driver and other gspca
28 * based drivers and may still contain code fragments taken from those
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #define MODULE_NAME "sq905"
36 #include <linux/workqueue.h>
37 #include <linux/slab.h>
40 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, Theodore Kilgore <kilgota@auburn.edu>");
41 MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
42 MODULE_LICENSE("GPL");
44 /* Default timeouts, in ms */
45 #define SQ905_CMD_TIMEOUT 500
46 #define SQ905_DATA_TIMEOUT 1000
48 /* Maximum transfer size to use. */
49 #define SQ905_MAX_TRANSFER 0x8000
50 #define FRAME_HEADER_LEN 64
52 /* The known modes, or registers. These go in the "value" slot. */
54 /* 00 is "none" obviously */
56 #define SQ905_BULK_READ 0x03 /* precedes any bulk read */
57 #define SQ905_COMMAND 0x06 /* precedes the command codes below */
58 #define SQ905_PING 0x07 /* when reading an "idling" command */
59 #define SQ905_READ_DONE 0xc0 /* ack bulk read completed */
61 /* Any non-zero value in the bottom 2 bits of the 2nd byte of
62 * the ID appears to indicate the camera can do 640*480. If the
63 * LSB of that byte is set the image is just upside down, otherwise
64 * it is rotated 180 degrees. */
65 #define SQ905_HIRES_MASK 0x00000300
66 #define SQ905_ORIENTATION_MASK 0x00000100
68 /* Some command codes. These go in the "index" slot. */
70 #define SQ905_ID 0xf0 /* asks for model string */
71 #define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */
72 #define SQ905_DATA 0x30 /* accesses photo data, not used here */
73 #define SQ905_CLEAR 0xa0 /* clear everything */
74 #define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */
75 #define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */
76 #define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
77 /* note that the capture command also controls the output dimensions */
79 /* Structure to hold all of our device specific stuff */
81 struct gspca_dev gspca_dev; /* !! must be the first item */
86 struct work_struct work_struct;
87 struct workqueue_struct *work_thread;
90 static struct v4l2_pix_format sq905_mode[] = {
91 { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
93 .sizeimage = 160 * 120,
94 .colorspace = V4L2_COLORSPACE_SRGB,
96 { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
98 .sizeimage = 320 * 240,
99 .colorspace = V4L2_COLORSPACE_SRGB,
101 { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
103 .sizeimage = 640 * 480,
104 .colorspace = V4L2_COLORSPACE_SRGB,
109 * Send a command to the camera.
111 static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
115 gspca_dev->usb_buf[0] = '\0';
116 ret = usb_control_msg(gspca_dev->dev,
117 usb_sndctrlpipe(gspca_dev->dev, 0),
118 USB_REQ_SYNCH_FRAME, /* request */
119 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
120 SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
123 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
127 ret = usb_control_msg(gspca_dev->dev,
128 usb_sndctrlpipe(gspca_dev->dev, 0),
129 USB_REQ_SYNCH_FRAME, /* request */
130 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 SQ905_PING, 0, gspca_dev->usb_buf, 1,
134 pr_err("%s: usb_control_msg failed 2 (%d)\n", __func__, ret);
142 * Acknowledge the end of a frame - see warning on sq905_command.
144 static int sq905_ack_frame(struct gspca_dev *gspca_dev)
148 gspca_dev->usb_buf[0] = '\0';
149 ret = usb_control_msg(gspca_dev->dev,
150 usb_sndctrlpipe(gspca_dev->dev, 0),
151 USB_REQ_SYNCH_FRAME, /* request */
152 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
153 SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
156 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
164 * request and read a block of data - see warning on sq905_command.
167 sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
172 gspca_dev->usb_buf[0] = '\0';
174 mutex_lock(&gspca_dev->usb_lock);
175 ret = usb_control_msg(gspca_dev->dev,
176 usb_sndctrlpipe(gspca_dev->dev, 0),
177 USB_REQ_SYNCH_FRAME, /* request */
178 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
179 SQ905_BULK_READ, size, gspca_dev->usb_buf,
180 1, SQ905_CMD_TIMEOUT);
182 mutex_unlock(&gspca_dev->usb_lock);
184 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
187 ret = usb_bulk_msg(gspca_dev->dev,
188 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
189 data, size, &act_len, SQ905_DATA_TIMEOUT);
191 /* successful, it returns 0, otherwise negative */
192 if (ret < 0 || act_len != size) {
193 pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size);
200 * This function is called as a workqueue function and runs whenever the camera
201 * is streaming data. Because it is a workqueue function it is allowed to sleep
202 * so we can use synchronous USB calls. To avoid possible collisions with other
203 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
204 * performing USB operations using it. In practice we don't really need this
205 * as the camera doesn't provide any controls.
207 static void sq905_dostream(struct work_struct *work)
209 struct sd *dev = container_of(work, struct sd, work_struct);
210 struct gspca_dev *gspca_dev = &dev->gspca_dev;
211 int bytes_left; /* bytes remaining in current frame. */
212 int data_len; /* size to use for the next read. */
213 int header_read; /* true if we have already read the frame header. */
220 buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
222 pr_err("Couldn't allocate USB buffer\n");
226 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
229 while (gspca_dev->present && gspca_dev->streaming) {
231 if (gspca_dev->frozen)
234 /* request some data and then read it until we have
235 * a complete frame. */
236 bytes_left = frame_sz;
239 /* Note we do not check for gspca_dev->streaming here, as
240 we must finish reading an entire frame, otherwise the
241 next time we stream we start reading in the middle of a
243 while (bytes_left > 0 && gspca_dev->present) {
244 data_len = bytes_left > SQ905_MAX_TRANSFER ?
245 SQ905_MAX_TRANSFER : bytes_left;
246 ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
250 "Got %d bytes out of %d for frame",
251 data_len, bytes_left);
252 bytes_left -= data_len;
255 packet_type = FIRST_PACKET;
256 /* The first 64 bytes of each frame are
257 * a header full of FF 00 bytes */
258 data += FRAME_HEADER_LEN;
259 data_len -= FRAME_HEADER_LEN;
261 } else if (bytes_left == 0) {
262 packet_type = LAST_PACKET;
264 packet_type = INTER_PACKET;
266 gspca_frame_add(gspca_dev, packet_type,
268 /* If entire frame fits in one packet we still
269 need to add a LAST_PACKET */
270 if (packet_type == FIRST_PACKET &&
272 gspca_frame_add(gspca_dev, LAST_PACKET,
275 if (gspca_dev->present) {
276 /* acknowledge the frame */
277 mutex_lock(&gspca_dev->usb_lock);
278 ret = sq905_ack_frame(gspca_dev);
279 mutex_unlock(&gspca_dev->usb_lock);
285 if (gspca_dev->present) {
286 mutex_lock(&gspca_dev->usb_lock);
287 sq905_command(gspca_dev, SQ905_CLEAR);
288 mutex_unlock(&gspca_dev->usb_lock);
293 /* This function is called at probe time just before sd_init */
294 static int sd_config(struct gspca_dev *gspca_dev,
295 const struct usb_device_id *id)
297 struct cam *cam = &gspca_dev->cam;
298 struct sd *dev = (struct sd *) gspca_dev;
300 /* We don't use the buffer gspca allocates so make it small. */
304 INIT_WORK(&dev->work_struct, sq905_dostream);
309 /* called on streamoff with alt==0 and on disconnect */
310 /* the usb_lock is held at entry - restore on exit */
311 static void sd_stop0(struct gspca_dev *gspca_dev)
313 struct sd *dev = (struct sd *) gspca_dev;
315 /* wait for the work queue to terminate */
316 mutex_unlock(&gspca_dev->usb_lock);
317 /* This waits for sq905_dostream to finish */
318 destroy_workqueue(dev->work_thread);
319 dev->work_thread = NULL;
320 mutex_lock(&gspca_dev->usb_lock);
323 /* this function is called at probe and resume time */
324 static int sd_init(struct gspca_dev *gspca_dev)
329 /* connect to the camera and read
330 * the model ID and process that and put it away.
332 ret = sq905_command(gspca_dev, SQ905_CLEAR);
335 ret = sq905_command(gspca_dev, SQ905_ID);
338 ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
341 /* usb_buf is allocated with kmalloc so is aligned.
342 * Camera model number is the right way round if we assume this
343 * reverse engineered ID is supposed to be big endian. */
344 ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
345 ret = sq905_command(gspca_dev, SQ905_CLEAR);
348 PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
349 gspca_dev->cam.cam_mode = sq905_mode;
350 gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
351 if (!(ident & SQ905_HIRES_MASK))
352 gspca_dev->cam.nmodes--;
354 if (ident & SQ905_ORIENTATION_MASK)
355 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
357 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
362 /* Set up for getting frames. */
363 static int sd_start(struct gspca_dev *gspca_dev)
365 struct sd *dev = (struct sd *) gspca_dev;
368 /* "Open the shutter" and set size, to start capture */
369 switch (gspca_dev->curr_mode) {
372 PDEBUG(D_STREAM, "Start streaming at high resolution");
373 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
376 PDEBUG(D_STREAM, "Start streaming at medium resolution");
377 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
380 PDEBUG(D_STREAM, "Start streaming at low resolution");
381 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
385 PERR("Start streaming command failed");
388 /* Start the workqueue function to do the streaming */
389 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
390 queue_work(dev->work_thread, &dev->work_struct);
395 /* Table of supported USB devices */
396 static const struct usb_device_id device_table[] = {
397 {USB_DEVICE(0x2770, 0x9120)},
401 MODULE_DEVICE_TABLE(usb, device_table);
403 /* sub-driver description */
404 static const struct sd_desc sd_desc = {
412 /* -- device connect -- */
413 static int sd_probe(struct usb_interface *intf,
414 const struct usb_device_id *id)
416 return gspca_dev_probe(intf, id,
422 static struct usb_driver sd_driver = {
424 .id_table = device_table,
426 .disconnect = gspca_disconnect,
428 .suspend = gspca_suspend,
429 .resume = gspca_resume,
430 .reset_resume = gspca_resume,
434 module_usb_driver(sd_driver);