webcam.c 12 KB

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