uvc_video.c 34 KB

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