blob: 1fcaca6a87f7f161352941ce5d7f4f95f11de64c [file] [log] [blame]
Adam Baker27d35fc2009-02-06 15:12:46 -03001/*
2 * SQ905 subdriver
3 *
4 * Copyright (C) 2008, 2009 Adam Baker and Theodore Kilgore
5 *
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
9 * any later version.
10 *
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.
15 *
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
19 */
20
21/*
22 * History and Acknowledgments
23 *
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/
27 *
28 * This driver takes advantage of the reverse engineering work done for
29 * that driver and for libgphoto2 but shares no code with them.
30 *
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
33 * drivers.
34 */
35
36#define MODULE_NAME "sq905"
37
38#include <linux/workqueue.h>
39#include "gspca.h"
40
41MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, "
42 "Theodore Kilgore <kilgota@auburn.edu>");
43MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
44MODULE_LICENSE("GPL");
45
46/* Default timeouts, in ms */
47#define SQ905_CMD_TIMEOUT 500
48#define SQ905_DATA_TIMEOUT 1000
49
50/* Maximum transfer size to use. */
51#define SQ905_MAX_TRANSFER 0x8000
52#define FRAME_HEADER_LEN 64
53
54/* The known modes, or registers. These go in the "value" slot. */
55
56/* 00 is "none" obviously */
57
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 */
62
Adam Baker3b275912009-03-03 20:20:47 -030063/* Any non-zero value in the bottom 2 bits of the 2nd byte of
64 * the ID appears to indicate the camera can do 640*480. If the
65 * LSB of that byte is set the image is just upside down, otherwise
66 * it is rotated 180 degrees. */
67#define SQ905_HIRES_MASK 0x00000300
68#define SQ905_ORIENTATION_MASK 0x00000100
69
Adam Baker27d35fc2009-02-06 15:12:46 -030070/* Some command codes. These go in the "index" slot. */
71
72#define SQ905_ID 0xf0 /* asks for model string */
73#define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */
74#define SQ905_DATA 0x30 /* accesses photo data, not used here */
75#define SQ905_CLEAR 0xa0 /* clear everything */
Adam Baker3b275912009-03-03 20:20:47 -030076#define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */
77#define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */
78#define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
Adam Baker27d35fc2009-02-06 15:12:46 -030079/* note that the capture command also controls the output dimensions */
Adam Baker27d35fc2009-02-06 15:12:46 -030080
81/* Structure to hold all of our device specific stuff */
82struct sd {
83 struct gspca_dev gspca_dev; /* !! must be the first item */
84
Adam Baker27d35fc2009-02-06 15:12:46 -030085 /*
86 * Driver stuff
87 */
88 struct work_struct work_struct;
89 struct workqueue_struct *work_thread;
90};
91
Adam Baker3b275912009-03-03 20:20:47 -030092static struct v4l2_pix_format sq905_mode[] = {
93 { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
94 .bytesperline = 160,
95 .sizeimage = 160 * 120,
96 .colorspace = V4L2_COLORSPACE_SRGB,
97 .priv = 0},
Adam Baker27d35fc2009-02-06 15:12:46 -030098 { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
99 .bytesperline = 320,
100 .sizeimage = 320 * 240,
101 .colorspace = V4L2_COLORSPACE_SRGB,
Adam Baker3b275912009-03-03 20:20:47 -0300102 .priv = 0},
103 { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
104 .bytesperline = 640,
105 .sizeimage = 640 * 480,
106 .colorspace = V4L2_COLORSPACE_SRGB,
Adam Baker27d35fc2009-02-06 15:12:46 -0300107 .priv = 0}
108};
109
Adam Baker27d35fc2009-02-06 15:12:46 -0300110/*
111 * Send a command to the camera.
112 */
113static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
114{
115 int ret;
116
117 gspca_dev->usb_buf[0] = '\0';
118 ret = usb_control_msg(gspca_dev->dev,
119 usb_sndctrlpipe(gspca_dev->dev, 0),
120 USB_REQ_SYNCH_FRAME, /* request */
121 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122 SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
123 SQ905_CMD_TIMEOUT);
124 if (ret < 0) {
125 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)",
126 __func__, ret);
127 return ret;
128 }
129
130 ret = usb_control_msg(gspca_dev->dev,
131 usb_sndctrlpipe(gspca_dev->dev, 0),
132 USB_REQ_SYNCH_FRAME, /* request */
133 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
134 SQ905_PING, 0, gspca_dev->usb_buf, 1,
135 SQ905_CMD_TIMEOUT);
136 if (ret < 0) {
137 PDEBUG(D_ERR, "%s: usb_control_msg failed 2 (%d)",
138 __func__, ret);
139 return ret;
140 }
141
142 return 0;
143}
144
145/*
146 * Acknowledge the end of a frame - see warning on sq905_command.
147 */
148static int sq905_ack_frame(struct gspca_dev *gspca_dev)
149{
150 int ret;
151
152 gspca_dev->usb_buf[0] = '\0';
153 ret = usb_control_msg(gspca_dev->dev,
154 usb_sndctrlpipe(gspca_dev->dev, 0),
155 USB_REQ_SYNCH_FRAME, /* request */
156 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
157 SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
158 SQ905_CMD_TIMEOUT);
159 if (ret < 0) {
160 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
161 return ret;
162 }
163
164 return 0;
165}
166
167/*
168 * request and read a block of data - see warning on sq905_command.
169 */
170static int
Hans de Goede85191102009-10-05 05:58:18 -0300171sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
Adam Baker27d35fc2009-02-06 15:12:46 -0300172{
173 int ret;
174 int act_len;
175
176 gspca_dev->usb_buf[0] = '\0';
Hans de Goede85191102009-10-05 05:58:18 -0300177 if (need_lock)
178 mutex_lock(&gspca_dev->usb_lock);
Adam Baker27d35fc2009-02-06 15:12:46 -0300179 ret = usb_control_msg(gspca_dev->dev,
180 usb_sndctrlpipe(gspca_dev->dev, 0),
181 USB_REQ_SYNCH_FRAME, /* request */
182 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
183 SQ905_BULK_READ, size, gspca_dev->usb_buf,
184 1, SQ905_CMD_TIMEOUT);
Hans de Goede85191102009-10-05 05:58:18 -0300185 if (need_lock)
186 mutex_unlock(&gspca_dev->usb_lock);
Adam Baker27d35fc2009-02-06 15:12:46 -0300187 if (ret < 0) {
188 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
189 return ret;
190 }
191 ret = usb_bulk_msg(gspca_dev->dev,
192 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
193 data, size, &act_len, SQ905_DATA_TIMEOUT);
194
195 /* successful, it returns 0, otherwise negative */
196 if (ret < 0 || act_len != size) {
197 PDEBUG(D_ERR, "bulk read fail (%d) len %d/%d",
198 ret, act_len, size);
199 return -EIO;
200 }
201 return 0;
202}
203
204/* This function is called as a workqueue function and runs whenever the camera
205 * is streaming data. Because it is a workqueue function it is allowed to sleep
206 * so we can use synchronous USB calls. To avoid possible collisions with other
207 * threads attempting to use the camera's USB interface we take the gspca
208 * usb_lock when performing USB operations. In practice the only thing we need
209 * to protect against is the usb_set_interface call that gspca makes during
210 * stream_off as the camera doesn't provide any controls that the user could try
211 * to change.
212 */
213static void sq905_dostream(struct work_struct *work)
214{
215 struct sd *dev = container_of(work, struct sd, work_struct);
216 struct gspca_dev *gspca_dev = &dev->gspca_dev;
Adam Baker27d35fc2009-02-06 15:12:46 -0300217 int bytes_left; /* bytes remaining in current frame. */
218 int data_len; /* size to use for the next read. */
219 int header_read; /* true if we have already read the frame header. */
Adam Baker27d35fc2009-02-06 15:12:46 -0300220 int packet_type;
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300221 int frame_sz;
Adam Baker27d35fc2009-02-06 15:12:46 -0300222 int ret;
223 u8 *data;
224 u8 *buffer;
225
226 buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
Adam Baker27d35fc2009-02-06 15:12:46 -0300227 if (!buffer) {
228 PDEBUG(D_ERR, "Couldn't allocate USB buffer");
229 goto quit_stream;
230 }
231
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300232 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
233 + FRAME_HEADER_LEN;
234
Adam Baker27d35fc2009-02-06 15:12:46 -0300235 while (gspca_dev->present && gspca_dev->streaming) {
Adam Baker27d35fc2009-02-06 15:12:46 -0300236 /* request some data and then read it until we have
237 * a complete frame. */
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300238 bytes_left = frame_sz;
Adam Baker27d35fc2009-02-06 15:12:46 -0300239 header_read = 0;
Adam Baker27d35fc2009-02-06 15:12:46 -0300240
Hans de Goede85191102009-10-05 05:58:18 -0300241 /* Note we do not check for gspca_dev->streaming here, as
242 we must finish reading an entire frame, otherwise the
243 next time we stream we start reading in the middle of a
244 frame. */
245 while (bytes_left > 0 && gspca_dev->present) {
Adam Baker27d35fc2009-02-06 15:12:46 -0300246 data_len = bytes_left > SQ905_MAX_TRANSFER ?
247 SQ905_MAX_TRANSFER : bytes_left;
Hans de Goede85191102009-10-05 05:58:18 -0300248 ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
Adam Baker27d35fc2009-02-06 15:12:46 -0300249 if (ret < 0)
250 goto quit_stream;
Hans de Goede85191102009-10-05 05:58:18 -0300251 PDEBUG(D_PACK,
Adam Baker27d35fc2009-02-06 15:12:46 -0300252 "Got %d bytes out of %d for frame",
253 data_len, bytes_left);
254 bytes_left -= data_len;
255 data = buffer;
256 if (!header_read) {
257 packet_type = FIRST_PACKET;
258 /* The first 64 bytes of each frame are
259 * a header full of FF 00 bytes */
260 data += FRAME_HEADER_LEN;
261 data_len -= FRAME_HEADER_LEN;
262 header_read = 1;
263 } else if (bytes_left == 0) {
264 packet_type = LAST_PACKET;
265 } else {
266 packet_type = INTER_PACKET;
267 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300268 gspca_frame_add(gspca_dev, packet_type,
269 data, data_len);
270 /* If entire frame fits in one packet we still
271 need to add a LAST_PACKET */
272 if (packet_type == FIRST_PACKET &&
273 bytes_left == 0)
274 gspca_frame_add(gspca_dev, LAST_PACKET,
275 NULL, 0);
Adam Baker27d35fc2009-02-06 15:12:46 -0300276 }
Hans de Goede85191102009-10-05 05:58:18 -0300277 if (gspca_dev->present) {
278 /* acknowledge the frame */
279 mutex_lock(&gspca_dev->usb_lock);
280 ret = sq905_ack_frame(gspca_dev);
281 mutex_unlock(&gspca_dev->usb_lock);
282 if (ret < 0)
283 goto quit_stream;
284 }
Adam Baker27d35fc2009-02-06 15:12:46 -0300285 }
286quit_stream:
Hans de Goede85191102009-10-05 05:58:18 -0300287 if (gspca_dev->present) {
288 mutex_lock(&gspca_dev->usb_lock);
Adam Baker27d35fc2009-02-06 15:12:46 -0300289 sq905_command(gspca_dev, SQ905_CLEAR);
Hans de Goede85191102009-10-05 05:58:18 -0300290 mutex_unlock(&gspca_dev->usb_lock);
291 }
Adam Baker27d35fc2009-02-06 15:12:46 -0300292 kfree(buffer);
293}
294
295/* This function is called at probe time just before sd_init */
296static int sd_config(struct gspca_dev *gspca_dev,
297 const struct usb_device_id *id)
298{
299 struct cam *cam = &gspca_dev->cam;
300 struct sd *dev = (struct sd *) gspca_dev;
301
Adam Baker27d35fc2009-02-06 15:12:46 -0300302 /* We don't use the buffer gspca allocates so make it small. */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300303 cam->bulk = 1;
Adam Baker27d35fc2009-02-06 15:12:46 -0300304 cam->bulk_size = 64;
305
306 INIT_WORK(&dev->work_struct, sq905_dostream);
307
308 return 0;
309}
310
311/* called on streamoff with alt==0 and on disconnect */
312/* the usb_lock is held at entry - restore on exit */
313static void sd_stop0(struct gspca_dev *gspca_dev)
314{
315 struct sd *dev = (struct sd *) gspca_dev;
316
317 /* wait for the work queue to terminate */
318 mutex_unlock(&gspca_dev->usb_lock);
319 /* This waits for sq905_dostream to finish */
320 destroy_workqueue(dev->work_thread);
321 dev->work_thread = NULL;
322 mutex_lock(&gspca_dev->usb_lock);
323}
324
325/* this function is called at probe and resume time */
326static int sd_init(struct gspca_dev *gspca_dev)
327{
Adam Baker27d35fc2009-02-06 15:12:46 -0300328 u32 ident;
329 int ret;
330
331 /* connect to the camera and read
332 * the model ID and process that and put it away.
333 */
334 ret = sq905_command(gspca_dev, SQ905_CLEAR);
335 if (ret < 0)
336 return ret;
337 ret = sq905_command(gspca_dev, SQ905_ID);
338 if (ret < 0)
339 return ret;
Hans de Goede85191102009-10-05 05:58:18 -0300340 ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
Adam Baker27d35fc2009-02-06 15:12:46 -0300341 if (ret < 0)
342 return ret;
Adam Baker3b275912009-03-03 20:20:47 -0300343 /* usb_buf is allocated with kmalloc so is aligned.
344 * Camera model number is the right way round if we assume this
345 * reverse engineered ID is supposed to be big endian. */
346 ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
Adam Baker27d35fc2009-02-06 15:12:46 -0300347 ret = sq905_command(gspca_dev, SQ905_CLEAR);
348 if (ret < 0)
349 return ret;
Adam Baker3b275912009-03-03 20:20:47 -0300350 PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
351 gspca_dev->cam.cam_mode = sq905_mode;
352 gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
353 if (!(ident & SQ905_HIRES_MASK))
354 gspca_dev->cam.nmodes--;
Adam Bakerdfa76fa2009-03-29 19:17:10 -0300355
356 if (ident & SQ905_ORIENTATION_MASK)
357 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
358 else
359 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
360 V4L2_IN_ST_HFLIP;
Adam Baker27d35fc2009-02-06 15:12:46 -0300361 return 0;
362}
363
364/* Set up for getting frames. */
365static int sd_start(struct gspca_dev *gspca_dev)
366{
367 struct sd *dev = (struct sd *) gspca_dev;
368 int ret;
369
370 /* "Open the shutter" and set size, to start capture */
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300371 switch (gspca_dev->curr_mode) {
372 default:
373/* case 2: */
Adam Baker3b275912009-03-03 20:20:47 -0300374 PDEBUG(D_STREAM, "Start streaming at high resolution");
Adam Baker3b275912009-03-03 20:20:47 -0300375 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
376 break;
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300377 case 1:
Adam Baker3b275912009-03-03 20:20:47 -0300378 PDEBUG(D_STREAM, "Start streaming at medium resolution");
Adam Baker3b275912009-03-03 20:20:47 -0300379 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
380 break;
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300381 case 0:
Adam Baker3b275912009-03-03 20:20:47 -0300382 PDEBUG(D_STREAM, "Start streaming at low resolution");
383 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
384 }
385
Adam Baker27d35fc2009-02-06 15:12:46 -0300386 if (ret < 0) {
387 PDEBUG(D_ERR, "Start streaming command failed");
388 return ret;
389 }
Adam Baker27d35fc2009-02-06 15:12:46 -0300390 /* Start the workqueue function to do the streaming */
391 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
392 queue_work(dev->work_thread, &dev->work_struct);
393
394 return 0;
395}
396
397/* Table of supported USB devices */
398static const __devinitdata struct usb_device_id device_table[] = {
399 {USB_DEVICE(0x2770, 0x9120)},
400 {}
401};
402
403MODULE_DEVICE_TABLE(usb, device_table);
404
405/* sub-driver description */
406static const struct sd_desc sd_desc = {
407 .name = MODULE_NAME,
408 .config = sd_config,
409 .init = sd_init,
410 .start = sd_start,
411 .stop0 = sd_stop0,
412};
413
414/* -- device connect -- */
415static int sd_probe(struct usb_interface *intf,
416 const struct usb_device_id *id)
417{
418 return gspca_dev_probe(intf, id,
419 &sd_desc,
420 sizeof(struct sd),
421 THIS_MODULE);
422}
423
424static struct usb_driver sd_driver = {
425 .name = MODULE_NAME,
426 .id_table = device_table,
427 .probe = sd_probe,
428 .disconnect = gspca_disconnect,
429#ifdef CONFIG_PM
430 .suspend = gspca_suspend,
431 .resume = gspca_resume,
432#endif
433};
434
435/* -- module insert / remove -- */
436static int __init sd_mod_init(void)
437{
438 int ret;
439
440 ret = usb_register(&sd_driver);
441 if (ret < 0)
442 return ret;
443 PDEBUG(D_PROBE, "registered");
444 return 0;
445}
446
447static void __exit sd_mod_exit(void)
448{
449 usb_deregister(&sd_driver);
450 PDEBUG(D_PROBE, "deregistered");
451}
452
453module_init(sd_mod_init);
454module_exit(sd_mod_exit);