From patchwork Wed Jun 22 10:15:18 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hans Petter Selasky X-Patchwork-Id: 904292 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p5MAGvok015281 for ; Wed, 22 Jun 2011 10:16:57 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756647Ab1FVKQz (ORCPT ); Wed, 22 Jun 2011 06:16:55 -0400 Received: from mailfe06.c2i.net ([212.247.154.162]:33503 "EHLO swip.net" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756646Ab1FVKQy convert rfc822-to-8bit (ORCPT ); Wed, 22 Jun 2011 06:16:54 -0400 X-Cloudmark-Score: 0.000000 [] X-Cloudmark-Analysis: v=1.1 cv=XR4AdwVFe5G+K9PtySS1/JOnv6WK/hruile8wX/SUjk= c=1 sm=1 a=SvYTsOw2Z4kA:10 a=OHBuAuDS3YsA:10 a=WQU8e4WWZSUA:10 a=kj9zAlcOel0A:10 a=CL8lFSKtTFcA:10 a=i9M/sDlu2rpZ9XS819oYzg==:17 a=8kQB0OdkAAAA:8 a=B6TPwNbD5Vdibiq_XHMA:9 a=CaI-t1dPX2lH7W1yXqMA:7 a=CjuIK1q_8ugA:10 a=9aOQ2cSd83gA:10 a=Zi7vWHg5UHX0chK0:21 a=Zt2GQ96Aq3rXK0xm:21 a=i9M/sDlu2rpZ9XS819oYzg==:117 Received: from [188.126.198.129] (account mc467741@c2i.net HELO laptop002.hselasky.homeunix.org) by mailfe06.swip.net (CommuniGate Pro SMTP 5.2.19) with ESMTPA id 142647963 for linux-media@vger.kernel.org; Wed, 22 Jun 2011 12:16:51 +0200 From: Hans Petter Selasky To: linux-media@vger.kernel.org Subject: [PATCH] Improve UVC buffering with regard to USB. Add checks to avoid division by zero. Date: Wed, 22 Jun 2011 12:15:18 +0200 User-Agent: KMail/1.13.5 (FreeBSD/8.2-STABLE; KDE/4.4.5; amd64; ; ) X-Face: *nPdTl_}RuAI6^PVpA02T?$%Xa^>@hE0uyUIoiha$pC:9TVgl.Oq,NwSZ4V" =?utf-8?q?=7CLR=2E+tj=7Dg5=0A=09=25V?=,x^qOs~mnU3]Gn; cQLv&.N>TrxmSFf+p6(30a/{)KUU!s}w\IhQBj}[g}bj0I3^glmC( =?utf-8?q?=0A=09=3AAuzV9=3A=2EhESm-x4h240C=609=3Dw?= MIME-Version: 1.0 Message-Id: <201106221215.18806.hselasky@c2i.net> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Wed, 22 Jun 2011 10:16:57 +0000 (UTC) Hi, In the UVC driver, the amount of buffering is controlled by the picture frame size divided by the USB packet size. This can sometimes lead to too small or too big buffers. The solution is to fix the buffer size with regard to the amount of buffering time: This patch will fix ISOCHRONOUS buffers around 2x16ms of buffering time. This should give around 1000 / 16 = 62 IRQ/s, which should suffice for up to 60 FPS unbuffered. If the frame rate goes beyond 60FPS it is likely that there will be data loss due to too small buffers. BULK buffers are fixed around 2x16K for non FULL speed and 2x4K for FULL speed USB, regardless of the frame size. This is a well known good combination for BULK buffering. --HPS From 9a9225cbfe22c978d3b92892580c2ca1e6f5abb2 Mon Sep 17 00:00:00 2001 From: Hans Petter Selasky Date: Wed, 22 Jun 2011 12:02:15 +0200 Subject: [PATCH] Improve UVC buffering with regard to USB. Add checks to avoid division by zero. Signed-off-by: Hans Petter Selasky --- drivers/media/video/uvc/uvc_video.c | 38 ++++++++++++++++++++++++++++++---- drivers/media/video/uvc/uvcvideo.h | 4 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/drivers/media/video/uvc/uvc_video.c b/drivers/media/video/uvc/uvc_video.c index fc766b9..dfc5978 100644 --- a/drivers/media/video/uvc/uvc_video.c +++ b/drivers/media/video/uvc/uvc_video.c @@ -823,9 +823,7 @@ static int uvc_alloc_urb_buffers(struct uvc_streaming *stream, /* Compute the number of packets. Bulk endpoints might transfer UVC * payloads across multiple URBs. */ - npackets = DIV_ROUND_UP(size, psize); - if (npackets > UVC_MAX_PACKETS) - npackets = UVC_MAX_PACKETS; + npackets = size / psize; /* Retry allocations until one succeed. */ for (; npackets > 1; npackets /= 2) { @@ -888,8 +886,25 @@ static int uvc_init_video_isoc(struct uvc_streaming *stream, u32 size; psize = le16_to_cpu(ep->desc.wMaxPacketSize); - psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); - size = stream->ctrl.dwMaxVideoFrameSize; + + if (stream->dev->udev->speed == USB_SPEED_FULL) { + /* (8000 >> 3) = 1000 FPS */ + psize = (psize & 0x07ff); + size = (UVC_MAX_PACKETS >> 3) * psize; + } else { + /* 1000 - 8000 FPS, figure out */ + psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); + size = ep->desc.bInterval; + if (size > 0) + size --; + if (size > 3) + size = 3; + size = (UVC_MAX_PACKETS >> size) * psize; + } + + /* avoid division by zero */ + if (psize == 0) + return -EINVAL; npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags); if (npackets == 0) @@ -943,6 +958,19 @@ static int uvc_init_video_bulk(struct uvc_streaming *stream, size = stream->ctrl.dwMaxPayloadTransferSize; stream->bulk.max_payload_size = size; + /* avoid division by zero */ + if (psize == 0) + return -EINVAL; + + /* roughly compute size for buffers */ + if (stream->dev->udev->speed == USB_SPEED_FULL) { + size = 4096; + } else { + size = 16384; + } + /* align to packet boundary */ + size += (psize - (size % psize)) % psize; + npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags); if (npackets == 0) return -ENOMEM; diff --git a/drivers/media/video/uvc/uvcvideo.h b/drivers/media/video/uvc/uvcvideo.h index 45f01e7..2adbaf2 100644 --- a/drivers/media/video/uvc/uvcvideo.h +++ b/drivers/media/video/uvc/uvcvideo.h @@ -161,9 +161,9 @@ struct uvc_xu_control { #define DRIVER_VERSION "v1.0.0" /* Number of isochronous URBs. */ -#define UVC_URBS 5 +#define UVC_URBS 2U /* Maximum number of packets per URB. */ -#define UVC_MAX_PACKETS 32 +#define UVC_MAX_PACKETS 128U /* at 8000 FPS */ /* Maximum number of video buffers. */ #define UVC_MAX_VIDEO_BUFFERS 32 /* Maximum status buffer size in bytes of interrupt URB. */