f_uvc.c 22 KB

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