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