1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * uvc_isight.c -- USB Video Class driver - iSight support
5 * Copyright (C) 2006-2007
6 * Ivan N. Zlatev <contact@i-nz.net>
7 * Copyright (C) 2008-2009
8 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 #include <linux/usb.h>
12 #include <linux/kernel.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.
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'
31 * The header can be prefixed by an optional, unknown-purpose byte.
34 static int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
35 const u8 *data, unsigned int len)
37 static const u8 hdr[] = {
38 0x11, 0x22, 0x33, 0x44,
39 0xde, 0xad, 0xbe, 0xef,
40 0xde, 0xad, 0xfa, 0xce
43 struct uvc_streaming *stream = uvc_queue_to_stream(queue);
44 unsigned int maxlen, nbytes;
51 if ((len >= 14 && memcmp(&data[2], hdr, 12) == 0) ||
52 (len >= 15 && memcmp(&data[3], hdr, 12) == 0)) {
53 uvc_trace(stream->dev, UVC_TRACE_FRAME,
54 "iSight header found\n");
58 /* Synchronize to the input stream by waiting for a header packet. */
59 if (buf->state != UVC_BUF_STATE_ACTIVE) {
61 uvc_trace(stream->dev, UVC_TRACE_FRAME,
62 "Dropping packet (out of sync).\n");
66 buf->state = UVC_BUF_STATE_ACTIVE;
69 /* Mark the buffer as done if we're at the beginning of a new frame.
71 * Empty buffers (bytesused == 0) don't trigger end of frame detection
72 * as it doesn't make sense to return an empty buffer.
74 if (is_header && buf->bytesused != 0) {
75 buf->state = UVC_BUF_STATE_DONE;
79 /* Copy the video data to the buffer. Skip header packets, as they
83 maxlen = buf->length - buf->bytesused;
84 mem = buf->mem + buf->bytesused;
85 nbytes = min(len, maxlen);
86 memcpy(mem, data, nbytes);
87 buf->bytesused += nbytes;
89 if (len > maxlen || buf->bytesused == buf->length) {
90 uvc_trace(stream->dev, UVC_TRACE_FRAME,
91 "Frame complete (overflow).\n");
92 buf->state = UVC_BUF_STATE_DONE;
99 void uvc_video_decode_isight(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
100 struct uvc_buffer *meta_buf)
102 struct urb *urb = uvc_urb->urb;
103 struct uvc_streaming *stream = uvc_urb->stream;
106 for (i = 0; i < urb->number_of_packets; ++i) {
107 if (urb->iso_frame_desc[i].status < 0) {
108 uvc_trace(stream->dev, UVC_TRACE_FRAME,
109 "USB isochronous frame lost (%d).\n",
110 urb->iso_frame_desc[i].status);
113 /* Decode the payload packet.
114 * uvc_video_decode is entered twice when a frame transition
115 * has been detected because the end of frame can only be
116 * reliably detected when the first packet of the new frame
117 * is processed. The first pass detects the transition and
118 * closes the previous frame's buffer, the second pass
119 * processes the data of the first payload of the new frame.
122 ret = isight_decode(&stream->queue, buf,
123 urb->transfer_buffer +
124 urb->iso_frame_desc[i].offset,
125 urb->iso_frame_desc[i].actual_length);
130 if (buf->state == UVC_BUF_STATE_DONE ||
131 buf->state == UVC_BUF_STATE_ERROR)
132 buf = uvc_queue_next_buffer(&stream->queue,
134 } while (ret == -EAGAIN);