uvc_video.c 32 KB

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