Merge branch 'x86-platform-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / media / video / gspca / sq905c.c
1 /*
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 <linux/slab.h>
34 #include "gspca.h"
35
36 MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
37 MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
38 MODULE_LICENSE("GPL");
39
40 /* Default timeouts, in ms */
41 #define SQ905C_CMD_TIMEOUT 500
42 #define SQ905C_DATA_TIMEOUT 1000
43
44 /* Maximum transfer size to use. */
45 #define SQ905C_MAX_TRANSFER 0x8000
46
47 #define FRAME_HEADER_LEN 0x50
48
49 /* Commands. These go in the "value" slot. */
50 #define SQ905C_CLEAR   0xa0             /* clear everything */
51 #define SQ905C_GET_ID  0x14f4           /* Read version number */
52 #define SQ905C_CAPTURE_LOW 0xa040       /* Starts capture at 160x120 */
53 #define SQ905C_CAPTURE_MED 0x1440       /* Starts capture at 320x240 */
54 #define SQ905C_CAPTURE_HI 0x2840        /* Starts capture at 320x240 */
55
56 /* For capture, this must go in the "index" slot. */
57 #define SQ905C_CAPTURE_INDEX 0x110f
58
59 /* Structure to hold all of our device specific stuff */
60 struct sd {
61         struct gspca_dev gspca_dev;     /* !! must be the first item */
62         const struct v4l2_pix_format *cap_mode;
63         /* Driver stuff */
64         struct work_struct work_struct;
65         struct workqueue_struct *work_thread;
66 };
67
68 /*
69  * Most of these cameras will do 640x480 and 320x240. 160x120 works
70  * in theory but gives very poor output. Therefore, not supported.
71  * The 0x2770:0x9050 cameras have max resolution of 320x240.
72  */
73 static struct v4l2_pix_format sq905c_mode[] = {
74         { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
75                 .bytesperline = 320,
76                 .sizeimage = 320 * 240,
77                 .colorspace = V4L2_COLORSPACE_SRGB,
78                 .priv = 0},
79         { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
80                 .bytesperline = 640,
81                 .sizeimage = 640 * 480,
82                 .colorspace = V4L2_COLORSPACE_SRGB,
83                 .priv = 0}
84 };
85
86 /* Send a command to the camera. */
87 static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
88 {
89         int ret;
90
91         ret = usb_control_msg(gspca_dev->dev,
92                               usb_sndctrlpipe(gspca_dev->dev, 0),
93                               USB_REQ_SYNCH_FRAME,                /* request */
94                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
95                               command, index, NULL, 0,
96                               SQ905C_CMD_TIMEOUT);
97         if (ret < 0) {
98                 err("%s: usb_control_msg failed (%d)",
99                         __func__, ret);
100                 return ret;
101         }
102
103         return 0;
104 }
105
106 static int sq905c_read(struct gspca_dev *gspca_dev, u16 command, u16 index,
107                        int size)
108 {
109         int ret;
110
111         ret = usb_control_msg(gspca_dev->dev,
112                               usb_rcvctrlpipe(gspca_dev->dev, 0),
113                               USB_REQ_SYNCH_FRAME,              /* request */
114                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
115                               command, index, gspca_dev->usb_buf, size,
116                               SQ905C_CMD_TIMEOUT);
117         if (ret < 0) {
118                 err("%s: usb_control_msg failed (%d)",
119                        __func__, ret);
120                 return ret;
121         }
122
123         return 0;
124 }
125
126 /* 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  */
136 static 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;
140         int bytes_left; /* bytes remaining in current frame. */
141         int data_len;   /* size to use for the next read. */
142         int act_len;
143         int packet_type;
144         int ret;
145         u8 *buffer;
146
147         buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
148         if (!buffer) {
149                 err("Couldn't allocate USB buffer");
150                 goto quit_stream;
151         }
152
153         while (gspca_dev->present && gspca_dev->streaming) {
154                 /* Request the header, which tells the size to download */
155                 ret = usb_bulk_msg(gspca_dev->dev,
156                                 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
157                                 buffer, FRAME_HEADER_LEN, &act_len,
158                                 SQ905C_DATA_TIMEOUT);
159                 PDEBUG(D_STREAM,
160                         "Got %d bytes out of %d for header",
161                         act_len, FRAME_HEADER_LEN);
162                 if (ret < 0 || act_len < FRAME_HEADER_LEN)
163                         goto quit_stream;
164                 /* size is read from 4 bytes starting 0x40, little endian */
165                 bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
166                                         |(buffer[0x43]<<24);
167                 PDEBUG(D_STREAM, "bytes_left = 0x%x", bytes_left);
168                 /* We keep the header. It has other information, too. */
169                 packet_type = FIRST_PACKET;
170                 gspca_frame_add(gspca_dev, packet_type,
171                                 buffer, FRAME_HEADER_LEN);
172                 while (bytes_left > 0 && gspca_dev->present) {
173                         data_len = bytes_left > SQ905C_MAX_TRANSFER ?
174                                 SQ905C_MAX_TRANSFER : bytes_left;
175                         ret = usb_bulk_msg(gspca_dev->dev,
176                                 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
177                                 buffer, data_len, &act_len,
178                                 SQ905C_DATA_TIMEOUT);
179                         if (ret < 0 || act_len < data_len)
180                                 goto quit_stream;
181                         PDEBUG(D_STREAM,
182                                 "Got %d bytes out of %d for frame",
183                                 data_len, bytes_left);
184                         bytes_left -= data_len;
185                         if (bytes_left == 0)
186                                 packet_type = LAST_PACKET;
187                         else
188                                 packet_type = INTER_PACKET;
189                         gspca_frame_add(gspca_dev, packet_type,
190                                         buffer, data_len);
191                 }
192         }
193 quit_stream:
194         if (gspca_dev->present) {
195                 mutex_lock(&gspca_dev->usb_lock);
196                 sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
197                 mutex_unlock(&gspca_dev->usb_lock);
198         }
199         kfree(buffer);
200 }
201
202 /* This function is called at probe time just before sd_init */
203 static int sd_config(struct gspca_dev *gspca_dev,
204                 const struct usb_device_id *id)
205 {
206         struct cam *cam = &gspca_dev->cam;
207         struct sd *dev = (struct sd *) gspca_dev;
208         int ret;
209
210         PDEBUG(D_PROBE,
211                 "SQ9050 camera detected"
212                 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
213
214         ret = sq905c_command(gspca_dev, SQ905C_GET_ID, 0);
215         if (ret < 0) {
216                 PDEBUG(D_ERR, "Get version command failed");
217                 return ret;
218         }
219
220         ret = sq905c_read(gspca_dev, 0xf5, 0, 20);
221         if (ret < 0) {
222                 PDEBUG(D_ERR, "Reading version command failed");
223                 return ret;
224         }
225         /* Note we leave out the usb id and the manufacturing date */
226         PDEBUG(D_PROBE,
227                "SQ9050 ID string: %02x - %02x %02x %02x %02x %02x %02x",
228                 gspca_dev->usb_buf[3],
229                 gspca_dev->usb_buf[14], gspca_dev->usb_buf[15],
230                 gspca_dev->usb_buf[16], gspca_dev->usb_buf[17],
231                 gspca_dev->usb_buf[18], gspca_dev->usb_buf[19]);
232
233         cam->cam_mode = sq905c_mode;
234         cam->nmodes = 2;
235         if (gspca_dev->usb_buf[15] == 0)
236                 cam->nmodes = 1;
237         /* We don't use the buffer gspca allocates so make it small. */
238         cam->bulk_size = 32;
239         cam->bulk = 1;
240         INIT_WORK(&dev->work_struct, sq905c_dostream);
241         return 0;
242 }
243
244 /* called on streamoff with alt==0 and on disconnect */
245 /* the usb_lock is held at entry - restore on exit */
246 static void sd_stop0(struct gspca_dev *gspca_dev)
247 {
248         struct sd *dev = (struct sd *) gspca_dev;
249
250         /* wait for the work queue to terminate */
251         mutex_unlock(&gspca_dev->usb_lock);
252         /* This waits for sq905c_dostream to finish */
253         destroy_workqueue(dev->work_thread);
254         dev->work_thread = NULL;
255         mutex_lock(&gspca_dev->usb_lock);
256 }
257
258 /* this function is called at probe and resume time */
259 static int sd_init(struct gspca_dev *gspca_dev)
260 {
261         int ret;
262
263         /* connect to the camera and reset it. */
264         ret = sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
265         return ret;
266 }
267
268 /* Set up for getting frames. */
269 static int sd_start(struct gspca_dev *gspca_dev)
270 {
271         struct sd *dev = (struct sd *) gspca_dev;
272         int ret;
273
274         dev->cap_mode = gspca_dev->cam.cam_mode;
275         /* "Open the shutter" and set size, to start capture */
276         switch (gspca_dev->width) {
277         case 640:
278                 PDEBUG(D_STREAM, "Start streaming at high resolution");
279                 dev->cap_mode++;
280                 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
281                                                 SQ905C_CAPTURE_INDEX);
282                 break;
283         default: /* 320 */
284         PDEBUG(D_STREAM, "Start streaming at medium resolution");
285                 ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
286                                                 SQ905C_CAPTURE_INDEX);
287         }
288
289         if (ret < 0) {
290                 PDEBUG(D_ERR, "Start streaming command failed");
291                 return ret;
292         }
293         /* Start the workqueue function to do the streaming */
294         dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
295         queue_work(dev->work_thread, &dev->work_struct);
296
297         return 0;
298 }
299
300 /* Table of supported USB devices */
301 static const __devinitdata struct usb_device_id device_table[] = {
302         {USB_DEVICE(0x2770, 0x905c)},
303         {USB_DEVICE(0x2770, 0x9050)},
304         {USB_DEVICE(0x2770, 0x9052)},
305         {USB_DEVICE(0x2770, 0x913d)},
306         {}
307 };
308
309 MODULE_DEVICE_TABLE(usb, device_table);
310
311 /* sub-driver description */
312 static const struct sd_desc sd_desc = {
313         .name   = MODULE_NAME,
314         .config = sd_config,
315         .init   = sd_init,
316         .start  = sd_start,
317         .stop0  = sd_stop0,
318 };
319
320 /* -- device connect -- */
321 static int sd_probe(struct usb_interface *intf,
322                 const struct usb_device_id *id)
323 {
324         return gspca_dev_probe(intf, id,
325                         &sd_desc,
326                         sizeof(struct sd),
327                         THIS_MODULE);
328 }
329
330 static struct usb_driver sd_driver = {
331         .name       = MODULE_NAME,
332         .id_table   = device_table,
333         .probe      = sd_probe,
334         .disconnect = gspca_disconnect,
335 #ifdef CONFIG_PM
336         .suspend = gspca_suspend,
337         .resume  = gspca_resume,
338 #endif
339 };
340
341 /* -- module insert / remove -- */
342 static int __init sd_mod_init(void)
343 {
344         return usb_register(&sd_driver);
345 }
346
347 static void __exit sd_mod_exit(void)
348 {
349         usb_deregister(&sd_driver);
350 }
351
352 module_init(sd_mod_init);
353 module_exit(sd_mod_exit);