webcam.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/module.h>
  15. #include <linux/usb/video.h>
  16. #include "u_uvc.h"
  17. USB_GADGET_COMPOSITE_OPTIONS();
  18. /*-------------------------------------------------------------------------*/
  19. /* module parameters specific to the Video streaming endpoint */
  20. static unsigned int streaming_interval = 1;
  21. module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
  22. MODULE_PARM_DESC(streaming_interval, "1 - 16");
  23. static unsigned int streaming_maxpacket = 1024;
  24. module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
  25. MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
  26. static unsigned int streaming_maxburst;
  27. module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
  28. MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
  29. static unsigned int trace;
  30. module_param(trace, uint, S_IRUGO|S_IWUSR);
  31. MODULE_PARM_DESC(trace, "Trace level bitmask");
  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_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  43. static struct usb_string webcam_strings[] = {
  44. [USB_GADGET_MANUFACTURER_IDX].s = webcam_vendor_label,
  45. [USB_GADGET_PRODUCT_IDX].s = webcam_product_label,
  46. [USB_GADGET_SERIAL_IDX].s = "",
  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_function_instance *fi_uvc;
  59. static struct usb_function *f_uvc;
  60. static struct usb_device_descriptor webcam_device_descriptor = {
  61. .bLength = USB_DT_DEVICE_SIZE,
  62. .bDescriptorType = USB_DT_DEVICE,
  63. .bcdUSB = cpu_to_le16(0x0200),
  64. .bDeviceClass = USB_CLASS_MISC,
  65. .bDeviceSubClass = 0x02,
  66. .bDeviceProtocol = 0x01,
  67. .bMaxPacketSize0 = 0, /* dynamic */
  68. .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID),
  69. .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID),
  70. .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD),
  71. .iManufacturer = 0, /* dynamic */
  72. .iProduct = 0, /* dynamic */
  73. .iSerialNumber = 0, /* dynamic */
  74. .bNumConfigurations = 0, /* dynamic */
  75. };
  76. DECLARE_UVC_HEADER_DESCRIPTOR(1);
  77. static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = {
  78. .bLength = UVC_DT_HEADER_SIZE(1),
  79. .bDescriptorType = USB_DT_CS_INTERFACE,
  80. .bDescriptorSubType = UVC_VC_HEADER,
  81. .bcdUVC = cpu_to_le16(0x0100),
  82. .wTotalLength = 0, /* dynamic */
  83. .dwClockFrequency = cpu_to_le32(48000000),
  84. .bInCollection = 0, /* dynamic */
  85. .baInterfaceNr[0] = 0, /* dynamic */
  86. };
  87. static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = {
  88. .bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3),
  89. .bDescriptorType = USB_DT_CS_INTERFACE,
  90. .bDescriptorSubType = UVC_VC_INPUT_TERMINAL,
  91. .bTerminalID = 1,
  92. .wTerminalType = cpu_to_le16(0x0201),
  93. .bAssocTerminal = 0,
  94. .iTerminal = 0,
  95. .wObjectiveFocalLengthMin = cpu_to_le16(0),
  96. .wObjectiveFocalLengthMax = cpu_to_le16(0),
  97. .wOcularFocalLength = cpu_to_le16(0),
  98. .bControlSize = 3,
  99. .bmControls[0] = 2,
  100. .bmControls[1] = 0,
  101. .bmControls[2] = 0,
  102. };
  103. static const struct uvc_processing_unit_descriptor uvc_processing = {
  104. .bLength = UVC_DT_PROCESSING_UNIT_SIZE(2),
  105. .bDescriptorType = USB_DT_CS_INTERFACE,
  106. .bDescriptorSubType = UVC_VC_PROCESSING_UNIT,
  107. .bUnitID = 2,
  108. .bSourceID = 1,
  109. .wMaxMultiplier = cpu_to_le16(16*1024),
  110. .bControlSize = 2,
  111. .bmControls[0] = 1,
  112. .bmControls[1] = 0,
  113. .iProcessing = 0,
  114. };
  115. static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
  116. .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE,
  117. .bDescriptorType = USB_DT_CS_INTERFACE,
  118. .bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL,
  119. .bTerminalID = 3,
  120. .wTerminalType = cpu_to_le16(0x0101),
  121. .bAssocTerminal = 0,
  122. .bSourceID = 2,
  123. .iTerminal = 0,
  124. };
  125. DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
  126. static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
  127. .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2),
  128. .bDescriptorType = USB_DT_CS_INTERFACE,
  129. .bDescriptorSubType = UVC_VS_INPUT_HEADER,
  130. .bNumFormats = 2,
  131. .wTotalLength = 0, /* dynamic */
  132. .bEndpointAddress = 0, /* dynamic */
  133. .bmInfo = 0,
  134. .bTerminalLink = 3,
  135. .bStillCaptureMethod = 0,
  136. .bTriggerSupport = 0,
  137. .bTriggerUsage = 0,
  138. .bControlSize = 1,
  139. .bmaControls[0][0] = 0,
  140. .bmaControls[1][0] = 4,
  141. };
  142. static const struct uvc_format_uncompressed uvc_format_yuv = {
  143. .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
  144. .bDescriptorType = USB_DT_CS_INTERFACE,
  145. .bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED,
  146. .bFormatIndex = 1,
  147. .bNumFrameDescriptors = 2,
  148. .guidFormat =
  149. { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
  150. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
  151. .bBitsPerPixel = 16,
  152. .bDefaultFrameIndex = 1,
  153. .bAspectRatioX = 0,
  154. .bAspectRatioY = 0,
  155. .bmInterfaceFlags = 0,
  156. .bCopyProtect = 0,
  157. };
  158. DECLARE_UVC_FRAME_UNCOMPRESSED(1);
  159. DECLARE_UVC_FRAME_UNCOMPRESSED(3);
  160. static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
  161. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
  162. .bDescriptorType = USB_DT_CS_INTERFACE,
  163. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  164. .bFrameIndex = 1,
  165. .bmCapabilities = 0,
  166. .wWidth = cpu_to_le16(640),
  167. .wHeight = cpu_to_le16(360),
  168. .dwMinBitRate = cpu_to_le32(18432000),
  169. .dwMaxBitRate = cpu_to_le32(55296000),
  170. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  171. .dwDefaultFrameInterval = cpu_to_le32(666666),
  172. .bFrameIntervalType = 3,
  173. .dwFrameInterval[0] = cpu_to_le32(666666),
  174. .dwFrameInterval[1] = cpu_to_le32(1000000),
  175. .dwFrameInterval[2] = cpu_to_le32(5000000),
  176. };
  177. static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
  178. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
  179. .bDescriptorType = USB_DT_CS_INTERFACE,
  180. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  181. .bFrameIndex = 2,
  182. .bmCapabilities = 0,
  183. .wWidth = cpu_to_le16(1280),
  184. .wHeight = cpu_to_le16(720),
  185. .dwMinBitRate = cpu_to_le32(29491200),
  186. .dwMaxBitRate = cpu_to_le32(29491200),
  187. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  188. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  189. .bFrameIntervalType = 1,
  190. .dwFrameInterval[0] = cpu_to_le32(5000000),
  191. };
  192. static const struct uvc_format_mjpeg uvc_format_mjpg = {
  193. .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
  194. .bDescriptorType = USB_DT_CS_INTERFACE,
  195. .bDescriptorSubType = UVC_VS_FORMAT_MJPEG,
  196. .bFormatIndex = 2,
  197. .bNumFrameDescriptors = 2,
  198. .bmFlags = 0,
  199. .bDefaultFrameIndex = 1,
  200. .bAspectRatioX = 0,
  201. .bAspectRatioY = 0,
  202. .bmInterfaceFlags = 0,
  203. .bCopyProtect = 0,
  204. };
  205. DECLARE_UVC_FRAME_MJPEG(1);
  206. DECLARE_UVC_FRAME_MJPEG(3);
  207. static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
  208. .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
  209. .bDescriptorType = USB_DT_CS_INTERFACE,
  210. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  211. .bFrameIndex = 1,
  212. .bmCapabilities = 0,
  213. .wWidth = cpu_to_le16(640),
  214. .wHeight = cpu_to_le16(360),
  215. .dwMinBitRate = cpu_to_le32(18432000),
  216. .dwMaxBitRate = cpu_to_le32(55296000),
  217. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  218. .dwDefaultFrameInterval = cpu_to_le32(666666),
  219. .bFrameIntervalType = 3,
  220. .dwFrameInterval[0] = cpu_to_le32(666666),
  221. .dwFrameInterval[1] = cpu_to_le32(1000000),
  222. .dwFrameInterval[2] = cpu_to_le32(5000000),
  223. };
  224. static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
  225. .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
  226. .bDescriptorType = USB_DT_CS_INTERFACE,
  227. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  228. .bFrameIndex = 2,
  229. .bmCapabilities = 0,
  230. .wWidth = cpu_to_le16(1280),
  231. .wHeight = cpu_to_le16(720),
  232. .dwMinBitRate = cpu_to_le32(29491200),
  233. .dwMaxBitRate = cpu_to_le32(29491200),
  234. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  235. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  236. .bFrameIntervalType = 1,
  237. .dwFrameInterval[0] = cpu_to_le32(5000000),
  238. };
  239. static const struct uvc_color_matching_descriptor uvc_color_matching = {
  240. .bLength = UVC_DT_COLOR_MATCHING_SIZE,
  241. .bDescriptorType = USB_DT_CS_INTERFACE,
  242. .bDescriptorSubType = UVC_VS_COLORFORMAT,
  243. .bColorPrimaries = 1,
  244. .bTransferCharacteristics = 1,
  245. .bMatrixCoefficients = 4,
  246. };
  247. static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
  248. (const struct uvc_descriptor_header *) &uvc_control_header,
  249. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  250. (const struct uvc_descriptor_header *) &uvc_processing,
  251. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  252. NULL,
  253. };
  254. static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
  255. (const struct uvc_descriptor_header *) &uvc_control_header,
  256. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  257. (const struct uvc_descriptor_header *) &uvc_processing,
  258. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  259. NULL,
  260. };
  261. static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
  262. (const struct uvc_descriptor_header *) &uvc_input_header,
  263. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  264. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  265. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  266. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  267. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  268. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  269. (const struct uvc_descriptor_header *) &uvc_color_matching,
  270. NULL,
  271. };
  272. static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
  273. (const struct uvc_descriptor_header *) &uvc_input_header,
  274. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  275. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  276. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  277. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  278. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  279. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  280. (const struct uvc_descriptor_header *) &uvc_color_matching,
  281. NULL,
  282. };
  283. static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
  284. (const struct uvc_descriptor_header *) &uvc_input_header,
  285. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  286. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  287. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  288. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  289. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  290. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  291. (const struct uvc_descriptor_header *) &uvc_color_matching,
  292. NULL,
  293. };
  294. /* --------------------------------------------------------------------------
  295. * USB configuration
  296. */
  297. static int
  298. webcam_config_bind(struct usb_configuration *c)
  299. {
  300. int status = 0;
  301. f_uvc = usb_get_function(fi_uvc);
  302. if (IS_ERR(f_uvc))
  303. return PTR_ERR(f_uvc);
  304. status = usb_add_function(c, f_uvc);
  305. if (status < 0)
  306. usb_put_function(f_uvc);
  307. return status;
  308. }
  309. static struct usb_configuration webcam_config_driver = {
  310. .label = webcam_config_label,
  311. .bConfigurationValue = 1,
  312. .iConfiguration = 0, /* dynamic */
  313. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  314. .MaxPower = CONFIG_USB_GADGET_VBUS_DRAW,
  315. };
  316. static int
  317. webcam_unbind(struct usb_composite_dev *cdev)
  318. {
  319. if (!IS_ERR_OR_NULL(f_uvc))
  320. usb_put_function(f_uvc);
  321. if (!IS_ERR_OR_NULL(fi_uvc))
  322. usb_put_function_instance(fi_uvc);
  323. return 0;
  324. }
  325. static int
  326. webcam_bind(struct usb_composite_dev *cdev)
  327. {
  328. struct f_uvc_opts *uvc_opts;
  329. int ret;
  330. fi_uvc = usb_get_function_instance("uvc");
  331. if (IS_ERR(fi_uvc))
  332. return PTR_ERR(fi_uvc);
  333. uvc_opts = container_of(fi_uvc, struct f_uvc_opts, func_inst);
  334. uvc_opts->streaming_interval = streaming_interval;
  335. uvc_opts->streaming_maxpacket = streaming_maxpacket;
  336. uvc_opts->streaming_maxburst = streaming_maxburst;
  337. uvc_set_trace_param(trace);
  338. uvc_opts->fs_control = uvc_fs_control_cls;
  339. uvc_opts->ss_control = uvc_ss_control_cls;
  340. uvc_opts->fs_streaming = uvc_fs_streaming_cls;
  341. uvc_opts->hs_streaming = uvc_hs_streaming_cls;
  342. uvc_opts->ss_streaming = uvc_ss_streaming_cls;
  343. /* Allocate string descriptor numbers ... note that string contents
  344. * can be overridden by the composite_dev glue.
  345. */
  346. ret = usb_string_ids_tab(cdev, webcam_strings);
  347. if (ret < 0)
  348. goto error;
  349. webcam_device_descriptor.iManufacturer =
  350. webcam_strings[USB_GADGET_MANUFACTURER_IDX].id;
  351. webcam_device_descriptor.iProduct =
  352. webcam_strings[USB_GADGET_PRODUCT_IDX].id;
  353. webcam_config_driver.iConfiguration =
  354. webcam_strings[STRING_DESCRIPTION_IDX].id;
  355. /* Register our configuration. */
  356. if ((ret = usb_add_config(cdev, &webcam_config_driver,
  357. webcam_config_bind)) < 0)
  358. goto error;
  359. usb_composite_overwrite_options(cdev, &coverwrite);
  360. INFO(cdev, "Webcam Video Gadget\n");
  361. return 0;
  362. error:
  363. usb_put_function_instance(fi_uvc);
  364. return ret;
  365. }
  366. /* --------------------------------------------------------------------------
  367. * Driver
  368. */
  369. static struct usb_composite_driver webcam_driver = {
  370. .name = "g_webcam",
  371. .dev = &webcam_device_descriptor,
  372. .strings = webcam_device_strings,
  373. .max_speed = USB_SPEED_SUPER,
  374. .bind = webcam_bind,
  375. .unbind = webcam_unbind,
  376. };
  377. module_usb_composite_driver(webcam_driver);
  378. MODULE_AUTHOR("Laurent Pinchart");
  379. MODULE_DESCRIPTION("Webcam Video Gadget");
  380. MODULE_LICENSE("GPL");
  381. MODULE_VERSION("0.1.0");