blob: 1d99f10a3e19962e4263acbdb630b8d6912165c8 [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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030025 * Marcell Lengyel and furter developed by many other contributors
Adam Baker27d35fc2009-02-06 15:12:46 -030026 * 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
Joe Perches133a9fe2011-08-21 19:56:57 -030036#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
Adam Baker27d35fc2009-02-06 15:12:46 -030038#define MODULE_NAME "sq905"
39
40#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Adam Baker27d35fc2009-02-06 15:12:46 -030042#include "gspca.h"
43
44MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, "
45 "Theodore Kilgore <kilgota@auburn.edu>");
46MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
47MODULE_LICENSE("GPL");
48
49/* Default timeouts, in ms */
50#define SQ905_CMD_TIMEOUT 500
51#define SQ905_DATA_TIMEOUT 1000
52
53/* Maximum transfer size to use. */
54#define SQ905_MAX_TRANSFER 0x8000
55#define FRAME_HEADER_LEN 64
56
57/* The known modes, or registers. These go in the "value" slot. */
58
59/* 00 is "none" obviously */
60
61#define SQ905_BULK_READ 0x03 /* precedes any bulk read */
62#define SQ905_COMMAND 0x06 /* precedes the command codes below */
63#define SQ905_PING 0x07 /* when reading an "idling" command */
64#define SQ905_READ_DONE 0xc0 /* ack bulk read completed */
65
Adam Baker3b275912009-03-03 20:20:47 -030066/* Any non-zero value in the bottom 2 bits of the 2nd byte of
67 * the ID appears to indicate the camera can do 640*480. If the
68 * LSB of that byte is set the image is just upside down, otherwise
69 * it is rotated 180 degrees. */
70#define SQ905_HIRES_MASK 0x00000300
71#define SQ905_ORIENTATION_MASK 0x00000100
72
Adam Baker27d35fc2009-02-06 15:12:46 -030073/* Some command codes. These go in the "index" slot. */
74
75#define SQ905_ID 0xf0 /* asks for model string */
76#define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */
77#define SQ905_DATA 0x30 /* accesses photo data, not used here */
78#define SQ905_CLEAR 0xa0 /* clear everything */
Adam Baker3b275912009-03-03 20:20:47 -030079#define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */
80#define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */
81#define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
Adam Baker27d35fc2009-02-06 15:12:46 -030082/* note that the capture command also controls the output dimensions */
Adam Baker27d35fc2009-02-06 15:12:46 -030083
84/* Structure to hold all of our device specific stuff */
85struct sd {
86 struct gspca_dev gspca_dev; /* !! must be the first item */
87
Adam Baker27d35fc2009-02-06 15:12:46 -030088 /*
89 * Driver stuff
90 */
91 struct work_struct work_struct;
92 struct workqueue_struct *work_thread;
93};
94
Adam Baker3b275912009-03-03 20:20:47 -030095static struct v4l2_pix_format sq905_mode[] = {
96 { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
97 .bytesperline = 160,
98 .sizeimage = 160 * 120,
99 .colorspace = V4L2_COLORSPACE_SRGB,
100 .priv = 0},
Adam Baker27d35fc2009-02-06 15:12:46 -0300101 { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
102 .bytesperline = 320,
103 .sizeimage = 320 * 240,
104 .colorspace = V4L2_COLORSPACE_SRGB,
Adam Baker3b275912009-03-03 20:20:47 -0300105 .priv = 0},
106 { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
107 .bytesperline = 640,
108 .sizeimage = 640 * 480,
109 .colorspace = V4L2_COLORSPACE_SRGB,
Adam Baker27d35fc2009-02-06 15:12:46 -0300110 .priv = 0}
111};
112
Adam Baker27d35fc2009-02-06 15:12:46 -0300113/*
114 * Send a command to the camera.
115 */
116static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
117{
118 int ret;
119
120 gspca_dev->usb_buf[0] = '\0';
121 ret = usb_control_msg(gspca_dev->dev,
122 usb_sndctrlpipe(gspca_dev->dev, 0),
123 USB_REQ_SYNCH_FRAME, /* request */
124 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
125 SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
126 SQ905_CMD_TIMEOUT);
127 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300128 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
Adam Baker27d35fc2009-02-06 15:12:46 -0300129 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) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300139 pr_err("%s: usb_control_msg failed 2 (%d)\n", __func__, ret);
Adam Baker27d35fc2009-02-06 15:12:46 -0300140 return ret;
141 }
142
143 return 0;
144}
145
146/*
147 * Acknowledge the end of a frame - see warning on sq905_command.
148 */
149static int sq905_ack_frame(struct gspca_dev *gspca_dev)
150{
151 int ret;
152
153 gspca_dev->usb_buf[0] = '\0';
154 ret = usb_control_msg(gspca_dev->dev,
155 usb_sndctrlpipe(gspca_dev->dev, 0),
156 USB_REQ_SYNCH_FRAME, /* request */
157 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
158 SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
159 SQ905_CMD_TIMEOUT);
160 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300161 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
Adam Baker27d35fc2009-02-06 15:12:46 -0300162 return ret;
163 }
164
165 return 0;
166}
167
168/*
169 * request and read a block of data - see warning on sq905_command.
170 */
171static int
Hans de Goede85191102009-10-05 05:58:18 -0300172sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
Adam Baker27d35fc2009-02-06 15:12:46 -0300173{
174 int ret;
175 int act_len;
176
177 gspca_dev->usb_buf[0] = '\0';
Hans de Goede85191102009-10-05 05:58:18 -0300178 if (need_lock)
179 mutex_lock(&gspca_dev->usb_lock);
Adam Baker27d35fc2009-02-06 15:12:46 -0300180 ret = usb_control_msg(gspca_dev->dev,
181 usb_sndctrlpipe(gspca_dev->dev, 0),
182 USB_REQ_SYNCH_FRAME, /* request */
183 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
184 SQ905_BULK_READ, size, gspca_dev->usb_buf,
185 1, SQ905_CMD_TIMEOUT);
Hans de Goede85191102009-10-05 05:58:18 -0300186 if (need_lock)
187 mutex_unlock(&gspca_dev->usb_lock);
Adam Baker27d35fc2009-02-06 15:12:46 -0300188 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300189 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
Adam Baker27d35fc2009-02-06 15:12:46 -0300190 return ret;
191 }
192 ret = usb_bulk_msg(gspca_dev->dev,
193 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
194 data, size, &act_len, SQ905_DATA_TIMEOUT);
195
196 /* successful, it returns 0, otherwise negative */
197 if (ret < 0 || act_len != size) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300198 pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size);
Adam Baker27d35fc2009-02-06 15:12:46 -0300199 return -EIO;
200 }
201 return 0;
202}
203
Hans de Goede844db452012-09-09 07:30:02 -0300204/*
205 * This function is called as a workqueue function and runs whenever the camera
Adam Baker27d35fc2009-02-06 15:12:46 -0300206 * 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
Hans de Goede844db452012-09-09 07:30:02 -0300208 * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
209 * performing USB operations using it. In practice we don't really need this
210 * as the camera doesn't provide any controls.
Adam Baker27d35fc2009-02-06 15:12:46 -0300211 */
212static void sq905_dostream(struct work_struct *work)
213{
214 struct sd *dev = container_of(work, struct sd, work_struct);
215 struct gspca_dev *gspca_dev = &dev->gspca_dev;
Adam Baker27d35fc2009-02-06 15:12:46 -0300216 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. */
Adam Baker27d35fc2009-02-06 15:12:46 -0300219 int packet_type;
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300220 int frame_sz;
Adam Baker27d35fc2009-02-06 15:12:46 -0300221 int ret;
222 u8 *data;
223 u8 *buffer;
224
225 buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
Adam Baker27d35fc2009-02-06 15:12:46 -0300226 if (!buffer) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300227 pr_err("Couldn't allocate USB buffer\n");
Adam Baker27d35fc2009-02-06 15:12:46 -0300228 goto quit_stream;
229 }
230
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300231 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
232 + FRAME_HEADER_LEN;
233
Hans de Goede345321d2012-09-09 06:30:02 -0300234 while (gspca_dev->present && gspca_dev->streaming) {
Hans Verkuil4ad34da2012-05-18 08:40:42 -0300235#ifdef CONFIG_PM
236 if (gspca_dev->frozen)
237 break;
238#endif
Adam Baker27d35fc2009-02-06 15:12:46 -0300239 /* request some data and then read it until we have
240 * a complete frame. */
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300241 bytes_left = frame_sz;
Adam Baker27d35fc2009-02-06 15:12:46 -0300242 header_read = 0;
Adam Baker27d35fc2009-02-06 15:12:46 -0300243
Hans de Goede85191102009-10-05 05:58:18 -0300244 /* Note we do not check for gspca_dev->streaming here, as
245 we must finish reading an entire frame, otherwise the
246 next time we stream we start reading in the middle of a
247 frame. */
Hans de Goede345321d2012-09-09 06:30:02 -0300248 while (bytes_left > 0 && gspca_dev->present) {
Adam Baker27d35fc2009-02-06 15:12:46 -0300249 data_len = bytes_left > SQ905_MAX_TRANSFER ?
250 SQ905_MAX_TRANSFER : bytes_left;
Hans de Goede85191102009-10-05 05:58:18 -0300251 ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
Adam Baker27d35fc2009-02-06 15:12:46 -0300252 if (ret < 0)
253 goto quit_stream;
Hans de Goede85191102009-10-05 05:58:18 -0300254 PDEBUG(D_PACK,
Adam Baker27d35fc2009-02-06 15:12:46 -0300255 "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 }
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300271 gspca_frame_add(gspca_dev, packet_type,
272 data, data_len);
273 /* If entire frame fits in one packet we still
274 need to add a LAST_PACKET */
275 if (packet_type == FIRST_PACKET &&
276 bytes_left == 0)
277 gspca_frame_add(gspca_dev, LAST_PACKET,
278 NULL, 0);
Adam Baker27d35fc2009-02-06 15:12:46 -0300279 }
Hans de Goede345321d2012-09-09 06:30:02 -0300280 if (gspca_dev->present) {
Hans de Goede85191102009-10-05 05:58:18 -0300281 /* acknowledge the frame */
282 mutex_lock(&gspca_dev->usb_lock);
283 ret = sq905_ack_frame(gspca_dev);
284 mutex_unlock(&gspca_dev->usb_lock);
285 if (ret < 0)
286 goto quit_stream;
287 }
Adam Baker27d35fc2009-02-06 15:12:46 -0300288 }
289quit_stream:
Hans de Goede345321d2012-09-09 06:30:02 -0300290 if (gspca_dev->present) {
Hans de Goede85191102009-10-05 05:58:18 -0300291 mutex_lock(&gspca_dev->usb_lock);
Adam Baker27d35fc2009-02-06 15:12:46 -0300292 sq905_command(gspca_dev, SQ905_CLEAR);
Hans de Goede85191102009-10-05 05:58:18 -0300293 mutex_unlock(&gspca_dev->usb_lock);
294 }
Adam Baker27d35fc2009-02-06 15:12:46 -0300295 kfree(buffer);
296}
297
298/* This function is called at probe time just before sd_init */
299static int sd_config(struct gspca_dev *gspca_dev,
300 const struct usb_device_id *id)
301{
302 struct cam *cam = &gspca_dev->cam;
303 struct sd *dev = (struct sd *) gspca_dev;
304
Adam Baker27d35fc2009-02-06 15:12:46 -0300305 /* We don't use the buffer gspca allocates so make it small. */
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300306 cam->bulk = 1;
Adam Baker27d35fc2009-02-06 15:12:46 -0300307 cam->bulk_size = 64;
308
309 INIT_WORK(&dev->work_struct, sq905_dostream);
310
311 return 0;
312}
313
314/* called on streamoff with alt==0 and on disconnect */
315/* the usb_lock is held at entry - restore on exit */
316static void sd_stop0(struct gspca_dev *gspca_dev)
317{
318 struct sd *dev = (struct sd *) gspca_dev;
319
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);
326}
327
328/* this function is called at probe and resume time */
329static int sd_init(struct gspca_dev *gspca_dev)
330{
Adam Baker27d35fc2009-02-06 15:12:46 -0300331 u32 ident;
332 int ret;
333
334 /* connect to the camera and read
335 * the model ID and process that and put it away.
336 */
337 ret = sq905_command(gspca_dev, SQ905_CLEAR);
338 if (ret < 0)
339 return ret;
340 ret = sq905_command(gspca_dev, SQ905_ID);
341 if (ret < 0)
342 return ret;
Hans de Goede85191102009-10-05 05:58:18 -0300343 ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
Adam Baker27d35fc2009-02-06 15:12:46 -0300344 if (ret < 0)
345 return ret;
Adam Baker3b275912009-03-03 20:20:47 -0300346 /* usb_buf is allocated with kmalloc so is aligned.
347 * Camera model number is the right way round if we assume this
348 * reverse engineered ID is supposed to be big endian. */
349 ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
Adam Baker27d35fc2009-02-06 15:12:46 -0300350 ret = sq905_command(gspca_dev, SQ905_CLEAR);
351 if (ret < 0)
352 return ret;
Adam Baker3b275912009-03-03 20:20:47 -0300353 PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
354 gspca_dev->cam.cam_mode = sq905_mode;
355 gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
356 if (!(ident & SQ905_HIRES_MASK))
357 gspca_dev->cam.nmodes--;
Adam Bakerdfa76fa2009-03-29 19:17:10 -0300358
359 if (ident & SQ905_ORIENTATION_MASK)
360 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
361 else
362 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
363 V4L2_IN_ST_HFLIP;
Adam Baker27d35fc2009-02-06 15:12:46 -0300364 return 0;
365}
366
367/* Set up for getting frames. */
368static int sd_start(struct gspca_dev *gspca_dev)
369{
370 struct sd *dev = (struct sd *) gspca_dev;
371 int ret;
372
373 /* "Open the shutter" and set size, to start capture */
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300374 switch (gspca_dev->curr_mode) {
375 default:
376/* case 2: */
Adam Baker3b275912009-03-03 20:20:47 -0300377 PDEBUG(D_STREAM, "Start streaming at high resolution");
Adam Baker3b275912009-03-03 20:20:47 -0300378 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
379 break;
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300380 case 1:
Adam Baker3b275912009-03-03 20:20:47 -0300381 PDEBUG(D_STREAM, "Start streaming at medium resolution");
Adam Baker3b275912009-03-03 20:20:47 -0300382 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
383 break;
Jean-Francois Moine67e45422009-03-26 05:03:13 -0300384 case 0:
Adam Baker3b275912009-03-03 20:20:47 -0300385 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 */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300401static const struct usb_device_id device_table[] = {
Adam Baker27d35fc2009-02-06 15:12:46 -0300402 {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,
Hans de Goede8bb58962012-06-30 06:44:47 -0300435 .reset_resume = gspca_resume,
Adam Baker27d35fc2009-02-06 15:12:46 -0300436#endif
437};
438
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800439module_usb_driver(sd_driver);