f_uvc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/fs.h>
  16. #include <linux/list.h>
  17. #include <linux/mutex.h>
  18. #include <linux/string.h>
  19. #include <linux/usb/ch9.h>
  20. #include <linux/usb/gadget.h>
  21. #include <linux/usb/video.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/wait.h>
  24. #include <media/v4l2-dev.h>
  25. #include <media/v4l2-event.h>
  26. #include "uvc.h"
  27. unsigned int uvc_gadget_trace_param;
  28. /*-------------------------------------------------------------------------*/
  29. /* module parameters specific to the Video streaming endpoint */
  30. static unsigned int streaming_interval = 1;
  31. module_param(streaming_interval, uint, S_IRUGO|S_IWUSR);
  32. MODULE_PARM_DESC(streaming_interval, "1 - 16");
  33. static unsigned int streaming_maxpacket = 1024;
  34. module_param(streaming_maxpacket, uint, S_IRUGO|S_IWUSR);
  35. MODULE_PARM_DESC(streaming_maxpacket, "1 - 1023 (FS), 1 - 3072 (hs/ss)");
  36. static unsigned int streaming_maxburst;
  37. module_param(streaming_maxburst, uint, S_IRUGO|S_IWUSR);
  38. MODULE_PARM_DESC(streaming_maxburst, "0 - 15 (ss only)");
  39. /* --------------------------------------------------------------------------
  40. * Function descriptors
  41. */
  42. /* string IDs are assigned dynamically */
  43. #define UVC_STRING_ASSOCIATION_IDX 0
  44. #define UVC_STRING_CONTROL_IDX 1
  45. #define UVC_STRING_STREAMING_IDX 2
  46. static struct usb_string uvc_en_us_strings[] = {
  47. [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
  48. [UVC_STRING_CONTROL_IDX].s = "Video Control",
  49. [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
  50. { }
  51. };
  52. static struct usb_gadget_strings uvc_stringtab = {
  53. .language = 0x0409, /* en-us */
  54. .strings = uvc_en_us_strings,
  55. };
  56. static struct usb_gadget_strings *uvc_function_strings[] = {
  57. &uvc_stringtab,
  58. NULL,
  59. };
  60. #define UVC_INTF_VIDEO_CONTROL 0
  61. #define UVC_INTF_VIDEO_STREAMING 1
  62. #define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
  63. static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
  64. .bLength = sizeof(uvc_iad),
  65. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  66. .bFirstInterface = 0,
  67. .bInterfaceCount = 2,
  68. .bFunctionClass = USB_CLASS_VIDEO,
  69. .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
  70. .bFunctionProtocol = 0x00,
  71. .iFunction = 0,
  72. };
  73. static struct usb_interface_descriptor uvc_control_intf __initdata = {
  74. .bLength = USB_DT_INTERFACE_SIZE,
  75. .bDescriptorType = USB_DT_INTERFACE,
  76. .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
  77. .bAlternateSetting = 0,
  78. .bNumEndpoints = 1,
  79. .bInterfaceClass = USB_CLASS_VIDEO,
  80. .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
  81. .bInterfaceProtocol = 0x00,
  82. .iInterface = 0,
  83. };
  84. static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
  85. .bLength = USB_DT_ENDPOINT_SIZE,
  86. .bDescriptorType = USB_DT_ENDPOINT,
  87. .bEndpointAddress = USB_DIR_IN,
  88. .bmAttributes = USB_ENDPOINT_XFER_INT,
  89. .wMaxPacketSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  90. .bInterval = 8,
  91. };
  92. static struct usb_ss_ep_comp_descriptor uvc_ss_control_comp __initdata = {
  93. .bLength = sizeof(uvc_ss_control_comp),
  94. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  95. /* The following 3 values can be tweaked if necessary. */
  96. .bMaxBurst = 0,
  97. .bmAttributes = 0,
  98. .wBytesPerInterval = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  99. };
  100. static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
  101. .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
  102. .bDescriptorType = USB_DT_CS_ENDPOINT,
  103. .bDescriptorSubType = UVC_EP_INTERRUPT,
  104. .wMaxTransferSize = cpu_to_le16(UVC_STATUS_MAX_PACKET_SIZE),
  105. };
  106. static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
  107. .bLength = USB_DT_INTERFACE_SIZE,
  108. .bDescriptorType = USB_DT_INTERFACE,
  109. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  110. .bAlternateSetting = 0,
  111. .bNumEndpoints = 0,
  112. .bInterfaceClass = USB_CLASS_VIDEO,
  113. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  114. .bInterfaceProtocol = 0x00,
  115. .iInterface = 0,
  116. };
  117. static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
  118. .bLength = USB_DT_INTERFACE_SIZE,
  119. .bDescriptorType = USB_DT_INTERFACE,
  120. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  121. .bAlternateSetting = 1,
  122. .bNumEndpoints = 1,
  123. .bInterfaceClass = USB_CLASS_VIDEO,
  124. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  125. .bInterfaceProtocol = 0x00,
  126. .iInterface = 0,
  127. };
  128. static struct usb_endpoint_descriptor uvc_fs_streaming_ep __initdata = {
  129. .bLength = USB_DT_ENDPOINT_SIZE,
  130. .bDescriptorType = USB_DT_ENDPOINT,
  131. .bEndpointAddress = USB_DIR_IN,
  132. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  133. /* The wMaxPacketSize and bInterval values will be initialized from
  134. * module parameters.
  135. */
  136. .wMaxPacketSize = 0,
  137. .bInterval = 0,
  138. };
  139. static struct usb_endpoint_descriptor uvc_hs_streaming_ep __initdata = {
  140. .bLength = USB_DT_ENDPOINT_SIZE,
  141. .bDescriptorType = USB_DT_ENDPOINT,
  142. .bEndpointAddress = USB_DIR_IN,
  143. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  144. /* The wMaxPacketSize and bInterval values will be initialized from
  145. * module parameters.
  146. */
  147. .wMaxPacketSize = 0,
  148. .bInterval = 0,
  149. };
  150. static struct usb_endpoint_descriptor uvc_ss_streaming_ep __initdata = {
  151. .bLength = USB_DT_ENDPOINT_SIZE,
  152. .bDescriptorType = USB_DT_ENDPOINT,
  153. .bEndpointAddress = USB_DIR_IN,
  154. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  155. /* The wMaxPacketSize and bInterval values will be initialized from
  156. * module parameters.
  157. */
  158. .wMaxPacketSize = 0,
  159. .bInterval = 0,
  160. };
  161. static struct usb_ss_ep_comp_descriptor uvc_ss_streaming_comp __initdata = {
  162. .bLength = sizeof(uvc_ss_streaming_comp),
  163. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  164. /* The following 3 values can be tweaked if necessary. */
  165. .bMaxBurst = 0,
  166. .bmAttributes = 0,
  167. .wBytesPerInterval = cpu_to_le16(1024),
  168. };
  169. static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
  170. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  171. (struct usb_descriptor_header *) &uvc_fs_streaming_ep,
  172. NULL,
  173. };
  174. static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
  175. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  176. (struct usb_descriptor_header *) &uvc_hs_streaming_ep,
  177. NULL,
  178. };
  179. static const struct usb_descriptor_header * const uvc_ss_streaming[] = {
  180. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  181. (struct usb_descriptor_header *) &uvc_ss_streaming_ep,
  182. (struct usb_descriptor_header *) &uvc_ss_streaming_comp,
  183. NULL,
  184. };
  185. /* --------------------------------------------------------------------------
  186. * Control requests
  187. */
  188. static void
  189. uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  190. {
  191. struct uvc_device *uvc = req->context;
  192. struct v4l2_event v4l2_event;
  193. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  194. if (uvc->event_setup_out) {
  195. uvc->event_setup_out = 0;
  196. memset(&v4l2_event, 0, sizeof(v4l2_event));
  197. v4l2_event.type = UVC_EVENT_DATA;
  198. uvc_event->data.length = req->actual;
  199. memcpy(&uvc_event->data.data, req->buf, req->actual);
  200. v4l2_event_queue(uvc->vdev, &v4l2_event);
  201. }
  202. }
  203. static int
  204. uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  205. {
  206. struct uvc_device *uvc = to_uvc(f);
  207. struct v4l2_event v4l2_event;
  208. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  209. /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
  210. * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
  211. * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
  212. */
  213. if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
  214. INFO(f->config->cdev, "invalid request type\n");
  215. return -EINVAL;
  216. }
  217. /* Stall too big requests. */
  218. if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
  219. return -EINVAL;
  220. memset(&v4l2_event, 0, sizeof(v4l2_event));
  221. v4l2_event.type = UVC_EVENT_SETUP;
  222. memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
  223. v4l2_event_queue(uvc->vdev, &v4l2_event);
  224. return 0;
  225. }
  226. static int
  227. uvc_function_get_alt(struct usb_function *f, unsigned interface)
  228. {
  229. struct uvc_device *uvc = to_uvc(f);
  230. INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
  231. if (interface == uvc->control_intf)
  232. return 0;
  233. else if (interface != uvc->streaming_intf)
  234. return -EINVAL;
  235. else
  236. return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
  237. }
  238. static int
  239. uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
  240. {
  241. struct uvc_device *uvc = to_uvc(f);
  242. struct v4l2_event v4l2_event;
  243. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  244. int ret;
  245. INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
  246. if (interface == uvc->control_intf) {
  247. if (alt)
  248. return -EINVAL;
  249. if (uvc->state == UVC_STATE_DISCONNECTED) {
  250. memset(&v4l2_event, 0, sizeof(v4l2_event));
  251. v4l2_event.type = UVC_EVENT_CONNECT;
  252. uvc_event->speed = f->config->cdev->gadget->speed;
  253. v4l2_event_queue(uvc->vdev, &v4l2_event);
  254. uvc->state = UVC_STATE_CONNECTED;
  255. }
  256. return 0;
  257. }
  258. if (interface != uvc->streaming_intf)
  259. return -EINVAL;
  260. /* TODO
  261. if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
  262. return alt ? -EINVAL : 0;
  263. */
  264. switch (alt) {
  265. case 0:
  266. if (uvc->state != UVC_STATE_STREAMING)
  267. return 0;
  268. if (uvc->video.ep)
  269. usb_ep_disable(uvc->video.ep);
  270. memset(&v4l2_event, 0, sizeof(v4l2_event));
  271. v4l2_event.type = UVC_EVENT_STREAMOFF;
  272. v4l2_event_queue(uvc->vdev, &v4l2_event);
  273. uvc->state = UVC_STATE_CONNECTED;
  274. break;
  275. case 1:
  276. if (uvc->state != UVC_STATE_CONNECTED)
  277. return 0;
  278. if (uvc->video.ep) {
  279. ret = config_ep_by_speed(f->config->cdev->gadget,
  280. &(uvc->func), uvc->video.ep);
  281. if (ret)
  282. return ret;
  283. usb_ep_enable(uvc->video.ep);
  284. }
  285. memset(&v4l2_event, 0, sizeof(v4l2_event));
  286. v4l2_event.type = UVC_EVENT_STREAMON;
  287. v4l2_event_queue(uvc->vdev, &v4l2_event);
  288. uvc->state = UVC_STATE_STREAMING;
  289. break;
  290. default:
  291. return -EINVAL;
  292. }
  293. return 0;
  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->parent = &cdev->gadget->dev;
  338. video->fops = &uvc_v4l2_fops;
  339. video->release = video_device_release;
  340. strlcpy(video->name, cdev->gadget->name, sizeof(video->name));
  341. uvc->vdev = video;
  342. video_set_drvdata(video, uvc);
  343. return video_register_device(video, VFL_TYPE_GRABBER, -1);
  344. }
  345. #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
  346. do { \
  347. memcpy(mem, desc, (desc)->bLength); \
  348. *(dst)++ = mem; \
  349. mem += (desc)->bLength; \
  350. } while (0);
  351. #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
  352. do { \
  353. const struct usb_descriptor_header * const *__src; \
  354. for (__src = src; *__src; ++__src) { \
  355. memcpy(mem, *__src, (*__src)->bLength); \
  356. *dst++ = mem; \
  357. mem += (*__src)->bLength; \
  358. } \
  359. } while (0)
  360. static struct usb_descriptor_header ** __init
  361. uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
  362. {
  363. struct uvc_input_header_descriptor *uvc_streaming_header;
  364. struct uvc_header_descriptor *uvc_control_header;
  365. const struct uvc_descriptor_header * const *uvc_control_desc;
  366. const struct uvc_descriptor_header * const *uvc_streaming_cls;
  367. const struct usb_descriptor_header * const *uvc_streaming_std;
  368. const struct usb_descriptor_header * const *src;
  369. struct usb_descriptor_header **dst;
  370. struct usb_descriptor_header **hdr;
  371. unsigned int control_size;
  372. unsigned int streaming_size;
  373. unsigned int n_desc;
  374. unsigned int bytes;
  375. void *mem;
  376. switch (speed) {
  377. case USB_SPEED_SUPER:
  378. uvc_control_desc = uvc->desc.ss_control;
  379. uvc_streaming_cls = uvc->desc.ss_streaming;
  380. uvc_streaming_std = uvc_ss_streaming;
  381. break;
  382. case USB_SPEED_HIGH:
  383. uvc_control_desc = uvc->desc.fs_control;
  384. uvc_streaming_cls = uvc->desc.hs_streaming;
  385. uvc_streaming_std = uvc_hs_streaming;
  386. break;
  387. case USB_SPEED_FULL:
  388. default:
  389. uvc_control_desc = uvc->desc.fs_control;
  390. uvc_streaming_cls = uvc->desc.fs_streaming;
  391. uvc_streaming_std = uvc_fs_streaming;
  392. break;
  393. }
  394. /* Descriptors layout
  395. *
  396. * uvc_iad
  397. * uvc_control_intf
  398. * Class-specific UVC control descriptors
  399. * uvc_control_ep
  400. * uvc_control_cs_ep
  401. * uvc_ss_control_comp (for SS only)
  402. * uvc_streaming_intf_alt0
  403. * Class-specific UVC streaming descriptors
  404. * uvc_{fs|hs}_streaming
  405. */
  406. /* Count descriptors and compute their size. */
  407. control_size = 0;
  408. streaming_size = 0;
  409. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  410. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  411. + uvc_streaming_intf_alt0.bLength;
  412. if (speed == USB_SPEED_SUPER) {
  413. bytes += uvc_ss_control_comp.bLength;
  414. n_desc = 6;
  415. } else {
  416. n_desc = 5;
  417. }
  418. for (src = (const struct usb_descriptor_header **)uvc_control_desc;
  419. *src; ++src) {
  420. control_size += (*src)->bLength;
  421. bytes += (*src)->bLength;
  422. n_desc++;
  423. }
  424. for (src = (const struct usb_descriptor_header **)uvc_streaming_cls;
  425. *src; ++src) {
  426. streaming_size += (*src)->bLength;
  427. bytes += (*src)->bLength;
  428. n_desc++;
  429. }
  430. for (src = uvc_streaming_std; *src; ++src) {
  431. bytes += (*src)->bLength;
  432. n_desc++;
  433. }
  434. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  435. if (mem == NULL)
  436. return NULL;
  437. hdr = mem;
  438. dst = mem;
  439. mem += (n_desc + 1) * sizeof(*src);
  440. /* Copy the descriptors. */
  441. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  442. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  443. uvc_control_header = mem;
  444. UVC_COPY_DESCRIPTORS(mem, dst,
  445. (const struct usb_descriptor_header **)uvc_control_desc);
  446. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  447. uvc_control_header->bInCollection = 1;
  448. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  449. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  450. if (speed == USB_SPEED_SUPER)
  451. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_ss_control_comp);
  452. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  453. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  454. uvc_streaming_header = mem;
  455. UVC_COPY_DESCRIPTORS(mem, dst,
  456. (const struct usb_descriptor_header**)uvc_streaming_cls);
  457. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  458. uvc_streaming_header->bEndpointAddress = uvc->video.ep->address;
  459. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  460. *dst = NULL;
  461. return hdr;
  462. }
  463. static void
  464. uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
  465. {
  466. struct usb_composite_dev *cdev = c->cdev;
  467. struct uvc_device *uvc = to_uvc(f);
  468. INFO(cdev, "uvc_function_unbind\n");
  469. video_unregister_device(uvc->vdev);
  470. uvc->control_ep->driver_data = NULL;
  471. uvc->video.ep->driver_data = NULL;
  472. uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = 0;
  473. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  474. kfree(uvc->control_buf);
  475. usb_free_all_descriptors(f);
  476. kfree(uvc);
  477. }
  478. static int __init
  479. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  480. {
  481. struct usb_composite_dev *cdev = c->cdev;
  482. struct uvc_device *uvc = to_uvc(f);
  483. unsigned int max_packet_mult;
  484. unsigned int max_packet_size;
  485. struct usb_ep *ep;
  486. int ret = -EINVAL;
  487. INFO(cdev, "uvc_function_bind\n");
  488. /* Sanity check the streaming endpoint module parameters.
  489. */
  490. streaming_interval = clamp(streaming_interval, 1U, 16U);
  491. streaming_maxpacket = clamp(streaming_maxpacket, 1U, 3072U);
  492. streaming_maxburst = min(streaming_maxburst, 15U);
  493. /* Fill in the FS/HS/SS Video Streaming specific descriptors from the
  494. * module parameters.
  495. *
  496. * NOTE: We assume that the user knows what they are doing and won't
  497. * give parameters that their UDC doesn't support.
  498. */
  499. if (streaming_maxpacket <= 1024) {
  500. max_packet_mult = 1;
  501. max_packet_size = streaming_maxpacket;
  502. } else if (streaming_maxpacket <= 2048) {
  503. max_packet_mult = 2;
  504. max_packet_size = streaming_maxpacket / 2;
  505. } else {
  506. max_packet_mult = 3;
  507. max_packet_size = streaming_maxpacket / 3;
  508. }
  509. uvc_fs_streaming_ep.wMaxPacketSize = min(streaming_maxpacket, 1023U);
  510. uvc_fs_streaming_ep.bInterval = streaming_interval;
  511. uvc_hs_streaming_ep.wMaxPacketSize = max_packet_size;
  512. uvc_hs_streaming_ep.wMaxPacketSize |= ((max_packet_mult - 1) << 11);
  513. uvc_hs_streaming_ep.bInterval = streaming_interval;
  514. uvc_ss_streaming_ep.wMaxPacketSize = max_packet_size;
  515. uvc_ss_streaming_ep.bInterval = streaming_interval;
  516. uvc_ss_streaming_comp.bmAttributes = max_packet_mult - 1;
  517. uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
  518. uvc_ss_streaming_comp.wBytesPerInterval =
  519. max_packet_size * max_packet_mult * streaming_maxburst;
  520. /* Allocate endpoints. */
  521. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  522. if (!ep) {
  523. INFO(cdev, "Unable to allocate control EP\n");
  524. goto error;
  525. }
  526. uvc->control_ep = ep;
  527. ep->driver_data = uvc;
  528. if (gadget_is_superspeed(c->cdev->gadget))
  529. ep = usb_ep_autoconfig_ss(cdev->gadget, &uvc_ss_streaming_ep,
  530. &uvc_ss_streaming_comp);
  531. else if (gadget_is_dualspeed(cdev->gadget))
  532. ep = usb_ep_autoconfig(cdev->gadget, &uvc_hs_streaming_ep);
  533. else
  534. ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
  535. if (!ep) {
  536. INFO(cdev, "Unable to allocate streaming EP\n");
  537. goto error;
  538. }
  539. uvc->video.ep = ep;
  540. ep->driver_data = uvc;
  541. uvc_fs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  542. uvc_hs_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  543. uvc_ss_streaming_ep.bEndpointAddress = uvc->video.ep->address;
  544. /* Allocate interface IDs. */
  545. if ((ret = usb_interface_id(c, f)) < 0)
  546. goto error;
  547. uvc_iad.bFirstInterface = ret;
  548. uvc_control_intf.bInterfaceNumber = ret;
  549. uvc->control_intf = ret;
  550. if ((ret = usb_interface_id(c, f)) < 0)
  551. goto error;
  552. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  553. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  554. uvc->streaming_intf = ret;
  555. /* Copy descriptors */
  556. f->fs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  557. if (gadget_is_dualspeed(cdev->gadget))
  558. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  559. if (gadget_is_superspeed(c->cdev->gadget))
  560. f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
  561. /* Preallocate control endpoint request. */
  562. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  563. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  564. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  565. ret = -ENOMEM;
  566. goto error;
  567. }
  568. uvc->control_req->buf = uvc->control_buf;
  569. uvc->control_req->complete = uvc_function_ep0_complete;
  570. uvc->control_req->context = uvc;
  571. /* Avoid letting this gadget enumerate until the userspace server is
  572. * active.
  573. */
  574. if ((ret = usb_function_deactivate(f)) < 0)
  575. goto error;
  576. /* Initialise video. */
  577. ret = uvc_video_init(&uvc->video);
  578. if (ret < 0)
  579. goto error;
  580. /* Register a V4L2 device. */
  581. ret = uvc_register_video(uvc);
  582. if (ret < 0) {
  583. printk(KERN_INFO "Unable to register video device\n");
  584. goto error;
  585. }
  586. return 0;
  587. error:
  588. if (uvc->vdev)
  589. video_device_release(uvc->vdev);
  590. if (uvc->control_ep)
  591. uvc->control_ep->driver_data = NULL;
  592. if (uvc->video.ep)
  593. uvc->video.ep->driver_data = NULL;
  594. if (uvc->control_req) {
  595. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  596. kfree(uvc->control_buf);
  597. }
  598. usb_free_all_descriptors(f);
  599. return ret;
  600. }
  601. /* --------------------------------------------------------------------------
  602. * USB gadget function
  603. */
  604. /**
  605. * uvc_bind_config - add a UVC function to a configuration
  606. * @c: the configuration to support the UVC instance
  607. * Context: single threaded during gadget setup
  608. *
  609. * Returns zero on success, else negative errno.
  610. *
  611. * Caller must have called @uvc_setup(). Caller is also responsible for
  612. * calling @uvc_cleanup() before module unload.
  613. */
  614. int __init
  615. uvc_bind_config(struct usb_configuration *c,
  616. const struct uvc_descriptor_header * const *fs_control,
  617. const struct uvc_descriptor_header * const *ss_control,
  618. const struct uvc_descriptor_header * const *fs_streaming,
  619. const struct uvc_descriptor_header * const *hs_streaming,
  620. const struct uvc_descriptor_header * const *ss_streaming)
  621. {
  622. struct uvc_device *uvc;
  623. int ret = 0;
  624. /* TODO Check if the USB device controller supports the required
  625. * features.
  626. */
  627. if (!gadget_is_dualspeed(c->cdev->gadget))
  628. return -EINVAL;
  629. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  630. if (uvc == NULL)
  631. return -ENOMEM;
  632. uvc->state = UVC_STATE_DISCONNECTED;
  633. /* Validate the descriptors. */
  634. if (fs_control == NULL || fs_control[0] == NULL ||
  635. fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
  636. goto error;
  637. if (ss_control == NULL || ss_control[0] == NULL ||
  638. ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
  639. goto error;
  640. if (fs_streaming == NULL || fs_streaming[0] == NULL ||
  641. fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  642. goto error;
  643. if (hs_streaming == NULL || hs_streaming[0] == NULL ||
  644. hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  645. goto error;
  646. if (ss_streaming == NULL || ss_streaming[0] == NULL ||
  647. ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  648. goto error;
  649. uvc->desc.fs_control = fs_control;
  650. uvc->desc.ss_control = ss_control;
  651. uvc->desc.fs_streaming = fs_streaming;
  652. uvc->desc.hs_streaming = hs_streaming;
  653. uvc->desc.ss_streaming = ss_streaming;
  654. /* String descriptors are global, we only need to allocate string IDs
  655. * for the first UVC function. UVC functions beyond the first (if any)
  656. * will reuse the same IDs.
  657. */
  658. if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
  659. ret = usb_string_ids_tab(c->cdev, uvc_en_us_strings);
  660. if (ret)
  661. goto error;
  662. uvc_iad.iFunction =
  663. uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id;
  664. uvc_control_intf.iInterface =
  665. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id;
  666. ret = uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id;
  667. uvc_streaming_intf_alt0.iInterface = ret;
  668. uvc_streaming_intf_alt1.iInterface = ret;
  669. }
  670. /* Register the function. */
  671. uvc->func.name = "uvc";
  672. uvc->func.strings = uvc_function_strings;
  673. uvc->func.bind = uvc_function_bind;
  674. uvc->func.unbind = uvc_function_unbind;
  675. uvc->func.get_alt = uvc_function_get_alt;
  676. uvc->func.set_alt = uvc_function_set_alt;
  677. uvc->func.disable = uvc_function_disable;
  678. uvc->func.setup = uvc_function_setup;
  679. ret = usb_add_function(c, &uvc->func);
  680. if (ret)
  681. kfree(uvc);
  682. return ret;
  683. error:
  684. kfree(uvc);
  685. return ret;
  686. }
  687. module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
  688. MODULE_PARM_DESC(trace, "Trace level bitmask");