webcam.c 13 KB

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