f_uvc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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->minor = -1;
  341. video->fops = &uvc_v4l2_fops;
  342. video->release = video_device_release;
  343. strncpy(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. if (uvc->vdev) {
  477. if (uvc->vdev->minor == -1)
  478. video_device_release(uvc->vdev);
  479. else
  480. video_unregister_device(uvc->vdev);
  481. uvc->vdev = NULL;
  482. }
  483. if (uvc->control_ep)
  484. uvc->control_ep->driver_data = NULL;
  485. if (uvc->video.ep)
  486. uvc->video.ep->driver_data = NULL;
  487. if (uvc->control_req) {
  488. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  489. kfree(uvc->control_buf);
  490. }
  491. kfree(f->descriptors);
  492. kfree(f->hs_descriptors);
  493. kfree(f->ss_descriptors);
  494. kfree(uvc);
  495. }
  496. static int __init
  497. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  498. {
  499. struct usb_composite_dev *cdev = c->cdev;
  500. struct uvc_device *uvc = to_uvc(f);
  501. struct usb_ep *ep;
  502. int ret = -EINVAL;
  503. INFO(cdev, "uvc_function_bind\n");
  504. /* sanity check the streaming endpoint module parameters */
  505. if (streaming_interval < 1)
  506. streaming_interval = 1;
  507. if (streaming_interval > 16)
  508. streaming_interval = 16;
  509. if (streaming_mult > 2)
  510. streaming_mult = 2;
  511. if (streaming_maxburst > 15)
  512. streaming_maxburst = 15;
  513. /*
  514. * fill in the FS video streaming specific descriptors from the
  515. * module parameters
  516. */
  517. uvc_fs_streaming_ep.wMaxPacketSize = streaming_maxpacket > 1023 ?
  518. 1023 : streaming_maxpacket;
  519. uvc_fs_streaming_ep.bInterval = streaming_interval;
  520. /* Allocate endpoints. */
  521. ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_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. ep = usb_ep_autoconfig(cdev->gadget, &uvc_fs_streaming_ep);
  529. if (!ep) {
  530. INFO(cdev, "Unable to allocate streaming EP\n");
  531. goto error;
  532. }
  533. uvc->video.ep = ep;
  534. ep->driver_data = uvc;
  535. /* Allocate interface IDs. */
  536. if ((ret = usb_interface_id(c, f)) < 0)
  537. goto error;
  538. uvc_iad.bFirstInterface = ret;
  539. uvc_control_intf.bInterfaceNumber = ret;
  540. uvc->control_intf = ret;
  541. if ((ret = usb_interface_id(c, f)) < 0)
  542. goto error;
  543. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  544. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  545. uvc->streaming_intf = ret;
  546. /* sanity check the streaming endpoint module parameters */
  547. if (streaming_maxpacket > 1024)
  548. streaming_maxpacket = 1024;
  549. /* Copy descriptors for FS. */
  550. f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  551. /* support high speed hardware */
  552. if (gadget_is_dualspeed(cdev->gadget)) {
  553. /*
  554. * Fill in the HS descriptors from the module parameters for the
  555. * Video Streaming endpoint.
  556. * NOTE: We assume that the user knows what they are doing and
  557. * won't give parameters that their UDC doesn't support.
  558. */
  559. uvc_hs_streaming_ep.wMaxPacketSize = streaming_maxpacket;
  560. uvc_hs_streaming_ep.wMaxPacketSize |= streaming_mult << 11;
  561. uvc_hs_streaming_ep.bInterval = streaming_interval;
  562. uvc_hs_streaming_ep.bEndpointAddress =
  563. uvc_fs_streaming_ep.bEndpointAddress;
  564. /* Copy descriptors. */
  565. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  566. }
  567. /* support super speed hardware */
  568. if (gadget_is_superspeed(c->cdev->gadget)) {
  569. /*
  570. * Fill in the SS descriptors from the module parameters for the
  571. * Video Streaming endpoint.
  572. * NOTE: We assume that the user knows what they are doing and
  573. * won't give parameters that their UDC doesn't support.
  574. */
  575. uvc_ss_streaming_ep.wMaxPacketSize = streaming_maxpacket;
  576. uvc_ss_streaming_ep.bInterval = streaming_interval;
  577. uvc_ss_streaming_comp.bmAttributes = streaming_mult;
  578. uvc_ss_streaming_comp.bMaxBurst = streaming_maxburst;
  579. uvc_ss_streaming_comp.wBytesPerInterval =
  580. streaming_maxpacket * (streaming_mult + 1) *
  581. (streaming_maxburst + 1);
  582. uvc_ss_streaming_ep.bEndpointAddress =
  583. uvc_fs_streaming_ep.bEndpointAddress;
  584. /* Copy descriptors. */
  585. f->ss_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_SUPER);
  586. }
  587. /* Preallocate control endpoint request. */
  588. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  589. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  590. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  591. ret = -ENOMEM;
  592. goto error;
  593. }
  594. uvc->control_req->buf = uvc->control_buf;
  595. uvc->control_req->complete = uvc_function_ep0_complete;
  596. uvc->control_req->context = uvc;
  597. /* Avoid letting this gadget enumerate until the userspace server is
  598. * active.
  599. */
  600. if ((ret = usb_function_deactivate(f)) < 0)
  601. goto error;
  602. /* Initialise video. */
  603. ret = uvc_video_init(&uvc->video);
  604. if (ret < 0)
  605. goto error;
  606. /* Register a V4L2 device. */
  607. ret = uvc_register_video(uvc);
  608. if (ret < 0) {
  609. printk(KERN_INFO "Unable to register video device\n");
  610. goto error;
  611. }
  612. return 0;
  613. error:
  614. uvc_function_unbind(c, f);
  615. return ret;
  616. }
  617. /* --------------------------------------------------------------------------
  618. * USB gadget function
  619. */
  620. /**
  621. * uvc_bind_config - add a UVC function to a configuration
  622. * @c: the configuration to support the UVC instance
  623. * Context: single threaded during gadget setup
  624. *
  625. * Returns zero on success, else negative errno.
  626. *
  627. * Caller must have called @uvc_setup(). Caller is also responsible for
  628. * calling @uvc_cleanup() before module unload.
  629. */
  630. int __init
  631. uvc_bind_config(struct usb_configuration *c,
  632. const struct uvc_descriptor_header * const *fs_control,
  633. const struct uvc_descriptor_header * const *ss_control,
  634. const struct uvc_descriptor_header * const *fs_streaming,
  635. const struct uvc_descriptor_header * const *hs_streaming,
  636. const struct uvc_descriptor_header * const *ss_streaming)
  637. {
  638. struct uvc_device *uvc;
  639. int ret = 0;
  640. /* TODO Check if the USB device controller supports the required
  641. * features.
  642. */
  643. if (!gadget_is_dualspeed(c->cdev->gadget))
  644. return -EINVAL;
  645. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  646. if (uvc == NULL)
  647. return -ENOMEM;
  648. uvc->state = UVC_STATE_DISCONNECTED;
  649. /* Validate the descriptors. */
  650. if (fs_control == NULL || fs_control[0] == NULL ||
  651. fs_control[0]->bDescriptorSubType != UVC_VC_HEADER)
  652. goto error;
  653. if (ss_control == NULL || ss_control[0] == NULL ||
  654. ss_control[0]->bDescriptorSubType != UVC_VC_HEADER)
  655. goto error;
  656. if (fs_streaming == NULL || fs_streaming[0] == NULL ||
  657. fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  658. goto error;
  659. if (hs_streaming == NULL || hs_streaming[0] == NULL ||
  660. hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  661. goto error;
  662. if (ss_streaming == NULL || ss_streaming[0] == NULL ||
  663. ss_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  664. goto error;
  665. uvc->desc.fs_control = fs_control;
  666. uvc->desc.ss_control = ss_control;
  667. uvc->desc.fs_streaming = fs_streaming;
  668. uvc->desc.hs_streaming = hs_streaming;
  669. uvc->desc.ss_streaming = ss_streaming;
  670. /* maybe allocate device-global string IDs, and patch descriptors */
  671. if (uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id == 0) {
  672. /* Allocate string descriptor numbers. */
  673. ret = usb_string_id(c->cdev);
  674. if (ret < 0)
  675. goto error;
  676. uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
  677. uvc_iad.iFunction = ret;
  678. ret = usb_string_id(c->cdev);
  679. if (ret < 0)
  680. goto error;
  681. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
  682. uvc_control_intf.iInterface = ret;
  683. ret = usb_string_id(c->cdev);
  684. if (ret < 0)
  685. goto error;
  686. uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
  687. uvc_streaming_intf_alt0.iInterface = ret;
  688. uvc_streaming_intf_alt1.iInterface = ret;
  689. }
  690. /* Register the function. */
  691. uvc->func.name = "uvc";
  692. uvc->func.strings = uvc_function_strings;
  693. uvc->func.bind = uvc_function_bind;
  694. uvc->func.unbind = uvc_function_unbind;
  695. uvc->func.get_alt = uvc_function_get_alt;
  696. uvc->func.set_alt = uvc_function_set_alt;
  697. uvc->func.disable = uvc_function_disable;
  698. uvc->func.setup = uvc_function_setup;
  699. ret = usb_add_function(c, &uvc->func);
  700. if (ret)
  701. kfree(uvc);
  702. return ret;
  703. error:
  704. kfree(uvc);
  705. return ret;
  706. }
  707. module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
  708. MODULE_PARM_DESC(trace, "Trace level bitmask");