uvc_video.c 38 KB

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