uvc_video.c 27 KB

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