webcam.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. #include "uvc_configfs.h"
  14. USB_GADGET_COMPOSITE_OPTIONS();
  15. /*-------------------------------------------------------------------------*/
  16. /* module parameters specific to the Video streaming endpoint */
  17. static unsigned int streaming_interval = 1;
  18. module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
  19. MODULE_PARM_DESC(streaming_interval, "1 - 16");
  20. static unsigned int streaming_maxpacket = 1024;
  21. module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
  22. MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
  23. static unsigned int streaming_maxburst;
  24. module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
  25. MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
  26. /* --------------------------------------------------------------------------
  27. * Device descriptor
  28. */
  29. #define WEBCAM_VENDOR_ID 0x1d6b /* Linux Foundation */
  30. #define WEBCAM_PRODUCT_ID 0x0102 /* Webcam A/V gadget */
  31. #define WEBCAM_DEVICE_BCD 0x0010 /* 0.10 */
  32. static char webcam_vendor_label[] = "Linux Foundation";
  33. static char webcam_product_label[] = "Webcam gadget";
  34. static char webcam_config_label[] = "Video";
  35. /* string IDs are assigned dynamically */
  36. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  37. static struct usb_string webcam_strings[] = {
  38. [USB_GADGET_MANUFACTURER_IDX].s = webcam_vendor_label,
  39. [USB_GADGET_PRODUCT_IDX].s = webcam_product_label,
  40. [USB_GADGET_SERIAL_IDX].s = "",
  41. [STRING_DESCRIPTION_IDX].s = webcam_config_label,
  42. { }
  43. };
  44. static struct usb_gadget_strings webcam_stringtab = {
  45. .language = 0x0409, /* en-us */
  46. .strings = webcam_strings,
  47. };
  48. static struct usb_gadget_strings *webcam_device_strings[] = {
  49. &webcam_stringtab,
  50. NULL,
  51. };
  52. static struct usb_function_instance *fi_uvc;
  53. static struct usb_function *f_uvc;
  54. static struct usb_device_descriptor webcam_device_descriptor = {
  55. .bLength = USB_DT_DEVICE_SIZE,
  56. .bDescriptorType = USB_DT_DEVICE,
  57. /* .bcdUSB = DYNAMIC */
  58. .bDeviceClass = USB_CLASS_MISC,
  59. .bDeviceSubClass = 0x02,
  60. .bDeviceProtocol = 0x01,
  61. .bMaxPacketSize0 = 0, /* dynamic */
  62. .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID),
  63. .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID),
  64. .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD),
  65. .iManufacturer = 0, /* dynamic */
  66. .iProduct = 0, /* dynamic */
  67. .iSerialNumber = 0, /* dynamic */
  68. .bNumConfigurations = 0, /* dynamic */
  69. };
  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 uvcg_color_matching uvcg_color_matching = {
  137. .desc = {
  138. .bLength = UVC_DT_COLOR_MATCHING_SIZE,
  139. .bDescriptorType = USB_DT_CS_INTERFACE,
  140. .bDescriptorSubType = UVC_VS_COLORFORMAT,
  141. .bColorPrimaries = 1,
  142. .bTransferCharacteristics = 1,
  143. .bMatrixCoefficients = 4,
  144. },
  145. };
  146. static struct uvcg_uncompressed uvcg_format_yuv = {
  147. .fmt = {
  148. .type = UVCG_UNCOMPRESSED,
  149. /* add to .frames and fill .num_frames at runtime */
  150. .color_matching = (struct uvcg_color_matching *)&uvcg_color_matching,
  151. },
  152. .desc = {
  153. .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
  154. .bDescriptorType = USB_DT_CS_INTERFACE,
  155. .bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED,
  156. .bFormatIndex = 1,
  157. .bNumFrameDescriptors = 2,
  158. .guidFormat = {
  159. 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
  160. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
  161. },
  162. .bBitsPerPixel = 16,
  163. .bDefaultFrameIndex = 1,
  164. .bAspectRatioX = 0,
  165. .bAspectRatioY = 0,
  166. .bmInterlaceFlags = 0,
  167. .bCopyProtect = 0,
  168. },
  169. };
  170. static struct uvcg_format_ptr uvcg_format_ptr_yuv = {
  171. .fmt = &uvcg_format_yuv.fmt,
  172. };
  173. DECLARE_UVC_FRAME_UNCOMPRESSED(1);
  174. DECLARE_UVC_FRAME_UNCOMPRESSED(3);
  175. #define UVCG_WIDTH_360P 640
  176. #define UVCG_HEIGHT_360P 360
  177. #define UVCG_MIN_BITRATE_360P 18432000
  178. #define UVCG_MAX_BITRATE_360P 55296000
  179. #define UVCG_MAX_VIDEO_FB_SZ_360P 460800
  180. #define UVCG_FRM_INTERV_0_360P 666666
  181. #define UVCG_FRM_INTERV_1_360P 1000000
  182. #define UVCG_FRM_INTERV_2_360P 5000000
  183. #define UVCG_DEFAULT_FRM_INTERV_360P UVCG_FRM_INTERV_0_360P
  184. static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
  185. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
  186. .bDescriptorType = USB_DT_CS_INTERFACE,
  187. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  188. .bFrameIndex = 1,
  189. .bmCapabilities = 0,
  190. .wWidth = cpu_to_le16(UVCG_WIDTH_360P),
  191. .wHeight = cpu_to_le16(UVCG_HEIGHT_360P),
  192. .dwMinBitRate = cpu_to_le32(UVCG_MIN_BITRATE_360P),
  193. .dwMaxBitRate = cpu_to_le32(UVCG_MAX_BITRATE_360P),
  194. .dwMaxVideoFrameBufferSize = cpu_to_le32(UVCG_MAX_VIDEO_FB_SZ_360P),
  195. .dwDefaultFrameInterval = cpu_to_le32(UVCG_DEFAULT_FRM_INTERV_360P),
  196. .bFrameIntervalType = 3,
  197. .dwFrameInterval[0] = cpu_to_le32(UVCG_FRM_INTERV_0_360P),
  198. .dwFrameInterval[1] = cpu_to_le32(UVCG_FRM_INTERV_1_360P),
  199. .dwFrameInterval[2] = cpu_to_le32(UVCG_FRM_INTERV_2_360P),
  200. };
  201. static u32 uvcg_frame_yuv_360p_dw_frame_interval[] = {
  202. [0] = UVCG_FRM_INTERV_0_360P,
  203. [1] = UVCG_FRM_INTERV_1_360P,
  204. [2] = UVCG_FRM_INTERV_2_360P,
  205. };
  206. static const struct uvcg_frame uvcg_frame_yuv_360p = {
  207. .fmt_type = UVCG_UNCOMPRESSED,
  208. .frame = {
  209. .b_length = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
  210. .b_descriptor_type = USB_DT_CS_INTERFACE,
  211. .b_descriptor_subtype = UVC_VS_FRAME_UNCOMPRESSED,
  212. .b_frame_index = 1,
  213. .bm_capabilities = 0,
  214. .w_width = UVCG_WIDTH_360P,
  215. .w_height = UVCG_HEIGHT_360P,
  216. .dw_min_bit_rate = UVCG_MIN_BITRATE_360P,
  217. .dw_max_bit_rate = UVCG_MAX_BITRATE_360P,
  218. .dw_max_video_frame_buffer_size = UVCG_MAX_VIDEO_FB_SZ_360P,
  219. .dw_default_frame_interval = UVCG_DEFAULT_FRM_INTERV_360P,
  220. .b_frame_interval_type = 3,
  221. },
  222. .dw_frame_interval = uvcg_frame_yuv_360p_dw_frame_interval,
  223. };
  224. static struct uvcg_frame_ptr uvcg_frame_ptr_yuv_360p = {
  225. .frm = (struct uvcg_frame *)&uvcg_frame_yuv_360p,
  226. };
  227. #define UVCG_WIDTH_720P 1280
  228. #define UVCG_HEIGHT_720P 720
  229. #define UVCG_MIN_BITRATE_720P 29491200
  230. #define UVCG_MAX_BITRATE_720P 29491200
  231. #define UVCG_MAX_VIDEO_FB_SZ_720P 1843200
  232. #define UVCG_FRM_INTERV_0_720P 5000000
  233. #define UVCG_DEFAULT_FRM_INTERV_720P UVCG_FRM_INTERV_0_720P
  234. static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
  235. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
  236. .bDescriptorType = USB_DT_CS_INTERFACE,
  237. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  238. .bFrameIndex = 2,
  239. .bmCapabilities = 0,
  240. .wWidth = cpu_to_le16(UVCG_WIDTH_720P),
  241. .wHeight = cpu_to_le16(UVCG_HEIGHT_720P),
  242. .dwMinBitRate = cpu_to_le32(UVCG_MIN_BITRATE_720P),
  243. .dwMaxBitRate = cpu_to_le32(UVCG_MAX_BITRATE_720P),
  244. .dwMaxVideoFrameBufferSize = cpu_to_le32(UVCG_MAX_VIDEO_FB_SZ_720P),
  245. .dwDefaultFrameInterval = cpu_to_le32(UVCG_DEFAULT_FRM_INTERV_720P),
  246. .bFrameIntervalType = 1,
  247. .dwFrameInterval[0] = cpu_to_le32(UVCG_FRM_INTERV_0_720P),
  248. };
  249. static u32 uvcg_frame_yuv_720p_dw_frame_interval[] = {
  250. [0] = UVCG_FRM_INTERV_0_720P,
  251. };
  252. static const struct uvcg_frame uvcg_frame_yuv_720p = {
  253. .fmt_type = UVCG_UNCOMPRESSED,
  254. .frame = {
  255. .b_length = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
  256. .b_descriptor_type = USB_DT_CS_INTERFACE,
  257. .b_descriptor_subtype = UVC_VS_FRAME_UNCOMPRESSED,
  258. .b_frame_index = 2,
  259. .bm_capabilities = 0,
  260. .w_width = UVCG_WIDTH_720P,
  261. .w_height = UVCG_HEIGHT_720P,
  262. .dw_min_bit_rate = UVCG_MIN_BITRATE_720P,
  263. .dw_max_bit_rate = UVCG_MAX_BITRATE_720P,
  264. .dw_max_video_frame_buffer_size = UVCG_MAX_VIDEO_FB_SZ_720P,
  265. .dw_default_frame_interval = UVCG_DEFAULT_FRM_INTERV_720P,
  266. .b_frame_interval_type = 1,
  267. },
  268. .dw_frame_interval = uvcg_frame_yuv_720p_dw_frame_interval,
  269. };
  270. static struct uvcg_frame_ptr uvcg_frame_ptr_yuv_720p = {
  271. .frm = (struct uvcg_frame *)&uvcg_frame_yuv_720p,
  272. };
  273. static struct uvcg_mjpeg uvcg_format_mjpeg = {
  274. .fmt = {
  275. .type = UVCG_MJPEG,
  276. /* add to .frames and fill .num_frames at runtime */
  277. .color_matching = (struct uvcg_color_matching *)&uvcg_color_matching,
  278. },
  279. .desc = {
  280. .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
  281. .bDescriptorType = USB_DT_CS_INTERFACE,
  282. .bDescriptorSubType = UVC_VS_FORMAT_MJPEG,
  283. .bFormatIndex = 2,
  284. .bNumFrameDescriptors = 2,
  285. .bmFlags = 0,
  286. .bDefaultFrameIndex = 1,
  287. .bAspectRatioX = 0,
  288. .bAspectRatioY = 0,
  289. .bmInterlaceFlags = 0,
  290. .bCopyProtect = 0,
  291. },
  292. };
  293. static struct uvcg_format_ptr uvcg_format_ptr_mjpeg = {
  294. .fmt = &uvcg_format_mjpeg.fmt,
  295. };
  296. DECLARE_UVC_FRAME_MJPEG(1);
  297. DECLARE_UVC_FRAME_MJPEG(3);
  298. static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
  299. .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
  300. .bDescriptorType = USB_DT_CS_INTERFACE,
  301. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  302. .bFrameIndex = 1,
  303. .bmCapabilities = 0,
  304. .wWidth = cpu_to_le16(UVCG_WIDTH_360P),
  305. .wHeight = cpu_to_le16(UVCG_HEIGHT_360P),
  306. .dwMinBitRate = cpu_to_le32(UVCG_MIN_BITRATE_360P),
  307. .dwMaxBitRate = cpu_to_le32(UVCG_MAX_BITRATE_360P),
  308. .dwMaxVideoFrameBufferSize = cpu_to_le32(UVCG_MAX_VIDEO_FB_SZ_360P),
  309. .dwDefaultFrameInterval = cpu_to_le32(UVCG_DEFAULT_FRM_INTERV_360P),
  310. .bFrameIntervalType = 3,
  311. .dwFrameInterval[0] = cpu_to_le32(UVCG_FRM_INTERV_0_360P),
  312. .dwFrameInterval[1] = cpu_to_le32(UVCG_FRM_INTERV_1_360P),
  313. .dwFrameInterval[2] = cpu_to_le32(UVCG_FRM_INTERV_2_360P),
  314. };
  315. static u32 uvcg_frame_mjpeg_360p_dw_frame_interval[] = {
  316. [0] = UVCG_FRM_INTERV_0_360P,
  317. [1] = UVCG_FRM_INTERV_1_360P,
  318. [2] = UVCG_FRM_INTERV_2_360P,
  319. };
  320. static const struct uvcg_frame uvcg_frame_mjpeg_360p = {
  321. .fmt_type = UVCG_MJPEG,
  322. .frame = {
  323. .b_length = UVC_DT_FRAME_MJPEG_SIZE(3),
  324. .b_descriptor_type = USB_DT_CS_INTERFACE,
  325. .b_descriptor_subtype = UVC_VS_FRAME_MJPEG,
  326. .b_frame_index = 1,
  327. .bm_capabilities = 0,
  328. .w_width = UVCG_WIDTH_360P,
  329. .w_height = UVCG_HEIGHT_360P,
  330. .dw_min_bit_rate = UVCG_MIN_BITRATE_360P,
  331. .dw_max_bit_rate = UVCG_MAX_BITRATE_360P,
  332. .dw_max_video_frame_buffer_size = UVCG_MAX_VIDEO_FB_SZ_360P,
  333. .dw_default_frame_interval = UVCG_DEFAULT_FRM_INTERV_360P,
  334. .b_frame_interval_type = 3,
  335. },
  336. .dw_frame_interval = uvcg_frame_mjpeg_360p_dw_frame_interval,
  337. };
  338. static struct uvcg_frame_ptr uvcg_frame_ptr_mjpeg_360p = {
  339. .frm = (struct uvcg_frame *)&uvcg_frame_mjpeg_360p,
  340. };
  341. static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
  342. .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
  343. .bDescriptorType = USB_DT_CS_INTERFACE,
  344. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  345. .bFrameIndex = 2,
  346. .bmCapabilities = 0,
  347. .wWidth = cpu_to_le16(UVCG_WIDTH_720P),
  348. .wHeight = cpu_to_le16(UVCG_HEIGHT_720P),
  349. .dwMinBitRate = cpu_to_le32(UVCG_MIN_BITRATE_720P),
  350. .dwMaxBitRate = cpu_to_le32(UVCG_MAX_BITRATE_720P),
  351. .dwMaxVideoFrameBufferSize = cpu_to_le32(UVCG_MAX_VIDEO_FB_SZ_720P),
  352. .dwDefaultFrameInterval = cpu_to_le32(UVCG_DEFAULT_FRM_INTERV_720P),
  353. .bFrameIntervalType = 1,
  354. .dwFrameInterval[0] = cpu_to_le32(UVCG_FRM_INTERV_0_720P),
  355. };
  356. static u32 uvcg_frame_mjpeg_720p_dw_frame_interval[] = {
  357. [0] = UVCG_FRM_INTERV_0_720P,
  358. };
  359. static const struct uvcg_frame uvcg_frame_mjpeg_720p = {
  360. .fmt_type = UVCG_MJPEG,
  361. .frame = {
  362. .b_length = UVC_DT_FRAME_MJPEG_SIZE(1),
  363. .b_descriptor_type = USB_DT_CS_INTERFACE,
  364. .b_descriptor_subtype = UVC_VS_FRAME_MJPEG,
  365. .b_frame_index = 2,
  366. .bm_capabilities = 0,
  367. .w_width = UVCG_WIDTH_720P,
  368. .w_height = UVCG_HEIGHT_720P,
  369. .dw_min_bit_rate = UVCG_MIN_BITRATE_720P,
  370. .dw_max_bit_rate = UVCG_MAX_BITRATE_720P,
  371. .dw_max_video_frame_buffer_size = UVCG_MAX_VIDEO_FB_SZ_720P,
  372. .dw_default_frame_interval = UVCG_DEFAULT_FRM_INTERV_720P,
  373. .b_frame_interval_type = 1,
  374. },
  375. .dw_frame_interval = uvcg_frame_mjpeg_720p_dw_frame_interval,
  376. };
  377. static struct uvcg_frame_ptr uvcg_frame_ptr_mjpeg_720p = {
  378. .frm = (struct uvcg_frame *)&uvcg_frame_mjpeg_720p,
  379. };
  380. static struct uvcg_streaming_header uvcg_streaming_header = {
  381. };
  382. static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
  383. (const struct uvc_descriptor_header *) &uvc_control_header,
  384. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  385. (const struct uvc_descriptor_header *) &uvc_processing,
  386. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  387. NULL,
  388. };
  389. static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
  390. (const struct uvc_descriptor_header *) &uvc_control_header,
  391. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  392. (const struct uvc_descriptor_header *) &uvc_processing,
  393. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  394. NULL,
  395. };
  396. static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
  397. (const struct uvc_descriptor_header *) &uvc_input_header,
  398. (const struct uvc_descriptor_header *) &uvcg_format_yuv.desc,
  399. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  400. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  401. (const struct uvc_descriptor_header *) &uvcg_color_matching.desc,
  402. (const struct uvc_descriptor_header *) &uvcg_format_mjpeg.desc,
  403. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  404. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  405. (const struct uvc_descriptor_header *) &uvcg_color_matching.desc,
  406. NULL,
  407. };
  408. static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
  409. (const struct uvc_descriptor_header *) &uvc_input_header,
  410. (const struct uvc_descriptor_header *) &uvcg_format_yuv.desc,
  411. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  412. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  413. (const struct uvc_descriptor_header *) &uvcg_color_matching.desc,
  414. (const struct uvc_descriptor_header *) &uvcg_format_mjpeg.desc,
  415. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  416. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  417. (const struct uvc_descriptor_header *) &uvcg_color_matching.desc,
  418. NULL,
  419. };
  420. static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
  421. (const struct uvc_descriptor_header *) &uvc_input_header,
  422. (const struct uvc_descriptor_header *) &uvcg_format_yuv.desc,
  423. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  424. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  425. (const struct uvc_descriptor_header *) &uvcg_color_matching.desc,
  426. (const struct uvc_descriptor_header *) &uvcg_format_mjpeg.desc,
  427. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  428. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  429. (const struct uvc_descriptor_header *) &uvcg_color_matching.desc,
  430. NULL,
  431. };
  432. /* --------------------------------------------------------------------------
  433. * USB configuration
  434. */
  435. static int
  436. webcam_config_bind(struct usb_configuration *c)
  437. {
  438. int status = 0;
  439. f_uvc = usb_get_function(fi_uvc);
  440. if (IS_ERR(f_uvc))
  441. return PTR_ERR(f_uvc);
  442. status = usb_add_function(c, f_uvc);
  443. if (status < 0)
  444. usb_put_function(f_uvc);
  445. return status;
  446. }
  447. static struct usb_configuration webcam_config_driver = {
  448. .label = webcam_config_label,
  449. .bConfigurationValue = 1,
  450. .iConfiguration = 0, /* dynamic */
  451. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  452. .MaxPower = CONFIG_USB_GADGET_VBUS_DRAW,
  453. };
  454. static int
  455. webcam_unbind(struct usb_composite_dev *cdev)
  456. {
  457. if (!IS_ERR_OR_NULL(f_uvc))
  458. usb_put_function(f_uvc);
  459. if (!IS_ERR_OR_NULL(fi_uvc))
  460. usb_put_function_instance(fi_uvc);
  461. return 0;
  462. }
  463. static int
  464. webcam_bind(struct usb_composite_dev *cdev)
  465. {
  466. struct f_uvc_opts *uvc_opts;
  467. int ret;
  468. fi_uvc = usb_get_function_instance("uvc");
  469. if (IS_ERR(fi_uvc))
  470. return PTR_ERR(fi_uvc);
  471. uvc_opts = container_of(fi_uvc, struct f_uvc_opts, func_inst);
  472. uvc_opts->streaming_interval = streaming_interval;
  473. uvc_opts->streaming_maxpacket = streaming_maxpacket;
  474. uvc_opts->streaming_maxburst = streaming_maxburst;
  475. uvc_opts->fs_control = uvc_fs_control_cls;
  476. uvc_opts->ss_control = uvc_ss_control_cls;
  477. uvc_opts->fs_streaming = uvc_fs_streaming_cls;
  478. uvc_opts->hs_streaming = uvc_hs_streaming_cls;
  479. uvc_opts->ss_streaming = uvc_ss_streaming_cls;
  480. INIT_LIST_HEAD(&uvcg_format_yuv.fmt.frames);
  481. list_add_tail(&uvcg_frame_ptr_yuv_360p.entry, &uvcg_format_yuv.fmt.frames);
  482. list_add_tail(&uvcg_frame_ptr_yuv_720p.entry, &uvcg_format_yuv.fmt.frames);
  483. uvcg_format_yuv.fmt.num_frames = 2;
  484. INIT_LIST_HEAD(&uvcg_format_mjpeg.fmt.frames);
  485. list_add_tail(&uvcg_frame_ptr_mjpeg_360p.entry, &uvcg_format_mjpeg.fmt.frames);
  486. list_add_tail(&uvcg_frame_ptr_mjpeg_720p.entry, &uvcg_format_mjpeg.fmt.frames);
  487. uvcg_format_mjpeg.fmt.num_frames = 2;
  488. INIT_LIST_HEAD(&uvcg_streaming_header.formats);
  489. list_add_tail(&uvcg_format_ptr_yuv.entry, &uvcg_streaming_header.formats);
  490. list_add_tail(&uvcg_format_ptr_mjpeg.entry, &uvcg_streaming_header.formats);
  491. uvcg_streaming_header.num_fmt = 2;
  492. uvc_opts->header = &uvcg_streaming_header;
  493. /* Allocate string descriptor numbers ... note that string contents
  494. * can be overridden by the composite_dev glue.
  495. */
  496. ret = usb_string_ids_tab(cdev, webcam_strings);
  497. if (ret < 0)
  498. goto error;
  499. webcam_device_descriptor.iManufacturer =
  500. webcam_strings[USB_GADGET_MANUFACTURER_IDX].id;
  501. webcam_device_descriptor.iProduct =
  502. webcam_strings[USB_GADGET_PRODUCT_IDX].id;
  503. webcam_config_driver.iConfiguration =
  504. webcam_strings[STRING_DESCRIPTION_IDX].id;
  505. /* Register our configuration. */
  506. if ((ret = usb_add_config(cdev, &webcam_config_driver,
  507. webcam_config_bind)) < 0)
  508. goto error;
  509. usb_composite_overwrite_options(cdev, &coverwrite);
  510. INFO(cdev, "Webcam Video Gadget\n");
  511. return 0;
  512. error:
  513. usb_put_function_instance(fi_uvc);
  514. return ret;
  515. }
  516. /* --------------------------------------------------------------------------
  517. * Driver
  518. */
  519. static struct usb_composite_driver webcam_driver = {
  520. .name = "g_webcam",
  521. .dev = &webcam_device_descriptor,
  522. .strings = webcam_device_strings,
  523. .max_speed = USB_SPEED_SUPER,
  524. .bind = webcam_bind,
  525. .unbind = webcam_unbind,
  526. };
  527. module_usb_composite_driver(webcam_driver);
  528. MODULE_AUTHOR("Laurent Pinchart");
  529. MODULE_DESCRIPTION("Webcam Video Gadget");
  530. MODULE_LICENSE("GPL");