]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/gspca/sq905.c
V4L/DVB (10827): Add support for GeoVision GV-800(S)
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / gspca / sq905.c
CommitLineData
27d35fc3
AB
1/*
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
63/* Some command codes. These go in the "index" slot. */
64
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 ? */
74
75/* Structure to hold all of our device specific stuff */
76struct sd {
77 struct gspca_dev gspca_dev; /* !! must be the first item */
78
79 u8 cam_type;
80
81 /*
82 * Driver stuff
83 */
84 struct work_struct work_struct;
85 struct workqueue_struct *work_thread;
86};
87
88/* The driver only supports 320x240 so far. */
89static struct v4l2_pix_format sq905_mode[1] = {
90 { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
91 .bytesperline = 320,
92 .sizeimage = 320 * 240,
93 .colorspace = V4L2_COLORSPACE_SRGB,
94 .priv = 0}
95};
96
97struct cam_type {
98 u32 ident_word;
99 char *name;
100 struct v4l2_pix_format *min_mode;
101 u8 num_modes;
102 u8 sensor_flags;
103};
104
105#define SQ905_FLIP_HORIZ (1 << 0)
106#define SQ905_FLIP_VERT (1 << 1)
107
108/* Last entry is default if nothing else matches */
109static 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 }
113};
114
115/*
116 * Send a command to the camera.
117 */
118static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
119{
120 int ret;
121
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,
128 SQ905_CMD_TIMEOUT);
129 if (ret < 0) {
130 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)",
131 __func__, ret);
132 return ret;
133 }
134
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,
140 SQ905_CMD_TIMEOUT);
141 if (ret < 0) {
142 PDEBUG(D_ERR, "%s: usb_control_msg failed 2 (%d)",
143 __func__, ret);
144 return ret;
145 }
146
147 return 0;
148}
149
150/*
151 * Acknowledge the end of a frame - see warning on sq905_command.
152 */
153static int sq905_ack_frame(struct gspca_dev *gspca_dev)
154{
155 int ret;
156
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,
163 SQ905_CMD_TIMEOUT);
164 if (ret < 0) {
165 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
166 return ret;
167 }
168
169 return 0;
170}
171
172/*
173 * request and read a block of data - see warning on sq905_command.
174 */
175static int
176sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size)
177{
178 int ret;
179 int act_len;
180
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);
188 if (ret < 0) {
189 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
190 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) {
198 PDEBUG(D_ERR, "bulk read fail (%d) len %d/%d",
199 ret, act_len, size);
200 return -EIO;
201 }
202 return 0;
203}
204
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
212 * to change.
213 */
214static void sq905_dostream(struct work_struct *work)
215{
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. */
223 int packet_type;
224 int ret;
225 u8 *data;
226 u8 *buffer;
227
228 buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
229 mutex_lock(&gspca_dev->usb_lock);
230 if (!buffer) {
231 PDEBUG(D_ERR, "Couldn't allocate USB buffer");
232 goto quit_stream;
233 }
234
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);
239 msleep(1);
240
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;
244 header_read = 0;
245 discarding = 0;
246
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)
252 goto quit_stream;
253 ret = sq905_read_data(gspca_dev, buffer, data_len);
254 if (ret < 0)
255 goto quit_stream;
256 mutex_unlock(&gspca_dev->usb_lock);
257 PDEBUG(D_STREAM,
258 "Got %d bytes out of %d for frame",
259 data_len, bytes_left);
260 bytes_left -= data_len;
261 data = buffer;
262 if (!header_read) {
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;
268 header_read = 1;
269 } else if (bytes_left == 0) {
270 packet_type = LAST_PACKET;
271 } else {
272 packet_type = INTER_PACKET;
273 }
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);
278 else
279 discarding = 1;
280 }
281 /* acknowledge the frame */
282 mutex_lock(&gspca_dev->usb_lock);
283 if (!gspca_dev->present)
284 goto quit_stream;
285 ret = sq905_ack_frame(gspca_dev);
286 if (ret < 0)
287 goto quit_stream;
288 }
289quit_stream:
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);
294 kfree(buffer);
295}
296
297/* This function is called at probe time just before sd_init */
298static int sd_config(struct gspca_dev *gspca_dev,
299 const struct usb_device_id *id)
300{
301 struct cam *cam = &gspca_dev->cam;
302 struct sd *dev = (struct sd *) gspca_dev;
303
304 cam->cam_mode = sq905_mode;
305 cam->nmodes = 1;
306 /* We don't use the buffer gspca allocates so make it small. */
307 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{
331 struct sd *dev = (struct sd *) gspca_dev;
332 u32 ident;
333 int ret;
334
335 /* connect to the camera and read
336 * the model ID and process that and put it away.
337 */
338 ret = sq905_command(gspca_dev, SQ905_CLEAR);
339 if (ret < 0)
340 return ret;
341 ret = sq905_command(gspca_dev, SQ905_ID);
342 if (ret < 0)
343 return ret;
344 ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4);
345 if (ret < 0)
346 return ret;
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);
350 if (ret < 0)
351 return ret;
352 dev->cam_type = 0;
353 while (dev->cam_type < ARRAY_SIZE(cam_types) - 1 &&
354 ident != cam_types[dev->cam_type].ident_word)
355 dev->cam_type++;
356 PDEBUG(D_CONF, "SQ905 camera %s, ID %08x detected",
357 cam_types[dev->cam_type].name, ident);
358 return 0;
359}
360
361/* Set up for getting frames. */
362static int sd_start(struct gspca_dev *gspca_dev)
363{
364 struct sd *dev = (struct sd *) gspca_dev;
365 int ret;
366
367 /* "Open the shutter" and set size, to start capture */
368 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
369 if (ret < 0) {
370 PDEBUG(D_ERR, "Start streaming command failed");
371 return ret;
372 }
373
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);
377
378 return 0;
379}
380
381/* Table of supported USB devices */
382static const __devinitdata struct usb_device_id device_table[] = {
383 {USB_DEVICE(0x2770, 0x9120)},
384 {}
385};
386
387MODULE_DEVICE_TABLE(usb, device_table);
388
389/* sub-driver description */
390static const struct sd_desc sd_desc = {
391 .name = MODULE_NAME,
392 .config = sd_config,
393 .init = sd_init,
394 .start = sd_start,
395 .stop0 = sd_stop0,
396};
397
398/* -- device connect -- */
399static int sd_probe(struct usb_interface *intf,
400 const struct usb_device_id *id)
401{
402 return gspca_dev_probe(intf, id,
403 &sd_desc,
404 sizeof(struct sd),
405 THIS_MODULE);
406}
407
408static struct usb_driver sd_driver = {
409 .name = MODULE_NAME,
410 .id_table = device_table,
411 .probe = sd_probe,
412 .disconnect = gspca_disconnect,
413#ifdef CONFIG_PM
414 .suspend = gspca_suspend,
415 .resume = gspca_resume,
416#endif
417};
418
419/* -- module insert / remove -- */
420static int __init sd_mod_init(void)
421{
422 int ret;
423
424 ret = usb_register(&sd_driver);
425 if (ret < 0)
426 return ret;
427 PDEBUG(D_PROBE, "registered");
428 return 0;
429}
430
431static void __exit sd_mod_exit(void)
432{
433 usb_deregister(&sd_driver);
434 PDEBUG(D_PROBE, "deregistered");
435}
436
437module_init(sd_mod_init);
438module_exit(sd_mod_exit);