webcam.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * webcam.c -- USB webcam gadget driver
  4. *
  5. * Copyright (C) 2009-2010
  6. * Laurent Pinchart ([email protected])
  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(0x0110),
  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. .bmVideoStandards = 0,
  108. };
  109. static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
  110. .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE,
  111. .bDescriptorType = USB_DT_CS_INTERFACE,
  112. .bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL,
  113. .bTerminalID = 3,
  114. .wTerminalType = cpu_to_le16(0x0101),
  115. .bAssocTerminal = 0,
  116. .bSourceID = 2,
  117. .iTerminal = 0,
  118. };
  119. DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
  120. static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
  121. .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2),
  122. .bDescriptorType = USB_DT_CS_INTERFACE,
  123. .bDescriptorSubType = UVC_VS_INPUT_HEADER,
  124. .bNumFormats = 2,
  125. .wTotalLength = 0, /* dynamic */
  126. .bEndpointAddress = 0, /* dynamic */
  127. .bmInfo = 0,
  128. .bTerminalLink = 3,
  129. .bStillCaptureMethod = 0,
  130. .bTriggerSupport = 0,
  131. .bTriggerUsage = 0,
  132. .bControlSize = 1,
  133. .bmaControls[0][0] = 0,
  134. .bmaControls[1][0] = 4,
  135. };
  136. static const struct uvc_format_uncompressed uvc_format_yuv = {
  137. .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
  138. .bDescriptorType = USB_DT_CS_INTERFACE,
  139. .bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED,
  140. .bFormatIndex = 1,
  141. .bNumFrameDescriptors = 2,
  142. .guidFormat =
  143. { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
  144. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
  145. .bBitsPerPixel = 16,
  146. .bDefaultFrameIndex = 1,
  147. .bAspectRatioX = 0,
  148. .bAspectRatioY = 0,
  149. .bmInterlaceFlags = 0,
  150. .bCopyProtect = 0,
  151. };
  152. DECLARE_UVC_FRAME_UNCOMPRESSED(1);
  153. DECLARE_UVC_FRAME_UNCOMPRESSED(3);
  154. static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
  155. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
  156. .bDescriptorType = USB_DT_CS_INTERFACE,
  157. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  158. .bFrameIndex = 1,
  159. .bmCapabilities = 0,
  160. .wWidth = cpu_to_le16(640),
  161. .wHeight = cpu_to_le16(360),
  162. .dwMinBitRate = cpu_to_le32(18432000),
  163. .dwMaxBitRate = cpu_to_le32(55296000),
  164. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  165. .dwDefaultFrameInterval = cpu_to_le32(666666),
  166. .bFrameIntervalType = 3,
  167. .dwFrameInterval[0] = cpu_to_le32(666666),
  168. .dwFrameInterval[1] = cpu_to_le32(1000000),
  169. .dwFrameInterval[2] = cpu_to_le32(5000000),
  170. };
  171. static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
  172. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
  173. .bDescriptorType = USB_DT_CS_INTERFACE,
  174. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  175. .bFrameIndex = 2,
  176. .bmCapabilities = 0,
  177. .wWidth = cpu_to_le16(1280),
  178. .wHeight = cpu_to_le16(720),
  179. .dwMinBitRate = cpu_to_le32(29491200),
  180. .dwMaxBitRate = cpu_to_le32(29491200),
  181. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  182. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  183. .bFrameIntervalType = 1,
  184. .dwFrameInterval[0] = cpu_to_le32(5000000),
  185. };
  186. static const struct uvc_format_mjpeg uvc_format_mjpg = {
  187. .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
  188. .bDescriptorType = USB_DT_CS_INTERFACE,
  189. .bDescriptorSubType = UVC_VS_FORMAT_MJPEG,
  190. .bFormatIndex = 2,
  191. .bNumFrameDescriptors = 2,
  192. .bmFlags = 0,
  193. .bDefaultFrameIndex = 1,
  194. .bAspectRatioX = 0,
  195. .bAspectRatioY = 0,
  196. .bmInterlaceFlags = 0,
  197. .bCopyProtect = 0,
  198. };
  199. DECLARE_UVC_FRAME_MJPEG(1);
  200. DECLARE_UVC_FRAME_MJPEG(3);
  201. static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
  202. .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
  203. .bDescriptorType = USB_DT_CS_INTERFACE,
  204. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  205. .bFrameIndex = 1,
  206. .bmCapabilities = 0,
  207. .wWidth = cpu_to_le16(640),
  208. .wHeight = cpu_to_le16(360),
  209. .dwMinBitRate = cpu_to_le32(18432000),
  210. .dwMaxBitRate = cpu_to_le32(55296000),
  211. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  212. .dwDefaultFrameInterval = cpu_to_le32(666666),
  213. .bFrameIntervalType = 3,
  214. .dwFrameInterval[0] = cpu_to_le32(666666),
  215. .dwFrameInterval[1] = cpu_to_le32(1000000),
  216. .dwFrameInterval[2] = cpu_to_le32(5000000),
  217. };
  218. static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
  219. .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
  220. .bDescriptorType = USB_DT_CS_INTERFACE,
  221. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  222. .bFrameIndex = 2,
  223. .bmCapabilities = 0,
  224. .wWidth = cpu_to_le16(1280),
  225. .wHeight = cpu_to_le16(720),
  226. .dwMinBitRate = cpu_to_le32(29491200),
  227. .dwMaxBitRate = cpu_to_le32(29491200),
  228. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  229. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  230. .bFrameIntervalType = 1,
  231. .dwFrameInterval[0] = cpu_to_le32(5000000),
  232. };
  233. static const struct uvc_color_matching_descriptor uvc_color_matching = {
  234. .bLength = UVC_DT_COLOR_MATCHING_SIZE,
  235. .bDescriptorType = USB_DT_CS_INTERFACE,
  236. .bDescriptorSubType = UVC_VS_COLORFORMAT,
  237. .bColorPrimaries = 1,
  238. .bTransferCharacteristics = 1,
  239. .bMatrixCoefficients = 4,
  240. };
  241. static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
  242. (const struct uvc_descriptor_header *) &uvc_control_header,
  243. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  244. (const struct uvc_descriptor_header *) &uvc_processing,
  245. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  246. NULL,
  247. };
  248. static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
  249. (const struct uvc_descriptor_header *) &uvc_control_header,
  250. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  251. (const struct uvc_descriptor_header *) &uvc_processing,
  252. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  253. NULL,
  254. };
  255. static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
  256. (const struct uvc_descriptor_header *) &uvc_input_header,
  257. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  258. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  259. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  260. (const struct uvc_descriptor_header *) &uvc_color_matching,
  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_color_matching,
  273. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  274. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  275. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  276. (const struct uvc_descriptor_header *) &uvc_color_matching,
  277. NULL,
  278. };
  279. static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
  280. (const struct uvc_descriptor_header *) &uvc_input_header,
  281. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  282. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  283. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  284. (const struct uvc_descriptor_header *) &uvc_color_matching,
  285. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  286. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  287. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  288. (const struct uvc_descriptor_header *) &uvc_color_matching,
  289. NULL,
  290. };
  291. /* --------------------------------------------------------------------------
  292. * USB configuration
  293. */
  294. static int
  295. webcam_config_bind(struct usb_configuration *c)
  296. {
  297. int status = 0;
  298. f_uvc = usb_get_function(fi_uvc);
  299. if (IS_ERR(f_uvc))
  300. return PTR_ERR(f_uvc);
  301. status = usb_add_function(c, f_uvc);
  302. if (status < 0)
  303. usb_put_function(f_uvc);
  304. return status;
  305. }
  306. static struct usb_configuration webcam_config_driver = {
  307. .label = webcam_config_label,
  308. .bConfigurationValue = 1,
  309. .iConfiguration = 0, /* dynamic */
  310. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  311. .MaxPower = CONFIG_USB_GADGET_VBUS_DRAW,
  312. };
  313. static int
  314. webcam_unbind(struct usb_composite_dev *cdev)
  315. {
  316. if (!IS_ERR_OR_NULL(f_uvc))
  317. usb_put_function(f_uvc);
  318. if (!IS_ERR_OR_NULL(fi_uvc))
  319. usb_put_function_instance(fi_uvc);
  320. return 0;
  321. }
  322. static int
  323. webcam_bind(struct usb_composite_dev *cdev)
  324. {
  325. struct f_uvc_opts *uvc_opts;
  326. int ret;
  327. fi_uvc = usb_get_function_instance("uvc");
  328. if (IS_ERR(fi_uvc))
  329. return PTR_ERR(fi_uvc);
  330. uvc_opts = container_of(fi_uvc, struct f_uvc_opts, func_inst);
  331. uvc_opts->streaming_interval = streaming_interval;
  332. uvc_opts->streaming_maxpacket = streaming_maxpacket;
  333. uvc_opts->streaming_maxburst = streaming_maxburst;
  334. uvc_opts->fs_control = uvc_fs_control_cls;
  335. uvc_opts->ss_control = uvc_ss_control_cls;
  336. uvc_opts->fs_streaming = uvc_fs_streaming_cls;
  337. uvc_opts->hs_streaming = uvc_hs_streaming_cls;
  338. uvc_opts->ss_streaming = uvc_ss_streaming_cls;
  339. /* Allocate string descriptor numbers ... note that string contents
  340. * can be overridden by the composite_dev glue.
  341. */
  342. ret = usb_string_ids_tab(cdev, webcam_strings);
  343. if (ret < 0)
  344. goto error;
  345. webcam_device_descriptor.iManufacturer =
  346. webcam_strings[USB_GADGET_MANUFACTURER_IDX].id;
  347. webcam_device_descriptor.iProduct =
  348. webcam_strings[USB_GADGET_PRODUCT_IDX].id;
  349. webcam_config_driver.iConfiguration =
  350. webcam_strings[STRING_DESCRIPTION_IDX].id;
  351. /* Register our configuration. */
  352. if ((ret = usb_add_config(cdev, &webcam_config_driver,
  353. webcam_config_bind)) < 0)
  354. goto error;
  355. usb_composite_overwrite_options(cdev, &coverwrite);
  356. INFO(cdev, "Webcam Video Gadget\n");
  357. return 0;
  358. error:
  359. usb_put_function_instance(fi_uvc);
  360. return ret;
  361. }
  362. /* --------------------------------------------------------------------------
  363. * Driver
  364. */
  365. static struct usb_composite_driver webcam_driver = {
  366. .name = "g_webcam",
  367. .dev = &webcam_device_descriptor,
  368. .strings = webcam_device_strings,
  369. .max_speed = USB_SPEED_SUPER,
  370. .bind = webcam_bind,
  371. .unbind = webcam_unbind,
  372. };
  373. module_usb_composite_driver(webcam_driver);
  374. MODULE_AUTHOR("Laurent Pinchart");
  375. MODULE_DESCRIPTION("Webcam Video Gadget");
  376. MODULE_LICENSE("GPL");