18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * SN9C2028 common functions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn,edu>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based closely upon the file gspca/pac_common.h
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic const unsigned char sn9c2028_sof_marker[] = {
118c2ecf20Sopenharmony_ci	0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96,
128c2ecf20Sopenharmony_ci	0x00,
138c2ecf20Sopenharmony_ci	0x00, /* seq */
148c2ecf20Sopenharmony_ci	0x00,
158c2ecf20Sopenharmony_ci	0x00,
168c2ecf20Sopenharmony_ci	0x00, /* avg luminance lower 8 bit */
178c2ecf20Sopenharmony_ci	0x00, /* avg luminance higher 8 bit */
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic unsigned char *sn9c2028_find_sof(struct gspca_dev *gspca_dev,
218c2ecf20Sopenharmony_ci					unsigned char *m, int len)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
248c2ecf20Sopenharmony_ci	int i;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	/* Search for the SOF marker (fixed part) in the header */
278c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
288c2ecf20Sopenharmony_ci		if ((m[i] == sn9c2028_sof_marker[sd->sof_read]) ||
298c2ecf20Sopenharmony_ci		    (sd->sof_read > 5)) {
308c2ecf20Sopenharmony_ci			sd->sof_read++;
318c2ecf20Sopenharmony_ci			if (sd->sof_read == 11)
328c2ecf20Sopenharmony_ci				sd->avg_lum_l = m[i];
338c2ecf20Sopenharmony_ci			if (sd->sof_read == 12)
348c2ecf20Sopenharmony_ci				sd->avg_lum = (m[i] << 8) + sd->avg_lum_l;
358c2ecf20Sopenharmony_ci			if (sd->sof_read == sizeof(sn9c2028_sof_marker)) {
368c2ecf20Sopenharmony_ci				gspca_dbg(gspca_dev, D_FRAM,
378c2ecf20Sopenharmony_ci					  "SOF found, bytes to analyze: %u - Frame starts at byte #%u\n",
388c2ecf20Sopenharmony_ci					  len, i + 1);
398c2ecf20Sopenharmony_ci				sd->sof_read = 0;
408c2ecf20Sopenharmony_ci				return m + i + 1;
418c2ecf20Sopenharmony_ci			}
428c2ecf20Sopenharmony_ci		} else {
438c2ecf20Sopenharmony_ci			sd->sof_read = 0;
448c2ecf20Sopenharmony_ci		}
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	return NULL;
488c2ecf20Sopenharmony_ci}
49