Skip to content

Commit 6cb786f

Browse files
ribaldahverkuil
authored andcommitted
media: uvcvideo: Auto-set UVC_QUIRK_MSXU_META
If the camera supports the MSXU_CONTROL_METADATA control, auto set the MSXU_META quirk. Reviewed-by: Hans de Goede <[email protected]> Signed-off-by: Ricardo Ribalda <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Hans de Goede <[email protected]> Signed-off-by: Hans Verkuil <[email protected]>
1 parent 2ab4019 commit 6cb786f

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

drivers/media/usb/uvc/uvc_driver.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,12 @@ static int uvc_probe(struct usb_interface *intf,
22932293
goto error;
22942294
}
22952295

2296-
uvc_meta_init(dev);
2296+
ret = uvc_meta_init(dev);
2297+
if (ret < 0) {
2298+
dev_err(&dev->udev->dev,
2299+
"Error initializing the metadata formats (%d)\n", ret);
2300+
goto error;
2301+
}
22972302

22982303
if (dev->quirks & UVC_QUIRK_NO_RESET_RESUME)
22992304
udev->quirks &= ~USB_QUIRK_RESET_RESUME;

drivers/media/usb/uvc/uvc_metadata.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/list.h>
1111
#include <linux/module.h>
1212
#include <linux/usb.h>
13+
#include <linux/usb/uvc.h>
1314
#include <linux/videodev2.h>
1415

1516
#include <media/v4l2-ioctl.h>
@@ -166,6 +167,71 @@ static const struct v4l2_file_operations uvc_meta_fops = {
166167
.mmap = vb2_fop_mmap,
167168
};
168169

170+
static struct uvc_entity *uvc_meta_find_msxu(struct uvc_device *dev)
171+
{
172+
static const u8 uvc_msxu_guid[16] = UVC_GUID_MSXU_1_5;
173+
struct uvc_entity *entity;
174+
175+
list_for_each_entry(entity, &dev->entities, list) {
176+
if (!memcmp(entity->guid, uvc_msxu_guid, sizeof(entity->guid)))
177+
return entity;
178+
}
179+
180+
return NULL;
181+
}
182+
183+
#define MSXU_CONTROL_METADATA 0x9
184+
static int uvc_meta_detect_msxu(struct uvc_device *dev)
185+
{
186+
u32 *data __free(kfree) = NULL;
187+
struct uvc_entity *entity;
188+
int ret;
189+
190+
entity = uvc_meta_find_msxu(dev);
191+
if (!entity)
192+
return 0;
193+
194+
/*
195+
* USB requires buffers aligned in a special way, simplest way is to
196+
* make sure that query_ctrl will work is to kmalloc() them.
197+
*/
198+
data = kmalloc(sizeof(*data), GFP_KERNEL);
199+
if (!data)
200+
return -ENOMEM;
201+
202+
/* Check if the metadata is already enabled. */
203+
ret = uvc_query_ctrl(dev, UVC_GET_CUR, entity->id, dev->intfnum,
204+
MSXU_CONTROL_METADATA, data, sizeof(*data));
205+
if (ret)
206+
return 0;
207+
208+
if (*data) {
209+
dev->quirks |= UVC_QUIRK_MSXU_META;
210+
return 0;
211+
}
212+
213+
/*
214+
* We have seen devices that require 1 to enable the metadata, others
215+
* requiring a value != 1 and others requiring a value >1. Luckily for
216+
* us, the value from GET_MAX seems to work all the time.
217+
*/
218+
ret = uvc_query_ctrl(dev, UVC_GET_MAX, entity->id, dev->intfnum,
219+
MSXU_CONTROL_METADATA, data, sizeof(*data));
220+
if (ret || !*data)
221+
return 0;
222+
223+
/*
224+
* If we can set MSXU_CONTROL_METADATA, the device will report
225+
* metadata.
226+
*/
227+
ret = uvc_query_ctrl(dev, UVC_SET_CUR, entity->id, dev->intfnum,
228+
MSXU_CONTROL_METADATA, data, sizeof(*data));
229+
if (!ret)
230+
dev->quirks |= UVC_QUIRK_MSXU_META;
231+
232+
return 0;
233+
}
234+
169235
int uvc_meta_register(struct uvc_streaming *stream)
170236
{
171237
struct uvc_device *dev = stream->dev;
@@ -179,9 +245,14 @@ int uvc_meta_register(struct uvc_streaming *stream)
179245
&uvc_meta_fops, &uvc_meta_ioctl_ops);
180246
}
181247

182-
void uvc_meta_init(struct uvc_device *dev)
248+
int uvc_meta_init(struct uvc_device *dev)
183249
{
184250
unsigned int i = 0;
251+
int ret;
252+
253+
ret = uvc_meta_detect_msxu(dev);
254+
if (ret)
255+
return ret;
185256

186257
dev->meta_formats[i++] = V4L2_META_FMT_UVC;
187258

@@ -195,4 +266,6 @@ void uvc_meta_init(struct uvc_device *dev)
195266

196267
/* IMPORTANT: for new meta-formats update UVC_MAX_META_DATA_FORMATS. */
197268
dev->meta_formats[i++] = 0;
269+
270+
return 0;
198271
}

drivers/media/usb/uvc/uvcvideo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
728728
void uvc_video_clock_update(struct uvc_streaming *stream,
729729
struct vb2_v4l2_buffer *vbuf,
730730
struct uvc_buffer *buf);
731-
void uvc_meta_init(struct uvc_device *dev);
731+
int uvc_meta_init(struct uvc_device *dev);
732732
int uvc_meta_register(struct uvc_streaming *stream);
733733

734734
int uvc_register_video_device(struct uvc_device *dev,

include/linux/usb/uvc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#define UVC_GUID_EXT_GPIO_CONTROLLER \
3030
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
3131
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03}
32+
#define UVC_GUID_MSXU_1_5 \
33+
{0xdc, 0x95, 0x3f, 0x0f, 0x32, 0x26, 0x4e, 0x4c, \
34+
0x92, 0xc9, 0xa0, 0x47, 0x82, 0xf4, 0x3b, 0xc8}
3235

3336
#define UVC_GUID_FORMAT_MJPEG \
3437
{ 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \

0 commit comments

Comments
 (0)