Skip to content

Commit c824345

Browse files
ribaldaHans Verkuil
authored andcommitted
media: uvcvideo: Pass allocation size directly to uvc_alloc_urb_buffer
The uvc_alloc_urb_buffer() function implicitly depended on the stream->urb_size field, which was set by its caller, uvc_alloc_urb_buffers(). This implicit data flow makes the code harder to follow. More importantly, stream->urb_size was updated within the allocation loop before the allocation was confirmed to be successful. If the allocation failed, the stream object would be left with a urb_size that doesn't correspond to valid, allocated URB buffers. Refactor uvc_alloc_urb_buffer() to accept the buffer size as an explicit argument. This makes the function's dependencies clear and improves the robustness of the error handling path. The stream->urb_size is now set only after a complete and successful allocation. This is a pure refactoring and introduces no functional changes. Signed-off-by: Ricardo Ribalda <[email protected]> Reviewed-by: Laurent Pinchart <[email protected]> Tested-by: Itay Chamiel <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Laurent Pinchart <[email protected]> Signed-off-by: Hans Verkuil <[email protected]>
1 parent 40d3ac2 commit c824345

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

drivers/media/usb/uvc/uvc_video.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,12 +1771,13 @@ static void uvc_free_urb_buffers(struct uvc_streaming *stream)
17711771
}
17721772

17731773
static bool uvc_alloc_urb_buffer(struct uvc_streaming *stream,
1774-
struct uvc_urb *uvc_urb, gfp_t gfp_flags)
1774+
struct uvc_urb *uvc_urb, unsigned int size,
1775+
gfp_t gfp_flags)
17751776
{
17761777
struct usb_device *udev = stream->dev->udev;
17771778

1778-
uvc_urb->buffer = usb_alloc_noncoherent(udev, stream->urb_size,
1779-
gfp_flags, &uvc_urb->dma,
1779+
uvc_urb->buffer = usb_alloc_noncoherent(udev, size, gfp_flags,
1780+
&uvc_urb->dma,
17801781
uvc_stream_dir(stream),
17811782
&uvc_urb->sgt);
17821783
return !!uvc_urb->buffer;
@@ -1813,12 +1814,13 @@ static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
18131814

18141815
/* Retry allocations until one succeed. */
18151816
for (; npackets > 0; npackets /= 2) {
1816-
stream->urb_size = psize * npackets;
1817+
unsigned int urb_size = psize * npackets;
18171818

18181819
for (i = 0; i < UVC_URBS; ++i) {
18191820
struct uvc_urb *uvc_urb = &stream->uvc_urb[i];
18201821

1821-
if (!uvc_alloc_urb_buffer(stream, uvc_urb, gfp_flags)) {
1822+
if (!uvc_alloc_urb_buffer(stream, uvc_urb, urb_size,
1823+
gfp_flags)) {
18221824
uvc_free_urb_buffers(stream);
18231825
break;
18241826
}
@@ -1830,14 +1832,14 @@ static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
18301832
uvc_dbg(stream->dev, VIDEO,
18311833
"Allocated %u URB buffers of %ux%u bytes each\n",
18321834
UVC_URBS, npackets, psize);
1835+
stream->urb_size = urb_size;
18331836
return npackets;
18341837
}
18351838
}
18361839

18371840
uvc_dbg(stream->dev, VIDEO,
18381841
"Failed to allocate URB buffers (%u bytes per packet)\n",
18391842
psize);
1840-
stream->urb_size = 0;
18411843
return 0;
18421844
}
18431845

0 commit comments

Comments
 (0)