uvc_isight.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * uvc_isight.c -- USB Video Class driver - iSight support
  4. *
  5. * Copyright (C) 2006-2007
  6. * Ivan N. Zlatev <[email protected]>
  7. * Copyright (C) 2008-2009
  8. * Laurent Pinchart <[email protected]>
  9. */
  10. #include <linux/usb.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include "uvcvideo.h"
  14. /*
  15. * Built-in iSight webcams implements most of UVC 1.0 except a
  16. * different packet format. Instead of sending a header at the
  17. * beginning of each isochronous transfer payload, the webcam sends a
  18. * single header per image (on its own in a packet), followed by
  19. * packets containing data only.
  20. *
  21. * Offset Size (bytes) Description
  22. * ------------------------------------------------------------------
  23. * 0x00 1 Header length
  24. * 0x01 1 Flags (UVC-compliant)
  25. * 0x02 4 Always equal to '11223344'
  26. * 0x06 8 Always equal to 'deadbeefdeadface'
  27. * 0x0e 16 Unknown
  28. *
  29. * The header can be prefixed by an optional, unknown-purpose byte.
  30. */
  31. static int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
  32. const u8 *data, unsigned int len)
  33. {
  34. static const u8 hdr[] = {
  35. 0x11, 0x22, 0x33, 0x44,
  36. 0xde, 0xad, 0xbe, 0xef,
  37. 0xde, 0xad, 0xfa, 0xce
  38. };
  39. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  40. unsigned int maxlen, nbytes;
  41. u8 *mem;
  42. int is_header = 0;
  43. if (buf == NULL)
  44. return 0;
  45. if ((len >= 14 && memcmp(&data[2], hdr, 12) == 0) ||
  46. (len >= 15 && memcmp(&data[3], hdr, 12) == 0)) {
  47. uvc_dbg(stream->dev, FRAME, "iSight header found\n");
  48. is_header = 1;
  49. }
  50. /* Synchronize to the input stream by waiting for a header packet. */
  51. if (buf->state != UVC_BUF_STATE_ACTIVE) {
  52. if (!is_header) {
  53. uvc_dbg(stream->dev, FRAME,
  54. "Dropping packet (out of sync)\n");
  55. return 0;
  56. }
  57. buf->state = UVC_BUF_STATE_ACTIVE;
  58. }
  59. /*
  60. * Mark the buffer as done if we're at the beginning of a new frame.
  61. *
  62. * Empty buffers (bytesused == 0) don't trigger end of frame detection
  63. * as it doesn't make sense to return an empty buffer.
  64. */
  65. if (is_header && buf->bytesused != 0) {
  66. buf->state = UVC_BUF_STATE_DONE;
  67. return -EAGAIN;
  68. }
  69. /*
  70. * Copy the video data to the buffer. Skip header packets, as they
  71. * contain no data.
  72. */
  73. if (!is_header) {
  74. maxlen = buf->length - buf->bytesused;
  75. mem = buf->mem + buf->bytesused;
  76. nbytes = min(len, maxlen);
  77. memcpy(mem, data, nbytes);
  78. buf->bytesused += nbytes;
  79. if (len > maxlen || buf->bytesused == buf->length) {
  80. uvc_dbg(stream->dev, FRAME,
  81. "Frame complete (overflow)\n");
  82. buf->state = UVC_BUF_STATE_DONE;
  83. }
  84. }
  85. return 0;
  86. }
  87. void uvc_video_decode_isight(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
  88. struct uvc_buffer *meta_buf)
  89. {
  90. struct urb *urb = uvc_urb->urb;
  91. struct uvc_streaming *stream = uvc_urb->stream;
  92. int ret, i;
  93. for (i = 0; i < urb->number_of_packets; ++i) {
  94. if (urb->iso_frame_desc[i].status < 0) {
  95. uvc_dbg(stream->dev, FRAME,
  96. "USB isochronous frame lost (%d)\n",
  97. urb->iso_frame_desc[i].status);
  98. }
  99. /*
  100. * Decode the payload packet.
  101. *
  102. * uvc_video_decode is entered twice when a frame transition
  103. * has been detected because the end of frame can only be
  104. * reliably detected when the first packet of the new frame
  105. * is processed. The first pass detects the transition and
  106. * closes the previous frame's buffer, the second pass
  107. * processes the data of the first payload of the new frame.
  108. */
  109. do {
  110. ret = isight_decode(&stream->queue, buf,
  111. urb->transfer_buffer +
  112. urb->iso_frame_desc[i].offset,
  113. urb->iso_frame_desc[i].actual_length);
  114. if (buf == NULL)
  115. break;
  116. if (buf->state == UVC_BUF_STATE_DONE ||
  117. buf->state == UVC_BUF_STATE_ERROR)
  118. buf = uvc_queue_next_buffer(&stream->queue,
  119. buf);
  120. } while (ret == -EAGAIN);
  121. }
  122. }