uvc_video.c 28 KB

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