uvc_video.c 28 KB

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