uvc_video.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. /* ------------------------------------------------------------------------
  397. * URB handling
  398. */
  399. /*
  400. * Completion handler for video URBs.
  401. */
  402. static void uvc_video_decode_isoc(struct urb *urb,
  403. struct uvc_video_device *video, struct uvc_buffer *buf)
  404. {
  405. u8 *mem;
  406. int ret, i;
  407. for (i = 0; i < urb->number_of_packets; ++i) {
  408. if (urb->iso_frame_desc[i].status < 0) {
  409. uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
  410. "lost (%d).\n", urb->iso_frame_desc[i].status);
  411. continue;
  412. }
  413. /* Decode the payload header. */
  414. mem = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  415. do {
  416. ret = uvc_video_decode_start(video, buf, mem,
  417. urb->iso_frame_desc[i].actual_length);
  418. if (ret == -EAGAIN)
  419. buf = uvc_queue_next_buffer(&video->queue, buf);
  420. } while (ret == -EAGAIN);
  421. if (ret < 0)
  422. continue;
  423. /* Decode the payload data. */
  424. uvc_video_decode_data(video, buf, mem + ret,
  425. urb->iso_frame_desc[i].actual_length - ret);
  426. /* Process the header again. */
  427. uvc_video_decode_end(video, buf, mem,
  428. urb->iso_frame_desc[i].actual_length);
  429. if (buf->state == UVC_BUF_STATE_DONE ||
  430. buf->state == UVC_BUF_STATE_ERROR)
  431. buf = uvc_queue_next_buffer(&video->queue, buf);
  432. }
  433. }
  434. static void uvc_video_decode_bulk(struct urb *urb,
  435. struct uvc_video_device *video, struct uvc_buffer *buf)
  436. {
  437. u8 *mem;
  438. int len, ret;
  439. mem = urb->transfer_buffer;
  440. len = urb->actual_length;
  441. video->bulk.payload_size += len;
  442. /* If the URB is the first of its payload, decode and save the
  443. * header.
  444. */
  445. if (video->bulk.header_size == 0 && !video->bulk.skip_payload) {
  446. do {
  447. ret = uvc_video_decode_start(video, buf, mem, len);
  448. if (ret == -EAGAIN)
  449. buf = uvc_queue_next_buffer(&video->queue, buf);
  450. } while (ret == -EAGAIN);
  451. /* If an error occured skip the rest of the payload. */
  452. if (ret < 0 || buf == NULL) {
  453. video->bulk.skip_payload = 1;
  454. } else {
  455. memcpy(video->bulk.header, mem, ret);
  456. video->bulk.header_size = ret;
  457. mem += ret;
  458. len -= ret;
  459. }
  460. }
  461. /* The buffer queue might have been cancelled while a bulk transfer
  462. * was in progress, so we can reach here with buf equal to NULL. Make
  463. * sure buf is never dereferenced if NULL.
  464. */
  465. /* Process video data. */
  466. if (!video->bulk.skip_payload && buf != NULL)
  467. uvc_video_decode_data(video, buf, mem, len);
  468. /* Detect the payload end by a URB smaller than the maximum size (or
  469. * a payload size equal to the maximum) and process the header again.
  470. */
  471. if (urb->actual_length < urb->transfer_buffer_length ||
  472. video->bulk.payload_size >= video->bulk.max_payload_size) {
  473. if (!video->bulk.skip_payload && buf != NULL) {
  474. uvc_video_decode_end(video, buf, video->bulk.header,
  475. video->bulk.payload_size);
  476. if (buf->state == UVC_BUF_STATE_DONE ||
  477. buf->state == UVC_BUF_STATE_ERROR)
  478. buf = uvc_queue_next_buffer(&video->queue, buf);
  479. }
  480. video->bulk.header_size = 0;
  481. video->bulk.skip_payload = 0;
  482. video->bulk.payload_size = 0;
  483. }
  484. }
  485. static void uvc_video_complete(struct urb *urb)
  486. {
  487. struct uvc_video_device *video = urb->context;
  488. struct uvc_video_queue *queue = &video->queue;
  489. struct uvc_buffer *buf = NULL;
  490. unsigned long flags;
  491. int ret;
  492. switch (urb->status) {
  493. case 0:
  494. break;
  495. default:
  496. uvc_printk(KERN_WARNING, "Non-zero status (%d) in video "
  497. "completion handler.\n", urb->status);
  498. case -ENOENT: /* usb_kill_urb() called. */
  499. if (video->frozen)
  500. return;
  501. case -ECONNRESET: /* usb_unlink_urb() called. */
  502. case -ESHUTDOWN: /* The endpoint is being disabled. */
  503. uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);
  504. return;
  505. }
  506. spin_lock_irqsave(&queue->irqlock, flags);
  507. if (!list_empty(&queue->irqqueue))
  508. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  509. queue);
  510. spin_unlock_irqrestore(&queue->irqlock, flags);
  511. video->decode(urb, video, buf);
  512. if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  513. uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",
  514. ret);
  515. }
  516. }
  517. /*
  518. * Free transfer buffers.
  519. */
  520. static void uvc_free_urb_buffers(struct uvc_video_device *video)
  521. {
  522. unsigned int i;
  523. for (i = 0; i < UVC_URBS; ++i) {
  524. if (video->urb_buffer[i]) {
  525. usb_buffer_free(video->dev->udev, video->urb_size,
  526. video->urb_buffer[i], video->urb_dma[i]);
  527. video->urb_buffer[i] = NULL;
  528. }
  529. }
  530. video->urb_size = 0;
  531. }
  532. /*
  533. * Allocate transfer buffers. This function can be called with buffers
  534. * already allocated when resuming from suspend, in which case it will
  535. * return without touching the buffers.
  536. *
  537. * Return 0 on success or -ENOMEM when out of memory.
  538. */
  539. static int uvc_alloc_urb_buffers(struct uvc_video_device *video,
  540. unsigned int size)
  541. {
  542. unsigned int i;
  543. /* Buffers are already allocated, bail out. */
  544. if (video->urb_size)
  545. return 0;
  546. for (i = 0; i < UVC_URBS; ++i) {
  547. video->urb_buffer[i] = usb_buffer_alloc(video->dev->udev,
  548. size, GFP_KERNEL, &video->urb_dma[i]);
  549. if (video->urb_buffer[i] == NULL) {
  550. uvc_free_urb_buffers(video);
  551. return -ENOMEM;
  552. }
  553. }
  554. video->urb_size = size;
  555. return 0;
  556. }
  557. /*
  558. * Uninitialize isochronous/bulk URBs and free transfer buffers.
  559. */
  560. static void uvc_uninit_video(struct uvc_video_device *video, int free_buffers)
  561. {
  562. struct urb *urb;
  563. unsigned int i;
  564. for (i = 0; i < UVC_URBS; ++i) {
  565. if ((urb = video->urb[i]) == NULL)
  566. continue;
  567. usb_kill_urb(urb);
  568. usb_free_urb(urb);
  569. video->urb[i] = NULL;
  570. }
  571. if (free_buffers)
  572. uvc_free_urb_buffers(video);
  573. }
  574. /*
  575. * Initialize isochronous URBs and allocate transfer buffers. The packet size
  576. * is given by the endpoint.
  577. */
  578. static int uvc_init_video_isoc(struct uvc_video_device *video,
  579. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  580. {
  581. struct urb *urb;
  582. unsigned int npackets, i, j;
  583. __u16 psize;
  584. __u32 size;
  585. /* Compute the number of isochronous packets to allocate by dividing
  586. * the maximum video frame size by the packet size. Limit the result
  587. * to UVC_MAX_ISO_PACKETS.
  588. */
  589. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  590. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  591. size = video->streaming->ctrl.dwMaxVideoFrameSize;
  592. if (size > UVC_MAX_FRAME_SIZE)
  593. return -EINVAL;
  594. npackets = DIV_ROUND_UP(size, psize);
  595. if (npackets > UVC_MAX_ISO_PACKETS)
  596. npackets = UVC_MAX_ISO_PACKETS;
  597. size = npackets * psize;
  598. if (uvc_alloc_urb_buffers(video, size) < 0)
  599. return -ENOMEM;
  600. for (i = 0; i < UVC_URBS; ++i) {
  601. urb = usb_alloc_urb(npackets, gfp_flags);
  602. if (urb == NULL) {
  603. uvc_uninit_video(video, 1);
  604. return -ENOMEM;
  605. }
  606. urb->dev = video->dev->udev;
  607. urb->context = video;
  608. urb->pipe = usb_rcvisocpipe(video->dev->udev,
  609. ep->desc.bEndpointAddress);
  610. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  611. urb->interval = ep->desc.bInterval;
  612. urb->transfer_buffer = video->urb_buffer[i];
  613. urb->transfer_dma = video->urb_dma[i];
  614. urb->complete = uvc_video_complete;
  615. urb->number_of_packets = npackets;
  616. urb->transfer_buffer_length = size;
  617. for (j = 0; j < npackets; ++j) {
  618. urb->iso_frame_desc[j].offset = j * psize;
  619. urb->iso_frame_desc[j].length = psize;
  620. }
  621. video->urb[i] = urb;
  622. }
  623. return 0;
  624. }
  625. /*
  626. * Initialize bulk URBs and allocate transfer buffers. The packet size is
  627. * given by the endpoint.
  628. */
  629. static int uvc_init_video_bulk(struct uvc_video_device *video,
  630. struct usb_host_endpoint *ep, gfp_t gfp_flags)
  631. {
  632. struct urb *urb;
  633. unsigned int pipe, i;
  634. __u16 psize;
  635. __u32 size;
  636. /* Compute the bulk URB size. Some devices set the maximum payload
  637. * size to a value too high for memory-constrained devices. We must
  638. * then transfer the payload accross multiple URBs. To be consistant
  639. * with isochronous mode, allocate maximum UVC_MAX_ISO_PACKETS per bulk
  640. * URB.
  641. */
  642. psize = le16_to_cpu(ep->desc.wMaxPacketSize) & 0x07ff;
  643. size = video->streaming->ctrl.dwMaxPayloadTransferSize;
  644. video->bulk.max_payload_size = size;
  645. if (size > psize * UVC_MAX_ISO_PACKETS)
  646. size = psize * UVC_MAX_ISO_PACKETS;
  647. if (uvc_alloc_urb_buffers(video, size) < 0)
  648. return -ENOMEM;
  649. pipe = usb_rcvbulkpipe(video->dev->udev, ep->desc.bEndpointAddress);
  650. for (i = 0; i < UVC_URBS; ++i) {
  651. urb = usb_alloc_urb(0, gfp_flags);
  652. if (urb == NULL) {
  653. uvc_uninit_video(video, 1);
  654. return -ENOMEM;
  655. }
  656. usb_fill_bulk_urb(urb, video->dev->udev, pipe,
  657. video->urb_buffer[i], size, uvc_video_complete,
  658. video);
  659. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  660. urb->transfer_dma = video->urb_dma[i];
  661. video->urb[i] = urb;
  662. }
  663. return 0;
  664. }
  665. /*
  666. * Initialize isochronous/bulk URBs and allocate transfer buffers.
  667. */
  668. static int uvc_init_video(struct uvc_video_device *video, gfp_t gfp_flags)
  669. {
  670. struct usb_interface *intf = video->streaming->intf;
  671. struct usb_host_interface *alts;
  672. struct usb_host_endpoint *ep = NULL;
  673. int intfnum = video->streaming->intfnum;
  674. unsigned int bandwidth, psize, i;
  675. int ret;
  676. video->last_fid = -1;
  677. video->bulk.header_size = 0;
  678. video->bulk.skip_payload = 0;
  679. video->bulk.payload_size = 0;
  680. if (intf->num_altsetting > 1) {
  681. /* Isochronous endpoint, select the alternate setting. */
  682. bandwidth = video->streaming->ctrl.dwMaxPayloadTransferSize;
  683. if (bandwidth == 0) {
  684. uvc_printk(KERN_WARNING, "device %s requested null "
  685. "bandwidth, defaulting to lowest.\n",
  686. video->vdev->name);
  687. bandwidth = 1;
  688. }
  689. for (i = 0; i < intf->num_altsetting; ++i) {
  690. alts = &intf->altsetting[i];
  691. ep = uvc_find_endpoint(alts,
  692. video->streaming->header.bEndpointAddress);
  693. if (ep == NULL)
  694. continue;
  695. /* Check if the bandwidth is high enough. */
  696. psize = le16_to_cpu(ep->desc.wMaxPacketSize);
  697. psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
  698. if (psize >= bandwidth)
  699. break;
  700. }
  701. if (i >= intf->num_altsetting)
  702. return -EIO;
  703. if ((ret = usb_set_interface(video->dev->udev, intfnum, i)) < 0)
  704. return ret;
  705. ret = uvc_init_video_isoc(video, ep, gfp_flags);
  706. } else {
  707. /* Bulk endpoint, proceed to URB initialization. */
  708. ep = uvc_find_endpoint(&intf->altsetting[0],
  709. video->streaming->header.bEndpointAddress);
  710. if (ep == NULL)
  711. return -EIO;
  712. ret = uvc_init_video_bulk(video, ep, gfp_flags);
  713. }
  714. if (ret < 0)
  715. return ret;
  716. /* Submit the URBs. */
  717. for (i = 0; i < UVC_URBS; ++i) {
  718. if ((ret = usb_submit_urb(video->urb[i], gfp_flags)) < 0) {
  719. uvc_printk(KERN_ERR, "Failed to submit URB %u "
  720. "(%d).\n", i, ret);
  721. uvc_uninit_video(video, 1);
  722. return ret;
  723. }
  724. }
  725. return 0;
  726. }
  727. /* --------------------------------------------------------------------------
  728. * Suspend/resume
  729. */
  730. /*
  731. * Stop streaming without disabling the video queue.
  732. *
  733. * To let userspace applications resume without trouble, we must not touch the
  734. * video buffers in any way. We mark the device as frozen to make sure the URB
  735. * completion handler won't try to cancel the queue when we kill the URBs.
  736. */
  737. int uvc_video_suspend(struct uvc_video_device *video)
  738. {
  739. if (!uvc_queue_streaming(&video->queue))
  740. return 0;
  741. video->frozen = 1;
  742. uvc_uninit_video(video, 0);
  743. usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
  744. return 0;
  745. }
  746. /*
  747. * Reconfigure the video interface and restart streaming if it was enable
  748. * before suspend.
  749. *
  750. * If an error occurs, disable the video queue. This will wake all pending
  751. * buffers, making sure userspace applications are notified of the problem
  752. * instead of waiting forever.
  753. */
  754. int uvc_video_resume(struct uvc_video_device *video)
  755. {
  756. int ret;
  757. video->frozen = 0;
  758. if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0) {
  759. uvc_queue_enable(&video->queue, 0);
  760. return ret;
  761. }
  762. if (!uvc_queue_streaming(&video->queue))
  763. return 0;
  764. if ((ret = uvc_init_video(video, GFP_NOIO)) < 0)
  765. uvc_queue_enable(&video->queue, 0);
  766. return ret;
  767. }
  768. /* ------------------------------------------------------------------------
  769. * Video device
  770. */
  771. /*
  772. * Initialize the UVC video device by retrieving the default format and
  773. * committing it.
  774. *
  775. * Some cameras (namely the Fuji Finepix) set the format and frame
  776. * indexes to zero. The UVC standard doesn't clearly make this a spec
  777. * violation, so try to silently fix the values if possible.
  778. *
  779. * This function is called before registering the device with V4L.
  780. */
  781. int uvc_video_init(struct uvc_video_device *video)
  782. {
  783. struct uvc_streaming_control *probe = &video->streaming->ctrl;
  784. struct uvc_format *format = NULL;
  785. struct uvc_frame *frame = NULL;
  786. unsigned int i;
  787. int ret;
  788. if (video->streaming->nformats == 0) {
  789. uvc_printk(KERN_INFO, "No supported video formats found.\n");
  790. return -EINVAL;
  791. }
  792. /* Alternate setting 0 should be the default, yet the XBox Live Vision
  793. * Cam (and possibly other devices) crash or otherwise misbehave if
  794. * they don't receive a SET_INTERFACE request before any other video
  795. * control request.
  796. */
  797. usb_set_interface(video->dev->udev, video->streaming->intfnum, 0);
  798. /* Some webcams don't suport GET_DEF request on the probe control. We
  799. * fall back to GET_CUR if GET_DEF fails.
  800. */
  801. if ((ret = uvc_get_video_ctrl(video, probe, 1, GET_DEF)) < 0 &&
  802. (ret = uvc_get_video_ctrl(video, probe, 1, GET_CUR)) < 0)
  803. return ret;
  804. /* Check if the default format descriptor exists. Use the first
  805. * available format otherwise.
  806. */
  807. for (i = video->streaming->nformats; i > 0; --i) {
  808. format = &video->streaming->format[i-1];
  809. if (format->index == probe->bFormatIndex)
  810. break;
  811. }
  812. if (format->nframes == 0) {
  813. uvc_printk(KERN_INFO, "No frame descriptor found for the "
  814. "default format.\n");
  815. return -EINVAL;
  816. }
  817. /* Zero bFrameIndex might be correct. Stream-based formats (including
  818. * MPEG-2 TS and DV) do not support frames but have a dummy frame
  819. * descriptor with bFrameIndex set to zero. If the default frame
  820. * descriptor is not found, use the first avalable frame.
  821. */
  822. for (i = format->nframes; i > 0; --i) {
  823. frame = &format->frame[i-1];
  824. if (frame->bFrameIndex == probe->bFrameIndex)
  825. break;
  826. }
  827. probe->bFormatIndex = format->index;
  828. probe->bFrameIndex = frame->bFrameIndex;
  829. video->streaming->cur_format = format;
  830. video->streaming->cur_frame = frame;
  831. atomic_set(&video->active, 0);
  832. /* Select the video decoding function */
  833. if (video->dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)
  834. video->decode = uvc_video_decode_isight;
  835. else if (video->streaming->intf->num_altsetting > 1)
  836. video->decode = uvc_video_decode_isoc;
  837. else
  838. video->decode = uvc_video_decode_bulk;
  839. return 0;
  840. }
  841. /*
  842. * Enable or disable the video stream.
  843. */
  844. int uvc_video_enable(struct uvc_video_device *video, int enable)
  845. {
  846. int ret;
  847. if (!enable) {
  848. uvc_uninit_video(video, 1);
  849. usb_set_interface(video->dev->udev,
  850. video->streaming->intfnum, 0);
  851. uvc_queue_enable(&video->queue, 0);
  852. return 0;
  853. }
  854. if ((video->streaming->cur_format->flags & UVC_FMT_FLAG_COMPRESSED) ||
  855. uvc_no_drop_param)
  856. video->queue.flags &= ~UVC_QUEUE_DROP_INCOMPLETE;
  857. else
  858. video->queue.flags |= UVC_QUEUE_DROP_INCOMPLETE;
  859. if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
  860. return ret;
  861. /* Commit the streaming parameters. */
  862. if ((ret = uvc_commit_video(video, &video->streaming->ctrl)) < 0)
  863. return ret;
  864. return uvc_init_video(video, GFP_KERNEL);
  865. }