include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / uvc / uvc_video.c
index acbd73a..821a996 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/usb.h>
 #include <linux/videodev2.h>
 #include <linux/vmalloc.h>
@@ -90,7 +91,8 @@ static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
                ctrl->dwMaxVideoFrameSize =
                        frame->dwMaxVideoFrameBufferSize;
 
-       if (stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
+       if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
+           stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
            stream->intf->num_altsetting > 1) {
                u32 interval;
                u32 bandwidth;
@@ -124,16 +126,17 @@ static int uvc_get_video_ctrl(struct uvc_streaming *stream,
        int ret;
 
        size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
+       if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
+                       query == UVC_GET_DEF)
+               return -EIO;
+
        data = kmalloc(size, GFP_KERNEL);
        if (data == NULL)
                return -ENOMEM;
 
-       if ((video->dev->quirks & UVC_QUIRK_PROBE_DEF) && query == UVC_GET_DEF)
-               return -EIO;
-
        ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
                probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
-               size, UVC_CTRL_STREAMING_TIMEOUT);
+               size, uvc_timeout_param);
 
        if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
                /* Some cameras, mostly based on Bison Electronics chipsets,
@@ -143,7 +146,7 @@ static int uvc_get_video_ctrl(struct uvc_streaming *stream,
                uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
                        "compliance - GET_MIN/MAX(PROBE) incorrectly "
                        "supported. Enabling workaround.\n");
-               memset(ctrl, 0, sizeof ctrl);
+               memset(ctrl, 0, sizeof *ctrl);
                ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
                ret = 0;
                goto out;
@@ -237,7 +240,7 @@ static int uvc_set_video_ctrl(struct uvc_streaming *stream,
 
        ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
                probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
-               size, UVC_CTRL_STREAMING_TIMEOUT);
+               size, uvc_timeout_param);
        if (ret != size) {
                uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
                        "%d (exp. %u).\n", probe ? "probe" : "commit",
@@ -408,6 +411,8 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
         * when the EOF bit is set to force synchronisation on the next packet.
         */
        if (buf->state != UVC_BUF_STATE_ACTIVE) {
+               struct timespec ts;
+
                if (fid == stream->last_fid) {
                        uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
                                "sync).\n");
@@ -417,6 +422,14 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
                        return -ENODATA;
                }
 
+               if (uvc_clock_param == CLOCK_MONOTONIC)
+                       ktime_get_ts(&ts);
+               else
+                       ktime_get_real_ts(&ts);
+
+               buf->buf.timestamp.tv_sec = ts.tv_sec;
+               buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
+
                /* TODO: Handle PTS and SCR. */
                buf->state = UVC_BUF_STATE_ACTIVE;
        }
@@ -439,7 +452,7 @@ static int uvc_video_decode_start(struct uvc_streaming *stream,
        if (fid != stream->last_fid && buf->buf.bytesused != 0) {
                uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
                                "toggled).\n");
-               buf->state = UVC_BUF_STATE_DONE;
+               buf->state = UVC_BUF_STATE_READY;
                return -EAGAIN;
        }
 
@@ -468,7 +481,7 @@ static void uvc_video_decode_data(struct uvc_streaming *stream,
        /* Complete the current frame if the buffer size was exceeded. */
        if (len > maxlen) {
                uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
-               buf->state = UVC_BUF_STATE_DONE;
+               buf->state = UVC_BUF_STATE_READY;
        }
 }
 
@@ -480,7 +493,7 @@ static void uvc_video_decode_end(struct uvc_streaming *stream,
                uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
                if (data[0] == len)
                        uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
-               buf->state = UVC_BUF_STATE_DONE;
+               buf->state = UVC_BUF_STATE_READY;
                if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
                        stream->last_fid ^= UVC_STREAM_FID;
        }
@@ -566,8 +579,7 @@ static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
                uvc_video_decode_end(stream, buf, mem,
                        urb->iso_frame_desc[i].actual_length);
 
-               if (buf->state == UVC_BUF_STATE_DONE ||
-                   buf->state == UVC_BUF_STATE_ERROR)
+               if (buf->state == UVC_BUF_STATE_READY)
                        buf = uvc_queue_next_buffer(&stream->queue, buf);
        }
 }
