uvc_isight.c 3.8 KB

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