xref: /openbmc/linux/drivers/media/usb/stk1160/stk1160-v4l.c (revision fbe04b49a54e31f4321d632270207f0e6304cd16)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab  * STK1160 driver
40c0d06caSMauro Carvalho Chehab  *
50c0d06caSMauro Carvalho Chehab  * Copyright (C) 2012 Ezequiel Garcia
60c0d06caSMauro Carvalho Chehab  * <elezegarcia--a.t--gmail.com>
70c0d06caSMauro Carvalho Chehab  *
80c0d06caSMauro Carvalho Chehab  * Based on Easycap driver by R.M. Thomas
90c0d06caSMauro Carvalho Chehab  *	Copyright (C) 2010 R.M. Thomas
100c0d06caSMauro Carvalho Chehab  *	<rmthomas--a.t--sciolus.org>
110c0d06caSMauro Carvalho Chehab  */
120c0d06caSMauro Carvalho Chehab 
130c0d06caSMauro Carvalho Chehab #include <linux/module.h>
140c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
150c0d06caSMauro Carvalho Chehab #include <linux/mm.h>
160c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
170c0d06caSMauro Carvalho Chehab 
180c0d06caSMauro Carvalho Chehab #include <linux/videodev2.h>
190c0d06caSMauro Carvalho Chehab #include <media/v4l2-device.h>
200c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
210c0d06caSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
220c0d06caSMauro Carvalho Chehab #include <media/v4l2-fh.h>
230c0d06caSMauro Carvalho Chehab #include <media/v4l2-event.h>
240c0d06caSMauro Carvalho Chehab #include <media/videobuf2-vmalloc.h>
250c0d06caSMauro Carvalho Chehab 
26b5dcee22SMauro Carvalho Chehab #include <media/i2c/saa7115.h>
270c0d06caSMauro Carvalho Chehab 
280c0d06caSMauro Carvalho Chehab #include "stk1160.h"
290c0d06caSMauro Carvalho Chehab #include "stk1160-reg.h"
300c0d06caSMauro Carvalho Chehab 
310c0d06caSMauro Carvalho Chehab static bool keep_buffers;
320c0d06caSMauro Carvalho Chehab module_param(keep_buffers, bool, 0644);
330c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
340c0d06caSMauro Carvalho Chehab 
35d3194520SEzequiel Garcia enum stk1160_decimate_mode {
36d3194520SEzequiel Garcia 	STK1160_DECIMATE_MORE_THAN_HALF,
37d3194520SEzequiel Garcia 	STK1160_DECIMATE_LESS_THAN_HALF,
38d3194520SEzequiel Garcia };
39d3194520SEzequiel Garcia 
40d3194520SEzequiel Garcia struct stk1160_decimate_ctrl {
41d3194520SEzequiel Garcia 	bool col_en, row_en;
42d3194520SEzequiel Garcia 	enum stk1160_decimate_mode col_mode, row_mode;
43d3194520SEzequiel Garcia 	unsigned int col_n, row_n;
44d3194520SEzequiel Garcia };
45d3194520SEzequiel Garcia 
460c0d06caSMauro Carvalho Chehab /* supported video standards */
470c0d06caSMauro Carvalho Chehab static struct stk1160_fmt format[] = {
480c0d06caSMauro Carvalho Chehab 	{
490c0d06caSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_UYVY,
500c0d06caSMauro Carvalho Chehab 		.depth    = 16,
510c0d06caSMauro Carvalho Chehab 	}
520c0d06caSMauro Carvalho Chehab };
530c0d06caSMauro Carvalho Chehab 
54d3194520SEzequiel Garcia /*
55d3194520SEzequiel Garcia  * Helper to find the next divisor that results in modulo being zero.
56d3194520SEzequiel Garcia  * This is required to guarantee valid decimation unit counts.
57d3194520SEzequiel Garcia  */
58d3194520SEzequiel Garcia static unsigned int
59d3194520SEzequiel Garcia div_round_integer(unsigned int x, unsigned int y)
60d3194520SEzequiel Garcia {
61d3194520SEzequiel Garcia 	for (;; y++) {
62d3194520SEzequiel Garcia 		if (x % y == 0)
63d3194520SEzequiel Garcia 			return x / y;
64d3194520SEzequiel Garcia 	}
65d3194520SEzequiel Garcia }
66d3194520SEzequiel Garcia 
670c0d06caSMauro Carvalho Chehab static void stk1160_set_std(struct stk1160 *dev)
680c0d06caSMauro Carvalho Chehab {
690c0d06caSMauro Carvalho Chehab 	int i;
700c0d06caSMauro Carvalho Chehab 
710c0d06caSMauro Carvalho Chehab 	static struct regval std525[] = {
720c0d06caSMauro Carvalho Chehab 
730c0d06caSMauro Carvalho Chehab 		/* 720x480 */
740c0d06caSMauro Carvalho Chehab 
750c0d06caSMauro Carvalho Chehab 		/* Frame start */
760c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO_STX_L, 0x0000},
770c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO_STX_H, 0x0000},
780c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO_STY_L, 0x0003},
790c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO_STY_H, 0x0000},
800c0d06caSMauro Carvalho Chehab 
810c0d06caSMauro Carvalho Chehab 		/* Frame end */
820c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO_ENX_L, 0x05a0},
830c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO_ENX_H, 0x0005},
840c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO_ENY_L, 0x00f3},
850c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO_ENY_H, 0x0000},
860c0d06caSMauro Carvalho Chehab 
870c0d06caSMauro Carvalho Chehab 		{0xffff, 0xffff}
880c0d06caSMauro Carvalho Chehab 	};
890c0d06caSMauro Carvalho Chehab 
900c0d06caSMauro Carvalho Chehab 	static struct regval std625[] = {
910c0d06caSMauro Carvalho Chehab 
920c0d06caSMauro Carvalho Chehab 		/* 720x576 */
930c0d06caSMauro Carvalho Chehab 
940c0d06caSMauro Carvalho Chehab 		/* TODO: Each line of frame has some junk at the end */
950c0d06caSMauro Carvalho Chehab 		/* Frame start */
960c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO,   0x0000},
970c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO+1, 0x0000},
980c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO+2, 0x0001},
990c0d06caSMauro Carvalho Chehab 		{STK116_CFSPO+3, 0x0000},
1000c0d06caSMauro Carvalho Chehab 
1010c0d06caSMauro Carvalho Chehab 		/* Frame end */
1020c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO,   0x05a0},
1030c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO+1, 0x0005},
1040c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO+2, 0x0121},
1050c0d06caSMauro Carvalho Chehab 		{STK116_CFEPO+3, 0x0001},
1060c0d06caSMauro Carvalho Chehab 
1070c0d06caSMauro Carvalho Chehab 		{0xffff, 0xffff}
1080c0d06caSMauro Carvalho Chehab 	};
1090c0d06caSMauro Carvalho Chehab 
1100c0d06caSMauro Carvalho Chehab 	if (dev->norm & V4L2_STD_525_60) {
1110c0d06caSMauro Carvalho Chehab 		stk1160_dbg("registers to NTSC like standard\n");
1120c0d06caSMauro Carvalho Chehab 		for (i = 0; std525[i].reg != 0xffff; i++)
1130c0d06caSMauro Carvalho Chehab 			stk1160_write_reg(dev, std525[i].reg, std525[i].val);
1140c0d06caSMauro Carvalho Chehab 	} else {
1150c0d06caSMauro Carvalho Chehab 		stk1160_dbg("registers to PAL like standard\n");
1160c0d06caSMauro Carvalho Chehab 		for (i = 0; std625[i].reg != 0xffff; i++)
1170c0d06caSMauro Carvalho Chehab 			stk1160_write_reg(dev, std625[i].reg, std625[i].val);
1180c0d06caSMauro Carvalho Chehab 	}
1190c0d06caSMauro Carvalho Chehab 
1200c0d06caSMauro Carvalho Chehab }
1210c0d06caSMauro Carvalho Chehab 
122d3194520SEzequiel Garcia static void stk1160_set_fmt(struct stk1160 *dev,
123d3194520SEzequiel Garcia 			    struct stk1160_decimate_ctrl *ctrl)
124d3194520SEzequiel Garcia {
125d3194520SEzequiel Garcia 	u32 val = 0;
126d3194520SEzequiel Garcia 
127d3194520SEzequiel Garcia 	if (ctrl) {
128d3194520SEzequiel Garcia 		/*
129d3194520SEzequiel Garcia 		 * Since the format is UYVY, the device must skip or send
130d3194520SEzequiel Garcia 		 * a number of rows/columns multiple of four. This way, the
131d3194520SEzequiel Garcia 		 * colour format is preserved. The STK1160_DEC_UNIT_SIZE bit
132d3194520SEzequiel Garcia 		 * does exactly this.
133d3194520SEzequiel Garcia 		 */
134d3194520SEzequiel Garcia 		val |= STK1160_DEC_UNIT_SIZE;
135d3194520SEzequiel Garcia 		val |= ctrl->col_en ? STK1160_H_DEC_EN : 0;
136d3194520SEzequiel Garcia 		val |= ctrl->row_en ? STK1160_V_DEC_EN : 0;
137d3194520SEzequiel Garcia 		val |= ctrl->col_mode ==
138d3194520SEzequiel Garcia 			STK1160_DECIMATE_MORE_THAN_HALF ?
139d3194520SEzequiel Garcia 			STK1160_H_DEC_MODE : 0;
140d3194520SEzequiel Garcia 		val |= ctrl->row_mode ==
141d3194520SEzequiel Garcia 			STK1160_DECIMATE_MORE_THAN_HALF ?
142d3194520SEzequiel Garcia 			STK1160_V_DEC_MODE : 0;
143d3194520SEzequiel Garcia 
144d3194520SEzequiel Garcia 		/* Horizontal count units */
145d3194520SEzequiel Garcia 		stk1160_write_reg(dev, STK1160_DMCTRL_H_UNITS, ctrl->col_n);
146d3194520SEzequiel Garcia 		/* Vertical count units */
147d3194520SEzequiel Garcia 		stk1160_write_reg(dev, STK1160_DMCTRL_V_UNITS, ctrl->row_n);
148d3194520SEzequiel Garcia 
149d3194520SEzequiel Garcia 		stk1160_dbg("decimate 0x%x, column units %d, row units %d\n",
150d3194520SEzequiel Garcia 			    val, ctrl->col_n, ctrl->row_n);
151d3194520SEzequiel Garcia 	}
152d3194520SEzequiel Garcia 
153d3194520SEzequiel Garcia 	/* Decimation control */
154d3194520SEzequiel Garcia 	stk1160_write_reg(dev, STK1160_DMCTRL, val);
155d3194520SEzequiel Garcia }
156d3194520SEzequiel Garcia 
1570c0d06caSMauro Carvalho Chehab /*
1580c0d06caSMauro Carvalho Chehab  * Set a new alternate setting.
1590c0d06caSMauro Carvalho Chehab  * Returns true is dev->max_pkt_size has changed, false otherwise.
1600c0d06caSMauro Carvalho Chehab  */
1610c0d06caSMauro Carvalho Chehab static bool stk1160_set_alternate(struct stk1160 *dev)
1620c0d06caSMauro Carvalho Chehab {
1630c0d06caSMauro Carvalho Chehab 	int i, prev_alt = dev->alt;
1640c0d06caSMauro Carvalho Chehab 	unsigned int min_pkt_size;
1650c0d06caSMauro Carvalho Chehab 	bool new_pkt_size;
1660c0d06caSMauro Carvalho Chehab 
1670c0d06caSMauro Carvalho Chehab 	/*
1680c0d06caSMauro Carvalho Chehab 	 * If we don't set right alternate,
1690c0d06caSMauro Carvalho Chehab 	 * then we will get a green screen with junk.
1700c0d06caSMauro Carvalho Chehab 	 */
1710c0d06caSMauro Carvalho Chehab 	min_pkt_size = STK1160_MIN_PKT_SIZE;
1720c0d06caSMauro Carvalho Chehab 
1730c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->num_alt; i++) {
1740c0d06caSMauro Carvalho Chehab 		/* stop when the selected alt setting offers enough bandwidth */
1750c0d06caSMauro Carvalho Chehab 		if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
1760c0d06caSMauro Carvalho Chehab 			dev->alt = i;
1770c0d06caSMauro Carvalho Chehab 			break;
1780c0d06caSMauro Carvalho Chehab 		/*
1790c0d06caSMauro Carvalho Chehab 		 * otherwise make sure that we end up with the maximum bandwidth
1800c0d06caSMauro Carvalho Chehab 		 * because the min_pkt_size equation might be wrong...
1810c0d06caSMauro Carvalho Chehab 		 */
1820c0d06caSMauro Carvalho Chehab 		} else if (dev->alt_max_pkt_size[i] >
1830c0d06caSMauro Carvalho Chehab 			   dev->alt_max_pkt_size[dev->alt])
1840c0d06caSMauro Carvalho Chehab 			dev->alt = i;
1850c0d06caSMauro Carvalho Chehab 	}
1860c0d06caSMauro Carvalho Chehab 
187890024adSEzequiel Garcia 	stk1160_dbg("setting alternate %d\n", dev->alt);
1880c0d06caSMauro Carvalho Chehab 
1890c0d06caSMauro Carvalho Chehab 	if (dev->alt != prev_alt) {
1900c0d06caSMauro Carvalho Chehab 		stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
1910c0d06caSMauro Carvalho Chehab 				min_pkt_size, dev->alt);
1920c0d06caSMauro Carvalho Chehab 		stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
1930c0d06caSMauro Carvalho Chehab 			       dev->alt, dev->alt_max_pkt_size[dev->alt]);
1940c0d06caSMauro Carvalho Chehab 		usb_set_interface(dev->udev, 0, dev->alt);
1950c0d06caSMauro Carvalho Chehab 	}
1960c0d06caSMauro Carvalho Chehab 
1970c0d06caSMauro Carvalho Chehab 	new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt];
1980c0d06caSMauro Carvalho Chehab 	dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
1990c0d06caSMauro Carvalho Chehab 
2000c0d06caSMauro Carvalho Chehab 	return new_pkt_size;
2010c0d06caSMauro Carvalho Chehab }
2020c0d06caSMauro Carvalho Chehab 
2030c0d06caSMauro Carvalho Chehab static int stk1160_start_streaming(struct stk1160 *dev)
2040c0d06caSMauro Carvalho Chehab {
2050c0d06caSMauro Carvalho Chehab 	bool new_pkt_size;
206f367cc1eSDan Carpenter 	int rc = 0;
207f367cc1eSDan Carpenter 	int i;
2080c0d06caSMauro Carvalho Chehab 
2090c0d06caSMauro Carvalho Chehab 	/* Check device presence */
2100c0d06caSMauro Carvalho Chehab 	if (!dev->udev)
2110c0d06caSMauro Carvalho Chehab 		return -ENODEV;
2120c0d06caSMauro Carvalho Chehab 
2130c0d06caSMauro Carvalho Chehab 	if (mutex_lock_interruptible(&dev->v4l_lock))
2140c0d06caSMauro Carvalho Chehab 		return -ERESTARTSYS;
2150c0d06caSMauro Carvalho Chehab 	/*
2160c0d06caSMauro Carvalho Chehab 	 * For some reason it is mandatory to set alternate *first*
2170c0d06caSMauro Carvalho Chehab 	 * and only *then* initialize isoc urbs.
2180c0d06caSMauro Carvalho Chehab 	 * Someone please explain me why ;)
2190c0d06caSMauro Carvalho Chehab 	 */
2200c0d06caSMauro Carvalho Chehab 	new_pkt_size = stk1160_set_alternate(dev);
2210c0d06caSMauro Carvalho Chehab 
2220c0d06caSMauro Carvalho Chehab 	/*
2230c0d06caSMauro Carvalho Chehab 	 * We (re)allocate isoc urbs if:
2240c0d06caSMauro Carvalho Chehab 	 * there is no allocated isoc urbs, OR
2250c0d06caSMauro Carvalho Chehab 	 * a new dev->max_pkt_size is detected
2260c0d06caSMauro Carvalho Chehab 	 */
2270c0d06caSMauro Carvalho Chehab 	if (!dev->isoc_ctl.num_bufs || new_pkt_size) {
2280c0d06caSMauro Carvalho Chehab 		rc = stk1160_alloc_isoc(dev);
2290c0d06caSMauro Carvalho Chehab 		if (rc < 0)
2308ac45649SEzequiel Garcia 			goto out_stop_hw;
2310c0d06caSMauro Carvalho Chehab 	}
2320c0d06caSMauro Carvalho Chehab 
2330c0d06caSMauro Carvalho Chehab 	/* submit urbs and enables IRQ */
2340c0d06caSMauro Carvalho Chehab 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
2350c0d06caSMauro Carvalho Chehab 		rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
2360c0d06caSMauro Carvalho Chehab 		if (rc) {
2370c0d06caSMauro Carvalho Chehab 			stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
2388ac45649SEzequiel Garcia 			goto out_uninit;
2390c0d06caSMauro Carvalho Chehab 		}
2400c0d06caSMauro Carvalho Chehab 	}
2410c0d06caSMauro Carvalho Chehab 
2420c0d06caSMauro Carvalho Chehab 	/* Start saa711x */
2430c0d06caSMauro Carvalho Chehab 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
2440c0d06caSMauro Carvalho Chehab 
245e3e30f63SHans Verkuil 	dev->sequence = 0;
246e3e30f63SHans Verkuil 
2470c0d06caSMauro Carvalho Chehab 	/* Start stk1160 */
2480c0d06caSMauro Carvalho Chehab 	stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
2490c0d06caSMauro Carvalho Chehab 	stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
2500c0d06caSMauro Carvalho Chehab 
2510c0d06caSMauro Carvalho Chehab 	stk1160_dbg("streaming started\n");
2520c0d06caSMauro Carvalho Chehab 
2538ac45649SEzequiel Garcia 	mutex_unlock(&dev->v4l_lock);
2548ac45649SEzequiel Garcia 
2558ac45649SEzequiel Garcia 	return 0;
2568ac45649SEzequiel Garcia 
2578ac45649SEzequiel Garcia out_uninit:
2588ac45649SEzequiel Garcia 	stk1160_uninit_isoc(dev);
2598ac45649SEzequiel Garcia out_stop_hw:
2608ac45649SEzequiel Garcia 	usb_set_interface(dev->udev, 0, 0);
261*fbe04b49SDafna Hirschfeld 	stk1160_clear_queue(dev, VB2_BUF_STATE_QUEUED);
2628ac45649SEzequiel Garcia 
2630c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dev->v4l_lock);
2640c0d06caSMauro Carvalho Chehab 
265f367cc1eSDan Carpenter 	return rc;
2660c0d06caSMauro Carvalho Chehab }
2670c0d06caSMauro Carvalho Chehab 
2680c0d06caSMauro Carvalho Chehab /* Must be called with v4l_lock hold */
2690c0d06caSMauro Carvalho Chehab static void stk1160_stop_hw(struct stk1160 *dev)
2700c0d06caSMauro Carvalho Chehab {
2710c0d06caSMauro Carvalho Chehab 	/* If the device is not physically present, there is nothing to do */
2720c0d06caSMauro Carvalho Chehab 	if (!dev->udev)
2730c0d06caSMauro Carvalho Chehab 		return;
2740c0d06caSMauro Carvalho Chehab 
2750c0d06caSMauro Carvalho Chehab 	/* set alternate 0 */
2760c0d06caSMauro Carvalho Chehab 	dev->alt = 0;
277890024adSEzequiel Garcia 	stk1160_dbg("setting alternate %d\n", dev->alt);
2780c0d06caSMauro Carvalho Chehab 	usb_set_interface(dev->udev, 0, 0);
2790c0d06caSMauro Carvalho Chehab 
2800c0d06caSMauro Carvalho Chehab 	/* Stop stk1160 */
2810c0d06caSMauro Carvalho Chehab 	stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
2820c0d06caSMauro Carvalho Chehab 	stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
2830c0d06caSMauro Carvalho Chehab 
2840c0d06caSMauro Carvalho Chehab 	/* Stop saa711x */
2850c0d06caSMauro Carvalho Chehab 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
2860c0d06caSMauro Carvalho Chehab }
2870c0d06caSMauro Carvalho Chehab 
2880c0d06caSMauro Carvalho Chehab static int stk1160_stop_streaming(struct stk1160 *dev)
2890c0d06caSMauro Carvalho Chehab {
2900c0d06caSMauro Carvalho Chehab 	if (mutex_lock_interruptible(&dev->v4l_lock))
2910c0d06caSMauro Carvalho Chehab 		return -ERESTARTSYS;
2920c0d06caSMauro Carvalho Chehab 
293aeff0927SEzequiel Garcia 	/*
294aeff0927SEzequiel Garcia 	 * Once URBs are cancelled, the URB complete handler
295aeff0927SEzequiel Garcia 	 * won't be running. This is required to safely release the
296aeff0927SEzequiel Garcia 	 * current buffer (dev->isoc_ctl.buf).
297aeff0927SEzequiel Garcia 	 */
2980c0d06caSMauro Carvalho Chehab 	stk1160_cancel_isoc(dev);
2990c0d06caSMauro Carvalho Chehab 
3000c0d06caSMauro Carvalho Chehab 	/*
3010c0d06caSMauro Carvalho Chehab 	 * It is possible to keep buffers around using a module parameter.
3020c0d06caSMauro Carvalho Chehab 	 * This is intended to avoid memory fragmentation.
3030c0d06caSMauro Carvalho Chehab 	 */
3040c0d06caSMauro Carvalho Chehab 	if (!keep_buffers)
3050c0d06caSMauro Carvalho Chehab 		stk1160_free_isoc(dev);
3060c0d06caSMauro Carvalho Chehab 
3070c0d06caSMauro Carvalho Chehab 	stk1160_stop_hw(dev);
3080c0d06caSMauro Carvalho Chehab 
309*fbe04b49SDafna Hirschfeld 	stk1160_clear_queue(dev, VB2_BUF_STATE_ERROR);
3100c0d06caSMauro Carvalho Chehab 
3110c0d06caSMauro Carvalho Chehab 	stk1160_dbg("streaming stopped\n");
3120c0d06caSMauro Carvalho Chehab 
3130c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dev->v4l_lock);
3140c0d06caSMauro Carvalho Chehab 
3150c0d06caSMauro Carvalho Chehab 	return 0;
3160c0d06caSMauro Carvalho Chehab }
3170c0d06caSMauro Carvalho Chehab 
318ff05c984SBhumika Goyal static const struct v4l2_file_operations stk1160_fops = {
3190c0d06caSMauro Carvalho Chehab 	.owner = THIS_MODULE,
3200c0d06caSMauro Carvalho Chehab 	.open = v4l2_fh_open,
3210c0d06caSMauro Carvalho Chehab 	.release = vb2_fop_release,
3220c0d06caSMauro Carvalho Chehab 	.read = vb2_fop_read,
3230c0d06caSMauro Carvalho Chehab 	.poll = vb2_fop_poll,
3240c0d06caSMauro Carvalho Chehab 	.mmap = vb2_fop_mmap,
3250c0d06caSMauro Carvalho Chehab 	.unlocked_ioctl = video_ioctl2,
3260c0d06caSMauro Carvalho Chehab };
3270c0d06caSMauro Carvalho Chehab 
3280c0d06caSMauro Carvalho Chehab /*
3290c0d06caSMauro Carvalho Chehab  * vidioc ioctls
3300c0d06caSMauro Carvalho Chehab  */
3310c0d06caSMauro Carvalho Chehab static int vidioc_querycap(struct file *file,
3320c0d06caSMauro Carvalho Chehab 		void *priv, struct v4l2_capability *cap)
3330c0d06caSMauro Carvalho Chehab {
3340c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
3350c0d06caSMauro Carvalho Chehab 
336cc1e6315SMauro Carvalho Chehab 	strscpy(cap->driver, "stk1160", sizeof(cap->driver));
337cc1e6315SMauro Carvalho Chehab 	strscpy(cap->card, "stk1160", sizeof(cap->card));
3380c0d06caSMauro Carvalho Chehab 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
3390c0d06caSMauro Carvalho Chehab 	return 0;
3400c0d06caSMauro Carvalho Chehab }
3410c0d06caSMauro Carvalho Chehab 
3420c0d06caSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
3430c0d06caSMauro Carvalho Chehab 		struct v4l2_fmtdesc *f)
3440c0d06caSMauro Carvalho Chehab {
3450c0d06caSMauro Carvalho Chehab 	if (f->index != 0)
3460c0d06caSMauro Carvalho Chehab 		return -EINVAL;
3470c0d06caSMauro Carvalho Chehab 
3480c0d06caSMauro Carvalho Chehab 	f->pixelformat = format[f->index].fourcc;
3490c0d06caSMauro Carvalho Chehab 	return 0;
3500c0d06caSMauro Carvalho Chehab }
3510c0d06caSMauro Carvalho Chehab 
3520c0d06caSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
3530c0d06caSMauro Carvalho Chehab 					struct v4l2_format *f)
3540c0d06caSMauro Carvalho Chehab {
3550c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
3560c0d06caSMauro Carvalho Chehab 
3570c0d06caSMauro Carvalho Chehab 	f->fmt.pix.width = dev->width;
3580c0d06caSMauro Carvalho Chehab 	f->fmt.pix.height = dev->height;
3590c0d06caSMauro Carvalho Chehab 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
3600c0d06caSMauro Carvalho Chehab 	f->fmt.pix.pixelformat = dev->fmt->fourcc;
3610c0d06caSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = dev->width * 2;
3620c0d06caSMauro Carvalho Chehab 	f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
3630c0d06caSMauro Carvalho Chehab 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
3640c0d06caSMauro Carvalho Chehab 
3650c0d06caSMauro Carvalho Chehab 	return 0;
3660c0d06caSMauro Carvalho Chehab }
3670c0d06caSMauro Carvalho Chehab 
368d3194520SEzequiel Garcia static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f,
369d3194520SEzequiel Garcia 			    struct stk1160_decimate_ctrl *ctrl)
370d3194520SEzequiel Garcia {
371d3194520SEzequiel Garcia 	unsigned int width, height;
372d3194520SEzequiel Garcia 	unsigned int base_width, base_height;
373d3194520SEzequiel Garcia 	unsigned int col_n, row_n;
374d3194520SEzequiel Garcia 	enum stk1160_decimate_mode col_mode, row_mode;
375d3194520SEzequiel Garcia 	bool col_en, row_en;
376d3194520SEzequiel Garcia 
377d3194520SEzequiel Garcia 	base_width = 720;
378d3194520SEzequiel Garcia 	base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576;
379d3194520SEzequiel Garcia 
380d3194520SEzequiel Garcia 	/* Minimum width and height is 5% the frame size */
381d3194520SEzequiel Garcia 	width = clamp_t(unsigned int, f->fmt.pix.width,
382d3194520SEzequiel Garcia 			base_width / 20, base_width);
383d3194520SEzequiel Garcia 	height = clamp_t(unsigned int, f->fmt.pix.height,
384d3194520SEzequiel Garcia 			base_height / 20, base_height);
385d3194520SEzequiel Garcia 
386d3194520SEzequiel Garcia 	/* Let's set default no decimation values */
387d3194520SEzequiel Garcia 	col_n = 0;
388d3194520SEzequiel Garcia 	row_n = 0;
389d3194520SEzequiel Garcia 	col_en = false;
390d3194520SEzequiel Garcia 	row_en = false;
391d3194520SEzequiel Garcia 	f->fmt.pix.width = base_width;
392d3194520SEzequiel Garcia 	f->fmt.pix.height = base_height;
393d3194520SEzequiel Garcia 	row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
394d3194520SEzequiel Garcia 	col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
395d3194520SEzequiel Garcia 
396d3194520SEzequiel Garcia 	if (width < base_width && width > base_width / 2) {
397d3194520SEzequiel Garcia 		/*
398d3194520SEzequiel Garcia 		 * The device will send count units for each
399d3194520SEzequiel Garcia 		 * unit skipped. This means count unit is:
400d3194520SEzequiel Garcia 		 *
401d3194520SEzequiel Garcia 		 * n = width / (frame width - width)
402d3194520SEzequiel Garcia 		 *
403d3194520SEzequiel Garcia 		 * And the width is:
404d3194520SEzequiel Garcia 		 *
405d3194520SEzequiel Garcia 		 * width = (n / n + 1) * frame width
406d3194520SEzequiel Garcia 		 */
407d3194520SEzequiel Garcia 		col_n = div_round_integer(width, base_width - width);
408d3194520SEzequiel Garcia 		if (col_n > 0 && col_n <= 255) {
409d3194520SEzequiel Garcia 			col_en = true;
410d3194520SEzequiel Garcia 			col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
411d3194520SEzequiel Garcia 			f->fmt.pix.width = (base_width * col_n) / (col_n + 1);
412d3194520SEzequiel Garcia 		}
413d3194520SEzequiel Garcia 
414d3194520SEzequiel Garcia 	} else if (width <= base_width / 2) {
415d3194520SEzequiel Garcia 
416d3194520SEzequiel Garcia 		/*
417d3194520SEzequiel Garcia 		 * The device will skip count units for each
418d3194520SEzequiel Garcia 		 * unit sent. This means count is:
419d3194520SEzequiel Garcia 		 *
420d3194520SEzequiel Garcia 		 * n = (frame width / width) - 1
421d3194520SEzequiel Garcia 		 *
422d3194520SEzequiel Garcia 		 * And the width is:
423d3194520SEzequiel Garcia 		 *
424d3194520SEzequiel Garcia 		 * width = frame width / (n + 1)
425d3194520SEzequiel Garcia 		 */
426d3194520SEzequiel Garcia 		col_n = div_round_integer(base_width, width) - 1;
427d3194520SEzequiel Garcia 		if (col_n > 0 && col_n <= 255) {
428d3194520SEzequiel Garcia 			col_en = true;
429d3194520SEzequiel Garcia 			col_mode = STK1160_DECIMATE_MORE_THAN_HALF;
430d3194520SEzequiel Garcia 			f->fmt.pix.width = base_width / (col_n + 1);
431d3194520SEzequiel Garcia 		}
432d3194520SEzequiel Garcia 	}
433d3194520SEzequiel Garcia 
434d3194520SEzequiel Garcia 	if (height < base_height && height > base_height / 2) {
435d3194520SEzequiel Garcia 		row_n = div_round_integer(height, base_height - height);
436d3194520SEzequiel Garcia 		if (row_n > 0 && row_n <= 255) {
437d3194520SEzequiel Garcia 			row_en = true;
438d3194520SEzequiel Garcia 			row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
439d3194520SEzequiel Garcia 			f->fmt.pix.height = (base_height * row_n) / (row_n + 1);
440d3194520SEzequiel Garcia 		}
441d3194520SEzequiel Garcia 
442d3194520SEzequiel Garcia 	} else if (height <= base_height / 2) {
443d3194520SEzequiel Garcia 		row_n = div_round_integer(base_height, height) - 1;
444d3194520SEzequiel Garcia 		if (row_n > 0 && row_n <= 255) {
445d3194520SEzequiel Garcia 			row_en = true;
446d3194520SEzequiel Garcia 			row_mode = STK1160_DECIMATE_MORE_THAN_HALF;
447d3194520SEzequiel Garcia 			f->fmt.pix.height = base_height / (row_n + 1);
448d3194520SEzequiel Garcia 		}
449d3194520SEzequiel Garcia 	}
450d3194520SEzequiel Garcia 
451d3194520SEzequiel Garcia 	f->fmt.pix.pixelformat = dev->fmt->fourcc;
452d3194520SEzequiel Garcia 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
453d3194520SEzequiel Garcia 	f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
454d3194520SEzequiel Garcia 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
455d3194520SEzequiel Garcia 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
456d3194520SEzequiel Garcia 
457d3194520SEzequiel Garcia 	if (ctrl) {
458d3194520SEzequiel Garcia 		ctrl->col_en = col_en;
459d3194520SEzequiel Garcia 		ctrl->col_n = col_n;
460d3194520SEzequiel Garcia 		ctrl->col_mode = col_mode;
461d3194520SEzequiel Garcia 		ctrl->row_en = row_en;
462d3194520SEzequiel Garcia 		ctrl->row_n = row_n;
463d3194520SEzequiel Garcia 		ctrl->row_mode = row_mode;
464d3194520SEzequiel Garcia 	}
465d3194520SEzequiel Garcia 
466d3194520SEzequiel Garcia 	stk1160_dbg("width %d, height %d\n",
467d3194520SEzequiel Garcia 		    f->fmt.pix.width, f->fmt.pix.height);
468d3194520SEzequiel Garcia 	return 0;
469d3194520SEzequiel Garcia }
470d3194520SEzequiel Garcia 
4710c0d06caSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
4720c0d06caSMauro Carvalho Chehab 				  struct v4l2_format *f)
4730c0d06caSMauro Carvalho Chehab {
4740c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
4750c0d06caSMauro Carvalho Chehab 
476d3194520SEzequiel Garcia 	return stk1160_try_fmt(dev, f, NULL);
4770c0d06caSMauro Carvalho Chehab }
4780c0d06caSMauro Carvalho Chehab 
4790c0d06caSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
4800c0d06caSMauro Carvalho Chehab 					struct v4l2_format *f)
4810c0d06caSMauro Carvalho Chehab {
4820c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
4830c0d06caSMauro Carvalho Chehab 	struct vb2_queue *q = &dev->vb_vidq;
484d3194520SEzequiel Garcia 	struct stk1160_decimate_ctrl ctrl;
485d3194520SEzequiel Garcia 	int rc;
4860c0d06caSMauro Carvalho Chehab 
4870c0d06caSMauro Carvalho Chehab 	if (vb2_is_busy(q))
4880c0d06caSMauro Carvalho Chehab 		return -EBUSY;
4890c0d06caSMauro Carvalho Chehab 
490d3194520SEzequiel Garcia 	rc = stk1160_try_fmt(dev, f, &ctrl);
491d3194520SEzequiel Garcia 	if (rc < 0)
492d3194520SEzequiel Garcia 		return rc;
493d3194520SEzequiel Garcia 	dev->width = f->fmt.pix.width;
494d3194520SEzequiel Garcia 	dev->height = f->fmt.pix.height;
495d3194520SEzequiel Garcia 	stk1160_set_fmt(dev, &ctrl);
4960c0d06caSMauro Carvalho Chehab 
4970c0d06caSMauro Carvalho Chehab 	return 0;
4980c0d06caSMauro Carvalho Chehab }
4990c0d06caSMauro Carvalho Chehab 
5000c0d06caSMauro Carvalho Chehab static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
5010c0d06caSMauro Carvalho Chehab {
5020c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
5030c0d06caSMauro Carvalho Chehab 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
5040c0d06caSMauro Carvalho Chehab 	return 0;
5050c0d06caSMauro Carvalho Chehab }
5060c0d06caSMauro Carvalho Chehab 
5070c0d06caSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
5080c0d06caSMauro Carvalho Chehab {
5090c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
5100c0d06caSMauro Carvalho Chehab 
5110c0d06caSMauro Carvalho Chehab 	*norm = dev->norm;
5120c0d06caSMauro Carvalho Chehab 	return 0;
5130c0d06caSMauro Carvalho Chehab }
5140c0d06caSMauro Carvalho Chehab 
515314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
5160c0d06caSMauro Carvalho Chehab {
5170c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
5180c0d06caSMauro Carvalho Chehab 	struct vb2_queue *q = &dev->vb_vidq;
5190c0d06caSMauro Carvalho Chehab 
520b3ba8fa6SEzequiel Garcia 	if (dev->norm == norm)
521b3ba8fa6SEzequiel Garcia 		return 0;
522b3ba8fa6SEzequiel Garcia 
5230c0d06caSMauro Carvalho Chehab 	if (vb2_is_busy(q))
5240c0d06caSMauro Carvalho Chehab 		return -EBUSY;
5250c0d06caSMauro Carvalho Chehab 
5260c0d06caSMauro Carvalho Chehab 	/* Check device presence */
5270c0d06caSMauro Carvalho Chehab 	if (!dev->udev)
5280c0d06caSMauro Carvalho Chehab 		return -ENODEV;
5290c0d06caSMauro Carvalho Chehab 
5300c0d06caSMauro Carvalho Chehab 	/* We need to set this now, before we call stk1160_set_std */
531d3194520SEzequiel Garcia 	dev->width = 720;
532d3194520SEzequiel Garcia 	dev->height = (norm & V4L2_STD_525_60) ? 480 : 576;
533314527acSHans Verkuil 	dev->norm = norm;
5340c0d06caSMauro Carvalho Chehab 
5350c0d06caSMauro Carvalho Chehab 	stk1160_set_std(dev);
5360c0d06caSMauro Carvalho Chehab 
537d3194520SEzequiel Garcia 	/* Calling with NULL disables frame decimation */
538d3194520SEzequiel Garcia 	stk1160_set_fmt(dev, NULL);
539d3194520SEzequiel Garcia 
5408774bed9SLaurent Pinchart 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
5410c0d06caSMauro Carvalho Chehab 			dev->norm);
5420c0d06caSMauro Carvalho Chehab 
5430c0d06caSMauro Carvalho Chehab 	return 0;
5440c0d06caSMauro Carvalho Chehab }
5450c0d06caSMauro Carvalho Chehab 
5460c0d06caSMauro Carvalho Chehab 
5470c0d06caSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
5480c0d06caSMauro Carvalho Chehab 				struct v4l2_input *i)
5490c0d06caSMauro Carvalho Chehab {
5500c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
5510c0d06caSMauro Carvalho Chehab 
5520c0d06caSMauro Carvalho Chehab 	if (i->index > STK1160_MAX_INPUT)
5530c0d06caSMauro Carvalho Chehab 		return -EINVAL;
5540c0d06caSMauro Carvalho Chehab 
55556a960bfSEzequiel Garcia 	/* S-Video special handling */
55656a960bfSEzequiel Garcia 	if (i->index == STK1160_SVIDEO_INPUT)
55756a960bfSEzequiel Garcia 		sprintf(i->name, "S-Video");
55856a960bfSEzequiel Garcia 	else
5590c0d06caSMauro Carvalho Chehab 		sprintf(i->name, "Composite%d", i->index);
56056a960bfSEzequiel Garcia 
5610c0d06caSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
5620c0d06caSMauro Carvalho Chehab 	i->std = dev->vdev.tvnorms;
5630c0d06caSMauro Carvalho Chehab 	return 0;
5640c0d06caSMauro Carvalho Chehab }
5650c0d06caSMauro Carvalho Chehab 
5660c0d06caSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
5670c0d06caSMauro Carvalho Chehab {
5680c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
5690c0d06caSMauro Carvalho Chehab 	*i = dev->ctl_input;
5700c0d06caSMauro Carvalho Chehab 	return 0;
5710c0d06caSMauro Carvalho Chehab }
5720c0d06caSMauro Carvalho Chehab 
5730c0d06caSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
5740c0d06caSMauro Carvalho Chehab {
5750c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
5760c0d06caSMauro Carvalho Chehab 
5770c0d06caSMauro Carvalho Chehab 	if (i > STK1160_MAX_INPUT)
5780c0d06caSMauro Carvalho Chehab 		return -EINVAL;
5790c0d06caSMauro Carvalho Chehab 
5800c0d06caSMauro Carvalho Chehab 	dev->ctl_input = i;
5810c0d06caSMauro Carvalho Chehab 
5820c0d06caSMauro Carvalho Chehab 	stk1160_select_input(dev);
5830c0d06caSMauro Carvalho Chehab 
5840c0d06caSMauro Carvalho Chehab 	return 0;
5850c0d06caSMauro Carvalho Chehab }
5860c0d06caSMauro Carvalho Chehab 
5870c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
5880c0d06caSMauro Carvalho Chehab static int vidioc_g_register(struct file *file, void *priv,
5890c0d06caSMauro Carvalho Chehab 			     struct v4l2_dbg_register *reg)
5900c0d06caSMauro Carvalho Chehab {
5910c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
5920c0d06caSMauro Carvalho Chehab 	int rc;
5930c0d06caSMauro Carvalho Chehab 	u8 val;
5940c0d06caSMauro Carvalho Chehab 
5950c0d06caSMauro Carvalho Chehab 	/* Match host */
5960c0d06caSMauro Carvalho Chehab 	rc = stk1160_read_reg(dev, reg->reg, &val);
5970c0d06caSMauro Carvalho Chehab 	reg->val = val;
5980c0d06caSMauro Carvalho Chehab 	reg->size = 1;
5990c0d06caSMauro Carvalho Chehab 
6000c0d06caSMauro Carvalho Chehab 	return rc;
6010c0d06caSMauro Carvalho Chehab }
6020c0d06caSMauro Carvalho Chehab 
6030c0d06caSMauro Carvalho Chehab static int vidioc_s_register(struct file *file, void *priv,
604977ba3b1SHans Verkuil 			     const struct v4l2_dbg_register *reg)
6050c0d06caSMauro Carvalho Chehab {
6060c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = video_drvdata(file);
6070c0d06caSMauro Carvalho Chehab 
6080c0d06caSMauro Carvalho Chehab 	/* Match host */
6094580278fSHans Verkuil 	return stk1160_write_reg(dev, reg->reg, reg->val);
6100c0d06caSMauro Carvalho Chehab }
6110c0d06caSMauro Carvalho Chehab #endif
6120c0d06caSMauro Carvalho Chehab 
6130c0d06caSMauro Carvalho Chehab static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
6140c0d06caSMauro Carvalho Chehab 	.vidioc_querycap      = vidioc_querycap,
6150c0d06caSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
6160c0d06caSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
6170c0d06caSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
6180c0d06caSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
6190c0d06caSMauro Carvalho Chehab 	.vidioc_querystd      = vidioc_querystd,
6200c0d06caSMauro Carvalho Chehab 	.vidioc_g_std         = vidioc_g_std,
6210c0d06caSMauro Carvalho Chehab 	.vidioc_s_std         = vidioc_s_std,
6220c0d06caSMauro Carvalho Chehab 	.vidioc_enum_input    = vidioc_enum_input,
6230c0d06caSMauro Carvalho Chehab 	.vidioc_g_input       = vidioc_g_input,
6240c0d06caSMauro Carvalho Chehab 	.vidioc_s_input       = vidioc_s_input,
6250c0d06caSMauro Carvalho Chehab 
6260c0d06caSMauro Carvalho Chehab 	/* vb2 takes care of these */
6270c0d06caSMauro Carvalho Chehab 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
6280c0d06caSMauro Carvalho Chehab 	.vidioc_querybuf      = vb2_ioctl_querybuf,
6290c0d06caSMauro Carvalho Chehab 	.vidioc_qbuf          = vb2_ioctl_qbuf,
6300c0d06caSMauro Carvalho Chehab 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
6310c0d06caSMauro Carvalho Chehab 	.vidioc_streamon      = vb2_ioctl_streamon,
6320c0d06caSMauro Carvalho Chehab 	.vidioc_streamoff     = vb2_ioctl_streamoff,
633ebbb5637SHans Verkuil 	.vidioc_expbuf        = vb2_ioctl_expbuf,
6340c0d06caSMauro Carvalho Chehab 
6350c0d06caSMauro Carvalho Chehab 	.vidioc_log_status  = v4l2_ctrl_log_status,
6360c0d06caSMauro Carvalho Chehab 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
6370c0d06caSMauro Carvalho Chehab 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
6380c0d06caSMauro Carvalho Chehab 
6390c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
6400c0d06caSMauro Carvalho Chehab 	.vidioc_g_register = vidioc_g_register,
6410c0d06caSMauro Carvalho Chehab 	.vidioc_s_register = vidioc_s_register,
6420c0d06caSMauro Carvalho Chehab #endif
6430c0d06caSMauro Carvalho Chehab };
6440c0d06caSMauro Carvalho Chehab 
6450c0d06caSMauro Carvalho Chehab /********************************************************************/
6460c0d06caSMauro Carvalho Chehab 
6470c0d06caSMauro Carvalho Chehab /*
6480c0d06caSMauro Carvalho Chehab  * Videobuf2 operations
6490c0d06caSMauro Carvalho Chehab  */
650df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *vq,
6510c0d06caSMauro Carvalho Chehab 				unsigned int *nbuffers, unsigned int *nplanes,
65236c0f8b3SHans Verkuil 				unsigned int sizes[], struct device *alloc_devs[])
6530c0d06caSMauro Carvalho Chehab {
6540c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = vb2_get_drv_priv(vq);
6550c0d06caSMauro Carvalho Chehab 	unsigned long size;
6560c0d06caSMauro Carvalho Chehab 
6570c0d06caSMauro Carvalho Chehab 	size = dev->width * dev->height * 2;
6580c0d06caSMauro Carvalho Chehab 
6590c0d06caSMauro Carvalho Chehab 	/*
6600c0d06caSMauro Carvalho Chehab 	 * Here we can change the number of buffers being requested.
6610c0d06caSMauro Carvalho Chehab 	 * So, we set a minimum and a maximum like this:
6620c0d06caSMauro Carvalho Chehab 	 */
6630c0d06caSMauro Carvalho Chehab 	*nbuffers = clamp_t(unsigned int, *nbuffers,
6640c0d06caSMauro Carvalho Chehab 			STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
6650c0d06caSMauro Carvalho Chehab 
666d27d5f1aSHelen Fornazier 	if (*nplanes)
667d27d5f1aSHelen Fornazier 		return sizes[0] < size ? -EINVAL : 0;
668d27d5f1aSHelen Fornazier 
6690c0d06caSMauro Carvalho Chehab 	/* This means a packed colorformat */
6700c0d06caSMauro Carvalho Chehab 	*nplanes = 1;
6710c0d06caSMauro Carvalho Chehab 
6720c0d06caSMauro Carvalho Chehab 	sizes[0] = size;
6730c0d06caSMauro Carvalho Chehab 
674890024adSEzequiel Garcia 	stk1160_dbg("%s: buffer count %d, each %ld bytes\n",
6750c0d06caSMauro Carvalho Chehab 		    __func__, *nbuffers, size);
6760c0d06caSMauro Carvalho Chehab 
6770c0d06caSMauro Carvalho Chehab 	return 0;
6780c0d06caSMauro Carvalho Chehab }
6790c0d06caSMauro Carvalho Chehab 
6800c0d06caSMauro Carvalho Chehab static void buffer_queue(struct vb2_buffer *vb)
6810c0d06caSMauro Carvalho Chehab {
6820c0d06caSMauro Carvalho Chehab 	unsigned long flags;
6830c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
6842d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6850c0d06caSMauro Carvalho Chehab 	struct stk1160_buffer *buf =
6862d700715SJunghak Sung 		container_of(vbuf, struct stk1160_buffer, vb);
6870c0d06caSMauro Carvalho Chehab 
6880c0d06caSMauro Carvalho Chehab 	spin_lock_irqsave(&dev->buf_lock, flags);
6890c0d06caSMauro Carvalho Chehab 	if (!dev->udev) {
6900c0d06caSMauro Carvalho Chehab 		/*
6910c0d06caSMauro Carvalho Chehab 		 * If the device is disconnected return the buffer to userspace
6920c0d06caSMauro Carvalho Chehab 		 * directly. The next QBUF call will fail with -ENODEV.
6930c0d06caSMauro Carvalho Chehab 		 */
6942d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
6950c0d06caSMauro Carvalho Chehab 	} else {
6960c0d06caSMauro Carvalho Chehab 
6970c0d06caSMauro Carvalho Chehab 		buf->mem = vb2_plane_vaddr(vb, 0);
6980c0d06caSMauro Carvalho Chehab 		buf->length = vb2_plane_size(vb, 0);
6990c0d06caSMauro Carvalho Chehab 		buf->bytesused = 0;
7000c0d06caSMauro Carvalho Chehab 		buf->pos = 0;
7010c0d06caSMauro Carvalho Chehab 
7020c0d06caSMauro Carvalho Chehab 		/*
7030c0d06caSMauro Carvalho Chehab 		 * If buffer length is less from expected then we return
7040c0d06caSMauro Carvalho Chehab 		 * the buffer to userspace directly.
7050c0d06caSMauro Carvalho Chehab 		 */
7060c0d06caSMauro Carvalho Chehab 		if (buf->length < dev->width * dev->height * 2)
7072d700715SJunghak Sung 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
7080c0d06caSMauro Carvalho Chehab 		else
7090c0d06caSMauro Carvalho Chehab 			list_add_tail(&buf->list, &dev->avail_bufs);
7100c0d06caSMauro Carvalho Chehab 
7110c0d06caSMauro Carvalho Chehab 	}
7120c0d06caSMauro Carvalho Chehab 	spin_unlock_irqrestore(&dev->buf_lock, flags);
7130c0d06caSMauro Carvalho Chehab }
7140c0d06caSMauro Carvalho Chehab 
7150c0d06caSMauro Carvalho Chehab static int start_streaming(struct vb2_queue *vq, unsigned int count)
7160c0d06caSMauro Carvalho Chehab {
7170c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = vb2_get_drv_priv(vq);
7180c0d06caSMauro Carvalho Chehab 	return stk1160_start_streaming(dev);
7190c0d06caSMauro Carvalho Chehab }
7200c0d06caSMauro Carvalho Chehab 
7210c0d06caSMauro Carvalho Chehab /* abort streaming and wait for last buffer */
722e37559b2SHans Verkuil static void stop_streaming(struct vb2_queue *vq)
7230c0d06caSMauro Carvalho Chehab {
7240c0d06caSMauro Carvalho Chehab 	struct stk1160 *dev = vb2_get_drv_priv(vq);
725e37559b2SHans Verkuil 	stk1160_stop_streaming(dev);
7260c0d06caSMauro Carvalho Chehab }
7270c0d06caSMauro Carvalho Chehab 
7281bc17717SJulia Lawall static const struct vb2_ops stk1160_video_qops = {
7290c0d06caSMauro Carvalho Chehab 	.queue_setup		= queue_setup,
7300c0d06caSMauro Carvalho Chehab 	.buf_queue		= buffer_queue,
7310c0d06caSMauro Carvalho Chehab 	.start_streaming	= start_streaming,
7320c0d06caSMauro Carvalho Chehab 	.stop_streaming		= stop_streaming,
7330c0d06caSMauro Carvalho Chehab 	.wait_prepare		= vb2_ops_wait_prepare,
7340c0d06caSMauro Carvalho Chehab 	.wait_finish		= vb2_ops_wait_finish,
7350c0d06caSMauro Carvalho Chehab };
7360c0d06caSMauro Carvalho Chehab 
73786844942SBhumika Goyal static const struct video_device v4l_template = {
7380c0d06caSMauro Carvalho Chehab 	.name = "stk1160",
7390c0d06caSMauro Carvalho Chehab 	.tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
7400c0d06caSMauro Carvalho Chehab 	.fops = &stk1160_fops,
7410c0d06caSMauro Carvalho Chehab 	.ioctl_ops = &stk1160_ioctl_ops,
7420c0d06caSMauro Carvalho Chehab 	.release = video_device_release_empty,
7430c0d06caSMauro Carvalho Chehab };
7440c0d06caSMauro Carvalho Chehab 
7450c0d06caSMauro Carvalho Chehab /********************************************************************/
7460c0d06caSMauro Carvalho Chehab 
7470c0d06caSMauro Carvalho Chehab /* Must be called with both v4l_lock and vb_queue_lock hold */
748*fbe04b49SDafna Hirschfeld void stk1160_clear_queue(struct stk1160 *dev, enum vb2_buffer_state vb2_state)
7490c0d06caSMauro Carvalho Chehab {
7500c0d06caSMauro Carvalho Chehab 	struct stk1160_buffer *buf;
7510c0d06caSMauro Carvalho Chehab 	unsigned long flags;
7520c0d06caSMauro Carvalho Chehab 
7530c0d06caSMauro Carvalho Chehab 	/* Release all active buffers */
7540c0d06caSMauro Carvalho Chehab 	spin_lock_irqsave(&dev->buf_lock, flags);
7550c0d06caSMauro Carvalho Chehab 	while (!list_empty(&dev->avail_bufs)) {
7560c0d06caSMauro Carvalho Chehab 		buf = list_first_entry(&dev->avail_bufs,
7570c0d06caSMauro Carvalho Chehab 			struct stk1160_buffer, list);
7580c0d06caSMauro Carvalho Chehab 		list_del(&buf->list);
759*fbe04b49SDafna Hirschfeld 		vb2_buffer_done(&buf->vb.vb2_buf, vb2_state);
760890024adSEzequiel Garcia 		stk1160_dbg("buffer [%p/%d] aborted\n",
7612d700715SJunghak Sung 			    buf, buf->vb.vb2_buf.index);
7620c0d06caSMauro Carvalho Chehab 	}
763aeff0927SEzequiel Garcia 
764aeff0927SEzequiel Garcia 	/* It's important to release the current buffer */
765aeff0927SEzequiel Garcia 	if (dev->isoc_ctl.buf) {
766aeff0927SEzequiel Garcia 		buf = dev->isoc_ctl.buf;
7670c0d06caSMauro Carvalho Chehab 		dev->isoc_ctl.buf = NULL;
768aeff0927SEzequiel Garcia 
769*fbe04b49SDafna Hirschfeld 		vb2_buffer_done(&buf->vb.vb2_buf, vb2_state);
770890024adSEzequiel Garcia 		stk1160_dbg("buffer [%p/%d] aborted\n",
7712d700715SJunghak Sung 			    buf, buf->vb.vb2_buf.index);
772aeff0927SEzequiel Garcia 	}
7730c0d06caSMauro Carvalho Chehab 	spin_unlock_irqrestore(&dev->buf_lock, flags);
7740c0d06caSMauro Carvalho Chehab }
7750c0d06caSMauro Carvalho Chehab 
7760c0d06caSMauro Carvalho Chehab int stk1160_vb2_setup(struct stk1160 *dev)
7770c0d06caSMauro Carvalho Chehab {
7780c0d06caSMauro Carvalho Chehab 	int rc;
7790c0d06caSMauro Carvalho Chehab 	struct vb2_queue *q;
7800c0d06caSMauro Carvalho Chehab 
7810c0d06caSMauro Carvalho Chehab 	q = &dev->vb_vidq;
7820c0d06caSMauro Carvalho Chehab 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
783ebbb5637SHans Verkuil 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
7840c0d06caSMauro Carvalho Chehab 	q->drv_priv = dev;
7850c0d06caSMauro Carvalho Chehab 	q->buf_struct_size = sizeof(struct stk1160_buffer);
7860c0d06caSMauro Carvalho Chehab 	q->ops = &stk1160_video_qops;
7870c0d06caSMauro Carvalho Chehab 	q->mem_ops = &vb2_vmalloc_memops;
788de3e581fSEzequiel Garcia 	q->lock = &dev->vb_queue_lock;
789ade48681SSakari Ailus 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
7900c0d06caSMauro Carvalho Chehab 
7910c0d06caSMauro Carvalho Chehab 	rc = vb2_queue_init(q);
7920c0d06caSMauro Carvalho Chehab 	if (rc < 0)
7930c0d06caSMauro Carvalho Chehab 		return rc;
7940c0d06caSMauro Carvalho Chehab 
7950c0d06caSMauro Carvalho Chehab 	/* initialize video dma queue */
7960c0d06caSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->avail_bufs);
7970c0d06caSMauro Carvalho Chehab 
7980c0d06caSMauro Carvalho Chehab 	return 0;
7990c0d06caSMauro Carvalho Chehab }
8000c0d06caSMauro Carvalho Chehab 
8010c0d06caSMauro Carvalho Chehab int stk1160_video_register(struct stk1160 *dev)
8020c0d06caSMauro Carvalho Chehab {
8030c0d06caSMauro Carvalho Chehab 	int rc;
8040c0d06caSMauro Carvalho Chehab 
8050c0d06caSMauro Carvalho Chehab 	/* Initialize video_device with a template structure */
8060c0d06caSMauro Carvalho Chehab 	dev->vdev = v4l_template;
8070c0d06caSMauro Carvalho Chehab 	dev->vdev.queue = &dev->vb_vidq;
8080c0d06caSMauro Carvalho Chehab 
8090c0d06caSMauro Carvalho Chehab 	/*
8100c0d06caSMauro Carvalho Chehab 	 * Provide mutexes for v4l2 core and for videobuf2 queue.
8110c0d06caSMauro Carvalho Chehab 	 * It will be used to protect *only* v4l2 ioctls.
8120c0d06caSMauro Carvalho Chehab 	 */
8130c0d06caSMauro Carvalho Chehab 	dev->vdev.lock = &dev->v4l_lock;
8140c0d06caSMauro Carvalho Chehab 
8150c0d06caSMauro Carvalho Chehab 	/* This will be used to set video_device parent */
8160c0d06caSMauro Carvalho Chehab 	dev->vdev.v4l2_dev = &dev->v4l2_dev;
8178c3854d0SHans Verkuil 	dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
8188c3854d0SHans Verkuil 				V4L2_CAP_READWRITE;
8190c0d06caSMauro Carvalho Chehab 
8200c0d06caSMauro Carvalho Chehab 	/* NTSC is default */
8210c0d06caSMauro Carvalho Chehab 	dev->norm = V4L2_STD_NTSC_M;
8220c0d06caSMauro Carvalho Chehab 	dev->width = 720;
8230c0d06caSMauro Carvalho Chehab 	dev->height = 480;
8240c0d06caSMauro Carvalho Chehab 
8250c0d06caSMauro Carvalho Chehab 	/* set default format */
8260c0d06caSMauro Carvalho Chehab 	dev->fmt = &format[0];
8270c0d06caSMauro Carvalho Chehab 	stk1160_set_std(dev);
8280c0d06caSMauro Carvalho Chehab 
8298774bed9SLaurent Pinchart 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
8300c0d06caSMauro Carvalho Chehab 			dev->norm);
8310c0d06caSMauro Carvalho Chehab 
8320c0d06caSMauro Carvalho Chehab 	video_set_drvdata(&dev->vdev, dev);
8337fbbbc78SHans Verkuil 	rc = video_register_device(&dev->vdev, VFL_TYPE_VIDEO, -1);
8340c0d06caSMauro Carvalho Chehab 	if (rc < 0) {
8350c0d06caSMauro Carvalho Chehab 		stk1160_err("video_register_device failed (%d)\n", rc);
8360c0d06caSMauro Carvalho Chehab 		return rc;
8370c0d06caSMauro Carvalho Chehab 	}
8380c0d06caSMauro Carvalho Chehab 
8390c0d06caSMauro Carvalho Chehab 	v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
8400c0d06caSMauro Carvalho Chehab 		  video_device_node_name(&dev->vdev));
8410c0d06caSMauro Carvalho Chehab 
8420c0d06caSMauro Carvalho Chehab 	return 0;
8430c0d06caSMauro Carvalho Chehab }
844