18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Motion Eye video4linux driver for Sony Vaio PictureBook
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2001-2004 Stelian Pop <stelian@popies.net>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Some parts borrowed from various video4linux drivers, especially
148c2ecf20Sopenharmony_ci * bttv-driver.c and zoran.c, see original files for credits.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/pci.h>
188c2ecf20Sopenharmony_ci#include <linux/sched.h>
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/gfp.h>
218c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
228c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
238c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
248c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
258c2ecf20Sopenharmony_ci#include <media/v4l2-fh.h>
268c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
278c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
288c2ecf20Sopenharmony_ci#include <asm/io.h>
298c2ecf20Sopenharmony_ci#include <linux/delay.h>
308c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
318c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
328c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include "meye.h"
358c2ecf20Sopenharmony_ci#include <linux/meye.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
388c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("v4l2 driver for the MotionEye camera");
398c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
408c2ecf20Sopenharmony_ciMODULE_VERSION(MEYE_DRIVER_VERSION);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* number of grab buffers */
438c2ecf20Sopenharmony_cistatic unsigned int gbuffers = 2;
448c2ecf20Sopenharmony_cimodule_param(gbuffers, int, 0444);
458c2ecf20Sopenharmony_ciMODULE_PARM_DESC(gbuffers, "number of capture buffers, default is 2 (32 max)");
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* size of a grab buffer */
488c2ecf20Sopenharmony_cistatic unsigned int gbufsize = MEYE_MAX_BUFSIZE;
498c2ecf20Sopenharmony_cimodule_param(gbufsize, int, 0444);
508c2ecf20Sopenharmony_ciMODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 614400 (will be rounded up to a page multiple)");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* /dev/videoX registration number */
538c2ecf20Sopenharmony_cistatic int video_nr = -1;
548c2ecf20Sopenharmony_cimodule_param(video_nr, int, 0444);
558c2ecf20Sopenharmony_ciMODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* driver structure - only one possible */
588c2ecf20Sopenharmony_cistatic struct meye meye;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/****************************************************************************/
618c2ecf20Sopenharmony_ci/* Memory allocation routines (stolen from bttv-driver.c)                   */
628c2ecf20Sopenharmony_ci/****************************************************************************/
638c2ecf20Sopenharmony_cistatic void *rvmalloc(unsigned long size)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	void *mem;
668c2ecf20Sopenharmony_ci	unsigned long adr;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	size = PAGE_ALIGN(size);
698c2ecf20Sopenharmony_ci	mem = vmalloc_32(size);
708c2ecf20Sopenharmony_ci	if (mem) {
718c2ecf20Sopenharmony_ci		memset(mem, 0, size);
728c2ecf20Sopenharmony_ci		adr = (unsigned long) mem;
738c2ecf20Sopenharmony_ci		while (size > 0) {
748c2ecf20Sopenharmony_ci			SetPageReserved(vmalloc_to_page((void *)adr));
758c2ecf20Sopenharmony_ci			adr += PAGE_SIZE;
768c2ecf20Sopenharmony_ci			size -= PAGE_SIZE;
778c2ecf20Sopenharmony_ci		}
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	return mem;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void rvfree(void * mem, unsigned long size)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	unsigned long adr;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (mem) {
878c2ecf20Sopenharmony_ci		adr = (unsigned long) mem;
888c2ecf20Sopenharmony_ci		while ((long) size > 0) {
898c2ecf20Sopenharmony_ci			ClearPageReserved(vmalloc_to_page((void *)adr));
908c2ecf20Sopenharmony_ci			adr += PAGE_SIZE;
918c2ecf20Sopenharmony_ci			size -= PAGE_SIZE;
928c2ecf20Sopenharmony_ci		}
938c2ecf20Sopenharmony_ci		vfree(mem);
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/*
988c2ecf20Sopenharmony_ci * return a page table pointing to N pages of locked memory
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci * NOTE: The meye device expects DMA addresses on 32 bits, we build
1018c2ecf20Sopenharmony_ci * a table of 1024 entries = 4 bytes * 1024 = 4096 bytes.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_cistatic int ptable_alloc(void)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	u32 *pt;
1068c2ecf20Sopenharmony_ci	int i;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* give only 32 bit DMA addresses */
1118c2ecf20Sopenharmony_ci	if (dma_set_mask(&meye.mchip_dev->dev, DMA_BIT_MASK(32)))
1128c2ecf20Sopenharmony_ci		return -1;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	meye.mchip_ptable_toc = dma_alloc_coherent(&meye.mchip_dev->dev,
1158c2ecf20Sopenharmony_ci						   PAGE_SIZE,
1168c2ecf20Sopenharmony_ci						   &meye.mchip_dmahandle,
1178c2ecf20Sopenharmony_ci						   GFP_KERNEL);
1188c2ecf20Sopenharmony_ci	if (!meye.mchip_ptable_toc) {
1198c2ecf20Sopenharmony_ci		meye.mchip_dmahandle = 0;
1208c2ecf20Sopenharmony_ci		return -1;
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	pt = meye.mchip_ptable_toc;
1248c2ecf20Sopenharmony_ci	for (i = 0; i < MCHIP_NB_PAGES; i++) {
1258c2ecf20Sopenharmony_ci		dma_addr_t dma;
1268c2ecf20Sopenharmony_ci		meye.mchip_ptable[i] = dma_alloc_coherent(&meye.mchip_dev->dev,
1278c2ecf20Sopenharmony_ci							  PAGE_SIZE,
1288c2ecf20Sopenharmony_ci							  &dma,
1298c2ecf20Sopenharmony_ci							  GFP_KERNEL);
1308c2ecf20Sopenharmony_ci		if (!meye.mchip_ptable[i]) {
1318c2ecf20Sopenharmony_ci			int j;
1328c2ecf20Sopenharmony_ci			pt = meye.mchip_ptable_toc;
1338c2ecf20Sopenharmony_ci			for (j = 0; j < i; ++j) {
1348c2ecf20Sopenharmony_ci				dma = (dma_addr_t) *pt;
1358c2ecf20Sopenharmony_ci				dma_free_coherent(&meye.mchip_dev->dev,
1368c2ecf20Sopenharmony_ci						  PAGE_SIZE,
1378c2ecf20Sopenharmony_ci						  meye.mchip_ptable[j], dma);
1388c2ecf20Sopenharmony_ci				pt++;
1398c2ecf20Sopenharmony_ci			}
1408c2ecf20Sopenharmony_ci			dma_free_coherent(&meye.mchip_dev->dev,
1418c2ecf20Sopenharmony_ci					  PAGE_SIZE,
1428c2ecf20Sopenharmony_ci					  meye.mchip_ptable_toc,
1438c2ecf20Sopenharmony_ci					  meye.mchip_dmahandle);
1448c2ecf20Sopenharmony_ci			meye.mchip_ptable_toc = NULL;
1458c2ecf20Sopenharmony_ci			meye.mchip_dmahandle = 0;
1468c2ecf20Sopenharmony_ci			return -1;
1478c2ecf20Sopenharmony_ci		}
1488c2ecf20Sopenharmony_ci		*pt = (u32) dma;
1498c2ecf20Sopenharmony_ci		pt++;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	return 0;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic void ptable_free(void)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	u32 *pt;
1578c2ecf20Sopenharmony_ci	int i;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	pt = meye.mchip_ptable_toc;
1608c2ecf20Sopenharmony_ci	for (i = 0; i < MCHIP_NB_PAGES; i++) {
1618c2ecf20Sopenharmony_ci		dma_addr_t dma = (dma_addr_t) *pt;
1628c2ecf20Sopenharmony_ci		if (meye.mchip_ptable[i])
1638c2ecf20Sopenharmony_ci			dma_free_coherent(&meye.mchip_dev->dev,
1648c2ecf20Sopenharmony_ci					  PAGE_SIZE,
1658c2ecf20Sopenharmony_ci					  meye.mchip_ptable[i], dma);
1668c2ecf20Sopenharmony_ci		pt++;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (meye.mchip_ptable_toc)
1708c2ecf20Sopenharmony_ci		dma_free_coherent(&meye.mchip_dev->dev,
1718c2ecf20Sopenharmony_ci				  PAGE_SIZE,
1728c2ecf20Sopenharmony_ci				  meye.mchip_ptable_toc,
1738c2ecf20Sopenharmony_ci				  meye.mchip_dmahandle);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	memset(meye.mchip_ptable, 0, sizeof(meye.mchip_ptable));
1768c2ecf20Sopenharmony_ci	meye.mchip_ptable_toc = NULL;
1778c2ecf20Sopenharmony_ci	meye.mchip_dmahandle = 0;
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/* copy data from ptable into buf */
1818c2ecf20Sopenharmony_cistatic void ptable_copy(u8 *buf, int start, int size, int pt_pages)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	int i;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	for (i = 0; i < (size / PAGE_SIZE) * PAGE_SIZE; i += PAGE_SIZE) {
1868c2ecf20Sopenharmony_ci		memcpy(buf + i, meye.mchip_ptable[start++], PAGE_SIZE);
1878c2ecf20Sopenharmony_ci		if (start >= pt_pages)
1888c2ecf20Sopenharmony_ci			start = 0;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci	memcpy(buf + i, meye.mchip_ptable[start], size % PAGE_SIZE);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/****************************************************************************/
1948c2ecf20Sopenharmony_ci/* JPEG tables at different qualities to load into the VRJ chip             */
1958c2ecf20Sopenharmony_ci/****************************************************************************/
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/* return a set of quantisation tables based on a quality from 1 to 10 */
1988c2ecf20Sopenharmony_cistatic u16 *jpeg_quantisation_tables(int *length, int quality)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	static u16 jpeg_tables[][70] = { {
2018c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0xff00, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2028c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2038c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2048c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2058c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff,
2068c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0xff01, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2078c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2088c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2098c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2108c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff,
2118c2ecf20Sopenharmony_ci	},
2128c2ecf20Sopenharmony_ci	{
2138c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x5000, 0x3c37, 0x3c46, 0x5032, 0x4146, 0x5a46,
2148c2ecf20Sopenharmony_ci		0x5055, 0x785f, 0x82c8, 0x6e78, 0x786e, 0xaff5, 0x91b9, 0xffc8,
2158c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2168c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2178c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff,
2188c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x5501, 0x5a5a, 0x6978, 0xeb78, 0x8282, 0xffeb,
2198c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2208c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2218c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
2228c2ecf20Sopenharmony_ci		0xffff, 0xffff, 0xffff,
2238c2ecf20Sopenharmony_ci	},
2248c2ecf20Sopenharmony_ci	{
2258c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x2800, 0x1e1c, 0x1e23, 0x2819, 0x2123, 0x2d23,
2268c2ecf20Sopenharmony_ci		0x282b, 0x3c30, 0x4164, 0x373c, 0x3c37, 0x587b, 0x495d, 0x9164,
2278c2ecf20Sopenharmony_ci		0x9980, 0x8f96, 0x8c80, 0xa08a, 0xe6b4, 0xa0c3, 0xdaaa, 0x8aad,
2288c2ecf20Sopenharmony_ci		0xc88c, 0xcbff, 0xeeda, 0xfff5, 0xffff, 0xc19b, 0xffff, 0xfaff,
2298c2ecf20Sopenharmony_ci		0xe6ff, 0xfffd, 0xfff8,
2308c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x2b01, 0x2d2d, 0x353c, 0x763c, 0x4141, 0xf876,
2318c2ecf20Sopenharmony_ci		0x8ca5, 0xf8a5, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
2328c2ecf20Sopenharmony_ci		0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
2338c2ecf20Sopenharmony_ci		0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8, 0xf8f8,
2348c2ecf20Sopenharmony_ci		0xf8f8, 0xf8f8, 0xfff8,
2358c2ecf20Sopenharmony_ci	},
2368c2ecf20Sopenharmony_ci	{
2378c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x1b00, 0x1412, 0x1417, 0x1b11, 0x1617, 0x1e17,
2388c2ecf20Sopenharmony_ci		0x1b1c, 0x2820, 0x2b42, 0x2528, 0x2825, 0x3a51, 0x303d, 0x6042,
2398c2ecf20Sopenharmony_ci		0x6555, 0x5f64, 0x5d55, 0x6a5b, 0x9978, 0x6a81, 0x9071, 0x5b73,
2408c2ecf20Sopenharmony_ci		0x855d, 0x86b5, 0x9e90, 0xaba3, 0xabad, 0x8067, 0xc9bc, 0xa6ba,
2418c2ecf20Sopenharmony_ci		0x99c7, 0xaba8, 0xffa4,
2428c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x1c01, 0x1e1e, 0x2328, 0x4e28, 0x2b2b, 0xa44e,
2438c2ecf20Sopenharmony_ci		0x5d6e, 0xa46e, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
2448c2ecf20Sopenharmony_ci		0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
2458c2ecf20Sopenharmony_ci		0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4, 0xa4a4,
2468c2ecf20Sopenharmony_ci		0xa4a4, 0xa4a4, 0xffa4,
2478c2ecf20Sopenharmony_ci	},
2488c2ecf20Sopenharmony_ci	{
2498c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x1400, 0x0f0e, 0x0f12, 0x140d, 0x1012, 0x1712,
2508c2ecf20Sopenharmony_ci		0x1415, 0x1e18, 0x2132, 0x1c1e, 0x1e1c, 0x2c3d, 0x242e, 0x4932,
2518c2ecf20Sopenharmony_ci		0x4c40, 0x474b, 0x4640, 0x5045, 0x735a, 0x5062, 0x6d55, 0x4556,
2528c2ecf20Sopenharmony_ci		0x6446, 0x6588, 0x776d, 0x817b, 0x8182, 0x604e, 0x978d, 0x7d8c,
2538c2ecf20Sopenharmony_ci		0x7396, 0x817e, 0xff7c,
2548c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x1501, 0x1717, 0x1a1e, 0x3b1e, 0x2121, 0x7c3b,
2558c2ecf20Sopenharmony_ci		0x4653, 0x7c53, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
2568c2ecf20Sopenharmony_ci		0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
2578c2ecf20Sopenharmony_ci		0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c, 0x7c7c,
2588c2ecf20Sopenharmony_ci		0x7c7c, 0x7c7c, 0xff7c,
2598c2ecf20Sopenharmony_ci	},
2608c2ecf20Sopenharmony_ci	{
2618c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x1000, 0x0c0b, 0x0c0e, 0x100a, 0x0d0e, 0x120e,
2628c2ecf20Sopenharmony_ci		0x1011, 0x1813, 0x1a28, 0x1618, 0x1816, 0x2331, 0x1d25, 0x3a28,
2638c2ecf20Sopenharmony_ci		0x3d33, 0x393c, 0x3833, 0x4037, 0x5c48, 0x404e, 0x5744, 0x3745,
2648c2ecf20Sopenharmony_ci		0x5038, 0x516d, 0x5f57, 0x6762, 0x6768, 0x4d3e, 0x7971, 0x6470,
2658c2ecf20Sopenharmony_ci		0x5c78, 0x6765, 0xff63,
2668c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x1101, 0x1212, 0x1518, 0x2f18, 0x1a1a, 0x632f,
2678c2ecf20Sopenharmony_ci		0x3842, 0x6342, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
2688c2ecf20Sopenharmony_ci		0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
2698c2ecf20Sopenharmony_ci		0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363, 0x6363,
2708c2ecf20Sopenharmony_ci		0x6363, 0x6363, 0xff63,
2718c2ecf20Sopenharmony_ci	},
2728c2ecf20Sopenharmony_ci	{
2738c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0d00, 0x0a09, 0x0a0b, 0x0d08, 0x0a0b, 0x0e0b,
2748c2ecf20Sopenharmony_ci		0x0d0e, 0x130f, 0x1520, 0x1213, 0x1312, 0x1c27, 0x171e, 0x2e20,
2758c2ecf20Sopenharmony_ci		0x3129, 0x2e30, 0x2d29, 0x332c, 0x4a3a, 0x333e, 0x4636, 0x2c37,
2768c2ecf20Sopenharmony_ci		0x402d, 0x4157, 0x4c46, 0x524e, 0x5253, 0x3e32, 0x615a, 0x505a,
2778c2ecf20Sopenharmony_ci		0x4a60, 0x5251, 0xff4f,
2788c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0e01, 0x0e0e, 0x1113, 0x2613, 0x1515, 0x4f26,
2798c2ecf20Sopenharmony_ci		0x2d35, 0x4f35, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
2808c2ecf20Sopenharmony_ci		0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
2818c2ecf20Sopenharmony_ci		0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f, 0x4f4f,
2828c2ecf20Sopenharmony_ci		0x4f4f, 0x4f4f, 0xff4f,
2838c2ecf20Sopenharmony_ci	},
2848c2ecf20Sopenharmony_ci	{
2858c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0a00, 0x0707, 0x0708, 0x0a06, 0x0808, 0x0b08,
2868c2ecf20Sopenharmony_ci		0x0a0a, 0x0e0b, 0x1018, 0x0d0e, 0x0e0d, 0x151d, 0x1116, 0x2318,
2878c2ecf20Sopenharmony_ci		0x251f, 0x2224, 0x221f, 0x2621, 0x372b, 0x262f, 0x3429, 0x2129,
2888c2ecf20Sopenharmony_ci		0x3022, 0x3141, 0x3934, 0x3e3b, 0x3e3e, 0x2e25, 0x4944, 0x3c43,
2898c2ecf20Sopenharmony_ci		0x3748, 0x3e3d, 0xff3b,
2908c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0a01, 0x0b0b, 0x0d0e, 0x1c0e, 0x1010, 0x3b1c,
2918c2ecf20Sopenharmony_ci		0x2228, 0x3b28, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
2928c2ecf20Sopenharmony_ci		0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
2938c2ecf20Sopenharmony_ci		0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b, 0x3b3b,
2948c2ecf20Sopenharmony_ci		0x3b3b, 0x3b3b, 0xff3b,
2958c2ecf20Sopenharmony_ci	},
2968c2ecf20Sopenharmony_ci	{
2978c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0600, 0x0504, 0x0506, 0x0604, 0x0506, 0x0706,
2988c2ecf20Sopenharmony_ci		0x0607, 0x0a08, 0x0a10, 0x090a, 0x0a09, 0x0e14, 0x0c0f, 0x1710,
2998c2ecf20Sopenharmony_ci		0x1814, 0x1718, 0x1614, 0x1a16, 0x251d, 0x1a1f, 0x231b, 0x161c,
3008c2ecf20Sopenharmony_ci		0x2016, 0x202c, 0x2623, 0x2927, 0x292a, 0x1f19, 0x302d, 0x282d,
3018c2ecf20Sopenharmony_ci		0x2530, 0x2928, 0xff28,
3028c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0701, 0x0707, 0x080a, 0x130a, 0x0a0a, 0x2813,
3038c2ecf20Sopenharmony_ci		0x161a, 0x281a, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
3048c2ecf20Sopenharmony_ci		0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
3058c2ecf20Sopenharmony_ci		0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828, 0x2828,
3068c2ecf20Sopenharmony_ci		0x2828, 0x2828, 0xff28,
3078c2ecf20Sopenharmony_ci	},
3088c2ecf20Sopenharmony_ci	{
3098c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0300, 0x0202, 0x0203, 0x0302, 0x0303, 0x0403,
3108c2ecf20Sopenharmony_ci		0x0303, 0x0504, 0x0508, 0x0405, 0x0504, 0x070a, 0x0607, 0x0c08,
3118c2ecf20Sopenharmony_ci		0x0c0a, 0x0b0c, 0x0b0a, 0x0d0b, 0x120e, 0x0d10, 0x110e, 0x0b0e,
3128c2ecf20Sopenharmony_ci		0x100b, 0x1016, 0x1311, 0x1514, 0x1515, 0x0f0c, 0x1817, 0x1416,
3138c2ecf20Sopenharmony_ci		0x1218, 0x1514, 0xff14,
3148c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0301, 0x0404, 0x0405, 0x0905, 0x0505, 0x1409,
3158c2ecf20Sopenharmony_ci		0x0b0d, 0x140d, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
3168c2ecf20Sopenharmony_ci		0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
3178c2ecf20Sopenharmony_ci		0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414, 0x1414,
3188c2ecf20Sopenharmony_ci		0x1414, 0x1414, 0xff14,
3198c2ecf20Sopenharmony_ci	},
3208c2ecf20Sopenharmony_ci	{
3218c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0100, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3228c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3238c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3248c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3258c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0xff01,
3268c2ecf20Sopenharmony_ci		0xdbff, 0x4300, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3278c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3288c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3298c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
3308c2ecf20Sopenharmony_ci		0x0101, 0x0101, 0xff01,
3318c2ecf20Sopenharmony_ci	} };
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (quality < 0 || quality > 10) {
3348c2ecf20Sopenharmony_ci		printk(KERN_WARNING
3358c2ecf20Sopenharmony_ci		       "meye: invalid quality level %d - using 8\n", quality);
3368c2ecf20Sopenharmony_ci		quality = 8;
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	*length = ARRAY_SIZE(jpeg_tables[quality]);
3408c2ecf20Sopenharmony_ci	return jpeg_tables[quality];
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/* return a generic set of huffman tables */
3448c2ecf20Sopenharmony_cistatic u16 *jpeg_huffman_tables(int *length)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	static u16 tables[] = {
3478c2ecf20Sopenharmony_ci		0xC4FF, 0xB500, 0x0010, 0x0102, 0x0303, 0x0402, 0x0503, 0x0405,
3488c2ecf20Sopenharmony_ci		0x0004, 0x0100, 0x017D, 0x0302, 0x0400, 0x0511, 0x2112, 0x4131,
3498c2ecf20Sopenharmony_ci		0x1306, 0x6151, 0x2207, 0x1471, 0x8132, 0xA191, 0x2308, 0xB142,
3508c2ecf20Sopenharmony_ci		0x15C1, 0xD152, 0x24F0, 0x6233, 0x8272, 0x0A09, 0x1716, 0x1918,
3518c2ecf20Sopenharmony_ci		0x251A, 0x2726, 0x2928, 0x342A, 0x3635, 0x3837, 0x3A39, 0x4443,
3528c2ecf20Sopenharmony_ci		0x4645, 0x4847, 0x4A49, 0x5453, 0x5655, 0x5857, 0x5A59, 0x6463,
3538c2ecf20Sopenharmony_ci		0x6665, 0x6867, 0x6A69, 0x7473, 0x7675, 0x7877, 0x7A79, 0x8483,
3548c2ecf20Sopenharmony_ci		0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998, 0xA29A,
3558c2ecf20Sopenharmony_ci		0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6, 0xB9B8,
3568c2ecf20Sopenharmony_ci		0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4, 0xD7D6,
3578c2ecf20Sopenharmony_ci		0xD9D8, 0xE1DA, 0xE3E2, 0xE5E4, 0xE7E6, 0xE9E8, 0xF1EA, 0xF3F2,
3588c2ecf20Sopenharmony_ci		0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
3598c2ecf20Sopenharmony_ci		0xC4FF, 0xB500, 0x0011, 0x0102, 0x0402, 0x0304, 0x0704, 0x0405,
3608c2ecf20Sopenharmony_ci		0x0004, 0x0201, 0x0077, 0x0201, 0x1103, 0x0504, 0x3121, 0x1206,
3618c2ecf20Sopenharmony_ci		0x5141, 0x6107, 0x1371, 0x3222, 0x0881, 0x4214, 0xA191, 0xC1B1,
3628c2ecf20Sopenharmony_ci		0x2309, 0x5233, 0x15F0, 0x7262, 0x0AD1, 0x2416, 0xE134, 0xF125,
3638c2ecf20Sopenharmony_ci		0x1817, 0x1A19, 0x2726, 0x2928, 0x352A, 0x3736, 0x3938, 0x433A,
3648c2ecf20Sopenharmony_ci		0x4544, 0x4746, 0x4948, 0x534A, 0x5554, 0x5756, 0x5958, 0x635A,
3658c2ecf20Sopenharmony_ci		0x6564, 0x6766, 0x6968, 0x736A, 0x7574, 0x7776, 0x7978, 0x827A,
3668c2ecf20Sopenharmony_ci		0x8483, 0x8685, 0x8887, 0x8A89, 0x9392, 0x9594, 0x9796, 0x9998,
3678c2ecf20Sopenharmony_ci		0xA29A, 0xA4A3, 0xA6A5, 0xA8A7, 0xAAA9, 0xB3B2, 0xB5B4, 0xB7B6,
3688c2ecf20Sopenharmony_ci		0xB9B8, 0xC2BA, 0xC4C3, 0xC6C5, 0xC8C7, 0xCAC9, 0xD3D2, 0xD5D4,
3698c2ecf20Sopenharmony_ci		0xD7D6, 0xD9D8, 0xE2DA, 0xE4E3, 0xE6E5, 0xE8E7, 0xEAE9, 0xF3F2,
3708c2ecf20Sopenharmony_ci		0xF5F4, 0xF7F6, 0xF9F8, 0xFFFA,
3718c2ecf20Sopenharmony_ci		0xC4FF, 0x1F00, 0x0000, 0x0501, 0x0101, 0x0101, 0x0101, 0x0000,
3728c2ecf20Sopenharmony_ci		0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
3738c2ecf20Sopenharmony_ci		0xFF0B,
3748c2ecf20Sopenharmony_ci		0xC4FF, 0x1F00, 0x0001, 0x0103, 0x0101, 0x0101, 0x0101, 0x0101,
3758c2ecf20Sopenharmony_ci		0x0000, 0x0000, 0x0000, 0x0201, 0x0403, 0x0605, 0x0807, 0x0A09,
3768c2ecf20Sopenharmony_ci		0xFF0B
3778c2ecf20Sopenharmony_ci	};
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	*length = ARRAY_SIZE(tables);
3808c2ecf20Sopenharmony_ci	return tables;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci/****************************************************************************/
3848c2ecf20Sopenharmony_ci/* MCHIP low-level functions                                                */
3858c2ecf20Sopenharmony_ci/****************************************************************************/
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci/* returns the horizontal capture size */
3888c2ecf20Sopenharmony_cistatic inline int mchip_hsize(void)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	return meye.params.subsample ? 320 : 640;
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci/* returns the vertical capture size */
3948c2ecf20Sopenharmony_cistatic inline int mchip_vsize(void)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	return meye.params.subsample ? 240 : 480;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/* waits for a register to be available */
4008c2ecf20Sopenharmony_cistatic void mchip_sync(int reg)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	u32 status;
4038c2ecf20Sopenharmony_ci	int i;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	if (reg == MCHIP_MM_FIFO_DATA) {
4068c2ecf20Sopenharmony_ci		for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
4078c2ecf20Sopenharmony_ci			status = readl(meye.mchip_mmregs +
4088c2ecf20Sopenharmony_ci				       MCHIP_MM_FIFO_STATUS);
4098c2ecf20Sopenharmony_ci			if (!(status & MCHIP_MM_FIFO_WAIT)) {
4108c2ecf20Sopenharmony_ci				printk(KERN_WARNING "meye: fifo not ready\n");
4118c2ecf20Sopenharmony_ci				return;
4128c2ecf20Sopenharmony_ci			}
4138c2ecf20Sopenharmony_ci			if (status & MCHIP_MM_FIFO_READY)
4148c2ecf20Sopenharmony_ci				return;
4158c2ecf20Sopenharmony_ci			udelay(1);
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci	} else if (reg > 0x80) {
4188c2ecf20Sopenharmony_ci		u32 mask = (reg < 0x100) ? MCHIP_HIC_STATUS_MCC_RDY
4198c2ecf20Sopenharmony_ci					 : MCHIP_HIC_STATUS_VRJ_RDY;
4208c2ecf20Sopenharmony_ci		for (i = 0; i < MCHIP_REG_TIMEOUT; i++) {
4218c2ecf20Sopenharmony_ci			status = readl(meye.mchip_mmregs + MCHIP_HIC_STATUS);
4228c2ecf20Sopenharmony_ci			if (status & mask)
4238c2ecf20Sopenharmony_ci				return;
4248c2ecf20Sopenharmony_ci			udelay(1);
4258c2ecf20Sopenharmony_ci		}
4268c2ecf20Sopenharmony_ci	} else
4278c2ecf20Sopenharmony_ci		return;
4288c2ecf20Sopenharmony_ci	printk(KERN_WARNING
4298c2ecf20Sopenharmony_ci	       "meye: mchip_sync() timeout on reg 0x%x status=0x%x\n",
4308c2ecf20Sopenharmony_ci	       reg, status);
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci/* sets a value into the register */
4348c2ecf20Sopenharmony_cistatic inline void mchip_set(int reg, u32 v)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	mchip_sync(reg);
4378c2ecf20Sopenharmony_ci	writel(v, meye.mchip_mmregs + reg);
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/* get the register value */
4418c2ecf20Sopenharmony_cistatic inline u32 mchip_read(int reg)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	mchip_sync(reg);
4448c2ecf20Sopenharmony_ci	return readl(meye.mchip_mmregs + reg);
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci/* wait for a register to become a particular value */
4488c2ecf20Sopenharmony_cistatic inline int mchip_delay(u32 reg, u32 v)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	int n = 10;
4518c2ecf20Sopenharmony_ci	while (--n && mchip_read(reg) != v)
4528c2ecf20Sopenharmony_ci		udelay(1);
4538c2ecf20Sopenharmony_ci	return n;
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci/* setup subsampling */
4578c2ecf20Sopenharmony_cistatic void mchip_subsample(void)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MCC_R_SAMPLING, meye.params.subsample);
4608c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MCC_R_XRANGE, mchip_hsize());
4618c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MCC_R_YRANGE, mchip_vsize());
4628c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MCC_B_XRANGE, mchip_hsize());
4638c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MCC_B_YRANGE, mchip_vsize());
4648c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci/* set the framerate into the mchip */
4688c2ecf20Sopenharmony_cistatic void mchip_set_framerate(void)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_S_RATE, meye.params.framerate);
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci/* load some huffman and quantisation tables into the VRJ chip ready
4748c2ecf20Sopenharmony_ci   for JPEG compression */
4758c2ecf20Sopenharmony_cistatic void mchip_load_tables(void)
4768c2ecf20Sopenharmony_ci{
4778c2ecf20Sopenharmony_ci	int i;
4788c2ecf20Sopenharmony_ci	int length;
4798c2ecf20Sopenharmony_ci	u16 *tables;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	tables = jpeg_huffman_tables(&length);
4828c2ecf20Sopenharmony_ci	for (i = 0; i < length; i++)
4838c2ecf20Sopenharmony_ci		writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	tables = jpeg_quantisation_tables(&length, meye.params.quality);
4868c2ecf20Sopenharmony_ci	for (i = 0; i < length; i++)
4878c2ecf20Sopenharmony_ci		writel(tables[i], meye.mchip_mmregs + MCHIP_VRJ_TABLE_DATA);
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/* setup the VRJ parameters in the chip */
4918c2ecf20Sopenharmony_cistatic void mchip_vrj_setup(u8 mode)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_BUS_MODE, 5);
4948c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SIGNAL_ACTIVE_LEVEL, 0x1f);
4958c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_PDAT_USE, 1);
4968c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_IRQ_FLAG, 0xa0);
4978c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_MODE_SPECIFY, mode);
4988c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_NUM_LINES, mchip_vsize());
4998c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_NUM_PIXELS, mchip_hsize());
5008c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_NUM_COMPONENTS, 0x1b);
5018c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_LO, 0xFFFF);
5028c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_LIMIT_COMPRESSED_HI, 0xFFFF);
5038c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_COMP_DATA_FORMAT, 0xC);
5048c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_RESTART_INTERVAL, 0);
5058c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SOF1, 0x601);
5068c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SOF2, 0x1502);
5078c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SOF3, 0x1503);
5088c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SOF4, 0x1596);
5098c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SOS, 0x0ed0);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	mchip_load_tables();
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci/* sets the DMA parameters into the chip */
5158c2ecf20Sopenharmony_cistatic void mchip_dma_setup(dma_addr_t dma_addr)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	int i;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_PT_ADDR, (u32)dma_addr);
5208c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++)
5218c2ecf20Sopenharmony_ci		mchip_set(MCHIP_MM_FIR(i), 0);
5228c2ecf20Sopenharmony_ci	meye.mchip_fnum = 0;
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci/* setup for DMA transfers - also zeros the framebuffer */
5268c2ecf20Sopenharmony_cistatic int mchip_dma_alloc(void)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	if (!meye.mchip_dmahandle)
5298c2ecf20Sopenharmony_ci		if (ptable_alloc())
5308c2ecf20Sopenharmony_ci			return -1;
5318c2ecf20Sopenharmony_ci	return 0;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci/* frees the DMA buffer */
5358c2ecf20Sopenharmony_cistatic void mchip_dma_free(void)
5368c2ecf20Sopenharmony_ci{
5378c2ecf20Sopenharmony_ci	if (meye.mchip_dmahandle) {
5388c2ecf20Sopenharmony_ci		mchip_dma_setup(0);
5398c2ecf20Sopenharmony_ci		ptable_free();
5408c2ecf20Sopenharmony_ci	}
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci/* stop any existing HIC action and wait for any dma to complete then
5448c2ecf20Sopenharmony_ci   reset the dma engine */
5458c2ecf20Sopenharmony_cistatic void mchip_hic_stop(void)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	int i, j;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	meye.mchip_mode = MCHIP_HIC_MODE_NOOP;
5508c2ecf20Sopenharmony_ci	if (!(mchip_read(MCHIP_HIC_STATUS) & MCHIP_HIC_STATUS_BUSY))
5518c2ecf20Sopenharmony_ci		return;
5528c2ecf20Sopenharmony_ci	for (i = 0; i < 20; ++i) {
5538c2ecf20Sopenharmony_ci		mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_STOP);
5548c2ecf20Sopenharmony_ci		mchip_delay(MCHIP_HIC_CMD, 0);
5558c2ecf20Sopenharmony_ci		for (j = 0; j < 100; ++j) {
5568c2ecf20Sopenharmony_ci			if (mchip_delay(MCHIP_HIC_STATUS,
5578c2ecf20Sopenharmony_ci					MCHIP_HIC_STATUS_IDLE))
5588c2ecf20Sopenharmony_ci				return;
5598c2ecf20Sopenharmony_ci			msleep(1);
5608c2ecf20Sopenharmony_ci		}
5618c2ecf20Sopenharmony_ci		printk(KERN_ERR "meye: need to reset HIC!\n");
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		mchip_set(MCHIP_HIC_CTL, MCHIP_HIC_CTL_SOFT_RESET);
5648c2ecf20Sopenharmony_ci		msleep(250);
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci	printk(KERN_ERR "meye: resetting HIC hanged!\n");
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci/****************************************************************************/
5708c2ecf20Sopenharmony_ci/* MCHIP frame processing functions                                         */
5718c2ecf20Sopenharmony_ci/****************************************************************************/
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci/* get the next ready frame from the dma engine */
5748c2ecf20Sopenharmony_cistatic u32 mchip_get_frame(void)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	return mchip_read(MCHIP_MM_FIR(meye.mchip_fnum));
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci/* frees the current frame from the dma engine */
5808c2ecf20Sopenharmony_cistatic void mchip_free_frame(void)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_FIR(meye.mchip_fnum), 0);
5838c2ecf20Sopenharmony_ci	meye.mchip_fnum++;
5848c2ecf20Sopenharmony_ci	meye.mchip_fnum %= 4;
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci/* read one frame from the framebuffer assuming it was captured using
5888c2ecf20Sopenharmony_ci   a uncompressed transfer */
5898c2ecf20Sopenharmony_cistatic void mchip_cont_read_frame(u32 v, u8 *buf, int size)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	int pt_id;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	pt_id = (v >> 17) & 0x3FF;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	ptable_copy(buf, pt_id, size, MCHIP_NB_PAGES);
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci/* read a compressed frame from the framebuffer */
5998c2ecf20Sopenharmony_cistatic int mchip_comp_read_frame(u32 v, u8 *buf, int size)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	int pt_start, pt_end, trailer;
6028c2ecf20Sopenharmony_ci	int fsize;
6038c2ecf20Sopenharmony_ci	int i;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	pt_start = (v >> 19) & 0xFF;
6068c2ecf20Sopenharmony_ci	pt_end = (v >> 11) & 0xFF;
6078c2ecf20Sopenharmony_ci	trailer = (v >> 1) & 0x3FF;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	if (pt_end < pt_start)
6108c2ecf20Sopenharmony_ci		fsize = (MCHIP_NB_PAGES_MJPEG - pt_start) * PAGE_SIZE +
6118c2ecf20Sopenharmony_ci			pt_end * PAGE_SIZE + trailer * 4;
6128c2ecf20Sopenharmony_ci	else
6138c2ecf20Sopenharmony_ci		fsize = (pt_end - pt_start) * PAGE_SIZE + trailer * 4;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	if (fsize > size) {
6168c2ecf20Sopenharmony_ci		printk(KERN_WARNING "meye: oversized compressed frame %d\n",
6178c2ecf20Sopenharmony_ci		       fsize);
6188c2ecf20Sopenharmony_ci		return -1;
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	ptable_copy(buf, pt_start, fsize, MCHIP_NB_PAGES_MJPEG);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci#ifdef MEYE_JPEG_CORRECTION
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* Some mchip generated jpeg frames are incorrect. In most
6268c2ecf20Sopenharmony_ci	 * (all ?) of those cases, the final EOI (0xff 0xd9) marker
6278c2ecf20Sopenharmony_ci	 * is not present at the end of the frame.
6288c2ecf20Sopenharmony_ci	 *
6298c2ecf20Sopenharmony_ci	 * Since adding the final marker is not enough to restore
6308c2ecf20Sopenharmony_ci	 * the jpeg integrity, we drop the frame.
6318c2ecf20Sopenharmony_ci	 */
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	for (i = fsize - 1; i > 0 && buf[i] == 0xff; i--) ;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (i < 2 || buf[i - 1] != 0xff || buf[i] != 0xd9)
6368c2ecf20Sopenharmony_ci		return -1;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci#endif
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	return fsize;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci/* take a picture into SDRAM */
6448c2ecf20Sopenharmony_cistatic void mchip_take_picture(void)
6458c2ecf20Sopenharmony_ci{
6468c2ecf20Sopenharmony_ci	int i;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	mchip_hic_stop();
6498c2ecf20Sopenharmony_ci	mchip_subsample();
6508c2ecf20Sopenharmony_ci	mchip_dma_setup(meye.mchip_dmahandle);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_CAP);
6538c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	for (i = 0; i < 100; ++i) {
6588c2ecf20Sopenharmony_ci		if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
6598c2ecf20Sopenharmony_ci			break;
6608c2ecf20Sopenharmony_ci		msleep(1);
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci/* dma a previously taken picture into a buffer */
6658c2ecf20Sopenharmony_cistatic void mchip_get_picture(u8 *buf, int bufsize)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	u32 v;
6688c2ecf20Sopenharmony_ci	int i;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_OUT);
6718c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
6748c2ecf20Sopenharmony_ci	for (i = 0; i < 100; ++i) {
6758c2ecf20Sopenharmony_ci		if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
6768c2ecf20Sopenharmony_ci			break;
6778c2ecf20Sopenharmony_ci		msleep(1);
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci	for (i = 0; i < 4; ++i) {
6808c2ecf20Sopenharmony_ci		v = mchip_get_frame();
6818c2ecf20Sopenharmony_ci		if (v & MCHIP_MM_FIR_RDY) {
6828c2ecf20Sopenharmony_ci			mchip_cont_read_frame(v, buf, bufsize);
6838c2ecf20Sopenharmony_ci			break;
6848c2ecf20Sopenharmony_ci		}
6858c2ecf20Sopenharmony_ci		mchip_free_frame();
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci/* start continuous dma capture */
6908c2ecf20Sopenharmony_cistatic void mchip_continuous_start(void)
6918c2ecf20Sopenharmony_ci{
6928c2ecf20Sopenharmony_ci	mchip_hic_stop();
6938c2ecf20Sopenharmony_ci	mchip_subsample();
6948c2ecf20Sopenharmony_ci	mchip_set_framerate();
6958c2ecf20Sopenharmony_ci	mchip_dma_setup(meye.mchip_dmahandle);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_OUT);
7008c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci/* compress one frame into a buffer */
7068c2ecf20Sopenharmony_cistatic int mchip_compress_frame(u8 *buf, int bufsize)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	u32 v;
7098c2ecf20Sopenharmony_ci	int len = -1, i;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	mchip_vrj_setup(0x3f);
7128c2ecf20Sopenharmony_ci	udelay(50);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_COMP);
7158c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
7188c2ecf20Sopenharmony_ci	for (i = 0; i < 100; ++i) {
7198c2ecf20Sopenharmony_ci		if (mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE))
7208c2ecf20Sopenharmony_ci			break;
7218c2ecf20Sopenharmony_ci		msleep(1);
7228c2ecf20Sopenharmony_ci	}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	for (i = 0; i < 4; ++i) {
7258c2ecf20Sopenharmony_ci		v = mchip_get_frame();
7268c2ecf20Sopenharmony_ci		if (v & MCHIP_MM_FIR_RDY) {
7278c2ecf20Sopenharmony_ci			len = mchip_comp_read_frame(v, buf, bufsize);
7288c2ecf20Sopenharmony_ci			break;
7298c2ecf20Sopenharmony_ci		}
7308c2ecf20Sopenharmony_ci		mchip_free_frame();
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci	return len;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci#if 0
7368c2ecf20Sopenharmony_ci/* uncompress one image into a buffer */
7378c2ecf20Sopenharmony_cistatic int mchip_uncompress_frame(u8 *img, int imgsize, u8 *buf, int bufsize)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	mchip_vrj_setup(0x3f);
7408c2ecf20Sopenharmony_ci	udelay(50);
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_STILL_DECOMP);
7438c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	return mchip_comp_read_frame(buf, bufsize);
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci#endif
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci/* start continuous compressed capture */
7528c2ecf20Sopenharmony_cistatic void mchip_cont_compression_start(void)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	mchip_hic_stop();
7558c2ecf20Sopenharmony_ci	mchip_vrj_setup(0x3f);
7568c2ecf20Sopenharmony_ci	mchip_subsample();
7578c2ecf20Sopenharmony_ci	mchip_set_framerate();
7588c2ecf20Sopenharmony_ci	mchip_dma_setup(meye.mchip_dmahandle);
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_MODE, MCHIP_HIC_MODE_CONT_COMP);
7638c2ecf20Sopenharmony_ci	mchip_set(MCHIP_HIC_CMD, MCHIP_HIC_CMD_START);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci/****************************************************************************/
7698c2ecf20Sopenharmony_ci/* Interrupt handling                                                       */
7708c2ecf20Sopenharmony_ci/****************************************************************************/
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cistatic irqreturn_t meye_irq(int irq, void *dev_id)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	u32 v;
7758c2ecf20Sopenharmony_ci	int reqnr;
7768c2ecf20Sopenharmony_ci	static int sequence;
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	v = mchip_read(MCHIP_MM_INTA);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_OUT &&
7818c2ecf20Sopenharmony_ci	    meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
7828c2ecf20Sopenharmony_ci		return IRQ_NONE;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ciagain:
7858c2ecf20Sopenharmony_ci	v = mchip_get_frame();
7868c2ecf20Sopenharmony_ci	if (!(v & MCHIP_MM_FIR_RDY))
7878c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	if (meye.mchip_mode == MCHIP_HIC_MODE_CONT_OUT) {
7908c2ecf20Sopenharmony_ci		if (kfifo_out_locked(&meye.grabq, (unsigned char *)&reqnr,
7918c2ecf20Sopenharmony_ci			      sizeof(int), &meye.grabq_lock) != sizeof(int)) {
7928c2ecf20Sopenharmony_ci			mchip_free_frame();
7938c2ecf20Sopenharmony_ci			return IRQ_HANDLED;
7948c2ecf20Sopenharmony_ci		}
7958c2ecf20Sopenharmony_ci		mchip_cont_read_frame(v, meye.grab_fbuffer + gbufsize * reqnr,
7968c2ecf20Sopenharmony_ci				      mchip_hsize() * mchip_vsize() * 2);
7978c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].size = mchip_hsize() * mchip_vsize() * 2;
7988c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].state = MEYE_BUF_DONE;
7998c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].ts = ktime_get_ns();
8008c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].sequence = sequence++;
8018c2ecf20Sopenharmony_ci		kfifo_in_locked(&meye.doneq, (unsigned char *)&reqnr,
8028c2ecf20Sopenharmony_ci				sizeof(int), &meye.doneq_lock);
8038c2ecf20Sopenharmony_ci		wake_up_interruptible(&meye.proc_list);
8048c2ecf20Sopenharmony_ci	} else {
8058c2ecf20Sopenharmony_ci		int size;
8068c2ecf20Sopenharmony_ci		size = mchip_comp_read_frame(v, meye.grab_temp, gbufsize);
8078c2ecf20Sopenharmony_ci		if (size == -1) {
8088c2ecf20Sopenharmony_ci			mchip_free_frame();
8098c2ecf20Sopenharmony_ci			goto again;
8108c2ecf20Sopenharmony_ci		}
8118c2ecf20Sopenharmony_ci		if (kfifo_out_locked(&meye.grabq, (unsigned char *)&reqnr,
8128c2ecf20Sopenharmony_ci			      sizeof(int), &meye.grabq_lock) != sizeof(int)) {
8138c2ecf20Sopenharmony_ci			mchip_free_frame();
8148c2ecf20Sopenharmony_ci			goto again;
8158c2ecf20Sopenharmony_ci		}
8168c2ecf20Sopenharmony_ci		memcpy(meye.grab_fbuffer + gbufsize * reqnr, meye.grab_temp,
8178c2ecf20Sopenharmony_ci		       size);
8188c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].size = size;
8198c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].state = MEYE_BUF_DONE;
8208c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].ts = ktime_get_ns();
8218c2ecf20Sopenharmony_ci		meye.grab_buffer[reqnr].sequence = sequence++;
8228c2ecf20Sopenharmony_ci		kfifo_in_locked(&meye.doneq, (unsigned char *)&reqnr,
8238c2ecf20Sopenharmony_ci				sizeof(int), &meye.doneq_lock);
8248c2ecf20Sopenharmony_ci		wake_up_interruptible(&meye.proc_list);
8258c2ecf20Sopenharmony_ci	}
8268c2ecf20Sopenharmony_ci	mchip_free_frame();
8278c2ecf20Sopenharmony_ci	goto again;
8288c2ecf20Sopenharmony_ci}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci/****************************************************************************/
8318c2ecf20Sopenharmony_ci/* video4linux integration                                                  */
8328c2ecf20Sopenharmony_ci/****************************************************************************/
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cistatic int meye_open(struct file *file)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	int i;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	if (test_and_set_bit(0, &meye.in_use))
8398c2ecf20Sopenharmony_ci		return -EBUSY;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	mchip_hic_stop();
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	if (mchip_dma_alloc()) {
8448c2ecf20Sopenharmony_ci		printk(KERN_ERR "meye: mchip framebuffer allocation failed\n");
8458c2ecf20Sopenharmony_ci		clear_bit(0, &meye.in_use);
8468c2ecf20Sopenharmony_ci		return -ENOBUFS;
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
8508c2ecf20Sopenharmony_ci		meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
8518c2ecf20Sopenharmony_ci	kfifo_reset(&meye.grabq);
8528c2ecf20Sopenharmony_ci	kfifo_reset(&meye.doneq);
8538c2ecf20Sopenharmony_ci	return v4l2_fh_open(file);
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_cistatic int meye_release(struct file *file)
8578c2ecf20Sopenharmony_ci{
8588c2ecf20Sopenharmony_ci	mchip_hic_stop();
8598c2ecf20Sopenharmony_ci	mchip_dma_free();
8608c2ecf20Sopenharmony_ci	clear_bit(0, &meye.in_use);
8618c2ecf20Sopenharmony_ci	return v4l2_fh_release(file);
8628c2ecf20Sopenharmony_ci}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_cistatic int meyeioc_g_params(struct meye_params *p)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	*p = meye.params;
8678c2ecf20Sopenharmony_ci	return 0;
8688c2ecf20Sopenharmony_ci}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_cistatic int meyeioc_s_params(struct meye_params *jp)
8718c2ecf20Sopenharmony_ci{
8728c2ecf20Sopenharmony_ci	if (jp->subsample > 1)
8738c2ecf20Sopenharmony_ci		return -EINVAL;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if (jp->quality > 10)
8768c2ecf20Sopenharmony_ci		return -EINVAL;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	if (jp->sharpness > 63 || jp->agc > 63 || jp->picture > 63)
8798c2ecf20Sopenharmony_ci		return -EINVAL;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	if (jp->framerate > 31)
8828c2ecf20Sopenharmony_ci		return -EINVAL;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if (meye.params.subsample != jp->subsample ||
8878c2ecf20Sopenharmony_ci	    meye.params.quality != jp->quality)
8888c2ecf20Sopenharmony_ci		mchip_hic_stop();	/* need restart */
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	meye.params = *jp;
8918c2ecf20Sopenharmony_ci	sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERASHARPNESS,
8928c2ecf20Sopenharmony_ci			      meye.params.sharpness);
8938c2ecf20Sopenharmony_ci	sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAAGC,
8948c2ecf20Sopenharmony_ci			      meye.params.agc);
8958c2ecf20Sopenharmony_ci	sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERAPICTURE,
8968c2ecf20Sopenharmony_ci			      meye.params.picture);
8978c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	return 0;
9008c2ecf20Sopenharmony_ci}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_cistatic int meyeioc_qbuf_capt(int *nb)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	if (!meye.grab_fbuffer)
9058c2ecf20Sopenharmony_ci		return -EINVAL;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	if (*nb >= gbuffers)
9088c2ecf20Sopenharmony_ci		return -EINVAL;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	if (*nb < 0) {
9118c2ecf20Sopenharmony_ci		/* stop capture */
9128c2ecf20Sopenharmony_ci		mchip_hic_stop();
9138c2ecf20Sopenharmony_ci		return 0;
9148c2ecf20Sopenharmony_ci	}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	if (meye.grab_buffer[*nb].state != MEYE_BUF_UNUSED)
9178c2ecf20Sopenharmony_ci		return -EBUSY;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (meye.mchip_mode != MCHIP_HIC_MODE_CONT_COMP)
9228c2ecf20Sopenharmony_ci		mchip_cont_compression_start();
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	meye.grab_buffer[*nb].state = MEYE_BUF_USING;
9258c2ecf20Sopenharmony_ci	kfifo_in_locked(&meye.grabq, (unsigned char *)nb, sizeof(int),
9268c2ecf20Sopenharmony_ci			 &meye.grabq_lock);
9278c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	return 0;
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_cistatic int meyeioc_sync(struct file *file, void *fh, int *i)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	int unused;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (*i < 0 || *i >= gbuffers)
9378c2ecf20Sopenharmony_ci		return -EINVAL;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
9408c2ecf20Sopenharmony_ci	switch (meye.grab_buffer[*i].state) {
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	case MEYE_BUF_UNUSED:
9438c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
9448c2ecf20Sopenharmony_ci		return -EINVAL;
9458c2ecf20Sopenharmony_ci	case MEYE_BUF_USING:
9468c2ecf20Sopenharmony_ci		if (file->f_flags & O_NONBLOCK) {
9478c2ecf20Sopenharmony_ci			mutex_unlock(&meye.lock);
9488c2ecf20Sopenharmony_ci			return -EAGAIN;
9498c2ecf20Sopenharmony_ci		}
9508c2ecf20Sopenharmony_ci		if (wait_event_interruptible(meye.proc_list,
9518c2ecf20Sopenharmony_ci			(meye.grab_buffer[*i].state != MEYE_BUF_USING))) {
9528c2ecf20Sopenharmony_ci			mutex_unlock(&meye.lock);
9538c2ecf20Sopenharmony_ci			return -EINTR;
9548c2ecf20Sopenharmony_ci		}
9558c2ecf20Sopenharmony_ci		fallthrough;
9568c2ecf20Sopenharmony_ci	case MEYE_BUF_DONE:
9578c2ecf20Sopenharmony_ci		meye.grab_buffer[*i].state = MEYE_BUF_UNUSED;
9588c2ecf20Sopenharmony_ci		if (kfifo_out_locked(&meye.doneq, (unsigned char *)&unused,
9598c2ecf20Sopenharmony_ci				sizeof(int), &meye.doneq_lock) != sizeof(int))
9608c2ecf20Sopenharmony_ci					break;
9618c2ecf20Sopenharmony_ci	}
9628c2ecf20Sopenharmony_ci	*i = meye.grab_buffer[*i].size;
9638c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
9648c2ecf20Sopenharmony_ci	return 0;
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic int meyeioc_stillcapt(void)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	if (!meye.grab_fbuffer)
9708c2ecf20Sopenharmony_ci		return -EINVAL;
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
9738c2ecf20Sopenharmony_ci		return -EBUSY;
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
9768c2ecf20Sopenharmony_ci	meye.grab_buffer[0].state = MEYE_BUF_USING;
9778c2ecf20Sopenharmony_ci	mchip_take_picture();
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	mchip_get_picture(meye.grab_fbuffer,
9808c2ecf20Sopenharmony_ci			mchip_hsize() * mchip_vsize() * 2);
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	meye.grab_buffer[0].state = MEYE_BUF_DONE;
9838c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	return 0;
9868c2ecf20Sopenharmony_ci}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_cistatic int meyeioc_stilljcapt(int *len)
9898c2ecf20Sopenharmony_ci{
9908c2ecf20Sopenharmony_ci	if (!meye.grab_fbuffer)
9918c2ecf20Sopenharmony_ci		return -EINVAL;
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	if (meye.grab_buffer[0].state != MEYE_BUF_UNUSED)
9948c2ecf20Sopenharmony_ci		return -EBUSY;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
9978c2ecf20Sopenharmony_ci	meye.grab_buffer[0].state = MEYE_BUF_USING;
9988c2ecf20Sopenharmony_ci	*len = -1;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	while (*len == -1) {
10018c2ecf20Sopenharmony_ci		mchip_take_picture();
10028c2ecf20Sopenharmony_ci		*len = mchip_compress_frame(meye.grab_fbuffer, gbufsize);
10038c2ecf20Sopenharmony_ci	}
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	meye.grab_buffer[0].state = MEYE_BUF_DONE;
10068c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
10078c2ecf20Sopenharmony_ci	return 0;
10088c2ecf20Sopenharmony_ci}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_cistatic int vidioc_querycap(struct file *file, void *fh,
10118c2ecf20Sopenharmony_ci				struct v4l2_capability *cap)
10128c2ecf20Sopenharmony_ci{
10138c2ecf20Sopenharmony_ci	strscpy(cap->driver, "meye", sizeof(cap->driver));
10148c2ecf20Sopenharmony_ci	strscpy(cap->card, "meye", sizeof(cap->card));
10158c2ecf20Sopenharmony_ci	sprintf(cap->bus_info, "PCI:%s", pci_name(meye.mchip_dev));
10168c2ecf20Sopenharmony_ci	return 0;
10178c2ecf20Sopenharmony_ci}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_cistatic int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
10208c2ecf20Sopenharmony_ci{
10218c2ecf20Sopenharmony_ci	if (i->index != 0)
10228c2ecf20Sopenharmony_ci		return -EINVAL;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	strscpy(i->name, "Camera", sizeof(i->name));
10258c2ecf20Sopenharmony_ci	i->type = V4L2_INPUT_TYPE_CAMERA;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	return 0;
10288c2ecf20Sopenharmony_ci}
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_cistatic int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
10318c2ecf20Sopenharmony_ci{
10328c2ecf20Sopenharmony_ci	*i = 0;
10338c2ecf20Sopenharmony_ci	return 0;
10348c2ecf20Sopenharmony_ci}
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_cistatic int vidioc_s_input(struct file *file, void *fh, unsigned int i)
10378c2ecf20Sopenharmony_ci{
10388c2ecf20Sopenharmony_ci	if (i != 0)
10398c2ecf20Sopenharmony_ci		return -EINVAL;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	return 0;
10428c2ecf20Sopenharmony_ci}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_cistatic int meye_s_ctrl(struct v4l2_ctrl *ctrl)
10458c2ecf20Sopenharmony_ci{
10468c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
10478c2ecf20Sopenharmony_ci	switch (ctrl->id) {
10488c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
10498c2ecf20Sopenharmony_ci		sony_pic_camera_command(
10508c2ecf20Sopenharmony_ci			SONY_PIC_COMMAND_SETCAMERABRIGHTNESS, ctrl->val);
10518c2ecf20Sopenharmony_ci		meye.brightness = ctrl->val << 10;
10528c2ecf20Sopenharmony_ci		break;
10538c2ecf20Sopenharmony_ci	case V4L2_CID_HUE:
10548c2ecf20Sopenharmony_ci		sony_pic_camera_command(
10558c2ecf20Sopenharmony_ci			SONY_PIC_COMMAND_SETCAMERAHUE, ctrl->val);
10568c2ecf20Sopenharmony_ci		meye.hue = ctrl->val << 10;
10578c2ecf20Sopenharmony_ci		break;
10588c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
10598c2ecf20Sopenharmony_ci		sony_pic_camera_command(
10608c2ecf20Sopenharmony_ci			SONY_PIC_COMMAND_SETCAMERACONTRAST, ctrl->val);
10618c2ecf20Sopenharmony_ci		meye.contrast = ctrl->val << 10;
10628c2ecf20Sopenharmony_ci		break;
10638c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
10648c2ecf20Sopenharmony_ci		sony_pic_camera_command(
10658c2ecf20Sopenharmony_ci			SONY_PIC_COMMAND_SETCAMERACOLOR, ctrl->val);
10668c2ecf20Sopenharmony_ci		meye.colour = ctrl->val << 10;
10678c2ecf20Sopenharmony_ci		break;
10688c2ecf20Sopenharmony_ci	case V4L2_CID_MEYE_AGC:
10698c2ecf20Sopenharmony_ci		sony_pic_camera_command(
10708c2ecf20Sopenharmony_ci			SONY_PIC_COMMAND_SETCAMERAAGC, ctrl->val);
10718c2ecf20Sopenharmony_ci		meye.params.agc = ctrl->val;
10728c2ecf20Sopenharmony_ci		break;
10738c2ecf20Sopenharmony_ci	case V4L2_CID_SHARPNESS:
10748c2ecf20Sopenharmony_ci		sony_pic_camera_command(
10758c2ecf20Sopenharmony_ci			SONY_PIC_COMMAND_SETCAMERASHARPNESS, ctrl->val);
10768c2ecf20Sopenharmony_ci		meye.params.sharpness = ctrl->val;
10778c2ecf20Sopenharmony_ci		break;
10788c2ecf20Sopenharmony_ci	case V4L2_CID_MEYE_PICTURE:
10798c2ecf20Sopenharmony_ci		sony_pic_camera_command(
10808c2ecf20Sopenharmony_ci			SONY_PIC_COMMAND_SETCAMERAPICTURE, ctrl->val);
10818c2ecf20Sopenharmony_ci		meye.params.picture = ctrl->val;
10828c2ecf20Sopenharmony_ci		break;
10838c2ecf20Sopenharmony_ci	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
10848c2ecf20Sopenharmony_ci		meye.params.quality = ctrl->val;
10858c2ecf20Sopenharmony_ci		break;
10868c2ecf20Sopenharmony_ci	case V4L2_CID_MEYE_FRAMERATE:
10878c2ecf20Sopenharmony_ci		meye.params.framerate = ctrl->val;
10888c2ecf20Sopenharmony_ci		break;
10898c2ecf20Sopenharmony_ci	default:
10908c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
10918c2ecf20Sopenharmony_ci		return -EINVAL;
10928c2ecf20Sopenharmony_ci	}
10938c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	return 0;
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_cistatic int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
10998c2ecf20Sopenharmony_ci				struct v4l2_fmtdesc *f)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	if (f->index > 1)
11028c2ecf20Sopenharmony_ci		return -EINVAL;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	if (f->index == 0) {
11058c2ecf20Sopenharmony_ci		/* standard YUV 422 capture */
11068c2ecf20Sopenharmony_ci		f->flags = 0;
11078c2ecf20Sopenharmony_ci		f->pixelformat = V4L2_PIX_FMT_YUYV;
11088c2ecf20Sopenharmony_ci	} else {
11098c2ecf20Sopenharmony_ci		/* compressed MJPEG capture */
11108c2ecf20Sopenharmony_ci		f->pixelformat = V4L2_PIX_FMT_MJPEG;
11118c2ecf20Sopenharmony_ci	}
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	return 0;
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_cistatic int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
11178c2ecf20Sopenharmony_ci				struct v4l2_format *f)
11188c2ecf20Sopenharmony_ci{
11198c2ecf20Sopenharmony_ci	if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
11208c2ecf20Sopenharmony_ci	    f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
11218c2ecf20Sopenharmony_ci		return -EINVAL;
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	if (f->fmt.pix.field != V4L2_FIELD_ANY &&
11248c2ecf20Sopenharmony_ci	    f->fmt.pix.field != V4L2_FIELD_NONE)
11258c2ecf20Sopenharmony_ci		return -EINVAL;
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	f->fmt.pix.field = V4L2_FIELD_NONE;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	if (f->fmt.pix.width <= 320) {
11308c2ecf20Sopenharmony_ci		f->fmt.pix.width = 320;
11318c2ecf20Sopenharmony_ci		f->fmt.pix.height = 240;
11328c2ecf20Sopenharmony_ci	} else {
11338c2ecf20Sopenharmony_ci		f->fmt.pix.width = 640;
11348c2ecf20Sopenharmony_ci		f->fmt.pix.height = 480;
11358c2ecf20Sopenharmony_ci	}
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
11388c2ecf20Sopenharmony_ci	f->fmt.pix.sizeimage = f->fmt.pix.height *
11398c2ecf20Sopenharmony_ci			       f->fmt.pix.bytesperline;
11408c2ecf20Sopenharmony_ci	f->fmt.pix.colorspace = 0;
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	return 0;
11438c2ecf20Sopenharmony_ci}
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_cistatic int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
11468c2ecf20Sopenharmony_ci				    struct v4l2_format *f)
11478c2ecf20Sopenharmony_ci{
11488c2ecf20Sopenharmony_ci	switch (meye.mchip_mode) {
11498c2ecf20Sopenharmony_ci	case MCHIP_HIC_MODE_CONT_OUT:
11508c2ecf20Sopenharmony_ci	default:
11518c2ecf20Sopenharmony_ci		f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
11528c2ecf20Sopenharmony_ci		break;
11538c2ecf20Sopenharmony_ci	case MCHIP_HIC_MODE_CONT_COMP:
11548c2ecf20Sopenharmony_ci		f->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
11558c2ecf20Sopenharmony_ci		break;
11568c2ecf20Sopenharmony_ci	}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	f->fmt.pix.field = V4L2_FIELD_NONE;
11598c2ecf20Sopenharmony_ci	f->fmt.pix.width = mchip_hsize();
11608c2ecf20Sopenharmony_ci	f->fmt.pix.height = mchip_vsize();
11618c2ecf20Sopenharmony_ci	f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
11628c2ecf20Sopenharmony_ci	f->fmt.pix.sizeimage = f->fmt.pix.height *
11638c2ecf20Sopenharmony_ci			       f->fmt.pix.bytesperline;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	return 0;
11668c2ecf20Sopenharmony_ci}
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_cistatic int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
11698c2ecf20Sopenharmony_ci				    struct v4l2_format *f)
11708c2ecf20Sopenharmony_ci{
11718c2ecf20Sopenharmony_ci	if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV &&
11728c2ecf20Sopenharmony_ci	    f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
11738c2ecf20Sopenharmony_ci		return -EINVAL;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	if (f->fmt.pix.field != V4L2_FIELD_ANY &&
11768c2ecf20Sopenharmony_ci	    f->fmt.pix.field != V4L2_FIELD_NONE)
11778c2ecf20Sopenharmony_ci		return -EINVAL;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	f->fmt.pix.field = V4L2_FIELD_NONE;
11808c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	if (f->fmt.pix.width <= 320) {
11838c2ecf20Sopenharmony_ci		f->fmt.pix.width = 320;
11848c2ecf20Sopenharmony_ci		f->fmt.pix.height = 240;
11858c2ecf20Sopenharmony_ci		meye.params.subsample = 1;
11868c2ecf20Sopenharmony_ci	} else {
11878c2ecf20Sopenharmony_ci		f->fmt.pix.width = 640;
11888c2ecf20Sopenharmony_ci		f->fmt.pix.height = 480;
11898c2ecf20Sopenharmony_ci		meye.params.subsample = 0;
11908c2ecf20Sopenharmony_ci	}
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	switch (f->fmt.pix.pixelformat) {
11938c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:
11948c2ecf20Sopenharmony_ci		meye.mchip_mode = MCHIP_HIC_MODE_CONT_OUT;
11958c2ecf20Sopenharmony_ci		break;
11968c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_MJPEG:
11978c2ecf20Sopenharmony_ci		meye.mchip_mode = MCHIP_HIC_MODE_CONT_COMP;
11988c2ecf20Sopenharmony_ci		break;
11998c2ecf20Sopenharmony_ci	}
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
12028c2ecf20Sopenharmony_ci	f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
12038c2ecf20Sopenharmony_ci	f->fmt.pix.sizeimage = f->fmt.pix.height *
12048c2ecf20Sopenharmony_ci			       f->fmt.pix.bytesperline;
12058c2ecf20Sopenharmony_ci	f->fmt.pix.colorspace = 0;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	return 0;
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_cistatic int vidioc_reqbufs(struct file *file, void *fh,
12118c2ecf20Sopenharmony_ci				struct v4l2_requestbuffers *req)
12128c2ecf20Sopenharmony_ci{
12138c2ecf20Sopenharmony_ci	int i;
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	if (req->memory != V4L2_MEMORY_MMAP)
12168c2ecf20Sopenharmony_ci		return -EINVAL;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	if (meye.grab_fbuffer && req->count == gbuffers) {
12198c2ecf20Sopenharmony_ci		/* already allocated, no modifications */
12208c2ecf20Sopenharmony_ci		return 0;
12218c2ecf20Sopenharmony_ci	}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
12248c2ecf20Sopenharmony_ci	if (meye.grab_fbuffer) {
12258c2ecf20Sopenharmony_ci		for (i = 0; i < gbuffers; i++)
12268c2ecf20Sopenharmony_ci			if (meye.vma_use_count[i]) {
12278c2ecf20Sopenharmony_ci				mutex_unlock(&meye.lock);
12288c2ecf20Sopenharmony_ci				return -EINVAL;
12298c2ecf20Sopenharmony_ci			}
12308c2ecf20Sopenharmony_ci		rvfree(meye.grab_fbuffer, gbuffers * gbufsize);
12318c2ecf20Sopenharmony_ci		meye.grab_fbuffer = NULL;
12328c2ecf20Sopenharmony_ci	}
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	gbuffers = max(2, min((int)req->count, MEYE_MAX_BUFNBRS));
12358c2ecf20Sopenharmony_ci	req->count = gbuffers;
12368c2ecf20Sopenharmony_ci	meye.grab_fbuffer = rvmalloc(gbuffers * gbufsize);
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	if (!meye.grab_fbuffer) {
12398c2ecf20Sopenharmony_ci		printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
12408c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
12418c2ecf20Sopenharmony_ci		return -ENOMEM;
12428c2ecf20Sopenharmony_ci	}
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	for (i = 0; i < gbuffers; i++)
12458c2ecf20Sopenharmony_ci		meye.vma_use_count[i] = 0;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	return 0;
12508c2ecf20Sopenharmony_ci}
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_cistatic int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
12538c2ecf20Sopenharmony_ci{
12548c2ecf20Sopenharmony_ci	unsigned int index = buf->index;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	if (index >= gbuffers)
12578c2ecf20Sopenharmony_ci		return -EINVAL;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	buf->bytesused = meye.grab_buffer[index].size;
12608c2ecf20Sopenharmony_ci	buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	if (meye.grab_buffer[index].state == MEYE_BUF_USING)
12638c2ecf20Sopenharmony_ci		buf->flags |= V4L2_BUF_FLAG_QUEUED;
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	if (meye.grab_buffer[index].state == MEYE_BUF_DONE)
12668c2ecf20Sopenharmony_ci		buf->flags |= V4L2_BUF_FLAG_DONE;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	buf->field = V4L2_FIELD_NONE;
12698c2ecf20Sopenharmony_ci	v4l2_buffer_set_timestamp(buf, meye.grab_buffer[index].ts);
12708c2ecf20Sopenharmony_ci	buf->sequence = meye.grab_buffer[index].sequence;
12718c2ecf20Sopenharmony_ci	buf->memory = V4L2_MEMORY_MMAP;
12728c2ecf20Sopenharmony_ci	buf->m.offset = index * gbufsize;
12738c2ecf20Sopenharmony_ci	buf->length = gbufsize;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	return 0;
12768c2ecf20Sopenharmony_ci}
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_cistatic int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
12798c2ecf20Sopenharmony_ci{
12808c2ecf20Sopenharmony_ci	if (buf->memory != V4L2_MEMORY_MMAP)
12818c2ecf20Sopenharmony_ci		return -EINVAL;
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	if (buf->index >= gbuffers)
12848c2ecf20Sopenharmony_ci		return -EINVAL;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	if (meye.grab_buffer[buf->index].state != MEYE_BUF_UNUSED)
12878c2ecf20Sopenharmony_ci		return -EINVAL;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
12908c2ecf20Sopenharmony_ci	buf->flags |= V4L2_BUF_FLAG_QUEUED;
12918c2ecf20Sopenharmony_ci	buf->flags &= ~V4L2_BUF_FLAG_DONE;
12928c2ecf20Sopenharmony_ci	meye.grab_buffer[buf->index].state = MEYE_BUF_USING;
12938c2ecf20Sopenharmony_ci	kfifo_in_locked(&meye.grabq, (unsigned char *)&buf->index,
12948c2ecf20Sopenharmony_ci			sizeof(int), &meye.grabq_lock);
12958c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci	return 0;
12988c2ecf20Sopenharmony_ci}
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_cistatic int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
13018c2ecf20Sopenharmony_ci{
13028c2ecf20Sopenharmony_ci	int reqnr;
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci	if (buf->memory != V4L2_MEMORY_MMAP)
13058c2ecf20Sopenharmony_ci		return -EINVAL;
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	if (kfifo_len(&meye.doneq) == 0 && file->f_flags & O_NONBLOCK) {
13108c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
13118c2ecf20Sopenharmony_ci		return -EAGAIN;
13128c2ecf20Sopenharmony_ci	}
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	if (wait_event_interruptible(meye.proc_list,
13158c2ecf20Sopenharmony_ci				     kfifo_len(&meye.doneq) != 0) < 0) {
13168c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
13178c2ecf20Sopenharmony_ci		return -EINTR;
13188c2ecf20Sopenharmony_ci	}
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci	if (!kfifo_out_locked(&meye.doneq, (unsigned char *)&reqnr,
13218c2ecf20Sopenharmony_ci		       sizeof(int), &meye.doneq_lock)) {
13228c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
13238c2ecf20Sopenharmony_ci		return -EBUSY;
13248c2ecf20Sopenharmony_ci	}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	if (meye.grab_buffer[reqnr].state != MEYE_BUF_DONE) {
13278c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
13288c2ecf20Sopenharmony_ci		return -EINVAL;
13298c2ecf20Sopenharmony_ci	}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	buf->index = reqnr;
13328c2ecf20Sopenharmony_ci	buf->bytesused = meye.grab_buffer[reqnr].size;
13338c2ecf20Sopenharmony_ci	buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
13348c2ecf20Sopenharmony_ci	buf->field = V4L2_FIELD_NONE;
13358c2ecf20Sopenharmony_ci	v4l2_buffer_set_timestamp(buf, meye.grab_buffer[reqnr].ts);
13368c2ecf20Sopenharmony_ci	buf->sequence = meye.grab_buffer[reqnr].sequence;
13378c2ecf20Sopenharmony_ci	buf->memory = V4L2_MEMORY_MMAP;
13388c2ecf20Sopenharmony_ci	buf->m.offset = reqnr * gbufsize;
13398c2ecf20Sopenharmony_ci	buf->length = gbufsize;
13408c2ecf20Sopenharmony_ci	meye.grab_buffer[reqnr].state = MEYE_BUF_UNUSED;
13418c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	return 0;
13448c2ecf20Sopenharmony_ci}
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_cistatic int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
13478c2ecf20Sopenharmony_ci{
13488c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	switch (meye.mchip_mode) {
13518c2ecf20Sopenharmony_ci	case MCHIP_HIC_MODE_CONT_OUT:
13528c2ecf20Sopenharmony_ci		mchip_continuous_start();
13538c2ecf20Sopenharmony_ci		break;
13548c2ecf20Sopenharmony_ci	case MCHIP_HIC_MODE_CONT_COMP:
13558c2ecf20Sopenharmony_ci		mchip_cont_compression_start();
13568c2ecf20Sopenharmony_ci		break;
13578c2ecf20Sopenharmony_ci	default:
13588c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
13598c2ecf20Sopenharmony_ci		return -EINVAL;
13608c2ecf20Sopenharmony_ci	}
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	return 0;
13658c2ecf20Sopenharmony_ci}
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_cistatic int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
13688c2ecf20Sopenharmony_ci{
13698c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
13708c2ecf20Sopenharmony_ci	mchip_hic_stop();
13718c2ecf20Sopenharmony_ci	kfifo_reset(&meye.grabq);
13728c2ecf20Sopenharmony_ci	kfifo_reset(&meye.doneq);
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	for (i = 0; i < MEYE_MAX_BUFNBRS; i++)
13758c2ecf20Sopenharmony_ci		meye.grab_buffer[i].state = MEYE_BUF_UNUSED;
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
13788c2ecf20Sopenharmony_ci	return 0;
13798c2ecf20Sopenharmony_ci}
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_cistatic long vidioc_default(struct file *file, void *fh, bool valid_prio,
13828c2ecf20Sopenharmony_ci			   unsigned int cmd, void *arg)
13838c2ecf20Sopenharmony_ci{
13848c2ecf20Sopenharmony_ci	switch (cmd) {
13858c2ecf20Sopenharmony_ci	case MEYEIOC_G_PARAMS:
13868c2ecf20Sopenharmony_ci		return meyeioc_g_params((struct meye_params *) arg);
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci	case MEYEIOC_S_PARAMS:
13898c2ecf20Sopenharmony_ci		return meyeioc_s_params((struct meye_params *) arg);
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	case MEYEIOC_QBUF_CAPT:
13928c2ecf20Sopenharmony_ci		return meyeioc_qbuf_capt((int *) arg);
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	case MEYEIOC_SYNC:
13958c2ecf20Sopenharmony_ci		return meyeioc_sync(file, fh, (int *) arg);
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	case MEYEIOC_STILLCAPT:
13988c2ecf20Sopenharmony_ci		return meyeioc_stillcapt();
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	case MEYEIOC_STILLJCAPT:
14018c2ecf20Sopenharmony_ci		return meyeioc_stilljcapt((int *) arg);
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	default:
14048c2ecf20Sopenharmony_ci		return -ENOTTY;
14058c2ecf20Sopenharmony_ci	}
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci}
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_cistatic __poll_t meye_poll(struct file *file, poll_table *wait)
14108c2ecf20Sopenharmony_ci{
14118c2ecf20Sopenharmony_ci	__poll_t res = v4l2_ctrl_poll(file, wait);
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
14148c2ecf20Sopenharmony_ci	poll_wait(file, &meye.proc_list, wait);
14158c2ecf20Sopenharmony_ci	if (kfifo_len(&meye.doneq))
14168c2ecf20Sopenharmony_ci		res |= EPOLLIN | EPOLLRDNORM;
14178c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
14188c2ecf20Sopenharmony_ci	return res;
14198c2ecf20Sopenharmony_ci}
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_cistatic void meye_vm_open(struct vm_area_struct *vma)
14228c2ecf20Sopenharmony_ci{
14238c2ecf20Sopenharmony_ci	long idx = (long)vma->vm_private_data;
14248c2ecf20Sopenharmony_ci	meye.vma_use_count[idx]++;
14258c2ecf20Sopenharmony_ci}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_cistatic void meye_vm_close(struct vm_area_struct *vma)
14288c2ecf20Sopenharmony_ci{
14298c2ecf20Sopenharmony_ci	long idx = (long)vma->vm_private_data;
14308c2ecf20Sopenharmony_ci	meye.vma_use_count[idx]--;
14318c2ecf20Sopenharmony_ci}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_cistatic const struct vm_operations_struct meye_vm_ops = {
14348c2ecf20Sopenharmony_ci	.open		= meye_vm_open,
14358c2ecf20Sopenharmony_ci	.close		= meye_vm_close,
14368c2ecf20Sopenharmony_ci};
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_cistatic int meye_mmap(struct file *file, struct vm_area_struct *vma)
14398c2ecf20Sopenharmony_ci{
14408c2ecf20Sopenharmony_ci	unsigned long start = vma->vm_start;
14418c2ecf20Sopenharmony_ci	unsigned long size = vma->vm_end - vma->vm_start;
14428c2ecf20Sopenharmony_ci	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
14438c2ecf20Sopenharmony_ci	unsigned long page, pos;
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci	mutex_lock(&meye.lock);
14468c2ecf20Sopenharmony_ci	if (size > gbuffers * gbufsize || offset > gbuffers * gbufsize - size) {
14478c2ecf20Sopenharmony_ci		mutex_unlock(&meye.lock);
14488c2ecf20Sopenharmony_ci		return -EINVAL;
14498c2ecf20Sopenharmony_ci	}
14508c2ecf20Sopenharmony_ci	if (!meye.grab_fbuffer) {
14518c2ecf20Sopenharmony_ci		int i;
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci		/* lazy allocation */
14548c2ecf20Sopenharmony_ci		meye.grab_fbuffer = rvmalloc(gbuffers*gbufsize);
14558c2ecf20Sopenharmony_ci		if (!meye.grab_fbuffer) {
14568c2ecf20Sopenharmony_ci			printk(KERN_ERR "meye: v4l framebuffer allocation failed\n");
14578c2ecf20Sopenharmony_ci			mutex_unlock(&meye.lock);
14588c2ecf20Sopenharmony_ci			return -ENOMEM;
14598c2ecf20Sopenharmony_ci		}
14608c2ecf20Sopenharmony_ci		for (i = 0; i < gbuffers; i++)
14618c2ecf20Sopenharmony_ci			meye.vma_use_count[i] = 0;
14628c2ecf20Sopenharmony_ci	}
14638c2ecf20Sopenharmony_ci	pos = (unsigned long)meye.grab_fbuffer + offset;
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	while (size > 0) {
14668c2ecf20Sopenharmony_ci		page = vmalloc_to_pfn((void *)pos);
14678c2ecf20Sopenharmony_ci		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
14688c2ecf20Sopenharmony_ci			mutex_unlock(&meye.lock);
14698c2ecf20Sopenharmony_ci			return -EAGAIN;
14708c2ecf20Sopenharmony_ci		}
14718c2ecf20Sopenharmony_ci		start += PAGE_SIZE;
14728c2ecf20Sopenharmony_ci		pos += PAGE_SIZE;
14738c2ecf20Sopenharmony_ci		if (size > PAGE_SIZE)
14748c2ecf20Sopenharmony_ci			size -= PAGE_SIZE;
14758c2ecf20Sopenharmony_ci		else
14768c2ecf20Sopenharmony_ci			size = 0;
14778c2ecf20Sopenharmony_ci	}
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	vma->vm_ops = &meye_vm_ops;
14808c2ecf20Sopenharmony_ci	vma->vm_flags &= ~VM_IO;	/* not I/O memory */
14818c2ecf20Sopenharmony_ci	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
14828c2ecf20Sopenharmony_ci	vma->vm_private_data = (void *) (offset / gbufsize);
14838c2ecf20Sopenharmony_ci	meye_vm_open(vma);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	mutex_unlock(&meye.lock);
14868c2ecf20Sopenharmony_ci	return 0;
14878c2ecf20Sopenharmony_ci}
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_cistatic const struct v4l2_file_operations meye_fops = {
14908c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
14918c2ecf20Sopenharmony_ci	.open		= meye_open,
14928c2ecf20Sopenharmony_ci	.release	= meye_release,
14938c2ecf20Sopenharmony_ci	.mmap		= meye_mmap,
14948c2ecf20Sopenharmony_ci	.unlocked_ioctl	= video_ioctl2,
14958c2ecf20Sopenharmony_ci	.poll		= meye_poll,
14968c2ecf20Sopenharmony_ci};
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_cistatic const struct v4l2_ioctl_ops meye_ioctl_ops = {
14998c2ecf20Sopenharmony_ci	.vidioc_querycap	= vidioc_querycap,
15008c2ecf20Sopenharmony_ci	.vidioc_enum_input	= vidioc_enum_input,
15018c2ecf20Sopenharmony_ci	.vidioc_g_input		= vidioc_g_input,
15028c2ecf20Sopenharmony_ci	.vidioc_s_input		= vidioc_s_input,
15038c2ecf20Sopenharmony_ci	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
15048c2ecf20Sopenharmony_ci	.vidioc_try_fmt_vid_cap	= vidioc_try_fmt_vid_cap,
15058c2ecf20Sopenharmony_ci	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
15068c2ecf20Sopenharmony_ci	.vidioc_s_fmt_vid_cap	= vidioc_s_fmt_vid_cap,
15078c2ecf20Sopenharmony_ci	.vidioc_reqbufs		= vidioc_reqbufs,
15088c2ecf20Sopenharmony_ci	.vidioc_querybuf	= vidioc_querybuf,
15098c2ecf20Sopenharmony_ci	.vidioc_qbuf		= vidioc_qbuf,
15108c2ecf20Sopenharmony_ci	.vidioc_dqbuf		= vidioc_dqbuf,
15118c2ecf20Sopenharmony_ci	.vidioc_streamon	= vidioc_streamon,
15128c2ecf20Sopenharmony_ci	.vidioc_streamoff	= vidioc_streamoff,
15138c2ecf20Sopenharmony_ci	.vidioc_log_status	= v4l2_ctrl_log_status,
15148c2ecf20Sopenharmony_ci	.vidioc_subscribe_event	= v4l2_ctrl_subscribe_event,
15158c2ecf20Sopenharmony_ci	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
15168c2ecf20Sopenharmony_ci	.vidioc_default		= vidioc_default,
15178c2ecf20Sopenharmony_ci};
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_cistatic const struct video_device meye_template = {
15208c2ecf20Sopenharmony_ci	.name		= "meye",
15218c2ecf20Sopenharmony_ci	.fops		= &meye_fops,
15228c2ecf20Sopenharmony_ci	.ioctl_ops	= &meye_ioctl_ops,
15238c2ecf20Sopenharmony_ci	.release	= video_device_release_empty,
15248c2ecf20Sopenharmony_ci	.device_caps	= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING,
15258c2ecf20Sopenharmony_ci};
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops meye_ctrl_ops = {
15288c2ecf20Sopenharmony_ci	.s_ctrl = meye_s_ctrl,
15298c2ecf20Sopenharmony_ci};
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_cistatic int __maybe_unused meye_suspend(struct device *dev)
15328c2ecf20Sopenharmony_ci{
15338c2ecf20Sopenharmony_ci	meye.pm_mchip_mode = meye.mchip_mode;
15348c2ecf20Sopenharmony_ci	mchip_hic_stop();
15358c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_INTA, 0x0);
15368c2ecf20Sopenharmony_ci	return 0;
15378c2ecf20Sopenharmony_ci}
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_cistatic int __maybe_unused meye_resume(struct device *dev)
15408c2ecf20Sopenharmony_ci{
15418c2ecf20Sopenharmony_ci	pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
15448c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
15458c2ecf20Sopenharmony_ci	msleep(1);
15468c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
15478c2ecf20Sopenharmony_ci	msleep(1);
15488c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_PCI_MODE, 5);
15498c2ecf20Sopenharmony_ci	msleep(1);
15508c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	switch (meye.pm_mchip_mode) {
15538c2ecf20Sopenharmony_ci	case MCHIP_HIC_MODE_CONT_OUT:
15548c2ecf20Sopenharmony_ci		mchip_continuous_start();
15558c2ecf20Sopenharmony_ci		break;
15568c2ecf20Sopenharmony_ci	case MCHIP_HIC_MODE_CONT_COMP:
15578c2ecf20Sopenharmony_ci		mchip_cont_compression_start();
15588c2ecf20Sopenharmony_ci		break;
15598c2ecf20Sopenharmony_ci	}
15608c2ecf20Sopenharmony_ci	return 0;
15618c2ecf20Sopenharmony_ci}
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_cistatic int meye_probe(struct pci_dev *pcidev, const struct pci_device_id *ent)
15648c2ecf20Sopenharmony_ci{
15658c2ecf20Sopenharmony_ci	static const struct v4l2_ctrl_config ctrl_agc = {
15668c2ecf20Sopenharmony_ci		.id = V4L2_CID_MEYE_AGC,
15678c2ecf20Sopenharmony_ci		.type = V4L2_CTRL_TYPE_INTEGER,
15688c2ecf20Sopenharmony_ci		.ops = &meye_ctrl_ops,
15698c2ecf20Sopenharmony_ci		.name = "AGC",
15708c2ecf20Sopenharmony_ci		.max = 63,
15718c2ecf20Sopenharmony_ci		.step = 1,
15728c2ecf20Sopenharmony_ci		.def = 48,
15738c2ecf20Sopenharmony_ci		.flags = V4L2_CTRL_FLAG_SLIDER,
15748c2ecf20Sopenharmony_ci	};
15758c2ecf20Sopenharmony_ci	static const struct v4l2_ctrl_config ctrl_picture = {
15768c2ecf20Sopenharmony_ci		.id = V4L2_CID_MEYE_PICTURE,
15778c2ecf20Sopenharmony_ci		.type = V4L2_CTRL_TYPE_INTEGER,
15788c2ecf20Sopenharmony_ci		.ops = &meye_ctrl_ops,
15798c2ecf20Sopenharmony_ci		.name = "Picture",
15808c2ecf20Sopenharmony_ci		.max = 63,
15818c2ecf20Sopenharmony_ci		.step = 1,
15828c2ecf20Sopenharmony_ci	};
15838c2ecf20Sopenharmony_ci	static const struct v4l2_ctrl_config ctrl_framerate = {
15848c2ecf20Sopenharmony_ci		.id = V4L2_CID_MEYE_FRAMERATE,
15858c2ecf20Sopenharmony_ci		.type = V4L2_CTRL_TYPE_INTEGER,
15868c2ecf20Sopenharmony_ci		.ops = &meye_ctrl_ops,
15878c2ecf20Sopenharmony_ci		.name = "Framerate",
15888c2ecf20Sopenharmony_ci		.max = 31,
15898c2ecf20Sopenharmony_ci		.step = 1,
15908c2ecf20Sopenharmony_ci	};
15918c2ecf20Sopenharmony_ci	struct v4l2_device *v4l2_dev = &meye.v4l2_dev;
15928c2ecf20Sopenharmony_ci	int ret = -EBUSY;
15938c2ecf20Sopenharmony_ci	unsigned long mchip_adr;
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	if (meye.mchip_dev != NULL) {
15968c2ecf20Sopenharmony_ci		printk(KERN_ERR "meye: only one device allowed!\n");
15978c2ecf20Sopenharmony_ci		return ret;
15988c2ecf20Sopenharmony_ci	}
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci	ret = v4l2_device_register(&pcidev->dev, v4l2_dev);
16018c2ecf20Sopenharmony_ci	if (ret < 0) {
16028c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
16038c2ecf20Sopenharmony_ci		return ret;
16048c2ecf20Sopenharmony_ci	}
16058c2ecf20Sopenharmony_ci	ret = -ENOMEM;
16068c2ecf20Sopenharmony_ci	meye.mchip_dev = pcidev;
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	meye.grab_temp = vmalloc(array_size(PAGE_SIZE, MCHIP_NB_PAGES_MJPEG));
16098c2ecf20Sopenharmony_ci	if (!meye.grab_temp)
16108c2ecf20Sopenharmony_ci		goto outvmalloc;
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	spin_lock_init(&meye.grabq_lock);
16138c2ecf20Sopenharmony_ci	if (kfifo_alloc(&meye.grabq, sizeof(int) * MEYE_MAX_BUFNBRS,
16148c2ecf20Sopenharmony_ci			GFP_KERNEL))
16158c2ecf20Sopenharmony_ci		goto outkfifoalloc1;
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ci	spin_lock_init(&meye.doneq_lock);
16188c2ecf20Sopenharmony_ci	if (kfifo_alloc(&meye.doneq, sizeof(int) * MEYE_MAX_BUFNBRS,
16198c2ecf20Sopenharmony_ci			GFP_KERNEL))
16208c2ecf20Sopenharmony_ci		goto outkfifoalloc2;
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci	meye.vdev = meye_template;
16238c2ecf20Sopenharmony_ci	meye.vdev.v4l2_dev = &meye.v4l2_dev;
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	ret = sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 1);
16268c2ecf20Sopenharmony_ci	if (ret) {
16278c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "meye: unable to power on the camera\n");
16288c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "meye: did you enable the camera in sonypi using the module options ?\n");
16298c2ecf20Sopenharmony_ci		goto outsonypienable;
16308c2ecf20Sopenharmony_ci	}
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci	ret = pci_enable_device(meye.mchip_dev);
16338c2ecf20Sopenharmony_ci	if (ret) {
16348c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "meye: pci_enable_device failed\n");
16358c2ecf20Sopenharmony_ci		goto outenabledev;
16368c2ecf20Sopenharmony_ci	}
16378c2ecf20Sopenharmony_ci
16388c2ecf20Sopenharmony_ci	ret = -EIO;
16398c2ecf20Sopenharmony_ci	mchip_adr = pci_resource_start(meye.mchip_dev,0);
16408c2ecf20Sopenharmony_ci	if (!mchip_adr) {
16418c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "meye: mchip has no device base address\n");
16428c2ecf20Sopenharmony_ci		goto outregions;
16438c2ecf20Sopenharmony_ci	}
16448c2ecf20Sopenharmony_ci	if (!request_mem_region(pci_resource_start(meye.mchip_dev, 0),
16458c2ecf20Sopenharmony_ci				pci_resource_len(meye.mchip_dev, 0),
16468c2ecf20Sopenharmony_ci				"meye")) {
16478c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "meye: request_mem_region failed\n");
16488c2ecf20Sopenharmony_ci		goto outregions;
16498c2ecf20Sopenharmony_ci	}
16508c2ecf20Sopenharmony_ci	meye.mchip_mmregs = ioremap(mchip_adr, MCHIP_MM_REGS);
16518c2ecf20Sopenharmony_ci	if (!meye.mchip_mmregs) {
16528c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "meye: ioremap failed\n");
16538c2ecf20Sopenharmony_ci		goto outremap;
16548c2ecf20Sopenharmony_ci	}
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci	meye.mchip_irq = pcidev->irq;
16578c2ecf20Sopenharmony_ci	if (request_irq(meye.mchip_irq, meye_irq,
16588c2ecf20Sopenharmony_ci			IRQF_SHARED, "meye", meye_irq)) {
16598c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "request_irq failed\n");
16608c2ecf20Sopenharmony_ci		goto outreqirq;
16618c2ecf20Sopenharmony_ci	}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci	pci_write_config_byte(meye.mchip_dev, PCI_CACHE_LINE_SIZE, 8);
16648c2ecf20Sopenharmony_ci	pci_write_config_byte(meye.mchip_dev, PCI_LATENCY_TIMER, 64);
16658c2ecf20Sopenharmony_ci
16668c2ecf20Sopenharmony_ci	pci_set_master(meye.mchip_dev);
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_ci	/* Ask the camera to perform a soft reset. */
16698c2ecf20Sopenharmony_ci	pci_write_config_word(meye.mchip_dev, MCHIP_PCI_SOFTRESET_SET, 1);
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_CMD, 0);
16728c2ecf20Sopenharmony_ci	mchip_delay(MCHIP_HIC_STATUS, MCHIP_HIC_STATUS_IDLE);
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	msleep(1);
16758c2ecf20Sopenharmony_ci	mchip_set(MCHIP_VRJ_SOFT_RESET, 1);
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	msleep(1);
16788c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_PCI_MODE, 5);
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci	msleep(1);
16818c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_INTA, MCHIP_MM_INTA_HIC_1_MASK);
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	mutex_init(&meye.lock);
16848c2ecf20Sopenharmony_ci	init_waitqueue_head(&meye.proc_list);
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&meye.hdl, 3);
16878c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
16888c2ecf20Sopenharmony_ci			  V4L2_CID_BRIGHTNESS, 0, 63, 1, 32);
16898c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
16908c2ecf20Sopenharmony_ci			  V4L2_CID_HUE, 0, 63, 1, 32);
16918c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
16928c2ecf20Sopenharmony_ci			  V4L2_CID_CONTRAST, 0, 63, 1, 32);
16938c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
16948c2ecf20Sopenharmony_ci			  V4L2_CID_SATURATION, 0, 63, 1, 32);
16958c2ecf20Sopenharmony_ci	v4l2_ctrl_new_custom(&meye.hdl, &ctrl_agc, NULL);
16968c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
16978c2ecf20Sopenharmony_ci			  V4L2_CID_SHARPNESS, 0, 63, 1, 32);
16988c2ecf20Sopenharmony_ci	v4l2_ctrl_new_custom(&meye.hdl, &ctrl_picture, NULL);
16998c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&meye.hdl, &meye_ctrl_ops,
17008c2ecf20Sopenharmony_ci			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 0, 10, 1, 8);
17018c2ecf20Sopenharmony_ci	v4l2_ctrl_new_custom(&meye.hdl, &ctrl_framerate, NULL);
17028c2ecf20Sopenharmony_ci	if (meye.hdl.error) {
17038c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "couldn't register controls\n");
17048c2ecf20Sopenharmony_ci		goto outvideoreg;
17058c2ecf20Sopenharmony_ci	}
17068c2ecf20Sopenharmony_ci
17078c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_setup(&meye.hdl);
17088c2ecf20Sopenharmony_ci	meye.vdev.ctrl_handler = &meye.hdl;
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_ci	if (video_register_device(&meye.vdev, VFL_TYPE_VIDEO,
17118c2ecf20Sopenharmony_ci				  video_nr) < 0) {
17128c2ecf20Sopenharmony_ci		v4l2_err(v4l2_dev, "video_register_device failed\n");
17138c2ecf20Sopenharmony_ci		goto outvideoreg;
17148c2ecf20Sopenharmony_ci	}
17158c2ecf20Sopenharmony_ci
17168c2ecf20Sopenharmony_ci	v4l2_info(v4l2_dev, "Motion Eye Camera Driver v%s.\n",
17178c2ecf20Sopenharmony_ci	       MEYE_DRIVER_VERSION);
17188c2ecf20Sopenharmony_ci	v4l2_info(v4l2_dev, "mchip KL5A72002 rev. %d, base %lx, irq %d\n",
17198c2ecf20Sopenharmony_ci	       meye.mchip_dev->revision, mchip_adr, meye.mchip_irq);
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	return 0;
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_cioutvideoreg:
17248c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&meye.hdl);
17258c2ecf20Sopenharmony_ci	free_irq(meye.mchip_irq, meye_irq);
17268c2ecf20Sopenharmony_cioutreqirq:
17278c2ecf20Sopenharmony_ci	iounmap(meye.mchip_mmregs);
17288c2ecf20Sopenharmony_cioutremap:
17298c2ecf20Sopenharmony_ci	release_mem_region(pci_resource_start(meye.mchip_dev, 0),
17308c2ecf20Sopenharmony_ci			   pci_resource_len(meye.mchip_dev, 0));
17318c2ecf20Sopenharmony_cioutregions:
17328c2ecf20Sopenharmony_ci	pci_disable_device(meye.mchip_dev);
17338c2ecf20Sopenharmony_cioutenabledev:
17348c2ecf20Sopenharmony_ci	sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
17358c2ecf20Sopenharmony_cioutsonypienable:
17368c2ecf20Sopenharmony_ci	kfifo_free(&meye.doneq);
17378c2ecf20Sopenharmony_cioutkfifoalloc2:
17388c2ecf20Sopenharmony_ci	kfifo_free(&meye.grabq);
17398c2ecf20Sopenharmony_cioutkfifoalloc1:
17408c2ecf20Sopenharmony_ci	vfree(meye.grab_temp);
17418c2ecf20Sopenharmony_cioutvmalloc:
17428c2ecf20Sopenharmony_ci	return ret;
17438c2ecf20Sopenharmony_ci}
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_cistatic void meye_remove(struct pci_dev *pcidev)
17468c2ecf20Sopenharmony_ci{
17478c2ecf20Sopenharmony_ci	video_unregister_device(&meye.vdev);
17488c2ecf20Sopenharmony_ci
17498c2ecf20Sopenharmony_ci	mchip_hic_stop();
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci	mchip_dma_free();
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	/* disable interrupts */
17548c2ecf20Sopenharmony_ci	mchip_set(MCHIP_MM_INTA, 0x0);
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci	free_irq(meye.mchip_irq, meye_irq);
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci	iounmap(meye.mchip_mmregs);
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci	release_mem_region(pci_resource_start(meye.mchip_dev, 0),
17618c2ecf20Sopenharmony_ci			   pci_resource_len(meye.mchip_dev, 0));
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	pci_disable_device(meye.mchip_dev);
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci	sony_pic_camera_command(SONY_PIC_COMMAND_SETCAMERA, 0);
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ci	kfifo_free(&meye.doneq);
17688c2ecf20Sopenharmony_ci	kfifo_free(&meye.grabq);
17698c2ecf20Sopenharmony_ci
17708c2ecf20Sopenharmony_ci	vfree(meye.grab_temp);
17718c2ecf20Sopenharmony_ci
17728c2ecf20Sopenharmony_ci	if (meye.grab_fbuffer) {
17738c2ecf20Sopenharmony_ci		rvfree(meye.grab_fbuffer, gbuffers*gbufsize);
17748c2ecf20Sopenharmony_ci		meye.grab_fbuffer = NULL;
17758c2ecf20Sopenharmony_ci	}
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci	printk(KERN_INFO "meye: removed\n");
17788c2ecf20Sopenharmony_ci}
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_cistatic const struct pci_device_id meye_pci_tbl[] = {
17818c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(KAWASAKI, PCI_DEVICE_ID_MCHIP_KL5A72002), 0 },
17828c2ecf20Sopenharmony_ci	{ }
17838c2ecf20Sopenharmony_ci};
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, meye_pci_tbl);
17868c2ecf20Sopenharmony_ci
17878c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(meye_pm_ops, meye_suspend, meye_resume);
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_cistatic struct pci_driver meye_driver = {
17908c2ecf20Sopenharmony_ci	.name		= "meye",
17918c2ecf20Sopenharmony_ci	.id_table	= meye_pci_tbl,
17928c2ecf20Sopenharmony_ci	.probe		= meye_probe,
17938c2ecf20Sopenharmony_ci	.remove		= meye_remove,
17948c2ecf20Sopenharmony_ci	.driver.pm	= &meye_pm_ops,
17958c2ecf20Sopenharmony_ci};
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_cistatic int __init meye_init(void)
17988c2ecf20Sopenharmony_ci{
17998c2ecf20Sopenharmony_ci	gbuffers = max(2, min((int)gbuffers, MEYE_MAX_BUFNBRS));
18008c2ecf20Sopenharmony_ci	if (gbufsize > MEYE_MAX_BUFSIZE)
18018c2ecf20Sopenharmony_ci		gbufsize = MEYE_MAX_BUFSIZE;
18028c2ecf20Sopenharmony_ci	gbufsize = PAGE_ALIGN(gbufsize);
18038c2ecf20Sopenharmony_ci	printk(KERN_INFO "meye: using %d buffers with %dk (%dk total) for capture\n",
18048c2ecf20Sopenharmony_ci			 gbuffers,
18058c2ecf20Sopenharmony_ci			 gbufsize / 1024, gbuffers * gbufsize / 1024);
18068c2ecf20Sopenharmony_ci	return pci_register_driver(&meye_driver);
18078c2ecf20Sopenharmony_ci}
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_cistatic void __exit meye_exit(void)
18108c2ecf20Sopenharmony_ci{
18118c2ecf20Sopenharmony_ci	pci_unregister_driver(&meye_driver);
18128c2ecf20Sopenharmony_ci}
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_cimodule_init(meye_init);
18158c2ecf20Sopenharmony_cimodule_exit(meye_exit);
1816