blob: b1d377e495c2a071188fd82e54c72dfae1ca09d3 [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 Baker3b275912009-03-03 20:20:47 -030085 const struct v4l2_pix_format *cap_mode;
Adam Baker27d35fc2009-02-06 15:12:46 -030086
87 /*
88 * Driver stuff
89 */
90 struct work_struct work_struct;
91 struct workqueue_struct *work_thread;
92};
93
Adam Baker3b275912009-03-03 20:20:47 -030094static struct v4l2_pix_format sq905_mode[] = {
95 { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
96 .bytesperline = 160,
97 .sizeimage = 160 * 120,
98 .colorspace = V4L2_COLORSPACE_SRGB,
99 .priv = 0},
Adam Baker27d35fc2009-02-06 15:12:46 -0300100 { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
101 .bytesperline = 320,
102 .sizeimage = 320 * 240,
103 .colorspace = V4L2_COLORSPACE_SRGB,
Adam Baker3b275912009-03-03 20:20:47 -0300104 .priv = 0},
105 { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
106 .bytesperline = 640,
107 .sizeimage = 640 * 480,
108 .colorspace = V4L2_COLORSPACE_SRGB,
Adam Baker27d35fc2009-02-06 15:12:46 -0300109 .priv = 0}
110};
111
Adam Baker27d35fc2009-02-06 15:12:46 -0300112/*
113 * Send a command to the camera.
114 */
115static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
116{
117 int ret;
118
119 gspca_dev->usb_buf[0] = '\0';
120 ret = usb_control_msg(gspca_dev->dev,
121 usb_sndctrlpipe(gspca_dev->dev, 0),
122 USB_REQ_SYNCH_FRAME, /* request */
123 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124 SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
125 SQ905_CMD_TIMEOUT);
126 if (ret < 0) {
127 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)",
128 __func__, ret);
129 return ret;
130 }
131
132 ret = usb_control_msg(gspca_dev->dev,
133 usb_sndctrlpipe(gspca_dev->dev, 0),
134 USB_REQ_SYNCH_FRAME, /* request */
135 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
136 SQ905_PING, 0, gspca_dev->usb_buf, 1,
137 SQ905_CMD_TIMEOUT);
138 if (ret < 0) {
139 PDEBUG(D_ERR, "%s: usb_control_msg failed 2 (%d)",
140 __func__, ret);
141 return ret;
142 }
143
144 return 0;
145}
146
147/*
148 * Acknowledge the end of a frame - see warning on sq905_command.
149 */
150static int sq905_ack_frame(struct gspca_dev *gspca_dev)
151{
152 int ret;
153
154 gspca_dev->usb_buf[0] = '\0';
155 ret = usb_control_msg(gspca_dev->dev,
156 usb_sndctrlpipe(gspca_dev->dev, 0),
157 USB_REQ_SYNCH_FRAME, /* request */
158 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
159 SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
160 SQ905_CMD_TIMEOUT);
161 if (ret < 0) {
162 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
163 return ret;
164 }
165
166 return 0;
167}
168
169/*
170 * request and read a block of data - see warning on sq905_command.
171 */
172static int
173sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size)
174{
175 int ret;
176 int act_len;
177
178 gspca_dev->usb_buf[0] = '\0';
179 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);
185 if (ret < 0) {
186 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
187 return ret;
188 }
189 ret = usb_bulk_msg(gspca_dev->dev,
190 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
191 data, size, &act_len, SQ905_DATA_TIMEOUT);
192
193 /* successful, it returns 0, otherwise negative */
194 if (ret < 0 || act_len != size) {
195 PDEBUG(D_ERR, "bulk read fail (%d) len %d/%d",
196 ret, act_len, size);
197 return -EIO;
198 }
199 return 0;
200}
201
202/* This function is called as a workqueue function and runs whenever the camera
203 * is streaming data. Because it is a workqueue function it is allowed to sleep
204 * so we can use synchronous USB calls. To avoid possible collisions with other
205 * threads attempting to use the camera's USB interface we take the gspca
206 * usb_lock when performing USB operations. In practice the only thing we need
207 * to protect against is the usb_set_interface call that gspca makes during
208 * stream_off as the camera doesn't provide any controls that the user could try
209 * to change.
210 */
211static void sq905_dostream(struct work_struct *work)
212{
213 struct sd *dev = container_of(work, struct sd, work_struct);
214 struct gspca_dev *gspca_dev = &dev->gspca_dev;
215 struct gspca_frame *frame;
216 int bytes_left; /* bytes remaining in current frame. */
217 int data_len; /* size to use for the next read. */
218 int header_read; /* true if we have already read the frame header. */
219 int discarding; /* true if we failed to get space for frame. */
220 int packet_type;
221 int ret;
222 u8 *data;
223 u8 *buffer;
224
225 buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
226 mutex_lock(&gspca_dev->usb_lock);
227 if (!buffer) {
228 PDEBUG(D_ERR, "Couldn't allocate USB buffer");
229 goto quit_stream;
230 }
231
232 while (gspca_dev->present && gspca_dev->streaming) {
233 /* Need a short delay to ensure streaming flag was set by
234 * gspca and to make sure gspca can grab the mutex. */
235 mutex_unlock(&gspca_dev->usb_lock);
236 msleep(1);
237
238 /* request some data and then read it until we have
239 * a complete frame. */
Adam Baker3b275912009-03-03 20:20:47 -0300240 bytes_left = dev->cap_mode->sizeimage + FRAME_HEADER_LEN;
Adam Baker27d35fc2009-02-06 15:12:46 -0300241 header_read = 0;
242 discarding = 0;
243
244 while (bytes_left > 0) {
245 data_len = bytes_left > SQ905_MAX_TRANSFER ?
246 SQ905_MAX_TRANSFER : bytes_left;
247 mutex_lock(&gspca_dev->usb_lock);
248 if (!gspca_dev->present)
249 goto quit_stream;
250 ret = sq905_read_data(gspca_dev, buffer, data_len);
251 if (ret < 0)
252 goto quit_stream;
253 mutex_unlock(&gspca_dev->usb_lock);
254 PDEBUG(D_STREAM,
255 "Got %d bytes out of %d for frame",
256 data_len, bytes_left);
257 bytes_left -= data_len;
258 data = buffer;
259 if (!header_read) {
260 packet_type = FIRST_PACKET;
261 /* The first 64 bytes of each frame are
262 * a header full of FF 00 bytes */
263 data += FRAME_HEADER_LEN;
264 data_len -= FRAME_HEADER_LEN;
265 header_read = 1;
266 } else if (bytes_left == 0) {
267 packet_type = LAST_PACKET;
268 } else {
269 packet_type = INTER_PACKET;
270 }
271 frame = gspca_get_i_frame(gspca_dev);
Adam Baker3b275912009-03-03 20:20:47 -0300272 if (frame && !discarding) {
Jean-Francois Moinef6b83322009-03-26 05:01:48 -0300273 frame = gspca_frame_add(gspca_dev, packet_type,
Adam Baker27d35fc2009-02-06 15:12:46 -0300274 frame, data, data_len);
Adam Baker3b275912009-03-03 20:20:47 -0300275 /* If entire frame fits in one packet we still
276 need to add a LAST_PACKET */
Jean-Francois Moinef6b83322009-03-26 05:01:48 -0300277 if (packet_type == FIRST_PACKET &&
278 bytes_left == 0)
279 frame = gspca_frame_add(gspca_dev,
280 LAST_PACKET,
Adam Baker3b275912009-03-03 20:20:47 -0300281 frame, data, 0);
282 } else {
Adam Baker27d35fc2009-02-06 15:12:46 -0300283 discarding = 1;
Adam Baker3b275912009-03-03 20:20:47 -0300284 }
Adam Baker27d35fc2009-02-06 15:12:46 -0300285 }
286 /* acknowledge the frame */
287 mutex_lock(&gspca_dev->usb_lock);
288 if (!gspca_dev->present)
289 goto quit_stream;
290 ret = sq905_ack_frame(gspca_dev);
291 if (ret < 0)
292 goto quit_stream;
293 }
294quit_stream:
295 /* the usb_lock is already acquired */
296 if (gspca_dev->present)
297 sq905_command(gspca_dev, SQ905_CLEAR);
298 mutex_unlock(&gspca_dev->usb_lock);
299 kfree(buffer);
300}
301
302/* This function is called at probe time just before sd_init */
303static int sd_config(struct gspca_dev *gspca_dev,
304 const struct usb_device_id *id)
305{
306 struct cam *cam = &gspca_dev->cam;
307 struct sd *dev = (struct sd *) gspca_dev;
308
Adam Baker27d35fc2009-02-06 15:12:46 -0300309 /* We don't use the buffer gspca allocates so make it small. */
310 cam->bulk_size = 64;
311
312 INIT_WORK(&dev->work_struct, sq905_dostream);
313
314 return 0;
315}
316
317/* called on streamoff with alt==0 and on disconnect */
318/* the usb_lock is held at entry - restore on exit */
319static void sd_stop0(struct gspca_dev *gspca_dev)
320{
321 struct sd *dev = (struct sd *) gspca_dev;
322
323 /* wait for the work queue to terminate */
324 mutex_unlock(&gspca_dev->usb_lock);
325 /* This waits for sq905_dostream to finish */
326 destroy_workqueue(dev->work_thread);
327 dev->work_thread = NULL;
328 mutex_lock(&gspca_dev->usb_lock);
329}
330
331/* this function is called at probe and resume time */
332static int sd_init(struct gspca_dev *gspca_dev)
333{
Adam Baker27d35fc2009-02-06 15:12:46 -0300334 u32 ident;
335 int ret;
336
337 /* connect to the camera and read
338 * the model ID and process that and put it away.
339 */
340 ret = sq905_command(gspca_dev, SQ905_CLEAR);
341 if (ret < 0)
342 return ret;
343 ret = sq905_command(gspca_dev, SQ905_ID);
344 if (ret < 0)
345 return ret;
346 ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4);
347 if (ret < 0)
348 return ret;
Adam Baker3b275912009-03-03 20:20:47 -0300349 /* usb_buf is allocated with kmalloc so is aligned.
350 * Camera model number is the right way round if we assume this
351 * reverse engineered ID is supposed to be big endian. */
352 ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
Adam Baker27d35fc2009-02-06 15:12:46 -0300353 ret = sq905_command(gspca_dev, SQ905_CLEAR);
354 if (ret < 0)
355 return ret;
Adam Baker3b275912009-03-03 20:20:47 -0300356 PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
357 gspca_dev->cam.cam_mode = sq905_mode;
358 gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
359 if (!(ident & SQ905_HIRES_MASK))
360 gspca_dev->cam.nmodes--;
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
Adam Baker3b275912009-03-03 20:20:47 -0300370 /* Set capture mode based on selected resolution. */
371 dev->cap_mode = gspca_dev->cam.cam_mode;
Adam Baker27d35fc2009-02-06 15:12:46 -0300372 /* "Open the shutter" and set size, to start capture */
Adam Baker3b275912009-03-03 20:20:47 -0300373 switch (gspca_dev->width) {
374 case 640:
375 PDEBUG(D_STREAM, "Start streaming at high resolution");
376 dev->cap_mode += 2;
377 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
378 break;
379 case 320:
380 PDEBUG(D_STREAM, "Start streaming at medium resolution");
381 dev->cap_mode++;
382 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
383 break;
384 default:
385 PDEBUG(D_STREAM, "Start streaming at low resolution");
386 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
387 }
388
Adam Baker27d35fc2009-02-06 15:12:46 -0300389 if (ret < 0) {
390 PDEBUG(D_ERR, "Start streaming command failed");
391 return ret;
392 }
Adam Baker27d35fc2009-02-06 15:12:46 -0300393 /* Start the workqueue function to do the streaming */
394 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
395 queue_work(dev->work_thread, &dev->work_struct);
396
397 return 0;
398}
399
400/* Table of supported USB devices */
401static const __devinitdata struct usb_device_id device_table[] = {
402 {USB_DEVICE(0x2770, 0x9120)},
403 {}
404};
405
406MODULE_DEVICE_TABLE(usb, device_table);
407
408/* sub-driver description */
409static const struct sd_desc sd_desc = {
410 .name = MODULE_NAME,
411 .config = sd_config,
412 .init = sd_init,
413 .start = sd_start,
414 .stop0 = sd_stop0,
415};
416
417/* -- device connect -- */
418static int sd_probe(struct usb_interface *intf,
419 const struct usb_device_id *id)
420{
421 return gspca_dev_probe(intf, id,
422 &sd_desc,
423 sizeof(struct sd),
424 THIS_MODULE);
425}
426
427static struct usb_driver sd_driver = {
428 .name = MODULE_NAME,
429 .id_table = device_table,
430 .probe = sd_probe,
431 .disconnect = gspca_disconnect,
432#ifdef CONFIG_PM
433 .suspend = gspca_suspend,
434 .resume = gspca_resume,
435#endif
436};
437
438/* -- module insert / remove -- */
439static int __init sd_mod_init(void)
440{
441 int ret;
442
443 ret = usb_register(&sd_driver);
444 if (ret < 0)
445 return ret;
446 PDEBUG(D_PROBE, "registered");
447 return 0;
448}
449
450static void __exit sd_mod_exit(void)
451{
452 usb_deregister(&sd_driver);
453 PDEBUG(D_PROBE, "deregistered");
454}
455
456module_init(sd_mod_init);
457module_exit(sd_mod_exit);