blob: 306006419f383ab006958de29faf1864831da722 [file] [log] [blame]
Laurent Pincharta99141272010-05-02 20:57:42 +02001/*
2 * webcam.c -- USB webcam gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Laurent Pincharta99141272010-05-02 20:57:42 +020011 */
Klaus Schwarzkopf28c9fc62011-09-09 16:10:44 +020012
Laurent Pincharta99141272010-05-02 20:57:42 +020013#include <linux/kernel.h>
14#include <linux/device.h>
15#include <linux/usb/video.h>
16
17#include "f_uvc.h"
18
19/*
20 * Kbuild is not very cooperative with respect to linking separately
21 * compiled library objects into one module. So for now we won't use
22 * separate compilation ... ensuring init/exit sections work to shrink
23 * the runtime footprint, and giving us at least some parts of what
24 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
25 */
26#include "composite.c"
Laurent Pincharta99141272010-05-02 20:57:42 +020027
Laurent Pincharta99141272010-05-02 20:57:42 +020028#include "uvc_queue.c"
Laurent Pincharta99141272010-05-02 20:57:42 +020029#include "uvc_video.c"
Laurent Pinchart5d9955f2010-07-10 16:13:05 -030030#include "uvc_v4l2.c"
31#include "f_uvc.c"
Laurent Pincharta99141272010-05-02 20:57:42 +020032
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020033USB_GADGET_COMPOSITE_OPTIONS();
Laurent Pincharta99141272010-05-02 20:57:42 +020034/* --------------------------------------------------------------------------
35 * Device descriptor
36 */
37
38#define WEBCAM_VENDOR_ID 0x1d6b /* Linux Foundation */
39#define WEBCAM_PRODUCT_ID 0x0102 /* Webcam A/V gadget */
40#define WEBCAM_DEVICE_BCD 0x0010 /* 0.10 */
41
42static char webcam_vendor_label[] = "Linux Foundation";
43static char webcam_product_label[] = "Webcam gadget";
44static char webcam_config_label[] = "Video";
45
46/* string IDs are assigned dynamically */
47
48#define STRING_MANUFACTURER_IDX 0
49#define STRING_PRODUCT_IDX 1
50#define STRING_DESCRIPTION_IDX 2
51
52static struct usb_string webcam_strings[] = {
53 [STRING_MANUFACTURER_IDX].s = webcam_vendor_label,
54 [STRING_PRODUCT_IDX].s = webcam_product_label,
55 [STRING_DESCRIPTION_IDX].s = webcam_config_label,
56 { }
57};
58
59static struct usb_gadget_strings webcam_stringtab = {
60 .language = 0x0409, /* en-us */
61 .strings = webcam_strings,
62};
63
64static struct usb_gadget_strings *webcam_device_strings[] = {
65 &webcam_stringtab,
66 NULL,
67};
68
69static struct usb_device_descriptor webcam_device_descriptor = {
70 .bLength = USB_DT_DEVICE_SIZE,
71 .bDescriptorType = USB_DT_DEVICE,
72 .bcdUSB = cpu_to_le16(0x0200),
73 .bDeviceClass = USB_CLASS_MISC,
74 .bDeviceSubClass = 0x02,
75 .bDeviceProtocol = 0x01,
76 .bMaxPacketSize0 = 0, /* dynamic */
77 .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID),
78 .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID),
79 .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD),
80 .iManufacturer = 0, /* dynamic */
81 .iProduct = 0, /* dynamic */
82 .iSerialNumber = 0, /* dynamic */
83 .bNumConfigurations = 0, /* dynamic */
84};
85
86DECLARE_UVC_HEADER_DESCRIPTOR(1);
87
88static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = {
89 .bLength = UVC_DT_HEADER_SIZE(1),
90 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030091 .bDescriptorSubType = UVC_VC_HEADER,
Laurent Pincharta99141272010-05-02 20:57:42 +020092 .bcdUVC = cpu_to_le16(0x0100),
93 .wTotalLength = 0, /* dynamic */
94 .dwClockFrequency = cpu_to_le32(48000000),
95 .bInCollection = 0, /* dynamic */
96 .baInterfaceNr[0] = 0, /* dynamic */
97};
98
99static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = {
100 .bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3),
101 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300102 .bDescriptorSubType = UVC_VC_INPUT_TERMINAL,
Laurent Pincharta99141272010-05-02 20:57:42 +0200103 .bTerminalID = 1,
104 .wTerminalType = cpu_to_le16(0x0201),
105 .bAssocTerminal = 0,
106 .iTerminal = 0,
107 .wObjectiveFocalLengthMin = cpu_to_le16(0),
108 .wObjectiveFocalLengthMax = cpu_to_le16(0),
109 .wOcularFocalLength = cpu_to_le16(0),
110 .bControlSize = 3,
111 .bmControls[0] = 2,
112 .bmControls[1] = 0,
113 .bmControls[2] = 0,
114};
115
116static const struct uvc_processing_unit_descriptor uvc_processing = {
117 .bLength = UVC_DT_PROCESSING_UNIT_SIZE(2),
118 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300119 .bDescriptorSubType = UVC_VC_PROCESSING_UNIT,
Laurent Pincharta99141272010-05-02 20:57:42 +0200120 .bUnitID = 2,
121 .bSourceID = 1,
122 .wMaxMultiplier = cpu_to_le16(16*1024),
123 .bControlSize = 2,
124 .bmControls[0] = 1,
125 .bmControls[1] = 0,
126 .iProcessing = 0,
127};
128
129static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
130 .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300132 .bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL,
Laurent Pincharta99141272010-05-02 20:57:42 +0200133 .bTerminalID = 3,
134 .wTerminalType = cpu_to_le16(0x0101),
135 .bAssocTerminal = 0,
136 .bSourceID = 2,
137 .iTerminal = 0,
138};
139
140DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
141
142static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
143 .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2),
144 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300145 .bDescriptorSubType = UVC_VS_INPUT_HEADER,
Laurent Pincharta99141272010-05-02 20:57:42 +0200146 .bNumFormats = 2,
147 .wTotalLength = 0, /* dynamic */
148 .bEndpointAddress = 0, /* dynamic */
149 .bmInfo = 0,
150 .bTerminalLink = 3,
151 .bStillCaptureMethod = 0,
152 .bTriggerSupport = 0,
153 .bTriggerUsage = 0,
154 .bControlSize = 1,
155 .bmaControls[0][0] = 0,
156 .bmaControls[1][0] = 4,
157};
158
159static const struct uvc_format_uncompressed uvc_format_yuv = {
160 .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
161 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300162 .bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED,
Laurent Pincharta99141272010-05-02 20:57:42 +0200163 .bFormatIndex = 1,
164 .bNumFrameDescriptors = 2,
165 .guidFormat =
166 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
167 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
168 .bBitsPerPixel = 16,
169 .bDefaultFrameIndex = 1,
170 .bAspectRatioX = 0,
171 .bAspectRatioY = 0,
172 .bmInterfaceFlags = 0,
173 .bCopyProtect = 0,
174};
175
176DECLARE_UVC_FRAME_UNCOMPRESSED(1);
177DECLARE_UVC_FRAME_UNCOMPRESSED(3);
178
179static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
180 .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
181 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300182 .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
Laurent Pincharta99141272010-05-02 20:57:42 +0200183 .bFrameIndex = 1,
184 .bmCapabilities = 0,
185 .wWidth = cpu_to_le16(640),
186 .wHeight = cpu_to_le16(360),
187 .dwMinBitRate = cpu_to_le32(18432000),
188 .dwMaxBitRate = cpu_to_le32(55296000),
189 .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
190 .dwDefaultFrameInterval = cpu_to_le32(666666),
191 .bFrameIntervalType = 3,
192 .dwFrameInterval[0] = cpu_to_le32(666666),
193 .dwFrameInterval[1] = cpu_to_le32(1000000),
194 .dwFrameInterval[2] = cpu_to_le32(5000000),
195};
196
197static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
198 .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
199 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300200 .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
Laurent Pincharta99141272010-05-02 20:57:42 +0200201 .bFrameIndex = 2,
202 .bmCapabilities = 0,
203 .wWidth = cpu_to_le16(1280),
204 .wHeight = cpu_to_le16(720),
205 .dwMinBitRate = cpu_to_le32(29491200),
206 .dwMaxBitRate = cpu_to_le32(29491200),
207 .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
208 .dwDefaultFrameInterval = cpu_to_le32(5000000),
209 .bFrameIntervalType = 1,
210 .dwFrameInterval[0] = cpu_to_le32(5000000),
211};
212
213static const struct uvc_format_mjpeg uvc_format_mjpg = {
214 .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
215 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300216 .bDescriptorSubType = UVC_VS_FORMAT_MJPEG,
Laurent Pincharta99141272010-05-02 20:57:42 +0200217 .bFormatIndex = 2,
218 .bNumFrameDescriptors = 2,
219 .bmFlags = 0,
220 .bDefaultFrameIndex = 1,
221 .bAspectRatioX = 0,
222 .bAspectRatioY = 0,
223 .bmInterfaceFlags = 0,
224 .bCopyProtect = 0,
225};
226
227DECLARE_UVC_FRAME_MJPEG(1);
228DECLARE_UVC_FRAME_MJPEG(3);
229
230static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
231 .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
232 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300233 .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
Laurent Pincharta99141272010-05-02 20:57:42 +0200234 .bFrameIndex = 1,
235 .bmCapabilities = 0,
236 .wWidth = cpu_to_le16(640),
237 .wHeight = cpu_to_le16(360),
238 .dwMinBitRate = cpu_to_le32(18432000),
239 .dwMaxBitRate = cpu_to_le32(55296000),
240 .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
241 .dwDefaultFrameInterval = cpu_to_le32(666666),
242 .bFrameIntervalType = 3,
243 .dwFrameInterval[0] = cpu_to_le32(666666),
244 .dwFrameInterval[1] = cpu_to_le32(1000000),
245 .dwFrameInterval[2] = cpu_to_le32(5000000),
246};
247
248static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
249 .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
250 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300251 .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
Laurent Pincharta99141272010-05-02 20:57:42 +0200252 .bFrameIndex = 2,
253 .bmCapabilities = 0,
254 .wWidth = cpu_to_le16(1280),
255 .wHeight = cpu_to_le16(720),
256 .dwMinBitRate = cpu_to_le32(29491200),
257 .dwMaxBitRate = cpu_to_le32(29491200),
258 .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
259 .dwDefaultFrameInterval = cpu_to_le32(5000000),
260 .bFrameIntervalType = 1,
261 .dwFrameInterval[0] = cpu_to_le32(5000000),
262};
263
264static const struct uvc_color_matching_descriptor uvc_color_matching = {
265 .bLength = UVC_DT_COLOR_MATCHING_SIZE,
266 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300267 .bDescriptorSubType = UVC_VS_COLORFORMAT,
Laurent Pincharta99141272010-05-02 20:57:42 +0200268 .bColorPrimaries = 1,
269 .bTransferCharacteristics = 1,
270 .bMatrixCoefficients = 4,
271};
272
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530273static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
274 (const struct uvc_descriptor_header *) &uvc_control_header,
275 (const struct uvc_descriptor_header *) &uvc_camera_terminal,
276 (const struct uvc_descriptor_header *) &uvc_processing,
277 (const struct uvc_descriptor_header *) &uvc_output_terminal,
278 NULL,
279};
280
281static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
Laurent Pincharta99141272010-05-02 20:57:42 +0200282 (const struct uvc_descriptor_header *) &uvc_control_header,
283 (const struct uvc_descriptor_header *) &uvc_camera_terminal,
284 (const struct uvc_descriptor_header *) &uvc_processing,
285 (const struct uvc_descriptor_header *) &uvc_output_terminal,
286 NULL,
287};
288
289static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
290 (const struct uvc_descriptor_header *) &uvc_input_header,
291 (const struct uvc_descriptor_header *) &uvc_format_yuv,
292 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
293 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
294 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
295 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
296 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
297 (const struct uvc_descriptor_header *) &uvc_color_matching,
298 NULL,
299};
300
301static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
302 (const struct uvc_descriptor_header *) &uvc_input_header,
303 (const struct uvc_descriptor_header *) &uvc_format_yuv,
304 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
305 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
306 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
307 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
308 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
309 (const struct uvc_descriptor_header *) &uvc_color_matching,
310 NULL,
311};
312
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530313static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
314 (const struct uvc_descriptor_header *) &uvc_input_header,
315 (const struct uvc_descriptor_header *) &uvc_format_yuv,
316 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
317 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
318 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
319 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
320 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
321 (const struct uvc_descriptor_header *) &uvc_color_matching,
322 NULL,
323};
324
Laurent Pincharta99141272010-05-02 20:57:42 +0200325/* --------------------------------------------------------------------------
326 * USB configuration
327 */
328
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200329static int __init
Laurent Pincharta99141272010-05-02 20:57:42 +0200330webcam_config_bind(struct usb_configuration *c)
331{
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530332 return uvc_bind_config(c, uvc_fs_control_cls, uvc_ss_control_cls,
333 uvc_fs_streaming_cls, uvc_hs_streaming_cls,
334 uvc_ss_streaming_cls);
Laurent Pincharta99141272010-05-02 20:57:42 +0200335}
336
337static struct usb_configuration webcam_config_driver = {
338 .label = webcam_config_label,
Laurent Pincharta99141272010-05-02 20:57:42 +0200339 .bConfigurationValue = 1,
340 .iConfiguration = 0, /* dynamic */
341 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
342 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
343};
344
345static int /* __init_or_exit */
346webcam_unbind(struct usb_composite_dev *cdev)
347{
348 return 0;
349}
350
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200351static int __init
Laurent Pincharta99141272010-05-02 20:57:42 +0200352webcam_bind(struct usb_composite_dev *cdev)
353{
354 int ret;
355
356 /* Allocate string descriptor numbers ... note that string contents
357 * can be overridden by the composite_dev glue.
358 */
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200359 ret = usb_string_ids_tab(cdev, webcam_strings);
360 if (ret < 0)
Laurent Pincharta99141272010-05-02 20:57:42 +0200361 goto error;
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200362 webcam_device_descriptor.iManufacturer =
363 webcam_strings[STRING_MANUFACTURER_IDX].id;
364 webcam_device_descriptor.iProduct =
365 webcam_strings[STRING_PRODUCT_IDX].id;
366 webcam_config_driver.iConfiguration =
367 webcam_strings[STRING_DESCRIPTION_IDX].id;
Laurent Pincharta99141272010-05-02 20:57:42 +0200368
369 /* Register our configuration. */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200370 if ((ret = usb_add_config(cdev, &webcam_config_driver,
371 webcam_config_bind)) < 0)
Laurent Pincharta99141272010-05-02 20:57:42 +0200372 goto error;
373
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200374 usb_composite_overwrite_options(cdev, &coverwrite);
Laurent Pincharta99141272010-05-02 20:57:42 +0200375 INFO(cdev, "Webcam Video Gadget\n");
376 return 0;
377
378error:
379 webcam_unbind(cdev);
380 return ret;
381}
382
383/* --------------------------------------------------------------------------
384 * Driver
385 */
386
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200387static __refdata struct usb_composite_driver webcam_driver = {
Laurent Pincharta99141272010-05-02 20:57:42 +0200388 .name = "g_webcam",
389 .dev = &webcam_device_descriptor,
390 .strings = webcam_device_strings,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530391 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200392 .bind = webcam_bind,
Laurent Pincharta99141272010-05-02 20:57:42 +0200393 .unbind = webcam_unbind,
394};
395
396static int __init
397webcam_init(void)
398{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200399 return usb_composite_probe(&webcam_driver);
Laurent Pincharta99141272010-05-02 20:57:42 +0200400}
401
402static void __exit
403webcam_cleanup(void)
404{
405 usb_composite_unregister(&webcam_driver);
406}
407
408module_init(webcam_init);
409module_exit(webcam_cleanup);
410
411MODULE_AUTHOR("Laurent Pinchart");
412MODULE_DESCRIPTION("Webcam Video Gadget");
413MODULE_LICENSE("GPL");
414MODULE_VERSION("0.1.0");
415