uvc_video.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  315. #define UVC_STREAM_EOH (1 << 7)
  316. #define UVC_STREAM_ERR (1 << 6)
  317. #define UVC_STREAM_STI (1 << 5)
  318. #define UVC_STREAM_RES (1 << 4)
  319. #define UVC_STREAM_SCR (1 << 3)
  320. #define UVC_STREAM_PTS (1 << 2)
  321. #define UVC_STREAM_EOF (1 << 1)
  322. #define UVC_STREAM_FID (1 << 0)
  323. /* Video payload decoding is handled by uvc_video_decode_start(),
  324. * uvc_video_decode_data() and uvc_video_decode_end().
  325. *
  326. * uvc_video_decode_start is called with URB data at the start of a bulk or
  327. * isochronous payload. It processes header data and returns the header size
  328. * in bytes if successful. If an error occurs, it returns a negative error
  329. * code. The following error codes have special meanings.
  330. *
  331. * - EAGAIN informs the caller that the current video buffer should be marked
  332. * as done, and that the function should be called again with the same data
  333. * and a new video buffer. This is used when end of frame conditions can be
  334. * reliably detected at the beginning of the next frame only.
  335. *
  336. * If an error other than -EAGAIN is returned, the caller will drop the current
  337. * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
  338. * made until the next payload. -ENODATA can be used to drop the current
  339. * payload if no other error code is appropriate.
  340. *
  341. * uvc_video_decode_data is called for every URB with URB data. It copies the
  342. * data to the video buffer.
  343. *
  344. * uvc_video_decode_end is called with header data at the end of a bulk or
  345. * isochronous payload. It performs any additional header data processing and
  346. * returns 0 or a negative error code if an error occurred. As header data have
  347. * already been processed by uvc_video_decode_start, this functions isn't
  348. * required to perform sanity checks a second time.
  349. *
  350. * For isochronous transfers where a payload is always transferred in a single
  351. * URB, the three functions will be called in a row.
  352. *
  353. * To let the decoder process header data and update its internal state even
  354. * when no video buffer is available, uvc_video_decode_start must be prepared
  355. * to be called with a NULL buf parameter. uvc_video_decode_data and
  356. * uvc_video_decode_end will never be called with a NULL buffer.
  357. */
  358. static int uvc_video_decode_start(struct uvc_streaming *stream,
  359. struct uvc_buffer *buf, const __u8 *data, int len)
  360. {
  361. __u8 fid;
  362. /* Sanity checks:
  363. * - packet must be at least 2 bytes long
  364. * - bHeaderLength value must be at least 2 bytes (see above)
  365. * - bHeaderLength value can't be larger than the packet size.
  366. */
  367. if (len < 2 || data[0] < 2 || data[0] > len)
  368. return -EINVAL;
  369. /* Skip payloads marked with the error bit ("error frames"). */
  370. if (data[1] & UVC_STREAM_ERR) {
  371. uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
  372. "set).\n");
  373. return -ENODATA;
  374. }
  375. fid = data[1] & UVC_STREAM_FID;
  376. /* Increase the sequence number regardless of any buffer states, so
  377. * that discontinuous sequence numbers always indicate lost frames.
  378. */
  379. if (stream->last_fid != fid)
  380. stream->sequence++;
  381. /* Store the payload FID bit and return immediately when the buffer is
  382. * NULL.
  383. */
  384. if (buf == NULL) {
  385. stream->last_fid = fid;
  386. return -ENODATA;
  387. }
  388. /* Synchronize to the input stream by waiting for the FID bit to be
  389. * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
  390. * stream->last_fid is initialized to -1, so the first isochronous
  391. * frame will always be in sync.
  392. *
  393. * If the device doesn't toggle the FID bit, invert stream->last_fid
  394. * when the EOF bit is set to force synchronisation on the next packet.
  395. */
  396. if (buf->state != UVC_BUF_STATE_ACTIVE) {
  397. struct timespec ts;
  398. if (fid == stream->last_fid) {
  399. uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
  400. "sync).\n");
  401. if ((stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
  402. (data[1] & UVC_STREAM_EOF))
  403. stream->last_fid ^= UVC_STREAM_FID;
  404. return -ENODATA;
  405. }
  406. if (uvc_clock_param == CLOCK_MONOTONIC)
  407. ktime_get_ts(&ts);
  408. else
  409. ktime_get_real_ts(&ts);
  410. buf->buf.sequence = stream->sequence;
  411. buf->buf.timestamp.tv_sec = ts.tv_sec;
  412. buf->buf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  413. /* TODO: Handle PTS and SCR. */
  414. buf->state = UVC_BUF_STATE_ACTIVE;
  415. }
  416. /* Mark the buffer as done if we're at the beginning of a new frame.
  417. * End of frame detection is better implemented by checking the EOF
  418. * bit (FID bit toggling is delayed by one frame compared to the EOF
  419. * bit), but some devices don't set the bit at end of frame (and the
  420. * last payload can be lost anyway). We thus must check if the FID has
  421. * been toggled.
  422. *
  423. * stream->last_fid is initialized to -1, so the first isochronous
  424. * frame will never trigger an end of frame detection.
  425. *
  426. * Empty buffers (bytesused == 0) don't trigger end of frame detection
  427. * as it doesn't make sense to return an empty buffer. This also
  428. * avoids detecting end of frame conditions at FID toggling if the
  429. * previous payload had the EOF bit set.
  430. */
  431. if (fid != stream->last_fid && buf->buf.bytesused != 0) {
  432. uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
  433. "toggled).\n");
  434. buf->state = UVC_BUF_STATE_READY;
  435. return -EAGAIN;
  436. }
  437. stream->last_fid = fid;
  438. return data[0];
  439. }
  440. static void uvc_video_decode_data(struct uvc_streaming *stream,
  441. struct uvc_buffer *buf, const __u8 *data, int len)
  442. {
  443. struct uvc_video_queue *queue = &stream->queue;
  444. unsigned int maxlen, nbytes;
  445. void *mem;
  446. if (len <= 0)
  447. return;
  448. /* Copy the video data to the buffer. */
  449. maxlen = buf->buf.length - buf->buf.bytesused;
  450. mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
  451. nbytes = min((unsigned int)len, maxlen);
  452. memcpy(mem, data, nbytes);
  453. buf->buf.bytesused += nbytes;
  454. /* Complete the current frame if the buffer size was exceeded. */
  455. if (len > maxlen) {
  456. uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
  457. buf->state = UVC_BUF_STATE_READY;
  458. }
  459. }
  460. static void uvc_video_decode_end(struct uvc_streaming *stream,
  461. struct uvc_buffer *buf, const __u8 *data, int len)
  462. {
  463. /* Mark the buffer as done if the EOF marker is set. */
  464. if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
  465. uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
  466. if (data[0] == len)
  467. uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
  468. buf->state = UVC_BUF_STATE_READY;
  469. if (stream->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
  470. stream->last_fid ^= UVC_STREAM_FID;
  471. }
  472. }
  473. /* Video payload encoding is handled by uvc_video_encode_header() and
  474. * uvc_video_encode_data(). Only bulk transfers are currently supported.
  475. *
  476. * uvc_video_encode_header is called at the start of a payload. It adds header
  477. * data to the transfer buffer and returns the header size. As the only known
  478. * UVC output device transfers a whole frame in a single payload, the EOF bit
  479. * is always set in the header.
  480. *
  481. * uvc_video_encode_data is called for every URB and copies the data from the
  482. * video buffer to the transfer buffer.
  483. */
  484. static int uvc_video_encode_header(struct uvc_streaming *stream,
  485. struct uvc_buffer *buf, __u8 *data, int len)
  486. {
  487. data[0] = 2; /* Header length */
  488. data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
  489. | (stream->last_fid & UVC_STREAM_FID);
  490. return 2;
  491. }
  492. static int uvc_video_encode_data(struct uvc_streaming *stream,
  493. struct uvc_buffer *buf, __u8 *data, int len)
  494. {
  495. struct uvc_video_queue *queue = &stream->queue;
  496. unsigned int nbytes;
  497. void *mem;
  498. /* Copy video data to the URB buffer. */
  499. mem = queue->mem + buf->buf.m.offset + queue->buf_used;
  500. nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
  501. nbytes = min(stream->bulk.max_payload_size - stream->bulk.payload_size,
  502. nbytes);
  503. memcpy(data, mem, nbytes);
  504. queue->buf_used += nbytes;
  505. return nbytes;
  506. }
  507. /* ------------------------------------------------------------------------
  508. * URB handling
  509. */
  510. /*
  511. * Completion handler for video URBs.
  512. */
  513. static void uvc_video_decode_isoc(struct urb *urb, struct uvc_streaming *stream,
  514. struct uvc_buffer *buf)
  515. {
  516. u8 *mem;
  517. int ret, i;
  518. for (i = 0; i < urb->number_of_packets; ++i) {
  519. if (urb->iso_frame_desc[i].status < 0) {
  520. uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
  521. "lost (%d).\n", urb->iso_frame_desc[i].status);
  522. /* Mark the buffer as faulty. */
  523. if (buf != NULL)
  524. buf->error = 1;
  525. continue;
  526. }
  527. /* Decode the payload header. */
  528. mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  529. do {
  530. ret = uvc_video_decode_start(stream, buf, mem,
  531. urb->iso_frame_desc[i].actual_length);
  532. if (ret == -EAGAIN)
  533. buf = uvc_queue_next_buffer(&stream->queue,
  534. buf);
  535. } while (ret == -EAGAIN);
  536. if (ret < 0)
  537. continue;
  538. /* Decode the payload data. */
  539. uvc_video_decode_data(stream, buf, mem + ret,
  540. urb->iso_frame_desc[i].actual_length - ret);
  541. /* Process the header again. */
  542. uvc_video_decode_end(stream, buf, mem,
  543. urb->iso_frame_desc[i].actual_length);
  544. if (buf->state == UVC_BUF_STATE_READY) {
  545. if (buf->buf.length != buf->buf.bytesused &&
  546. !(stream->cur_format->flags &
  547. UVC_FMT_FLAG_COMPRESSED))
  548. buf->error = 1;
  549. buf = uvc_queue_next_buffer(&stream->queue, buf);
  550. }
  551. }
  552. }
  553. static void uvc_video_decode_bulk(struct urb *urb, struct uvc_streaming *stream,
  554. struct uvc_buffer *buf)
  555. {
  556. u8 *mem;
  557. int len, ret;
  558. if (urb->actual_length == 0)
  559. return;
  560. mem = urb->transfer_buffer;
  561. len = urb->actual_length;
  562. stream->bulk.payload_size += len;
  563. /* If the URB is the first of its payload, decode and save the
  564. * header.
  565. */
  566. if (stream->bulk.header_size == 0 && !stream->bulk.skip_payload) {
  567. do {
  568. ret = uvc_video_decode_start(stream, buf, mem, len);
  569. if (ret == -EAGAIN)
  570. buf = uvc_queue_next_buffer(&stream->queue,
  571. buf);
  572. } while (ret == -EAGAIN);
  573. /* If an error occurred skip the rest of the payload. */
  574. if (ret < 0 || buf == NULL) {
  575. stream->bulk.skip_payload = 1;
  576. } else {
  577. memcpy(stream->bulk.header, mem, ret);
  578. stream->bulk.header_size = ret;
  579. mem += ret;
  580. len -= ret;
  581. }
  582. }
  583. /* The buffer queue might have been cancelled while a bulk transfer
  584. * was in progress, so we can reach here with buf equal to NULL. Make
  585. * sure buf is never dereferenced if NULL.
  586. */
  587. /* Process video data. */
  588. if (!stream->bulk.skip_payload && buf != NULL)
  589. uvc_video_decode_data(stream, buf, mem, len);
  590. /* Detect the payload end by a URB smaller than the maximum size (or
  591. * a payload size equal to the maximum) and process the header again.
  592. */
  593. if (urb->actual_length < urb->transfer_buffer_length ||
  594. stream->bulk.payload_size >= stream->bulk.max_payload_size) {
  595. if (!stream->bulk.skip_payload && buf != NULL) {
  596. uvc_video_decode_end(stream, buf, stream->bulk.header,
  597. stream->bulk.payload_size);
  598. if (buf->state == UVC_BUF_STATE_READY)
  599. buf = uvc_queue_next_buffer(&stream->queue,
  600. buf);
  601. }
  602. stream->bulk.header_size = 0;
  603. stream->bulk.skip_payload = 0;
  604. stream->bulk.payload_size = 0;
  605. }
  606. }
  607. static void uvc_video_encode_bulk(struct urb *urb, struct uvc_streaming *stream,
  608. struct uvc_buffer *buf)
  609. {
  610. u8 *mem = urb->transfer_buffer;
  611. int len = stream->urb_size, ret;
  612. if (buf == NULL) {
  613. urb->transfer_buffer_length = 0;
  614. return;
  615. }
  616. /* If the URB is the first of its payload, add the header. */
  617. if (stream->bulk.header_size == 0) {
  618. ret = uvc_video_encode_header(stream, buf, mem, len);
  619. stream->bulk.header_size = ret;
  620. stream->bulk.payload_size += ret;
  621. mem += ret;
  622. len -= ret;
  623. }
  624. /* Process video data. */
  625. ret = uvc_video_encode_data(stream, buf, mem, len);
  626. stream->bulk.payload_size += ret;
  627. len -= ret;
  628. if (buf->buf.bytesused == stream->queue.buf_used ||
  629. stream->bulk.payload_size == stream->bulk.max_payload_size) {
  630. if (buf->buf.bytesused == stream->queue.buf_used) {
  631. stream->queue.buf_used = 0;
  632. buf->state = UVC_BUF_STATE_READY;
  633. buf->buf.sequence = ++stream->sequence;
  634. uvc_queue_next_buffer(&stream->queue, buf);
  635. stream->last_fid ^= UVC_STREAM_FID;
  636. }
  637. stream->bulk.header_size = 0;
  638. stream->bulk.payload_size = 0;
  639. }
  640. urb->transfer_buffer_length = stream->urb_size - len;
  641. }
  642. static void uvc_video_complete(struct urb *urb)
  643. {
  644. struct uvc_streaming *stream = urb->context;
  645. struct uvc_video_queue *queue = &stream->queue;
  646. struct uvc_buffer *buf = NULL;
  647. unsigned long flags;
  648. int ret;
  649. switch (urb->status) {
  650. case 0:
  651. break;
  652. default:
  653. uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
  654. "completion handler.\n", urb->status);
  655. case -ENOENT: /* usb_kill_urb() called. */
  656. if (stream->frozen)
  657. return;
  658. case -ECONNRESET: /* usb_unlink_urb() called. */
  659. case -ESHUTDOWN: /* The endpoint is being disabled. */
  660. uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
  661. return;
  662. }
  663. spin_lock_irqsave(&queue->irqlock, flags);
  664. if (!list_empty(&queue->irqqueue))
  665. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  666. queue);
  667. spin_unlock_irqrestore(&queue->irqlock, flags);
  668. stream->decode(urb, stream, buf);
  669. if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  670. uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
  671. ret);
  672. }
  673. }
  674. /*
  675. * Free transfer buffers.
  676. */
  677. static void uvc_free_urb_buffers(struct uvc_streaming *stream)
  678. {
  679. unsigned int i;
  680. for (i = 0; i < UVC_URBS; ++i) {
  681. if (stream->urb_buffer[i]) {
  682. #ifndef CONFIG_DMA_NONCOHERENT
  683. usb_free_coherent(stream->dev->udev, stream->urb_size,
  684. stream->urb_buffer[i], stream->urb_dma[i]);
  685. #else
  686. kfree(stream->urb_buffer[i]);
  687. #endif
  688. stream->urb_buffer[i] = NULL;
  689. }
  690. }
  691. stream->urb_size = 0;
  692. }
  693. /*
  694. * Allocate transfer buffers. This function can be called with buffers
  695. * already allocated when resuming from suspend, in which case it will
  696. * return without touching the buffers.
  697. *
  698. * Limit the buffer size to UVC_MAX_PACKETS bulk/isochronous packets. If the
  699. * system is too low on memory try successively smaller numbers of packets
  700. * until allocation succeeds.
  701. *
  702. * Return the number of allocated packets on success or 0 when out of memory.
  703. */
  704. static int uvc_alloc_urb_buffers(struct uvc_streaming *stream,
  705. unsigned int size, unsigned int psize, gfp_t gfp_flags)
  706. {
  707. unsigned int npackets;
  708. unsigned int i;
  709. /* Buffers are already allocated, bail out. */
  710. if (stream->urb_size)
  711. return stream->urb_size / psize;
  712. /* Compute the number of packets. Bulk endpoints might transfer UVC
  713. * payloads across multiple URBs.
  714. */
  715. npackets = DIV_ROUND_UP(size, psize);
  716. if (npackets > UVC_MAX_PACKETS)
  717. npackets = UVC_MAX_PACKETS;
  718. /* Retry allocations until one succeed. */
  719. for (; npackets > 1; npackets /= 2) {
  720. for (i = 0; i < UVC_URBS; ++i) {
  721. stream->urb_size = psize * npackets;
  722. #ifndef CONFIG_DMA_NONCOHERENT
  723. stream->urb_buffer[i] = usb_alloc_coherent(
  724. stream->dev->udev, stream->urb_size,
  725. gfp_flags | __GFP_NOWARN, &stream->urb_dma[i]);
  726. #else
  727. stream->urb_buffer[i] =
  728. kmalloc(stream->urb_size, gfp_flags | __GFP_NOWARN);
  729. #endif
  730. if (!stream->urb_buffer[i]) {
  731. uvc_free_urb_buffers(stream);
  732. break;
  733. }
  734. }
  735. if (i == UVC_URBS) {
  736. uvc_trace(UVC_TRACE_VIDEO, "Allocated %u URB buffers "
  737. "of %ux%u bytes each.\n", UVC_URBS, npackets,
  738. psize);
  739. return npackets;
  740. }
  741. }
  742. uvc_trace(UVC_TRACE_VIDEO, "Failed to allocate URB buffers (%u bytes "
  743. "per packet).\n", psize);
  744. return 0;
  745. }
  746. /*
  747. * Uninitialize isochronous/bulk URBs and free transfer buffers.
  748. */
  749. static void uvc_uninit_video(struct uvc_streaming *stream, int free_buffers)
  750. {
  751. struct urb *urb;
  752. unsigned int i;
  753. for (i = 0; i < UVC_URBS; ++i) {
  754. urb = stream->urb[i];
  755. if (urb == NULL)
  756. continue;
  757. usb_kill_urb(urb);
  758. usb_free_urb(urb);
  759. stream->urb[i] = NULL;
  760. }
  761. if (free_buffers)
  762. uvc_free_urb_buffers(stream);
  763. }
  764. /*
  765. * Initialize isochronous URBs and allocate transfer buffers. The packet size
  766. * is given by the endpoint.
  767. */
  768. static int uvc_init_video_isoc(struct uvc_streaming *stream,
  769. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  770. {
  771. struct urb *urb;
  772. unsigned int npackets, i, j;
  773. u16 psize;
  774. u32 size;
  775. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  776. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  777. size = stream->ctrl.dwMaxVideoFrameSize;
  778. npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
  779. if (npackets == 0)
  780. return -ENOMEM;
  781. size = npackets * psize;
  782. for (i = 0; i < UVC_URBS; ++i) {
  783. urb = usb_alloc_urb(npackets, gfp_flags);
  784. if (urb == NULL) {
  785. uvc_uninit_video(stream, 1);
  786. return -ENOMEM;
  787. }
  788. urb->dev = stream->dev->udev;
  789. urb->context = stream;
  790. urb->pipe = usb_rcvisocpipe(stream->dev->udev,
  791. ep->desc.bEndpointAddress);
  792. #ifndef CONFIG_DMA_NONCOHERENT
  793. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  794. urb->transfer_dma = stream->urb_dma[i];
  795. #else
  796. urb->transfer_flags = URB_ISO_ASAP;
  797. #endif
  798. urb->interval = ep->desc.bInterval;
  799. urb->transfer_buffer = stream->urb_buffer[i];
  800. urb->complete = uvc_video_complete;
  801. urb->number_of_packets = npackets;
  802. urb->transfer_buffer_length = size;
  803. for (j = 0; j < npackets; ++j) {
  804. urb->iso_frame_desc[j].offset = j * psize;
  805. urb->iso_frame_desc[j].length = psize;
  806. }
  807. stream->urb[i] = urb;
  808. }
  809. return 0;
  810. }
  811. /*
  812. * Initialize bulk URBs and allocate transfer buffers. The packet size is
  813. * given by the endpoint.
  814. */
  815. static int uvc_init_video_bulk(struct uvc_streaming *stream,
  816. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  817. {
  818. struct urb *urb;
  819. unsigned int npackets, pipe, i;
  820. u16 psize;
  821. u32 size;
  822. psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
  823. size = stream->ctrl.dwMaxPayloadTransferSize;
  824. stream->bulk.max_payload_size = size;
  825. npackets = uvc_alloc_urb_buffers(stream, size, psize, gfp_flags);
  826. if (npackets == 0)
  827. return -ENOMEM;
  828. size = npackets * psize;
  829. if (usb_endpoint_dir_in(&ep->desc))
  830. pipe = usb_rcvbulkpipe(stream->dev->udev,
  831. ep->desc.bEndpointAddress);
  832. else
  833. pipe = usb_sndbulkpipe(stream->dev->udev,
  834. ep->desc.bEndpointAddress);
  835. if (stream->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  836. size = 0;
  837. for (i = 0; i < UVC_URBS; ++i) {
  838. urb = usb_alloc_urb(0, gfp_flags);
  839. if (urb == NULL) {
  840. uvc_uninit_video(stream, 1);
  841. return -ENOMEM;
  842. }
  843. usb_fill_bulk_urb(urb, stream->dev->udev, pipe,
  844. stream->urb_buffer[i], size, uvc_video_complete,
  845. stream);
  846. #ifndef CONFIG_DMA_NONCOHERENT
  847. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  848. urb->transfer_dma = stream->urb_dma[i];
  849. #endif
  850. stream->urb[i] = urb;
  851. }
  852. return 0;
  853. }
  854. /*
  855. * Initialize isochronous/bulk URBs and allocate transfer buffers.
  856. */
  857. static int uvc_init_video(struct uvc_streaming *stream, gfp_t gfp_flags)
  858. {
  859. struct usb_interface *intf = stream->intf;
  860. struct usb_host_endpoint *ep;
  861. unsigned int i;
  862. int ret;
  863. stream->sequence = -1;
  864. stream->last_fid = -1;
  865. stream->bulk.header_size = 0;
  866. stream->bulk.skip_payload = 0;
  867. stream->bulk.payload_size = 0;
  868. if (intf->num_altsetting > 1) {
  869. struct usb_host_endpoint *best_ep = NULL;
  870. unsigned int best_psize = 3 * 1024;
  871. unsigned int bandwidth;
  872. unsigned int uninitialized_var(altsetting);
  873. int intfnum = stream->intfnum;
  874. /* Isochronous endpoint, select the alternate setting. */
  875. bandwidth = stream->ctrl.dwMaxPayloadTransferSize;
  876. if (bandwidth == 0) {
  877. uvc_trace(UVC_TRACE_VIDEO, "Device requested null "
  878. "bandwidth, defaulting to lowest.\n");
  879. bandwidth = 1;
  880. } else {
  881. uvc_trace(UVC_TRACE_VIDEO, "Device requested %u "
  882. "B/frame bandwidth.\n", bandwidth);
  883. }
  884. for (i = 0; i < intf->num_altsetting; ++i) {
  885. struct usb_host_interface *alts;
  886. unsigned int psize;
  887. alts = &intf->altsetting[i];
  888. ep = uvc_find_endpoint(alts,
  889. stream->header.bEndpointAddress);
  890. if (ep == NULL)
  891. continue;
  892. /* Check if the bandwidth is high enough. */
  893. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  894. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  895. if (psize >= bandwidth && psize <= best_psize) {
  896. altsetting = i;
  897. best_psize = psize;
  898. best_ep = ep;
  899. }
  900. }
  901. if (best_ep == NULL) {
  902. uvc_trace(UVC_TRACE_VIDEO, "No fast enough alt setting "
  903. "for requested bandwidth.\n");
  904. return -EIO;
  905. }
  906. uvc_trace(UVC_TRACE_VIDEO, "Selecting alternate setting %u "
  907. "(%u B/frame bandwidth).\n", altsetting, best_psize);
  908. ret = usb_set_interface(stream->dev->udev, intfnum, altsetting);
  909. if (ret < 0)
  910. return ret;
  911. ret = uvc_init_video_isoc(stream, best_ep, gfp_flags);
  912. } else {
  913. /* Bulk endpoint, proceed to URB initialization. */
  914. ep = uvc_find_endpoint(&intf->altsetting[0],
  915. stream->header.bEndpointAddress);
  916. if (ep == NULL)
  917. return -EIO;
  918. ret = uvc_init_video_bulk(stream, ep, gfp_flags);
  919. }
  920. if (ret < 0)
  921. return ret;
  922. /* Submit the URBs. */
  923. for (i = 0; i < UVC_URBS; ++i) {
  924. ret = usb_submit_urb(stream->urb[i], gfp_flags);
  925. if (ret < 0) {
  926. uvc_printk(KERN_ERR, "Failed to submit URB %u "
  927. "(%d).\n", i, ret);
  928. uvc_uninit_video(stream, 1);
  929. return ret;
  930. }
  931. }
  932. return 0;
  933. }
  934. /* --------------------------------------------------------------------------
  935. * Suspend/resume
  936. */
  937. /*
  938. * Stop streaming without disabling the video queue.
  939. *
  940. * To let userspace applications resume without trouble, we must not touch the
  941. * video buffers in any way. We mark the device as frozen to make sure the URB
  942. * completion handler won't try to cancel the queue when we kill the URBs.
  943. */
  944. int uvc_video_suspend(struct uvc_streaming *stream)
  945. {
  946. if (!uvc_queue_streaming(&stream->queue))
  947. return 0;
  948. stream->frozen = 1;
  949. uvc_uninit_video(stream, 0);
  950. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  951. return 0;
  952. }
  953. /*
  954. * Reconfigure the video interface and restart streaming if it was enabled
  955. * before suspend.
  956. *
  957. * If an error occurs, disable the video queue. This will wake all pending
  958. * buffers, making sure userspace applications are notified of the problem
  959. * instead of waiting forever.
  960. */
  961. int uvc_video_resume(struct uvc_streaming *stream, int reset)
  962. {
  963. int ret;
  964. /* If the bus has been reset on resume, set the alternate setting to 0.
  965. * This should be the default value, but some devices crash or otherwise
  966. * misbehave if they don't receive a SET_INTERFACE request before any
  967. * other video control request.
  968. */
  969. if (reset)
  970. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  971. stream->frozen = 0;
  972. ret = uvc_commit_video(stream, &stream->ctrl);
  973. if (ret < 0) {
  974. uvc_queue_enable(&stream->queue, 0);
  975. return ret;
  976. }
  977. if (!uvc_queue_streaming(&stream->queue))
  978. return 0;
  979. ret = uvc_init_video(stream, GFP_NOIO);
  980. if (ret < 0)
  981. uvc_queue_enable(&stream->queue, 0);
  982. return ret;
  983. }
  984. /* ------------------------------------------------------------------------
  985. * Video device
  986. */
  987. /*
  988. * Initialize the UVC video device by switching to alternate setting 0 and
  989. * retrieve the default format.
  990. *
  991. * Some cameras (namely the Fuji Finepix) set the format and frame
  992. * indexes to zero. The UVC standard doesn't clearly make this a spec
  993. * violation, so try to silently fix the values if possible.
  994. *
  995. * This function is called before registering the device with V4L.
  996. */
  997. int uvc_video_init(struct uvc_streaming *stream)
  998. {
  999. struct uvc_streaming_control *probe = &stream->ctrl;
  1000. struct uvc_format *format = NULL;
  1001. struct uvc_frame *frame = NULL;
  1002. unsigned int i;
  1003. int ret;
  1004. if (stream->nformats == 0) {
  1005. uvc_printk(KERN_INFO, "No supported video formats found.\n");
  1006. return -EINVAL;
  1007. }
  1008. atomic_set(&stream->active, 0);
  1009. /* Initialize the video buffers queue. */
  1010. uvc_queue_init(&stream->queue, stream->type, !uvc_no_drop_param);
  1011. /* Alternate setting 0 should be the default, yet the XBox Live Vision
  1012. * Cam (and possibly other devices) crash or otherwise misbehave if
  1013. * they don't receive a SET_INTERFACE request before any other video
  1014. * control request.
  1015. */
  1016. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  1017. /* Set the streaming probe control with default streaming parameters
  1018. * retrieved from the device. Webcams that don't suport GET_DEF
  1019. * requests on the probe control will just keep their current streaming
  1020. * parameters.
  1021. */
  1022. if (uvc_get_video_ctrl(stream, probe, 1, UVC_GET_DEF) == 0)
  1023. uvc_set_video_ctrl(stream, probe, 1);
  1024. /* Initialize the streaming parameters with the probe control current
  1025. * value. This makes sure SET_CUR requests on the streaming commit
  1026. * control will always use values retrieved from a successful GET_CUR
  1027. * request on the probe control, as required by the UVC specification.
  1028. */
  1029. ret = uvc_get_video_ctrl(stream, probe, 1, UVC_GET_CUR);
  1030. if (ret < 0)
  1031. return ret;
  1032. /* Check if the default format descriptor exists. Use the first
  1033. * available format otherwise.
  1034. */
  1035. for (i = stream->nformats; i > 0; --i) {
  1036. format = &stream->format[i-1];
  1037. if (format->index == probe->bFormatIndex)
  1038. break;
  1039. }
  1040. if (format->nframes == 0) {
  1041. uvc_printk(KERN_INFO, "No frame descriptor found for the "
  1042. "default format.\n");
  1043. return -EINVAL;
  1044. }
  1045. /* Zero bFrameIndex might be correct. Stream-based formats (including
  1046. * MPEG-2 TS and DV) do not support frames but have a dummy frame
  1047. * descriptor with bFrameIndex set to zero. If the default frame
  1048. * descriptor is not found, use the first available frame.
  1049. */
  1050. for (i = format->nframes; i > 0; --i) {
  1051. frame = &format->frame[i-1];
  1052. if (frame->bFrameIndex == probe->bFrameIndex)
  1053. break;
  1054. }
  1055. probe->bFormatIndex = format->index;
  1056. probe->bFrameIndex = frame->bFrameIndex;
  1057. stream->cur_format = format;
  1058. stream->cur_frame = frame;
  1059. /* Select the video decoding function */
  1060. if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1061. if (stream->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
  1062. stream->decode = uvc_video_decode_isight;
  1063. else if (stream->intf->num_altsetting > 1)
  1064. stream->decode = uvc_video_decode_isoc;
  1065. else
  1066. stream->decode = uvc_video_decode_bulk;
  1067. } else {
  1068. if (stream->intf->num_altsetting == 1)
  1069. stream->decode = uvc_video_encode_bulk;
  1070. else {
  1071. uvc_printk(KERN_INFO, "Isochronous endpoints are not "
  1072. "supported for video output devices.\n");
  1073. return -EINVAL;
  1074. }
  1075. }
  1076. return 0;
  1077. }
  1078. /*
  1079. * Enable or disable the video stream.
  1080. */
  1081. int uvc_video_enable(struct uvc_streaming *stream, int enable)
  1082. {
  1083. int ret;
  1084. if (!enable) {
  1085. uvc_uninit_video(stream, 1);
  1086. usb_set_interface(stream->dev->udev, stream->intfnum, 0);
  1087. uvc_queue_enable(&stream->queue, 0);
  1088. return 0;
  1089. }
  1090. ret = uvc_queue_enable(&stream->queue, 1);
  1091. if (ret < 0)
  1092. return ret;
  1093. /* Commit the streaming parameters. */
  1094. ret = uvc_commit_video(stream, &stream->ctrl);
  1095. if (ret < 0) {
  1096. uvc_queue_enable(&stream->queue, 0);
  1097. return ret;
  1098. }
  1099. return uvc_init_video(stream, GFP_KERNEL);
  1100. }