uvc_video.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * uvc_video.c -- USB Video Class driver - Video handling
  3. *
  4. * Copyright (C) 2005-2008
  5. * Laurent Pinchart (laurent.pinchart@skynet.be)
  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/version.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/usb.h>
  18. #include <linux/videodev2.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/wait.h>
  21. #include <asm/atomic.h>
  22. #include <asm/unaligned.h>
  23. #include <media/v4l2-common.h>
  24. #include "uvcvideo.h"
  25. /* ------------------------------------------------------------------------
  26. * UVC Controls
  27. */
  28. static int __uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
  29. __u8 intfnum, __u8 cs, void *data, __u16 size,
  30. int timeout)
  31. {
  32. __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  33. unsigned int pipe;
  34. pipe = (query & 0x80) ? usb_rcvctrlpipe(dev->udev, 0)
  35. : usb_sndctrlpipe(dev->udev, 0);
  36. type |= (query & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
  37. return usb_control_msg(dev->udev, pipe, query, type, cs << 8,
  38. unit << 8 | intfnum, data, size, timeout);
  39. }
  40. int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
  41. __u8 intfnum, __u8 cs, void *data, __u16 size)
  42. {
  43. int ret;
  44. ret = __uvc_query_ctrl(dev, query, unit, intfnum, cs, data, size,
  45. UVC_CTRL_CONTROL_TIMEOUT);
  46. if (ret != size) {
  47. uvc_printk(KERN_ERR, "Failed to query (%u) UVC control %u "
  48. "(unit %u) : %d (exp. %u).\n", query, cs, unit, ret,
  49. size);
  50. return -EIO;
  51. }
  52. return 0;
  53. }
  54. static void uvc_fixup_buffer_size(struct uvc_video_device *video,
  55. struct uvc_streaming_control *ctrl)
  56. {
  57. struct uvc_format *format;
  58. struct uvc_frame *frame;
  59. if (ctrl->bFormatIndex <= 0 ||
  60. ctrl->bFormatIndex > video->streaming->nformats)
  61. return;
  62. format = &video->streaming->format[ctrl->bFormatIndex - 1];
  63. if (ctrl->bFrameIndex <= 0 ||
  64. ctrl->bFrameIndex > format->nframes)
  65. return;
  66. frame = &format->frame[ctrl->bFrameIndex - 1];
  67. if (!(format->flags & UVC_FMT_FLAG_COMPRESSED) ||
  68. (ctrl->dwMaxVideoFrameSize == 0 &&
  69. video->dev->uvc_version < 0x0110))
  70. ctrl->dwMaxVideoFrameSize =
  71. frame->dwMaxVideoFrameBufferSize;
  72. }
  73. static int uvc_get_video_ctrl(struct uvc_video_device *video,
  74. struct uvc_streaming_control *ctrl, int probe, __u8 query)
  75. {
  76. __u8 *data;
  77. __u16 size;
  78. int ret;
  79. size = video->dev->uvc_version >= 0x0110 ? 34 : 26;
  80. data = kmalloc(size, GFP_KERNEL);
  81. if (data == NULL)
  82. return -ENOMEM;
  83. ret = __uvc_query_ctrl(video->dev, query, 0, video->streaming->intfnum,
  84. probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
  85. UVC_CTRL_STREAMING_TIMEOUT);
  86. if ((query == GET_MIN || query == GET_MAX) && ret == 2) {
  87. /* Some cameras, mostly based on Bison Electronics chipsets,
  88. * answer a GET_MIN or GET_MAX request with the wCompQuality
  89. * field only.
  90. */
  91. uvc_warn_once(video->dev, UVC_WARN_MINMAX, "UVC non "
  92. "compliance - GET_MIN/MAX(PROBE) incorrectly "
  93. "supported. Enabling workaround.\n");
  94. memset(ctrl, 0, sizeof ctrl);
  95. ctrl->wCompQuality = le16_to_cpup((__le16 *)data);
  96. ret = 0;
  97. goto out;
  98. } else if (query == GET_DEF && probe == 1) {
  99. /* Many cameras don't support the GET_DEF request on their
  100. * video probe control. Warn once and return, the caller will
  101. * fall back to GET_CUR.
  102. */
  103. uvc_warn_once(video->dev, UVC_WARN_PROBE_DEF, "UVC non "
  104. "compliance - GET_DEF(PROBE) not supported. "
  105. "Enabling workaround.\n");
  106. ret = -EIO;
  107. goto out;
  108. } else if (ret != size) {
  109. uvc_printk(KERN_ERR, "Failed to query (%u) UVC %s control : "
  110. "%d (exp. %u).\n", query, probe ? "probe" : "commit",
  111. ret, size);
  112. ret = -EIO;
  113. goto out;
  114. }
  115. ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]);
  116. ctrl->bFormatIndex = data[2];
  117. ctrl->bFrameIndex = data[3];
  118. ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]);
  119. ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]);
  120. ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]);
  121. ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]);
  122. ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]);
  123. ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]);
  124. ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]);
  125. ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]);
  126. if (size == 34) {
  127. ctrl->dwClockFrequency = get_unaligned_le32(&data[26]);
  128. ctrl->bmFramingInfo = data[30];
  129. ctrl->bPreferedVersion = data[31];
  130. ctrl->bMinVersion = data[32];
  131. ctrl->bMaxVersion = data[33];
  132. } else {
  133. ctrl->dwClockFrequency = video->dev->clock_frequency;
  134. ctrl->bmFramingInfo = 0;
  135. ctrl->bPreferedVersion = 0;
  136. ctrl->bMinVersion = 0;
  137. ctrl->bMaxVersion = 0;
  138. }
  139. /* Some broken devices return a null or wrong dwMaxVideoFrameSize.
  140. * Try to get the value from the format and frame descriptor.
  141. */
  142. uvc_fixup_buffer_size(video, ctrl);
  143. ret = 0;
  144. out:
  145. kfree(data);
  146. return ret;
  147. }
  148. static int uvc_set_video_ctrl(struct uvc_video_device *video,
  149. struct uvc_streaming_control *ctrl, int probe)
  150. {
  151. __u8 *data;
  152. __u16 size;
  153. int ret;
  154. size = video->dev->uvc_version >= 0x0110 ? 34 : 26;
  155. data = kzalloc(size, GFP_KERNEL);
  156. if (data == NULL)
  157. return -ENOMEM;
  158. *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint);
  159. data[2] = ctrl->bFormatIndex;
  160. data[3] = ctrl->bFrameIndex;
  161. *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval);
  162. *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate);
  163. *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate);
  164. *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality);
  165. *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize);
  166. *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay);
  167. /* Note: Some of the fields below are not required for IN devices (see
  168. * UVC spec, 4.3.1.1), but we still copy them in case support for OUT
  169. * devices is added in the future. */
  170. put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]);
  171. put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]);
  172. if (size == 34) {
  173. put_unaligned_le32(ctrl->dwClockFrequency, &data[26]);
  174. data[30] = ctrl->bmFramingInfo;
  175. data[31] = ctrl->bPreferedVersion;
  176. data[32] = ctrl->bMinVersion;
  177. data[33] = ctrl->bMaxVersion;
  178. }
  179. ret = __uvc_query_ctrl(video->dev, SET_CUR, 0,
  180. video->streaming->intfnum,
  181. probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
  182. UVC_CTRL_STREAMING_TIMEOUT);
  183. if (ret != size) {
  184. uvc_printk(KERN_ERR, "Failed to set UVC %s control : "
  185. "%d (exp. %u).\n", probe ? "probe" : "commit",
  186. ret, size);
  187. ret = -EIO;
  188. }
  189. kfree(data);
  190. return ret;
  191. }
  192. int uvc_probe_video(struct uvc_video_device *video,
  193. struct uvc_streaming_control *probe)
  194. {
  195. struct uvc_streaming_control probe_min, probe_max;
  196. __u16 bandwidth;
  197. unsigned int i;
  198. int ret;
  199. mutex_lock(&video->streaming->mutex);
  200. /* Perform probing. The device should adjust the requested values
  201. * according to its capabilities. However, some devices, namely the
  202. * first generation UVC Logitech webcams, don't implement the Video
  203. * Probe control properly, and just return the needed bandwidth. For
  204. * that reason, if the needed bandwidth exceeds the maximum available
  205. * bandwidth, try to lower the quality.
  206. */
  207. if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0)
  208. goto done;
  209. /* Get the minimum and maximum values for compression settings. */
  210. if (!(video->dev->quirks & UVC_QUIRK_PROBE_MINMAX)) {
  211. ret = uvc_get_video_ctrl(video, &probe_min, 1, GET_MIN);
  212. if (ret < 0)
  213. goto done;
  214. ret = uvc_get_video_ctrl(video, &probe_max, 1, GET_MAX);
  215. if (ret < 0)
  216. goto done;
  217. probe->wCompQuality = probe_max.wCompQuality;
  218. }
  219. for (i = 0; i < 2; ++i) {
  220. if ((ret = uvc_set_video_ctrl(video, probe, 1)) < 0 ||
  221. (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
  222. goto done;
  223. if (video->streaming->intf->num_altsetting == 1)
  224. break;
  225. bandwidth = probe->dwMaxPayloadTransferSize;
  226. if (bandwidth <= video->streaming->maxpsize)
  227. break;
  228. if (video->dev->quirks & UVC_QUIRK_PROBE_MINMAX) {
  229. ret = -ENOSPC;
  230. goto done;
  231. }
  232. /* TODO: negotiate compression parameters */
  233. probe->wKeyFrameRate = probe_min.wKeyFrameRate;
  234. probe->wPFrameRate = probe_min.wPFrameRate;
  235. probe->wCompQuality = probe_max.wCompQuality;
  236. probe->wCompWindowSize = probe_min.wCompWindowSize;
  237. }
  238. done:
  239. mutex_unlock(&video->streaming->mutex);
  240. return ret;
  241. }
  242. int uvc_commit_video(struct uvc_video_device *video,
  243. struct uvc_streaming_control *probe)
  244. {
  245. return uvc_set_video_ctrl(video, probe, 0);
  246. }
  247. /* ------------------------------------------------------------------------
  248. * Video codecs
  249. */
  250. /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
  251. #define UVC_STREAM_EOH (1 << 7)
  252. #define UVC_STREAM_ERR (1 << 6)
  253. #define UVC_STREAM_STI (1 << 5)
  254. #define UVC_STREAM_RES (1 << 4)
  255. #define UVC_STREAM_SCR (1 << 3)
  256. #define UVC_STREAM_PTS (1 << 2)
  257. #define UVC_STREAM_EOF (1 << 1)
  258. #define UVC_STREAM_FID (1 << 0)
  259. /* Video payload decoding is handled by uvc_video_decode_start(),
  260. * uvc_video_decode_data() and uvc_video_decode_end().
  261. *
  262. * uvc_video_decode_start is called with URB data at the start of a bulk or
  263. * isochronous payload. It processes header data and returns the header size
  264. * in bytes if successful. If an error occurs, it returns a negative error
  265. * code. The following error codes have special meanings.
  266. *
  267. * - EAGAIN informs the caller that the current video buffer should be marked
  268. * as done, and that the function should be called again with the same data
  269. * and a new video buffer. This is used when end of frame conditions can be
  270. * reliably detected at the beginning of the next frame only.
  271. *
  272. * If an error other than -EAGAIN is returned, the caller will drop the current
  273. * payload. No call to uvc_video_decode_data and uvc_video_decode_end will be
  274. * made until the next payload. -ENODATA can be used to drop the current
  275. * payload if no other error code is appropriate.
  276. *
  277. * uvc_video_decode_data is called for every URB with URB data. It copies the
  278. * data to the video buffer.
  279. *
  280. * uvc_video_decode_end is called with header data at the end of a bulk or
  281. * isochronous payload. It performs any additional header data processing and
  282. * returns 0 or a negative error code if an error occured. As header data have
  283. * already been processed by uvc_video_decode_start, this functions isn't
  284. * required to perform sanity checks a second time.
  285. *
  286. * For isochronous transfers where a payload is always transfered in a single
  287. * URB, the three functions will be called in a row.
  288. *
  289. * To let the decoder process header data and update its internal state even
  290. * when no video buffer is available, uvc_video_decode_start must be prepared
  291. * to be called with a NULL buf parameter. uvc_video_decode_data and
  292. * uvc_video_decode_end will never be called with a NULL buffer.
  293. */
  294. static int uvc_video_decode_start(struct uvc_video_device *video,
  295. struct uvc_buffer *buf, const __u8 *data, int len)
  296. {
  297. __u8 fid;
  298. /* Sanity checks:
  299. * - packet must be at least 2 bytes long
  300. * - bHeaderLength value must be at least 2 bytes (see above)
  301. * - bHeaderLength value can't be larger than the packet size.
  302. */
  303. if (len < 2 || data[0] < 2 || data[0] > len)
  304. return -EINVAL;
  305. /* Skip payloads marked with the error bit ("error frames"). */
  306. if (data[1] & UVC_STREAM_ERR) {
  307. uvc_trace(UVC_TRACE_FRAME, "Dropping payload (error bit "
  308. "set).\n");
  309. return -ENODATA;
  310. }
  311. fid = data[1] & UVC_STREAM_FID;
  312. /* Store the payload FID bit and return immediately when the buffer is
  313. * NULL.
  314. */
  315. if (buf == NULL) {
  316. video->last_fid = fid;
  317. return -ENODATA;
  318. }
  319. /* Synchronize to the input stream by waiting for the FID bit to be
  320. * toggled when the the buffer state is not UVC_BUF_STATE_ACTIVE.
  321. * video->last_fid is initialized to -1, so the first isochronous
  322. * frame will always be in sync.
  323. *
  324. * If the device doesn't toggle the FID bit, invert video->last_fid
  325. * when the EOF bit is set to force synchronisation on the next packet.
  326. */
  327. if (buf->state != UVC_BUF_STATE_ACTIVE) {
  328. if (fid == video->last_fid) {
  329. uvc_trace(UVC_TRACE_FRAME, "Dropping payload (out of "
  330. "sync).\n");
  331. if ((video->dev->quirks & UVC_QUIRK_STREAM_NO_FID) &&
  332. (data[1] & UVC_STREAM_EOF))
  333. video->last_fid ^= UVC_STREAM_FID;
  334. return -ENODATA;
  335. }
  336. /* TODO: Handle PTS and SCR. */
  337. buf->state = UVC_BUF_STATE_ACTIVE;
  338. }
  339. /* Mark the buffer as done if we're at the beginning of a new frame.
  340. * End of frame detection is better implemented by checking the EOF
  341. * bit (FID bit toggling is delayed by one frame compared to the EOF
  342. * bit), but some devices don't set the bit at end of frame (and the
  343. * last payload can be lost anyway). We thus must check if the FID has
  344. * been toggled.
  345. *
  346. * video->last_fid is initialized to -1, so the first isochronous
  347. * frame will never trigger an end of frame detection.
  348. *
  349. * Empty buffers (bytesused == 0) don't trigger end of frame detection
  350. * as it doesn't make sense to return an empty buffer. This also
  351. * avoids detecting and of frame conditions at FID toggling if the
  352. * previous payload had the EOF bit set.
  353. */
  354. if (fid != video->last_fid && buf->buf.bytesused != 0) {
  355. uvc_trace(UVC_TRACE_FRAME, "Frame complete (FID bit "
  356. "toggled).\n");
  357. buf->state = UVC_BUF_STATE_DONE;
  358. return -EAGAIN;
  359. }
  360. video->last_fid = fid;
  361. return data[0];
  362. }
  363. static void uvc_video_decode_data(struct uvc_video_device *video,
  364. struct uvc_buffer *buf, const __u8 *data, int len)
  365. {
  366. struct uvc_video_queue *queue = &video->queue;
  367. unsigned int maxlen, nbytes;
  368. void *mem;
  369. if (len <= 0)
  370. return;
  371. /* Copy the video data to the buffer. */
  372. maxlen = buf->buf.length - buf->buf.bytesused;
  373. mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;
  374. nbytes = min((unsigned int)len, maxlen);
  375. memcpy(mem, data, nbytes);
  376. buf->buf.bytesused += nbytes;
  377. /* Complete the current frame if the buffer size was exceeded. */
  378. if (len > maxlen) {
  379. uvc_trace(UVC_TRACE_FRAME, "Frame complete (overflow).\n");
  380. buf->state = UVC_BUF_STATE_DONE;
  381. }
  382. }
  383. static void uvc_video_decode_end(struct uvc_video_device *video,
  384. struct uvc_buffer *buf, const __u8 *data, int len)
  385. {
  386. /* Mark the buffer as done if the EOF marker is set. */
  387. if (data[1] & UVC_STREAM_EOF && buf->buf.bytesused != 0) {
  388. uvc_trace(UVC_TRACE_FRAME, "Frame complete (EOF found).\n");
  389. if (data[0] == len)
  390. uvc_trace(UVC_TRACE_FRAME, "EOF in empty payload.\n");
  391. buf->state = UVC_BUF_STATE_DONE;
  392. if (video->dev->quirks & UVC_QUIRK_STREAM_NO_FID)
  393. video->last_fid ^= UVC_STREAM_FID;
  394. }
  395. }
  396. static int uvc_video_encode_header(struct uvc_video_device *video,
  397. struct uvc_buffer *buf, __u8 *data, int len)
  398. {
  399. data[0] = 2; /* Header length */
  400. data[1] = UVC_STREAM_EOH | UVC_STREAM_EOF
  401. | (video->last_fid & UVC_STREAM_FID);
  402. return 2;
  403. }
  404. static int uvc_video_encode_data(struct uvc_video_device *video,
  405. struct uvc_buffer *buf, __u8 *data, int len)
  406. {
  407. struct uvc_video_queue *queue = &video->queue;
  408. unsigned int nbytes;
  409. void *mem;
  410. /* Copy video data to the URB buffer. */
  411. mem = queue->mem + buf->buf.m.offset + queue->buf_used;
  412. nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
  413. nbytes = min(video->bulk.max_payload_size - video->bulk.payload_size,
  414. nbytes);
  415. memcpy(data, mem, nbytes);
  416. queue->buf_used += nbytes;
  417. return nbytes;
  418. }
  419. /* ------------------------------------------------------------------------
  420. * URB handling
  421. */
  422. /*
  423. * Completion handler for video URBs.
  424. */
  425. static void uvc_video_decode_isoc(struct urb *urb,
  426. struct uvc_video_device *video, struct uvc_buffer *buf)
  427. {
  428. u8 *mem;
  429. int ret, i;
  430. for (i = 0; i < urb->number_of_packets; ++i) {
  431. if (urb->iso_frame_desc[i].status < 0) {
  432. uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
  433. "lost (%d).\n", urb->iso_frame_desc[i].status);
  434. continue;
  435. }
  436. /* Decode the payload header. */
  437. mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  438. do {
  439. ret = uvc_video_decode_start(video, buf, mem,
  440. urb->iso_frame_desc[i].actual_length);
  441. if (ret == -EAGAIN)
  442. buf = uvc_queue_next_buffer(&video->queue, buf);
  443. } while (ret == -EAGAIN);
  444. if (ret < 0)
  445. continue;
  446. /* Decode the payload data. */
  447. uvc_video_decode_data(video, buf, mem + ret,
  448. urb->iso_frame_desc[i].actual_length - ret);
  449. /* Process the header again. */
  450. uvc_video_decode_end(video, buf, mem,
  451. urb->iso_frame_desc[i].actual_length);
  452. if (buf->state == UVC_BUF_STATE_DONE ||
  453. buf->state == UVC_BUF_STATE_ERROR)
  454. buf = uvc_queue_next_buffer(&video->queue, buf);
  455. }
  456. }
  457. static void uvc_video_decode_bulk(struct urb *urb,
  458. struct uvc_video_device *video, struct uvc_buffer *buf)
  459. {
  460. u8 *mem;
  461. int len, ret;
  462. mem = urb->transfer_buffer;
  463. len = urb->actual_length;
  464. video->bulk.payload_size += len;
  465. /* If the URB is the first of its payload, decode and save the
  466. * header.
  467. */
  468. if (video->bulk.header_size == 0 && !video->bulk.skip_payload) {
  469. do {
  470. ret = uvc_video_decode_start(video, buf, mem, len);
  471. if (ret == -EAGAIN)
  472. buf = uvc_queue_next_buffer(&video->queue, buf);
  473. } while (ret == -EAGAIN);
  474. /* If an error occured skip the rest of the payload. */
  475. if (ret < 0 || buf == NULL) {
  476. video->bulk.skip_payload = 1;
  477. } else {
  478. memcpy(video->bulk.header, mem, ret);
  479. video->bulk.header_size = ret;
  480. mem += ret;
  481. len -= ret;
  482. }
  483. }
  484. /* The buffer queue might have been cancelled while a bulk transfer
  485. * was in progress, so we can reach here with buf equal to NULL. Make
  486. * sure buf is never dereferenced if NULL.
  487. */
  488. /* Process video data. */
  489. if (!video->bulk.skip_payload && buf != NULL)
  490. uvc_video_decode_data(video, buf, mem, len);
  491. /* Detect the payload end by a URB smaller than the maximum size (or
  492. * a payload size equal to the maximum) and process the header again.
  493. */
  494. if (urb->actual_length < urb->transfer_buffer_length ||
  495. video->bulk.payload_size >= video->bulk.max_payload_size) {
  496. if (!video->bulk.skip_payload && buf != NULL) {
  497. uvc_video_decode_end(video, buf, video->bulk.header,
  498. video->bulk.payload_size);
  499. if (buf->state == UVC_BUF_STATE_DONE ||
  500. buf->state == UVC_BUF_STATE_ERROR)
  501. buf = uvc_queue_next_buffer(&video->queue, buf);
  502. }
  503. video->bulk.header_size = 0;
  504. video->bulk.skip_payload = 0;
  505. video->bulk.payload_size = 0;
  506. }
  507. }
  508. static void uvc_video_encode_bulk(struct urb *urb,
  509. struct uvc_video_device *video, struct uvc_buffer *buf)
  510. {
  511. u8 *mem = urb->transfer_buffer;
  512. int len = video->urb_size, ret;
  513. if (buf == NULL) {
  514. urb->transfer_buffer_length = 0;
  515. return;
  516. }
  517. /* If the URB is the first of its payload, add the header. */
  518. if (video->bulk.header_size == 0) {
  519. ret = uvc_video_encode_header(video, buf, mem, len);
  520. video->bulk.header_size = ret;
  521. video->bulk.payload_size += ret;
  522. mem += ret;
  523. len -= ret;
  524. }
  525. /* Process video data. */
  526. ret = uvc_video_encode_data(video, buf, mem, len);
  527. video->bulk.payload_size += ret;
  528. len -= ret;
  529. if (buf->buf.bytesused == video->queue.buf_used ||
  530. video->bulk.payload_size == video->bulk.max_payload_size) {
  531. if (buf->buf.bytesused == video->queue.buf_used) {
  532. video->queue.buf_used = 0;
  533. buf->state = UVC_BUF_STATE_DONE;
  534. uvc_queue_next_buffer(&video->queue, buf);
  535. video->last_fid ^= UVC_STREAM_FID;
  536. }
  537. video->bulk.header_size = 0;
  538. video->bulk.payload_size = 0;
  539. }
  540. urb->transfer_buffer_length = video->urb_size - len;
  541. }
  542. static void uvc_video_complete(struct urb *urb)
  543. {
  544. struct uvc_video_device *video = urb->context;
  545. struct uvc_video_queue *queue = &video->queue;
  546. struct uvc_buffer *buf = NULL;
  547. unsigned long flags;
  548. int ret;
  549. switch (urb->status) {
  550. case 0:
  551. break;
  552. default:
  553. uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
  554. "completion handler.\n", urb->status);
  555. case -ENOENT: /* usb_kill_urb() called. */
  556. if (video->frozen)
  557. return;
  558. case -ECONNRESET: /* usb_unlink_urb() called. */
  559. case -ESHUTDOWN: /* The endpoint is being disabled. */
  560. uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
  561. return;
  562. }
  563. spin_lock_irqsave(&queue->irqlock, flags);
  564. if (!list_empty(&queue->irqqueue))
  565. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  566. queue);
  567. spin_unlock_irqrestore(&queue->irqlock, flags);
  568. video->decode(urb, video, buf);
  569. if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  570. uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
  571. ret);
  572. }
  573. }
  574. /*
  575. * Free transfer buffers.
  576. */
  577. static void uvc_free_urb_buffers(struct uvc_video_device *video)
  578. {
  579. unsigned int i;
  580. for (i = 0; i < UVC_URBS; ++i) {
  581. if (video->urb_buffer[i]) {
  582. usb_buffer_free(video->dev->udev, video->urb_size,
  583. video->urb_buffer[i], video->urb_dma[i]);
  584. video->urb_buffer[i] = NULL;
  585. }
  586. }
  587. video->urb_size = 0;
  588. }
  589. /*
  590. * Allocate transfer buffers. This function can be called with buffers
  591. * already allocated when resuming from suspend, in which case it will
  592. * return without touching the buffers.
  593. *
  594. * Return 0 on success or -ENOMEM when out of memory.
  595. */
  596. static int uvc_alloc_urb_buffers(struct uvc_video_device *video,
  597. unsigned int size)
  598. {
  599. unsigned int i;
  600. /* Buffers are already allocated, bail out. */
  601. if (video->urb_size)
  602. return 0;
  603. for (i = 0; i < UVC_URBS; ++i) {
  604. video->urb_buffer[i] = usb_buffer_alloc(video->dev->udev,
  605. size, GFP_KERNEL, &video->urb_dma[i]);
  606. if (video->urb_buffer[i] == NULL) {
  607. uvc_free_urb_buffers(video);
  608. return -ENOMEM;
  609. }
  610. }
  611. video->urb_size = size;
  612. return 0;
  613. }
  614. /*
  615. * Uninitialize isochronous/bulk URBs and free transfer buffers.
  616. */
  617. static void uvc_uninit_video(struct uvc_video_device *video, int free_buffers)
  618. {
  619. struct urb *urb;
  620. unsigned int i;
  621. for (i = 0; i < UVC_URBS; ++i) {
  622. if ((urb = video->urb[i]) == NULL)
  623. continue;
  624. usb_kill_urb(urb);
  625. usb_free_urb(urb);
  626. video->urb[i] = NULL;
  627. }
  628. if (free_buffers)
  629. uvc_free_urb_buffers(video);
  630. }
  631. /*
  632. * Initialize isochronous URBs and allocate transfer buffers. The packet size
  633. * is given by the endpoint.
  634. */
  635. static int uvc_init_video_isoc(struct uvc_video_device *video,
  636. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  637. {
  638. struct urb *urb;
  639. unsigned int npackets, i, j;
  640. __u16 psize;
  641. __u32 size;
  642. /* Compute the number of isochronous packets to allocate by dividing
  643. * the maximum video frame size by the packet size. Limit the result
  644. * to UVC_MAX_ISO_PACKETS.
  645. */
  646. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  647. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  648. size = video->streaming->ctrl.dwMaxVideoFrameSize;
  649. if (size > UVC_MAX_FRAME_SIZE)
  650. return -EINVAL;
  651. npackets = DIV_ROUND_UP(size, psize);
  652. if (npackets > UVC_MAX_ISO_PACKETS)
  653. npackets = UVC_MAX_ISO_PACKETS;
  654. size = npackets * psize;
  655. if (uvc_alloc_urb_buffers(video, size) < 0)
  656. return -ENOMEM;
  657. for (i = 0; i < UVC_URBS; ++i) {
  658. urb = usb_alloc_urb(npackets, gfp_flags);
  659. if (urb == NULL) {
  660. uvc_uninit_video(video, 1);
  661. return -ENOMEM;
  662. }
  663. urb->dev = video->dev->udev;
  664. urb->context = video;
  665. urb->pipe = usb_rcvisocpipe(video->dev->udev,
  666. ep->desc.bEndpointAddress);
  667. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  668. urb->interval = ep->desc.bInterval;
  669. urb->transfer_buffer = video->urb_buffer[i];
  670. urb->transfer_dma = video->urb_dma[i];
  671. urb->complete = uvc_video_complete;
  672. urb->number_of_packets = npackets;
  673. urb->transfer_buffer_length = size;
  674. for (j = 0; j < npackets; ++j) {
  675. urb->iso_frame_desc[j].offset = j * psize;
  676. urb->iso_frame_desc[j].length = psize;
  677. }
  678. video->urb[i] = urb;
  679. }
  680. return 0;
  681. }
  682. /*
  683. * Initialize bulk URBs and allocate transfer buffers. The packet size is
  684. * given by the endpoint.
  685. */
  686. static int uvc_init_video_bulk(struct uvc_video_device *video,
  687. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  688. {
  689. struct urb *urb;
  690. unsigned int pipe, i;
  691. __u16 psize;
  692. __u32 size;
  693. /* Compute the bulk URB size. Some devices set the maximum payload
  694. * size to a value too high for memory-constrained devices. We must
  695. * then transfer the payload accross multiple URBs. To be consistant
  696. * with isochronous mode, allocate maximum UVC_MAX_ISO_PACKETS per bulk
  697. * URB.
  698. */
  699. psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
  700. size = video->streaming->ctrl.dwMaxPayloadTransferSize;
  701. video->bulk.max_payload_size = size;
  702. if (size > psize * UVC_MAX_ISO_PACKETS)
  703. size = psize * UVC_MAX_ISO_PACKETS;
  704. if (uvc_alloc_urb_buffers(video, size) < 0)
  705. return -ENOMEM;
  706. if (usb_endpoint_dir_in(&ep->desc))
  707. pipe = usb_rcvbulkpipe(video->dev->udev,
  708. ep->desc.bEndpointAddress);
  709. else
  710. pipe = usb_sndbulkpipe(video->dev->udev,
  711. ep->desc.bEndpointAddress);
  712. if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  713. size = 0;
  714. for (i = 0; i < UVC_URBS; ++i) {
  715. urb = usb_alloc_urb(0, gfp_flags);
  716. if (urb == NULL) {
  717. uvc_uninit_video(video, 1);
  718. return -ENOMEM;
  719. }
  720. usb_fill_bulk_urb(urb, video->dev->udev, pipe,
  721. video->urb_buffer[i], size, uvc_video_complete,
  722. video);
  723. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  724. urb->transfer_dma = video->urb_dma[i];
  725. video->urb[i] = urb;
  726. }
  727. return 0;
  728. }
  729. /*
  730. * Initialize isochronous/bulk URBs and allocate transfer buffers.
  731. */
  732. static int uvc_init_video(struct uvc_video_device *video, gfp_t gfp_flags)
  733. {
  734. struct usb_interface *intf = video->streaming->intf;
  735. struct usb_host_interface *alts;
  736. struct usb_host_endpoint *ep = NULL;
  737. int intfnum = video->streaming->intfnum;
  738. unsigned int bandwidth, psize, i;
  739. int ret;
  740. video->last_fid = -1;
  741. video->bulk.header_size = 0;
  742. video->bulk.skip_payload = 0;
  743. video->bulk.payload_size = 0;
  744. if (intf->num_altsetting > 1) {
  745. /* Isochronous endpoint, select the alternate setting. */
  746. bandwidth = video->streaming->ctrl.dwMaxPayloadTransferSize;
  747. if (bandwidth == 0) {
  748. uvc_printk(KERN_WARNING, "device %s requested null "
  749. "bandwidth, defaulting to lowest.\n",
  750. video->vdev->name);
  751. bandwidth = 1;
  752. }
  753. for (i = 0; i < intf->num_altsetting; ++i) {
  754. alts = &intf->altsetting[i];
  755. ep = uvc_find_endpoint(alts,
  756. video->streaming->header.bEndpointAddress);
  757. if (ep == NULL)
  758. continue;
  759. /* Check if the bandwidth is high enough. */
  760. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  761. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  762. if (psize >= bandwidth)
  763. break;
  764. }
  765. if (i >= intf->num_altsetting)
  766. return -EIO;
  767. if ((ret = usb_set_interface(video->dev->udev, intfnum, i)) < 0)
  768. return ret;
  769. ret = uvc_init_video_isoc(video, ep, gfp_flags);
  770. } else {
  771. /* Bulk endpoint, proceed to URB initialization. */
  772. ep = uvc_find_endpoint(&intf->altsetting[0],
  773. video->streaming->header.bEndpointAddress);
  774. if (ep == NULL)
  775. return -EIO;
  776. ret = uvc_init_video_bulk(video, ep, gfp_flags);
  777. }
  778. if (ret < 0)
  779. return ret;
  780. /* Submit the URBs. */
  781. for (i = 0; i < UVC_URBS; ++i) {
  782. if ((ret = usb_submit_urb(video->urb[i], gfp_flags)) < 0) {
  783. uvc_printk(KERN_ERR, "Failed to submit URB %u "
  784. "(%d).\n", i, ret);
  785. uvc_uninit_video(video, 1);
  786. return ret;
  787. }
  788. }
  789. return 0;
  790. }
  791. /* --------------------------------------------------------------------------
  792. * Suspend/resume
  793. */
  794. /*
  795. * Stop streaming without disabling the video queue.
  796. *
  797. * To let userspace applications resume without trouble, we must not touch the
  798. * video buffers in any way. We mark the device as frozen to make sure the URB
  799. * completion handler won't try to cancel the queue when we kill the URBs.
  800. */
  801. int uvc_video_suspend(struct uvc_video_device *video)
  802. {
  803. if (!uvc_queue_streaming(&video->queue))
  804. return 0;
  805. video->frozen = 1;
  806. uvc_uninit_video(video, 0);
  807. usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
  808. return 0;
  809. }
  810. /*
  811. * Reconfigure the video interface and restart streaming if it was enable
  812. * before suspend.
  813. *
  814. * If an error occurs, disable the video queue. This will wake all pending
  815. * buffers, making sure userspace applications are notified of the problem
  816. * instead of waiting forever.
  817. */
  818. int uvc_video_resume(struct uvc_video_device *video)
  819. {
  820. int ret;
  821. video->frozen = 0;
  822. if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0) {
  823. uvc_queue_enable(&video->queue, 0);
  824. return ret;
  825. }
  826. if (!uvc_queue_streaming(&video->queue))
  827. return 0;
  828. if ((ret = uvc_init_video(video, GFP_NOIO)) < 0)
  829. uvc_queue_enable(&video->queue, 0);
  830. return ret;
  831. }
  832. /* ------------------------------------------------------------------------
  833. * Video device
  834. */
  835. /*
  836. * Initialize the UVC video device by retrieving the default format and
  837. * committing it.
  838. *
  839. * Some cameras (namely the Fuji Finepix) set the format and frame
  840. * indexes to zero. The UVC standard doesn't clearly make this a spec
  841. * violation, so try to silently fix the values if possible.
  842. *
  843. * This function is called before registering the device with V4L.
  844. */
  845. int uvc_video_init(struct uvc_video_device *video)
  846. {
  847. struct uvc_streaming_control *probe = &video->streaming->ctrl;
  848. struct uvc_format *format = NULL;
  849. struct uvc_frame *frame = NULL;
  850. unsigned int i;
  851. int ret;
  852. if (video->streaming->nformats == 0) {
  853. uvc_printk(KERN_INFO, "No supported video formats found.\n");
  854. return -EINVAL;
  855. }
  856. /* Alternate setting 0 should be the default, yet the XBox Live Vision
  857. * Cam (and possibly other devices) crash or otherwise misbehave if
  858. * they don't receive a SET_INTERFACE request before any other video
  859. * control request.
  860. */
  861. usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
  862. /* Some webcams don't suport GET_DEF request on the probe control. We
  863. * fall back to GET_CUR if GET_DEF fails.
  864. */
  865. if ((ret = uvc_get_video_ctrl(video, probe, 1, GET_DEF)) < 0 &&
  866. (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
  867. return ret;
  868. /* Check if the default format descriptor exists. Use the first
  869. * available format otherwise.
  870. */
  871. for (i = video->streaming->nformats; i > 0; --i) {
  872. format = &video->streaming->format[i-1];
  873. if (format->index == probe->bFormatIndex)
  874. break;
  875. }
  876. if (format->nframes == 0) {
  877. uvc_printk(KERN_INFO, "No frame descriptor found for the "
  878. "default format.\n");
  879. return -EINVAL;
  880. }
  881. /* Zero bFrameIndex might be correct. Stream-based formats (including
  882. * MPEG-2 TS and DV) do not support frames but have a dummy frame
  883. * descriptor with bFrameIndex set to zero. If the default frame
  884. * descriptor is not found, use the first avalable frame.
  885. */
  886. for (i = format->nframes; i > 0; --i) {
  887. frame = &format->frame[i-1];
  888. if (frame->bFrameIndex == probe->bFrameIndex)
  889. break;
  890. }
  891. probe->bFormatIndex = format->index;
  892. probe->bFrameIndex = frame->bFrameIndex;
  893. video->streaming->cur_format = format;
  894. video->streaming->cur_frame = frame;
  895. atomic_set(&video->active, 0);
  896. /* Select the video decoding function */
  897. if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  898. if (video->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
  899. video->decode = uvc_video_decode_isight;
  900. else if (video->streaming->intf->num_altsetting > 1)
  901. video->decode = uvc_video_decode_isoc;
  902. else
  903. video->decode = uvc_video_decode_bulk;
  904. } else {
  905. if (video->streaming->intf->num_altsetting == 1)
  906. video->decode = uvc_video_encode_bulk;
  907. else {
  908. uvc_printk(KERN_INFO, "Isochronous endpoints are not "
  909. "supported for video output devices.\n");
  910. return -EINVAL;
  911. }
  912. }
  913. return 0;
  914. }
  915. /*
  916. * Enable or disable the video stream.
  917. */
  918. int uvc_video_enable(struct uvc_video_device *video, int enable)
  919. {
  920. int ret;
  921. if (!enable) {
  922. uvc_uninit_video(video, 1);
  923. usb_set_interface(video->dev->udev,
  924. video->streaming->intfnum, 0);
  925. uvc_queue_enable(&video->queue, 0);
  926. return 0;
  927. }
  928. if ((video->streaming->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
  929. uvc_no_drop_param)
  930. video->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
  931. else
  932. video->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
  933. if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
  934. return ret;
  935. /* Commit the streaming parameters. */
  936. if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0)
  937. return ret;
  938. return uvc_init_video(video, GFP_KERNEL);
  939. }