uvc_video.c 31 KB

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