uvc_video.c 35 KB

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