18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *      uvc_isight.c  --  USB Video Class driver - iSight support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	Copyright (C) 2006-2007
68c2ecf20Sopenharmony_ci *		Ivan N. Zlatev <contact@i-nz.net>
78c2ecf20Sopenharmony_ci *	Copyright (C) 2008-2009
88c2ecf20Sopenharmony_ci *		Laurent Pinchart <laurent.pinchart@ideasonboard.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/usb.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/mm.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "uvcvideo.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* Built-in iSight webcams implements most of UVC 1.0 except a
188c2ecf20Sopenharmony_ci * different packet format. Instead of sending a header at the
198c2ecf20Sopenharmony_ci * beginning of each isochronous transfer payload, the webcam sends a
208c2ecf20Sopenharmony_ci * single header per image (on its own in a packet), followed by
218c2ecf20Sopenharmony_ci * packets containing data only.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Offset   Size (bytes)	Description
248c2ecf20Sopenharmony_ci * ------------------------------------------------------------------
258c2ecf20Sopenharmony_ci * 0x00	1	Header length
268c2ecf20Sopenharmony_ci * 0x01	1	Flags (UVC-compliant)
278c2ecf20Sopenharmony_ci * 0x02	4	Always equal to '11223344'
288c2ecf20Sopenharmony_ci * 0x06	8	Always equal to 'deadbeefdeadface'
298c2ecf20Sopenharmony_ci * 0x0e	16	Unknown
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * The header can be prefixed by an optional, unknown-purpose byte.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int isight_decode(struct uvc_video_queue *queue, struct uvc_buffer *buf,
358c2ecf20Sopenharmony_ci		const u8 *data, unsigned int len)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	static const u8 hdr[] = {
388c2ecf20Sopenharmony_ci		0x11, 0x22, 0x33, 0x44,
398c2ecf20Sopenharmony_ci		0xde, 0xad, 0xbe, 0xef,
408c2ecf20Sopenharmony_ci		0xde, 0xad, 0xfa, 0xce
418c2ecf20Sopenharmony_ci	};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	unsigned int maxlen, nbytes;
448c2ecf20Sopenharmony_ci	u8 *mem;
458c2ecf20Sopenharmony_ci	int is_header = 0;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (buf == NULL)
488c2ecf20Sopenharmony_ci		return 0;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if ((len >= 14 && memcmp(&data[2], hdr, 12) == 0) ||
518c2ecf20Sopenharmony_ci	    (len >= 15 && memcmp(&data[3], hdr, 12) == 0)) {
528c2ecf20Sopenharmony_ci		uvc_trace(UVC_TRACE_FRAME, "iSight header found\n");
538c2ecf20Sopenharmony_ci		is_header = 1;
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	/* Synchronize to the input stream by waiting for a header packet. */
578c2ecf20Sopenharmony_ci	if (buf->state != UVC_BUF_STATE_ACTIVE) {
588c2ecf20Sopenharmony_ci		if (!is_header) {
598c2ecf20Sopenharmony_ci			uvc_trace(UVC_TRACE_FRAME, "Dropping packet (out of "
608c2ecf20Sopenharmony_ci				  "sync).\n");
618c2ecf20Sopenharmony_ci			return 0;
628c2ecf20Sopenharmony_ci		}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		buf->state = UVC_BUF_STATE_ACTIVE;
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* Mark the buffer as done if we're at the beginning of a new frame.
688c2ecf20Sopenharmony_ci	 *
698c2ecf20Sopenharmony_ci	 * Empty buffers (bytesused == 0) don't trigger end of frame detection
708c2ecf20Sopenharmony_ci	 * as it doesn't make sense to return an empty buffer.
718c2ecf20Sopenharmony_ci	 */
728c2ecf20Sopenharmony_ci	if (is_header && buf->bytesused != 0) {
738c2ecf20Sopenharmony_ci		buf->state = UVC_BUF_STATE_DONE;
748c2ecf20Sopenharmony_ci		return -EAGAIN;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* Copy the video data to the buffer. Skip header packets, as they
788c2ecf20Sopenharmony_ci	 * contain no data.
798c2ecf20Sopenharmony_ci	 */
808c2ecf20Sopenharmony_ci	if (!is_header) {
818c2ecf20Sopenharmony_ci		maxlen = buf->length - buf->bytesused;
828c2ecf20Sopenharmony_ci		mem = buf->mem + buf->bytesused;
838c2ecf20Sopenharmony_ci		nbytes = min(len, maxlen);
848c2ecf20Sopenharmony_ci		memcpy(mem, data, nbytes);
858c2ecf20Sopenharmony_ci		buf->bytesused += nbytes;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci		if (len > maxlen || buf->bytesused == buf->length) {
888c2ecf20Sopenharmony_ci			uvc_trace(UVC_TRACE_FRAME, "Frame complete "
898c2ecf20Sopenharmony_ci				  "(overflow).\n");
908c2ecf20Sopenharmony_ci			buf->state = UVC_BUF_STATE_DONE;
918c2ecf20Sopenharmony_ci		}
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return 0;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_civoid uvc_video_decode_isight(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
988c2ecf20Sopenharmony_ci			struct uvc_buffer *meta_buf)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct urb *urb = uvc_urb->urb;
1018c2ecf20Sopenharmony_ci	struct uvc_streaming *stream = uvc_urb->stream;
1028c2ecf20Sopenharmony_ci	int ret, i;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	for (i = 0; i < urb->number_of_packets; ++i) {
1058c2ecf20Sopenharmony_ci		if (urb->iso_frame_desc[i].status < 0) {
1068c2ecf20Sopenharmony_ci			uvc_trace(UVC_TRACE_FRAME, "USB isochronous frame "
1078c2ecf20Sopenharmony_ci				  "lost (%d).\n",
1088c2ecf20Sopenharmony_ci				  urb->iso_frame_desc[i].status);
1098c2ecf20Sopenharmony_ci		}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci		/* Decode the payload packet.
1128c2ecf20Sopenharmony_ci		 * uvc_video_decode is entered twice when a frame transition
1138c2ecf20Sopenharmony_ci		 * has been detected because the end of frame can only be
1148c2ecf20Sopenharmony_ci		 * reliably detected when the first packet of the new frame
1158c2ecf20Sopenharmony_ci		 * is processed. The first pass detects the transition and
1168c2ecf20Sopenharmony_ci		 * closes the previous frame's buffer, the second pass
1178c2ecf20Sopenharmony_ci		 * processes the data of the first payload of the new frame.
1188c2ecf20Sopenharmony_ci		 */
1198c2ecf20Sopenharmony_ci		do {
1208c2ecf20Sopenharmony_ci			ret = isight_decode(&stream->queue, buf,
1218c2ecf20Sopenharmony_ci					urb->transfer_buffer +
1228c2ecf20Sopenharmony_ci					urb->iso_frame_desc[i].offset,
1238c2ecf20Sopenharmony_ci					urb->iso_frame_desc[i].actual_length);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci			if (buf == NULL)
1268c2ecf20Sopenharmony_ci				break;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci			if (buf->state == UVC_BUF_STATE_DONE ||
1298c2ecf20Sopenharmony_ci			    buf->state == UVC_BUF_STATE_ERROR)
1308c2ecf20Sopenharmony_ci				buf = uvc_queue_next_buffer(&stream->queue,
1318c2ecf20Sopenharmony_ci							buf);
1328c2ecf20Sopenharmony_ci		} while (ret == -EAGAIN);
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci}
135