uvc_video.c 36 KB

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