uvc_video.c 36 KB

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