include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / gspca / sq905.c
index dafaed6..09b3f93 100644 (file)
@@ -36,6 +36,7 @@
 #define MODULE_NAME "sq905"
 
 #include <linux/workqueue.h>
+#include <linux/slab.h>
 #include "gspca.h"
 
 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>, "
@@ -60,24 +61,28 @@ MODULE_LICENSE("GPL");
 #define SQ905_PING     0x07    /* when reading an "idling" command */
 #define SQ905_READ_DONE 0xc0    /* ack bulk read completed */
 
+/* Any non-zero value in the bottom 2 bits of the 2nd byte of
+ * the ID appears to indicate the camera can do 640*480. If the
+ * LSB of that byte is set the image is just upside down, otherwise
+ * it is rotated 180 degrees. */
+#define SQ905_HIRES_MASK       0x00000300
+#define SQ905_ORIENTATION_MASK 0x00000100
+
 /* Some command codes. These go in the "index" slot. */
 
 #define SQ905_ID      0xf0     /* asks for model string */
 #define SQ905_CONFIG  0x20     /* gets photo alloc. table, not used here */
 #define SQ905_DATA    0x30     /* accesses photo data, not used here */
 #define SQ905_CLEAR   0xa0     /* clear everything */
-#define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */
-#define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */
+#define SQ905_CAPTURE_LOW  0x60        /* Starts capture at 160x120 */
+#define SQ905_CAPTURE_MED  0x61        /* Starts capture at 320x240 */
+#define SQ905_CAPTURE_HIGH 0x62        /* Starts capture at 640x480 (some cams only) */
 /* note that the capture command also controls the output dimensions */
