webcam.c 13 KB

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