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