V4L/DVB (11387): Sensor orientation reporting
[safe/jmp/linux-2.6] / drivers / media / video / gspca / sq905.c
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
41 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, "
42                 "Theodore Kilgore <kilgota@auburn.edu>");
43 MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
44 MODULE_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 /* Any non-zero value in the bottom 2 bits of the 2nd byte of
64  * the ID appears to indicate the camera can do 640*480. If the
65  * LSB of that byte is set the image is just upside down, otherwise
66  * it is rotated 180 degrees. */
67 #define SQ905_HIRES_MASK        0x00000300
68 #define SQ905_ORIENTATION_MASK  0x00000100
69
70 /* Some command codes. These go in the "index" slot. */
71
72 #define SQ905_ID      0xf0      /* asks for model string */
73 #define SQ905_CONFIG  0x20      /* gets photo alloc. table, not used here */
74 #define SQ905_DATA    0x30      /* accesses photo data, not used here */
75 #define SQ905_CLEAR   0xa0      /* clear everything */
76 #define SQ905_CAPTURE_LOW  0x60 /* Starts capture at 160x120 */
77 #define SQ905_CAPTURE_MED  0x61 /* Starts capture at 320x240 */
78 #define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
79 /* note that the capture command also controls the output dimensions */
80
81 /* Structure to hold all of our device specific stuff */
82 struct sd {
83         struct gspca_dev gspca_dev;     /* !! must be the first item */
84
85         /*
86          * Driver stuff
87          */
88         struct work_struct work_struct;
89         struct workqueue_struct *work_thread;
90 };
91
92 static struct v4l2_pix_format sq905_mode[] = {
93         { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
94                 .bytesperline = 160,
95                 .sizeimage = 160 * 120,
96                 .colorspace = V4L2_COLORSPACE_SRGB,
97                 .priv = 0},
98         { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
99                 .bytesperline = 320,
100                 .sizeimage = 320 * 240,
101                 .colorspace = V4L2_COLORSPACE_SRGB,
102                 .priv = 0},
103         { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
104                 .bytesperline = 640,
105                 .sizeimage = 640 * 480,
106                 .colorspace = V4L2_COLORSPACE_SRGB,
107                 .priv = 0}
108 };
109
110 /*
111  * Send a command to the camera.
112  */
113 static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
114 {
115         int ret;
116
117         gspca_dev->usb_buf[0] = '\0';
118         ret = usb_control_msg(gspca_dev->dev,
119                               usb_sndctrlpipe(gspca_dev->dev, 0),
120                               USB_REQ_SYNCH_FRAME,                /* request */
121                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122                               SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
123                               SQ905_CMD_TIMEOUT);
124         if (ret < 0) {
125                 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)",
126                         __func__, ret);
127                 return ret;
128         }
129
130         ret = usb_control_msg(gspca_dev->dev,
131                               usb_sndctrlpipe(gspca_dev->dev, 0),
132                               USB_REQ_SYNCH_FRAME,                /* request */
133                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
134                               SQ905_PING, 0, gspca_dev->usb_buf, 1,
135                               SQ905_CMD_TIMEOUT);
136         if (ret < 0) {
137                 PDEBUG(D_ERR, "%s: usb_control_msg failed 2 (%d)",
138                         __func__, ret);
139                 return ret;
140         }
141
142         return 0;
143 }
144
145 /*
146  * Acknowledge the end of a frame - see warning on sq905_command.
147  */
148 static int sq905_ack_frame(struct gspca_dev *gspca_dev)
149 {
150         int ret;
151
152         gspca_dev->usb_buf[0] = '\0';
153         ret = usb_control_msg(gspca_dev->dev,
154                               usb_sndctrlpipe(gspca_dev->dev, 0),
155                               USB_REQ_SYNCH_FRAME,                /* request */
156                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
157                               SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
158                               SQ905_CMD_TIMEOUT);
159         if (ret < 0) {
160                 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
161                 return ret;
162         }
163
164         return 0;
165 }
166
167 /*
168  *  request and read a block of data - see warning on sq905_command.
169  */
170 static int
171 sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size)
172 {
173         int ret;
174         int act_len;
175
176         gspca_dev->usb_buf[0] = '\0';
177         ret = usb_control_msg(gspca_dev->dev,
178                               usb_sndctrlpipe(gspca_dev->dev, 0),
179                               USB_REQ_SYNCH_FRAME,                /* request */
180                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
181                               SQ905_BULK_READ, size, gspca_dev->usb_buf,
182                               1, SQ905_CMD_TIMEOUT);
183         if (ret < 0) {
184                 PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
185                 return ret;
186         }
187         ret = usb_bulk_msg(gspca_dev->dev,
188                            usb_rcvbulkpipe(gspca_dev->dev, 0x81),
189                            data, size, &act_len, SQ905_DATA_TIMEOUT);
190
191         /* successful, it returns 0, otherwise  negative */
192         if (ret < 0 || act_len != size) {
193                 PDEBUG(D_ERR, "bulk read fail (%d) len %d/%d",
194                         ret, act_len, size);
195                 return -EIO;
196         }
197         return 0;
198 }
199
200 /* This function is called as a workqueue function and runs whenever the camera
201  * is streaming data. Because it is a workqueue function it is allowed to sleep
202  * so we can use synchronous USB calls. To avoid possible collisions with other
203  * threads attempting to use the camera's USB interface we take the gspca
204  * usb_lock when performing USB operations. In practice the only thing we need
205  * to protect against is the usb_set_interface call that gspca makes during
206  * stream_off as the camera doesn't provide any controls that the user could try
207  * to change.
208  */
209 static void sq905_dostream(struct work_struct *work)
210 {
211         struct sd *dev = container_of(work, struct sd, work_struct);
212         struct gspca_dev *gspca_dev = &dev->gspca_dev;
213         struct gspca_frame *frame;
214         int bytes_left; /* bytes remaining in current frame. */
215         int data_len;   /* size to use for the next read. */
216         int header_read; /* true if we have already read the frame header. */
217         int discarding; /* true if we failed to get space for frame. */
218         int packet_type;
219         int frame_sz;
220         int ret;
221         u8 *data;
222         u8 *buffer;
223
224         buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
225         mutex_lock(&gspca_dev->usb_lock);
226         if (!buffer) {
227                 PDEBUG(D_ERR, "Couldn't allocate USB buffer");
228                 goto quit_stream;
229         }
230
231         frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
232                         + FRAME_HEADER_LEN;
233
234         while (gspca_dev->present && gspca_dev->streaming) {
235                 /* Need a short delay to ensure streaming flag was set by
236                  * gspca and to make sure gspca can grab the mutex. */
237                 mutex_unlock(&gspca_dev->usb_lock);
238                 msleep(1);
239
240                 /* request some data and then read it until we have
241                  * a complete frame. */
242                 bytes_left = frame_sz;
243                 header_read = 0;
244                 discarding = 0;
245
246                 while (bytes_left > 0) {
247                         data_len = bytes_left > SQ905_MAX_TRANSFER ?
248                                 SQ905_MAX_TRANSFER : bytes_left;
249                         mutex_lock(&gspca_dev->usb_lock);
250                         if (!gspca_dev->present)
251                                 goto quit_stream;
252                         ret = sq905_read_data(gspca_dev, buffer, data_len);
253                         if (ret < 0)
254                                 goto quit_stream;
255                         mutex_unlock(&gspca_dev->usb_lock);
256                         PDEBUG(D_STREAM,
257                                 "Got %d bytes out of %d for frame",
258                                 data_len, bytes_left);
259                         bytes_left -= data_len;
260                         data = buffer;
261                         if (!header_read) {
262                                 packet_type = FIRST_PACKET;
263                                 /* The first 64 bytes of each frame are
264                                  * a header full of FF 00 bytes */
265                                 data += FRAME_HEADER_LEN;
266                                 data_len -= FRAME_HEADER_LEN;
267                                 header_read = 1;
268                         } else if (bytes_left == 0) {
269                                 packet_type = LAST_PACKET;
270                         } else {
271                                 packet_type = INTER_PACKET;
272                         }
273                         frame = gspca_get_i_frame(gspca_dev);
274                         if (frame && !discarding) {
275                                 frame = gspca_frame_add(gspca_dev, packet_type,
276                                                 frame, data, data_len);
277                                 /* If entire frame fits in one packet we still
278                                    need to add a LAST_PACKET */
279                                 if (packet_type == FIRST_PACKET &&
280                                     bytes_left == 0)
281                                         frame = gspca_frame_add(gspca_dev,
282                                                         LAST_PACKET,
283                                                         frame, data, 0);
284                         } else {
285                                 discarding = 1;
286                         }
287                 }
288                 /* acknowledge the frame */
289                 mutex_lock(&gspca_dev->usb_lock);
290                 if (!gspca_dev->present)
291                         goto quit_stream;
292                 ret = sq905_ack_frame(gspca_dev);
293                 if (ret < 0)
294                         goto quit_stream;
295         }
296 quit_stream:
297         /* the usb_lock is already acquired */
298         if (gspca_dev->present)
299                 sq905_command(gspca_dev, SQ905_CLEAR);
300         mutex_unlock(&gspca_dev->usb_lock);
301         kfree(buffer);
302 }
303
304 /* This function is called at probe time just before sd_init */
305 static int sd_config(struct gspca_dev *gspca_dev,
306                 const struct usb_device_id *id)
307 {
308         struct cam *cam = &gspca_dev->cam;
309         struct sd *dev = (struct sd *) gspca_dev;
310
311         /* We don't use the buffer gspca allocates so make it small. */
312         cam->bulk_size = 64;
313
314         INIT_WORK(&dev->work_struct, sq905_dostream);
315
316         return 0;
317 }
318
319 /* called on streamoff with alt==0 and on disconnect */
320 /* the usb_lock is held at entry - restore on exit */
321 static void sd_stop0(struct gspca_dev *gspca_dev)
322 {
323         struct sd *dev = (struct sd *) gspca_dev;
324
325         /* wait for the work queue to terminate */
326         mutex_unlock(&gspca_dev->usb_lock);
327         /* This waits for sq905_dostream to finish */
328         destroy_workqueue(dev->work_thread);
329         dev->work_thread = NULL;
330         mutex_lock(&gspca_dev->usb_lock);
331 }
332
333 /* this function is called at probe and resume time */
334 static int sd_init(struct gspca_dev *gspca_dev)
335 {
336         u32 ident;
337         int ret;
338
339         /* connect to the camera and read
340          * the model ID and process that and put it away.
341          */
342         ret = sq905_command(gspca_dev, SQ905_CLEAR);
343         if (ret < 0)
344                 return ret;
345         ret = sq905_command(gspca_dev, SQ905_ID);
346         if (ret < 0)
347                 return ret;
348         ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4);
349         if (ret < 0)
350                 return ret;
351         /* usb_buf is allocated with kmalloc so is aligned.
352          * Camera model number is the right way round if we assume this
353          * reverse engineered ID is supposed to be big endian. */
354         ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
355         ret = sq905_command(gspca_dev, SQ905_CLEAR);
356         if (ret < 0)
357                 return ret;
358         PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
359         gspca_dev->cam.cam_mode = sq905_mode;
360         gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
361         if (!(ident & SQ905_HIRES_MASK))
362                 gspca_dev->cam.nmodes--;
363
364         if (ident & SQ905_ORIENTATION_MASK)
365                 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
366         else
367                 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
368                                              V4L2_IN_ST_HFLIP;
369         return 0;
370 }
371
372 /* Set up for getting frames. */
373 static int sd_start(struct gspca_dev *gspca_dev)
374 {
375         struct sd *dev = (struct sd *) gspca_dev;
376         int ret;
377
378         /* "Open the shutter" and set size, to start capture */
379         switch (gspca_dev->curr_mode) {
380         default:
381 /*      case 2: */
382                 PDEBUG(D_STREAM, "Start streaming at high resolution");
383                 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
384                 break;
385         case 1:
386                 PDEBUG(D_STREAM, "Start streaming at medium resolution");
387                 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
388                 break;
389         case 0:
390                 PDEBUG(D_STREAM, "Start streaming at low resolution");
391                 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
392         }
393
394         if (ret < 0) {
395                 PDEBUG(D_ERR, "Start streaming command failed");
396                 return ret;
397         }
398         /* Start the workqueue function to do the streaming */
399         dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
400         queue_work(dev->work_thread, &dev->work_struct);
401
402         return 0;
403 }
404
405 /* Table of supported USB devices */
406 static const __devinitdata struct usb_device_id device_table[] = {
407         {USB_DEVICE(0x2770, 0x9120)},
408         {}
409 };
410
411 MODULE_DEVICE_TABLE(usb, device_table);
412
413 /* sub-driver description */
414 static const struct sd_desc sd_desc = {
415         .name   = MODULE_NAME,
416         .config = sd_config,
417         .init   = sd_init,
418         .start  = sd_start,
419         .stop0  = sd_stop0,
420 };
421
422 /* -- device connect -- */
423 static int sd_probe(struct usb_interface *intf,
424                 const struct usb_device_id *id)
425 {
426         return gspca_dev_probe(intf, id,
427                         &sd_desc,
428                         sizeof(struct sd),
429                         THIS_MODULE);
430 }
431
432 static struct usb_driver sd_driver = {
433         .name       = MODULE_NAME,
434         .id_table   = device_table,
435         .probe      = sd_probe,
436         .disconnect = gspca_disconnect,
437 #ifdef CONFIG_PM
438         .suspend = gspca_suspend,
439         .resume  = gspca_resume,
440 #endif
441 };
442
443 /* -- module insert / remove -- */
444 static int __init sd_mod_init(void)
445 {
446         int ret;
447
448         ret = usb_register(&sd_driver);
449         if (ret < 0)
450                 return ret;
451         PDEBUG(D_PROBE, "registered");
452         return 0;
453 }
454
455 static void __exit sd_mod_exit(void)
456 {
457         usb_deregister(&sd_driver);
458         PDEBUG(D_PROBE, "deregistered");
459 }
460
461 module_init(sd_mod_init);
462 module_exit(sd_mod_exit);