f_uvc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * uvc_gadget.c -- USB Video Class Gadget driver
  3. *
  4. * Copyright (C) 2009-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/errno.h>
  16. #include <linux/fs.h>
  17. #include <linux/list.h>
  18. #include <linux/mutex.h>
  19. #include <linux/string.h>
  20. #include <linux/usb/ch9.h>
  21. #include <linux/usb/gadget.h>
  22. #include <linux/usb/video.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/wait.h>
  25. #include <media/v4l2-dev.h>
  26. #include <media/v4l2-event.h>
  27. #include "uvc.h"
  28. #include "uvc_v4l2.h"
  29. #include "uvc_video.h"
  30. #include "u_uvc.h"
  31. unsigned int uvc_gadget_trace_param;
  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->driver_data ? 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. if (uvc->control_ep->driver_data) {
  253. INFO(cdev, "reset UVC Control\n");
  254. usb_ep_disable(uvc->control_ep);
  255. uvc->control_ep->driver_data = NULL;
  256. }
  257. if (!uvc->control_ep->desc)
  258. if (config_ep_by_speed(cdev->gadget, f, uvc->control_ep))
  259. return -EINVAL;
  260. usb_ep_enable(uvc->control_ep);
  261. uvc->control_ep->driver_data = uvc;
  262. if (uvc->state == UVC_STATE_DISCONNECTED) {
  263. memset(&v4l2_event, 0, sizeof(v4l2_event));
  264. v4l2_event.type = UVC_EVENT_CONNECT;
  265. uvc_event->speed = cdev->gadget->speed;
  266. v4l2_event_queue(uvc->vdev, &v4l2_event);
  267. uvc->state = UVC_STATE_CONNECTED;
  268. }
  269. return 0;
  270. }
  271. if (interface != uvc->streaming_intf)
  272. return -EINVAL;
  273. /* TODO
  274. if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
  275. return alt ? -EINVAL : 0;
  276. */
  277. switch (alt) {
  278. case 0:
  279. if (uvc->state != UVC_STATE_STREAMING)
  280. return 0;
  281. if (uvc->video.ep) {
  282. usb_ep_disable(uvc->video.ep);
  283. uvc->video.ep->driver_data = NULL;
  284. }
  285. memset(&v4l2_event, 0, sizeof(v4l2_event));
  286. v4l2_event.type = UVC_EVENT_STREAMOFF;
  287. v4l2_event_queue(uvc->vdev, &v4l2_event);
  288. uvc->state = UVC_STATE_CONNECTED;
  289. return 0;
  290. case 1:
  291. if (uvc->state != UVC_STATE_CONNECTED)
  292. return 0;
  293. if (!uvc->video.ep)
  294. return -EINVAL;
  295. if (uvc->video.ep->driver_data) {
  296. INFO(cdev, "reset UVC\n");
  297. usb_ep_disable(uvc->video.ep);
  298. uvc->video.ep->driver_data = NULL;
  299. }
  300. ret = config_ep_by_speed(f->config->cdev->gadget,
  301. &(uvc->func), uvc->video.ep);
  302. if (ret)
  303. return ret;
  304. usb_ep_enable(uvc->video.ep);
  305. uvc->video.ep->driver_data = uvc;
  306. memset(&v4l2_event, 0, sizeof(v4l2_event));
  307. v4l2_event.type = UVC_EVENT_STREAMON;
  308. v4l2_event_queue(uvc->vdev, &v4l2_event);
  309. return USB_GADGET_DELAYED_STATUS;
  310. default:
  311. return -EINVAL;
  312. }
  313. }
  314. static void
  315. uvc_function_disable(struct usb_function *f)
  316. {
  317. struct uvc_device *uvc = to_uvc(f);
  318. struct v4l2_event v4l2_event;
  319. INFO(f->config->cdev, "uvc_function_disable\n");
  320. memset(&v4l2_event, 0, sizeof(v4l2_event));
  321. v4l2_event.type = UVC_EVENT_DISCONNECT;
  322. v4l2_event_queue(uvc->vdev, &v4l2_event);
  323. uvc->state = UVC_STATE_DISCONNECTED;
  324. if (uvc->video.ep->driver_data) {
  325. usb_ep_disable(uvc->video.ep);
  326. uvc->video.ep->driver_data = NULL;
  327. }
  328. if (uvc->control_ep->driver_data) {
  329. usb_ep_disable(uvc->control_ep);
  330. uvc->control_ep->driver_data = NULL;
  331. }
  332. }
  333. /* --------------------------------------------------------------------------
  334. * Connection / disconnection
  335. */
  336. void
  337. uvc_function_connect(struct uvc_device *uvc)
  338. {
  339. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  340. int ret;
  341. if ((ret = usb_function_activate(&uvc->func)) < 0)
  342. INFO(cdev, "UVC connect failed with %d\n", ret);
  343. }
  344. void
  345. uvc_function_disconnect(struct uvc_device *uvc)
  346. {
  347. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  348. int ret;
  349. if ((ret = usb_function_deactivate(&uvc->func)) < 0)
  350. INFO(cdev, "UVC disconnect failed with %d\n", ret);
  351. }
  352. /* --------------------------------------------------------------------------
  353. * USB probe and disconnect
  354. */
  355. static int
  356. uvc_register_video(struct uvc_device *uvc)
  357. {
  358. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  359. struct video_device *video;
  360. /* TODO reference counting. */
  361. video = video_device_alloc();
  362. if (video == NULL)
  363. return -ENOMEM;
  364. video->v4l2_dev = &uvc->v4l2_dev;
  365. video->fops = &uvc_v4l2_fops;
  366. video->ioctl_ops = &uvc_v4l2_ioctl_ops;
  367. video->release = video_device_release;
  368. video->vfl_dir = VFL_DIR_TX;
  369. strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
  370. uvc->vdev = video;
  371. video_set_drvdata(video, uvc);
  372. return video_register_device(video, VFL_TYPE_GRABBER, -1);
  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:
  407. uvc_control_desc = uvc->desc.ss_control;
  408. uvc_streaming_cls = uvc->desc.ss_streaming;
  409. uvc_streaming_std = uvc_ss_streaming;
  410. break;
  411. case USB_SPEED_HIGH:
  412. uvc_control_desc = uvc->desc.fs_control;
  413. uvc_streaming_cls = uvc->desc.hs_streaming;
  414. uvc_streaming_std = uvc_hs_streaming;
  415. break;
  416. case USB_SPEED_FULL:
  417. default:
  418. uvc_control_desc = uvc->desc.fs_control;
  419. uvc_streaming_cls = uvc->desc.fs_streaming;
  420. uvc_streaming_std = uvc_fs_streaming;
  421. break;
  422. }
  423. /* Descriptors layout
  424. *
  425. * uvc_iad
  426. * uvc_control_intf
  427. * Class-specific UVC control descriptors
  428. * uvc_control_ep
  429. * uvc_control_cs_ep
  430. * uvc_ss_control_comp (for SS only)
  431. * uvc_streaming_intf_alt0
  432. * Class-specific UVC streaming descriptors
  433. * uvc_{fs|hs}_streaming
  434. */
  435. /* Count descriptors and compute their size. */
  436. control_size = 0;
  437. streaming_size = 0;
  438. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  439. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  440. + uvc_streaming_intf_alt0.bLength;
  441. if (speed == USB_SPEED_SUPER) {
  442. bytes += uvc_ss_control_comp.bLength;
  443. n_desc = 6;
  444. } else {
  445. n_desc = 5;
  446. }
  447. for (src = (const struct usb_descriptor_header **)uvc_control_desc;
  448. *src; ++src) {
  449. control_size += (*src)->bLength;
  450. bytes += (*src)->bLength;
  451. n_desc++;
  452. }
  453. for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
  454. *src; ++src) {
  455. streaming_size += (*src)->bLength;
  456. bytes += (*src)->bLength;
  457. n_desc++;
  458. }
  459. for (src = uvc_streaming_std; *src; ++src) {
  460. bytes += (*src)->bLength;
  461. n_desc++;
  462. }
  463. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  464. if (mem == NULL)
  465. return NULL;
  466. hdr = mem;
  467. dst = mem;
  468. mem += (n_desc + 1) * sizeof(*src);
  469. /* Copy the descriptors. */
  470. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  471. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  472. uvc_control_header = mem;
  473. UVC_COPY_DESCRIPTORS(mem, dst,
  474. (const struct usb_descriptor_header **)uvc_control_desc);
  475. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  476. uvc_control_header->bInCollection = 1;
  477. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  478. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  479. if (speed == USB_SPEED_SUPER)
  480. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
  481. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  482. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  483. uvc_streaming_header = mem;
  484. UVC_COPY_DESCRIPTORS(mem, dst,
  485. (const struct usb_descriptor_header**)uvc_streaming_cls);
  486. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  487. uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
  488. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  489. *dst = NULL;
  490. return hdr;
  491. }
  492. static int
  493. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  494. {
  495. struct usb_composite_dev *cdev = c->cdev;
  496. struct uvc_device *uvc = to_uvc(f);
  497. struct usb_string *us;
  498. unsigned int max_packet_mult;
  499. unsigned int max_packet_size;
  500. struct usb_ep *ep;
  501. struct f_uvc_opts *opts;
  502. int ret = -EINVAL;
  503. INFO(cdev, "uvc_function_bind\n");
  504. opts = to_f_uvc_opts(f->fi);
  505. /* Sanity check the streaming endpoint module parameters.
  506. */
  507. opts->streaming_interval = clamp(opts->streaming_interval, 1U, 16U);
  508. opts->streaming_maxpacket = clamp(opts->streaming_maxpacket, 1U, 3072U);
  509. opts->streaming_maxburst = min(opts->streaming_maxburst, 15U);
  510. /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
  511. * module parameters.
  512. *
  513. * NOTE: We assume that the user knows what they are doing and won't
  514. * give parameters that their UDC doesn't support.
  515. */
  516. if (opts->streaming_maxpacket <= 1024) {
  517. max_packet_mult = 1;
  518. max_packet_size = opts->streaming_maxpacket;
  519. } else if (opts->streaming_maxpacket <= 2048) {
  520. max_packet_mult = 2;
  521. max_packet_size = opts->streaming_maxpacket / 2;
  522. } else {
  523. max_packet_mult = 3;
  524. max_packet_size = opts->streaming_maxpacket / 3;
  525. }
  526. uvc_fs_streaming_ep.wMaxPacketSize =
  527. cpu_to_le16(min(opts->streaming_maxpacket, 1023U));
  528. uvc_fs_streaming_ep.bInterval = opts->streaming_interval;
  529. uvc_hs_streaming_ep.wMaxPacketSize =
  530. cpu_to_le16(max_packet_size | ((max_packet_mult - 1) << 11));
  531. uvc_hs_streaming_ep.bInterval = opts->streaming_interval;
  532. uvc_ss_streaming_ep.wMaxPacketSize = cpu_to_le16(max_packet_size);
  533. uvc_ss_streaming_ep.bInterval = opts->streaming_interval;
  534. uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
  535. uvc_ss_streaming_comp.bMaxBurst = opts->streaming_maxburst;
  536. uvc_ss_streaming_comp.wBytesPerInterval =
  537. cpu_to_le16(max_packet_size * max_packet_mult *
  538. opts->streaming_maxburst);
  539. /* Allocate endpoints. */
  540. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  541. if (!ep) {
  542. INFO(cdev, "Unable to allocate control EP\n");
  543. goto error;
  544. }
  545. uvc->control_ep = ep;
  546. ep->driver_data = uvc;
  547. if (gadget_is_superspeed(c->cdev->gadget))
  548. ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
  549. &uvc_ss_streaming_comp);
  550. else if (gadget_is_dualspeed(cdev->gadget))
  551. ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
  552. else
  553. ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
  554. if (!ep) {
  555. INFO(cdev, "Unable to allocate streaming EP\n");
  556. goto error;
  557. }
  558. uvc->video.ep = ep;
  559. ep->driver_data = uvc;
  560. uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  561. uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  562. uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  563. us = usb_gstrings_attach(cdev, uvc_function_strings,
  564. ARRAY_SIZE(uvc_en_us_strings));
  565. if (IS_ERR(us)) {
  566. ret = PTR_ERR(us);
  567. goto error;
  568. }
  569. uvc_iad.iFunction = us[UVC_STRING_CONTROL_IDX].id;
  570. uvc_control_intf.iInterface = us[UVC_STRING_CONTROL_IDX].id;
  571. ret = us[UVC_STRING_STREAMING_IDX].id;
  572. uvc_streaming_intf_alt0.iInterface = ret;
  573. uvc_streaming_intf_alt1.iInterface = ret;
  574. /* Allocate interface IDs. */
  575. if ((ret = usb_interface_id(c, f)) < 0)
  576. goto error;
  577. uvc_iad.bFirstInterface = ret;
  578. uvc_control_intf.bInterfaceNumber = ret;
  579. uvc->control_intf = ret;
  580. if ((ret = usb_interface_id(c, f)) < 0)
  581. goto error;
  582. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  583. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  584. uvc->streaming_intf = ret;
  585. /* Copy descriptors */
  586. f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  587. if (gadget_is_dualspeed(cdev->gadget))
  588. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  589. if (gadget_is_superspeed(c->cdev->gadget))
  590. f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
  591. /* Preallocate control endpoint request. */
  592. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  593. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  594. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  595. ret = -ENOMEM;
  596. goto error;
  597. }
  598. uvc->control_req->buf = uvc->control_buf;
  599. uvc->control_req->complete = uvc_function_ep0_complete;
  600. uvc->control_req->context = uvc;
  601. /* Avoid letting this gadget enumerate until the userspace server is
  602. * active.
  603. */
  604. if ((ret = usb_function_deactivate(f)) < 0)
  605. goto error;
  606. if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
  607. printk(KERN_INFO "v4l2_device_register failed\n");
  608. goto error;
  609. }
  610. /* Initialise video. */
  611. ret = uvcg_video_init(&uvc->video);
  612. if (ret < 0)
  613. goto error;
  614. /* Register a V4L2 device. */
  615. ret = uvc_register_video(uvc);
  616. if (ret < 0) {
  617. printk(KERN_INFO "Unable to register video device\n");
  618. goto error;
  619. }
  620. return 0;
  621. error:
  622. v4l2_device_unregister(&uvc->v4l2_dev);
  623. if (uvc->vdev)
  624. video_device_release(uvc->vdev);
  625. if (uvc->control_ep)
  626. uvc->control_ep->driver_data = NULL;
  627. if (uvc->video.ep)
  628. uvc->video.ep->driver_data = NULL;
  629. if (uvc->control_req)
  630. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  631. kfree(uvc->control_buf);
  632. usb_free_all_descriptors(f);
  633. return ret;
  634. }
  635. /* --------------------------------------------------------------------------
  636. * USB gadget function
  637. */
  638. static void uvc_free_inst(struct usb_function_instance *f)
  639. {
  640. struct f_uvc_opts *opts = to_f_uvc_opts(f);
  641. kfree(opts);
  642. }
  643. static struct usb_function_instance *uvc_alloc_inst(void)
  644. {
  645. struct f_uvc_opts *opts;
  646. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  647. if (!opts)
  648. return ERR_PTR(-ENOMEM);
  649. opts->func_inst.free_func_inst = uvc_free_inst;
  650. return &opts->func_inst;
  651. }
  652. static void uvc_free(struct usb_function *f)
  653. {
  654. struct uvc_device *uvc = to_uvc(f);
  655. kfree(uvc);
  656. }
  657. static void uvc_unbind(struct usb_configuration *c, struct usb_function *f)
  658. {
  659. struct usb_composite_dev *cdev = c->cdev;
  660. struct uvc_device *uvc = to_uvc(f);
  661. INFO(cdev, "%s\n", __func__);
  662. video_unregister_device(uvc->vdev);
  663. v4l2_device_unregister(&uvc->v4l2_dev);
  664. uvc->control_ep->driver_data = NULL;
  665. uvc->video.ep->driver_data = NULL;
  666. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  667. kfree(uvc->control_buf);
  668. usb_free_all_descriptors(f);
  669. }
  670. static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
  671. {
  672. struct uvc_device *uvc;
  673. struct f_uvc_opts *opts;
  674. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  675. if (uvc == NULL)
  676. return ERR_PTR(-ENOMEM);
  677. uvc->state = UVC_STATE_DISCONNECTED;
  678. opts = to_f_uvc_opts(fi);
  679. uvc->desc.fs_control = opts->fs_control;
  680. uvc->desc.ss_control = opts->ss_control;
  681. uvc->desc.fs_streaming = opts->fs_streaming;
  682. uvc->desc.hs_streaming = opts->hs_streaming;
  683. uvc->desc.ss_streaming = opts->ss_streaming;
  684. /* Register the function. */
  685. uvc->func.name = "uvc";
  686. uvc->func.bind = uvc_function_bind;
  687. uvc->func.unbind = uvc_unbind;
  688. uvc->func.get_alt = uvc_function_get_alt;
  689. uvc->func.set_alt = uvc_function_set_alt;
  690. uvc->func.disable = uvc_function_disable;
  691. uvc->func.setup = uvc_function_setup;
  692. uvc->func.free_func = uvc_free;
  693. return &uvc->func;
  694. }
  695. DECLARE_USB_FUNCTION_INIT(uvc, uvc_alloc_inst, uvc_alloc);
  696. MODULE_LICENSE("GPL");
  697. MODULE_AUTHOR("Laurent Pinchart");