f_uvc.c 26 KB

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