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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * History and Acknowledgments
24 * The original Linux driver for SQ905 based cameras was written by
25 * Marcell Lengyel and furter developed by many other contributers
26 * and is available from http://sourceforge.net/projects/sqcam/
28 * This driver takes advantage of the reverse engineering work done for
29 * that driver and for libgphoto2 but shares no code with them.
31 * This driver has used as a base the finepix driver and other gspca
32 * based drivers and may still contain code fragments taken from those
36 #define MODULE_NAME "sq905"
38 #include <linux/workqueue.h>
41 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, "
42 "Theodore Kilgore <kilgota@auburn.edu>");
43 MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
44 MODULE_LICENSE("GPL");
46 /* Default timeouts, in ms */
47 #define SQ905_CMD_TIMEOUT 500
48 #define SQ905_DATA_TIMEOUT 1000
50 /* Maximum transfer size to use. */
51 #define SQ905_MAX_TRANSFER 0x8000
52 #define FRAME_HEADER_LEN 64
54 /* The known modes, or registers. These go in the "value" slot. */
56 /* 00 is "none" obviously */
58 #define SQ905_BULK_READ 0x03 /* precedes any bulk read */
59 #define SQ905_COMMAND 0x06 /* precedes the command codes below */
60 #define SQ905_PING 0x07 /* when reading an "idling" command */
61 #define SQ905_READ_DONE 0xc0 /* ack bulk read completed */
63 /* Some command codes. These go in the "index" slot. */
65 #define SQ905_ID 0xf0 /* asks for model string */
66 #define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */
67 #define SQ905_DATA 0x30 /* accesses photo data, not used here */
68 #define SQ905_CLEAR 0xa0 /* clear everything */
69 #define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */
70 #define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */
71 /* note that the capture command also controls the output dimensions */
72 /* 0x60 -> 160x120, 0x61 -> 320x240 0x62 -> 640x480 depends on camera */
73 /* 0x62 is not correct, at least for some cams. Should be 0x63 ? */
75 /* Structure to hold all of our device specific stuff */
77 struct gspca_dev gspca_dev; /* !! must be the first item */
84 struct work_struct work_struct;
85 struct workqueue_struct *work_thread;
88 /* The driver only supports 320x240 so far. */
89 static struct v4l2_pix_format sq905_mode[1] = {
90 { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
92 .sizeimage = 320 * 240,
93 .colorspace = V4L2_COLORSPACE_SRGB,
100 struct v4l2_pix_format *min_mode;
105 #define SQ905_FLIP_HORIZ (1 << 0)
106 #define SQ905_FLIP_VERT (1 << 1)
108 /* Last entry is default if nothing else matches */
109 static struct cam_type cam_types[] = {
110 { 0x19010509, "PocketCam", &sq905_mode[0], 1, SQ905_FLIP_HORIZ },
111 { 0x32010509, "Magpix", &sq905_mode[0], 1, SQ905_FLIP_HORIZ },
112 { 0, "Default", &sq905_mode[0], 1, SQ905_FLIP_HORIZ | SQ905_FLIP_VERT }
116 * Send a command to the camera.
118 static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
122 gspca_dev->usb_buf[0] = '\0';
123 ret = usb_control_msg(gspca_dev->dev,
124 usb_sndctrlpipe(gspca_dev->dev, 0),
125 USB_REQ_SYNCH_FRAME, /* request */
126 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
127 SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
130 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)",
135 ret = usb_control_msg(gspca_dev->dev,
136 usb_sndctrlpipe(gspca_dev->dev, 0),
137 USB_REQ_SYNCH_FRAME, /* request */
138 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
139 SQ905_PING, 0, gspca_dev->usb_buf, 1,
142 PDEBUG(D_ERR, "%s: usb_control_msg failed 2 (%d)",
151 * Acknowledge the end of a frame - see warning on sq905_command.
153 static int sq905_ack_frame(struct gspca_dev *gspca_dev)
157 gspca_dev->usb_buf[0] = '\0';
158 ret = usb_control_msg(gspca_dev->dev,
159 usb_sndctrlpipe(gspca_dev->dev, 0),
160 USB_REQ_SYNCH_FRAME, /* request */
161 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
162 SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
165 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
173 * request and read a block of data - see warning on sq905_command.
176 sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size)
181 gspca_dev->usb_buf[0] = '\0';
182 ret = usb_control_msg(gspca_dev->dev,
183 usb_sndctrlpipe(gspca_dev->dev, 0),
184 USB_REQ_SYNCH_FRAME, /* request */
185 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
186 SQ905_BULK_READ, size, gspca_dev->usb_buf,
187 1, SQ905_CMD_TIMEOUT);
189 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
192 ret = usb_bulk_msg(gspca_dev->dev,
193 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
194 data, size, &act_len, SQ905_DATA_TIMEOUT);
196 /* successful, it returns 0, otherwise negative */
197 if (ret < 0 || act_len != size) {
198 PDEBUG(D_ERR, "bulk read fail (%d) len %d/%d",
205 /* This function is called as a workqueue function and runs whenever the camera
206 * is streaming data. Because it is a workqueue function it is allowed to sleep
207 * so we can use synchronous USB calls. To avoid possible collisions with other
208 * threads attempting to use the camera's USB interface we take the gspca
209 * usb_lock when performing USB operations. In practice the only thing we need
210 * to protect against is the usb_set_interface call that gspca makes during
211 * stream_off as the camera doesn't provide any controls that the user could try
214 static void sq905_dostream(struct work_struct *work)
216 struct sd *dev = container_of(work, struct sd, work_struct);
217 struct gspca_dev *gspca_dev = &dev->gspca_dev;
218 struct gspca_frame *frame;
219 int bytes_left; /* bytes remaining in current frame. */
220 int data_len; /* size to use for the next read. */
221 int header_read; /* true if we have already read the frame header. */
222 int discarding; /* true if we failed to get space for frame. */
228 buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
229 mutex_lock(&gspca_dev->usb_lock);
231 PDEBUG(D_ERR, "Couldn't allocate USB buffer");
235 while (gspca_dev->present && gspca_dev->streaming) {
236 /* Need a short delay to ensure streaming flag was set by
237 * gspca and to make sure gspca can grab the mutex. */
238 mutex_unlock(&gspca_dev->usb_lock);
241 /* request some data and then read it until we have
242 * a complete frame. */
243 bytes_left = sq905_mode[0].sizeimage + FRAME_HEADER_LEN;
247 while (bytes_left > 0) {
248 data_len = bytes_left > SQ905_MAX_TRANSFER ?
249 SQ905_MAX_TRANSFER : bytes_left;
250 mutex_lock(&gspca_dev->usb_lock);
251 if (!gspca_dev->present)
253 ret = sq905_read_data(gspca_dev, buffer, data_len);
256 mutex_unlock(&gspca_dev->usb_lock);
258 "Got %d bytes out of %d for frame",
259 data_len, bytes_left);
260 bytes_left -= data_len;
263 packet_type = FIRST_PACKET;
264 /* The first 64 bytes of each frame are
265 * a header full of FF 00 bytes */
266 data += FRAME_HEADER_LEN;
267 data_len -= FRAME_HEADER_LEN;
269 } else if (bytes_left == 0) {
270 packet_type = LAST_PACKET;
272 packet_type = INTER_PACKET;
274 frame = gspca_get_i_frame(gspca_dev);
275 if (frame && !discarding)
276 gspca_frame_add(gspca_dev, packet_type,
277 frame, data, data_len);
281 /* acknowledge the frame */
282 mutex_lock(&gspca_dev->usb_lock);
283 if (!gspca_dev->present)
285 ret = sq905_ack_frame(gspca_dev);
290 /* the usb_lock is already acquired */
291 if (gspca_dev->present)
292 sq905_command(gspca_dev, SQ905_CLEAR);
293 mutex_unlock(&gspca_dev->usb_lock);
297 /* This function is called at probe time just before sd_init */
298 static int sd_config(struct gspca_dev *gspca_dev,
299 const struct usb_device_id *id)
301 struct cam *cam = &gspca_dev->cam;
302 struct sd *dev = (struct sd *) gspca_dev;
304 cam->cam_mode = sq905_mode;
306 /* We don't use the buffer gspca allocates so make it small. */
309 INIT_WORK(&dev->work_struct, sq905_dostream);
314 /* called on streamoff with alt==0 and on disconnect */
315 /* the usb_lock is held at entry - restore on exit */
316 static void sd_stop0(struct gspca_dev *gspca_dev)
318 struct sd *dev = (struct sd *) gspca_dev;
320 /* wait for the work queue to terminate */
321 mutex_unlock(&gspca_dev->usb_lock);
322 /* This waits for sq905_dostream to finish */
323 destroy_workqueue(dev->work_thread);
324 dev->work_thread = NULL;
325 mutex_lock(&gspca_dev->usb_lock);
328 /* this function is called at probe and resume time */
329 static int sd_init(struct gspca_dev *gspca_dev)
331 struct sd *dev = (struct sd *) gspca_dev;
335 /* connect to the camera and read
336 * the model ID and process that and put it away.
338 ret = sq905_command(gspca_dev, SQ905_CLEAR);
341 ret = sq905_command(gspca_dev, SQ905_ID);
344 ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4);
347 /* usb_buf is allocated with kmalloc so is aligned. */
348 ident = le32_to_cpup((u32 *)gspca_dev->usb_buf);
349 ret = sq905_command(gspca_dev, SQ905_CLEAR);
353 while (dev->cam_type < ARRAY_SIZE(cam_types) - 1 &&
354 ident != cam_types[dev->cam_type].ident_word)
356 PDEBUG(D_CONF, "SQ905 camera %s, ID %08x detected",
357 cam_types[dev->cam_type].name, ident);
361 /* Set up for getting frames. */
362 static int sd_start(struct gspca_dev *gspca_dev)
364 struct sd *dev = (struct sd *) gspca_dev;
367 /* "Open the shutter" and set size, to start capture */
368 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
370 PDEBUG(D_ERR, "Start streaming command failed");
374 /* Start the workqueue function to do the streaming */
375 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
376 queue_work(dev->work_thread, &dev->work_struct);
381 /* Table of supported USB devices */
382 static const __devinitdata struct usb_device_id device_table[] = {
383 {USB_DEVICE(0x2770, 0x9120)},
387 MODULE_DEVICE_TABLE(usb, device_table);
389 /* sub-driver description */
390 static const struct sd_desc sd_desc = {
398 /* -- device connect -- */
399 static int sd_probe(struct usb_interface *intf,
400 const struct usb_device_id *id)
402 return gspca_dev_probe(intf, id,
408 static struct usb_driver sd_driver = {
410 .id_table = device_table,
412 .disconnect = gspca_disconnect,
414 .suspend = gspca_suspend,
415 .resume = gspca_resume,
419 /* -- module insert / remove -- */
420 static int __init sd_mod_init(void)
424 ret = usb_register(&sd_driver);
427 PDEBUG(D_PROBE, "registered");
431 static void __exit sd_mod_exit(void)
433 usb_deregister(&sd_driver);
434 PDEBUG(D_PROBE, "deregistered");
437 module_init(sd_mod_init);
438 module_exit(sd_mod_exit);