Skip to content

Commit 181babf

Browse files
mgrzeschikgregkh
authored andcommitted
usb: gadget: uvc: also use try_format in set_format
Since e219a71 (usb: gadget: uvc: add v4l2 try_format api call) the try_format function is available. With this function includes checks for valid configurations programmed in the configfs. We use this function to ensure to return valid values on the set_format callback. Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de> Fixes: e219a71 ("usb: gadget: uvc: add v4l2 try_format api call") Link: https://lore.kernel.org/r/20221026182240.363055-1-m.grzeschik@pengutronix.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent eb70814 commit 181babf

File tree

1 file changed

+21
-51
lines changed

1 file changed

+21
-51
lines changed

drivers/usb/gadget/function/uvc_v4l2.c

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,6 @@ uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
199199
* V4L2 ioctls
200200
*/
201201

202-
struct uvc_format {
203-
u8 bpp;
204-
u32 fcc;
205-
};
206-
207-
static struct uvc_format uvc_formats[] = {
208-
{ 16, V4L2_PIX_FMT_YUYV },
209-
{ 0, V4L2_PIX_FMT_MJPEG },
210-
};
211-
212202
static int
213203
uvc_v4l2_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
214204
{
@@ -242,47 +232,6 @@ uvc_v4l2_get_format(struct file *file, void *fh, struct v4l2_format *fmt)
242232
return 0;
243233
}
244234

245-
static int
246-
uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
247-
{
248-
struct video_device *vdev = video_devdata(file);
249-
struct uvc_device *uvc = video_get_drvdata(vdev);
250-
struct uvc_video *video = &uvc->video;
251-
struct uvc_format *format;
252-
unsigned int imagesize;
253-
unsigned int bpl;
254-
unsigned int i;
255-
256-
for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
257-
format = &uvc_formats[i];
258-
if (format->fcc == fmt->fmt.pix.pixelformat)
259-
break;
260-
}
261-
262-
if (i == ARRAY_SIZE(uvc_formats)) {
263-
uvcg_info(&uvc->func, "Unsupported format 0x%08x.\n",
264-
fmt->fmt.pix.pixelformat);
265-
return -EINVAL;
266-
}
267-
268-
bpl = format->bpp * fmt->fmt.pix.width / 8;
269-
imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
270-
271-
video->fcc = format->fcc;
272-
video->bpp = format->bpp;
273-
video->width = fmt->fmt.pix.width;
274-
video->height = fmt->fmt.pix.height;
275-
video->imagesize = imagesize;
276-
277-
fmt->fmt.pix.field = V4L2_FIELD_NONE;
278-
fmt->fmt.pix.bytesperline = bpl;
279-
fmt->fmt.pix.sizeimage = imagesize;
280-
fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
281-
fmt->fmt.pix.priv = 0;
282-
283-
return 0;
284-
}
285-
286235
static int
287236
uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
288237
{
@@ -323,6 +272,27 @@ uvc_v4l2_try_format(struct file *file, void *fh, struct v4l2_format *fmt)
323272
return 0;
324273
}
325274

275+
static int
276+
uvc_v4l2_set_format(struct file *file, void *fh, struct v4l2_format *fmt)
277+
{
278+
struct video_device *vdev = video_devdata(file);
279+
struct uvc_device *uvc = video_get_drvdata(vdev);
280+
struct uvc_video *video = &uvc->video;
281+
int ret;
282+
283+
ret = uvc_v4l2_try_format(file, fh, fmt);
284+
if (ret)
285+
return ret;
286+
287+
video->fcc = fmt->fmt.pix.pixelformat;
288+
video->bpp = fmt->fmt.pix.bytesperline * 8 / video->width;
289+
video->width = fmt->fmt.pix.width;
290+
video->height = fmt->fmt.pix.height;
291+
video->imagesize = fmt->fmt.pix.sizeimage;
292+
293+
return ret;
294+
}
295+
326296
static int
327297
uvc_v4l2_enum_frameintervals(struct file *file, void *fh,
328298
struct v4l2_frmivalenum *fival)

0 commit comments

Comments
 (0)