uvc_video.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /*
  2. * uvc_video.c -- USB Video Class driver - Video handling
  3. *
  4. * Copyright (C) 2005-2009
  5. * Laurent Pinchart (laurent.pinchart@skynet.be)
  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/list.h>
  15. #include <linux/module.h>
  16. #include <linux/usb.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/wait.h>
  20. #include <asm/atomic.h>
  21. #include <asm/unaligned.h>
  22. #include <media/v4l2-common.h>
  23. #include "uvcvideo.h"
  24. /* ------------------------------------------------------------------------
  25. * UVC Controls
  26. */
  27. static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
  28. __u8 intfnum, __u8 cs, void *data, __u16 size,
  29. int timeout)
  30. {
  31. __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  32. unsigned int pipe;
  33. pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
  34. : usb_sndctrlpipe(dev->udev, 0);
  35. type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
  36. return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
  37. unit << 8 | intfnum, data, size, timeout);
  38. }
  39. int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
  40. __u8 intfnum, __u8 cs, void *data, __u16 size)
  41. {
  42. int ret;
  43. ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
  44. UVC_CTRL_CONTROL_TIMEOUT);
  45. if (ret != size) {
  46. uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u "
  47. "(unit %u) : %d (exp. %u).\n", query, cs, unit, ret,
  48. size);
  49. return -EIO;
  50. }
  51. return 0;
  52. }
  53. static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,
  54. struct uvc_streaming_control *ctrl)
  55. {
  56. struct uvc_format *format;
  57. struct uvc_frame *frame = NULL;
  58. unsigned int i;
  59. if (ctrl->bFormatIndex <= 0 ||
  60. ctrl->bFormatIndex > stream->nformats)
  61. return;
  62. format = &stream->format[ctrl->bFormatIndex - 1];
  63. for (i = 0; i < format->nframes; ++i) {
  64. if (format->frame[i].bFrameIndex == ctrl->bFrameIndex) {
  65. frame = &format->frame[i];
  66. break;
  67. }
  68. }
  69. if (frame == NULL)
  70. return;
  71. if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
  72. (ctrl->dwMaxVideoFrameSize == 0 &&
  73. stream->dev->uvc_version < 0x0110))
  74. ctrl->dwMaxVideoFrameSize =
  75. frame->dwMaxVideoFrameBufferSize;
  76. if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) &&
  77. stream->dev->quirks & UVC_QUIRK_FIX_BANDWIDTH &&
  78. stream->intf->num_altsetting > 1) {
  79. u32 interval;
  80. u32 bandwidth;
  81. interval = (ctrl->dwFrameInterval > 100000)
  82. ? ctrl->dwFrameInterval
  83. : frame->dwFrameInterval[0];
  84. /* Compute a bandwidth estimation by multiplying the frame
  85. * size by the number of video frames per second, divide the
  86. * result by the number of USB frames (or micro-frames for
  87. * high-speed devices) per second and add the UVC header size
  88. * (assumed to be 12 bytes long).
  89. */
  90. bandwidth = frame->wWidth * frame->wHeight / 8 * format->bpp;
  91. bandwidth *= 10000000 / interval + 1;
  92. bandwidth /= 1000;
  93. if (stream->dev->udev->speed == USB_SPEED_HIGH)
  94. bandwidth /= 8;
  95. bandwidth += 12;
  96. ctrl->dwMaxPayloadTransferSize = bandwidth;
  97. }
  98. }
  99. static int uvc_get_video_ctrl(struct uvc_streaming *stream,
  100. struct uvc_streaming_control *ctrl, int probe, __u8 query)
  101. {
  102. __u8 *data;
  103. __u16 size;
  104. int ret;
  105. size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
  106. if ((stream->dev->quirks & UVC_QUIRK_PROBE_DEF) &&
  107. query == UVC_GET_DEF)
  108. return -EIO;
  109. data = kmalloc(size, GFP_KERNEL);
  110. if (data == NULL)
  111. return -ENOMEM;
  112. ret = __uvc_query_ctrl(stream->dev, query, 0, stream->intfnum,
  113. probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
  114. size, uvc_timeout_param);
  115. if ((query == UVC_GET_MIN || query == UVC_GET_MAX) && ret == 2) {
  116. /* Some cameras, mostly based on Bison Electronics chipsets,
  117. * answer a GET_MIN or GET_MAX request with the wCompQuality
  118. * field only.
  119. */
  120. uvc_warn_once(stream->dev, UVC_WARN_MINMAX, "UVC non "
  121. "compliance - GET_MIN/MAX(PROBE) incorrectly "
  122. "supported. Enabling workaround.\n");
  123. memset(ctrl, 0, sizeof *ctrl);
  124. ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
  125. ret = 0;
  126. goto out;
  127. } else if (query == UVC_GET_DEF && probe == 1 && ret != size) {
  128. /* Many cameras don't support the GET_DEF request on their
  129. * video probe control. Warn once and return, the caller will
  130. * fall back to GET_CUR.
  131. */
  132. uvc_warn_once(stream->dev, UVC_WARN_PROBE_DEF, "UVC non "
  133. "compliance - GET_DEF(PROBE) not supported. "
  134. "Enabling workaround.\n");
  135. ret = -EIO;
  136. goto out;
  137. } else if (ret != size) {
  138. uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
  139. "%d (exp. %u).\n", query, probe ? "probe" : "commit",
  140. ret, size);
  141. ret = -EIO;
  142. goto out;
  143. }
  144. ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
  145. ctrl->bFormatIndex = data[2];
  146. ctrl->bFrameIndex = data[3];
  147. ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
  148. ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
  149. ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
  150. ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
  151. ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
  152. ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
  153. ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
  154. ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
  155. if (size == 34) {
  156. ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
  157. ctrl->bmFramingInfo = data[30];
  158. ctrl->bPreferedVersion = data[31];
  159. ctrl->bMinVersion = data[32];
  160. ctrl->bMaxVersion = data[33];
  161. } else {
  162. ctrl->dwClockFrequency = stream->dev->clock_frequency;
  163. ctrl->bmFramingInfo = 0;
  164. ctrl->bPreferedVersion = 0;
  165. ctrl->bMinVersion = 0;
  166. ctrl->bMaxVersion = 0;
  167. }
  168. /* Some broken devices return null or wrong dwMaxVideoFrameSize and
  169. * dwMaxPayloadTransferSize fields. Try to get the value from the
  170. * format and frame descriptors.
  171. */
  172. uvc_fixup_video_ctrl(stream, ctrl);
  173. ret = 0;
  174. out:
  175. kfree(data);
  176. return ret;
  177. }
  178. static int uvc_set_video_ctrl(struct uvc_streaming *stream,
  179. struct uvc_streaming_control *ctrl, int probe)
  180. {
  181. __u8 *data;
  182. __u16 size;
  183. int ret;
  184. size = stream->dev->uvc_version >= 0x0110 ? 34 : 26;
  185. data = kzalloc(size, GFP_KERNEL);
  186. if (data == NULL)
  187. return -ENOMEM;
  188. *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
  189. data[2] = ctrl->bFormatIndex;
  190. data[3] = ctrl->bFrameIndex;
  191. *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
  192. *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
  193. *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
  194. *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
  195. *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
  196. *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
  197. put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
  198. put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
  199. if (size == 34) {
  200. put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
  201. data[30] = ctrl->bmFramingInfo;
  202. data[31] = ctrl->bPreferedVersion;
  203. data[32] = ctrl->bMinVersion;
  204. data[33] = ctrl->bMaxVersion;
  205. }
  206. ret = __uvc_query_ctrl(stream->dev, UVC_SET_CUR, 0, stream->intfnum,
  207. probe ? UVC_VS_PROBE_CONTROL : UVC_VS_COMMIT_CONTROL, data,
  208. size, uvc_timeout_param);
  209. if (ret != size) {
  210. uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
  211. "%d (exp. %u).\n", probe ? "probe" : "commit",
  212. ret, size);
  213. ret = -EIO;
  214. }
  215. kfree(data);
  216. return ret;
  217. }
  218. int uvc_probe_video(struct uvc_streaming *stream,
  219. struct uvc_streaming_control *probe)
  220. {
  221. struct uvc_streaming_control probe_min, probe_max;
  222. __u16 bandwidth;
  223. unsigned int i;
  224. int ret;
  225. mutex_lock(&stream->mutex);
  226. /* Perform probing. The device should adjust the requested values
  227. * according to its capabilities. However, some devices, namely the
  228. * first generation UVC Logitech webcams, don't implement the Video
  229. * Probe control properly, and just return the needed bandwidth. For
  230. * that reason, if the needed bandwidth exceeds the maximum available
  231. * bandwidth, try to lower the quality.
  232. */
  233. ret = uvc_set_video_ctrl(stream, probe, 1);
  234. if (ret < 0)
  235. goto done;
  236. /* Get the minimum and maximum values for compression settings. */
  237. if (!(stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
  238. ret = uvc_get_video_ctrl(stream, &probe_min, 1, UVC_GET_MIN);
  239. if (ret < 0)
  240. goto done;
  241. ret = uvc_get_video_ctrl(stream, &probe_max, 1, UVC_GET_MAX);
  242. if (ret < 0)
  243. goto done;
  244. probe->wCompQuality = probe_max.wCompQuality;
  245. }
  246. for (i = 0; i < 2; ++i) {
  247. ret = uvc_set_video_ctrl(stream, probe, 1);
  248. if (ret < 0)
  249. goto done;
  250. ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
  251. if (ret < 0)
  252. goto done;
  253. if (stream->intf->num_altsetting == 1)
  254. break;
  255. bandwidth = probe->dwMaxPayloadTransferSize;
  256. if (bandwidth <= stream->maxpsize)
  257. break;
  258. if (stream->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
  259. ret = -ENOSPC;
  260. goto done;
  261. }
  262. /* TODO: negotiate compression parameters */
  263. probe->wKeyFrameRate = probe_min.wKeyFrameRate;
  264. probe->wPFrameRate = probe_min.wPFrameRate;
  265. probe->wCompQuality = probe_max.wCompQuality;
  266. probe->wCompWindowSize = probe_min.wCompWindowSize;
  267. }
  268. done:
  269. mutex_unlock(&stream->mutex);
  270. return ret;
  271. }
  272. int uvc_commit_video(struct uvc_streaming *stream,
  273. struct uvc_streaming_control *probe)
  274. {
  275. return uvc_set_video_ctrl(stream, probe, 0);
  276. }
  277. /* ------------------------------------------------------------------------
  278. * Video codecs
  279. */
  280. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  281. #define UVC_STREAM_EOH (1 << 7)
  282. #define UVC_STREAM_ERR (1 << 6)
  283. #define UVC_STREAM_STI (1 << 5)
  284. #define UVC_STREAM_RES (1 << 4)
  285. #define UVC_STREAM_SCR (1 << 3)
  286. #define UVC_STREAM_PTS (1 << 2)
  287. #define UVC_STREAM_EOF (1 << 1)
  288. #define UVC_STREAM_FID (1 << 0)
  289. /* Video payload decoding is handled by uvc_video_decode_start(),
  290. * uvc_video_decode_data() and uvc_video_decode_end().
  291. *
  292. * uvc_video_decode_start is called with URB data at the start of a bulk or
  293. * isochronous payload. It processes header data and returns the header size
  294. * in bytes if successful. If an error occurs, it returns a negative error
  295. * code. The following error codes have special meanings.
  296. *
  297. * - EAGAIN informs the caller that the current video buffer should be marked
  298. * as done, and that the function should be called again with the same data
  299. * and a new video buffer. This is used when end of frame conditions can be
  300. * reliably detected at the beginning of the next frame only.
  301. *
  302. * If an error other than -EAGAIN is returned, the caller will drop the current
  303. * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
  304. * made until the next payload. -ENODATA can be used to drop the current
  305. * payload if no other error code is appropriate.
  306. *
  307. * uvc_video_decode_data is called for every URB with URB data. It copies the
  308. * data to the video buffer.
  309. *
  310. * uvc_video_decode_end is called with header data at the end of a bulk or
  311. * isochronous payload. It performs any additional header data processing and
  312. * returns 0 or a negative error code if an error occured. As header data have
  313. * already been processed by uvc_video_decode_start, this functions isn't
  314. * required to perform sanity checks a second time.
  315. *
  316. * For isochronous transfers where a payload is always transfered in a single
  317. * URB, the three functions will be called in a row.
  318. *
  319. * To let the decoder process header data and update its internal state even
  320. * when no video buffer is available, uvc_video_decode_start must be prepared
  321. * to be called with a NULL buf parameter. uvc_video_decode_data and
  322. * uvc_video_decode_end will never be called with a NULL buffer.
  323. */
  324. static int uvc_video_decode_start(struct uvc_streaming *stream,
  325. struct uvc_buffer *buf, const __u8 *data, int len)
  326. {
  327. __u8 fid;
  328. /* Sanity checks:
  329. * - packet must be at least 2 bytes long
  330. * - bHeaderLength value must be at least 2 bytes (see above)
  331. * - bHeaderLength value can't be larger than the packet size.
  332. */
  333. if (len < 2 || data[0] < 2 || data[0] > len)
  334. return -EINVAL;
  335. /* Skip payloads marked with the error bit ("error frames"). */
  336. if (data[1] & UVC_STREAM_ERR) {
  337. uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
  338. "set).\n");
  339. return -ENODATA;
  340. }
  341. fid = data[1] & UVC_STREAM_FID;
  342. /* Store the payload FID bit and return immediately when the buffer is
  343. * NULL.
  344. */
  345. if (buf == NULL) {
  346. stream->last_fid = fid;
  347. return -ENODATA;
  348. }
  349. /* Synchronize to the input stream by waiting for the FID bit to be
  350. * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
  351. * stream->last_fid is initialized to -1, so the first isochronous
  352. * frame will always be in sync.
  353. *
  354. * If the device doesn't toggle the FID bit, invert stream->last_fid
  355. * when the EOF bit is set to force synchronisation on the next packet.
  356. */
  357. if (buf->state != UVC_BUF_STATE_ACTIVE) {
  358. if (fid == stream->last_fid) {
  359. uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
  360. "sync).\n");
  361. if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
  362. (data[1] & UVC_STREAM_EOF))
  363. stream->last_fid ^= UVC_STREAM_FID;
  364. return -ENODATA;
  365. }
  366. /* TODO: Handle PTS and SCR. */
  367. buf->state = UVC_BUF_STATE_ACTIVE;
  368. }
  369. /* Mark the buffer as done if we're at the beginning of a new frame.
  370. * End of frame detection is better implemented by checking the EOF
  371. * bit (FID bit toggling is delayed by one frame compared to the EOF
  372. * bit), but some devices don't set the bit at end of frame (and the
  373. * last payload can be lost anyway). We thus must check if the FID has
  374. * been toggled.
  375. *
  376. * stream->last_fid is initialized to -1, so the first isochronous
  377. * frame will never trigger an end of frame detection.
  378. *
  379. * Empty buffers (bytesused == 0) don't trigger end of frame detection
  380. * as it doesn't make sense to return an empty buffer. This also
  381. * avoids detecting end of frame conditions at FID toggling if the
  382. * previous payload had the EOF bit set.
  383. */
  384. if (fid != stream->last_fid && buf->buf.bytesused != 0) {
  385. uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
  386. "toggled).\n");
  387. buf->state = UVC_BUF_STATE_READY;
  388. return -EAGAIN;
  389. }
  390. stream->last_fid = fid;
  391. return data[0];
  392. }
  393. static void uvc_video_decode_data(struct uvc_streaming *stream,
  394. struct uvc_buffer *buf, const __u8 *data, int len)
  395. {
  396. struct uvc_video_queue *queue = &stream->queue;
  397. unsigned int maxlen, nbytes;
  398. void *mem;
  399. if (len <= 0)
  400. return;
  401. /* Copy the video data to the buffer. */
  402. maxlen = buf->buf.length - buf->buf.bytesused;
  403. mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
  404. nbytes = min((unsigned int)len, maxlen);
  405. memcpy(mem, data, nbytes);
  406. buf->buf.bytesused += nbytes;
  407. /* Complete the current frame if the buffer size was exceeded. */
  408. if (len > maxlen) {
  409. uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
  410. buf->state = UVC_BUF_STATE_READY;
  411. }
  412. }
  413. static void uvc_video_decode_end(struct uvc_streaming *stream,
  414. struct uvc_buffer *buf, const __u8 *data, int len)
  415. {
  416. /* Mark the buffer as done if the EOF marker is set. */
  417. if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
  418. uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
  419. if (data[0] == len)
  420. uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
  421. buf->state = UVC_BUF_STATE_READY;
  422. if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
  423. stream->last_fid ^= UVC_STREAM_FID;
  424. }
  425. }
  426. /* Video payload encoding is handled by uvc_video_encode_header() and
  427. * uvc_video_encode_data(). Only bulk transfers are currently supported.
  428. *
  429. * uvc_video_encode_header is called at the start of a payload. It adds header
  430. * data to the transfer buffer and returns the header size. As the only known
  431. * UVC output device transfers a whole frame in a single payload, the EOF bit
  432. * is always set in the header.
  433. *
  434. * uvc_video_encode_data is called for every URB and copies the data from the
  435. * video buffer to the transfer buffer.
  436. */
  437. static int uvc_video_encode_header(struct uvc_streaming *stream,
  438. struct uvc_buffer *buf, __u8 *data, int len)
  439. {
  440. data[0] = 2; /* Header length */
  441. data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
  442. | (stream->last_fid & UVC_STREAM_FID);
  443. return 2;
  444. }
  445. static int uvc_video_encode_data(struct uvc_streaming *stream,
  446. struct uvc_buffer *buf, __u8 *data, int len)
  447. {
  448. struct uvc_video_queue *queue = &stream->queue;
  449. unsigned int nbytes;
  450. void *mem;
  451. /* Copy video data to the URB buffer. */
  452. mem = queue->mem + buf->buf.m.offset + queue->buf_used;
  453. nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
  454. nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
  455. nbytes);
  456. memcpy(data, mem, nbytes);
  457. queue->buf_used += nbytes;
  458. return nbytes;
  459. }
  460. /* ------------------------------------------------------------------------
  461. * URB handling
  462. */
  463. /*
  464. * Completion handler for video URBs.
  465. */
  466. static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
  467. struct uvc_buffer *buf)
  468. {
  469. u8 *mem;
  470. int ret, i;
  471. for (i = 0; i < urb->number_of_packets; ++i) {
  472. if (urb->iso_frame_desc[i].status < 0) {
  473. uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
  474. "lost (%d).\n", urb->iso_frame_desc[i].status);
  475. continue;
  476. }
  477. /* Decode the payload header. */
  478. mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  479. do {
  480. ret = uvc_video_decode_start(stream, buf, mem,
  481. urb->iso_frame_desc[i].actual_length);
  482. if (ret == -EAGAIN)
  483. buf = uvc_queue_next_buffer(&stream->queue,
  484. buf);
  485. } while (ret == -EAGAIN);
  486. if (ret < 0)
  487. continue;
  488. /* Decode the payload data. */
  489. uvc_video_decode_data(stream, buf, mem + ret,
  490. urb->iso_frame_desc[i].actual_length - ret);
  491. /* Process the header again. */
  492. uvc_video_decode_end(stream, buf, mem,
  493. urb->iso_frame_desc[i].actual_length);
  494. if (buf->state == UVC_BUF_STATE_READY)
  495. buf = uvc_queue_next_buffer(&stream->queue, buf);
  496. }
  497. }
  498. static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
  499. struct uvc_buffer *buf)
  500. {
  501. u8 *mem;
  502. int len, ret;
  503. if (urb->actual_length == 0)
  504. return;
  505. mem = urb->transfer_buffer;
  506. len = urb->actual_length;
  507. stream->bulk.payload_size += len;
  508. /* If the URB is the first of its payload, decode and save the
  509. * header.
  510. */
  511. if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
  512. do {
  513. ret = uvc_video_decode_start(stream, buf, mem, len);
  514. if (ret == -EAGAIN)
  515. buf = uvc_queue_next_buffer(&stream->queue,
  516. buf);
  517. } while (ret == -EAGAIN);
  518. /* If an error occured skip the rest of the payload. */
  519. if (ret < 0 || buf == NULL) {
  520. stream->bulk.skip_payload = 1;
  521. } else {
  522. memcpy(stream->bulk.header, mem, ret);
  523. stream->bulk.header_size = ret;
  524. mem += ret;
  525. len -= ret;
  526. }
  527. }
  528. /* The buffer queue might have been cancelled while a bulk transfer
  529. * was in progress, so we can reach here with buf equal to NULL. Make
  530. * sure buf is never dereferenced if NULL.
  531. */
  532. /* Process video data. */
  533. if (!stream->bulk.skip_payload && buf != NULL)
  534. uvc_video_decode_data(stream, buf, mem, len);
  535. /* Detect the payload end by a URB smaller than the maximum size (or
  536. * a payload size equal to the maximum) and process the header again.
  537. */
  538. if (urb->actual_length < urb->transfer_buffer_length ||
  539. stream->bulk.payload_size >= stream->bulk.max_payload_size) {
  540. if (!stream->bulk.skip_payload && buf != NULL) {
  541. uvc_video_decode_end(stream, buf, stream->bulk.header,
  542. stream->bulk.payload_size);
  543. if (buf->state == UVC_BUF_STATE_READY)
  544. buf = uvc_queue_next_buffer(&stream->queue,
  545. buf);
  546. }
  547. stream->bulk.header_size = 0;
  548. stream->bulk.skip_payload = 0;
  549. stream->bulk.payload_size = 0;
  550. }
  551. }
  552. static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
  553. struct uvc_buffer *buf)
  554. {
  555. u8 *mem = urb->transfer_buffer;
  556. int len = stream->urb_size, ret;
  557. if (buf == NULL) {
  558. urb->transfer_buffer_length = 0;
  559. return;
  560. }
  561. /* If the URB is the first of its payload, add the header. */
  562. if (stream->bulk.header_size == 0) {
  563. ret = uvc_video_encode_header(stream, buf, mem, len);
  564. stream->bulk.header_size = ret;
  565. stream->bulk.payload_size += ret;
  566. mem += ret;
  567. len -= ret;
  568. }
  569. /* Process video data. */
  570. ret = uvc_video_encode_data(stream, buf, mem, len);
  571. stream->bulk.payload_size += ret;
  572. len -= ret;
  573. if (buf->buf.bytesused == stream->queue.buf_used ||
  574. stream->bulk.payload_size == stream->bulk.max_payload_size) {
  575. if (buf->buf.bytesused == stream->queue.buf_used) {
  576. stream->queue.buf_used = 0;
  577. buf->state = UVC_BUF_STATE_READY;
  578. uvc_queue_next_buffer(&stream->queue, buf);
  579. stream->last_fid ^= UVC_STREAM_FID;
  580. }
  581. stream->bulk.header_size = 0;
  582. stream->bulk.payload_size = 0;
  583. }
  584. urb->transfer_buffer_length = stream->urb_size - len;
  585. }
  586. static void uvc_video_complete(struct urb *urb)
  587. {
  588. struct uvc_streaming *stream = urb->context;
  589. struct uvc_video_queue *queue = &stream->queue;
  590. struct uvc_buffer *buf = NULL;
  591. unsigned long flags;
  592. int ret;
  593. switch (urb->status) {
  594. case 0:
  595. break;
  596. default:
  597. uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
  598. "completion handler.\n", urb->status);
  599. case -ENOENT: /* usb_kill_urb() called. */
  600. if (stream->frozen)
  601. return;
  602. case -ECONNRESET: /* usb_unlink_urb() called. */
  603. case -ESHUTDOWN: /* The endpoint is being disabled. */
  604. uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
  605. return;
  606. }
  607. spin_lock_irqsave(&queue->irqlock, flags);
  608. if (!list_empty(&queue->irqqueue))
  609. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  610. queue);
  611. spin_unlock_irqrestore(&queue->irqlock, flags);
  612. stream->decode(urb, stream, buf);
  613. if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  614. uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
  615. ret);
  616. }
  617. }
  618. /*
  619. * Free transfer buffers.
  620. */
  621. static void uvc_free_urb_buffers(struct uvc_streaming *stream)
  622. {
  623. unsigned int i;
  624. for (i = 0; i < UVC_URBS; ++i) {
  625. if (stream->urb_buffer[i]) {
  626. usb_buffer_free(stream->dev->udev, stream->urb_size,
  627. stream->urb_buffer[i], stream->urb_dma[i]);
  628. stream->urb_buffer[i] = NULL;
  629. }
  630. }
  631. stream->urb_size = 0;
  632. }
  633. /*
  634. * Allocate transfer buffers. This function can be called with buffers
  635. * already allocated when resuming from suspend, in which case it will
  636. * return without touching the buffers.
  637. *
  638. * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
  639. * system is too low on memory try successively smaller numbers of packets
  640. * until allocation succeeds.
  641. *
  642. * Return the number of allocated packets on success or 0 when out of memory.
  643. */
  644. static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
  645. unsigned int size, unsigned int psize, gfp_t gfp_flags)
  646. {
  647. unsigned int npackets;
  648. unsigned int i;
  649. /* Buffers are already allocated, bail out. */
  650. if (stream->urb_size)
  651. return stream->urb_size / psize;
  652. /* Compute the number of packets. Bulk endpoints might transfer UVC
  653. * payloads accross multiple URBs.
  654. */
  655. npackets = DIV_ROUND_UP(size, psize);
  656. if (npackets > UVC_MAX_PACKETS)
  657. npackets = UVC_MAX_PACKETS;
  658. /* Retry allocations until one succeed. */
  659. for (; npackets > 1; npackets /= 2) {
  660. for (i = 0; i < UVC_URBS; ++i) {
  661. stream->urb_size = psize * npackets;
  662. stream->urb_buffer[i] = usb_buffer_alloc(
  663. stream->dev->udev, stream->urb_size,
  664. gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
  665. if (!stream->urb_buffer[i]) {
  666. uvc_free_urb_buffers(stream);
  667. break;
  668. }
  669. }
  670. if (i == UVC_URBS) {
  671. uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
  672. "of %ux%u bytes each.\n", UVC_URBS, npackets,
  673. psize);
  674. return npackets;
  675. }
  676. }
  677. uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
  678. "per packet).\n", psize);
  679. return 0;
  680. }
  681. /*
  682. * Uninitialize isochronous/bulk URBs and free transfer buffers.
  683. */
  684. static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
  685. {
  686. struct urb *urb;
  687. unsigned int i;
  688. for (i = 0; i < UVC_URBS; ++i) {
  689. urb = stream->urb[i];
  690. if (urb == NULL)
  691. continue;
  692. usb_kill_urb(urb);
  693. usb_free_urb(urb);
  694. stream->urb[i] = NULL;
  695. }
  696. if (free_buffers)
  697. uvc_free_urb_buffers(stream);
  698. }
  699. /*
  700. * Initialize isochronous URBs and allocate transfer buffers. The packet size
  701. * is given by the endpoint.
  702. */
  703. static int uvc_init_video_isoc(struct uvc_streaming *stream,
  704. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  705. {
  706. struct urb *urb;
  707. unsigned int npackets, i, j;
  708. u16 psize;
  709. u32 size;
  710. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  711. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  712. size = stream->ctrl.dwMaxVideoFrameSize;
  713. npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
  714. if (npackets == 0)
  715. return -ENOMEM;
  716. size = npackets * psize;
  717. for (i = 0; i < UVC_URBS; ++i) {
  718. urb = usb_alloc_urb(npackets, gfp_flags);
  719. if (urb == NULL) {
  720. uvc_uninit_video(stream, 1);
  721. return -ENOMEM;
  722. }
  723. urb->dev = stream->dev->udev;
  724. urb->context = stream;
  725. urb->pipe = usb_rcvisocpipe(stream->dev->udev,
  726. ep->desc.bEndpointAddress);
  727. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  728. urb->interval = ep->desc.bInterval;
  729. urb->transfer_buffer = stream->urb_buffer[i];
  730. urb->transfer_dma = stream->urb_dma[i];
  731. urb->complete = uvc_video_complete;
  732. urb->number_of_packets = npackets;
  733. urb->transfer_buffer_length = size;
  734. for (j = 0; j < npackets; ++j) {
  735. urb->iso_frame_desc[j].offset = j * psize;
  736. urb->iso_frame_desc[j].length = psize;
  737. }
  738. stream->urb[i] = urb;
  739. }
  740. return 0;
  741. }
  742. /*
  743. * Initialize bulk URBs and allocate transfer buffers. The packet size is
  744. * given by the endpoint.
  745. */
  746. static int uvc_init_video_bulk(struct uvc_streaming *stream,
  747. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  748. {
  749. struct urb *urb;
  750. unsigned int npackets, pipe, i;
  751. u16 psize;
  752. u32 size;
  753. psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
  754. size = stream->ctrl.dwMaxPayloadTransferSize;
  755. stream->bulk.max_payload_size = size;
  756. npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
  757. if (npackets == 0)
  758. return -ENOMEM;
  759. size = npackets * psize;
  760. if (usb_endpoint_dir_in(&ep->desc))
  761. pipe = usb_rcvbulkpipe(stream->dev->udev,
  762. ep->desc.bEndpointAddress);
  763. else
  764. pipe = usb_sndbulkpipe(stream->dev->udev,
  765. ep->desc.bEndpointAddress);
  766. if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  767. size = 0;
  768. for (i = 0; i < UVC_URBS; ++i) {
  769. urb = usb_alloc_urb(0, gfp_flags);
  770. if (urb == NULL) {
  771. uvc_uninit_video(stream, 1);
  772. return -ENOMEM;
  773. }
  774. usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
  775. stream->urb_buffer[i], size, uvc_video_complete,
  776. stream);
  777. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  778. urb->transfer_dma = stream->urb_dma[i];
  779. stream->urb[i] = urb;
  780. }
  781. return 0;
  782. }
  783. /*
  784. * Initialize isochronous/bulk URBs and allocate transfer buffers.
  785. */
  786. static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
  787. {
  788. struct usb_interface *intf = stream->intf;
  789. struct usb_host_endpoint *ep;
  790. unsigned int i;
  791. int ret;
  792. stream->last_fid = -1;
  793. stream->bulk.header_size = 0;
  794. stream->bulk.skip_payload = 0;
  795. stream->bulk.payload_size = 0;
  796. if (intf->num_altsetting > 1) {
  797. struct usb_host_endpoint *best_ep = NULL;
  798. unsigned int best_psize = 3 * 1024;
  799. unsigned int bandwidth;
  800. unsigned int uninitialized_var(altsetting);
  801. int intfnum = stream->intfnum;
  802. /* Isochronous endpoint, select the alternate setting. */
  803. bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
  804. if (bandwidth == 0) {
  805. uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
  806. "bandwidth, defaulting to lowest.\n");
  807. bandwidth = 1;
  808. } else {
  809. uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
  810. "B/frame bandwidth.\n", bandwidth);
  811. }
  812. for (i = 0; i < intf->num_altsetting; ++i) {
  813. struct usb_host_interface *alts;
  814. unsigned int psize;
  815. alts = &intf->altsetting[i];
  816. ep = uvc_find_endpoint(alts,
  817. stream->header.bEndpointAddress);
  818. if (ep == NULL)
  819. continue;
  820. /* Check if the bandwidth is high enough. */
  821. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  822. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  823. if (psize >= bandwidth && psize <= best_psize) {
  824. altsetting = i;
  825. best_psize = psize;
  826. best_ep = ep;
  827. }
  828. }
  829. if (best_ep == NULL) {
  830. uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
  831. "for requested bandwidth.\n");
  832. return -EIO;
  833. }
  834. uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
  835. "(%u B/frame bandwidth).\n", altsetting, best_psize);
  836. ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
  837. if (ret < 0)
  838. return ret;
  839. ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
  840. } else {
  841. /* Bulk endpoint, proceed to URB initialization. */
  842. ep = uvc_find_endpoint(&intf->altsetting[0],
  843. stream->header.bEndpointAddress);
  844. if (ep == NULL)
  845. return -EIO;
  846. ret = uvc_init_video_bulk(stream, ep, gfp_flags);
  847. }
  848. if (ret < 0)
  849. return ret;
  850. /* Submit the URBs. */
  851. for (i = 0; i < UVC_URBS; ++i) {
  852. ret = usb_submit_urb(stream->urb[i], gfp_flags);
  853. if (ret < 0) {
  854. uvc_printk(KERN_ERR, "Failed to submit URB %u "
  855. "(%d).\n", i, ret);
  856. uvc_uninit_video(stream, 1);
  857. return ret;
  858. }
  859. }
  860. return 0;
  861. }
  862. /* --------------------------------------------------------------------------
  863. * Suspend/resume
  864. */
  865. /*
  866. * Stop streaming without disabling the video queue.
  867. *
  868. * To let userspace applications resume without trouble, we must not touch the
  869. * video buffers in any way. We mark the device as frozen to make sure the URB
  870. * completion handler won't try to cancel the queue when we kill the URBs.
  871. */
  872. int uvc_video_suspend(struct uvc_streaming *stream)
  873. {
  874. if (!uvc_queue_streaming(&stream->queue))
  875. return 0;
  876. stream->frozen = 1;
  877. uvc_uninit_video(stream, 0);
  878. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  879. return 0;
  880. }
  881. /*
  882. * Reconfigure the video interface and restart streaming if it was enabled
  883. * before suspend.
  884. *
  885. * If an error occurs, disable the video queue. This will wake all pending
  886. * buffers, making sure userspace applications are notified of the problem
  887. * instead of waiting forever.
  888. */
  889. int uvc_video_resume(struct uvc_streaming *stream)
  890. {
  891. int ret;
  892. stream->frozen = 0;
  893. ret = uvc_commit_video(stream, &stream->ctrl);
  894. if (ret < 0) {
  895. uvc_queue_enable(&stream->queue, 0);
  896. return ret;
  897. }
  898. if (!uvc_queue_streaming(&stream->queue))
  899. return 0;
  900. ret = uvc_init_video(stream, GFP_NOIO);
  901. if (ret < 0)
  902. uvc_queue_enable(&stream->queue, 0);
  903. return ret;
  904. }
  905. /* ------------------------------------------------------------------------
  906. * Video device
  907. */
  908. /*
  909. * Initialize the UVC video device by switching to alternate setting 0 and
  910. * retrieve the default format.
  911. *
  912. * Some cameras (namely the Fuji Finepix) set the format and frame
  913. * indexes to zero. The UVC standard doesn't clearly make this a spec
  914. * violation, so try to silently fix the values if possible.
  915. *
  916. * This function is called before registering the device with V4L.
  917. */
  918. int uvc_video_init(struct uvc_streaming *stream)
  919. {
  920. struct uvc_streaming_control *probe = &stream->ctrl;
  921. struct uvc_format *format = NULL;
  922. struct uvc_frame *frame = NULL;
  923. unsigned int i;
  924. int ret;
  925. if (stream->nformats == 0) {
  926. uvc_printk(KERN_INFO, "No supported video formats found.\n");
  927. return -EINVAL;
  928. }
  929. atomic_set(&stream->active, 0);
  930. /* Initialize the video buffers queue. */
  931. uvc_queue_init(&stream->queue, stream->type);
  932. /* Alternate setting 0 should be the default, yet the XBox Live Vision
  933. * Cam (and possibly other devices) crash or otherwise misbehave if
  934. * they don't receive a SET_INTERFACE request before any other video
  935. * control request.
  936. */
  937. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  938. /* Set the streaming probe control with default streaming parameters
  939. * retrieved from the device. Webcams that don't suport GET_DEF
  940. * requests on the probe control will just keep their current streaming
  941. * parameters.
  942. */
  943. if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
  944. uvc_set_video_ctrl(stream, probe, 1);
  945. /* Initialize the streaming parameters with the probe control current
  946. * value. This makes sure SET_CUR requests on the streaming commit
  947. * control will always use values retrieved from a successful GET_CUR
  948. * request on the probe control, as required by the UVC specification.
  949. */
  950. ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
  951. if (ret < 0)
  952. return ret;
  953. /* Check if the default format descriptor exists. Use the first
  954. * available format otherwise.
  955. */
  956. for (i = stream->nformats; i > 0; --i) {
  957. format = &stream->format[i-1];
  958. if (format->index == probe->bFormatIndex)
  959. break;
  960. }
  961. if (format->nframes == 0) {
  962. uvc_printk(KERN_INFO, "No frame descriptor found for the "
  963. "default format.\n");
  964. return -EINVAL;
  965. }
  966. /* Zero bFrameIndex might be correct. Stream-based formats (including
  967. * MPEG-2 TS and DV) do not support frames but have a dummy frame
  968. * descriptor with bFrameIndex set to zero. If the default frame
  969. * descriptor is not found, use the first available frame.
  970. */
  971. for (i = format->nframes; i > 0; --i) {
  972. frame = &format->frame[i-1];
  973. if (frame->bFrameIndex == probe->bFrameIndex)
  974. break;
  975. }
  976. probe->bFormatIndex = format->index;
  977. probe->bFrameIndex = frame->bFrameIndex;
  978. stream->cur_format = format;
  979. stream->cur_frame = frame;
  980. /* Select the video decoding function */
  981. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  982. if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
  983. stream->decode = uvc_video_decode_isight;
  984. else if (stream->intf->num_altsetting > 1)
  985. stream->decode = uvc_video_decode_isoc;
  986. else
  987. stream->decode = uvc_video_decode_bulk;
  988. } else {
  989. if (stream->intf->num_altsetting == 1)
  990. stream->decode = uvc_video_encode_bulk;
  991. else {
  992. uvc_printk(KERN_INFO, "Isochronous endpoints are not "
  993. "supported for video output devices.\n");
  994. return -EINVAL;
  995. }
  996. }
  997. return 0;
  998. }
  999. /*
  1000. * Enable or disable the video stream.
  1001. */
  1002. int uvc_video_enable(struct uvc_streaming *stream, int enable)
  1003. {
  1004. int ret;
  1005. if (!enable) {
  1006. uvc_uninit_video(stream, 1);
  1007. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  1008. uvc_queue_enable(&stream->queue, 0);
  1009. return 0;
  1010. }
  1011. if ((stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
  1012. uvc_no_drop_param)
  1013. stream->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
  1014. else
  1015. stream->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
  1016. ret = uvc_queue_enable(&stream->queue, 1);
  1017. if (ret < 0)
  1018. return ret;
  1019. /* Commit the streaming parameters. */
  1020. ret = uvc_commit_video(stream, &stream->ctrl);
  1021. if (ret < 0)
  1022. return ret;
  1023. return uvc_init_video(stream, GFP_KERNEL);
  1024. }