uvc_video.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. struct timespec ts;
  359. if (fid == stream->last_fid) {
  360. uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
  361. "sync).\n");
  362. if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
  363. (data[1] & UVC_STREAM_EOF))
  364. stream->last_fid ^= UVC_STREAM_FID;
  365. return -ENODATA;
  366. }
  367. if (uvc_clock_param == CLOCK_MONOTONIC)
  368. ktime_get_ts(&ts);
  369. else
  370. ktime_get_real_ts(&ts);
  371. buf->buf.timestamp.tv_sec = ts.tv_sec;
  372. buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  373. /* TODO: Handle PTS and SCR. */
  374. buf->state = UVC_BUF_STATE_ACTIVE;
  375. }
  376. /* Mark the buffer as done if we're at the beginning of a new frame.
  377. * End of frame detection is better implemented by checking the EOF
  378. * bit (FID bit toggling is delayed by one frame compared to the EOF
  379. * bit), but some devices don't set the bit at end of frame (and the
  380. * last payload can be lost anyway). We thus must check if the FID has
  381. * been toggled.
  382. *
  383. * stream->last_fid is initialized to -1, so the first isochronous
  384. * frame will never trigger an end of frame detection.
  385. *
  386. * Empty buffers (bytesused == 0) don't trigger end of frame detection
  387. * as it doesn't make sense to return an empty buffer. This also
  388. * avoids detecting end of frame conditions at FID toggling if the
  389. * previous payload had the EOF bit set.
  390. */
  391. if (fid != stream->last_fid && buf->buf.bytesused != 0) {
  392. uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
  393. "toggled).\n");
  394. buf->state = UVC_BUF_STATE_READY;
  395. return -EAGAIN;
  396. }
  397. stream->last_fid = fid;
  398. return data[0];
  399. }
  400. static void uvc_video_decode_data(struct uvc_streaming *stream,
  401. struct uvc_buffer *buf, const __u8 *data, int len)
  402. {
  403. struct uvc_video_queue *queue = &stream->queue;
  404. unsigned int maxlen, nbytes;
  405. void *mem;
  406. if (len <= 0)
  407. return;
  408. /* Copy the video data to the buffer. */
  409. maxlen = buf->buf.length - buf->buf.bytesused;
  410. mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
  411. nbytes = min((unsigned int)len, maxlen);
  412. memcpy(mem, data, nbytes);
  413. buf->buf.bytesused += nbytes;
  414. /* Complete the current frame if the buffer size was exceeded. */
  415. if (len > maxlen) {
  416. uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
  417. buf->state = UVC_BUF_STATE_READY;
  418. }
  419. }
  420. static void uvc_video_decode_end(struct uvc_streaming *stream,
  421. struct uvc_buffer *buf, const __u8 *data, int len)
  422. {
  423. /* Mark the buffer as done if the EOF marker is set. */
  424. if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
  425. uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
  426. if (data[0] == len)
  427. uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
  428. buf->state = UVC_BUF_STATE_READY;
  429. if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
  430. stream->last_fid ^= UVC_STREAM_FID;
  431. }
  432. }
  433. /* Video payload encoding is handled by uvc_video_encode_header() and
  434. * uvc_video_encode_data(). Only bulk transfers are currently supported.
  435. *
  436. * uvc_video_encode_header is called at the start of a payload. It adds header
  437. * data to the transfer buffer and returns the header size. As the only known
  438. * UVC output device transfers a whole frame in a single payload, the EOF bit
  439. * is always set in the header.
  440. *
  441. * uvc_video_encode_data is called for every URB and copies the data from the
  442. * video buffer to the transfer buffer.
  443. */
  444. static int uvc_video_encode_header(struct uvc_streaming *stream,
  445. struct uvc_buffer *buf, __u8 *data, int len)
  446. {
  447. data[0] = 2; /* Header length */
  448. data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
  449. | (stream->last_fid & UVC_STREAM_FID);
  450. return 2;
  451. }
  452. static int uvc_video_encode_data(struct uvc_streaming *stream,
  453. struct uvc_buffer *buf, __u8 *data, int len)
  454. {
  455. struct uvc_video_queue *queue = &stream->queue;
  456. unsigned int nbytes;
  457. void *mem;
  458. /* Copy video data to the URB buffer. */
  459. mem = queue->mem + buf->buf.m.offset + queue->buf_used;
  460. nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
  461. nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
  462. nbytes);
  463. memcpy(data, mem, nbytes);
  464. queue->buf_used += nbytes;
  465. return nbytes;
  466. }
  467. /* ------------------------------------------------------------------------
  468. * URB handling
  469. */
  470. /*
  471. * Completion handler for video URBs.
  472. */
  473. static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
  474. struct uvc_buffer *buf)
  475. {
  476. u8 *mem;
  477. int ret, i;
  478. for (i = 0; i < urb->number_of_packets; ++i) {
  479. if (urb->iso_frame_desc[i].status < 0) {
  480. uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
  481. "lost (%d).\n", urb->iso_frame_desc[i].status);
  482. continue;
  483. }
  484. /* Decode the payload header. */
  485. mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  486. do {
  487. ret = uvc_video_decode_start(stream, buf, mem,
  488. urb->iso_frame_desc[i].actual_length);
  489. if (ret == -EAGAIN)
  490. buf = uvc_queue_next_buffer(&stream->queue,
  491. buf);
  492. } while (ret == -EAGAIN);
  493. if (ret < 0)
  494. continue;
  495. /* Decode the payload data. */
  496. uvc_video_decode_data(stream, buf, mem + ret,
  497. urb->iso_frame_desc[i].actual_length - ret);
  498. /* Process the header again. */
  499. uvc_video_decode_end(stream, buf, mem,
  500. urb->iso_frame_desc[i].actual_length);
  501. if (buf->state == UVC_BUF_STATE_READY)
  502. buf = uvc_queue_next_buffer(&stream->queue, buf);
  503. }
  504. }
  505. static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
  506. struct uvc_buffer *buf)
  507. {
  508. u8 *mem;
  509. int len, ret;
  510. if (urb->actual_length == 0)
  511. return;
  512. mem = urb->transfer_buffer;
  513. len = urb->actual_length;
  514. stream->bulk.payload_size += len;
  515. /* If the URB is the first of its payload, decode and save the
  516. * header.
  517. */
  518. if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
  519. do {
  520. ret = uvc_video_decode_start(stream, buf, mem, len);
  521. if (ret == -EAGAIN)
  522. buf = uvc_queue_next_buffer(&stream->queue,
  523. buf);
  524. } while (ret == -EAGAIN);
  525. /* If an error occured skip the rest of the payload. */
  526. if (ret < 0 || buf == NULL) {
  527. stream->bulk.skip_payload = 1;
  528. } else {
  529. memcpy(stream->bulk.header, mem, ret);
  530. stream->bulk.header_size = ret;
  531. mem += ret;
  532. len -= ret;
  533. }
  534. }
  535. /* The buffer queue might have been cancelled while a bulk transfer
  536. * was in progress, so we can reach here with buf equal to NULL. Make
  537. * sure buf is never dereferenced if NULL.
  538. */
  539. /* Process video data. */
  540. if (!stream->bulk.skip_payload && buf != NULL)
  541. uvc_video_decode_data(stream, buf, mem, len);
  542. /* Detect the payload end by a URB smaller than the maximum size (or
  543. * a payload size equal to the maximum) and process the header again.
  544. */
  545. if (urb->actual_length < urb->transfer_buffer_length ||
  546. stream->bulk.payload_size >= stream->bulk.max_payload_size) {
  547. if (!stream->bulk.skip_payload && buf != NULL) {
  548. uvc_video_decode_end(stream, buf, stream->bulk.header,
  549. stream->bulk.payload_size);
  550. if (buf->state == UVC_BUF_STATE_READY)
  551. buf = uvc_queue_next_buffer(&stream->queue,
  552. buf);
  553. }
  554. stream->bulk.header_size = 0;
  555. stream->bulk.skip_payload = 0;
  556. stream->bulk.payload_size = 0;
  557. }
  558. }
  559. static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
  560. struct uvc_buffer *buf)
  561. {
  562. u8 *mem = urb->transfer_buffer;
  563. int len = stream->urb_size, ret;
  564. if (buf == NULL) {
  565. urb->transfer_buffer_length = 0;
  566. return;
  567. }
  568. /* If the URB is the first of its payload, add the header. */
  569. if (stream->bulk.header_size == 0) {
  570. ret = uvc_video_encode_header(stream, buf, mem, len);
  571. stream->bulk.header_size = ret;
  572. stream->bulk.payload_size += ret;
  573. mem += ret;
  574. len -= ret;
  575. }
  576. /* Process video data. */
  577. ret = uvc_video_encode_data(stream, buf, mem, len);
  578. stream->bulk.payload_size += ret;
  579. len -= ret;
  580. if (buf->buf.bytesused == stream->queue.buf_used ||
  581. stream->bulk.payload_size == stream->bulk.max_payload_size) {
  582. if (buf->buf.bytesused == stream->queue.buf_used) {
  583. stream->queue.buf_used = 0;
  584. buf->state = UVC_BUF_STATE_READY;
  585. uvc_queue_next_buffer(&stream->queue, buf);
  586. stream->last_fid ^= UVC_STREAM_FID;
  587. }
  588. stream->bulk.header_size = 0;
  589. stream->bulk.payload_size = 0;
  590. }
  591. urb->transfer_buffer_length = stream->urb_size - len;
  592. }
  593. static void uvc_video_complete(struct urb *urb)
  594. {
  595. struct uvc_streaming *stream = urb->context;
  596. struct uvc_video_queue *queue = &stream->queue;
  597. struct uvc_buffer *buf = NULL;
  598. unsigned long flags;
  599. int ret;
  600. switch (urb->status) {
  601. case 0:
  602. break;
  603. default:
  604. uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
  605. "completion handler.\n", urb->status);
  606. case -ENOENT: /* usb_kill_urb() called. */
  607. if (stream->frozen)
  608. return;
  609. case -ECONNRESET: /* usb_unlink_urb() called. */
  610. case -ESHUTDOWN: /* The endpoint is being disabled. */
  611. uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
  612. return;
  613. }
  614. spin_lock_irqsave(&queue->irqlock, flags);
  615. if (!list_empty(&queue->irqqueue))
  616. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  617. queue);
  618. spin_unlock_irqrestore(&queue->irqlock, flags);
  619. stream->decode(urb, stream, buf);
  620. if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  621. uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
  622. ret);
  623. }
  624. }
  625. /*
  626. * Free transfer buffers.
  627. */
  628. static void uvc_free_urb_buffers(struct uvc_streaming *stream)
  629. {
  630. unsigned int i;
  631. for (i = 0; i < UVC_URBS; ++i) {
  632. if (stream->urb_buffer[i]) {
  633. usb_buffer_free(stream->dev->udev, stream->urb_size,
  634. stream->urb_buffer[i], stream->urb_dma[i]);
  635. stream->urb_buffer[i] = NULL;
  636. }
  637. }
  638. stream->urb_size = 0;
  639. }
  640. /*
  641. * Allocate transfer buffers. This function can be called with buffers
  642. * already allocated when resuming from suspend, in which case it will
  643. * return without touching the buffers.
  644. *
  645. * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
  646. * system is too low on memory try successively smaller numbers of packets
  647. * until allocation succeeds.
  648. *
  649. * Return the number of allocated packets on success or 0 when out of memory.
  650. */
  651. static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
  652. unsigned int size, unsigned int psize, gfp_t gfp_flags)
  653. {
  654. unsigned int npackets;
  655. unsigned int i;
  656. /* Buffers are already allocated, bail out. */
  657. if (stream->urb_size)
  658. return stream->urb_size / psize;
  659. /* Compute the number of packets. Bulk endpoints might transfer UVC
  660. * payloads accross multiple URBs.
  661. */
  662. npackets = DIV_ROUND_UP(size, psize);
  663. if (npackets > UVC_MAX_PACKETS)
  664. npackets = UVC_MAX_PACKETS;
  665. /* Retry allocations until one succeed. */
  666. for (; npackets > 1; npackets /= 2) {
  667. for (i = 0; i < UVC_URBS; ++i) {
  668. stream->urb_size = psize * npackets;
  669. stream->urb_buffer[i] = usb_buffer_alloc(
  670. stream->dev->udev, stream->urb_size,
  671. gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
  672. if (!stream->urb_buffer[i]) {
  673. uvc_free_urb_buffers(stream);
  674. break;
  675. }
  676. }
  677. if (i == UVC_URBS) {
  678. uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
  679. "of %ux%u bytes each.\n", UVC_URBS, npackets,
  680. psize);
  681. return npackets;
  682. }
  683. }
  684. uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
  685. "per packet).\n", psize);
  686. return 0;
  687. }
  688. /*
  689. * Uninitialize isochronous/bulk URBs and free transfer buffers.
  690. */
  691. static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
  692. {
  693. struct urb *urb;
  694. unsigned int i;
  695. for (i = 0; i < UVC_URBS; ++i) {
  696. urb = stream->urb[i];
  697. if (urb == NULL)
  698. continue;
  699. usb_kill_urb(urb);
  700. usb_free_urb(urb);
  701. stream->urb[i] = NULL;
  702. }
  703. if (free_buffers)
  704. uvc_free_urb_buffers(stream);
  705. }
  706. /*
  707. * Initialize isochronous URBs and allocate transfer buffers. The packet size
  708. * is given by the endpoint.
  709. */
  710. static int uvc_init_video_isoc(struct uvc_streaming *stream,
  711. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  712. {
  713. struct urb *urb;
  714. unsigned int npackets, i, j;
  715. u16 psize;
  716. u32 size;
  717. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  718. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  719. size = stream->ctrl.dwMaxVideoFrameSize;
  720. npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
  721. if (npackets == 0)
  722. return -ENOMEM;
  723. size = npackets * psize;
  724. for (i = 0; i < UVC_URBS; ++i) {
  725. urb = usb_alloc_urb(npackets, gfp_flags);
  726. if (urb == NULL) {
  727. uvc_uninit_video(stream, 1);
  728. return -ENOMEM;
  729. }
  730. urb->dev = stream->dev->udev;
  731. urb->context = stream;
  732. urb->pipe = usb_rcvisocpipe(stream->dev->udev,
  733. ep->desc.bEndpointAddress);
  734. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  735. urb->interval = ep->desc.bInterval;
  736. urb->transfer_buffer = stream->urb_buffer[i];
  737. urb->transfer_dma = stream->urb_dma[i];
  738. urb->complete = uvc_video_complete;
  739. urb->number_of_packets = npackets;
  740. urb->transfer_buffer_length = size;
  741. for (j = 0; j < npackets; ++j) {
  742. urb->iso_frame_desc[j].offset = j * psize;
  743. urb->iso_frame_desc[j].length = psize;
  744. }
  745. stream->urb[i] = urb;
  746. }
  747. return 0;
  748. }
  749. /*
  750. * Initialize bulk URBs and allocate transfer buffers. The packet size is
  751. * given by the endpoint.
  752. */
  753. static int uvc_init_video_bulk(struct uvc_streaming *stream,
  754. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  755. {
  756. struct urb *urb;
  757. unsigned int npackets, pipe, i;
  758. u16 psize;
  759. u32 size;
  760. psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
  761. size = stream->ctrl.dwMaxPayloadTransferSize;
  762. stream->bulk.max_payload_size = size;
  763. npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
  764. if (npackets == 0)
  765. return -ENOMEM;
  766. size = npackets * psize;
  767. if (usb_endpoint_dir_in(&ep->desc))
  768. pipe = usb_rcvbulkpipe(stream->dev->udev,
  769. ep->desc.bEndpointAddress);
  770. else
  771. pipe = usb_sndbulkpipe(stream->dev->udev,
  772. ep->desc.bEndpointAddress);
  773. if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  774. size = 0;
  775. for (i = 0; i < UVC_URBS; ++i) {
  776. urb = usb_alloc_urb(0, gfp_flags);
  777. if (urb == NULL) {
  778. uvc_uninit_video(stream, 1);
  779. return -ENOMEM;
  780. }
  781. usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
  782. stream->urb_buffer[i], size, uvc_video_complete,
  783. stream);
  784. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  785. urb->transfer_dma = stream->urb_dma[i];
  786. stream->urb[i] = urb;
  787. }
  788. return 0;
  789. }
  790. /*
  791. * Initialize isochronous/bulk URBs and allocate transfer buffers.
  792. */
  793. static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
  794. {
  795. struct usb_interface *intf = stream->intf;
  796. struct usb_host_endpoint *ep;
  797. unsigned int i;
  798. int ret;
  799. stream->last_fid = -1;
  800. stream->bulk.header_size = 0;
  801. stream->bulk.skip_payload = 0;
  802. stream->bulk.payload_size = 0;
  803. if (intf->num_altsetting > 1) {
  804. struct usb_host_endpoint *best_ep = NULL;
  805. unsigned int best_psize = 3 * 1024;
  806. unsigned int bandwidth;
  807. unsigned int uninitialized_var(altsetting);
  808. int intfnum = stream->intfnum;
  809. /* Isochronous endpoint, select the alternate setting. */
  810. bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
  811. if (bandwidth == 0) {
  812. uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
  813. "bandwidth, defaulting to lowest.\n");
  814. bandwidth = 1;
  815. } else {
  816. uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
  817. "B/frame bandwidth.\n", bandwidth);
  818. }
  819. for (i = 0; i < intf->num_altsetting; ++i) {
  820. struct usb_host_interface *alts;
  821. unsigned int psize;
  822. alts = &intf->altsetting[i];
  823. ep = uvc_find_endpoint(alts,
  824. stream->header.bEndpointAddress);
  825. if (ep == NULL)
  826. continue;
  827. /* Check if the bandwidth is high enough. */
  828. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  829. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  830. if (psize >= bandwidth && psize <= best_psize) {
  831. altsetting = i;
  832. best_psize = psize;
  833. best_ep = ep;
  834. }
  835. }
  836. if (best_ep == NULL) {
  837. uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
  838. "for requested bandwidth.\n");
  839. return -EIO;
  840. }
  841. uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
  842. "(%u B/frame bandwidth).\n", altsetting, best_psize);
  843. ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
  844. if (ret < 0)
  845. return ret;
  846. ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
  847. } else {
  848. /* Bulk endpoint, proceed to URB initialization. */
  849. ep = uvc_find_endpoint(&intf->altsetting[0],
  850. stream->header.bEndpointAddress);
  851. if (ep == NULL)
  852. return -EIO;
  853. ret = uvc_init_video_bulk(stream, ep, gfp_flags);
  854. }
  855. if (ret < 0)
  856. return ret;
  857. /* Submit the URBs. */
  858. for (i = 0; i < UVC_URBS; ++i) {
  859. ret = usb_submit_urb(stream->urb[i], gfp_flags);
  860. if (ret < 0) {
  861. uvc_printk(KERN_ERR, "Failed to submit URB %u "
  862. "(%d).\n", i, ret);
  863. uvc_uninit_video(stream, 1);
  864. return ret;
  865. }
  866. }
  867. return 0;
  868. }
  869. /* --------------------------------------------------------------------------
  870. * Suspend/resume
  871. */
  872. /*
  873. * Stop streaming without disabling the video queue.
  874. *
  875. * To let userspace applications resume without trouble, we must not touch the
  876. * video buffers in any way. We mark the device as frozen to make sure the URB
  877. * completion handler won't try to cancel the queue when we kill the URBs.
  878. */
  879. int uvc_video_suspend(struct uvc_streaming *stream)
  880. {
  881. if (!uvc_queue_streaming(&stream->queue))
  882. return 0;
  883. stream->frozen = 1;
  884. uvc_uninit_video(stream, 0);
  885. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  886. return 0;
  887. }
  888. /*
  889. * Reconfigure the video interface and restart streaming if it was enabled
  890. * before suspend.
  891. *
  892. * If an error occurs, disable the video queue. This will wake all pending
  893. * buffers, making sure userspace applications are notified of the problem
  894. * instead of waiting forever.
  895. */
  896. int uvc_video_resume(struct uvc_streaming *stream)
  897. {
  898. int ret;
  899. stream->frozen = 0;
  900. ret = uvc_commit_video(stream, &stream->ctrl);
  901. if (ret < 0) {
  902. uvc_queue_enable(&stream->queue, 0);
  903. return ret;
  904. }
  905. if (!uvc_queue_streaming(&stream->queue))
  906. return 0;
  907. ret = uvc_init_video(stream, GFP_NOIO);
  908. if (ret < 0)
  909. uvc_queue_enable(&stream->queue, 0);
  910. return ret;
  911. }
  912. /* ------------------------------------------------------------------------
  913. * Video device
  914. */
  915. /*
  916. * Initialize the UVC video device by switching to alternate setting 0 and
  917. * retrieve the default format.
  918. *
  919. * Some cameras (namely the Fuji Finepix) set the format and frame
  920. * indexes to zero. The UVC standard doesn't clearly make this a spec
  921. * violation, so try to silently fix the values if possible.
  922. *
  923. * This function is called before registering the device with V4L.
  924. */
  925. int uvc_video_init(struct uvc_streaming *stream)
  926. {
  927. struct uvc_streaming_control *probe = &stream->ctrl;
  928. struct uvc_format *format = NULL;
  929. struct uvc_frame *frame = NULL;
  930. unsigned int i;
  931. int ret;
  932. if (stream->nformats == 0) {
  933. uvc_printk(KERN_INFO, "No supported video formats found.\n");
  934. return -EINVAL;
  935. }
  936. atomic_set(&stream->active, 0);
  937. /* Initialize the video buffers queue. */
  938. uvc_queue_init(&stream->queue, stream->type);
  939. /* Alternate setting 0 should be the default, yet the XBox Live Vision
  940. * Cam (and possibly other devices) crash or otherwise misbehave if
  941. * they don't receive a SET_INTERFACE request before any other video
  942. * control request.
  943. */
  944. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  945. /* Set the streaming probe control with default streaming parameters
  946. * retrieved from the device. Webcams that don't suport GET_DEF
  947. * requests on the probe control will just keep their current streaming
  948. * parameters.
  949. */
  950. if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
  951. uvc_set_video_ctrl(stream, probe, 1);
  952. /* Initialize the streaming parameters with the probe control current
  953. * value. This makes sure SET_CUR requests on the streaming commit
  954. * control will always use values retrieved from a successful GET_CUR
  955. * request on the probe control, as required by the UVC specification.
  956. */
  957. ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
  958. if (ret < 0)
  959. return ret;
  960. /* Check if the default format descriptor exists. Use the first
  961. * available format otherwise.
  962. */
  963. for (i = stream->nformats; i > 0; --i) {
  964. format = &stream->format[i-1];
  965. if (format->index == probe->bFormatIndex)
  966. break;
  967. }
  968. if (format->nframes == 0) {
  969. uvc_printk(KERN_INFO, "No frame descriptor found for the "
  970. "default format.\n");
  971. return -EINVAL;
  972. }
  973. /* Zero bFrameIndex might be correct. Stream-based formats (including
  974. * MPEG-2 TS and DV) do not support frames but have a dummy frame
  975. * descriptor with bFrameIndex set to zero. If the default frame
  976. * descriptor is not found, use the first available frame.
  977. */
  978. for (i = format->nframes; i > 0; --i) {
  979. frame = &format->frame[i-1];
  980. if (frame->bFrameIndex == probe->bFrameIndex)
  981. break;
  982. }
  983. probe->bFormatIndex = format->index;
  984. probe->bFrameIndex = frame->bFrameIndex;
  985. stream->cur_format = format;
  986. stream->cur_frame = frame;
  987. /* Select the video decoding function */
  988. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  989. if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
  990. stream->decode = uvc_video_decode_isight;
  991. else if (stream->intf->num_altsetting > 1)
  992. stream->decode = uvc_video_decode_isoc;
  993. else
  994. stream->decode = uvc_video_decode_bulk;
  995. } else {
  996. if (stream->intf->num_altsetting == 1)
  997. stream->decode = uvc_video_encode_bulk;
  998. else {
  999. uvc_printk(KERN_INFO, "Isochronous endpoints are not "
  1000. "supported for video output devices.\n");
  1001. return -EINVAL;
  1002. }
  1003. }
  1004. return 0;
  1005. }
  1006. /*
  1007. * Enable or disable the video stream.
  1008. */
  1009. int uvc_video_enable(struct uvc_streaming *stream, int enable)
  1010. {
  1011. int ret;
  1012. if (!enable) {
  1013. uvc_uninit_video(stream, 1);
  1014. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  1015. uvc_queue_enable(&stream->queue, 0);
  1016. return 0;
  1017. }
  1018. if ((stream->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
  1019. uvc_no_drop_param)
  1020. stream->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
  1021. else
  1022. stream->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
  1023. ret = uvc_queue_enable(&stream->queue, 1);
  1024. if (ret < 0)
  1025. return ret;
  1026. /* Commit the streaming parameters. */
  1027. ret = uvc_commit_video(stream, &stream->ctrl);
  1028. if (ret < 0)
  1029. return ret;
  1030. return uvc_init_video(stream, GFP_KERNEL);
  1031. }