-/* 0x60 -> 160x120, 0x61 -> 320x240 0x62 -> 640x480 depends on camera */
-/* 0x62 is not correct, at least for some cams. Should be 0x63 ? */
 
 /* Structure to hold all of our device specific stuff */
 struct sd {
        struct gspca_dev gspca_dev;     /* !! must be the first item */
 
-       u8 cam_type;
-
        /*
         * Driver stuff
         */
@@ -85,33 +90,24 @@ struct sd {
        struct workqueue_struct *work_thread;
 };
 
-/* The driver only supports 320x240 so far. */
-static struct v4l2_pix_format sq905_mode[1] = {
+static struct v4l2_pix_format sq905_mode[] = {
+       { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
+               .bytesperline = 160,
+               .sizeimage = 160 * 120,
+               .colorspace = V4L2_COLORSPACE_SRGB,
+               .priv = 0},
        { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
                .bytesperline = 320,
                .sizeimage = 320 * 240,
                .colorspace = V4L2_COLORSPACE_SRGB,
+               .priv = 0},
+       { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
+               .bytesperline = 640,
+               .sizeimage = 640 * 480,
+               .colorspace = V4L2_COLORSPACE_SRGB,
                .priv = 0}
 };
 
-struct cam_type {
-       u32 ident_word;
-       char *name;
-       struct v4l2_pix_format *min_mode;
-       u8 num_modes;
-       u8 sensor_flags;
-};
-
-#define SQ905_FLIP_HORIZ (1 << 0)
-#define SQ905_FLIP_VERT  (1 << 1)
-
-/* Last entry is default if nothing else matches */
-static struct cam_type cam_types[] = {
-       { 0x19010509, "PocketCam", &sq905_mode[0], 1, SQ905_FLIP_HORIZ },
-       { 0x32010509, "Magpix", &sq905_mode[0], 1, SQ905_FLIP_HORIZ },
-       { 0, "Default", &sq905_mode[0], 1, SQ905_FLIP_HORIZ | SQ905_FLIP_VERT }
-};
-
 /*
  * Send a command to the camera.
  */
@@ -173,18 +169,22 @@ static int sq905_ack_frame(struct gspca_dev *gspca_dev)
  *  request and read a block of data - see warning on sq905_command.
  */
 static int
-sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size)
+sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
 {
        int ret;
        int act_len;
 
        gspca_dev->usb_buf[0] = '\0';
+       if (need_lock)
+               mutex_lock(&gspca_dev->usb_lock);
        ret = usb_control_msg(gspca_dev->dev,
                              usb_sndctrlpipe(gspca_dev->dev, 0),
                              USB_REQ_SYNCH_FRAME,                /* request */
                              USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
                              SQ905_BULK_READ, size, gspca_dev->usb_buf,
                              1, SQ905_CMD_TIMEOUT);
+       if (need_lock)
+               mutex_unlock(&gspca_dev->usb_lock);
        if (ret < 0) {
                PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)", __func__, ret);
                return ret;
@@ -215,46 +215,41 @@ static void sq905_dostream(struct work_struct *work)
 {
        struct sd *dev = container_of(work, struct sd, work_struct);
        struct gspca_dev *gspca_dev = &dev->gspca_dev;
-       struct gspca_frame *frame;
        int bytes_left; /* bytes remaining in current frame. */
        int data_len;   /* size to use for the next read. */
        int header_read; /* true if we have already read the frame header. */
-       int discarding; /* true if we failed to get space for frame. */
        int packet_type;
+       int frame_sz;
        int ret;
        u8 *data;
        u8 *buffer;
 
        buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
-       mutex_lock(&gspca_dev->usb_lock);
        if (!buffer) {
                PDEBUG(D_ERR, "Couldn't allocate USB buffer");
                goto quit_stream;
        }
 
-       while (gspca_dev->present && gspca_dev->streaming) {
-               /* Need a short delay to ensure streaming flag was set by
-                * gspca and to make sure gspca can grab the mutex. */
-               mutex_unlock(&gspca_dev->usb_lock);
-               msleep(1);
+       frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
+                       + FRAME_HEADER_LEN;
 
+       while (gspca_dev->present && gspca_dev->streaming) {
                /* request some data and then read it until we have
                 * a complete frame. */
-               bytes_left = sq905_mode[0].sizeimage + FRAME_HEADER_LEN;
+               bytes_left = frame_sz;
                header_read = 0;
-               discarding = 0;
 
-               while (bytes_left > 0) {
+               /* Note we do not check for gspca_dev->streaming here, as
+                  we must finish reading an entire frame, otherwise the
+                  next time we stream we start reading in the middle of a
+                  frame. */
+               while (bytes_left > 0 && gspca_dev->present) {
                        data_len = bytes_left > SQ905_MAX_TRANSFER ?
                                SQ905_MAX_TRANSFER : bytes_left;
-                       mutex_lock(&gspca_dev->usb_lock);
-                       if (!gspca_dev->present)
-                               goto quit_stream;
-                       ret = sq905_read_data(gspca_dev, buffer, data_len);
+                       ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
                        if (ret < 0)
                                goto quit_stream;
-                       mutex_unlock(&gspca_dev->usb_lock);
-                       PDEBUG(D_STREAM,
+                       PDEBUG(D_PACK,
                                "Got %d bytes out of %d for frame",
                                data_len, bytes_left);
                        bytes_left -= data_len;
@@ -271,26 +266,30 @@ static void sq905_dostream(struct work_struct *work)
                        } else {
                                packet_type = INTER_PACKET;
                        }
-                       frame = gspca_get_i_frame(gspca_dev);
-                       if (frame && !discarding)
-                               gspca_frame_add(gspca_dev, packet_type,
-                                               frame, data, data_len);
-                       else
-                               discarding = 1;
+                       gspca_frame_add(gspca_dev, packet_type,
+                                       data, data_len);
+                       /* If entire frame fits in one packet we still
+                          need to add a LAST_PACKET */
+                       if (packet_type == FIRST_PACKET &&
+                           bytes_left == 0)
+                               gspca_frame_add(gspca_dev, LAST_PACKET,
+                                               NULL, 0);
+               }
+               if (gspca_dev->present) {
+                       /* acknowledge the frame */
+                       mutex_lock(&gspca_dev->usb_lock);
+                       ret = sq905_ack_frame(gspca_dev);
+                       mutex_unlock(&gspca_dev->usb_lock);
+                       if (ret < 0)
+                               goto quit_stream;
                }
-               /* acknowledge the frame */
-               mutex_lock(&gspca_dev->usb_lock);
-               if (!gspca_dev->present)
-                       goto quit_stream;
-               ret = sq905_ack_frame(gspca_dev);
-               if (ret < 0)
-                       goto quit_stream;
        }
 quit_stream:
-       /* the usb_lock is already acquired */
-       if (gspca_dev->present)
+       if (gspca_dev->present) {
+               mutex_lock(&gspca_dev->usb_lock);
                sq905_command(gspca_dev, SQ905_CLEAR);
-       mutex_unlock(&gspca_dev->usb_lock);
+               mutex_unlock(&gspca_dev->usb_lock);
+       }
        kfree(buffer);
 }
 
@@ -301,9 +300,8 @@ static int sd_config(struct gspca_dev *gspca_dev,
        struct cam *cam = &gspca_dev->cam;
        struct sd *dev = (struct sd *) gspca_dev;
 
-       cam->cam_mode = sq905_mode;
-       cam->nmodes = 1;
        /* We don't use the buffer gspca allocates so make it small. */
+       cam->bulk = 1;
        cam->bulk_size = 64;
 
        INIT_WORK(&dev->work_struct, sq905_dostream);
@@ -328,7 +326,6 @@ static void sd_stop0(struct gspca_dev *gspca_dev)
 /* this function is called at probe and resume time */
 static int sd_init(struct gspca_dev *gspca_dev)
 {
-       struct sd *dev = (struct sd *) gspca_dev;
        u32 ident;
        int ret;
 
@@ -341,20 +338,27 @@ static int sd_init(struct gspca_dev *gspca_dev)
        ret = sq905_command(gspca_dev, SQ905_ID);
        if (ret < 0)
                return ret;
-       ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4);
+       ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
        if (ret < 0)
                return ret;
-       /* usb_buf is allocated with kmalloc so is aligned. */
-       ident = le32_to_cpup((u32 *)gspca_dev->usb_buf);
+       /* usb_buf is allocated with kmalloc so is aligned.
+        * Camera model number is the right way round if we assume this
+        * reverse engineered ID is supposed to be big endian. */
+       ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
        ret = sq905_command(gspca_dev, SQ905_CLEAR);
        if (ret < 0)
                return ret;
-       dev->cam_type = 0;
-       while (dev->cam_type < ARRAY_SIZE(cam_types) - 1 &&
-              ident != cam_types[dev->cam_type].ident_word)
-               dev->cam_type++;
-       PDEBUG(D_CONF, "SQ905 camera %s, ID %08x detected",
-              cam_types[dev->cam_type].name, ident);
+       PDEBUG(D_CONF, "SQ905 camera ID %08x detected", ident);
+       gspca_dev->cam.cam_mode = sq905_mode;
+       gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
+       if (!(ident & SQ905_HIRES_MASK))
+               gspca_dev->cam.nmodes--;
+
+       if (ident & SQ905_ORIENTATION_MASK)
+               gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
+       else
+               gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
+                                            V4L2_IN_ST_HFLIP;
        return 0;
 }
 
@@ -365,12 +369,25 @@ static int sd_start(struct gspca_dev *gspca_dev)
        int ret;
 
        /* "Open the shutter" and set size, to start capture */
-       ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
+       switch (gspca_dev->curr_mode) {
+       default:
+/*     case 2: */
+               PDEBUG(D_STREAM, "Start streaming at high resolution");
+               ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
+               break;
+       case 1:
+               PDEBUG(D_STREAM, "Start streaming at medium resolution");
+               ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
+               break;
+       case 0:
+               PDEBUG(D_STREAM, "Start streaming at low resolution");
+               ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
+       }
+
        if (ret < 0) {
                PDEBUG(D_ERR, "Start streaming command failed");
                return ret;
        }
-
        /* Start the workqueue function to do the streaming */
        dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
        queue_work(dev->work_thread, &dev->work_struct);