GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / media / usb / uvc / uvc_isight.c
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 <contact@i-nz.net>
7  *      Copyright (C) 2008-2009
8  *              Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9  */
10
11 #include <linux/usb.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14
15 #include "uvcvideo.h"
16
17 /*
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.
23  *
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'
30  * 0x0e 16      Unknown
31  *
32  * The header can be prefixed by an optional, unknown-purpose byte.
33  */
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
44         struct uvc_streaming *stream = uvc_queue_to_stream(queue);
45         unsigned int maxlen, nbytes;
46         u8 *mem;
47         int is_header = 0;
48
49         if (buf == NULL)
50                 return 0;
51
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");
55                 is_header = 1;
56         }
57
58         /* Synchronize to the input stream by waiting for a header packet. */
59         if (buf->state != UVC_BUF_STATE_ACTIVE) {
60                 if (!is_header) {
61                         uvc_dbg(stream->dev, FRAME,
62                                 "Dropping packet (out of sync)\n");
63                         return 0;
64                 }
65
66                 buf->state = UVC_BUF_STATE_ACTIVE;
67         }
68
69         /*
70          * Mark the buffer as done if we're at the beginning of a new frame.
71          *
72          * Empty buffers (bytesused == 0) don't trigger end of frame detection
73          * as it doesn't make sense to return an empty buffer.
74          */
75         if (is_header && buf->bytesused != 0) {
76                 buf->state = UVC_BUF_STATE_DONE;
77                 return -EAGAIN;
78         }
79
80         /*
81          * Copy the video data to the buffer. Skip header packets, as they
82          * contain no data.
83          */
84         if (!is_header) {
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;
90
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;
95                 }
96         }
97
98         return 0;
99 }
100
101 void uvc_video_decode_isight(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
102                         struct uvc_buffer *meta_buf)
103 {
104         struct urb *urb = uvc_urb->urb;
105         struct uvc_streaming *stream = uvc_urb->stream;
106         int ret, i;
107
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);
113                 }
114
115                 /*
116                  * Decode the payload packet.
117                  *
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.
124                  */
125                 do {
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);
130
131                         if (buf == NULL)
132                                 break;
133
134                         if (buf->state == UVC_BUF_STATE_DONE ||
135                             buf->state == UVC_BUF_STATE_ERROR)
136                                 buf = uvc_queue_next_buffer(&stream->queue,
137                                                         buf);
138                 } while (ret == -EAGAIN);
139         }
140 }