f_uvc.c 23 KB

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