Commit | Line | Data |
---|---|---|
a9914127 LP |
1 | /* |
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. | |
a9914127 | 11 | */ |
28c9fc68 | 12 | |
a9914127 LP |
13 | #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" | |
a9914127 LP |
27 | #include "config.c" |
28 | #include "epautoconf.c" | |
29 | ||
a9914127 | 30 | #include "uvc_queue.c" |
a9914127 | 31 | #include "uvc_video.c" |
5d9955f8 LP |
32 | #include "uvc_v4l2.c" |
33 | #include "f_uvc.c" | |
a9914127 LP |
34 | |
35 | /* -------------------------------------------------------------------------- | |
36 | * Device descriptor | |
37 | */ | |
38 | ||
39 | #define WEBCAM_VENDOR_ID 0x1d6b /* Linux Foundation */ | |
40 | #define WEBCAM_PRODUCT_ID 0x0102 /* Webcam A/V gadget */ | |
41 | #define WEBCAM_DEVICE_BCD 0x0010 /* 0.10 */ | |
42 | ||
43 | static char webcam_vendor_label[] = "Linux Foundation"; | |
44 | static char webcam_product_label[] = "Webcam gadget"; | |
45 | static char webcam_config_label[] = "Video"; | |
46 | ||
47 | /* string IDs are assigned dynamically */ | |
48 | ||
49 | #define STRING_MANUFACTURER_IDX 0 | |
50 | #define STRING_PRODUCT_IDX 1 | |
51 | #define STRING_DESCRIPTION_IDX 2 | |
52 | ||
53 | static struct usb_string webcam_strings[] = { | |
54 | [STRING_MANUFACTURER_IDX].s = webcam_vendor_label, | |
55 | [STRING_PRODUCT_IDX].s = webcam_product_label, | |
56 | [STRING_DESCRIPTION_IDX].s = webcam_config_label, | |
57 | { } | |
58 | }; | |
59 | ||
60 | static struct usb_gadget_strings webcam_stringtab = { | |
61 | .language = 0x0409, /* en-us */ | |
62 | .strings = webcam_strings, | |
63 | }; | |
64 | ||
65 | static struct usb_gadget_strings *webcam_device_strings[] = { | |
66 | &webcam_stringtab, | |
67 | NULL, | |
68 | }; | |
69 | ||
70 | static struct usb_device_descriptor webcam_device_descriptor = { | |
71 | .bLength = USB_DT_DEVICE_SIZE, | |
72 | .bDescriptorType = USB_DT_DEVICE, | |
73 | .bcdUSB = cpu_to_le16(0x0200), | |
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 | ||
87 | DECLARE_UVC_HEADER_DESCRIPTOR(1); | |
88 | ||
89 | static 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 | ||
100 | static 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 | ||
117 | static 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 | ||
130 | static 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 | ||
141 | DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2); | |
142 | ||
143 | static 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 | ||
160 | static 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 | ||
177 | DECLARE_UVC_FRAME_UNCOMPRESSED(1); | |
178 | DECLARE_UVC_FRAME_UNCOMPRESSED(3); | |
179 | ||
180 | static 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 | ||
198 | static 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 | ||
214 | static 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 | ||
228 | DECLARE_UVC_FRAME_MJPEG(1); | |
229 | DECLARE_UVC_FRAME_MJPEG(3); | |
230 | ||
231 | static 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 | ||
249 | static 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 | ||
265 | static 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 |
274 | static 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 | ||
282 | static 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 | ||
290 | static 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 | ||
302 | static 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 |
314 | static 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 | ||
e12995ec | 330 | static int __init |
a9914127 LP |
331 | webcam_config_bind(struct usb_configuration *c) |
332 | { | |
fbcaba0e BS |
333 | return uvc_bind_config(c, uvc_fs_control_cls, uvc_ss_control_cls, |
334 | uvc_fs_streaming_cls, uvc_hs_streaming_cls, | |
335 | uvc_ss_streaming_cls); | |
a9914127 LP |
336 | } |
337 | ||
338 | static struct usb_configuration webcam_config_driver = { | |
339 | .label = webcam_config_label, | |
a9914127 LP |
340 | .bConfigurationValue = 1, |
341 | .iConfiguration = 0, /* dynamic */ | |
342 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
343 | .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, | |
344 | }; | |
345 | ||
346 | static int /* __init_or_exit */ | |
347 | webcam_unbind(struct usb_composite_dev *cdev) | |
348 | { | |
349 | return 0; | |
350 | } | |
351 | ||
e12995ec | 352 | static int __init |
a9914127 LP |
353 | webcam_bind(struct usb_composite_dev *cdev) |
354 | { | |
355 | int ret; | |
356 | ||
357 | /* Allocate string descriptor numbers ... note that string contents | |
358 | * can be overridden by the composite_dev glue. | |
359 | */ | |
360 | if ((ret = usb_string_id(cdev)) < 0) | |
361 | goto error; | |
362 | webcam_strings[STRING_MANUFACTURER_IDX].id = ret; | |
363 | webcam_device_descriptor.iManufacturer = ret; | |
364 | ||
365 | if ((ret = usb_string_id(cdev)) < 0) | |
366 | goto error; | |
367 | webcam_strings[STRING_PRODUCT_IDX].id = ret; | |
368 | webcam_device_descriptor.iProduct = ret; | |
369 | ||
370 | if ((ret = usb_string_id(cdev)) < 0) | |
371 | goto error; | |
372 | webcam_strings[STRING_DESCRIPTION_IDX].id = ret; | |
373 | webcam_config_driver.iConfiguration = ret; | |
374 | ||
375 | /* Register our configuration. */ | |
c9bfff9c UKK |
376 | if ((ret = usb_add_config(cdev, &webcam_config_driver, |
377 | webcam_config_bind)) < 0) | |
a9914127 LP |
378 | goto error; |
379 | ||
380 | INFO(cdev, "Webcam Video Gadget\n"); | |
381 | return 0; | |
382 | ||
383 | error: | |
384 | webcam_unbind(cdev); | |
385 | return ret; | |
386 | } | |
387 | ||
388 | /* -------------------------------------------------------------------------- | |
389 | * Driver | |
390 | */ | |
391 | ||
c2ec75c2 | 392 | static __refdata struct usb_composite_driver webcam_driver = { |
a9914127 LP |
393 | .name = "g_webcam", |
394 | .dev = &webcam_device_descriptor, | |
395 | .strings = webcam_device_strings, | |
fbcaba0e | 396 | .max_speed = USB_SPEED_SUPER, |
03e42bd5 | 397 | .bind = webcam_bind, |
a9914127 LP |
398 | .unbind = webcam_unbind, |
399 | }; | |
400 | ||
401 | static int __init | |
402 | webcam_init(void) | |
403 | { | |
03e42bd5 | 404 | return usb_composite_probe(&webcam_driver); |
a9914127 LP |
405 | } |
406 | ||
407 | static void __exit | |
408 | webcam_cleanup(void) | |
409 | { | |
410 | usb_composite_unregister(&webcam_driver); | |
411 | } | |
412 | ||
413 | module_init(webcam_init); | |
414 | module_exit(webcam_cleanup); | |
415 | ||
416 | MODULE_AUTHOR("Laurent Pinchart"); | |
417 | MODULE_DESCRIPTION("Webcam Video Gadget"); | |
418 | MODULE_LICENSE("GPL"); | |
419 | MODULE_VERSION("0.1.0"); | |
420 |