xref: /OK3568_Linux_fs/kernel/drivers/usb/gadget/legacy/webcam.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	webcam.c -- USB webcam gadget driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	Copyright (C) 2009-2010
6*4882a593Smuzhiyun  *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/usb/video.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "u_uvc.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun USB_GADGET_COMPOSITE_OPTIONS();
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /* module parameters specific to the Video streaming endpoint */
21*4882a593Smuzhiyun static unsigned int streaming_interval = 1;
22*4882a593Smuzhiyun module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
23*4882a593Smuzhiyun MODULE_PARM_DESC(streaming_interval, "1 - 16");
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun static unsigned int streaming_maxpacket = 1024;
26*4882a593Smuzhiyun module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
27*4882a593Smuzhiyun MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static unsigned int streaming_maxburst;
30*4882a593Smuzhiyun module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
31*4882a593Smuzhiyun MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* --------------------------------------------------------------------------
34*4882a593Smuzhiyun  * Device descriptor
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define WEBCAM_VENDOR_ID		0x1d6b	/* Linux Foundation */
38*4882a593Smuzhiyun #define WEBCAM_PRODUCT_ID		0x0102	/* Webcam A/V gadget */
39*4882a593Smuzhiyun #define WEBCAM_DEVICE_BCD		0x0010	/* 0.10 */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static char webcam_vendor_label[] = "Linux Foundation";
42*4882a593Smuzhiyun static char webcam_product_label[] = "Webcam gadget";
43*4882a593Smuzhiyun static char webcam_config_label[] = "Video";
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /* string IDs are assigned dynamically */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define STRING_DESCRIPTION_IDX		USB_GADGET_FIRST_AVAIL_IDX
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static struct usb_string webcam_strings[] = {
50*4882a593Smuzhiyun 	[USB_GADGET_MANUFACTURER_IDX].s = webcam_vendor_label,
51*4882a593Smuzhiyun 	[USB_GADGET_PRODUCT_IDX].s = webcam_product_label,
52*4882a593Smuzhiyun 	[USB_GADGET_SERIAL_IDX].s = "",
53*4882a593Smuzhiyun 	[STRING_DESCRIPTION_IDX].s = webcam_config_label,
54*4882a593Smuzhiyun 	{  }
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun static struct usb_gadget_strings webcam_stringtab = {
58*4882a593Smuzhiyun 	.language = 0x0409,	/* en-us */
59*4882a593Smuzhiyun 	.strings = webcam_strings,
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun static struct usb_gadget_strings *webcam_device_strings[] = {
63*4882a593Smuzhiyun 	&webcam_stringtab,
64*4882a593Smuzhiyun 	NULL,
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static struct usb_function_instance *fi_uvc;
68*4882a593Smuzhiyun static struct usb_function *f_uvc;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun static struct usb_device_descriptor webcam_device_descriptor = {
71*4882a593Smuzhiyun 	.bLength		= USB_DT_DEVICE_SIZE,
72*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_DEVICE,
73*4882a593Smuzhiyun 	/* .bcdUSB = DYNAMIC */
74*4882a593Smuzhiyun 	.bDeviceClass		= USB_CLASS_MISC,
75*4882a593Smuzhiyun 	.bDeviceSubClass	= 0x02,
76*4882a593Smuzhiyun 	.bDeviceProtocol	= 0x01,
77*4882a593Smuzhiyun 	.bMaxPacketSize0	= 0, /* dynamic */
78*4882a593Smuzhiyun 	.idVendor		= cpu_to_le16(WEBCAM_VENDOR_ID),
79*4882a593Smuzhiyun 	.idProduct		= cpu_to_le16(WEBCAM_PRODUCT_ID),
80*4882a593Smuzhiyun 	.bcdDevice		= cpu_to_le16(WEBCAM_DEVICE_BCD),
81*4882a593Smuzhiyun 	.iManufacturer		= 0, /* dynamic */
82*4882a593Smuzhiyun 	.iProduct		= 0, /* dynamic */
83*4882a593Smuzhiyun 	.iSerialNumber		= 0, /* dynamic */
84*4882a593Smuzhiyun 	.bNumConfigurations	= 0, /* dynamic */
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun DECLARE_UVC_HEADER_DESCRIPTOR(1);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = {
90*4882a593Smuzhiyun 	.bLength		= UVC_DT_HEADER_SIZE(1),
91*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
92*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VC_HEADER,
93*4882a593Smuzhiyun 	.bcdUVC			= cpu_to_le16(0x0100),
94*4882a593Smuzhiyun 	.wTotalLength		= 0, /* dynamic */
95*4882a593Smuzhiyun 	.dwClockFrequency	= cpu_to_le32(48000000),
96*4882a593Smuzhiyun 	.bInCollection		= 0, /* dynamic */
97*4882a593Smuzhiyun 	.baInterfaceNr[0]	= 0, /* dynamic */
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = {
101*4882a593Smuzhiyun 	.bLength		= UVC_DT_CAMERA_TERMINAL_SIZE(3),
102*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
103*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VC_INPUT_TERMINAL,
104*4882a593Smuzhiyun 	.bTerminalID		= 1,
105*4882a593Smuzhiyun 	.wTerminalType		= cpu_to_le16(0x0201),
106*4882a593Smuzhiyun 	.bAssocTerminal		= 0,
107*4882a593Smuzhiyun 	.iTerminal		= 0,
108*4882a593Smuzhiyun 	.wObjectiveFocalLengthMin	= cpu_to_le16(0),
109*4882a593Smuzhiyun 	.wObjectiveFocalLengthMax	= cpu_to_le16(0),
110*4882a593Smuzhiyun 	.wOcularFocalLength		= cpu_to_le16(0),
111*4882a593Smuzhiyun 	.bControlSize		= 3,
112*4882a593Smuzhiyun 	.bmControls[0]		= 2,
113*4882a593Smuzhiyun 	.bmControls[1]		= 0,
114*4882a593Smuzhiyun 	.bmControls[2]		= 0,
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun static const struct uvc_processing_unit_descriptor uvc_processing = {
118*4882a593Smuzhiyun 	.bLength		= UVC_DT_PROCESSING_UNIT_SIZE(2),
119*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
120*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VC_PROCESSING_UNIT,
121*4882a593Smuzhiyun 	.bUnitID		= 2,
122*4882a593Smuzhiyun 	.bSourceID		= 1,
123*4882a593Smuzhiyun 	.wMaxMultiplier		= cpu_to_le16(16*1024),
124*4882a593Smuzhiyun 	.bControlSize		= 2,
125*4882a593Smuzhiyun 	.bmControls[0]		= 1,
126*4882a593Smuzhiyun 	.bmControls[1]		= 0,
127*4882a593Smuzhiyun 	.iProcessing		= 0,
128*4882a593Smuzhiyun 	.bmVideoStandards	= 0,
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
132*4882a593Smuzhiyun 	.bLength		= UVC_DT_OUTPUT_TERMINAL_SIZE,
133*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
134*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VC_OUTPUT_TERMINAL,
135*4882a593Smuzhiyun 	.bTerminalID		= 3,
136*4882a593Smuzhiyun 	.wTerminalType		= cpu_to_le16(0x0101),
137*4882a593Smuzhiyun 	.bAssocTerminal		= 0,
138*4882a593Smuzhiyun 	.bSourceID		= 2,
139*4882a593Smuzhiyun 	.iTerminal		= 0,
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
145*4882a593Smuzhiyun 	.bLength		= UVC_DT_INPUT_HEADER_SIZE(1, 2),
146*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
147*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_INPUT_HEADER,
148*4882a593Smuzhiyun 	.bNumFormats		= 2,
149*4882a593Smuzhiyun 	.wTotalLength		= 0, /* dynamic */
150*4882a593Smuzhiyun 	.bEndpointAddress	= 0, /* dynamic */
151*4882a593Smuzhiyun 	.bmInfo			= 0,
152*4882a593Smuzhiyun 	.bTerminalLink		= 3,
153*4882a593Smuzhiyun 	.bStillCaptureMethod	= 0,
154*4882a593Smuzhiyun 	.bTriggerSupport	= 0,
155*4882a593Smuzhiyun 	.bTriggerUsage		= 0,
156*4882a593Smuzhiyun 	.bControlSize		= 1,
157*4882a593Smuzhiyun 	.bmaControls[0][0]	= 0,
158*4882a593Smuzhiyun 	.bmaControls[1][0]	= 4,
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun static const struct uvc_format_uncompressed uvc_format_yuv = {
162*4882a593Smuzhiyun 	.bLength		= UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
163*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
164*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_FORMAT_UNCOMPRESSED,
165*4882a593Smuzhiyun 	.bFormatIndex		= 1,
166*4882a593Smuzhiyun 	.bNumFrameDescriptors	= 2,
167*4882a593Smuzhiyun 	.guidFormat		=
168*4882a593Smuzhiyun 		{ 'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00,
169*4882a593Smuzhiyun 		 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
170*4882a593Smuzhiyun 	.bBitsPerPixel		= 16,
171*4882a593Smuzhiyun 	.bDefaultFrameIndex	= 1,
172*4882a593Smuzhiyun 	.bAspectRatioX		= 0,
173*4882a593Smuzhiyun 	.bAspectRatioY		= 0,
174*4882a593Smuzhiyun 	.bmInterfaceFlags	= 0,
175*4882a593Smuzhiyun 	.bCopyProtect		= 0,
176*4882a593Smuzhiyun };
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun DECLARE_UVC_FRAME_UNCOMPRESSED(1);
179*4882a593Smuzhiyun DECLARE_UVC_FRAME_UNCOMPRESSED(3);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
182*4882a593Smuzhiyun 	.bLength		= UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
183*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
184*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_FRAME_UNCOMPRESSED,
185*4882a593Smuzhiyun 	.bFrameIndex		= 1,
186*4882a593Smuzhiyun 	.bmCapabilities		= 0,
187*4882a593Smuzhiyun 	.wWidth			= cpu_to_le16(640),
188*4882a593Smuzhiyun 	.wHeight		= cpu_to_le16(360),
189*4882a593Smuzhiyun 	.dwMinBitRate		= cpu_to_le32(18432000),
190*4882a593Smuzhiyun 	.dwMaxBitRate		= cpu_to_le32(55296000),
191*4882a593Smuzhiyun 	.dwMaxVideoFrameBufferSize	= cpu_to_le32(460800),
192*4882a593Smuzhiyun 	.dwDefaultFrameInterval	= cpu_to_le32(666666),
193*4882a593Smuzhiyun 	.bFrameIntervalType	= 3,
194*4882a593Smuzhiyun 	.dwFrameInterval[0]	= cpu_to_le32(666666),
195*4882a593Smuzhiyun 	.dwFrameInterval[1]	= cpu_to_le32(1000000),
196*4882a593Smuzhiyun 	.dwFrameInterval[2]	= cpu_to_le32(5000000),
197*4882a593Smuzhiyun };
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
200*4882a593Smuzhiyun 	.bLength		= UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
201*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
202*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_FRAME_UNCOMPRESSED,
203*4882a593Smuzhiyun 	.bFrameIndex		= 2,
204*4882a593Smuzhiyun 	.bmCapabilities		= 0,
205*4882a593Smuzhiyun 	.wWidth			= cpu_to_le16(1280),
206*4882a593Smuzhiyun 	.wHeight		= cpu_to_le16(720),
207*4882a593Smuzhiyun 	.dwMinBitRate		= cpu_to_le32(29491200),
208*4882a593Smuzhiyun 	.dwMaxBitRate		= cpu_to_le32(29491200),
209*4882a593Smuzhiyun 	.dwMaxVideoFrameBufferSize	= cpu_to_le32(1843200),
210*4882a593Smuzhiyun 	.dwDefaultFrameInterval	= cpu_to_le32(5000000),
211*4882a593Smuzhiyun 	.bFrameIntervalType	= 1,
212*4882a593Smuzhiyun 	.dwFrameInterval[0]	= cpu_to_le32(5000000),
213*4882a593Smuzhiyun };
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun static const struct uvc_format_mjpeg uvc_format_mjpg = {
216*4882a593Smuzhiyun 	.bLength		= UVC_DT_FORMAT_MJPEG_SIZE,
217*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
218*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_FORMAT_MJPEG,
219*4882a593Smuzhiyun 	.bFormatIndex		= 2,
220*4882a593Smuzhiyun 	.bNumFrameDescriptors	= 2,
221*4882a593Smuzhiyun 	.bmFlags		= 0,
222*4882a593Smuzhiyun 	.bDefaultFrameIndex	= 1,
223*4882a593Smuzhiyun 	.bAspectRatioX		= 0,
224*4882a593Smuzhiyun 	.bAspectRatioY		= 0,
225*4882a593Smuzhiyun 	.bmInterfaceFlags	= 0,
226*4882a593Smuzhiyun 	.bCopyProtect		= 0,
227*4882a593Smuzhiyun };
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun DECLARE_UVC_FRAME_MJPEG(1);
230*4882a593Smuzhiyun DECLARE_UVC_FRAME_MJPEG(3);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
233*4882a593Smuzhiyun 	.bLength		= UVC_DT_FRAME_MJPEG_SIZE(3),
234*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
235*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_FRAME_MJPEG,
236*4882a593Smuzhiyun 	.bFrameIndex		= 1,
237*4882a593Smuzhiyun 	.bmCapabilities		= 0,
238*4882a593Smuzhiyun 	.wWidth			= cpu_to_le16(640),
239*4882a593Smuzhiyun 	.wHeight		= cpu_to_le16(360),
240*4882a593Smuzhiyun 	.dwMinBitRate		= cpu_to_le32(18432000),
241*4882a593Smuzhiyun 	.dwMaxBitRate		= cpu_to_le32(55296000),
242*4882a593Smuzhiyun 	.dwMaxVideoFrameBufferSize	= cpu_to_le32(460800),
243*4882a593Smuzhiyun 	.dwDefaultFrameInterval	= cpu_to_le32(666666),
244*4882a593Smuzhiyun 	.bFrameIntervalType	= 3,
245*4882a593Smuzhiyun 	.dwFrameInterval[0]	= cpu_to_le32(666666),
246*4882a593Smuzhiyun 	.dwFrameInterval[1]	= cpu_to_le32(1000000),
247*4882a593Smuzhiyun 	.dwFrameInterval[2]	= cpu_to_le32(5000000),
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
251*4882a593Smuzhiyun 	.bLength		= UVC_DT_FRAME_MJPEG_SIZE(1),
252*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
253*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_FRAME_MJPEG,
254*4882a593Smuzhiyun 	.bFrameIndex		= 2,
255*4882a593Smuzhiyun 	.bmCapabilities		= 0,
256*4882a593Smuzhiyun 	.wWidth			= cpu_to_le16(1280),
257*4882a593Smuzhiyun 	.wHeight		= cpu_to_le16(720),
258*4882a593Smuzhiyun 	.dwMinBitRate		= cpu_to_le32(29491200),
259*4882a593Smuzhiyun 	.dwMaxBitRate		= cpu_to_le32(29491200),
260*4882a593Smuzhiyun 	.dwMaxVideoFrameBufferSize	= cpu_to_le32(1843200),
261*4882a593Smuzhiyun 	.dwDefaultFrameInterval	= cpu_to_le32(5000000),
262*4882a593Smuzhiyun 	.bFrameIntervalType	= 1,
263*4882a593Smuzhiyun 	.dwFrameInterval[0]	= cpu_to_le32(5000000),
264*4882a593Smuzhiyun };
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun static const struct uvc_color_matching_descriptor uvc_color_matching = {
267*4882a593Smuzhiyun 	.bLength		= UVC_DT_COLOR_MATCHING_SIZE,
268*4882a593Smuzhiyun 	.bDescriptorType	= USB_DT_CS_INTERFACE,
269*4882a593Smuzhiyun 	.bDescriptorSubType	= UVC_VS_COLORFORMAT,
270*4882a593Smuzhiyun 	.bColorPrimaries	= 1,
271*4882a593Smuzhiyun 	.bTransferCharacteristics	= 1,
272*4882a593Smuzhiyun 	.bMatrixCoefficients	= 4,
273*4882a593Smuzhiyun };
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
276*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_control_header,
277*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_camera_terminal,
278*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_processing,
279*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_output_terminal,
280*4882a593Smuzhiyun 	NULL,
281*4882a593Smuzhiyun };
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
284*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_control_header,
285*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_camera_terminal,
286*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_processing,
287*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_output_terminal,
288*4882a593Smuzhiyun 	NULL,
289*4882a593Smuzhiyun };
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
292*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_input_header,
293*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_format_yuv,
294*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
295*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
296*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_format_mjpg,
297*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
298*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
299*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_color_matching,
300*4882a593Smuzhiyun 	NULL,
301*4882a593Smuzhiyun };
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
304*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_input_header,
305*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_format_yuv,
306*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
307*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
308*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_format_mjpg,
309*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
310*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
311*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_color_matching,
312*4882a593Smuzhiyun 	NULL,
313*4882a593Smuzhiyun };
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
316*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_input_header,
317*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_format_yuv,
318*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
319*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
320*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_format_mjpg,
321*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
322*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
323*4882a593Smuzhiyun 	(const struct uvc_descriptor_header *) &uvc_color_matching,
324*4882a593Smuzhiyun 	NULL,
325*4882a593Smuzhiyun };
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun /* --------------------------------------------------------------------------
328*4882a593Smuzhiyun  * USB configuration
329*4882a593Smuzhiyun  */
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun static int
webcam_config_bind(struct usb_configuration * c)332*4882a593Smuzhiyun webcam_config_bind(struct usb_configuration *c)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	int status = 0;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	f_uvc = usb_get_function(fi_uvc);
337*4882a593Smuzhiyun 	if (IS_ERR(f_uvc))
338*4882a593Smuzhiyun 		return PTR_ERR(f_uvc);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	status = usb_add_function(c, f_uvc);
341*4882a593Smuzhiyun 	if (status < 0)
342*4882a593Smuzhiyun 		usb_put_function(f_uvc);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	return status;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun static struct usb_configuration webcam_config_driver = {
348*4882a593Smuzhiyun 	.label			= webcam_config_label,
349*4882a593Smuzhiyun 	.bConfigurationValue	= 1,
350*4882a593Smuzhiyun 	.iConfiguration		= 0, /* dynamic */
351*4882a593Smuzhiyun 	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
352*4882a593Smuzhiyun 	.MaxPower		= CONFIG_USB_GADGET_VBUS_DRAW,
353*4882a593Smuzhiyun };
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun static int
webcam_unbind(struct usb_composite_dev * cdev)356*4882a593Smuzhiyun webcam_unbind(struct usb_composite_dev *cdev)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(f_uvc))
359*4882a593Smuzhiyun 		usb_put_function(f_uvc);
360*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(fi_uvc))
361*4882a593Smuzhiyun 		usb_put_function_instance(fi_uvc);
362*4882a593Smuzhiyun 	return 0;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun static int
webcam_bind(struct usb_composite_dev * cdev)366*4882a593Smuzhiyun webcam_bind(struct usb_composite_dev *cdev)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	struct f_uvc_opts *uvc_opts;
369*4882a593Smuzhiyun 	int ret;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	fi_uvc = usb_get_function_instance("uvc");
372*4882a593Smuzhiyun 	if (IS_ERR(fi_uvc))
373*4882a593Smuzhiyun 		return PTR_ERR(fi_uvc);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	uvc_opts = container_of(fi_uvc, struct f_uvc_opts, func_inst);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	uvc_opts->streaming_interval = streaming_interval;
378*4882a593Smuzhiyun 	uvc_opts->streaming_maxpacket = streaming_maxpacket;
379*4882a593Smuzhiyun 	uvc_opts->streaming_maxburst = streaming_maxburst;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	uvc_opts->fs_control = uvc_fs_control_cls;
382*4882a593Smuzhiyun 	uvc_opts->ss_control = uvc_ss_control_cls;
383*4882a593Smuzhiyun 	uvc_opts->fs_streaming = uvc_fs_streaming_cls;
384*4882a593Smuzhiyun 	uvc_opts->hs_streaming = uvc_hs_streaming_cls;
385*4882a593Smuzhiyun 	uvc_opts->ss_streaming = uvc_ss_streaming_cls;
386*4882a593Smuzhiyun 	uvc_opts->pm_qos_latency = 0;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	/* Allocate string descriptor numbers ... note that string contents
389*4882a593Smuzhiyun 	 * can be overridden by the composite_dev glue.
390*4882a593Smuzhiyun 	 */
391*4882a593Smuzhiyun 	ret = usb_string_ids_tab(cdev, webcam_strings);
392*4882a593Smuzhiyun 	if (ret < 0)
393*4882a593Smuzhiyun 		goto error;
394*4882a593Smuzhiyun 	webcam_device_descriptor.iManufacturer =
395*4882a593Smuzhiyun 		webcam_strings[USB_GADGET_MANUFACTURER_IDX].id;
396*4882a593Smuzhiyun 	webcam_device_descriptor.iProduct =
397*4882a593Smuzhiyun 		webcam_strings[USB_GADGET_PRODUCT_IDX].id;
398*4882a593Smuzhiyun 	webcam_config_driver.iConfiguration =
399*4882a593Smuzhiyun 		webcam_strings[STRING_DESCRIPTION_IDX].id;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	/* Register our configuration. */
402*4882a593Smuzhiyun 	if ((ret = usb_add_config(cdev, &webcam_config_driver,
403*4882a593Smuzhiyun 					webcam_config_bind)) < 0)
404*4882a593Smuzhiyun 		goto error;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	usb_composite_overwrite_options(cdev, &coverwrite);
407*4882a593Smuzhiyun 	INFO(cdev, "Webcam Video Gadget\n");
408*4882a593Smuzhiyun 	return 0;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun error:
411*4882a593Smuzhiyun 	usb_put_function_instance(fi_uvc);
412*4882a593Smuzhiyun 	return ret;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun /* --------------------------------------------------------------------------
416*4882a593Smuzhiyun  * Driver
417*4882a593Smuzhiyun  */
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun static struct usb_composite_driver webcam_driver = {
420*4882a593Smuzhiyun 	.name		= "g_webcam",
421*4882a593Smuzhiyun 	.dev		= &webcam_device_descriptor,
422*4882a593Smuzhiyun 	.strings	= webcam_device_strings,
423*4882a593Smuzhiyun 	.max_speed	= USB_SPEED_SUPER,
424*4882a593Smuzhiyun 	.bind		= webcam_bind,
425*4882a593Smuzhiyun 	.unbind		= webcam_unbind,
426*4882a593Smuzhiyun };
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun module_usb_composite_driver(webcam_driver);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun MODULE_AUTHOR("Laurent Pinchart");
431*4882a593Smuzhiyun MODULE_DESCRIPTION("Webcam Video Gadget");
432*4882a593Smuzhiyun MODULE_LICENSE("GPL");
433*4882a593Smuzhiyun 
434