f_uvc.c 23 KB

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