uvc_isight.c 3.8 KB

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