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>
18 * Built-in iSight webcams implements most of UVC 1.0 except a
19 * different packet format. Instead of sending a header at the
20 * beginning of each isochronous transfer payload, the webcam sends a
21 * single header per image (on its own in a packet), followed by
22 * packets containing data only.
24 * Offset Size (bytes) Description
25 * ------------------------------------------------------------------
26 * 0x00 1 Header length
27 * 0x01 1 Flags (UVC-compliant)
28 * 0x02 4 Always equal to '11223344'
29 * 0x06 8 Always equal to 'deadbeefdeadface'
32 * The header can be prefixed by an optional, unknown-purpose byte.
35 static int isight_decode(struct uvc_video_queue
*queue
, struct uvc_buffer
*buf
,
36 const u8
*data
, unsigned int len
)
38 static const u8 hdr
[] = {
39 0x11, 0x22, 0x33, 0x44,
40 0xde, 0xad, 0xbe, 0xef,
41 0xde, 0xad, 0xfa, 0xce
44 struct uvc_streaming
*stream
= uvc_queue_to_stream(queue
);
45 unsigned int maxlen
, nbytes
;
52 if ((len
>= 14 && memcmp(&data
[2], hdr
, 12) == 0) ||
53 (len
>= 15 && memcmp(&data
[3], hdr
, 12) == 0)) {
54 uvc_dbg(stream
->dev
, FRAME
, "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_dbg(stream
->dev
, FRAME
,
62 "Dropping packet (out of sync)\n");
66 buf
->state
= UVC_BUF_STATE_ACTIVE
;
70 * Mark the buffer as done if we're at the beginning of a new frame.
72 * Empty buffers (bytesused == 0) don't trigger end of frame detection
73 * as it doesn't make sense to return an empty buffer.
75 if (is_header
&& buf
->bytesused
!= 0) {
76 buf
->state
= UVC_BUF_STATE_DONE
;
81 * Copy the video data to the buffer. Skip header packets, as they
85 maxlen
= buf
->length
- buf
->bytesused
;
86 mem
= buf
->mem
+ buf
->bytesused
;
87 nbytes
= min(len
, maxlen
);
88 memcpy(mem
, data
, nbytes
);
89 buf
->bytesused
+= nbytes
;
91 if (len
> maxlen
|| buf
->bytesused
== buf
->length
) {
92 uvc_dbg(stream
->dev
, FRAME
,
93 "Frame complete (overflow)\n");
94 buf
->state
= UVC_BUF_STATE_DONE
;
101 void uvc_video_decode_isight(struct uvc_urb
*uvc_urb
, struct uvc_buffer
*buf
,
102 struct uvc_buffer
*meta_buf
)
104 struct urb
*urb
= uvc_urb
->urb
;
105 struct uvc_streaming
*stream
= uvc_urb
->stream
;
108 for (i
= 0; i
< urb
->number_of_packets
; ++i
) {
109 if (urb
->iso_frame_desc
[i
].status
< 0) {
110 uvc_dbg(stream
->dev
, FRAME
,
111 "USB isochronous frame lost (%d)\n",
112 urb
->iso_frame_desc
[i
].status
);
116 * Decode the payload packet.
118 * uvc_video_decode is entered twice when a frame transition
119 * has been detected because the end of frame can only be
120 * reliably detected when the first packet of the new frame
121 * is processed. The first pass detects the transition and
122 * closes the previous frame's buffer, the second pass
123 * processes the data of the first payload of the new frame.
126 ret
= isight_decode(&stream
->queue
, buf
,
127 urb
->transfer_buffer
+
128 urb
->iso_frame_desc
[i
].offset
,
129 urb
->iso_frame_desc
[i
].actual_length
);
134 if (buf
->state
== UVC_BUF_STATE_DONE
||
135 buf
->state
== UVC_BUF_STATE_ERROR
)
136 buf
= uvc_queue_next_buffer(&stream
->queue
,
138 } while (ret
== -EAGAIN
);