webcam.c 13 KB

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