Skip to content

Commit eee7d35

Browse files
committed
[media] v4l2-mc: add a routine to create USB media_device
Instead of copying exactly the same code on all USB devices, add an ancillary routine that will create and fill the struct media_device with the values imported from the USB device. Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent db15227 commit eee7d35

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

drivers/media/v4l2-core/v4l2-mc.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
#include <linux/module.h>
1818
#include <linux/pci.h>
19+
#include <linux/usb.h>
1920
#include <media/media-entity.h>
2021
#include <media/v4l2-mc.h>
2122

2223

2324
struct media_device *v4l2_mc_pci_media_device_init(struct pci_dev *pci_dev,
24-
char *name)
25+
const char *name)
2526
{
2627
#ifdef CONFIG_PCI
2728
struct media_device *mdev;
@@ -53,6 +54,44 @@ struct media_device *v4l2_mc_pci_media_device_init(struct pci_dev *pci_dev,
5354
}
5455
EXPORT_SYMBOL_GPL(v4l2_mc_pci_media_device_init);
5556

57+
struct media_device *__v4l2_mc_usb_media_device_init(struct usb_device *udev,
58+
const char *board_name,
59+
const char *driver_name)
60+
{
61+
#ifdef CONFIG_USB
62+
struct media_device *mdev;
63+
64+
mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
65+
if (!mdev)
66+
return NULL;
67+
68+
mdev->dev = &udev->dev;
69+
70+
if (driver_name)
71+
strlcpy(mdev->driver_name, driver_name,
72+
sizeof(mdev->driver_name));
73+
74+
if (board_name)
75+
strlcpy(mdev->model, board_name, sizeof(mdev->model));
76+
else if (udev->product)
77+
strlcpy(mdev->model, udev->product, sizeof(mdev->model));
78+
else
79+
strlcpy(mdev->model, "unknown model", sizeof(mdev->model));
80+
if (udev->serial)
81+
strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
82+
usb_make_path(udev, mdev->bus_info, sizeof(mdev->bus_info));
83+
mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
84+
mdev->driver_version = LINUX_VERSION_CODE;
85+
86+
media_device_init(mdev);
87+
88+
return mdev;
89+
#else
90+
return NULL;
91+
#endif
92+
}
93+
EXPORT_SYMBOL_GPL(__v4l2_mc_usb_media_device_init);
94+
5695
int v4l2_mc_create_media_graph(struct media_device *mdev)
5796

5897
{

include/media/v4l2-mc.h

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ enum demod_pad_index {
9595
DEMOD_NUM_PADS
9696
};
9797

98-
99-
struct pci_dev; /* We don't need to include pci.h here */
98+
/* We don't need to include pci.h or usb.h here */
99+
struct pci_dev;
100+
struct usb_device;
100101

101102
#ifdef CONFIG_MEDIA_CONTROLLER
102103
/**
@@ -124,20 +125,48 @@ int v4l2_mc_create_media_graph(struct media_device *mdev);
124125
* name for the pci device, given by pci_name() macro.
125126
*/
126127
struct media_device *v4l2_mc_pci_media_device_init(struct pci_dev *pci_dev,
127-
char *name);
128-
128+
const char *name);
129+
/**
130+
* __v4l2_mc_usb_media_device_init() - create and initialize a
131+
* struct &media_device from a PCI device.
132+
*
133+
* @udev: pointer to struct usb_device
134+
* @board_name: media device name. If %NULL, the routine will use the usb
135+
* product name, if available.
136+
* @driver_name: name of the driver. if %NULL, the routine will use the name
137+
* given by udev->dev->driver->name, with is usually the wrong
138+
* thing to do.
139+
*
140+
* NOTE: It is better to call v4l2_mc_usb_media_device_init() instead, as
141+
* such macro fills driver_name with %KBUILD_MODNAME.
142+
*/
143+
struct media_device *__v4l2_mc_usb_media_device_init(struct usb_device *udev,
144+
const char *board_name,
145+
const char *driver_name);
129146

130147
#else
131148
static inline int v4l2_mc_create_media_graph(struct media_device *mdev)
132149
{
133150
return 0;
134151
}
135152

153+
static inline
136154
struct media_device *v4l2_mc_pci_media_device_init(struct pci_dev *pci_dev,
137-
char *name) {
155+
char *name)
156+
{
138157
return NULL;
139158
}
140159

160+
static inline
161+
struct media_device *__v4l2_mc_usb_media_device_init(struct usb_device *udev,
162+
char *board_name,
163+
char *driver_name)
164+
{
165+
return NULL;
166+
}
141167
#endif
142168

169+
#define v4l2_mc_usb_media_device_init(udev, name) \
170+
__v4l2_mc_usb_media_device_init(udev, name, KBUILD_MODNAME)
171+
143172
#endif

0 commit comments

Comments
 (0)