f_uvc.c 23 KB

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