f_uvc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. * Function descriptors
  29. */
  30. /* string IDs are assigned dynamically */
  31. #define UVC_STRING_ASSOCIATION_IDX 0
  32. #define UVC_STRING_CONTROL_IDX 1
  33. #define UVC_STRING_STREAMING_IDX 2
  34. static struct usb_string uvc_en_us_strings[] = {
  35. [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
  36. [UVC_STRING_CONTROL_IDX].s = "Video Control",
  37. [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
  38. { }
  39. };
  40. static struct usb_gadget_strings uvc_stringtab = {
  41. .language = 0x0409, /* en-us */
  42. .strings = uvc_en_us_strings,
  43. };
  44. static struct usb_gadget_strings *uvc_function_strings[] = {
  45. &uvc_stringtab,
  46. NULL,
  47. };
  48. #define UVC_INTF_VIDEO_CONTROL 0
  49. #define UVC_INTF_VIDEO_STREAMING 1
  50. static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
  51. .bLength = sizeof(uvc_iad),
  52. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  53. .bFirstInterface = 0,
  54. .bInterfaceCount = 2,
  55. .bFunctionClass = USB_CLASS_VIDEO,
  56. .bFunctionSubClass = UVC_SC_VIDEO_INTERFACE_COLLECTION,
  57. .bFunctionProtocol = 0x00,
  58. .iFunction = 0,
  59. };
  60. static struct usb_interface_descriptor uvc_control_intf __initdata = {
  61. .bLength = USB_DT_INTERFACE_SIZE,
  62. .bDescriptorType = USB_DT_INTERFACE,
  63. .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
  64. .bAlternateSetting = 0,
  65. .bNumEndpoints = 1,
  66. .bInterfaceClass = USB_CLASS_VIDEO,
  67. .bInterfaceSubClass = UVC_SC_VIDEOCONTROL,
  68. .bInterfaceProtocol = 0x00,
  69. .iInterface = 0,
  70. };
  71. static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
  72. .bLength = USB_DT_ENDPOINT_SIZE,
  73. .bDescriptorType = USB_DT_ENDPOINT,
  74. .bEndpointAddress = USB_DIR_IN,
  75. .bmAttributes = USB_ENDPOINT_XFER_INT,
  76. .wMaxPacketSize = cpu_to_le16(16),
  77. .bInterval = 8,
  78. };
  79. static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
  80. .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
  81. .bDescriptorType = USB_DT_CS_ENDPOINT,
  82. .bDescriptorSubType = UVC_EP_INTERRUPT,
  83. .wMaxTransferSize = cpu_to_le16(16),
  84. };
  85. static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
  86. .bLength = USB_DT_INTERFACE_SIZE,
  87. .bDescriptorType = USB_DT_INTERFACE,
  88. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  89. .bAlternateSetting = 0,
  90. .bNumEndpoints = 0,
  91. .bInterfaceClass = USB_CLASS_VIDEO,
  92. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  93. .bInterfaceProtocol = 0x00,
  94. .iInterface = 0,
  95. };
  96. static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
  97. .bLength = USB_DT_INTERFACE_SIZE,
  98. .bDescriptorType = USB_DT_INTERFACE,
  99. .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
  100. .bAlternateSetting = 1,
  101. .bNumEndpoints = 1,
  102. .bInterfaceClass = USB_CLASS_VIDEO,
  103. .bInterfaceSubClass = UVC_SC_VIDEOSTREAMING,
  104. .bInterfaceProtocol = 0x00,
  105. .iInterface = 0,
  106. };
  107. static struct usb_endpoint_descriptor uvc_streaming_ep = {
  108. .bLength = USB_DT_ENDPOINT_SIZE,
  109. .bDescriptorType = USB_DT_ENDPOINT,
  110. .bEndpointAddress = USB_DIR_IN,
  111. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  112. .wMaxPacketSize = cpu_to_le16(512),
  113. .bInterval = 1,
  114. };
  115. static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
  116. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  117. (struct usb_descriptor_header *) &uvc_streaming_ep,
  118. NULL,
  119. };
  120. static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
  121. (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
  122. (struct usb_descriptor_header *) &uvc_streaming_ep,
  123. NULL,
  124. };
  125. /* --------------------------------------------------------------------------
  126. * Control requests
  127. */
  128. static void
  129. uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
  130. {
  131. struct uvc_device *uvc = req->context;
  132. struct v4l2_event v4l2_event;
  133. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  134. if (uvc->event_setup_out) {
  135. uvc->event_setup_out = 0;
  136. memset(&v4l2_event, 0, sizeof(v4l2_event));
  137. v4l2_event.type = UVC_EVENT_DATA;
  138. uvc_event->data.length = req->actual;
  139. memcpy(&uvc_event->data.data, req->buf, req->actual);
  140. v4l2_event_queue(uvc->vdev, &v4l2_event);
  141. }
  142. }
  143. static int
  144. uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  145. {
  146. struct uvc_device *uvc = to_uvc(f);
  147. struct v4l2_event v4l2_event;
  148. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  149. /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
  150. * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
  151. * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
  152. */
  153. if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
  154. INFO(f->config->cdev, "invalid request type\n");
  155. return -EINVAL;
  156. }
  157. /* Stall too big requests. */
  158. if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
  159. return -EINVAL;
  160. memset(&v4l2_event, 0, sizeof(v4l2_event));
  161. v4l2_event.type = UVC_EVENT_SETUP;
  162. memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
  163. v4l2_event_queue(uvc->vdev, &v4l2_event);
  164. return 0;
  165. }
  166. static int
  167. uvc_function_get_alt(struct usb_function *f, unsigned interface)
  168. {
  169. struct uvc_device *uvc = to_uvc(f);
  170. INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
  171. if (interface == uvc->control_intf)
  172. return 0;
  173. else if (interface != uvc->streaming_intf)
  174. return -EINVAL;
  175. else
  176. return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
  177. }
  178. static int
  179. uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
  180. {
  181. struct uvc_device *uvc = to_uvc(f);
  182. struct v4l2_event v4l2_event;
  183. struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
  184. INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
  185. if (interface == uvc->control_intf) {
  186. if (alt)
  187. return -EINVAL;
  188. if (uvc->state == UVC_STATE_DISCONNECTED) {
  189. memset(&v4l2_event, 0, sizeof(v4l2_event));
  190. v4l2_event.type = UVC_EVENT_CONNECT;
  191. uvc_event->speed = f->config->cdev->gadget->speed;
  192. v4l2_event_queue(uvc->vdev, &v4l2_event);
  193. uvc->state = UVC_STATE_CONNECTED;
  194. }
  195. return 0;
  196. }
  197. if (interface != uvc->streaming_intf)
  198. return -EINVAL;
  199. /* TODO
  200. if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
  201. return alt ? -EINVAL : 0;
  202. */
  203. switch (alt) {
  204. case 0:
  205. if (uvc->state != UVC_STATE_STREAMING)
  206. return 0;
  207. if (uvc->video.ep)
  208. usb_ep_disable(uvc->video.ep);
  209. memset(&v4l2_event, 0, sizeof(v4l2_event));
  210. v4l2_event.type = UVC_EVENT_STREAMOFF;
  211. v4l2_event_queue(uvc->vdev, &v4l2_event);
  212. uvc->state = UVC_STATE_CONNECTED;
  213. break;
  214. case 1:
  215. if (uvc->state != UVC_STATE_CONNECTED)
  216. return 0;
  217. if (uvc->video.ep) {
  218. uvc->video.ep->desc = &uvc_streaming_ep;
  219. usb_ep_enable(uvc->video.ep);
  220. }
  221. memset(&v4l2_event, 0, sizeof(v4l2_event));
  222. v4l2_event.type = UVC_EVENT_STREAMON;
  223. v4l2_event_queue(uvc->vdev, &v4l2_event);
  224. uvc->state = UVC_STATE_STREAMING;
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. return 0;
  230. }
  231. static void
  232. uvc_function_disable(struct usb_function *f)
  233. {
  234. struct uvc_device *uvc = to_uvc(f);
  235. struct v4l2_event v4l2_event;
  236. INFO(f->config->cdev, "uvc_function_disable\n");
  237. memset(&v4l2_event, 0, sizeof(v4l2_event));
  238. v4l2_event.type = UVC_EVENT_DISCONNECT;
  239. v4l2_event_queue(uvc->vdev, &v4l2_event);
  240. uvc->state = UVC_STATE_DISCONNECTED;
  241. }
  242. /* --------------------------------------------------------------------------
  243. * Connection / disconnection
  244. */
  245. void
  246. uvc_function_connect(struct uvc_device *uvc)
  247. {
  248. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  249. int ret;
  250. if ((ret = usb_function_activate(&uvc->func)) < 0)
  251. INFO(cdev, "UVC connect failed with %d\n", ret);
  252. }
  253. void
  254. uvc_function_disconnect(struct uvc_device *uvc)
  255. {
  256. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  257. int ret;
  258. if ((ret = usb_function_deactivate(&uvc->func)) < 0)
  259. INFO(cdev, "UVC disconnect failed with %d\n", ret);
  260. }
  261. /* --------------------------------------------------------------------------
  262. * USB probe and disconnect
  263. */
  264. static int
  265. uvc_register_video(struct uvc_device *uvc)
  266. {
  267. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  268. struct video_device *video;
  269. /* TODO reference counting. */
  270. video = video_device_alloc();
  271. if (video == NULL)
  272. return -ENOMEM;
  273. video->parent = &cdev->gadget->dev;
  274. video->minor = -1;
  275. video->fops = &uvc_v4l2_fops;
  276. video->release = video_device_release;
  277. strncpy(video->name, cdev->gadget->name, sizeof(video->name));
  278. uvc->vdev = video;
  279. video_set_drvdata(video, uvc);
  280. return video_register_device(video, VFL_TYPE_GRABBER, -1);
  281. }
  282. #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
  283. do { \
  284. memcpy(mem, desc, (desc)->bLength); \
  285. *(dst)++ = mem; \
  286. mem += (desc)->bLength; \
  287. } while (0);
  288. #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
  289. do { \
  290. const struct usb_descriptor_header * const *__src; \
  291. for (__src = src; *__src; ++__src) { \
  292. memcpy(mem, *__src, (*__src)->bLength); \
  293. *dst++ = mem; \
  294. mem += (*__src)->bLength; \
  295. } \
  296. } while (0)
  297. static struct usb_descriptor_header ** __init
  298. uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
  299. {
  300. struct uvc_input_header_descriptor *uvc_streaming_header;
  301. struct uvc_header_descriptor *uvc_control_header;
  302. const struct uvc_descriptor_header * const *uvc_streaming_cls;
  303. const struct usb_descriptor_header * const *uvc_streaming_std;
  304. const struct usb_descriptor_header * const *src;
  305. struct usb_descriptor_header **dst;
  306. struct usb_descriptor_header **hdr;
  307. unsigned int control_size;
  308. unsigned int streaming_size;
  309. unsigned int n_desc;
  310. unsigned int bytes;
  311. void *mem;
  312. uvc_streaming_cls = (speed == USB_SPEED_FULL)
  313. ? uvc->desc.fs_streaming : uvc->desc.hs_streaming;
  314. uvc_streaming_std = (speed == USB_SPEED_FULL)
  315. ? uvc_fs_streaming : uvc_hs_streaming;
  316. /* Descriptors layout
  317. *
  318. * uvc_iad
  319. * uvc_control_intf
  320. * Class-specific UVC control descriptors
  321. * uvc_control_ep
  322. * uvc_control_cs_ep
  323. * uvc_streaming_intf_alt0
  324. * Class-specific UVC streaming descriptors
  325. * uvc_{fs|hs}_streaming
  326. */
  327. /* Count descriptors and compute their size. */
  328. control_size = 0;
  329. streaming_size = 0;
  330. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  331. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  332. + uvc_streaming_intf_alt0.bLength;
  333. n_desc = 5;
  334. for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) {
  335. control_size += (*src)->bLength;
  336. bytes += (*src)->bLength;
  337. n_desc++;
  338. }
  339. for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) {
  340. streaming_size += (*src)->bLength;
  341. bytes += (*src)->bLength;
  342. n_desc++;
  343. }
  344. for (src = uvc_streaming_std; *src; ++src) {
  345. bytes += (*src)->bLength;
  346. n_desc++;
  347. }
  348. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  349. if (mem == NULL)
  350. return NULL;
  351. hdr = mem;
  352. dst = mem;
  353. mem += (n_desc + 1) * sizeof(*src);
  354. /* Copy the descriptors. */
  355. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  356. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  357. uvc_control_header = mem;
  358. UVC_COPY_DESCRIPTORS(mem, dst,
  359. (const struct usb_descriptor_header**)uvc->desc.control);
  360. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  361. uvc_control_header->bInCollection = 1;
  362. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  363. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  364. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  365. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  366. uvc_streaming_header = mem;
  367. UVC_COPY_DESCRIPTORS(mem, dst,
  368. (const struct usb_descriptor_header**)uvc_streaming_cls);
  369. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  370. uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress;
  371. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  372. *dst = NULL;
  373. return hdr;
  374. }
  375. static void
  376. uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
  377. {
  378. struct usb_composite_dev *cdev = c->cdev;
  379. struct uvc_device *uvc = to_uvc(f);
  380. INFO(cdev, "uvc_function_unbind\n");
  381. if (uvc->vdev) {
  382. if (uvc->vdev->minor == -1)
  383. video_device_release(uvc->vdev);
  384. else
  385. video_unregister_device(uvc->vdev);
  386. uvc->vdev = NULL;
  387. }
  388. if (uvc->control_ep)
  389. uvc->control_ep->driver_data = NULL;
  390. if (uvc->video.ep)
  391. uvc->video.ep->driver_data = NULL;
  392. if (uvc->control_req) {
  393. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  394. kfree(uvc->control_buf);
  395. }
  396. kfree(f->descriptors);
  397. kfree(f->hs_descriptors);
  398. kfree(uvc);
  399. }
  400. static int __init
  401. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  402. {
  403. struct usb_composite_dev *cdev = c->cdev;
  404. struct uvc_device *uvc = to_uvc(f);
  405. struct usb_ep *ep;
  406. int ret = -EINVAL;
  407. INFO(cdev, "uvc_function_bind\n");
  408. /* Allocate endpoints. */
  409. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  410. if (!ep) {
  411. INFO(cdev, "Unable to allocate control EP\n");
  412. goto error;
  413. }
  414. uvc->control_ep = ep;
  415. ep->driver_data = uvc;
  416. ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep);
  417. if (!ep) {
  418. INFO(cdev, "Unable to allocate streaming EP\n");
  419. goto error;
  420. }
  421. uvc->video.ep = ep;
  422. ep->driver_data = uvc;
  423. /* Allocate interface IDs. */
  424. if ((ret = usb_interface_id(c, f)) < 0)
  425. goto error;
  426. uvc_iad.bFirstInterface = ret;
  427. uvc_control_intf.bInterfaceNumber = ret;
  428. uvc->control_intf = ret;
  429. if ((ret = usb_interface_id(c, f)) < 0)
  430. goto error;
  431. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  432. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  433. uvc->streaming_intf = ret;
  434. /* Copy descriptors. */
  435. f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  436. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  437. /* Preallocate control endpoint request. */
  438. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  439. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  440. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  441. ret = -ENOMEM;
  442. goto error;
  443. }
  444. uvc->control_req->buf = uvc->control_buf;
  445. uvc->control_req->complete = uvc_function_ep0_complete;
  446. uvc->control_req->context = uvc;
  447. /* Avoid letting this gadget enumerate until the userspace server is
  448. * active.
  449. */
  450. if ((ret = usb_function_deactivate(f)) < 0)
  451. goto error;
  452. /* Initialise video. */
  453. ret = uvc_video_init(&uvc->video);
  454. if (ret < 0)
  455. goto error;
  456. /* Register a V4L2 device. */
  457. ret = uvc_register_video(uvc);
  458. if (ret < 0) {
  459. printk(KERN_INFO "Unable to register video device\n");
  460. goto error;
  461. }
  462. return 0;
  463. error:
  464. uvc_function_unbind(c, f);
  465. return ret;
  466. }
  467. /* --------------------------------------------------------------------------
  468. * USB gadget function
  469. */
  470. /**
  471. * uvc_bind_config - add a UVC function to a configuration
  472. * @c: the configuration to support the UVC instance
  473. * Context: single threaded during gadget setup
  474. *
  475. * Returns zero on success, else negative errno.
  476. *
  477. * Caller must have called @uvc_setup(). Caller is also responsible for
  478. * calling @uvc_cleanup() before module unload.
  479. */
  480. int __init
  481. uvc_bind_config(struct usb_configuration *c,
  482. const struct uvc_descriptor_header * const *control,
  483. const struct uvc_descriptor_header * const *fs_streaming,
  484. const struct uvc_descriptor_header * const *hs_streaming)
  485. {
  486. struct uvc_device *uvc;
  487. int ret = 0;
  488. /* TODO Check if the USB device controller supports the required
  489. * features.
  490. */
  491. if (!gadget_is_dualspeed(c->cdev->gadget))
  492. return -EINVAL;
  493. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  494. if (uvc == NULL)
  495. return -ENOMEM;
  496. uvc->state = UVC_STATE_DISCONNECTED;
  497. /* Validate the descriptors. */
  498. if (control == NULL || control[0] == NULL ||
  499. control[0]->bDescriptorSubType != UVC_VC_HEADER)
  500. goto error;
  501. if (fs_streaming == NULL || fs_streaming[0] == NULL ||
  502. fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  503. goto error;
  504. if (hs_streaming == NULL || hs_streaming[0] == NULL ||
  505. hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  506. goto error;
  507. uvc->desc.control = control;
  508. uvc->desc.fs_streaming = fs_streaming;
  509. uvc->desc.hs_streaming = hs_streaming;
  510. /* Allocate string descriptor numbers. */
  511. if ((ret = usb_string_id(c->cdev)) < 0)
  512. goto error;
  513. uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
  514. uvc_iad.iFunction = ret;
  515. if ((ret = usb_string_id(c->cdev)) < 0)
  516. goto error;
  517. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
  518. uvc_control_intf.iInterface = ret;
  519. if ((ret = usb_string_id(c->cdev)) < 0)
  520. goto error;
  521. uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
  522. uvc_streaming_intf_alt0.iInterface = ret;
  523. uvc_streaming_intf_alt1.iInterface = ret;
  524. /* Register the function. */
  525. uvc->func.name = "uvc";
  526. uvc->func.strings = uvc_function_strings;
  527. uvc->func.bind = uvc_function_bind;
  528. uvc->func.unbind = uvc_function_unbind;
  529. uvc->func.get_alt = uvc_function_get_alt;
  530. uvc->func.set_alt = uvc_function_set_alt;
  531. uvc->func.disable = uvc_function_disable;
  532. uvc->func.setup = uvc_function_setup;
  533. ret = usb_add_function(c, &uvc->func);
  534. if (ret)
  535. kfree(uvc);
  536. return ret;
  537. error:
  538. kfree(uvc);
  539. return ret;
  540. }
  541. module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
  542. MODULE_PARM_DESC(trace, "Trace level bitmask");