uvc_video.c 27 KB

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