f_uvc.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * uvc_gadget.c -- USB Video Class Gadget driver
  4. *
  5. * Copyright (C) 2009-2010
  6. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. */
  8. #include <linux/device.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/string.h>
  16. #include <linux/usb/ch9.h>
  17. #include <linux/usb/gadget.h>
  18. #include <linux/usb/g_uvc.h>
  19. #include <linux/usb/video.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/wait.h>
  22. #include <media/v4l2-dev.h>
  23. #include <media/v4l2-event.h>
  24. #include "uvc.h"
  25. #include "uvc_configfs.h"
  26. #include "uvc_v4l2.h"
  27. #include "uvc_video.h"
  28. unsigned int uvc_gadget_trace_param;
  29. module_param_named(trace, uvc_gadget_trace_param, uint, 0644);
  30. MODULE_PARM_DESC(trace, "Trace level bitmask");
  31. /* --------------------------------------------------------------------------
  32. * Function descriptors
  33. */
  34. /* string IDs are assigned dynamically */
  35. static struct usb_string uvc_en_us_strings[] = {
  36. /* [UVC_STRING_CONTROL_IDX].s = DYNAMIC, */
  37. [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
  38. { }
  39. };
  40. static struct usb_gadget_strings uvc_stringtab = {
  41. .language = 0x0409, /* en-us */
  42. .strings = uvc_en_us_strings,
  43. };
  44. static struct usb_gadget_strings *uvc_function_strings[] = {
  45. &uvc_stringtab,
  46. NULL,
  47. };
  48. #define UVC_INTF_VIDEO_CONTROL 0
  49. #define UVC_INTF_VIDEO_STREAMING 1
  50. #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
  51. static struct usb_interface_assoc_descriptor uvc_iad = {
  52. .bLength = sizeof(uvc_iad),
  53. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  54. .bFirstInterface = 0,
  55. .bInterfaceCount = 2,
  56. .bFunctionClass = USB_CLASS_VIDEO,
  57. .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
  58. .bFunctionProtocol = 0x00,
  59. .iFunction = 0,
  60. };
  61. static struct usb_interface_descriptor uvc_control_intf = {
  62. .bLength = USB_DT_INTERFACE_SIZE,
  63. .bDescriptorType = USB_DT_INTERFACE,
  64. .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
  65. .bAlternateSetting = 0,
  66. .bNumEndpoints = 1,
  67. .bInterfaceClass = USB_CLASS_VIDEO,
  68. .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
  69. .bInterfaceProtocol = 0x00,
  70. .iInterface = 0,
  71. };
  72. static struct usb_endpoint_descriptor uvc_control_ep = {
  73. .bLength = USB_DT_ENDPOINT_SIZE,
  74. .bDescriptorType = USB_DT_ENDPOINT,
  75. .bEndpointAddress = USB_DIR_IN,
  76. .bmAttributes = USB_ENDPOINT_XFER_INT,
  77. .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  78. .bInterval = 8,
  79. };
  80. static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp = {
  81. .bLength = sizeof(uvc_ss_control_comp),
  82. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  83. /* The following 3 values can be tweaked if necessary. */
  84. .bMaxBurst = 0,
  85. .bmAttributes = 0,
  86. .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  87. };
  88. static struct uvc_control_endpoint_descriptor uvc_control_cs_ep = {
  89. .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
  90. .bDescriptorType = USB_DT_CS_ENDPOINT,
  91. .bDescriptorSubType = UVC_EP_INTERRUPT,
  92. .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  93. };
  94. static struct usb_interface_descriptor uvc_streaming_intf_alt0 = {
  95. .bLength = USB_DT_INTERFACE_SIZE,
  96. .bDescriptorType = USB_DT_INTERFACE,
  97. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  98. .bAlternateSetting = 0,
  99. .bNumEndpoints = 0,
  100. .bInterfaceClass = USB_CLASS_VIDEO,
  101. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  102. .bInterfaceProtocol = 0x00,
  103. .iInterface = 0,
  104. };
  105. static struct usb_interface_descriptor uvc_streaming_intf_alt1 = {
  106. .bLength = USB_DT_INTERFACE_SIZE,
  107. .bDescriptorType = USB_DT_INTERFACE,
  108. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  109. .bAlternateSetting = 1,
  110. .bNumEndpoints = 1,
  111. .bInterfaceClass = USB_CLASS_VIDEO,
  112. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  113. .bInterfaceProtocol = 0x00,
  114. .iInterface = 0,
  115. };
  116. static struct usb_endpoint_descriptor uvc_fs_streaming_ep = {
  117. .bLength = USB_DT_ENDPOINT_SIZE,
  118. .bDescriptorType = USB_DT_ENDPOINT,
  119. .bEndpointAddress = USB_DIR_IN,
  120. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  121. | USB_ENDPOINT_XFER_ISOC,
  122. /*
  123. * The wMaxPacketSize and bInterval values will be initialized from
  124. * module parameters.
  125. */
  126. };
  127. static struct usb_endpoint_descriptor uvc_hs_streaming_ep = {
  128. .bLength = USB_DT_ENDPOINT_SIZE,
  129. .bDescriptorType = USB_DT_ENDPOINT,
  130. .bEndpointAddress = USB_DIR_IN,
  131. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  132. | USB_ENDPOINT_XFER_ISOC,
  133. /*
  134. * The wMaxPacketSize and bInterval values will be initialized from
  135. * module parameters.
  136. */
  137. };
  138. static struct usb_endpoint_descriptor uvc_ss_streaming_ep = {
  139. .bLength = USB_DT_ENDPOINT_SIZE,
  140. .bDescriptorType = USB_DT_ENDPOINT,
  141. .bEndpointAddress = USB_DIR_IN,
  142. .bmAttributes = USB_ENDPOINT_SYNC_ASYNC
  143. | USB_ENDPOINT_XFER_ISOC,
  144. /*
  145. * The wMaxPacketSize and bInterval values will be initialized from
  146. * module parameters.
  147. */
  148. };
  149. static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp = {
  150. .bLength = sizeof(uvc_ss_streaming_comp),
  151. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  152. /*
  153. * The bMaxBurst, bmAttributes and wBytesPerInterval values will be
  154. * initialized from module parameters.
  155. */
  156. };
  157. static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
  158. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  159. (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
  160. NULL,
  161. };
  162. static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
  163. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  164. (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
  165. NULL,
  166. };
  167. static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
  168. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  169. (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
  170. (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
  171. NULL,
  172. };
  173. /* --------------------------------------------------------------------------
  174. * Control requests
  175. */
  176. static void
  177. uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  178. {
  179. struct uvc_device *uvc = req->context;
  180. struct v4l2_event v4l2_event;
  181. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  182. if (uvc->event_setup_out) {
  183. uvc->event_setup_out = 0;
  184. memset(&v4l2_event, 0, sizeof(v4l2_event));
  185. v4l2_event.type = UVC_EVENT_DATA;
  186. uvc_event->data.length = min_t(unsigned int, req->actual,
  187. sizeof(uvc_event->data.data));
  188. memcpy(&uvc_event->data.data, req->buf, uvc_event->data.length);
  189. v4l2_event_queue(&uvc->vdev, &v4l2_event);
  190. }
  191. }
  192. static int
  193. uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  194. {
  195. struct uvc_device *uvc = to_uvc(f);
  196. struct v4l2_event v4l2_event;
  197. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  198. unsigned int interface = le16_to_cpu(ctrl->wIndex) & 0xff;
  199. struct usb_ctrlrequest *mctrl;
  200. if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
  201. uvcg_info(f, "invalid request type\n");
  202. return -EINVAL;
  203. }
  204. /* Stall too big requests. */
  205. if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
  206. return -EINVAL;
  207. /*
  208. * Tell the complete callback to generate an event for the next request
  209. * that will be enqueued by UVCIOC_SEND_RESPONSE.
  210. */
  211. uvc->event_setup_out = !(ctrl->bRequestType & USB_DIR_IN);
  212. uvc->event_length = le16_to_cpu(ctrl->wLength);
  213. memset(&v4l2_event, 0, sizeof(v4l2_event));
  214. v4l2_event.type = UVC_EVENT_SETUP;
  215. memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
  216. /* check for the interface number, fixup the interface number in
  217. * the ctrl request so the userspace doesn't have to bother with
  218. * offset and configfs parsing
  219. */
  220. mctrl = &uvc_event->req;
  221. mctrl->wIndex &= ~cpu_to_le16(0xff);
  222. if (interface == uvc->streaming_intf)
  223. mctrl->wIndex = cpu_to_le16(UVC_STRING_STREAMING_IDX);
  224. v4l2_event_queue(&uvc->vdev, &v4l2_event);
  225. return 0;
  226. }
  227. void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep)
  228. {
  229. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  230. if (disable_ep && uvc->video.ep)
  231. usb_ep_disable(uvc->video.ep);
  232. usb_composite_setup_continue(cdev);
  233. }
  234. static int
  235. uvc_function_get_alt(struct usb_function *f, unsigned interface)
  236. {
  237. struct uvc_device *uvc = to_uvc(f);
  238. uvcg_info(f, "%s(%u)\n", __func__, interface);
  239. if (interface == uvc->control_intf)
  240. return 0;
  241. else if (interface != uvc->streaming_intf)
  242. return -EINVAL;
  243. else
  244. return uvc->video.ep->enabled ? 1 : 0;
  245. }
  246. static int
  247. uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
  248. {
  249. struct uvc_device *uvc = to_uvc(f);
  250. struct usb_composite_dev *cdev = f->config->cdev;
  251. struct v4l2_event v4l2_event;
  252. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  253. int ret;
  254. uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
  255. if (interface == uvc->control_intf) {
  256. if (alt)
  257. return -EINVAL;
  258. uvcg_info(f, "reset UVC Control\n");
  259. usb_ep_disable(uvc->control_ep);
  260. if (!uvc->control_ep->desc)
  261. if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
  262. return -EINVAL;
  263. usb_ep_enable(uvc->control_ep);
  264. if (uvc->state == UVC_STATE_DISCONNECTED) {
  265. memset(&v4l2_event, 0, sizeof(v4l2_event));
  266. v4l2_event.type = UVC_EVENT_CONNECT;
  267. uvc_event->speed = cdev->gadget->speed;
  268. v4l2_event_queue(&uvc->vdev, &v4l2_event);
  269. uvc->state = UVC_STATE_CONNECTED;
  270. }
  271. return 0;
  272. }
  273. if (interface != uvc->streaming_intf)
  274. return -EINVAL;
  275. /* TODO
  276. if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
  277. return alt ? -EINVAL : 0;
  278. */
  279. switch (alt) {
  280. case 0:
  281. if (uvc->state != UVC_STATE_STREAMING)
  282. return 0;
  283. memset(&v4l2_event, 0, sizeof(v4l2_event));
  284. v4l2_event.type = UVC_EVENT_STREAMOFF;
  285. v4l2_event_queue(&uvc->vdev, &v4l2_event);
  286. return USB_GADGET_DELAYED_STATUS;
  287. case 1:
  288. if (uvc->state != UVC_STATE_CONNECTED)
  289. return 0;
  290. if (!uvc->video.ep)
  291. return -EINVAL;
  292. uvcg_info(f, "reset UVC\n");
  293. usb_ep_disable(uvc->video.ep);
  294. ret = config_ep_by_speed(f->config->cdev->gadget,
  295. &(uvc->func), uvc->video.ep);
  296. if (ret)
  297. return ret;
  298. usb_ep_enable(uvc->video.ep);
  299. memset(&v4l2_event, 0, sizeof(v4l2_event));
  300. v4l2_event.type = UVC_EVENT_STREAMON;
  301. v4l2_event_queue(&uvc->vdev, &v4l2_event);
  302. return USB_GADGET_DELAYED_STATUS;
  303. default:
  304. return -EINVAL;
  305. }
  306. }
  307. static void
  308. uvc_function_disable(struct usb_function *f)
  309. {
  310. struct uvc_device *uvc = to_uvc(f);
  311. struct v4l2_event v4l2_event;
  312. uvcg_info(f, "%s()\n", __func__);
  313. memset(&v4l2_event, 0, sizeof(v4l2_event));
  314. v4l2_event.type = UVC_EVENT_DISCONNECT;
  315. v4l2_event_queue(&uvc->vdev, &v4l2_event);
  316. uvc->state = UVC_STATE_DISCONNECTED;
  317. usb_ep_disable(uvc->video.ep);
  318. usb_ep_disable(uvc->control_ep);
  319. }
  320. /* --------------------------------------------------------------------------
  321. * Connection / disconnection
  322. */
  323. void
  324. uvc_function_connect(struct uvc_device *uvc)
  325. {
  326. int ret;
  327. if ((ret = usb_function_activate(&uvc->func)) < 0)
  328. uvcg_info(&uvc->func, "UVC connect failed with %d\n", ret);
  329. }
  330. void
  331. uvc_function_disconnect(struct uvc_device *uvc)
  332. {
  333. int ret;
  334. if ((ret = usb_function_deactivate(&uvc->func)) < 0)
  335. uvcg_info(&uvc->func, "UVC disconnect failed with %d\n", ret);
  336. }
  337. /* --------------------------------------------------------------------------
  338. * USB probe and disconnect
  339. */
  340. static ssize_t function_name_show(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. struct uvc_device *uvc = dev_get_drvdata(dev);
  344. return sprintf(buf, "%s\n", uvc->func.fi->group.cg_item.ci_name);
  345. }
  346. static DEVICE_ATTR_RO(function_name);
  347. static int
  348. uvc_register_video(struct uvc_device *uvc)
  349. {
  350. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  351. int ret;
  352. /* TODO reference counting. */
  353. memset(&uvc->vdev, 0, sizeof(uvc->vdev));
  354. uvc->vdev.v4l2_dev = &uvc->v4l2_dev;
  355. uvc->vdev.v4l2_dev->dev = &cdev->gadget->dev;
  356. uvc->vdev.fops = &uvc_v4l2_fops;
  357. uvc->vdev.ioctl_ops = &uvc_v4l2_ioctl_ops;
  358. uvc->vdev.release = video_device_release_empty;
  359. uvc->vdev.vfl_dir = VFL_DIR_TX;
  360. uvc->vdev.lock = &uvc->video.mutex;
  361. uvc->vdev.device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  362. strscpy(uvc->vdev.name, cdev->gadget->name, sizeof(uvc->vdev.name));
  363. video_set_drvdata(&uvc->vdev, uvc);
  364. ret = video_register_device(&uvc->vdev, VFL_TYPE_VIDEO, -1);
  365. if (ret < 0)
  366. return ret;
  367. ret = device_create_file(&uvc->vdev.dev, &dev_attr_function_name);
  368. if (ret < 0) {
  369. video_unregister_device(&uvc->vdev);
  370. return ret;
  371. }
  372. return 0;
  373. }
  374. #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
  375. do { \
  376. memcpy(mem, desc, (desc)->bLength); \
  377. *(dst)++ = mem; \
  378. mem += (desc)->bLength; \
  379. } while (0);
  380. #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
  381. do { \
  382. const struct usb_descriptor_header * const *__src; \
  383. for (__src = src; *__src; ++__src) { \
  384. memcpy(mem, *__src, (*__src)->bLength); \
  385. *dst++ = mem; \
  386. mem += (*__src)->bLength; \
  387. } \
  388. } while (0)
  389. static struct usb_descriptor_header **
  390. uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
  391. {
  392. struct uvc_input_header_descriptor *uvc_streaming_header;
  393. struct uvc_header_descriptor *uvc_control_header;
  394. const struct uvc_descriptor_header * const *uvc_control_desc;
  395. const struct uvc_descriptor_header * const *uvc_streaming_cls;
  396. const struct usb_descriptor_header * const *uvc_streaming_std;
  397. const struct usb_descriptor_header * const *src;
  398. struct usb_descriptor_header **dst;
  399. struct usb_descriptor_header **hdr;
  400. unsigned int control_size;
  401. unsigned int streaming_size;
  402. unsigned int n_desc;
  403. unsigned int bytes;
  404. void *mem;
  405. switch (speed) {
  406. case USB_SPEED_SUPER_PLUS:
  407. case USB_SPEED_SUPER:
  408. uvc_control_desc = uvc->desc.ss_control;
  409. uvc_streaming_cls = uvc->desc.ss_streaming;
  410. uvc_streaming_std = uvc_ss_streaming;
  411. break;
  412. case USB_SPEED_HIGH:
  413. uvc_control_desc = uvc->desc.fs_control;
  414. uvc_streaming_cls = uvc->desc.hs_streaming;
  415. uvc_streaming_std = uvc_hs_streaming;
  416. break;
  417. case USB_SPEED_FULL:
  418. default:
  419. uvc_control_desc = uvc->desc.fs_control;
  420. uvc_streaming_cls = uvc->desc.fs_streaming;
  421. uvc_streaming_std = uvc_fs_streaming;
  422. break;
  423. }
  424. if (!uvc_control_desc || !uvc_streaming_cls)
  425. return ERR_PTR(-ENODEV);
  426. /*
  427. * Descriptors layout
  428. *
  429. * uvc_iad
  430. * uvc_control_intf
  431. * Class-specific UVC control descriptors
  432. * uvc_control_ep
  433. * uvc_control_cs_ep
  434. * uvc_ss_control_comp (for SS only)
  435. * uvc_streaming_intf_alt0
  436. * Class-specific UVC streaming descriptors
  437. * uvc_{fs|hs}_streaming
  438. */
  439. /* Count descriptors and compute their size. */
  440. control_size = 0;
  441. streaming_size = 0;
  442. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  443. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  444. + uvc_streaming_intf_alt0.bLength;
  445. if (speed == USB_SPEED_SUPER ||
  446. speed == USB_SPEED_SUPER_PLUS) {
  447. bytes += uvc_ss_control_comp.bLength;
  448. n_desc = 6;
  449. } else {
  450. n_desc = 5;
  451. }
  452. for (src = (const struct usb_descriptor_header **)uvc_control_desc;
  453. *src; ++src) {
  454. control_size += (*src)->bLength;
  455. bytes += (*src)->bLength;
  456. n_desc++;
  457. }
  458. for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
  459. *src; ++src) {
  460. streaming_size += (*src)->bLength;
  461. bytes += (*src)->bLength;
  462. n_desc++;
  463. }
  464. for (src = uvc_streaming_std; *src; ++src) {
  465. bytes += (*src)->bLength;
  466. n_desc++;
  467. }
  468. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  469. if (mem == NULL)
  470. return NULL;
  471. hdr = mem;
  472. dst = mem;
  473. mem += (n_desc + 1) * sizeof(*src);
  474. /* Copy the descriptors. */
  475. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  476. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  477. uvc_control_header = mem;
  478. UVC_COPY_DESCRIPTORS(mem, dst,
  479. (const struct usb_descriptor_header **)uvc_control_desc);
  480. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  481. uvc_control_header->bInCollection = 1;
  482. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  483. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  484. if (speed == USB_SPEED_SUPER
  485. || speed == USB_SPEED_SUPER_PLUS)
  486. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
  487. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  488. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  489. uvc_streaming_header = mem;
  490. UVC_COPY_DESCRIPTORS(mem, dst,
  491. (const struct usb_descriptor_header**)uvc_streaming_cls);
  492. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  493. uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
  494. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  495. *dst = NULL;
  496. return hdr;
  497. }
  498. static int
  499. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  500. {
  501. struct usb_composite_dev *cdev = c->cdev;
  502. struct uvc_device *uvc = to_uvc(f);
  503. struct usb_string *us;
  504. unsigned int max_packet_mult;
  505. unsigned int max_packet_size;
  506. struct usb_ep *ep;
  507. struct f_uvc_opts *opts;
  508. int ret = -EINVAL;
  509. uvcg_info(f, "%s()\n", __func__);
  510. opts = fi_to_f_uvc_opts(f->fi);
  511. /* Sanity check the streaming endpoint module parameters. */
  512. opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
  513. opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
  514. opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
  515. /* For SS, wMaxPacketSize has to be 1024 if bMaxBurst is not 0 */
  516. if (opts->streaming_maxburst &&
  517. (opts->streaming_maxpacket % 1024) != 0) {
  518. opts->streaming_maxpacket = roundup(opts->streaming_maxpacket, 1024);
  519. uvcg_info(f, "overriding streaming_maxpacket to %d\n",
  520. opts->streaming_maxpacket);
  521. }
  522. /*
  523. * Fill in the FS/HS/SS Video Streaming specific descriptors from the
  524. * module parameters.
  525. *
  526. * NOTE: We assume that the user knows what they are doing and won't
  527. * give parameters that their UDC doesn't support.
  528. */
  529. if (opts->streaming_maxpacket <= 1024) {
  530. max_packet_mult = 1;
  531. max_packet_size = opts->streaming_maxpacket;
  532. } else if (opts->streaming_maxpacket <= 2048) {
  533. max_packet_mult = 2;
  534. max_packet_size = opts->streaming_maxpacket / 2;
  535. } else {
  536. max_packet_mult = 3;
  537. max_packet_size = opts->streaming_maxpacket / 3;
  538. }
  539. uvc_fs_streaming_ep.wMaxPacketSize =
  540. cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
  541. uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
  542. uvc_hs_streaming_ep.wMaxPacketSize =
  543. cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
  544. /* A high-bandwidth endpoint must specify a bInterval value of 1 */
  545. if (max_packet_mult > 1)
  546. uvc_hs_streaming_ep.bInterval = 1;
  547. else
  548. uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
  549. uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
  550. uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
  551. uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
  552. uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
  553. uvc_ss_streaming_comp.wBytesPerInterval =
  554. cpu_to_le16(max_packet_size * max_packet_mult *
  555. (opts->streaming_maxburst + 1));
  556. /* Allocate endpoints. */
  557. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  558. if (!ep) {
  559. uvcg_info(f, "Unable to allocate control EP\n");
  560. goto error;
  561. }
  562. uvc->control_ep = ep;
  563. ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
  564. if (!ep) {
  565. uvcg_info(f, "Unable to allocate streaming EP\n");
  566. goto error;
  567. }
  568. uvc->video.ep = ep;
  569. uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  570. uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  571. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].s = opts->function_name;
  572. us = usb_gstrings_attach(cdev, uvc_function_strings,
  573. ARRAY_SIZE(uvc_en_us_strings));
  574. if (IS_ERR(us)) {
  575. ret = PTR_ERR(us);
  576. goto error;
  577. }
  578. uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
  579. uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
  580. ret = us[UVC_STRING_STREAMING_IDX].id;
  581. uvc_streaming_intf_alt0.iInterface = ret;
  582. uvc_streaming_intf_alt1.iInterface = ret;
  583. /* Allocate interface IDs. */
  584. if ((ret = usb_interface_id(c, f)) < 0)
  585. goto error;
  586. uvc_iad.bFirstInterface = ret;
  587. uvc_control_intf.bInterfaceNumber = ret;
  588. uvc->control_intf = ret;
  589. opts->control_interface = ret;
  590. if ((ret = usb_interface_id(c, f)) < 0)
  591. goto error;
  592. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  593. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  594. uvc->streaming_intf = ret;
  595. opts->streaming_interface = ret;
  596. /* Copy descriptors */
  597. f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  598. if (IS_ERR(f->fs_descriptors)) {
  599. ret = PTR_ERR(f->fs_descriptors);
  600. f->fs_descriptors = NULL;
  601. goto error;
  602. }
  603. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  604. if (IS_ERR(f->hs_descriptors)) {
  605. ret = PTR_ERR(f->hs_descriptors);
  606. f->hs_descriptors = NULL;
  607. goto error;
  608. }
  609. f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
  610. if (IS_ERR(f->ss_descriptors)) {
  611. ret = PTR_ERR(f->ss_descriptors);
  612. f->ss_descriptors = NULL;
  613. goto error;
  614. }
  615. f->ssp_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER_PLUS);
  616. if (IS_ERR(f->ssp_descriptors)) {
  617. ret = PTR_ERR(f->ssp_descriptors);
  618. f->ssp_descriptors = NULL;
  619. goto error;
  620. }
  621. /* Preallocate control endpoint request. */
  622. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  623. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  624. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  625. ret = -ENOMEM;
  626. goto error;
  627. }
  628. uvc->control_req->buf = uvc->control_buf;
  629. uvc->control_req->complete = uvc_function_ep0_complete;
  630. uvc->control_req->context = uvc;
  631. if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
  632. uvcg_err(f, "failed to register V4L2 device\n");
  633. goto error;
  634. }
  635. /* Initialise video. */
  636. ret = uvcg_video_init(&uvc->video, uvc);
  637. if (ret < 0)
  638. goto v4l2_error;
  639. /* Register a V4L2 device. */
  640. ret = uvc_register_video(uvc);
  641. if (ret < 0) {
  642. uvcg_err(f, "failed to register video device\n");
  643. goto v4l2_error;
  644. }
  645. return 0;
  646. v4l2_error:
  647. v4l2_device_unregister(&uvc->v4l2_dev);
  648. error:
  649. if (uvc->control_req)
  650. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  651. kfree(uvc->control_buf);
  652. usb_free_all_descriptors(f);
  653. return ret;
  654. }
  655. /* --------------------------------------------------------------------------
  656. * USB gadget function
  657. */
  658. static void uvc_free_inst(struct usb_function_instance *f)
  659. {
  660. struct f_uvc_opts *opts = fi_to_f_uvc_opts(f);
  661. mutex_destroy(&opts->lock);
  662. kfree(opts);
  663. }
  664. static struct usb_function_instance *uvc_alloc_inst(void)
  665. {
  666. struct f_uvc_opts *opts;
  667. struct uvc_camera_terminal_descriptor *cd;
  668. struct uvc_processing_unit_descriptor *pd;
  669. struct uvc_output_terminal_descriptor *od;
  670. struct uvc_color_matching_descriptor *md;
  671. struct uvc_descriptor_header **ctl_cls;
  672. int ret;
  673. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  674. if (!opts)
  675. return ERR_PTR(-ENOMEM);
  676. opts->func_inst.free_func_inst = uvc_free_inst;
  677. mutex_init(&opts->lock);
  678. cd = &opts->uvc_camera_terminal;
  679. cd->bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3);
  680. cd->bDescriptorType = USB_DT_CS_INTERFACE;
  681. cd->bDescriptorSubType = UVC_VC_INPUT_TERMINAL;
  682. cd->bTerminalID = 1;
  683. cd->wTerminalType = cpu_to_le16(0x0201);
  684. cd->bAssocTerminal = 0;
  685. cd->iTerminal = 0;
  686. cd->wObjectiveFocalLengthMin = cpu_to_le16(0);
  687. cd->wObjectiveFocalLengthMax = cpu_to_le16(0);
  688. cd->wOcularFocalLength = cpu_to_le16(0);
  689. cd->bControlSize = 3;
  690. cd->bmControls[0] = 2;
  691. cd->bmControls[1] = 0;
  692. cd->bmControls[2] = 0;
  693. pd = &opts->uvc_processing;
  694. pd->bLength = UVC_DT_PROCESSING_UNIT_SIZE(2);
  695. pd->bDescriptorType = USB_DT_CS_INTERFACE;
  696. pd->bDescriptorSubType = UVC_VC_PROCESSING_UNIT;
  697. pd->bUnitID = 2;
  698. pd->bSourceID = 1;
  699. pd->wMaxMultiplier = cpu_to_le16(16*1024);
  700. pd->bControlSize = 2;
  701. pd->bmControls[0] = 1;
  702. pd->bmControls[1] = 0;
  703. pd->iProcessing = 0;
  704. pd->bmVideoStandards = 0;
  705. od = &opts->uvc_output_terminal;
  706. od->bLength = UVC_DT_OUTPUT_TERMINAL_SIZE;
  707. od->bDescriptorType = USB_DT_CS_INTERFACE;
  708. od->bDescriptorSubType = UVC_VC_OUTPUT_TERMINAL;
  709. od->bTerminalID = 3;
  710. od->wTerminalType = cpu_to_le16(0x0101);
  711. od->bAssocTerminal = 0;
  712. od->bSourceID = 2;
  713. od->iTerminal = 0;
  714. md = &opts->uvc_color_matching;
  715. md->bLength = UVC_DT_COLOR_MATCHING_SIZE;
  716. md->bDescriptorType = USB_DT_CS_INTERFACE;
  717. md->bDescriptorSubType = UVC_VS_COLORFORMAT;
  718. md->bColorPrimaries = 1;
  719. md->bTransferCharacteristics = 1;
  720. md->bMatrixCoefficients = 4;
  721. /* Prepare fs control class descriptors for configfs-based gadgets */
  722. ctl_cls = opts->uvc_fs_control_cls;
  723. ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
  724. ctl_cls[1] = (struct uvc_descriptor_header *)cd;
  725. ctl_cls[2] = (struct uvc_descriptor_header *)pd;
  726. ctl_cls[3] = (struct uvc_descriptor_header *)od;
  727. ctl_cls[4] = NULL; /* NULL-terminate */
  728. opts->fs_control =
  729. (const struct uvc_descriptor_header * const *)ctl_cls;
  730. /* Prepare hs control class descriptors for configfs-based gadgets */
  731. ctl_cls = opts->uvc_ss_control_cls;
  732. ctl_cls[0] = NULL; /* assigned elsewhere by configfs */
  733. ctl_cls[1] = (struct uvc_descriptor_header *)cd;
  734. ctl_cls[2] = (struct uvc_descriptor_header *)pd;
  735. ctl_cls[3] = (struct uvc_descriptor_header *)od;
  736. ctl_cls[4] = NULL; /* NULL-terminate */
  737. opts->ss_control =
  738. (const struct uvc_descriptor_header * const *)ctl_cls;
  739. opts->streaming_interval = 1;
  740. opts->streaming_maxpacket = 1024;
  741. snprintf(opts->function_name, sizeof(opts->function_name), "UVC Camera");
  742. ret = uvcg_attach_configfs(opts);
  743. if (ret < 0) {
  744. kfree(opts);
  745. return ERR_PTR(ret);
  746. }
  747. return &opts->func_inst;
  748. }
  749. static void uvc_free(struct usb_function *f)
  750. {
  751. struct uvc_device *uvc = to_uvc(f);
  752. struct f_uvc_opts *opts = container_of(f->fi, struct f_uvc_opts,
  753. func_inst);
  754. config_item_put(&uvc->header->item);
  755. --opts->refcnt;
  756. kfree(uvc);
  757. }
  758. static void uvc_function_unbind(struct usb_configuration *c,
  759. struct usb_function *f)
  760. {
  761. struct usb_composite_dev *cdev = c->cdev;
  762. struct uvc_device *uvc = to_uvc(f);
  763. struct uvc_video *video = &uvc->video;
  764. long wait_ret = 1;
  765. uvcg_info(f, "%s()\n", __func__);
  766. if (video->async_wq)
  767. destroy_workqueue(video->async_wq);
  768. /*
  769. * If we know we're connected via v4l2, then there should be a cleanup
  770. * of the device from userspace either via UVC_EVENT_DISCONNECT or
  771. * though the video device removal uevent. Allow some time for the
  772. * application to close out before things get deleted.
  773. */
  774. if (uvc->func_connected) {
  775. uvcg_dbg(f, "waiting for clean disconnect\n");
  776. wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
  777. uvc->func_connected == false, msecs_to_jiffies(500));
  778. uvcg_dbg(f, "done waiting with ret: %ld\n", wait_ret);
  779. }
  780. device_remove_file(&uvc->vdev.dev, &dev_attr_function_name);
  781. video_unregister_device(&uvc->vdev);
  782. v4l2_device_unregister(&uvc->v4l2_dev);
  783. if (uvc->func_connected) {
  784. /*
  785. * Wait for the release to occur to ensure there are no longer any
  786. * pending operations that may cause panics when resources are cleaned
  787. * up.
  788. */
  789. uvcg_warn(f, "%s no clean disconnect, wait for release\n", __func__);
  790. wait_ret = wait_event_interruptible_timeout(uvc->func_connected_queue,
  791. uvc->func_connected == false, msecs_to_jiffies(1000));
  792. uvcg_dbg(f, "done waiting for release with ret: %ld\n", wait_ret);
  793. }
  794. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  795. kfree(uvc->control_buf);
  796. usb_free_all_descriptors(f);
  797. }
  798. static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
  799. {
  800. struct uvc_device *uvc;
  801. struct f_uvc_opts *opts;
  802. struct uvc_descriptor_header **strm_cls;
  803. struct config_item *streaming, *header, *h;
  804. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  805. if (uvc == NULL)
  806. return ERR_PTR(-ENOMEM);
  807. mutex_init(&uvc->video.mutex);
  808. uvc->state = UVC_STATE_DISCONNECTED;
  809. init_waitqueue_head(&uvc->func_connected_queue);
  810. opts = fi_to_f_uvc_opts(fi);
  811. mutex_lock(&opts->lock);
  812. if (opts->uvc_fs_streaming_cls) {
  813. strm_cls = opts->uvc_fs_streaming_cls;
  814. opts->fs_streaming =
  815. (const struct uvc_descriptor_header * const *)strm_cls;
  816. }
  817. if (opts->uvc_hs_streaming_cls) {
  818. strm_cls = opts->uvc_hs_streaming_cls;
  819. opts->hs_streaming =
  820. (const struct uvc_descriptor_header * const *)strm_cls;
  821. }
  822. if (opts->uvc_ss_streaming_cls) {
  823. strm_cls = opts->uvc_ss_streaming_cls;
  824. opts->ss_streaming =
  825. (const struct uvc_descriptor_header * const *)strm_cls;
  826. }
  827. uvc->desc.fs_control = opts->fs_control;
  828. uvc->desc.ss_control = opts->ss_control;
  829. uvc->desc.fs_streaming = opts->fs_streaming;
  830. uvc->desc.hs_streaming = opts->hs_streaming;
  831. uvc->desc.ss_streaming = opts->ss_streaming;
  832. streaming = config_group_find_item(&opts->func_inst.group, "streaming");
  833. if (!streaming)
  834. goto err_config;
  835. header = config_group_find_item(to_config_group(streaming), "header");
  836. config_item_put(streaming);
  837. if (!header)
  838. goto err_config;
  839. h = config_group_find_item(to_config_group(header), "h");
  840. config_item_put(header);
  841. if (!h)
  842. goto err_config;
  843. uvc->header = to_uvcg_streaming_header(h);
  844. if (!uvc->header->linked) {
  845. mutex_unlock(&opts->lock);
  846. kfree(uvc);
  847. return ERR_PTR(-EBUSY);
  848. }
  849. ++opts->refcnt;
  850. mutex_unlock(&opts->lock);
  851. /* Register the function. */
  852. uvc->func.name = "uvc";
  853. uvc->func.bind = uvc_function_bind;
  854. uvc->func.unbind = uvc_function_unbind;
  855. uvc->func.get_alt = uvc_function_get_alt;
  856. uvc->func.set_alt = uvc_function_set_alt;
  857. uvc->func.disable = uvc_function_disable;
  858. uvc->func.setup = uvc_function_setup;
  859. uvc->func.free_func = uvc_free;
  860. uvc->func.bind_deactivated = true;
  861. return &uvc->func;
  862. err_config:
  863. mutex_unlock(&opts->lock);
  864. kfree(uvc);
  865. return ERR_PTR(-ENOENT);
  866. }
  867. DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
  868. MODULE_LICENSE("GPL");
  869. MODULE_AUTHOR("Laurent Pinchart");