OMAP3 SRF: Generic shared resource f/w
[linux-ginger.git] / drivers / media / video / gspca / sq905c.c
blob9168925054328229f0c2ff7dad6d9a53381d2383
1 /*
2 * SQ905C subdriver
4 * Copyright (C) 2009 Theodore Kilgore
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.
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.
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
23 * This driver uses work done in
24 * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
26 * This driver has also used as a base the sq905c driver
27 * and may contain code fragments from it.
30 #define MODULE_NAME "sq905c"
32 #include <linux/workqueue.h>
33 #include "gspca.h"
35 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
36 MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
37 MODULE_LICENSE("GPL");
39 /* Default timeouts, in ms */
40 #define SQ905C_CMD_TIMEOUT 500
41 #define SQ905C_DATA_TIMEOUT 1000
43 /* Maximum transfer size to use. */
44 #define SQ905C_MAX_TRANSFER 0x8000
46 #define FRAME_HEADER_LEN 0x50
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 */
54 /* For capture, this must go in the "index" slot. */
55 #define SQ905C_CAPTURE_INDEX 0x110f
57 /* Structure to hold all of our device specific stuff */
58 struct 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;
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.
71 static 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}
84 /* Send a command to the camera. */
85 static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
87 int ret;
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;
101 return 0;
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.
114 static void sq905c_dostream(struct work_struct *work)
116 struct sd *dev = container_of(work, struct sd, work_struct);
117 struct gspca_dev *gspca_dev = &dev->gspca_dev;
118 struct gspca_frame *frame;
119 int bytes_left; /* bytes remaining in current frame. */
120 int data_len; /* size to use for the next read. */
121 int act_len;
122 int discarding = 0; /* true if we failed to get space for frame. */
123 int packet_type;
124 int ret;
125 u8 *buffer;
127 buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
128 if (!buffer) {
129 PDEBUG(D_ERR, "Couldn't allocate USB buffer");
130 goto quit_stream;
133 while (gspca_dev->present && gspca_dev->streaming) {
134 if (!gspca_dev->present)
135 goto quit_stream;
136 /* Request the header, which tells the size to download */
137 ret = usb_bulk_msg(gspca_dev->dev,
138 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
139 buffer, FRAME_HEADER_LEN, &act_len,
140 SQ905C_DATA_TIMEOUT);
141 PDEBUG(D_STREAM,
142 "Got %d bytes out of %d for header",
143 act_len, FRAME_HEADER_LEN);
144 if (ret < 0 || act_len < FRAME_HEADER_LEN)
145 goto quit_stream;
146 /* size is read from 4 bytes starting 0x40, little endian */
147 bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
148 |(buffer[0x43]<<24);
149 PDEBUG(D_STREAM, "bytes_left = 0x%x", bytes_left);
150 /* We keep the header. It has other information, too. */
151 packet_type = FIRST_PACKET;
152 frame = gspca_get_i_frame(gspca_dev);
153 if (frame && !discarding) {
154 gspca_frame_add(gspca_dev, packet_type,
155 frame, buffer, FRAME_HEADER_LEN);
156 } else
157 discarding = 1;
158 while (bytes_left > 0) {
159 data_len = bytes_left > SQ905C_MAX_TRANSFER ?
160 SQ905C_MAX_TRANSFER : bytes_left;
161 if (!gspca_dev->present)
162 goto quit_stream;
163 ret = usb_bulk_msg(gspca_dev->dev,
164 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
165 buffer, data_len, &act_len,
166 SQ905C_DATA_TIMEOUT);
167 if (ret < 0 || act_len < data_len)
168 goto quit_stream;
169 PDEBUG(D_STREAM,
170 "Got %d bytes out of %d for frame",
171 data_len, bytes_left);
172 bytes_left -= data_len;
173 if (bytes_left == 0)
174 packet_type = LAST_PACKET;
175 else
176 packet_type = INTER_PACKET;
177 frame = gspca_get_i_frame(gspca_dev);
178 if (frame && !discarding)
179 gspca_frame_add(gspca_dev, packet_type,
180 frame, buffer, data_len);
181 else
182 discarding = 1;
185 quit_stream:
186 mutex_lock(&gspca_dev->usb_lock);
187 if (gspca_dev->present)
188 sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
189 mutex_unlock(&gspca_dev->usb_lock);
190 kfree(buffer);
193 /* This function is called at probe time just before sd_init */
194 static int sd_config(struct gspca_dev *gspca_dev,
195 const struct usb_device_id *id)
197 struct cam *cam = &gspca_dev->cam;
198 struct sd *dev = (struct sd *) gspca_dev;
200 PDEBUG(D_PROBE,
201 "SQ9050 camera detected"
202 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
203 cam->cam_mode = sq905c_mode;
204 cam->nmodes = 2;
205 if (id->idProduct == 0x9050)
206 cam->nmodes = 1;
207 /* We don't use the buffer gspca allocates so make it small. */
208 cam->bulk_size = 32;
209 cam->bulk = 1;
210 INIT_WORK(&dev->work_struct, sq905c_dostream);
211 return 0;
214 /* called on streamoff with alt==0 and on disconnect */
215 /* the usb_lock is held at entry - restore on exit */
216 static void sd_stop0(struct gspca_dev *gspca_dev)
218 struct sd *dev = (struct sd *) gspca_dev;
220 /* wait for the work queue to terminate */
221 mutex_unlock(&gspca_dev->usb_lock);
222 /* This waits for sq905c_dostream to finish */
223 destroy_workqueue(dev->work_thread);
224 dev->work_thread = NULL;
225 mutex_lock(&gspca_dev->usb_lock);
228 /* this function is called at probe and resume time */
229 static int sd_init(struct gspca_dev *gspca_dev)
231 int ret;
233 /* connect to the camera and reset it. */
234 ret = sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
235 return ret;
238 /* Set up for getting frames. */
239 static int sd_start(struct gspca_dev *gspca_dev)
241 struct sd *dev = (struct sd *) gspca_dev;
242 int ret;
244 dev->cap_mode = gspca_dev->cam.cam_mode;
245 /* "Open the shutter" and set size, to start capture */
246 switch (gspca_dev->width) {
247 case 640:
248 PDEBUG(D_STREAM, "Start streaming at high resolution");
249 dev->cap_mode++;
250 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
251 SQ905C_CAPTURE_INDEX);
252 break;
253 default: /* 320 */
254 PDEBUG(D_STREAM, "Start streaming at medium resolution");
255 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
256 SQ905C_CAPTURE_INDEX);
259 if (ret < 0) {
260 PDEBUG(D_ERR, "Start streaming command failed");
261 return ret;
263 /* Start the workqueue function to do the streaming */
264 dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
265 queue_work(dev->work_thread, &dev->work_struct);
267 return 0;
270 /* Table of supported USB devices */
271 static const __devinitdata struct usb_device_id device_table[] = {
272 {USB_DEVICE(0x2770, 0x905c)},
273 {USB_DEVICE(0x2770, 0x9050)},
274 {USB_DEVICE(0x2770, 0x913d)},
278 MODULE_DEVICE_TABLE(usb, device_table);
280 /* sub-driver description */
281 static const struct sd_desc sd_desc = {
282 .name = MODULE_NAME,
283 .config = sd_config,
284 .init = sd_init,
285 .start = sd_start,
286 .stop0 = sd_stop0,
289 /* -- device connect -- */
290 static int sd_probe(struct usb_interface *intf,
291 const struct usb_device_id *id)
293 return gspca_dev_probe(intf, id,
294 &sd_desc,
295 sizeof(struct sd),
296 THIS_MODULE);
299 static struct usb_driver sd_driver = {
300 .name = MODULE_NAME,
301 .id_table = device_table,
302 .probe = sd_probe,
303 .disconnect = gspca_disconnect,
304 #ifdef CONFIG_PM
305 .suspend = gspca_suspend,
306 .resume = gspca_resume,
307 #endif
310 /* -- module insert / remove -- */
311 static int __init sd_mod_init(void)
313 int ret;
315 ret = usb_register(&sd_driver);
316 if (ret < 0)
317 return ret;
318 PDEBUG(D_PROBE, "registered");
319 return 0;
322 static void __exit sd_mod_exit(void)
324 usb_deregister(&sd_driver);
325 PDEBUG(D_PROBE, "deregistered");
328 module_init(sd_mod_init);
329 module_exit(sd_mod_exit);