blob: 66dfca86b21593059c2570eda95d9ffa5516b6fa [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
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
47#define STRING_MANUFACTURER_IDX 0
48#define STRING_PRODUCT_IDX 1
49#define STRING_DESCRIPTION_IDX 2
50
51static struct usb_string webcam_strings[] = {
52 [STRING_MANUFACTURER_IDX].s = webcam_vendor_label,
53 [STRING_PRODUCT_IDX].s = webcam_product_label,
54 [STRING_DESCRIPTION_IDX].s = webcam_config_label,
55 { }
56};
57
58static struct usb_gadget_strings webcam_stringtab = {
59 .language = 0x0409, /* en-us */
60 .strings = webcam_strings,
61};
62
63static struct usb_gadget_strings *webcam_device_strings[] = {
64 &webcam_stringtab,
65 NULL,
66};
67
68static struct usb_device_descriptor webcam_device_descriptor = {
69 .bLength = USB_DT_DEVICE_SIZE,
70 .bDescriptorType = USB_DT_DEVICE,
71 .bcdUSB = cpu_to_le16(0x0200),
72 .bDeviceClass = USB_CLASS_MISC,
73 .bDeviceSubClass = 0x02,
74 .bDeviceProtocol = 0x01,
75 .bMaxPacketSize0 = 0, /* dynamic */
76 .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID),
77 .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID),
78 .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD),
79 .iManufacturer = 0, /* dynamic */
80 .iProduct = 0, /* dynamic */
81 .iSerialNumber = 0, /* dynamic */
82 .bNumConfigurations = 0, /* dynamic */
83};
84
85DECLARE_UVC_HEADER_DESCRIPTOR(1);
86
87static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = {
88 .bLength = UVC_DT_HEADER_SIZE(1),
89 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -030090 .bDescriptorSubType = UVC_VC_HEADER,
Laurent Pincharta99141272010-05-02 20:57:42 +020091 .bcdUVC = cpu_to_le16(0x0100),
92 .wTotalLength = 0, /* dynamic */
93 .dwClockFrequency = cpu_to_le32(48000000),
94 .bInCollection = 0, /* dynamic */
95 .baInterfaceNr[0] = 0, /* dynamic */
96};
97
98static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = {
99 .bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3),
100 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300101 .bDescriptorSubType = UVC_VC_INPUT_TERMINAL,
Laurent Pincharta99141272010-05-02 20:57:42 +0200102 .bTerminalID = 1,
103 .wTerminalType = cpu_to_le16(0x0201),
104 .bAssocTerminal = 0,
105 .iTerminal = 0,
106 .wObjectiveFocalLengthMin = cpu_to_le16(0),
107 .wObjectiveFocalLengthMax = cpu_to_le16(0),
108 .wOcularFocalLength = cpu_to_le16(0),
109 .bControlSize = 3,
110 .bmControls[0] = 2,
111 .bmControls[1] = 0,
112 .bmControls[2] = 0,
113};
114
115static const struct uvc_processing_unit_descriptor uvc_processing = {
116 .bLength = UVC_DT_PROCESSING_UNIT_SIZE(2),
117 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300118 .bDescriptorSubType = UVC_VC_PROCESSING_UNIT,
Laurent Pincharta99141272010-05-02 20:57:42 +0200119 .bUnitID = 2,
120 .bSourceID = 1,
121 .wMaxMultiplier = cpu_to_le16(16*1024),
122 .bControlSize = 2,
123 .bmControls[0] = 1,
124 .bmControls[1] = 0,
125 .iProcessing = 0,
126};
127
128static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
129 .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE,
130 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300131 .bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL,
Laurent Pincharta99141272010-05-02 20:57:42 +0200132 .bTerminalID = 3,
133 .wTerminalType = cpu_to_le16(0x0101),
134 .bAssocTerminal = 0,
135 .bSourceID = 2,
136 .iTerminal = 0,
137};
138
139DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
140
141static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
142 .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2),
143 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300144 .bDescriptorSubType = UVC_VS_INPUT_HEADER,
Laurent Pincharta99141272010-05-02 20:57:42 +0200145 .bNumFormats = 2,
146 .wTotalLength = 0, /* dynamic */
147 .bEndpointAddress = 0, /* dynamic */
148 .bmInfo = 0,
149 .bTerminalLink = 3,
150 .bStillCaptureMethod = 0,
151 .bTriggerSupport = 0,
152 .bTriggerUsage = 0,
153 .bControlSize = 1,
154 .bmaControls[0][0] = 0,
155 .bmaControls[1][0] = 4,
156};
157
158static const struct uvc_format_uncompressed uvc_format_yuv = {
159 .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
160 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300161 .bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED,
Laurent Pincharta99141272010-05-02 20:57:42 +0200162 .bFormatIndex = 1,
163 .bNumFrameDescriptors = 2,
164 .guidFormat =
165 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
166 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
167 .bBitsPerPixel = 16,
168 .bDefaultFrameIndex = 1,
169 .bAspectRatioX = 0,
170 .bAspectRatioY = 0,
171 .bmInterfaceFlags = 0,
172 .bCopyProtect = 0,
173};
174
175DECLARE_UVC_FRAME_UNCOMPRESSED(1);
176DECLARE_UVC_FRAME_UNCOMPRESSED(3);
177
178static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
179 .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
180 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300181 .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
Laurent Pincharta99141272010-05-02 20:57:42 +0200182 .bFrameIndex = 1,
183 .bmCapabilities = 0,
184 .wWidth = cpu_to_le16(640),
185 .wHeight = cpu_to_le16(360),
186 .dwMinBitRate = cpu_to_le32(18432000),
187 .dwMaxBitRate = cpu_to_le32(55296000),
188 .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
189 .dwDefaultFrameInterval = cpu_to_le32(666666),
190 .bFrameIntervalType = 3,
191 .dwFrameInterval[0] = cpu_to_le32(666666),
192 .dwFrameInterval[1] = cpu_to_le32(1000000),
193 .dwFrameInterval[2] = cpu_to_le32(5000000),
194};
195
196static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
197 .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
198 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300199 .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
Laurent Pincharta99141272010-05-02 20:57:42 +0200200 .bFrameIndex = 2,
201 .bmCapabilities = 0,
202 .wWidth = cpu_to_le16(1280),
203 .wHeight = cpu_to_le16(720),
204 .dwMinBitRate = cpu_to_le32(29491200),
205 .dwMaxBitRate = cpu_to_le32(29491200),
206 .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
207 .dwDefaultFrameInterval = cpu_to_le32(5000000),
208 .bFrameIntervalType = 1,
209 .dwFrameInterval[0] = cpu_to_le32(5000000),
210};
211
212static const struct uvc_format_mjpeg uvc_format_mjpg = {
213 .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
214 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300215 .bDescriptorSubType = UVC_VS_FORMAT_MJPEG,
Laurent Pincharta99141272010-05-02 20:57:42 +0200216 .bFormatIndex = 2,
217 .bNumFrameDescriptors = 2,
218 .bmFlags = 0,
219 .bDefaultFrameIndex = 1,
220 .bAspectRatioX = 0,
221 .bAspectRatioY = 0,
222 .bmInterfaceFlags = 0,
223 .bCopyProtect = 0,
224};
225
226DECLARE_UVC_FRAME_MJPEG(1);
227DECLARE_UVC_FRAME_MJPEG(3);
228
229static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
230 .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
231 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300232 .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
Laurent Pincharta99141272010-05-02 20:57:42 +0200233 .bFrameIndex = 1,
234 .bmCapabilities = 0,
235 .wWidth = cpu_to_le16(640),
236 .wHeight = cpu_to_le16(360),
237 .dwMinBitRate = cpu_to_le32(18432000),
238 .dwMaxBitRate = cpu_to_le32(55296000),
239 .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
240 .dwDefaultFrameInterval = cpu_to_le32(666666),
241 .bFrameIntervalType = 3,
242 .dwFrameInterval[0] = cpu_to_le32(666666),
243 .dwFrameInterval[1] = cpu_to_le32(1000000),
244 .dwFrameInterval[2] = cpu_to_le32(5000000),
245};
246
247static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
248 .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
249 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300250 .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
Laurent Pincharta99141272010-05-02 20:57:42 +0200251 .bFrameIndex = 2,
252 .bmCapabilities = 0,
253 .wWidth = cpu_to_le16(1280),
254 .wHeight = cpu_to_le16(720),
255 .dwMinBitRate = cpu_to_le32(29491200),
256 .dwMaxBitRate = cpu_to_le32(29491200),
257 .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
258 .dwDefaultFrameInterval = cpu_to_le32(5000000),
259 .bFrameIntervalType = 1,
260 .dwFrameInterval[0] = cpu_to_le32(5000000),
261};
262
263static const struct uvc_color_matching_descriptor uvc_color_matching = {
264 .bLength = UVC_DT_COLOR_MATCHING_SIZE,
265 .bDescriptorType = USB_DT_CS_INTERFACE,
Laurent Pinchartbbafc0c2010-07-10 15:03:20 -0300266 .bDescriptorSubType = UVC_VS_COLORFORMAT,
Laurent Pincharta99141272010-05-02 20:57:42 +0200267 .bColorPrimaries = 1,
268 .bTransferCharacteristics = 1,
269 .bMatrixCoefficients = 4,
270};
271
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530272static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
273 (const struct uvc_descriptor_header *) &uvc_control_header,
274 (const struct uvc_descriptor_header *) &uvc_camera_terminal,
275 (const struct uvc_descriptor_header *) &uvc_processing,
276 (const struct uvc_descriptor_header *) &uvc_output_terminal,
277 NULL,
278};
279
280static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
Laurent Pincharta99141272010-05-02 20:57:42 +0200281 (const struct uvc_descriptor_header *) &uvc_control_header,
282 (const struct uvc_descriptor_header *) &uvc_camera_terminal,
283 (const struct uvc_descriptor_header *) &uvc_processing,
284 (const struct uvc_descriptor_header *) &uvc_output_terminal,
285 NULL,
286};
287
288static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
289 (const struct uvc_descriptor_header *) &uvc_input_header,
290 (const struct uvc_descriptor_header *) &uvc_format_yuv,
291 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
292 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
293 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
294 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
295 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
296 (const struct uvc_descriptor_header *) &uvc_color_matching,
297 NULL,
298};
299
300static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
301 (const struct uvc_descriptor_header *) &uvc_input_header,
302 (const struct uvc_descriptor_header *) &uvc_format_yuv,
303 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
304 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
305 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
306 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
307 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
308 (const struct uvc_descriptor_header *) &uvc_color_matching,
309 NULL,
310};
311
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530312static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
313 (const struct uvc_descriptor_header *) &uvc_input_header,
314 (const struct uvc_descriptor_header *) &uvc_format_yuv,
315 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
316 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
317 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
318 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
319 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
320 (const struct uvc_descriptor_header *) &uvc_color_matching,
321 NULL,
322};
323
Laurent Pincharta99141272010-05-02 20:57:42 +0200324/* --------------------------------------------------------------------------
325 * USB configuration
326 */
327
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200328static int __init
Laurent Pincharta99141272010-05-02 20:57:42 +0200329webcam_config_bind(struct usb_configuration *c)
330{
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530331 return uvc_bind_config(c, uvc_fs_control_cls, uvc_ss_control_cls,
332 uvc_fs_streaming_cls, uvc_hs_streaming_cls,
333 uvc_ss_streaming_cls);
Laurent Pincharta99141272010-05-02 20:57:42 +0200334}
335
336static struct usb_configuration webcam_config_driver = {
337 .label = webcam_config_label,
Laurent Pincharta99141272010-05-02 20:57:42 +0200338 .bConfigurationValue = 1,
339 .iConfiguration = 0, /* dynamic */
340 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
341 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
342};
343
344static int /* __init_or_exit */
345webcam_unbind(struct usb_composite_dev *cdev)
346{
347 return 0;
348}
349
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200350static int __init
Laurent Pincharta99141272010-05-02 20:57:42 +0200351webcam_bind(struct usb_composite_dev *cdev)
352{
353 int ret;
354
355 /* Allocate string descriptor numbers ... note that string contents
356 * can be overridden by the composite_dev glue.
357 */
358 if ((ret = usb_string_id(cdev)) < 0)
359 goto error;
360 webcam_strings[STRING_MANUFACTURER_IDX].id = ret;
361 webcam_device_descriptor.iManufacturer = ret;
362
363 if ((ret = usb_string_id(cdev)) < 0)
364 goto error;
365 webcam_strings[STRING_PRODUCT_IDX].id = ret;
366 webcam_device_descriptor.iProduct = ret;
367
368 if ((ret = usb_string_id(cdev)) < 0)
369 goto error;
370 webcam_strings[STRING_DESCRIPTION_IDX].id = ret;
371 webcam_config_driver.iConfiguration = ret;
372
373 /* Register our configuration. */
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200374 if ((ret = usb_add_config(cdev, &webcam_config_driver,
375 webcam_config_bind)) < 0)
Laurent Pincharta99141272010-05-02 20:57:42 +0200376 goto error;
377
378 INFO(cdev, "Webcam Video Gadget\n");
379 return 0;
380
381error:
382 webcam_unbind(cdev);
383 return ret;
384}
385
386/* --------------------------------------------------------------------------
387 * Driver
388 */
389
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200390static __refdata struct usb_composite_driver webcam_driver = {
Laurent Pincharta99141272010-05-02 20:57:42 +0200391 .name = "g_webcam",
392 .dev = &webcam_device_descriptor,
393 .strings = webcam_device_strings,
Bhupesh Sharmafbcaba02012-06-01 15:08:56 +0530394 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200395 .bind = webcam_bind,
Laurent Pincharta99141272010-05-02 20:57:42 +0200396 .unbind = webcam_unbind,
397};
398
399static int __init
400webcam_init(void)
401{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200402 return usb_composite_probe(&webcam_driver);
Laurent Pincharta99141272010-05-02 20:57:42 +0200403}
404
405static void __exit
406webcam_cleanup(void)
407{
408 usb_composite_unregister(&webcam_driver);
409}
410
411module_init(webcam_init);
412module_exit(webcam_cleanup);
413
414MODULE_AUTHOR("Laurent Pinchart");
415MODULE_DESCRIPTION("Webcam Video Gadget");
416MODULE_LICENSE("GPL");
417MODULE_VERSION("0.1.0");
418