@@ -625,8 +637,7 @@ static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
                if (!stream->bulk.skip_payload && buf != NULL) {
                        uvc_video_decode_end(stream, buf, stream->bulk.header,
                                stream->bulk.payload_size);
-                       if (buf->state == UVC_BUF_STATE_DONE ||
-                           buf->state == UVC_BUF_STATE_ERROR)
+                       if (buf->state == UVC_BUF_STATE_READY)
                                buf = uvc_queue_next_buffer(&stream->queue,
                                                            buf);
                }
@@ -667,7 +678,7 @@ static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
            stream->bulk.payload_size == stream->bulk.max_payload_size) {
                if (buf->buf.bytesused == stream->queue.buf_used) {
                        stream->queue.buf_used = 0;
-                       buf->state = UVC_BUF_STATE_DONE;
+                       buf->state = UVC_BUF_STATE_READY;
                        uvc_queue_next_buffer(&stream->queue, buf);
                        stream->last_fid ^= UVC_STREAM_FID;
                }
@@ -768,8 +779,9 @@ static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
        /* Retry allocations until one succeed. */
        for (; npackets > 1; npackets /= 2) {
                for (i = 0; i < UVC_URBS; ++i) {
+                       stream->urb_size = psize * npackets;
                        stream->urb_buffer[i] = usb_buffer_alloc(
-                               stream->dev->udev, psize * npackets,
+                               stream->dev->udev, stream->urb_size,
                                gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
                        if (!stream->urb_buffer[i]) {
                                uvc_free_urb_buffers(stream);
@@ -778,11 +790,15 @@ static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
                }
 
                if (i == UVC_URBS) {
-                       stream->urb_size = psize * npackets;
+                       uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
+                               "of %ux%u bytes each.\n", UVC_URBS, npackets,
+                               psize);
                        return npackets;
                }
        }
 
+       uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
+               "per packet).\n", psize);
        return 0;
 }
 
@@ -917,10 +933,8 @@ static int uvc_init_video_bulk(struct uvc_streaming *stream,
 static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
 {
        struct usb_interface *intf = stream->intf;
-       struct usb_host_interface *alts;
-       struct usb_host_endpoint *ep = NULL;
-       int intfnum = stream->intfnum;
-       unsigned int bandwidth, psize, i;
+       struct usb_host_endpoint *ep;
+       unsigned int i;
        int ret;
 
        stream->last_fid = -1;
@@ -929,17 +943,28 @@ static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
        stream->bulk.payload_size = 0;
 
        if (intf->num_altsetting > 1) {
+               struct usb_host_endpoint *best_ep = NULL;
+               unsigned int best_psize = 3 * 1024;
+               unsigned int bandwidth;
+               unsigned int uninitialized_var(altsetting);
+               int intfnum = stream->intfnum;
+
                /* Isochronous endpoint, select the alternate setting. */
                bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
 
                if (bandwidth == 0) {
-                       uvc_printk(KERN_WARNING, "device %s requested null "
-                               "bandwidth, defaulting to lowest.\n",
-                               stream->dev->name);
+                       uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
+                               "bandwidth, defaulting to lowest.\n");
                        bandwidth = 1;
+               } else {
+                       uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
+                               "B/frame bandwidth.\n", bandwidth);
                }
 
                for (i = 0; i < intf->num_altsetting; ++i) {
+                       struct usb_host_interface *alts;
+                       unsigned int psize;
+
                        alts = &intf->altsetting[i];
                        ep = uvc_find_endpoint(alts,
                                stream->header.bEndpointAddress);
@@ -949,18 +974,27 @@ static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
                        /* Check if the bandwidth is high enough. */
                        psize = le16_to_cpu(ep->desc.wMaxPacketSize);
                        psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
-                       if (psize >= bandwidth)
-                               break;
+                       if (psize >= bandwidth && psize <= best_psize) {
+                               altsetting = i;
+                               best_psize = psize;
+                               best_ep = ep;
+                       }
                }
 
-               if (i >= intf->num_altsetting)
+               if (best_ep == NULL) {
+                       uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
+                               "for requested bandwidth.\n");
                        return -EIO;
+               }
+
+               uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
+                       "(%u B/frame bandwidth).\n", altsetting, best_psize);
 
-               ret = usb_set_interface(stream->dev->udev, intfnum, i);
+               ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
                if (ret < 0)
                        return ret;
 
-               ret = uvc_init_video_isoc(stream, ep, gfp_flags);
+               ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
        } else {
                /* Bulk endpoint, proceed to URB initialization. */
                ep = uvc_find_endpoint(&intf->altsetting[0],