f_uvc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. uvc->video.ep->desc = &uvc_streaming_ep;
  220. usb_ep_enable(uvc->video.ep);
  221. }
  222. memset(&v4l2_event, 0, sizeof(v4l2_event));
  223. v4l2_event.type = UVC_EVENT_STREAMON;
  224. v4l2_event_queue(uvc->vdev, &v4l2_event);
  225. uvc->state = UVC_STATE_STREAMING;
  226. break;
  227. default:
  228. return -EINVAL;
  229. }
  230. return 0;
  231. }
  232. static void
  233. uvc_function_disable(struct usb_function *f)
  234. {
  235. struct uvc_device *uvc = to_uvc(f);
  236. struct v4l2_event v4l2_event;
  237. INFO(f->config->cdev, "uvc_function_disable\n");
  238. memset(&v4l2_event, 0, sizeof(v4l2_event));
  239. v4l2_event.type = UVC_EVENT_DISCONNECT;
  240. v4l2_event_queue(uvc->vdev, &v4l2_event);
  241. uvc->state = UVC_STATE_DISCONNECTED;
  242. }
  243. /* --------------------------------------------------------------------------
  244. * Connection / disconnection
  245. */
  246. void
  247. uvc_function_connect(struct uvc_device *uvc)
  248. {
  249. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  250. int ret;
  251. if ((ret = usb_function_activate(&uvc->func)) < 0)
  252. INFO(cdev, "UVC connect failed with %d\n", ret);
  253. }
  254. void
  255. uvc_function_disconnect(struct uvc_device *uvc)
  256. {
  257. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  258. int ret;
  259. if ((ret = usb_function_deactivate(&uvc->func)) < 0)
  260. INFO(cdev, "UVC disconnect failed with %d\n", ret);
  261. }
  262. /* --------------------------------------------------------------------------
  263. * USB probe and disconnect
  264. */
  265. static int
  266. uvc_register_video(struct uvc_device *uvc)
  267. {
  268. struct usb_composite_dev *cdev = uvc->func.config->cdev;
  269. struct video_device *video;
  270. /* TODO reference counting. */
  271. video = video_device_alloc();
  272. if (video == NULL)
  273. return -ENOMEM;
  274. video->parent = &cdev->gadget->dev;
  275. video->minor = -1;
  276. video->fops = &uvc_v4l2_fops;
  277. video->release = video_device_release;
  278. strncpy(video->name, cdev->gadget->name, sizeof(video->name));
  279. uvc->vdev = video;
  280. video_set_drvdata(video, uvc);
  281. return video_register_device(video, VFL_TYPE_GRABBER, -1);
  282. }
  283. #define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
  284. do { \
  285. memcpy(mem, desc, (desc)->bLength); \
  286. *(dst)++ = mem; \
  287. mem += (desc)->bLength; \
  288. } while (0);
  289. #define UVC_COPY_DESCRIPTORS(mem, dst, src) \
  290. do { \
  291. const struct usb_descriptor_header * const *__src; \
  292. for (__src = src; *__src; ++__src) { \
  293. memcpy(mem, *__src, (*__src)->bLength); \
  294. *dst++ = mem; \
  295. mem += (*__src)->bLength; \
  296. } \
  297. } while (0)
  298. static struct usb_descriptor_header ** __init
  299. uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
  300. {
  301. struct uvc_input_header_descriptor *uvc_streaming_header;
  302. struct uvc_header_descriptor *uvc_control_header;
  303. const struct uvc_descriptor_header * const *uvc_streaming_cls;
  304. const struct usb_descriptor_header * const *uvc_streaming_std;
  305. const struct usb_descriptor_header * const *src;
  306. struct usb_descriptor_header **dst;
  307. struct usb_descriptor_header **hdr;
  308. unsigned int control_size;
  309. unsigned int streaming_size;
  310. unsigned int n_desc;
  311. unsigned int bytes;
  312. void *mem;
  313. uvc_streaming_cls = (speed == USB_SPEED_FULL)
  314. ? uvc->desc.fs_streaming : uvc->desc.hs_streaming;
  315. uvc_streaming_std = (speed == USB_SPEED_FULL)
  316. ? uvc_fs_streaming : uvc_hs_streaming;
  317. /* Descriptors layout
  318. *
  319. * uvc_iad
  320. * uvc_control_intf
  321. * Class-specific UVC control descriptors
  322. * uvc_control_ep
  323. * uvc_control_cs_ep
  324. * uvc_streaming_intf_alt0
  325. * Class-specific UVC streaming descriptors
  326. * uvc_{fs|hs}_streaming
  327. */
  328. /* Count descriptors and compute their size. */
  329. control_size = 0;
  330. streaming_size = 0;
  331. bytes = uvc_iad.bLength + uvc_control_intf.bLength
  332. + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
  333. + uvc_streaming_intf_alt0.bLength;
  334. n_desc = 5;
  335. for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) {
  336. control_size += (*src)->bLength;
  337. bytes += (*src)->bLength;
  338. n_desc++;
  339. }
  340. for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) {
  341. streaming_size += (*src)->bLength;
  342. bytes += (*src)->bLength;
  343. n_desc++;
  344. }
  345. for (src = uvc_streaming_std; *src; ++src) {
  346. bytes += (*src)->bLength;
  347. n_desc++;
  348. }
  349. mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
  350. if (mem == NULL)
  351. return NULL;
  352. hdr = mem;
  353. dst = mem;
  354. mem += (n_desc + 1) * sizeof(*src);
  355. /* Copy the descriptors. */
  356. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
  357. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
  358. uvc_control_header = mem;
  359. UVC_COPY_DESCRIPTORS(mem, dst,
  360. (const struct usb_descriptor_header**)uvc->desc.control);
  361. uvc_control_header->wTotalLength = cpu_to_le16(control_size);
  362. uvc_control_header->bInCollection = 1;
  363. uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
  364. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
  365. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
  366. UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
  367. uvc_streaming_header = mem;
  368. UVC_COPY_DESCRIPTORS(mem, dst,
  369. (const struct usb_descriptor_header**)uvc_streaming_cls);
  370. uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
  371. uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress;
  372. UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
  373. *dst = NULL;
  374. return hdr;
  375. }
  376. static void
  377. uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
  378. {
  379. struct usb_composite_dev *cdev = c->cdev;
  380. struct uvc_device *uvc = to_uvc(f);
  381. INFO(cdev, "uvc_function_unbind\n");
  382. if (uvc->vdev) {
  383. if (uvc->vdev->minor == -1)
  384. video_device_release(uvc->vdev);
  385. else
  386. video_unregister_device(uvc->vdev);
  387. uvc->vdev = NULL;
  388. }
  389. if (uvc->control_ep)
  390. uvc->control_ep->driver_data = NULL;
  391. if (uvc->video.ep)
  392. uvc->video.ep->driver_data = NULL;
  393. if (uvc->control_req) {
  394. usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
  395. kfree(uvc->control_buf);
  396. }
  397. kfree(f->descriptors);
  398. kfree(f->hs_descriptors);
  399. kfree(uvc);
  400. }
  401. static int __init
  402. uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
  403. {
  404. struct usb_composite_dev *cdev = c->cdev;
  405. struct uvc_device *uvc = to_uvc(f);
  406. struct usb_ep *ep;
  407. int ret = -EINVAL;
  408. INFO(cdev, "uvc_function_bind\n");
  409. /* Allocate endpoints. */
  410. ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
  411. if (!ep) {
  412. INFO(cdev, "Unable to allocate control EP\n");
  413. goto error;
  414. }
  415. uvc->control_ep = ep;
  416. ep->driver_data = uvc;
  417. ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep);
  418. if (!ep) {
  419. INFO(cdev, "Unable to allocate streaming EP\n");
  420. goto error;
  421. }
  422. uvc->video.ep = ep;
  423. ep->driver_data = uvc;
  424. /* Allocate interface IDs. */
  425. if ((ret = usb_interface_id(c, f)) < 0)
  426. goto error;
  427. uvc_iad.bFirstInterface = ret;
  428. uvc_control_intf.bInterfaceNumber = ret;
  429. uvc->control_intf = ret;
  430. if ((ret = usb_interface_id(c, f)) < 0)
  431. goto error;
  432. uvc_streaming_intf_alt0.bInterfaceNumber = ret;
  433. uvc_streaming_intf_alt1.bInterfaceNumber = ret;
  434. uvc->streaming_intf = ret;
  435. /* Copy descriptors. */
  436. f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
  437. f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
  438. /* Preallocate control endpoint request. */
  439. uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
  440. uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
  441. if (uvc->control_req == NULL || uvc->control_buf == NULL) {
  442. ret = -ENOMEM;
  443. goto error;
  444. }
  445. uvc->control_req->buf = uvc->control_buf;
  446. uvc->control_req->complete = uvc_function_ep0_complete;
  447. uvc->control_req->context = uvc;
  448. /* Avoid letting this gadget enumerate until the userspace server is
  449. * active.
  450. */
  451. if ((ret = usb_function_deactivate(f)) < 0)
  452. goto error;
  453. /* Initialise video. */
  454. ret = uvc_video_init(&uvc->video);
  455. if (ret < 0)
  456. goto error;
  457. /* Register a V4L2 device. */
  458. ret = uvc_register_video(uvc);
  459. if (ret < 0) {
  460. printk(KERN_INFO "Unable to register video device\n");
  461. goto error;
  462. }
  463. return 0;
  464. error:
  465. uvc_function_unbind(c, f);
  466. return ret;
  467. }
  468. /* --------------------------------------------------------------------------
  469. * USB gadget function
  470. */
  471. /**
  472. * uvc_bind_config - add a UVC function to a configuration
  473. * @c: the configuration to support the UVC instance
  474. * Context: single threaded during gadget setup
  475. *
  476. * Returns zero on success, else negative errno.
  477. *
  478. * Caller must have called @uvc_setup(). Caller is also responsible for
  479. * calling @uvc_cleanup() before module unload.
  480. */
  481. int __init
  482. uvc_bind_config(struct usb_configuration *c,
  483. const struct uvc_descriptor_header * const *control,
  484. const struct uvc_descriptor_header * const *fs_streaming,
  485. const struct uvc_descriptor_header * const *hs_streaming)
  486. {
  487. struct uvc_device *uvc;
  488. int ret = 0;
  489. /* TODO Check if the USB device controller supports the required
  490. * features.
  491. */
  492. if (!gadget_is_dualspeed(c->cdev->gadget))
  493. return -EINVAL;
  494. uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
  495. if (uvc == NULL)
  496. return -ENOMEM;
  497. uvc->state = UVC_STATE_DISCONNECTED;
  498. /* Validate the descriptors. */
  499. if (control == NULL || control[0] == NULL ||
  500. control[0]->bDescriptorSubType != UVC_VC_HEADER)
  501. goto error;
  502. if (fs_streaming == NULL || fs_streaming[0] == NULL ||
  503. fs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  504. goto error;
  505. if (hs_streaming == NULL || hs_streaming[0] == NULL ||
  506. hs_streaming[0]->bDescriptorSubType != UVC_VS_INPUT_HEADER)
  507. goto error;
  508. uvc->desc.control = control;
  509. uvc->desc.fs_streaming = fs_streaming;
  510. uvc->desc.hs_streaming = hs_streaming;
  511. /* Allocate string descriptor numbers. */
  512. if ((ret = usb_string_id(c->cdev)) < 0)
  513. goto error;
  514. uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
  515. uvc_iad.iFunction = ret;
  516. if ((ret = usb_string_id(c->cdev)) < 0)
  517. goto error;
  518. uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
  519. uvc_control_intf.iInterface = ret;
  520. if ((ret = usb_string_id(c->cdev)) < 0)
  521. goto error;
  522. uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
  523. uvc_streaming_intf_alt0.iInterface = ret;
  524. uvc_streaming_intf_alt1.iInterface = ret;
  525. /* Register the function. */
  526. uvc->func.name = "uvc";
  527. uvc->func.strings = uvc_function_strings;
  528. uvc->func.bind = uvc_function_bind;
  529. uvc->func.unbind = uvc_function_unbind;
  530. uvc->func.get_alt = uvc_function_get_alt;
  531. uvc->func.set_alt = uvc_function_set_alt;
  532. uvc->func.disable = uvc_function_disable;
  533. uvc->func.setup = uvc_function_setup;
  534. ret = usb_add_function(c, &uvc->func);
  535. if (ret)
  536. kfree(uvc);
  537. return ret;
  538. error:
  539. kfree(uvc);
  540. return ret;
  541. }
  542. module_param_named(trace, uvc_gadget_trace_param, uint, S_IRUGO|S_IWUSR);
  543. MODULE_PARM_DESC(trace, "Trace level bitmask");