uvc_video.c 34 KB

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