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