blob: d70b156872d6fd80e30c356557d473c19a7ff1ac [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
30#define MODULE_NAME "sq905c"
31
32#include <linux/workqueue.h>
33#include "gspca.h"
34
35MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
36MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
37MODULE_LICENSE("GPL");
38
39/* Default timeouts, in ms */
40#define SQ905C_CMD_TIMEOUT 500
41#define SQ905C_DATA_TIMEOUT 1000
42
43/* Maximum transfer size to use. */
44#define SQ905C_MAX_TRANSFER 0x8000
45
46#define FRAME_HEADER_LEN 0x50
47
48/* Commands. These go in the "value" slot. */
49#define SQ905C_CLEAR 0xa0 /* clear everything */
50#define SQ905C_CAPTURE_LOW 0xa040 /* Starts capture at 160x120 */
51#define SQ905C_CAPTURE_MED 0x1440 /* Starts capture at 320x240 */
52#define SQ905C_CAPTURE_HI 0x2840 /* Starts capture at 320x240 */
53
54/* For capture, this must go in the "index" slot. */
55#define SQ905C_CAPTURE_INDEX 0x110f
56
57/* Structure to hold all of our device specific stuff */
58struct sd {
59 struct gspca_dev gspca_dev; /* !! must be the first item */
60 const struct v4l2_pix_format *cap_mode;
61 /* Driver stuff */
62 struct work_struct work_struct;
63 struct workqueue_struct *work_thread;
64};
65
66/*
67 * Most of these cameras will do 640x480 and 320x240. 160x120 works
68 * in theory but gives very poor output. Therefore, not supported.
69 * The 0x2770:0x9050 cameras have max resolution of 320x240.
70 */
71static struct v4l2_pix_format sq905c_mode[] = {
72 { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
73 .bytesperline = 320,
74 .sizeimage = 320 * 240,
75 .colorspace = V4L2_COLORSPACE_SRGB,
76 .priv = 0},
77 { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
78 .bytesperline = 640,
79 .sizeimage = 640 * 480,
80 .colorspace = V4L2_COLORSPACE_SRGB,
81 .priv = 0}
82};
83
84/* Send a command to the camera. */
85static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
86{
87 int ret;
88
89 ret = usb_control_msg(gspca_dev->dev,
90 usb_sndctrlpipe(gspca_dev->dev, 0),
91 USB_REQ_SYNCH_FRAME, /* request */
92 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
93 command, index, NULL, 0,
94 SQ905C_CMD_TIMEOUT);
95 if (ret < 0) {
96 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)",
97 __func__, ret);
98 return ret;
99 }
100
101 return 0;
102}
103
104/* This function is called as a workqueue function and runs whenever the camera
105 * is streaming data. Because it is a workqueue function it is allowed to sleep
106 * so we can use synchronous USB calls. To avoid possible collisions with other
107 * threads attempting to use the camera's USB interface the gspca usb_lock is
108 * used when performing the one USB control operation inside the workqueue,
109 * which tells the camera to close the stream. In practice the only thing
110 * which needs to be protected against is the usb_set_interface call that
111 * gspca makes during stream_off. Otherwise the camera doesn't provide any
112 * controls that the user could try to change.
113 */
114static void sq905c_dostream(struct work_struct *work)
115{
116 struct sd *dev = container_of(work, struct sd, work_struct);
117 struct gspca_dev *gspca_dev = &dev->gspca_dev;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300118 int bytes_left; /* bytes remaining in current frame. */
119 int data_len; /* size to use for the next read. */
120 int act_len;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300121 int packet_type;
122 int ret;
123 u8 *buffer;
124
125 buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
126 if (!buffer) {
127 PDEBUG(D_ERR, "Couldn't allocate USB buffer");
128 goto quit_stream;
129 }
130
131 while (gspca_dev->present && gspca_dev->streaming) {
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300132 /* Request the header, which tells the size to download */
133 ret = usb_bulk_msg(gspca_dev->dev,
134 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
135 buffer, FRAME_HEADER_LEN, &act_len,
136 SQ905C_DATA_TIMEOUT);
137 PDEBUG(D_STREAM,
138 "Got %d bytes out of %d for header",
139 act_len, FRAME_HEADER_LEN);
140 if (ret < 0 || act_len < FRAME_HEADER_LEN)
141 goto quit_stream;
142 /* size is read from 4 bytes starting 0x40, little endian */
143 bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
144 |(buffer[0x43]<<24);
145 PDEBUG(D_STREAM, "bytes_left = 0x%x", bytes_left);
146 /* We keep the header. It has other information, too. */
147 packet_type = FIRST_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300148 gspca_frame_add(gspca_dev, packet_type,
149 buffer, FRAME_HEADER_LEN);
Hans de Goede20526012009-10-09 04:17:42 -0300150 while (bytes_left > 0 && gspca_dev->present) {
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300151 data_len = bytes_left > SQ905C_MAX_TRANSFER ?
152 SQ905C_MAX_TRANSFER : bytes_left;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300153 ret = usb_bulk_msg(gspca_dev->dev,
154 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
155 buffer, data_len, &act_len,
156 SQ905C_DATA_TIMEOUT);
157 if (ret < 0 || act_len < data_len)
158 goto quit_stream;
159 PDEBUG(D_STREAM,
160 "Got %d bytes out of %d for frame",
161 data_len, bytes_left);
162 bytes_left -= data_len;
163 if (bytes_left == 0)
164 packet_type = LAST_PACKET;
165 else
166 packet_type = INTER_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -0300167 gspca_frame_add(gspca_dev, packet_type,
168 buffer, data_len);
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300169 }
170 }
171quit_stream:
Hans de Goede20526012009-10-09 04:17:42 -0300172 if (gspca_dev->present) {
173 mutex_lock(&gspca_dev->usb_lock);
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300174 sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
Hans de Goede20526012009-10-09 04:17:42 -0300175 mutex_unlock(&gspca_dev->usb_lock);
176 }
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300177 kfree(buffer);
178}
179
180/* This function is called at probe time just before sd_init */
181static int sd_config(struct gspca_dev *gspca_dev,
182 const struct usb_device_id *id)
183{
184 struct cam *cam = &gspca_dev->cam;
185 struct sd *dev = (struct sd *) gspca_dev;
186
187 PDEBUG(D_PROBE,
188 "SQ9050 camera detected"
189 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
190 cam->cam_mode = sq905c_mode;
191 cam->nmodes = 2;
192 if (id->idProduct == 0x9050)
193 cam->nmodes = 1;
194 /* We don't use the buffer gspca allocates so make it small. */
195 cam->bulk_size = 32;
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300196 cam->bulk = 1;
Theodore Kilgore14a19c02009-03-25 07:13:13 -0300197 INIT_WORK(&dev->work_struct, sq905c_dostream);
198 return 0;
199}
200
201/* called on streamoff with alt==0 and on disconnect */
202/* the usb_lock is held at entry - restore on exit */
203static void sd_stop0(struct gspca_dev *gspca_dev)
204{
205 struct sd *dev = (struct sd *) gspca_dev;
206
207 /* wait for the work queue to terminate */
208 mutex_unlock(&gspca_dev->usb_lock);
209 /* This waits for sq905c_dostream to finish */
210 destroy_workqueue(dev->work_thread);
211 dev->work_thread = NULL;
212 mutex_lock(&gspca_dev->usb_lock);
213}
214
215/* this function is called at probe and resume time */
216static int sd_init(struct gspca_dev *gspca_dev)
217{
218 int ret;
219
220 /* connect to the camera and reset it. */
221 ret = sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
222 return ret;
223}
224
225/* Set up for getting frames. */
226static int sd_start(struct gspca_dev *gspca_dev)
227{
228 struct sd *dev = (struct sd *) gspca_dev;
229 int ret;
230
231 dev->cap_mode = gspca_dev->cam.cam_mode;
232 /* "Open the shutter" and set size, to start capture */
233 switch (gspca_dev->width) {
234 case 640:
235 PDEBUG(D_STREAM, "Start streaming at high resolution");
236 dev->cap_mode++;
237 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
238 SQ905C_CAPTURE_INDEX);
239 break;
240 default: /* 320 */
241 PDEBUG(D_STREAM, "Start streaming at medium resolution");
242 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
243 SQ905C_CAPTURE_INDEX);
244 }
245
246 if (ret < 0) {
247 PDEBUG(D_ERR, "Start streaming command failed");
248 return ret;
249 }
250 /* Start the workqueue function to do the streaming */
251 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
252 queue_work(dev->work_thread, &dev->work_struct);
253
254 return 0;
255}
256
257/* Table of supported USB devices */
258static const __devinitdata struct usb_device_id device_table[] = {
259 {USB_DEVICE(0x2770, 0x905c)},
260 {USB_DEVICE(0x2770, 0x9050)},
261 {USB_DEVICE(0x2770, 0x913d)},
262 {}
263};
264
265MODULE_DEVICE_TABLE(usb, device_table);
266
267/* sub-driver description */
268static const struct sd_desc sd_desc = {
269 .name = MODULE_NAME,
270 .config = sd_config,
271 .init = sd_init,
272 .start = sd_start,
273 .stop0 = sd_stop0,
274};
275
276/* -- device connect -- */
277static int sd_probe(struct usb_interface *intf,
278 const struct usb_device_id *id)
279{
280 return gspca_dev_probe(intf, id,
281 &sd_desc,
282 sizeof(struct sd),
283 THIS_MODULE);
284}
285
286static struct usb_driver sd_driver = {
287 .name = MODULE_NAME,
288 .id_table = device_table,
289 .probe = sd_probe,
290 .disconnect = gspca_disconnect,
291#ifdef CONFIG_PM
292 .suspend = gspca_suspend,
293 .resume = gspca_resume,
294#endif
295};
296
297/* -- module insert / remove -- */
298static int __init sd_mod_init(void)
299{
300 int ret;
301
302 ret = usb_register(&sd_driver);
303 if (ret < 0)
304 return ret;
305 PDEBUG(D_PROBE, "registered");
306 return 0;
307}
308
309static void __exit sd_mod_exit(void)
310{
311 usb_deregister(&sd_driver);
312 PDEBUG(D_PROBE, "deregistered");
313}
314
315module_init(sd_mod_init);
316module_exit(sd_mod_exit);