blob: f34ddb0570c86a7f41242fc6d4082d3e07890890 [file] [log] [blame]
Theodore Kilgore14a19c02009-03-25 07:13:13 -03001/*
2 * SQ905C subdriver
3 *
4 * Copyright (C) 2009 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 *
23 * This driver uses work done in
24 * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
25 *
26 * This driver has also used as a base the sq905c driver
27 * and may contain code fragments from it.
28 */
29
Joe Perches133a9fe2011-08-21 19:56:57 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Theodore Kilgore14a19c02009-03-25 07:13:13 -030032#define MODULE_NAME "sq905c"
33
34#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Theodore Kilgore14a19c02009-03-25 07:13:13 -030036#include "gspca.h"
37
38MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
39MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
40MODULE_LICENSE("GPL");
41
42/* Default timeouts, in ms */
43#define SQ905C_CMD_TIMEOUT 500
44#define SQ905C_DATA_TIMEOUT 1000
45
46/* Maximum transfer size to use. */
47#define SQ905C_MAX_TRANSFER 0x8000
48
49#define FRAME_HEADER_LEN 0x50
50
51/* Commands. These go in the "value" slot. */
52#define SQ905C_CLEAR 0xa0 /* clear everything */
Theodore Kilgoreed9885a2010-01-14 12:07:18 -030053#define SQ905C_GET_ID 0x14f4 /* Read version number */
Theodore Kilgore14a19c02009-03-25 07:13:13 -030054#define SQ905C_CAPTURE_LOW 0xa040 /* Starts capture at 160x120 */
55#define SQ905C_CAPTURE_MED 0x1440 /* Starts capture at 320x240 */
56#define SQ905C_CAPTURE_HI 0x2840 /* Starts capture at 320x240 */
57
58/* For capture, this must go in the "index" slot. */
59#define SQ905C_CAPTURE_INDEX 0x110f
60
61/* Structure to hold all of our device specific stuff */
62struct sd {
63 struct gspca_dev gspca_dev; /* !! must be the first item */
64 const struct v4l2_pix_format *cap_mode;
65 /* Driver stuff */
66 struct work_struct work_struct;
67 struct workqueue_struct *work_thread;
68};
69
70/*
71 * Most of these cameras will do 640x480 and 320x240. 160x120 works
72 * in theory but gives very poor output. Therefore, not supported.
73 * The 0x2770:0x9050 cameras have max resolution of 320x240.
74 */
75static struct v4l2_pix_format sq905c_mode[] = {
76 { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
77 .bytesperline = 320,
78 .sizeimage = 320 * 240,
79 .colorspace = V4L2_COLORSPACE_SRGB,
80 .priv = 0},
81 { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
82 .bytesperline = 640,
83 .sizeimage = 640 * 480,
84 .colorspace = V4L2_COLORSPACE_SRGB,
85 .priv = 0}
86};
87
88/* Send a command to the camera. */
89static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
90{
91 int ret;
92
93 ret = usb_control_msg(gspca_dev->dev,
94 usb_sndctrlpipe(gspca_dev->dev, 0),
95 USB_REQ_SYNCH_FRAME, /* request */
96 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
97 command, index, NULL, 0,
98 SQ905C_CMD_TIMEOUT);
99 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300100 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300101 return ret;
102 }
103
104 return 0;
105}
106
Theodore Kilgoreed9885a2010-01-14 12:07:18 -0300107static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,
108 int size)
109{
110 int ret;
111
112 ret = usb_control_msg(gspca_dev->dev,
113 usb_rcvctrlpipe(gspca_dev->dev, 0),
114 USB_REQ_SYNCH_FRAME, /* request */
115 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
116 command, index, gspca_dev->usb_buf, size,
117 SQ905C_CMD_TIMEOUT);
118 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300119 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
Theodore Kilgoreed9885a2010-01-14 12:07:18 -0300120 return ret;
121 }
122
123 return 0;
124}
125
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300126/* This function is called as a workqueue function and runs whenever the camera
127 * is streaming data. Because it is a workqueue function it is allowed to sleep
128 * so we can use synchronous USB calls. To avoid possible collisions with other
129 * threads attempting to use the camera's USB interface the gspca usb_lock is
130 * used when performing the one USB control operation inside the workqueue,
131 * which tells the camera to close the stream. In practice the only thing
132 * which needs to be protected against is the usb_set_interface call that
133 * gspca makes during stream_off. Otherwise the camera doesn't provide any
134 * controls that the user could try to change.
135 */
136static void sq905c_dostream(struct work_struct *work)
137{
138 struct sd *dev = container_of(work, struct sd, work_struct);
139 struct gspca_dev *gspca_dev = &dev->gspca_dev;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300140 int bytes_left; /* bytes remaining in current frame. */
141 int data_len; /* size to use for the next read. */
142 int act_len;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300143 int packet_type;
144 int ret;
145 u8 *buffer;
146
147 buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
148 if (!buffer) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300149 pr_err("Couldn't allocate USB buffer\n");
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300150 goto quit_stream;
151 }
152
Hans Verkuil4ad34da2012-05-18 08:40:42 -0300153 while (gspca_dev->dev && gspca_dev->streaming) {
154#ifdef CONFIG_PM
155 if (gspca_dev->frozen)
156 break;
157#endif
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300158 /* Request the header, which tells the size to download */
159 ret = usb_bulk_msg(gspca_dev->dev,
160 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
161 buffer, FRAME_HEADER_LEN, &act_len,
162 SQ905C_DATA_TIMEOUT);
163 PDEBUG(D_STREAM,
164 "Got %d bytes out of %d for header",
165 act_len, FRAME_HEADER_LEN);
166 if (ret < 0 || act_len < FRAME_HEADER_LEN)
167 goto quit_stream;
168 /* size is read from 4 bytes starting 0x40, little endian */
169 bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
170 |(buffer[0x43]<<24);
171 PDEBUG(D_STREAM, "bytes_left = 0x%x", bytes_left);
172 /* We keep the header. It has other information, too. */
173 packet_type = FIRST_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300174 gspca_frame_add(gspca_dev, packet_type,
175 buffer, FRAME_HEADER_LEN);
Hans Verkuil254902b2012-05-06 09:28:22 -0300176 while (bytes_left > 0 && gspca_dev->dev) {
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300177 data_len = bytes_left > SQ905C_MAX_TRANSFER ?
178 SQ905C_MAX_TRANSFER : bytes_left;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300179 ret = usb_bulk_msg(gspca_dev->dev,
180 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
181 buffer, data_len, &act_len,
182 SQ905C_DATA_TIMEOUT);
183 if (ret < 0 || act_len < data_len)
184 goto quit_stream;
185 PDEBUG(D_STREAM,
186 "Got %d bytes out of %d for frame",
187 data_len, bytes_left);
188 bytes_left -= data_len;
189 if (bytes_left == 0)
190 packet_type = LAST_PACKET;
191 else
192 packet_type = INTER_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300193 gspca_frame_add(gspca_dev, packet_type,
194 buffer, data_len);
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300195 }
196 }
197quit_stream:
Hans Verkuil254902b2012-05-06 09:28:22 -0300198 if (gspca_dev->dev) {
Hans de Goede20526012009-10-09 04:17:42 -0300199 mutex_lock(&gspca_dev->usb_lock);
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300200 sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
Hans de Goede20526012009-10-09 04:17:42 -0300201 mutex_unlock(&gspca_dev->usb_lock);
202 }
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300203 kfree(buffer);
204}
205
206/* This function is called at probe time just before sd_init */
207static int sd_config(struct gspca_dev *gspca_dev,
208 const struct usb_device_id *id)
209{
210 struct cam *cam = &gspca_dev->cam;
211 struct sd *dev = (struct sd *) gspca_dev;
Jean-Francois Moine914e8712010-01-17 03:58:51 -0300212 int ret;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300213
214 PDEBUG(D_PROBE,
215 "SQ9050 camera detected"
216 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
Theodore Kilgoreed9885a2010-01-14 12:07:18 -0300217
218 ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);
219 if (ret < 0) {
220 PDEBUG(D_ERR, "Get version command failed");
221 return ret;
222 }
223
224 ret = sq905c_read(gspca_dev, 0xf5, 0, 20);
225 if (ret < 0) {
226 PDEBUG(D_ERR, "Reading version command failed");
227 return ret;
228 }
229 /* Note we leave out the usb id and the manufacturing date */
230 PDEBUG(D_PROBE,
231 "SQ9050 ID string: %02x - %02x %02x %02x %02x %02x %02x",
232 gspca_dev->usb_buf[3],
233 gspca_dev->usb_buf[14], gspca_dev->usb_buf[15],
234 gspca_dev->usb_buf[16], gspca_dev->usb_buf[17],
235 gspca_dev->usb_buf[18], gspca_dev->usb_buf[19]);
236
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300237 cam->cam_mode = sq905c_mode;
238 cam->nmodes = 2;
Theodore Kilgoreed9885a2010-01-14 12:07:18 -0300239 if (gspca_dev->usb_buf[15] == 0)
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300240 cam->nmodes = 1;
241 /* We don't use the buffer gspca allocates so make it small. */
242 cam->bulk_size = 32;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300243 cam->bulk = 1;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300244 INIT_WORK(&dev->work_struct, sq905c_dostream);
245 return 0;
246}
247
248/* called on streamoff with alt==0 and on disconnect */
249/* the usb_lock is held at entry - restore on exit */
250static void sd_stop0(struct gspca_dev *gspca_dev)
251{
252 struct sd *dev = (struct sd *) gspca_dev;
253
254 /* wait for the work queue to terminate */
255 mutex_unlock(&gspca_dev->usb_lock);
256 /* This waits for sq905c_dostream to finish */
257 destroy_workqueue(dev->work_thread);
258 dev->work_thread = NULL;
259 mutex_lock(&gspca_dev->usb_lock);
260}
261
262/* this function is called at probe and resume time */
263static int sd_init(struct gspca_dev *gspca_dev)
264{
265 int ret;
266
267 /* connect to the camera and reset it. */
268 ret = sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
269 return ret;
270}
271
272/* Set up for getting frames. */
273static int sd_start(struct gspca_dev *gspca_dev)
274{
275 struct sd *dev = (struct sd *) gspca_dev;
276 int ret;
277
278 dev->cap_mode = gspca_dev->cam.cam_mode;
279 /* "Open the shutter" and set size, to start capture */
280 switch (gspca_dev->width) {
281 case 640:
282 PDEBUG(D_STREAM, "Start streaming at high resolution");
283 dev->cap_mode++;
284 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
285 SQ905C_CAPTURE_INDEX);
286 break;
287 default: /* 320 */
288 PDEBUG(D_STREAM, "Start streaming at medium resolution");
289 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
290 SQ905C_CAPTURE_INDEX);
291 }
292
293 if (ret < 0) {
294 PDEBUG(D_ERR, "Start streaming command failed");
295 return ret;
296 }
297 /* Start the workqueue function to do the streaming */
298 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
299 queue_work(dev->work_thread, &dev->work_struct);
300
301 return 0;
302}
303
304/* Table of supported USB devices */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300305static const struct usb_device_id device_table[] = {
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300306 {USB_DEVICE(0x2770, 0x905c)},
307 {USB_DEVICE(0x2770, 0x9050)},
Theodore Kilgore634b4772010-12-24 17:06:04 -0300308 {USB_DEVICE(0x2770, 0x9051)},
Hans de Goedeeb900692010-01-17 10:42:55 -0300309 {USB_DEVICE(0x2770, 0x9052)},
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300310 {USB_DEVICE(0x2770, 0x913d)},
311 {}
312};
313
314MODULE_DEVICE_TABLE(usb, device_table);
315
316/* sub-driver description */
317static const struct sd_desc sd_desc = {
318 .name = MODULE_NAME,
319 .config = sd_config,
320 .init = sd_init,
321 .start = sd_start,
322 .stop0 = sd_stop0,
323};
324
325/* -- device connect -- */
326static int sd_probe(struct usb_interface *intf,
327 const struct usb_device_id *id)
328{
329 return gspca_dev_probe(intf, id,
330 &sd_desc,
331 sizeof(struct sd),
332 THIS_MODULE);
333}
334
335static struct usb_driver sd_driver = {
336 .name = MODULE_NAME,
337 .id_table = device_table,
338 .probe = sd_probe,
339 .disconnect = gspca_disconnect,
340#ifdef CONFIG_PM
341 .suspend = gspca_suspend,
342 .resume = gspca_resume,
343#endif
344};
345
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800346module_usb_driver(sd_driver);