445c4d39aeffea7022d9146ad410b3eeec66d81b
[linux-2.6-microblaze.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 /* 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
34 static int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
35                 const u8 *data, unsigned int len)
36 {
37         static const u8 hdr[] = {
38                 0x11, 0x22, 0x33, 0x44,
39                 0xde, 0xad, 0xbe, 0xef,
40                 0xde, 0xad, 0xfa, 0xce
41         };
42
43         struct uvc_streaming *stream = uvc_queue_to_stream(queue);
44         unsigned int maxlen, nbytes;
45         u8 *mem;
46         int is_header = 0;
47
48         if (buf == NULL)
49                 return 0;
50
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");
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_trace(stream->dev, UVC_TRACE_FRAME,
62                                   "Dropping packet (out of sync).\n");
63                         return 0;
64                 }
65
66                 buf->state = UVC_BUF_STATE_ACTIVE;
67         }
68
69         /* Mark the buffer as done if we're at the beginning of a new frame.
70          *
71          * Empty buffers (bytesused == 0) don't trigger end of frame detection
72          * as it doesn't make sense to return an empty buffer.
73          */
74         if (is_header && buf->bytesused != 0) {
75                 buf->state = UVC_BUF_STATE_DONE;
76                 return -EAGAIN;
77         }
78
79         /* Copy the video data to the buffer. Skip header packets, as they
80          * contain no data.
81          */
82         if (!is_header) {
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;
88
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;
93                 }
94         }
95
96         return 0;
97 }
98
99 void uvc_video_decode_isight(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
100                         struct uvc_buffer *meta_buf)
101 {
102         struct urb *urb = uvc_urb->urb;
103         struct uvc_streaming *stream = uvc_urb->stream;
104         int ret, i;
105
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);
111                 }
112
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.
120                  */
121                 do {
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);
126
127                         if (buf == NULL)
128                                 break;
129
130                         if (buf->state == UVC_BUF_STATE_DONE ||
131                             buf->state == UVC_BUF_STATE_ERROR)
132                                 buf = uvc_queue_next_buffer(&stream->queue,
133                                                         buf);
134                 } while (ret == -EAGAIN);
135         }
136 }