webcam.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/usb/video.h>
  15. #include "f_uvc.h"
  16. /*
  17. * Kbuild is not very cooperative with respect to linking separately
  18. * compiled library objects into one module. So for now we won't use
  19. * separate compilation ... ensuring init/exit sections work to shrink
  20. * the runtime footprint, and giving us at least some parts of what
  21. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  22. */
  23. #include "uvc_queue.c"
  24. #include "uvc_video.c"
  25. #include "uvc_v4l2.c"
  26. #include "f_uvc.c"
  27. USB_GADGET_COMPOSITE_OPTIONS();
  28. /* --------------------------------------------------------------------------
  29. * Device descriptor
  30. */
  31. #define WEBCAM_VENDOR_ID 0x1d6b /* Linux Foundation */
  32. #define WEBCAM_PRODUCT_ID 0x0102 /* Webcam A/V gadget */
  33. #define WEBCAM_DEVICE_BCD 0x0010 /* 0.10 */
  34. static char webcam_vendor_label[] = "Linux Foundation";
  35. static char webcam_product_label[] = "Webcam gadget";
  36. static char webcam_config_label[] = "Video";
  37. /* string IDs are assigned dynamically */
  38. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  39. static struct usb_string webcam_strings[] = {
  40. [USB_GADGET_MANUFACTURER_IDX].s = webcam_vendor_label,
  41. [USB_GADGET_PRODUCT_IDX].s = webcam_product_label,
  42. [USB_GADGET_SERIAL_IDX].s = "",
  43. [STRING_DESCRIPTION_IDX].s = webcam_config_label,
  44. { }
  45. };
  46. static struct usb_gadget_strings webcam_stringtab = {
  47. .language = 0x0409, /* en-us */
  48. .strings = webcam_strings,
  49. };
  50. static struct usb_gadget_strings *webcam_device_strings[] = {
  51. &webcam_stringtab,
  52. NULL,
  53. };
  54. static struct usb_device_descriptor webcam_device_descriptor = {
  55. .bLength = USB_DT_DEVICE_SIZE,
  56. .bDescriptorType = USB_DT_DEVICE,
  57. .bcdUSB = cpu_to_le16(0x0200),
  58. .bDeviceClass = USB_CLASS_MISC,
  59. .bDeviceSubClass = 0x02,
  60. .bDeviceProtocol = 0x01,
  61. .bMaxPacketSize0 = 0, /* dynamic */
  62. .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID),
  63. .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID),
  64. .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD),
  65. .iManufacturer = 0, /* dynamic */
  66. .iProduct = 0, /* dynamic */
  67. .iSerialNumber = 0, /* dynamic */
  68. .bNumConfigurations = 0, /* dynamic */
  69. };
  70. DECLARE_UVC_HEADER_DESCRIPTOR(1);
  71. static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = {
  72. .bLength = UVC_DT_HEADER_SIZE(1),
  73. .bDescriptorType = USB_DT_CS_INTERFACE,
  74. .bDescriptorSubType = UVC_VC_HEADER,
  75. .bcdUVC = cpu_to_le16(0x0100),
  76. .wTotalLength = 0, /* dynamic */
  77. .dwClockFrequency = cpu_to_le32(48000000),
  78. .bInCollection = 0, /* dynamic */
  79. .baInterfaceNr[0] = 0, /* dynamic */
  80. };
  81. static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = {
  82. .bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3),
  83. .bDescriptorType = USB_DT_CS_INTERFACE,
  84. .bDescriptorSubType = UVC_VC_INPUT_TERMINAL,
  85. .bTerminalID = 1,
  86. .wTerminalType = cpu_to_le16(0x0201),
  87. .bAssocTerminal = 0,
  88. .iTerminal = 0,
  89. .wObjectiveFocalLengthMin = cpu_to_le16(0),
  90. .wObjectiveFocalLengthMax = cpu_to_le16(0),
  91. .wOcularFocalLength = cpu_to_le16(0),
  92. .bControlSize = 3,
  93. .bmControls[0] = 2,
  94. .bmControls[1] = 0,
  95. .bmControls[2] = 0,
  96. };
  97. static const struct uvc_processing_unit_descriptor uvc_processing = {
  98. .bLength = UVC_DT_PROCESSING_UNIT_SIZE(2),
  99. .bDescriptorType = USB_DT_CS_INTERFACE,
  100. .bDescriptorSubType = UVC_VC_PROCESSING_UNIT,
  101. .bUnitID = 2,
  102. .bSourceID = 1,
  103. .wMaxMultiplier = cpu_to_le16(16*1024),
  104. .bControlSize = 2,
  105. .bmControls[0] = 1,
  106. .bmControls[1] = 0,
  107. .iProcessing = 0,
  108. };
  109. static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
  110. .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE,
  111. .bDescriptorType = USB_DT_CS_INTERFACE,
  112. .bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL,
  113. .bTerminalID = 3,
  114. .wTerminalType = cpu_to_le16(0x0101),
  115. .bAssocTerminal = 0,
  116. .bSourceID = 2,
  117. .iTerminal = 0,
  118. };
  119. DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
  120. static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
  121. .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2),
  122. .bDescriptorType = USB_DT_CS_INTERFACE,
  123. .bDescriptorSubType = UVC_VS_INPUT_HEADER,
  124. .bNumFormats = 2,
  125. .wTotalLength = 0, /* dynamic */
  126. .bEndpointAddress = 0, /* dynamic */
  127. .bmInfo = 0,
  128. .bTerminalLink = 3,
  129. .bStillCaptureMethod = 0,
  130. .bTriggerSupport = 0,
  131. .bTriggerUsage = 0,
  132. .bControlSize = 1,
  133. .bmaControls[0][0] = 0,
  134. .bmaControls[1][0] = 4,
  135. };
  136. static const struct uvc_format_uncompressed uvc_format_yuv = {
  137. .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
  138. .bDescriptorType = USB_DT_CS_INTERFACE,
  139. .bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED,
  140. .bFormatIndex = 1,
  141. .bNumFrameDescriptors = 2,
  142. .guidFormat =
  143. { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
  144. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
  145. .bBitsPerPixel = 16,
  146. .bDefaultFrameIndex = 1,
  147. .bAspectRatioX = 0,
  148. .bAspectRatioY = 0,
  149. .bmInterfaceFlags = 0,
  150. .bCopyProtect = 0,
  151. };
  152. DECLARE_UVC_FRAME_UNCOMPRESSED(1);
  153. DECLARE_UVC_FRAME_UNCOMPRESSED(3);
  154. static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
  155. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
  156. .bDescriptorType = USB_DT_CS_INTERFACE,
  157. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  158. .bFrameIndex = 1,
  159. .bmCapabilities = 0,
  160. .wWidth = cpu_to_le16(640),
  161. .wHeight = cpu_to_le16(360),
  162. .dwMinBitRate = cpu_to_le32(18432000),
  163. .dwMaxBitRate = cpu_to_le32(55296000),
  164. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  165. .dwDefaultFrameInterval = cpu_to_le32(666666),
  166. .bFrameIntervalType = 3,
  167. .dwFrameInterval[0] = cpu_to_le32(666666),
  168. .dwFrameInterval[1] = cpu_to_le32(1000000),
  169. .dwFrameInterval[2] = cpu_to_le32(5000000),
  170. };
  171. static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
  172. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
  173. .bDescriptorType = USB_DT_CS_INTERFACE,
  174. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  175. .bFrameIndex = 2,
  176. .bmCapabilities = 0,
  177. .wWidth = cpu_to_le16(1280),
  178. .wHeight = cpu_to_le16(720),
  179. .dwMinBitRate = cpu_to_le32(29491200),
  180. .dwMaxBitRate = cpu_to_le32(29491200),
  181. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  182. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  183. .bFrameIntervalType = 1,
  184. .dwFrameInterval[0] = cpu_to_le32(5000000),
  185. };
  186. static const struct uvc_format_mjpeg uvc_format_mjpg = {
  187. .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
  188. .bDescriptorType = USB_DT_CS_INTERFACE,
  189. .bDescriptorSubType = UVC_VS_FORMAT_MJPEG,
  190. .bFormatIndex = 2,
  191. .bNumFrameDescriptors = 2,
  192. .bmFlags = 0,
  193. .bDefaultFrameIndex = 1,
  194. .bAspectRatioX = 0,
  195. .bAspectRatioY = 0,
  196. .bmInterfaceFlags = 0,
  197. .bCopyProtect = 0,
  198. };
  199. DECLARE_UVC_FRAME_MJPEG(1);
  200. DECLARE_UVC_FRAME_MJPEG(3);
  201. static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
  202. .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
  203. .bDescriptorType = USB_DT_CS_INTERFACE,
  204. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  205. .bFrameIndex = 1,
  206. .bmCapabilities = 0,
  207. .wWidth = cpu_to_le16(640),
  208. .wHeight = cpu_to_le16(360),
  209. .dwMinBitRate = cpu_to_le32(18432000),
  210. .dwMaxBitRate = cpu_to_le32(55296000),
  211. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  212. .dwDefaultFrameInterval = cpu_to_le32(666666),
  213. .bFrameIntervalType = 3,
  214. .dwFrameInterval[0] = cpu_to_le32(666666),
  215. .dwFrameInterval[1] = cpu_to_le32(1000000),
  216. .dwFrameInterval[2] = cpu_to_le32(5000000),
  217. };
  218. static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
  219. .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
  220. .bDescriptorType = USB_DT_CS_INTERFACE,
  221. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  222. .bFrameIndex = 2,
  223. .bmCapabilities = 0,
  224. .wWidth = cpu_to_le16(1280),
  225. .wHeight = cpu_to_le16(720),
  226. .dwMinBitRate = cpu_to_le32(29491200),
  227. .dwMaxBitRate = cpu_to_le32(29491200),
  228. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  229. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  230. .bFrameIntervalType = 1,
  231. .dwFrameInterval[0] = cpu_to_le32(5000000),
  232. };
  233. static const struct uvc_color_matching_descriptor uvc_color_matching = {
  234. .bLength = UVC_DT_COLOR_MATCHING_SIZE,
  235. .bDescriptorType = USB_DT_CS_INTERFACE,
  236. .bDescriptorSubType = UVC_VS_COLORFORMAT,
  237. .bColorPrimaries = 1,
  238. .bTransferCharacteristics = 1,
  239. .bMatrixCoefficients = 4,
  240. };
  241. static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
  242. (const struct uvc_descriptor_header *) &uvc_control_header,
  243. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  244. (const struct uvc_descriptor_header *) &uvc_processing,
  245. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  246. NULL,
  247. };
  248. static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
  249. (const struct uvc_descriptor_header *) &uvc_control_header,
  250. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  251. (const struct uvc_descriptor_header *) &uvc_processing,
  252. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  253. NULL,
  254. };
  255. static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
  256. (const struct uvc_descriptor_header *) &uvc_input_header,
  257. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  258. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  259. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  260. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  261. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  262. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  263. (const struct uvc_descriptor_header *) &uvc_color_matching,
  264. NULL,
  265. };
  266. static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
  267. (const struct uvc_descriptor_header *) &uvc_input_header,
  268. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  269. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  270. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  271. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  272. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  273. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  274. (const struct uvc_descriptor_header *) &uvc_color_matching,
  275. NULL,
  276. };
  277. static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
  278. (const struct uvc_descriptor_header *) &uvc_input_header,
  279. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  280. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  281. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  282. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  283. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  284. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  285. (const struct uvc_descriptor_header *) &uvc_color_matching,
  286. NULL,
  287. };
  288. /* --------------------------------------------------------------------------
  289. * USB configuration
  290. */
  291. static int __init
  292. webcam_config_bind(struct usb_configuration *c)
  293. {
  294. return uvc_bind_config(c, uvc_fs_control_cls, uvc_ss_control_cls,
  295. uvc_fs_streaming_cls, uvc_hs_streaming_cls,
  296. uvc_ss_streaming_cls);
  297. }
  298. static struct usb_configuration webcam_config_driver = {
  299. .label = webcam_config_label,
  300. .bConfigurationValue = 1,
  301. .iConfiguration = 0, /* dynamic */
  302. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  303. .MaxPower = CONFIG_USB_GADGET_VBUS_DRAW,
  304. };
  305. static int /* __init_or_exit */
  306. webcam_unbind(struct usb_composite_dev *cdev)
  307. {
  308. return 0;
  309. }
  310. static int __init
  311. webcam_bind(struct usb_composite_dev *cdev)
  312. {
  313. int ret;
  314. /* Allocate string descriptor numbers ... note that string contents
  315. * can be overridden by the composite_dev glue.
  316. */
  317. ret = usb_string_ids_tab(cdev, webcam_strings);
  318. if (ret < 0)
  319. goto error;
  320. webcam_device_descriptor.iManufacturer =
  321. webcam_strings[USB_GADGET_MANUFACTURER_IDX].id;
  322. webcam_device_descriptor.iProduct =
  323. webcam_strings[USB_GADGET_PRODUCT_IDX].id;
  324. webcam_config_driver.iConfiguration =
  325. webcam_strings[STRING_DESCRIPTION_IDX].id;
  326. /* Register our configuration. */
  327. if ((ret = usb_add_config(cdev, &webcam_config_driver,
  328. webcam_config_bind)) < 0)
  329. goto error;
  330. usb_composite_overwrite_options(cdev, &coverwrite);
  331. INFO(cdev, "Webcam Video Gadget\n");
  332. return 0;
  333. error:
  334. webcam_unbind(cdev);
  335. return ret;
  336. }
  337. /* --------------------------------------------------------------------------
  338. * Driver
  339. */
  340. static __refdata struct usb_composite_driver webcam_driver = {
  341. .name = "g_webcam",
  342. .dev = &webcam_device_descriptor,
  343. .strings = webcam_device_strings,
  344. .max_speed = USB_SPEED_SUPER,
  345. .bind = webcam_bind,
  346. .unbind = webcam_unbind,
  347. };
  348. static int __init
  349. webcam_init(void)
  350. {
  351. return usb_composite_probe(&webcam_driver);
  352. }
  353. static void __exit
  354. webcam_cleanup(void)
  355. {
  356. usb_composite_unregister(&webcam_driver);
  357. }
  358. module_init(webcam_init);
  359. module_exit(webcam_cleanup);
  360. MODULE_AUTHOR("Laurent Pinchart");
  361. MODULE_DESCRIPTION("Webcam Video Gadget");
  362. MODULE_LICENSE("GPL");
  363. MODULE_VERSION("0.1.0");