uvc_video.c 35 KB

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