webcam.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. static unsigned int trace;
  26. module_param(trace, uint, S_IRUGO|S_IWUSR);
  27. MODULE_PARM_DESC(trace, "Trace level bitmask");
  28. //#undef USE_UAC1
  29. #define USE_UAC1
  30. #ifdef USE_UAC1
  31. #include "u_uac1.h"
  32. /* Playback(USB-IN) Default Stereo - Fl/Fr */
  33. static int p_chmask = UAC1_DEF_PCHMASK;
  34. module_param(p_chmask, uint, S_IRUGO);
  35. MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");
  36. /* Playback Default 48 KHz */
  37. static int p_srate = UAC1_DEF_PSRATE;
  38. module_param(p_srate, uint, S_IRUGO);
  39. MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");
  40. /* Playback Default 16bits/sample */
  41. static int p_ssize = UAC1_DEF_PSSIZE;
  42. module_param(p_ssize, uint, S_IRUGO);
  43. MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");
  44. /* Capture(USB-OUT) Default Stereo - Fl/Fr */
  45. static int c_chmask = UAC1_DEF_CCHMASK;
  46. module_param(c_chmask, uint, S_IRUGO);
  47. MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");
  48. /* Capture Default 48 KHz */
  49. static int c_srate = UAC1_DEF_CSRATE;
  50. module_param(c_srate, uint, S_IRUGO);
  51. MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");
  52. /* Capture Default 16bits/sample */
  53. static int c_ssize = UAC1_DEF_CSSIZE;
  54. module_param(c_ssize, uint, S_IRUGO);
  55. MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");
  56. static struct usb_function_instance *fi_uac1;
  57. static struct usb_function *f_uac1;
  58. #endif
  59. /* --------------------------------------------------------------------------
  60. * Device descriptor
  61. */
  62. #define WEBCAM_VENDOR_ID 0x1d6b /* Linux Foundation */
  63. #define WEBCAM_PRODUCT_ID 0x0102 /* Webcam A/V gadget */
  64. #define WEBCAM_DEVICE_BCD 0x0010 /* 0.10 */
  65. static char webcam_vendor_label[] = "Linux Foundation";
  66. static char webcam_product_label[] = "Webcam gadget";
  67. static char webcam_config_label[] = "Video";
  68. /* string IDs are assigned dynamically */
  69. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  70. static struct usb_string webcam_strings[] = {
  71. [USB_GADGET_MANUFACTURER_IDX].s = webcam_vendor_label,
  72. [USB_GADGET_PRODUCT_IDX].s = webcam_product_label,
  73. [USB_GADGET_SERIAL_IDX].s = "",
  74. [STRING_DESCRIPTION_IDX].s = webcam_config_label,
  75. { }
  76. };
  77. static struct usb_gadget_strings webcam_stringtab = {
  78. .language = 0x0409, /* en-us */
  79. .strings = webcam_strings,
  80. };
  81. static struct usb_gadget_strings *webcam_device_strings[] = {
  82. &webcam_stringtab,
  83. NULL,
  84. };
  85. static struct usb_function_instance *fi_uvc;
  86. static struct usb_function *f_uvc;
  87. static struct usb_device_descriptor webcam_device_descriptor = {
  88. .bLength = USB_DT_DEVICE_SIZE,
  89. .bDescriptorType = USB_DT_DEVICE,
  90. /* .bcdUSB = DYNAMIC */
  91. .bDeviceClass = USB_CLASS_MISC,
  92. .bDeviceSubClass = 0x02,
  93. .bDeviceProtocol = 0x01,
  94. .bMaxPacketSize0 = 0, /* dynamic */
  95. .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID),
  96. .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID),
  97. .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD),
  98. .iManufacturer = 0, /* dynamic */
  99. .iProduct = 0, /* dynamic */
  100. .iSerialNumber = 0, /* dynamic */
  101. .bNumConfigurations = 0, /* dynamic */
  102. };
  103. DECLARE_UVC_HEADER_DESCRIPTOR(1);
  104. static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = {
  105. .bLength = UVC_DT_HEADER_SIZE(1),
  106. .bDescriptorType = USB_DT_CS_INTERFACE,
  107. .bDescriptorSubType = UVC_VC_HEADER,
  108. .bcdUVC = cpu_to_le16(0x0100),
  109. .wTotalLength = 0, /* dynamic */
  110. .dwClockFrequency = cpu_to_le32(48000000),
  111. .bInCollection = 0, /* dynamic */
  112. .baInterfaceNr[0] = 0, /* dynamic */
  113. };
  114. static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = {
  115. .bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3),
  116. .bDescriptorType = USB_DT_CS_INTERFACE,
  117. .bDescriptorSubType = UVC_VC_INPUT_TERMINAL,
  118. .bTerminalID = 1,
  119. .wTerminalType = cpu_to_le16(0x0201),
  120. .bAssocTerminal = 0,
  121. .iTerminal = 0,
  122. .wObjectiveFocalLengthMin = cpu_to_le16(0),
  123. .wObjectiveFocalLengthMax = cpu_to_le16(0),
  124. .wOcularFocalLength = cpu_to_le16(0),
  125. .bControlSize = 3,
  126. .bmControls[0] = 2,
  127. .bmControls[1] = 0,
  128. .bmControls[2] = 0,
  129. };
  130. static const struct uvc_processing_unit_descriptor uvc_processing = {
  131. .bLength = UVC_DT_PROCESSING_UNIT_SIZE(2),
  132. .bDescriptorType = USB_DT_CS_INTERFACE,
  133. .bDescriptorSubType = UVC_VC_PROCESSING_UNIT,
  134. .bUnitID = 2,
  135. .bSourceID = 1,
  136. .wMaxMultiplier = cpu_to_le16(16*1024),
  137. .bControlSize = 2,
  138. .bmControls[0] = 1,
  139. .bmControls[1] = 0,
  140. .iProcessing = 0,
  141. };
  142. static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
  143. .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE,
  144. .bDescriptorType = USB_DT_CS_INTERFACE,
  145. .bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL,
  146. .bTerminalID = 3,
  147. .wTerminalType = cpu_to_le16(0x0101),
  148. .bAssocTerminal = 0,
  149. .bSourceID = 2,
  150. .iTerminal = 0,
  151. };
  152. DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
  153. static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
  154. .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2),
  155. .bDescriptorType = USB_DT_CS_INTERFACE,
  156. .bDescriptorSubType = UVC_VS_INPUT_HEADER,
  157. .bNumFormats = 2,
  158. .wTotalLength = 0, /* dynamic */
  159. .bEndpointAddress = 0, /* dynamic */
  160. .bmInfo = 0,
  161. .bTerminalLink = 3,
  162. .bStillCaptureMethod = 0,
  163. .bTriggerSupport = 0,
  164. .bTriggerUsage = 0,
  165. .bControlSize = 1,
  166. .bmaControls[0][0] = 0,
  167. .bmaControls[1][0] = 4,
  168. };
  169. static const struct uvc_format_uncompressed uvc_format_yuv = {
  170. .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
  171. .bDescriptorType = USB_DT_CS_INTERFACE,
  172. .bDescriptorSubType = UVC_VS_FORMAT_UNCOMPRESSED,
  173. .bFormatIndex = 1,
  174. .bNumFrameDescriptors = 2,
  175. .guidFormat =
  176. { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
  177. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
  178. .bBitsPerPixel = 16,
  179. .bDefaultFrameIndex = 1,
  180. .bAspectRatioX = 0,
  181. .bAspectRatioY = 0,
  182. .bmInterfaceFlags = 0,
  183. .bCopyProtect = 0,
  184. };
  185. DECLARE_UVC_FRAME_UNCOMPRESSED(1);
  186. DECLARE_UVC_FRAME_UNCOMPRESSED(3);
  187. static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
  188. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
  189. .bDescriptorType = USB_DT_CS_INTERFACE,
  190. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  191. .bFrameIndex = 1,
  192. .bmCapabilities = 0,
  193. .wWidth = cpu_to_le16(640),
  194. .wHeight = cpu_to_le16(360),
  195. .dwMinBitRate = cpu_to_le32(18432000),
  196. .dwMaxBitRate = cpu_to_le32(55296000),
  197. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  198. .dwDefaultFrameInterval = cpu_to_le32(666666),
  199. .bFrameIntervalType = 3,
  200. .dwFrameInterval[0] = cpu_to_le32(666666),
  201. .dwFrameInterval[1] = cpu_to_le32(1000000),
  202. .dwFrameInterval[2] = cpu_to_le32(5000000),
  203. };
  204. static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
  205. .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
  206. .bDescriptorType = USB_DT_CS_INTERFACE,
  207. .bDescriptorSubType = UVC_VS_FRAME_UNCOMPRESSED,
  208. .bFrameIndex = 2,
  209. .bmCapabilities = 0,
  210. .wWidth = cpu_to_le16(1280),
  211. .wHeight = cpu_to_le16(720),
  212. .dwMinBitRate = cpu_to_le32(29491200),
  213. .dwMaxBitRate = cpu_to_le32(29491200),
  214. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  215. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  216. .bFrameIntervalType = 1,
  217. .dwFrameInterval[0] = cpu_to_le32(5000000),
  218. };
  219. static const struct uvc_format_mjpeg uvc_format_mjpg = {
  220. .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
  221. .bDescriptorType = USB_DT_CS_INTERFACE,
  222. .bDescriptorSubType = UVC_VS_FORMAT_MJPEG,
  223. .bFormatIndex = 2,
  224. .bNumFrameDescriptors = 2,
  225. .bmFlags = 0,
  226. .bDefaultFrameIndex = 1,
  227. .bAspectRatioX = 0,
  228. .bAspectRatioY = 0,
  229. .bmInterfaceFlags = 0,
  230. .bCopyProtect = 0,
  231. };
  232. DECLARE_UVC_FRAME_MJPEG(1);
  233. DECLARE_UVC_FRAME_MJPEG(3);
  234. static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
  235. .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
  236. .bDescriptorType = USB_DT_CS_INTERFACE,
  237. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  238. .bFrameIndex = 1,
  239. .bmCapabilities = 0,
  240. .wWidth = cpu_to_le16(640),
  241. .wHeight = cpu_to_le16(360),
  242. .dwMinBitRate = cpu_to_le32(18432000),
  243. .dwMaxBitRate = cpu_to_le32(55296000),
  244. .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
  245. .dwDefaultFrameInterval = cpu_to_le32(666666),
  246. .bFrameIntervalType = 3,
  247. .dwFrameInterval[0] = cpu_to_le32(666666),
  248. .dwFrameInterval[1] = cpu_to_le32(1000000),
  249. .dwFrameInterval[2] = cpu_to_le32(5000000),
  250. };
  251. static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
  252. .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
  253. .bDescriptorType = USB_DT_CS_INTERFACE,
  254. .bDescriptorSubType = UVC_VS_FRAME_MJPEG,
  255. .bFrameIndex = 2,
  256. .bmCapabilities = 0,
  257. .wWidth = cpu_to_le16(1280),
  258. .wHeight = cpu_to_le16(720),
  259. .dwMinBitRate = cpu_to_le32(29491200),
  260. .dwMaxBitRate = cpu_to_le32(29491200),
  261. .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
  262. .dwDefaultFrameInterval = cpu_to_le32(5000000),
  263. .bFrameIntervalType = 1,
  264. .dwFrameInterval[0] = cpu_to_le32(5000000),
  265. };
  266. static const struct uvc_format2_h264 uvc_format_h264 = {
  267. .bLength = 28,
  268. .bDescriptorType = 36,
  269. .bDescriptorSubtype = 16 ,
  270. .bFormatIndex = 2,
  271. .bNumFrameDescriptors = 3,
  272. .guidFormat = { 'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00,
  273. 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
  274. .bBitsPerPixel = 16,
  275. .bDefaultFrameIndex = 1,
  276. .bAspectRatioX = 0,
  277. .bAspectRatioY = 0,
  278. .bmInterlaceFlags = 0x00,
  279. .bCopyProtect = 0,
  280. .bVariableSize = 1,
  281. };
  282. DECLARE_UVC_FRAME_H2642(2);
  283. static const struct UVC_FRAME_H2642(2) uvc_frame_h264_1080p = {
  284. .bLength = UVC_DT_FRAME_H264_SIZE2(2) ,
  285. .bDescriptorType = 36,
  286. .bDescriptorSubtype = 17 ,
  287. .bFrameIndex = 1 ,
  288. .bmCapabilities = 0x00,
  289. .wWidth = cpu_to_le32(1920),
  290. .wHeight = cpu_to_le32(1080),
  291. .dwMinBitRate = cpu_to_le32(29491200),
  292. .dwMaxBitRate = cpu_to_le32(29491200),
  293. .dwDefaultFrameInterval = cpu_to_le32(400000),
  294. .bFrameIntervalType = 2,
  295. .dwBytesPerLine = 0,
  296. .dwFrameInterval[0] = cpu_to_le32(400000),
  297. .dwFrameInterval[1] = cpu_to_le32(666666),
  298. };
  299. static const struct UVC_FRAME_H2642(2) uvc_frame_h264_720p = {
  300. .bLength = UVC_DT_FRAME_H264_SIZE2(2) ,
  301. .bDescriptorType = 36,
  302. .bDescriptorSubtype = 17 ,
  303. .bFrameIndex = 2 ,
  304. .bmCapabilities = 0x00,
  305. .wWidth = cpu_to_le32(1280),
  306. .wHeight = cpu_to_le32(720),
  307. .dwMinBitRate = cpu_to_le32(29491200),//cpu_to_le32(497664000),
  308. .dwMaxBitRate = cpu_to_le32(29491200),//cpu_to_le32(995328000),
  309. .dwDefaultFrameInterval = cpu_to_le32(400000),
  310. .bFrameIntervalType = 2,
  311. .dwBytesPerLine = 0,
  312. .dwFrameInterval[0] = cpu_to_le32(400000),
  313. .dwFrameInterval[1] = cpu_to_le32(666666),
  314. };
  315. static const struct UVC_FRAME_H2642(2) uvc_frame_h264_480p = {
  316. .bLength = UVC_DT_FRAME_H264_SIZE2(2) ,
  317. .bDescriptorType = 36,
  318. .bDescriptorSubtype = 17 ,
  319. .bFrameIndex = 3 ,
  320. .bmCapabilities = 0x00,
  321. .wWidth = cpu_to_le32(720),
  322. .wHeight = cpu_to_le32(480),
  323. .dwMinBitRate = cpu_to_le32(29491200),
  324. .dwMaxBitRate = cpu_to_le32(29491200),
  325. .dwDefaultFrameInterval = cpu_to_le32(400000),
  326. .bFrameIntervalType = 2,
  327. .dwBytesPerLine = 0,
  328. .dwFrameInterval[0] = cpu_to_le32(400000),
  329. .dwFrameInterval[1] = cpu_to_le32(666666),
  330. };
  331. static const struct uvc_color_matching_descriptor uvc_color_matching = {
  332. .bLength = UVC_DT_COLOR_MATCHING_SIZE,
  333. .bDescriptorType = USB_DT_CS_INTERFACE,
  334. .bDescriptorSubType = UVC_VS_COLORFORMAT,
  335. .bColorPrimaries = 1,
  336. .bTransferCharacteristics = 1,
  337. .bMatrixCoefficients = 4,
  338. };
  339. static const struct uvc_descriptor_header * const uvc_fs_control_cls[] = {
  340. (const struct uvc_descriptor_header *) &uvc_control_header,
  341. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  342. (const struct uvc_descriptor_header *) &uvc_processing,
  343. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  344. NULL,
  345. };
  346. static const struct uvc_descriptor_header * const uvc_ss_control_cls[] = {
  347. (const struct uvc_descriptor_header *) &uvc_control_header,
  348. (const struct uvc_descriptor_header *) &uvc_camera_terminal,
  349. (const struct uvc_descriptor_header *) &uvc_processing,
  350. (const struct uvc_descriptor_header *) &uvc_output_terminal,
  351. NULL,
  352. };
  353. static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
  354. (const struct uvc_descriptor_header *) &uvc_input_header,
  355. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  356. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  357. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  358. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  359. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  360. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  361. (const struct uvc_descriptor_header *) &uvc_format_h264,
  362. (const struct uvc_descriptor_header *) &uvc_frame_h264_1080p,
  363. (const struct uvc_descriptor_header *) &uvc_frame_h264_720p,
  364. (const struct uvc_descriptor_header *) &uvc_frame_h264_480p,
  365. (const struct uvc_descriptor_header *) &uvc_color_matching,
  366. NULL,
  367. };
  368. static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
  369. (const struct uvc_descriptor_header *) &uvc_input_header,
  370. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  371. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  372. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  373. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  374. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  375. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  376. (const struct uvc_descriptor_header *) &uvc_format_h264,
  377. (const struct uvc_descriptor_header *) &uvc_frame_h264_1080p,
  378. (const struct uvc_descriptor_header *) &uvc_frame_h264_720p,
  379. (const struct uvc_descriptor_header *) &uvc_frame_h264_480p,
  380. (const struct uvc_descriptor_header *) &uvc_color_matching,
  381. NULL,
  382. };
  383. static const struct uvc_descriptor_header * const uvc_ss_streaming_cls[] = {
  384. (const struct uvc_descriptor_header *) &uvc_input_header,
  385. (const struct uvc_descriptor_header *) &uvc_format_yuv,
  386. (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
  387. (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
  388. (const struct uvc_descriptor_header *) &uvc_format_mjpg,
  389. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
  390. (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
  391. (const struct uvc_descriptor_header *) &uvc_format_h264,
  392. (const struct uvc_descriptor_header *) &uvc_frame_h264_1080p,
  393. (const struct uvc_descriptor_header *) &uvc_frame_h264_720p,
  394. (const struct uvc_descriptor_header *) &uvc_frame_h264_480p,
  395. (const struct uvc_descriptor_header *) &uvc_color_matching,
  396. NULL,
  397. };
  398. /* --------------------------------------------------------------------------
  399. * USB configuration
  400. */
  401. static int
  402. webcam_config_bind(struct usb_configuration *c)
  403. {
  404. int status = 0;
  405. f_uvc = usb_get_function(fi_uvc);
  406. if (IS_ERR(f_uvc))
  407. return PTR_ERR(f_uvc);
  408. status = usb_add_function(c, f_uvc);
  409. if (status < 0)
  410. usb_put_function(f_uvc);
  411. #ifdef USE_UAC1
  412. f_uac1 = usb_get_function(fi_uac1);
  413. if (IS_ERR(f_uac1)) {
  414. printk("%s:%d err\n", __func__, __LINE__);
  415. status = PTR_ERR(f_uac1);
  416. return status;
  417. }
  418. status = usb_add_function(c, f_uac1);
  419. if (status < 0) {
  420. printk("%s:%d err\n", __func__, __LINE__);
  421. usb_put_function(f_uac1);
  422. return status;
  423. }
  424. #endif
  425. return status;
  426. }
  427. static struct usb_configuration webcam_config_driver = {
  428. .label = webcam_config_label,
  429. .bConfigurationValue = 1,
  430. .iConfiguration = 0, /* dynamic */
  431. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  432. .MaxPower = CONFIG_USB_GADGET_VBUS_DRAW,
  433. };
  434. static int
  435. webcam_unbind(struct usb_composite_dev *cdev)
  436. {
  437. #ifdef USE_UAC1
  438. if (!IS_ERR_OR_NULL(f_uac1))
  439. usb_put_function(f_uac1);
  440. if (!IS_ERR_OR_NULL(fi_uac1))
  441. usb_put_function_instance(fi_uac1);
  442. #endif
  443. if (!IS_ERR_OR_NULL(f_uvc))
  444. usb_put_function(f_uvc);
  445. if (!IS_ERR_OR_NULL(fi_uvc))
  446. usb_put_function_instance(fi_uvc);
  447. return 0;
  448. }
  449. static int
  450. webcam_bind(struct usb_composite_dev *cdev)
  451. {
  452. struct f_uvc_opts *uvc_opts;
  453. int ret;
  454. #ifdef USE_UAC1
  455. struct f_uac1_opts *uac1_opts;
  456. fi_uac1 = usb_get_function_instance("uac1");
  457. if (IS_ERR(fi_uac1)) {
  458. printk("%s:%d err\n", __func__, __LINE__);
  459. return PTR_ERR(fi_uac1);
  460. }
  461. uac1_opts = container_of(fi_uac1, struct f_uac1_opts, func_inst);
  462. uac1_opts->p_chmask = p_chmask;
  463. uac1_opts->p_srate = p_srate;
  464. uac1_opts->p_ssize = p_ssize;
  465. uac1_opts->c_chmask = c_chmask;
  466. uac1_opts->c_srate = c_srate;
  467. uac1_opts->c_ssize = c_ssize;
  468. uac1_opts->req_number = UAC1_DEF_REQ_NUM;
  469. #endif
  470. fi_uvc = usb_get_function_instance("uvc");
  471. if (IS_ERR(fi_uvc))
  472. return PTR_ERR(fi_uvc);
  473. uvc_opts = container_of(fi_uvc, struct f_uvc_opts, func_inst);
  474. uvc_opts->streaming_interval = streaming_interval;
  475. uvc_opts->streaming_maxpacket = streaming_maxpacket;
  476. uvc_opts->streaming_maxburst = streaming_maxburst;
  477. uvc_set_trace_param(trace);
  478. uvc_opts->fs_control = uvc_fs_control_cls;
  479. uvc_opts->ss_control = uvc_ss_control_cls;
  480. uvc_opts->fs_streaming = uvc_fs_streaming_cls;
  481. uvc_opts->hs_streaming = uvc_hs_streaming_cls;
  482. uvc_opts->ss_streaming = uvc_ss_streaming_cls;
  483. /* Allocate string descriptor numbers ... note that string contents
  484. * can be overridden by the composite_dev glue.
  485. */
  486. ret = usb_string_ids_tab(cdev, webcam_strings);
  487. if (ret < 0)
  488. goto error;
  489. webcam_device_descriptor.iManufacturer =
  490. webcam_strings[USB_GADGET_MANUFACTURER_IDX].id;
  491. webcam_device_descriptor.iProduct =
  492. webcam_strings[USB_GADGET_PRODUCT_IDX].id;
  493. webcam_config_driver.iConfiguration =
  494. webcam_strings[STRING_DESCRIPTION_IDX].id;
  495. /* Register our configuration. */
  496. if ((ret = usb_add_config(cdev, &webcam_config_driver,
  497. webcam_config_bind)) < 0)
  498. goto error;
  499. usb_composite_overwrite_options(cdev, &coverwrite);
  500. INFO(cdev, "Webcam Video Gadget\n");
  501. return 0;
  502. error:
  503. #ifdef USE_UAC1
  504. usb_put_function_instance(fi_uac1);
  505. #endif
  506. usb_put_function_instance(fi_uvc);
  507. return ret;
  508. }
  509. /* --------------------------------------------------------------------------
  510. * Driver
  511. */
  512. static struct usb_composite_driver webcam_driver = {
  513. .name = "g_webcam",
  514. .dev = &webcam_device_descriptor,
  515. .strings = webcam_device_strings,
  516. .max_speed = USB_SPEED_SUPER,
  517. .bind = webcam_bind,
  518. .unbind = webcam_unbind,
  519. };
  520. module_usb_composite_driver(webcam_driver);
  521. MODULE_AUTHOR("Laurent Pinchart");
  522. MODULE_DESCRIPTION("Webcam Video Gadget");
  523. MODULE_LICENSE("GPL");