Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/drivers/media/usb/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 contributors
 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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 37 
 38 #define MODULE_NAME "sq905"
 39 
 40 #include <linux/workqueue.h>
 41 #include <linux/slab.h>
 42 #include "gspca.h"
 43 
 44 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, Theodore Kilgore <kilgota@auburn.edu>");
 45 MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
 46 MODULE_LICENSE("GPL");
 47 
 48 /* Default timeouts, in ms */
 49 #define SQ905_CMD_TIMEOUT 500
 50 #define SQ905_DATA_TIMEOUT 1000
 51 
 52 /* Maximum transfer size to use. */
 53 #define SQ905_MAX_TRANSFER 0x8000
 54 #define FRAME_HEADER_LEN 64
 55 
 56 /* The known modes, or registers. These go in the "value" slot. */
 57 
 58 /* 00 is "none" obviously */
 59 
 60 #define SQ905_BULK_READ 0x03    /* precedes any bulk read */
 61 #define SQ905_COMMAND   0x06    /* precedes the command codes below */
 62 #define SQ905_PING      0x07    /* when reading an "idling" command */
 63 #define SQ905_READ_DONE 0xc0    /* ack bulk read completed */
 64 
 65 /* Any non-zero value in the bottom 2 bits of the 2nd byte of
 66  * the ID appears to indicate the camera can do 640*480. If the
 67  * LSB of that byte is set the image is just upside down, otherwise
 68  * it is rotated 180 degrees. */
 69 #define SQ905_HIRES_MASK        0x00000300
 70 #define SQ905_ORIENTATION_MASK  0x00000100
 71 
 72 /* Some command codes. These go in the "index" slot. */
 73 
 74 #define SQ905_ID      0xf0      /* asks for model string */
 75 #define SQ905_CONFIG  0x20      /* gets photo alloc. table, not used here */
 76 #define SQ905_DATA    0x30      /* accesses photo data, not used here */
 77 #define SQ905_CLEAR   0xa0      /* clear everything */
 78 #define SQ905_CAPTURE_LOW  0x60 /* Starts capture at 160x120 */
 79 #define SQ905_CAPTURE_MED  0x61 /* Starts capture at 320x240 */
 80 #define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
 81 /* note that the capture command also controls the output dimensions */
 82 
 83 /* Structure to hold all of our device specific stuff */
 84 struct sd {
 85         struct gspca_dev gspca_dev;     /* !! must be the first item */
 86 
 87         /*
 88          * Driver stuff
 89          */
 90         struct work_struct work_struct;
 91         struct workqueue_struct *work_thread;
 92 };
 93 
 94 static struct v4l2_pix_format sq905_mode[] = {
 95         { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
 96                 .bytesperline = 160,
 97                 .sizeimage = 160 * 120,
 98                 .colorspace = V4L2_COLORSPACE_SRGB,
 99                 .priv = 0},
100         { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
101                 .bytesperline = 320,
102                 .sizeimage = 320 * 240,
103                 .colorspace = V4L2_COLORSPACE_SRGB,
104                 .priv = 0},
105         { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
106                 .bytesperline = 640,
107                 .sizeimage = 640 * 480,
108                 .colorspace = V4L2_COLORSPACE_SRGB,
109                 .priv = 0}
110 };
111 
112 /*
113  * Send a command to the camera.
114  */
115 static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
116 {
117         int ret;
118 
119         gspca_dev->usb_buf[0] = '\0';
120         ret = usb_control_msg(gspca_dev->dev,
121                               usb_sndctrlpipe(gspca_dev->dev, 0),
122                               USB_REQ_SYNCH_FRAME,                /* request */
123                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124                               SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
125                               SQ905_CMD_TIMEOUT);
126         if (ret < 0) {
127                 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
128                 return ret;
129         }
130 
131         ret = usb_control_msg(gspca_dev->dev,
132                               usb_sndctrlpipe(gspca_dev->dev, 0),
133                               USB_REQ_SYNCH_FRAME,                /* request */
134                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
135                               SQ905_PING, 0, gspca_dev->usb_buf, 1,
136                               SQ905_CMD_TIMEOUT);
137         if (ret < 0) {
138                 pr_err("%s: usb_control_msg failed 2 (%d)\n", __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                 pr_err("%s: usb_control_msg failed (%d)\n", __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, int need_lock)
172 {
173         int ret;
174         int act_len;
175 
176         gspca_dev->usb_buf[0] = '\0';
177         if (need_lock)
178                 mutex_lock(&gspca_dev->usb_lock);
179         ret = usb_control_msg(gspca_dev->dev,
180                               usb_sndctrlpipe(gspca_dev->dev, 0),
181                               USB_REQ_SYNCH_FRAME,                /* request */
182                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
183                               SQ905_BULK_READ, size, gspca_dev->usb_buf,
184                               1, SQ905_CMD_TIMEOUT);
185         if (need_lock)
186                 mutex_unlock(&gspca_dev->usb_lock);
187         if (ret < 0) {
188                 pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
189                 return ret;
190         }
191         ret = usb_bulk_msg(gspca_dev->dev,
192                            usb_rcvbulkpipe(gspca_dev->dev, 0x81),
193                            data, size, &act_len, SQ905_DATA_TIMEOUT);
194 
195         /* successful, it returns 0, otherwise  negative */
196         if (ret < 0 || act_len != size) {
197                 pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size);
198                 return -EIO;
199         }
200         return 0;
201 }
202 
203 /*
204  * This function is called as a workqueue function and runs whenever the camera
205  * is streaming data. Because it is a workqueue function it is allowed to sleep
206  * so we can use synchronous USB calls. To avoid possible collisions with other
207  * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
208  * performing USB operations using it. In practice we don't really need this
209  * as the camera doesn't provide any controls.
210  */
211 static void sq905_dostream(struct work_struct *work)
212 {
213         struct sd *dev = container_of(work, struct sd, work_struct);
214         struct gspca_dev *gspca_dev = &dev->gspca_dev;
215         int bytes_left; /* bytes remaining in current frame. */
216         int data_len;   /* size to use for the next read. */
217         int header_read; /* true if we have already read the frame header. */
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         if (!buffer) {
226                 pr_err("Couldn't allocate USB buffer\n");
227                 goto quit_stream;
228         }
229 
230         frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
231                         + FRAME_HEADER_LEN;
232 
233         while (gspca_dev->present && gspca_dev->streaming) {
234 #ifdef CONFIG_PM
235                 if (gspca_dev->frozen)
236                         break;
237 #endif
238                 /* request some data and then read it until we have
239                  * a complete frame. */
240                 bytes_left = frame_sz;
241                 header_read = 0;
242 
243                 /* Note we do not check for gspca_dev->streaming here, as
244                    we must finish reading an entire frame, otherwise the
245                    next time we stream we start reading in the middle of a
246                    frame. */
247                 while (bytes_left > 0 && gspca_dev->present) {
248                         data_len = bytes_left > SQ905_MAX_TRANSFER ?
249                                 SQ905_MAX_TRANSFER : bytes_left;
250                         ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
251                         if (ret < 0)
252                                 goto quit_stream;
253                         PDEBUG(D_PACK,
254                                 "Got %d bytes out of %d for frame",
255                                 data_len, bytes_left);
256                         bytes_left -= data_len;
257                         data = buffer;
258                         if (!header_read) {
259                                 packet_type = FIRST_PACKET;
260                                 /* The first 64 bytes of each frame are
261                                  * a header full of FF 00 bytes */
262                                 data += FRAME_HEADER_LEN;
263                                 data_len -= FRAME_HEADER_LEN;
264                                 header_read = 1;
265                         } else if (bytes_left == 0) {
266                                 packet_type = LAST_PACKET;
267                         } else {
268                                 packet_type = INTER_PACKET;
269                         }
270                         gspca_frame_add(gspca_dev, packet_type,
271                                         data, data_len);
272                         /* If entire frame fits in one packet we still
273                            need to add a LAST_PACKET */
274                         if (packet_type == FIRST_PACKET &&
275                             bytes_left == 0)
276                                 gspca_frame_add(gspca_dev, LAST_PACKET,
277                                                 NULL, 0);
278                 }
279                 if (gspca_dev->present) {
280                         /* acknowledge the frame */
281                         mutex_lock(&gspca_dev->usb_lock);
282                         ret = sq905_ack_frame(gspca_dev);
283                         mutex_unlock(&gspca_dev->usb_lock);
284                         if (ret < 0)
285                                 goto quit_stream;
286                 }
287         }
288 quit_stream:
289         if (gspca_dev->present) {
290                 mutex_lock(&gspca_dev->usb_lock);
291                 sq905_command(gspca_dev, SQ905_CLEAR);
292                 mutex_unlock(&gspca_dev->usb_lock);
293         }
294         kfree(buffer);
295 }
296 
297 /* This function is called at probe time just before sd_init */
298 static int sd_config(struct gspca_dev *gspca_dev,
299                 const struct usb_device_id *id)
300 {
301         struct cam *cam = &gspca_dev->cam;
302         struct sd *dev = (struct sd *) gspca_dev;
303 
304         /* We don't use the buffer gspca allocates so make it small. */
305         cam->bulk = 1;
306         cam->bulk_size = 64;
307 
308         INIT_WORK(&dev->work_struct, sq905_dostream);
309 
310         return 0;
311 }
312 
313 /* called on streamoff with alt==0 and on disconnect */
314 /* the usb_lock is held at entry - restore on exit */
315 static void sd_stop0(struct gspca_dev *gspca_dev)
316 {
317         struct sd *dev = (struct sd *) gspca_dev;
318 
319         /* wait for the work queue to terminate */
320         mutex_unlock(&gspca_dev->usb_lock);
321         /* This waits for sq905_dostream to finish */
322         destroy_workqueue(dev->work_thread);
323         dev->work_thread = NULL;
324         mutex_lock(&gspca_dev->usb_lock);
325 }
326 
327 /* this function is called at probe and resume time */
328 static int sd_init(struct gspca_dev *gspca_dev)
329 {
330         u32 ident;
331         int ret;
332 
333         /* connect to the camera and read
334          * the model ID and process that and put it away.
335          */
336         ret = sq905_command(gspca_dev, SQ905_CLEAR);
337         if (ret < 0)
338                 return ret;
339         ret = sq905_command(gspca_dev, SQ905_ID);
340         if (ret < 0)
341                 return ret;
342         ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
343         if (ret < 0)
344                 return ret;
345         /* usb_buf is allocated with kmalloc so is aligned.
346          * Camera model number is the right way round if we assume this
347          * reverse engineered ID is supposed to be big endian. */
348         ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
349         ret = sq905_command(gspca_dev, SQ905_CLEAR);
350         if (ret < 0)
351                 return ret;
352         PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
353         gspca_dev->cam.cam_mode = sq905_mode;
354         gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
355         if (!(ident & SQ905_HIRES_MASK))
356                 gspca_dev->cam.nmodes--;
357 
358         if (ident & SQ905_ORIENTATION_MASK)
359                 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
360         else
361                 gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
362                                              V4L2_IN_ST_HFLIP;
363         return 0;
364 }
365 
366 /* Set up for getting frames. */
367 static int sd_start(struct gspca_dev *gspca_dev)
368 {
369         struct sd *dev = (struct sd *) gspca_dev;
370         int ret;
371 
372         /* "Open the shutter" and set size, to start capture */
373         switch (gspca_dev->curr_mode) {
374         default:
375 /*      case 2: */
376                 PDEBUG(D_STREAM, "Start streaming at high resolution");
377                 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
378                 break;
379         case 1:
380                 PDEBUG(D_STREAM, "Start streaming at medium resolution");
381                 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
382                 break;
383         case 0:
384                 PDEBUG(D_STREAM, "Start streaming at low resolution");
385                 ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
386         }
387 
388         if (ret < 0) {
389                 PERR("Start streaming command failed");
390                 return ret;
391         }
392         /* Start the workqueue function to do the streaming */
393         dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
394         queue_work(dev->work_thread, &dev->work_struct);
395 
396         return 0;
397 }
398 
399 /* Table of supported USB devices */
400 static const struct usb_device_id device_table[] = {
401         {USB_DEVICE(0x2770, 0x9120)},
402         {}
403 };
404 
405 MODULE_DEVICE_TABLE(usb, device_table);
406 
407 /* sub-driver description */
408 static const struct sd_desc sd_desc = {
409         .name   = MODULE_NAME,
410         .config = sd_config,
411         .init   = sd_init,
412         .start  = sd_start,
413         .stop0  = sd_stop0,
414 };
415 
416 /* -- device connect -- */
417 static int sd_probe(struct usb_interface *intf,
418                 const struct usb_device_id *id)
419 {
420         return gspca_dev_probe(intf, id,
421                         &sd_desc,
422                         sizeof(struct sd),
423                         THIS_MODULE);
424 }
425 
426 static struct usb_driver sd_driver = {
427         .name       = MODULE_NAME,
428         .id_table   = device_table,
429         .probe      = sd_probe,
430         .disconnect = gspca_disconnect,
431 #ifdef CONFIG_PM
432         .suspend = gspca_suspend,
433         .resume  = gspca_resume,
434         .reset_resume = gspca_resume,
435 #endif
436 };
437 
438 module_usb_driver(sd_driver);
439 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us