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
div_round_integer(unsigned int x,unsigned int y)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
stk1160_set_std(struct stk1160 * dev)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
stk1160_set_fmt(struct stk1160 * dev,struct stk1160_decimate_ctrl * ctrl)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 */
stk1160_set_alternate(struct stk1160 * dev)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
stk1160_start_streaming(struct stk1160 * dev)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++) {
23568d0c331SDafna Hirschfeld struct stk1160_urb *stk_urb = &dev->isoc_ctl.urb_ctl[i];
23668d0c331SDafna Hirschfeld
23768d0c331SDafna Hirschfeld dma_sync_sgtable_for_device(stk1160_get_dmadev(dev), stk_urb->sgt,
23868d0c331SDafna Hirschfeld DMA_FROM_DEVICE);
23969fd04d0SDafna Hirschfeld rc = usb_submit_urb(dev->isoc_ctl.urb_ctl[i].urb, GFP_KERNEL);
2400c0d06caSMauro Carvalho Chehab if (rc) {
2410c0d06caSMauro Carvalho Chehab stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
2428ac45649SEzequiel Garcia goto out_uninit;
2430c0d06caSMauro Carvalho Chehab }
2440c0d06caSMauro Carvalho Chehab }
2450c0d06caSMauro Carvalho Chehab
2460c0d06caSMauro Carvalho Chehab /* Start saa711x */
2470c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
2480c0d06caSMauro Carvalho Chehab
249e3e30f63SHans Verkuil dev->sequence = 0;
250e3e30f63SHans Verkuil
2510c0d06caSMauro Carvalho Chehab /* Start stk1160 */
2520c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
2530c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
2540c0d06caSMauro Carvalho Chehab
2550c0d06caSMauro Carvalho Chehab stk1160_dbg("streaming started\n");
2560c0d06caSMauro Carvalho Chehab
2578ac45649SEzequiel Garcia mutex_unlock(&dev->v4l_lock);
2588ac45649SEzequiel Garcia
2598ac45649SEzequiel Garcia return 0;
2608ac45649SEzequiel Garcia
2618ac45649SEzequiel Garcia out_uninit:
2628ac45649SEzequiel Garcia stk1160_uninit_isoc(dev);
2638ac45649SEzequiel Garcia out_stop_hw:
2648ac45649SEzequiel Garcia usb_set_interface(dev->udev, 0, 0);
265fbe04b49SDafna Hirschfeld stk1160_clear_queue(dev, VB2_BUF_STATE_QUEUED);
2668ac45649SEzequiel Garcia
2670c0d06caSMauro Carvalho Chehab mutex_unlock(&dev->v4l_lock);
2680c0d06caSMauro Carvalho Chehab
269f367cc1eSDan Carpenter return rc;
2700c0d06caSMauro Carvalho Chehab }
2710c0d06caSMauro Carvalho Chehab
2720c0d06caSMauro Carvalho Chehab /* Must be called with v4l_lock hold */
stk1160_stop_hw(struct stk1160 * dev)2730c0d06caSMauro Carvalho Chehab static void stk1160_stop_hw(struct stk1160 *dev)
2740c0d06caSMauro Carvalho Chehab {
2750c0d06caSMauro Carvalho Chehab /* If the device is not physically present, there is nothing to do */
2760c0d06caSMauro Carvalho Chehab if (!dev->udev)
2770c0d06caSMauro Carvalho Chehab return;
2780c0d06caSMauro Carvalho Chehab
2790c0d06caSMauro Carvalho Chehab /* set alternate 0 */
2800c0d06caSMauro Carvalho Chehab dev->alt = 0;
281890024adSEzequiel Garcia stk1160_dbg("setting alternate %d\n", dev->alt);
2820c0d06caSMauro Carvalho Chehab usb_set_interface(dev->udev, 0, 0);
2830c0d06caSMauro Carvalho Chehab
2840c0d06caSMauro Carvalho Chehab /* Stop stk1160 */
2850c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
2860c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
2870c0d06caSMauro Carvalho Chehab
2880c0d06caSMauro Carvalho Chehab /* Stop saa711x */
2890c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
2900c0d06caSMauro Carvalho Chehab }
2910c0d06caSMauro Carvalho Chehab
stk1160_stop_streaming(struct stk1160 * dev)2920c0d06caSMauro Carvalho Chehab static int stk1160_stop_streaming(struct stk1160 *dev)
2930c0d06caSMauro Carvalho Chehab {
2940c0d06caSMauro Carvalho Chehab if (mutex_lock_interruptible(&dev->v4l_lock))
2950c0d06caSMauro Carvalho Chehab return -ERESTARTSYS;
2960c0d06caSMauro Carvalho Chehab
297aeff0927SEzequiel Garcia /*
298aeff0927SEzequiel Garcia * Once URBs are cancelled, the URB complete handler
299aeff0927SEzequiel Garcia * won't be running. This is required to safely release the
300aeff0927SEzequiel Garcia * current buffer (dev->isoc_ctl.buf).
301aeff0927SEzequiel Garcia */
3020c0d06caSMauro Carvalho Chehab stk1160_cancel_isoc(dev);
3030c0d06caSMauro Carvalho Chehab
3040c0d06caSMauro Carvalho Chehab /*
3050c0d06caSMauro Carvalho Chehab * It is possible to keep buffers around using a module parameter.
3060c0d06caSMauro Carvalho Chehab * This is intended to avoid memory fragmentation.
3070c0d06caSMauro Carvalho Chehab */
3080c0d06caSMauro Carvalho Chehab if (!keep_buffers)
3090c0d06caSMauro Carvalho Chehab stk1160_free_isoc(dev);
3100c0d06caSMauro Carvalho Chehab
3110c0d06caSMauro Carvalho Chehab stk1160_stop_hw(dev);
3120c0d06caSMauro Carvalho Chehab
313fbe04b49SDafna Hirschfeld stk1160_clear_queue(dev, VB2_BUF_STATE_ERROR);
3140c0d06caSMauro Carvalho Chehab
3150c0d06caSMauro Carvalho Chehab stk1160_dbg("streaming stopped\n");
3160c0d06caSMauro Carvalho Chehab
3170c0d06caSMauro Carvalho Chehab mutex_unlock(&dev->v4l_lock);
3180c0d06caSMauro Carvalho Chehab
3190c0d06caSMauro Carvalho Chehab return 0;
3200c0d06caSMauro Carvalho Chehab }
3210c0d06caSMauro Carvalho Chehab
322ff05c984SBhumika Goyal static const struct v4l2_file_operations stk1160_fops = {
3230c0d06caSMauro Carvalho Chehab .owner = THIS_MODULE,
3240c0d06caSMauro Carvalho Chehab .open = v4l2_fh_open,
3250c0d06caSMauro Carvalho Chehab .release = vb2_fop_release,
3260c0d06caSMauro Carvalho Chehab .read = vb2_fop_read,
3270c0d06caSMauro Carvalho Chehab .poll = vb2_fop_poll,
3280c0d06caSMauro Carvalho Chehab .mmap = vb2_fop_mmap,
3290c0d06caSMauro Carvalho Chehab .unlocked_ioctl = video_ioctl2,
3300c0d06caSMauro Carvalho Chehab };
3310c0d06caSMauro Carvalho Chehab
3320c0d06caSMauro Carvalho Chehab /*
3330c0d06caSMauro Carvalho Chehab * vidioc ioctls
3340c0d06caSMauro Carvalho Chehab */
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)3350c0d06caSMauro Carvalho Chehab static int vidioc_querycap(struct file *file,
3360c0d06caSMauro Carvalho Chehab void *priv, struct v4l2_capability *cap)
3370c0d06caSMauro Carvalho Chehab {
3380c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
3390c0d06caSMauro Carvalho Chehab
340cc1e6315SMauro Carvalho Chehab strscpy(cap->driver, "stk1160", sizeof(cap->driver));
341cc1e6315SMauro Carvalho Chehab strscpy(cap->card, "stk1160", sizeof(cap->card));
3420c0d06caSMauro Carvalho Chehab usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
3430c0d06caSMauro Carvalho Chehab return 0;
3440c0d06caSMauro Carvalho Chehab }
3450c0d06caSMauro Carvalho Chehab
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)3460c0d06caSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
3470c0d06caSMauro Carvalho Chehab struct v4l2_fmtdesc *f)
3480c0d06caSMauro Carvalho Chehab {
3490c0d06caSMauro Carvalho Chehab if (f->index != 0)
3500c0d06caSMauro Carvalho Chehab return -EINVAL;
3510c0d06caSMauro Carvalho Chehab
3520c0d06caSMauro Carvalho Chehab f->pixelformat = format[f->index].fourcc;
3530c0d06caSMauro Carvalho Chehab return 0;
3540c0d06caSMauro Carvalho Chehab }
3550c0d06caSMauro Carvalho Chehab
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)3560c0d06caSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
3570c0d06caSMauro Carvalho Chehab struct v4l2_format *f)
3580c0d06caSMauro Carvalho Chehab {
3590c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
3600c0d06caSMauro Carvalho Chehab
3610c0d06caSMauro Carvalho Chehab f->fmt.pix.width = dev->width;
3620c0d06caSMauro Carvalho Chehab f->fmt.pix.height = dev->height;
3630c0d06caSMauro Carvalho Chehab f->fmt.pix.field = V4L2_FIELD_INTERLACED;
3640c0d06caSMauro Carvalho Chehab f->fmt.pix.pixelformat = dev->fmt->fourcc;
3650c0d06caSMauro Carvalho Chehab f->fmt.pix.bytesperline = dev->width * 2;
3660c0d06caSMauro Carvalho Chehab f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
3670c0d06caSMauro Carvalho Chehab f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
3680c0d06caSMauro Carvalho Chehab
3690c0d06caSMauro Carvalho Chehab return 0;
3700c0d06caSMauro Carvalho Chehab }
3710c0d06caSMauro Carvalho Chehab
stk1160_try_fmt(struct stk1160 * dev,struct v4l2_format * f,struct stk1160_decimate_ctrl * ctrl)372d3194520SEzequiel Garcia static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f,
373d3194520SEzequiel Garcia struct stk1160_decimate_ctrl *ctrl)
374d3194520SEzequiel Garcia {
375d3194520SEzequiel Garcia unsigned int width, height;
376d3194520SEzequiel Garcia unsigned int base_width, base_height;
377d3194520SEzequiel Garcia unsigned int col_n, row_n;
378d3194520SEzequiel Garcia enum stk1160_decimate_mode col_mode, row_mode;
379d3194520SEzequiel Garcia bool col_en, row_en;
380d3194520SEzequiel Garcia
381d3194520SEzequiel Garcia base_width = 720;
382d3194520SEzequiel Garcia base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576;
383d3194520SEzequiel Garcia
384d3194520SEzequiel Garcia /* Minimum width and height is 5% the frame size */
385d3194520SEzequiel Garcia width = clamp_t(unsigned int, f->fmt.pix.width,
386d3194520SEzequiel Garcia base_width / 20, base_width);
387d3194520SEzequiel Garcia height = clamp_t(unsigned int, f->fmt.pix.height,
388d3194520SEzequiel Garcia base_height / 20, base_height);
389d3194520SEzequiel Garcia
390d3194520SEzequiel Garcia /* Let's set default no decimation values */
391d3194520SEzequiel Garcia col_n = 0;
392d3194520SEzequiel Garcia row_n = 0;
393d3194520SEzequiel Garcia col_en = false;
394d3194520SEzequiel Garcia row_en = false;
395d3194520SEzequiel Garcia f->fmt.pix.width = base_width;
396d3194520SEzequiel Garcia f->fmt.pix.height = base_height;
397d3194520SEzequiel Garcia row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
398d3194520SEzequiel Garcia col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
399d3194520SEzequiel Garcia
400d3194520SEzequiel Garcia if (width < base_width && width > base_width / 2) {
401d3194520SEzequiel Garcia /*
402d3194520SEzequiel Garcia * The device will send count units for each
403d3194520SEzequiel Garcia * unit skipped. This means count unit is:
404d3194520SEzequiel Garcia *
405d3194520SEzequiel Garcia * n = width / (frame width - width)
406d3194520SEzequiel Garcia *
407d3194520SEzequiel Garcia * And the width is:
408d3194520SEzequiel Garcia *
409d3194520SEzequiel Garcia * width = (n / n + 1) * frame width
410d3194520SEzequiel Garcia */
411d3194520SEzequiel Garcia col_n = div_round_integer(width, base_width - width);
412d3194520SEzequiel Garcia if (col_n > 0 && col_n <= 255) {
413d3194520SEzequiel Garcia col_en = true;
414d3194520SEzequiel Garcia col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
415d3194520SEzequiel Garcia f->fmt.pix.width = (base_width * col_n) / (col_n + 1);
416d3194520SEzequiel Garcia }
417d3194520SEzequiel Garcia
418d3194520SEzequiel Garcia } else if (width <= base_width / 2) {
419d3194520SEzequiel Garcia
420d3194520SEzequiel Garcia /*
421d3194520SEzequiel Garcia * The device will skip count units for each
422d3194520SEzequiel Garcia * unit sent. This means count is:
423d3194520SEzequiel Garcia *
424d3194520SEzequiel Garcia * n = (frame width / width) - 1
425d3194520SEzequiel Garcia *
426d3194520SEzequiel Garcia * And the width is:
427d3194520SEzequiel Garcia *
428d3194520SEzequiel Garcia * width = frame width / (n + 1)
429d3194520SEzequiel Garcia */
430d3194520SEzequiel Garcia col_n = div_round_integer(base_width, width) - 1;
431d3194520SEzequiel Garcia if (col_n > 0 && col_n <= 255) {
432d3194520SEzequiel Garcia col_en = true;
433d3194520SEzequiel Garcia col_mode = STK1160_DECIMATE_MORE_THAN_HALF;
434d3194520SEzequiel Garcia f->fmt.pix.width = base_width / (col_n + 1);
435d3194520SEzequiel Garcia }
436d3194520SEzequiel Garcia }
437d3194520SEzequiel Garcia
438d3194520SEzequiel Garcia if (height < base_height && height > base_height / 2) {
439d3194520SEzequiel Garcia row_n = div_round_integer(height, base_height - height);
440d3194520SEzequiel Garcia if (row_n > 0 && row_n <= 255) {
441d3194520SEzequiel Garcia row_en = true;
442d3194520SEzequiel Garcia row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
443d3194520SEzequiel Garcia f->fmt.pix.height = (base_height * row_n) / (row_n + 1);
444d3194520SEzequiel Garcia }
445d3194520SEzequiel Garcia
446d3194520SEzequiel Garcia } else if (height <= base_height / 2) {
447d3194520SEzequiel Garcia row_n = div_round_integer(base_height, height) - 1;
448d3194520SEzequiel Garcia if (row_n > 0 && row_n <= 255) {
449d3194520SEzequiel Garcia row_en = true;
450d3194520SEzequiel Garcia row_mode = STK1160_DECIMATE_MORE_THAN_HALF;
451d3194520SEzequiel Garcia f->fmt.pix.height = base_height / (row_n + 1);
452d3194520SEzequiel Garcia }
453d3194520SEzequiel Garcia }
454d3194520SEzequiel Garcia
455d3194520SEzequiel Garcia f->fmt.pix.pixelformat = dev->fmt->fourcc;
456d3194520SEzequiel Garcia f->fmt.pix.field = V4L2_FIELD_INTERLACED;
457d3194520SEzequiel Garcia f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
458d3194520SEzequiel Garcia f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
459d3194520SEzequiel Garcia f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
460d3194520SEzequiel Garcia
461d3194520SEzequiel Garcia if (ctrl) {
462d3194520SEzequiel Garcia ctrl->col_en = col_en;
463d3194520SEzequiel Garcia ctrl->col_n = col_n;
464d3194520SEzequiel Garcia ctrl->col_mode = col_mode;
465d3194520SEzequiel Garcia ctrl->row_en = row_en;
466d3194520SEzequiel Garcia ctrl->row_n = row_n;
467d3194520SEzequiel Garcia ctrl->row_mode = row_mode;
468d3194520SEzequiel Garcia }
469d3194520SEzequiel Garcia
470d3194520SEzequiel Garcia stk1160_dbg("width %d, height %d\n",
471d3194520SEzequiel Garcia f->fmt.pix.width, f->fmt.pix.height);
472d3194520SEzequiel Garcia return 0;
473d3194520SEzequiel Garcia }
474d3194520SEzequiel Garcia
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)4750c0d06caSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
4760c0d06caSMauro Carvalho Chehab struct v4l2_format *f)
4770c0d06caSMauro Carvalho Chehab {
4780c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
4790c0d06caSMauro Carvalho Chehab
480d3194520SEzequiel Garcia return stk1160_try_fmt(dev, f, NULL);
4810c0d06caSMauro Carvalho Chehab }
4820c0d06caSMauro Carvalho Chehab
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)4830c0d06caSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
4840c0d06caSMauro Carvalho Chehab struct v4l2_format *f)
4850c0d06caSMauro Carvalho Chehab {
4860c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
4870c0d06caSMauro Carvalho Chehab struct vb2_queue *q = &dev->vb_vidq;
488d3194520SEzequiel Garcia struct stk1160_decimate_ctrl ctrl;
489d3194520SEzequiel Garcia int rc;
4900c0d06caSMauro Carvalho Chehab
4910c0d06caSMauro Carvalho Chehab if (vb2_is_busy(q))
4920c0d06caSMauro Carvalho Chehab return -EBUSY;
4930c0d06caSMauro Carvalho Chehab
494d3194520SEzequiel Garcia rc = stk1160_try_fmt(dev, f, &ctrl);
495d3194520SEzequiel Garcia if (rc < 0)
496d3194520SEzequiel Garcia return rc;
497d3194520SEzequiel Garcia dev->width = f->fmt.pix.width;
498d3194520SEzequiel Garcia dev->height = f->fmt.pix.height;
499d3194520SEzequiel Garcia stk1160_set_fmt(dev, &ctrl);
5000c0d06caSMauro Carvalho Chehab
5010c0d06caSMauro Carvalho Chehab return 0;
5020c0d06caSMauro Carvalho Chehab }
5030c0d06caSMauro Carvalho Chehab
vidioc_querystd(struct file * file,void * priv,v4l2_std_id * norm)5040c0d06caSMauro Carvalho Chehab static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
5050c0d06caSMauro Carvalho Chehab {
5060c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
5070c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
5080c0d06caSMauro Carvalho Chehab return 0;
5090c0d06caSMauro Carvalho Chehab }
5100c0d06caSMauro Carvalho Chehab
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * norm)5110c0d06caSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
5120c0d06caSMauro Carvalho Chehab {
5130c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
5140c0d06caSMauro Carvalho Chehab
5150c0d06caSMauro Carvalho Chehab *norm = dev->norm;
5160c0d06caSMauro Carvalho Chehab return 0;
5170c0d06caSMauro Carvalho Chehab }
5180c0d06caSMauro Carvalho Chehab
vidioc_s_std(struct file * file,void * priv,v4l2_std_id norm)519314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
5200c0d06caSMauro Carvalho Chehab {
5210c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
5220c0d06caSMauro Carvalho Chehab struct vb2_queue *q = &dev->vb_vidq;
5230c0d06caSMauro Carvalho Chehab
524b3ba8fa6SEzequiel Garcia if (dev->norm == norm)
525b3ba8fa6SEzequiel Garcia return 0;
526b3ba8fa6SEzequiel Garcia
5270c0d06caSMauro Carvalho Chehab if (vb2_is_busy(q))
5280c0d06caSMauro Carvalho Chehab return -EBUSY;
5290c0d06caSMauro Carvalho Chehab
5300c0d06caSMauro Carvalho Chehab /* Check device presence */
5310c0d06caSMauro Carvalho Chehab if (!dev->udev)
5320c0d06caSMauro Carvalho Chehab return -ENODEV;
5330c0d06caSMauro Carvalho Chehab
5340c0d06caSMauro Carvalho Chehab /* We need to set this now, before we call stk1160_set_std */
535d3194520SEzequiel Garcia dev->width = 720;
536d3194520SEzequiel Garcia dev->height = (norm & V4L2_STD_525_60) ? 480 : 576;
537314527acSHans Verkuil dev->norm = norm;
5380c0d06caSMauro Carvalho Chehab
5390c0d06caSMauro Carvalho Chehab stk1160_set_std(dev);
5400c0d06caSMauro Carvalho Chehab
541d3194520SEzequiel Garcia /* Calling with NULL disables frame decimation */
542d3194520SEzequiel Garcia stk1160_set_fmt(dev, NULL);
543d3194520SEzequiel Garcia
5448774bed9SLaurent Pinchart v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
5450c0d06caSMauro Carvalho Chehab dev->norm);
5460c0d06caSMauro Carvalho Chehab
5470c0d06caSMauro Carvalho Chehab return 0;
5480c0d06caSMauro Carvalho Chehab }
5490c0d06caSMauro Carvalho Chehab
5500c0d06caSMauro Carvalho Chehab
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)5510c0d06caSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
5520c0d06caSMauro Carvalho Chehab struct v4l2_input *i)
5530c0d06caSMauro Carvalho Chehab {
5540c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
5550c0d06caSMauro Carvalho Chehab
5560c0d06caSMauro Carvalho Chehab if (i->index > STK1160_MAX_INPUT)
5570c0d06caSMauro Carvalho Chehab return -EINVAL;
5580c0d06caSMauro Carvalho Chehab
55956a960bfSEzequiel Garcia /* S-Video special handling */
56056a960bfSEzequiel Garcia if (i->index == STK1160_SVIDEO_INPUT)
56156a960bfSEzequiel Garcia sprintf(i->name, "S-Video");
56256a960bfSEzequiel Garcia else
5630c0d06caSMauro Carvalho Chehab sprintf(i->name, "Composite%d", i->index);
56456a960bfSEzequiel Garcia
5650c0d06caSMauro Carvalho Chehab i->type = V4L2_INPUT_TYPE_CAMERA;
5660c0d06caSMauro Carvalho Chehab i->std = dev->vdev.tvnorms;
5670c0d06caSMauro Carvalho Chehab return 0;
5680c0d06caSMauro Carvalho Chehab }
5690c0d06caSMauro Carvalho Chehab
vidioc_g_input(struct file * file,void * priv,unsigned int * i)5700c0d06caSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
5710c0d06caSMauro Carvalho Chehab {
5720c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
5730c0d06caSMauro Carvalho Chehab *i = dev->ctl_input;
5740c0d06caSMauro Carvalho Chehab return 0;
5750c0d06caSMauro Carvalho Chehab }
5760c0d06caSMauro Carvalho Chehab
vidioc_s_input(struct file * file,void * priv,unsigned int i)5770c0d06caSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
5780c0d06caSMauro Carvalho Chehab {
5790c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
5800c0d06caSMauro Carvalho Chehab
5810c0d06caSMauro Carvalho Chehab if (i > STK1160_MAX_INPUT)
5820c0d06caSMauro Carvalho Chehab return -EINVAL;
5830c0d06caSMauro Carvalho Chehab
5840c0d06caSMauro Carvalho Chehab dev->ctl_input = i;
5850c0d06caSMauro Carvalho Chehab
5860c0d06caSMauro Carvalho Chehab stk1160_select_input(dev);
5870c0d06caSMauro Carvalho Chehab
5880c0d06caSMauro Carvalho Chehab return 0;
5890c0d06caSMauro Carvalho Chehab }
5900c0d06caSMauro Carvalho Chehab
5910c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
vidioc_g_register(struct file * file,void * priv,struct v4l2_dbg_register * reg)5920c0d06caSMauro Carvalho Chehab static int vidioc_g_register(struct file *file, void *priv,
5930c0d06caSMauro Carvalho Chehab struct v4l2_dbg_register *reg)
5940c0d06caSMauro Carvalho Chehab {
5950c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
5960c0d06caSMauro Carvalho Chehab int rc;
5970c0d06caSMauro Carvalho Chehab u8 val;
5980c0d06caSMauro Carvalho Chehab
5990c0d06caSMauro Carvalho Chehab /* Match host */
6000c0d06caSMauro Carvalho Chehab rc = stk1160_read_reg(dev, reg->reg, &val);
6010c0d06caSMauro Carvalho Chehab reg->val = val;
6020c0d06caSMauro Carvalho Chehab reg->size = 1;
6030c0d06caSMauro Carvalho Chehab
6040c0d06caSMauro Carvalho Chehab return rc;
6050c0d06caSMauro Carvalho Chehab }
6060c0d06caSMauro Carvalho Chehab
vidioc_s_register(struct file * file,void * priv,const struct v4l2_dbg_register * reg)6070c0d06caSMauro Carvalho Chehab static int vidioc_s_register(struct file *file, void *priv,
608977ba3b1SHans Verkuil const struct v4l2_dbg_register *reg)
6090c0d06caSMauro Carvalho Chehab {
6100c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file);
6110c0d06caSMauro Carvalho Chehab
6120c0d06caSMauro Carvalho Chehab /* Match host */
6134580278fSHans Verkuil return stk1160_write_reg(dev, reg->reg, reg->val);
6140c0d06caSMauro Carvalho Chehab }
6150c0d06caSMauro Carvalho Chehab #endif
6160c0d06caSMauro Carvalho Chehab
6170c0d06caSMauro Carvalho Chehab static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
6180c0d06caSMauro Carvalho Chehab .vidioc_querycap = vidioc_querycap,
6190c0d06caSMauro Carvalho Chehab .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
6200c0d06caSMauro Carvalho Chehab .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
6210c0d06caSMauro Carvalho Chehab .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
6220c0d06caSMauro Carvalho Chehab .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
6230c0d06caSMauro Carvalho Chehab .vidioc_querystd = vidioc_querystd,
6240c0d06caSMauro Carvalho Chehab .vidioc_g_std = vidioc_g_std,
6250c0d06caSMauro Carvalho Chehab .vidioc_s_std = vidioc_s_std,
6260c0d06caSMauro Carvalho Chehab .vidioc_enum_input = vidioc_enum_input,
6270c0d06caSMauro Carvalho Chehab .vidioc_g_input = vidioc_g_input,
6280c0d06caSMauro Carvalho Chehab .vidioc_s_input = vidioc_s_input,
6290c0d06caSMauro Carvalho Chehab
6300c0d06caSMauro Carvalho Chehab /* vb2 takes care of these */
6310c0d06caSMauro Carvalho Chehab .vidioc_reqbufs = vb2_ioctl_reqbufs,
6320c0d06caSMauro Carvalho Chehab .vidioc_querybuf = vb2_ioctl_querybuf,
6330c0d06caSMauro Carvalho Chehab .vidioc_qbuf = vb2_ioctl_qbuf,
6340c0d06caSMauro Carvalho Chehab .vidioc_dqbuf = vb2_ioctl_dqbuf,
6350c0d06caSMauro Carvalho Chehab .vidioc_streamon = vb2_ioctl_streamon,
6360c0d06caSMauro Carvalho Chehab .vidioc_streamoff = vb2_ioctl_streamoff,
637ebbb5637SHans Verkuil .vidioc_expbuf = vb2_ioctl_expbuf,
6380c0d06caSMauro Carvalho Chehab
6390c0d06caSMauro Carvalho Chehab .vidioc_log_status = v4l2_ctrl_log_status,
6400c0d06caSMauro Carvalho Chehab .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
6410c0d06caSMauro Carvalho Chehab .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
6420c0d06caSMauro Carvalho Chehab
6430c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
6440c0d06caSMauro Carvalho Chehab .vidioc_g_register = vidioc_g_register,
6450c0d06caSMauro Carvalho Chehab .vidioc_s_register = vidioc_s_register,
6460c0d06caSMauro Carvalho Chehab #endif
6470c0d06caSMauro Carvalho Chehab };
6480c0d06caSMauro Carvalho Chehab
6490c0d06caSMauro Carvalho Chehab /********************************************************************/
6500c0d06caSMauro Carvalho Chehab
6510c0d06caSMauro Carvalho Chehab /*
6520c0d06caSMauro Carvalho Chehab * Videobuf2 operations
6530c0d06caSMauro Carvalho Chehab */
queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])654df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *vq,
6550c0d06caSMauro Carvalho Chehab unsigned int *nbuffers, unsigned int *nplanes,
65636c0f8b3SHans Verkuil unsigned int sizes[], struct device *alloc_devs[])
6570c0d06caSMauro Carvalho Chehab {
6580c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vq);
6590c0d06caSMauro Carvalho Chehab unsigned long size;
6600c0d06caSMauro Carvalho Chehab
6610c0d06caSMauro Carvalho Chehab size = dev->width * dev->height * 2;
6620c0d06caSMauro Carvalho Chehab
6630c0d06caSMauro Carvalho Chehab /*
6640c0d06caSMauro Carvalho Chehab * Here we can change the number of buffers being requested.
6650c0d06caSMauro Carvalho Chehab * So, we set a minimum and a maximum like this:
6660c0d06caSMauro Carvalho Chehab */
6670c0d06caSMauro Carvalho Chehab *nbuffers = clamp_t(unsigned int, *nbuffers,
6680c0d06caSMauro Carvalho Chehab STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
6690c0d06caSMauro Carvalho Chehab
670d27d5f1aSHelen Fornazier if (*nplanes)
671d27d5f1aSHelen Fornazier return sizes[0] < size ? -EINVAL : 0;
672d27d5f1aSHelen Fornazier
6730c0d06caSMauro Carvalho Chehab /* This means a packed colorformat */
6740c0d06caSMauro Carvalho Chehab *nplanes = 1;
6750c0d06caSMauro Carvalho Chehab
6760c0d06caSMauro Carvalho Chehab sizes[0] = size;
6770c0d06caSMauro Carvalho Chehab
678890024adSEzequiel Garcia stk1160_dbg("%s: buffer count %d, each %ld bytes\n",
6790c0d06caSMauro Carvalho Chehab __func__, *nbuffers, size);
6800c0d06caSMauro Carvalho Chehab
6810c0d06caSMauro Carvalho Chehab return 0;
6820c0d06caSMauro Carvalho Chehab }
6830c0d06caSMauro Carvalho Chehab
buffer_queue(struct vb2_buffer * vb)6840c0d06caSMauro Carvalho Chehab static void buffer_queue(struct vb2_buffer *vb)
6850c0d06caSMauro Carvalho Chehab {
6860c0d06caSMauro Carvalho Chehab unsigned long flags;
6870c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
6882d700715SJunghak Sung struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6890c0d06caSMauro Carvalho Chehab struct stk1160_buffer *buf =
6902d700715SJunghak Sung container_of(vbuf, struct stk1160_buffer, vb);
6910c0d06caSMauro Carvalho Chehab
6920c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&dev->buf_lock, flags);
6930c0d06caSMauro Carvalho Chehab if (!dev->udev) {
6940c0d06caSMauro Carvalho Chehab /*
6950c0d06caSMauro Carvalho Chehab * If the device is disconnected return the buffer to userspace
6960c0d06caSMauro Carvalho Chehab * directly. The next QBUF call will fail with -ENODEV.
6970c0d06caSMauro Carvalho Chehab */
6982d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
6990c0d06caSMauro Carvalho Chehab } else {
7000c0d06caSMauro Carvalho Chehab
7010c0d06caSMauro Carvalho Chehab buf->mem = vb2_plane_vaddr(vb, 0);
7020c0d06caSMauro Carvalho Chehab buf->length = vb2_plane_size(vb, 0);
7030c0d06caSMauro Carvalho Chehab buf->bytesused = 0;
7040c0d06caSMauro Carvalho Chehab buf->pos = 0;
7050c0d06caSMauro Carvalho Chehab
7060c0d06caSMauro Carvalho Chehab /*
7070c0d06caSMauro Carvalho Chehab * If buffer length is less from expected then we return
7080c0d06caSMauro Carvalho Chehab * the buffer to userspace directly.
7090c0d06caSMauro Carvalho Chehab */
7100c0d06caSMauro Carvalho Chehab if (buf->length < dev->width * dev->height * 2)
7112d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
7120c0d06caSMauro Carvalho Chehab else
7130c0d06caSMauro Carvalho Chehab list_add_tail(&buf->list, &dev->avail_bufs);
7140c0d06caSMauro Carvalho Chehab
7150c0d06caSMauro Carvalho Chehab }
7160c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&dev->buf_lock, flags);
7170c0d06caSMauro Carvalho Chehab }
7180c0d06caSMauro Carvalho Chehab
start_streaming(struct vb2_queue * vq,unsigned int count)7190c0d06caSMauro Carvalho Chehab static int start_streaming(struct vb2_queue *vq, unsigned int count)
7200c0d06caSMauro Carvalho Chehab {
7210c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vq);
7220c0d06caSMauro Carvalho Chehab return stk1160_start_streaming(dev);
7230c0d06caSMauro Carvalho Chehab }
7240c0d06caSMauro Carvalho Chehab
7250c0d06caSMauro Carvalho Chehab /* abort streaming and wait for last buffer */
stop_streaming(struct vb2_queue * vq)726e37559b2SHans Verkuil static void stop_streaming(struct vb2_queue *vq)
7270c0d06caSMauro Carvalho Chehab {
7280c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vq);
729e37559b2SHans Verkuil stk1160_stop_streaming(dev);
7300c0d06caSMauro Carvalho Chehab }
7310c0d06caSMauro Carvalho Chehab
7321bc17717SJulia Lawall static const struct vb2_ops stk1160_video_qops = {
7330c0d06caSMauro Carvalho Chehab .queue_setup = queue_setup,
7340c0d06caSMauro Carvalho Chehab .buf_queue = buffer_queue,
7350c0d06caSMauro Carvalho Chehab .start_streaming = start_streaming,
7360c0d06caSMauro Carvalho Chehab .stop_streaming = stop_streaming,
7370c0d06caSMauro Carvalho Chehab .wait_prepare = vb2_ops_wait_prepare,
7380c0d06caSMauro Carvalho Chehab .wait_finish = vb2_ops_wait_finish,
7390c0d06caSMauro Carvalho Chehab };
7400c0d06caSMauro Carvalho Chehab
74186844942SBhumika Goyal static const struct video_device v4l_template = {
7420c0d06caSMauro Carvalho Chehab .name = "stk1160",
7430c0d06caSMauro Carvalho Chehab .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
7440c0d06caSMauro Carvalho Chehab .fops = &stk1160_fops,
7450c0d06caSMauro Carvalho Chehab .ioctl_ops = &stk1160_ioctl_ops,
7460c0d06caSMauro Carvalho Chehab .release = video_device_release_empty,
7470c0d06caSMauro Carvalho Chehab };
7480c0d06caSMauro Carvalho Chehab
7490c0d06caSMauro Carvalho Chehab /********************************************************************/
7500c0d06caSMauro Carvalho Chehab
7510c0d06caSMauro Carvalho Chehab /* Must be called with both v4l_lock and vb_queue_lock hold */
stk1160_clear_queue(struct stk1160 * dev,enum vb2_buffer_state vb2_state)752fbe04b49SDafna Hirschfeld void stk1160_clear_queue(struct stk1160 *dev, enum vb2_buffer_state vb2_state)
7530c0d06caSMauro Carvalho Chehab {
7540c0d06caSMauro Carvalho Chehab struct stk1160_buffer *buf;
7550c0d06caSMauro Carvalho Chehab unsigned long flags;
7560c0d06caSMauro Carvalho Chehab
7570c0d06caSMauro Carvalho Chehab /* Release all active buffers */
7580c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&dev->buf_lock, flags);
7590c0d06caSMauro Carvalho Chehab while (!list_empty(&dev->avail_bufs)) {
7600c0d06caSMauro Carvalho Chehab buf = list_first_entry(&dev->avail_bufs,
7610c0d06caSMauro Carvalho Chehab struct stk1160_buffer, list);
7620c0d06caSMauro Carvalho Chehab list_del(&buf->list);
763fbe04b49SDafna Hirschfeld vb2_buffer_done(&buf->vb.vb2_buf, vb2_state);
764890024adSEzequiel Garcia stk1160_dbg("buffer [%p/%d] aborted\n",
7652d700715SJunghak Sung buf, buf->vb.vb2_buf.index);
7660c0d06caSMauro Carvalho Chehab }
767aeff0927SEzequiel Garcia
768aeff0927SEzequiel Garcia /* It's important to release the current buffer */
769aeff0927SEzequiel Garcia if (dev->isoc_ctl.buf) {
770aeff0927SEzequiel Garcia buf = dev->isoc_ctl.buf;
7710c0d06caSMauro Carvalho Chehab dev->isoc_ctl.buf = NULL;
772aeff0927SEzequiel Garcia
773fbe04b49SDafna Hirschfeld vb2_buffer_done(&buf->vb.vb2_buf, vb2_state);
774890024adSEzequiel Garcia stk1160_dbg("buffer [%p/%d] aborted\n",
7752d700715SJunghak Sung buf, buf->vb.vb2_buf.index);
776aeff0927SEzequiel Garcia }
7770c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&dev->buf_lock, flags);
7780c0d06caSMauro Carvalho Chehab }
7790c0d06caSMauro Carvalho Chehab
stk1160_vb2_setup(struct stk1160 * dev)7800c0d06caSMauro Carvalho Chehab int stk1160_vb2_setup(struct stk1160 *dev)
7810c0d06caSMauro Carvalho Chehab {
7820c0d06caSMauro Carvalho Chehab int rc;
7830c0d06caSMauro Carvalho Chehab struct vb2_queue *q;
7840c0d06caSMauro Carvalho Chehab
7850c0d06caSMauro Carvalho Chehab q = &dev->vb_vidq;
7860c0d06caSMauro Carvalho Chehab q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
787ebbb5637SHans Verkuil q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
7880c0d06caSMauro Carvalho Chehab q->drv_priv = dev;
7890c0d06caSMauro Carvalho Chehab q->buf_struct_size = sizeof(struct stk1160_buffer);
7900c0d06caSMauro Carvalho Chehab q->ops = &stk1160_video_qops;
7910c0d06caSMauro Carvalho Chehab q->mem_ops = &vb2_vmalloc_memops;
792de3e581fSEzequiel Garcia q->lock = &dev->vb_queue_lock;
793ade48681SSakari Ailus q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
7940c0d06caSMauro Carvalho Chehab
7950c0d06caSMauro Carvalho Chehab rc = vb2_queue_init(q);
7960c0d06caSMauro Carvalho Chehab if (rc < 0)
7970c0d06caSMauro Carvalho Chehab return rc;
7980c0d06caSMauro Carvalho Chehab
7990c0d06caSMauro Carvalho Chehab /* initialize video dma queue */
8000c0d06caSMauro Carvalho Chehab INIT_LIST_HEAD(&dev->avail_bufs);
8010c0d06caSMauro Carvalho Chehab
8020c0d06caSMauro Carvalho Chehab return 0;
8030c0d06caSMauro Carvalho Chehab }
8040c0d06caSMauro Carvalho Chehab
stk1160_video_register(struct stk1160 * dev)8050c0d06caSMauro Carvalho Chehab int stk1160_video_register(struct stk1160 *dev)
8060c0d06caSMauro Carvalho Chehab {
8070c0d06caSMauro Carvalho Chehab int rc;
8080c0d06caSMauro Carvalho Chehab
8090c0d06caSMauro Carvalho Chehab /* Initialize video_device with a template structure */
8100c0d06caSMauro Carvalho Chehab dev->vdev = v4l_template;
8110c0d06caSMauro Carvalho Chehab dev->vdev.queue = &dev->vb_vidq;
8120c0d06caSMauro Carvalho Chehab
8130c0d06caSMauro Carvalho Chehab /*
8140c0d06caSMauro Carvalho Chehab * Provide mutexes for v4l2 core and for videobuf2 queue.
8150c0d06caSMauro Carvalho Chehab * It will be used to protect *only* v4l2 ioctls.
8160c0d06caSMauro Carvalho Chehab */
8170c0d06caSMauro Carvalho Chehab dev->vdev.lock = &dev->v4l_lock;
8180c0d06caSMauro Carvalho Chehab
8190c0d06caSMauro Carvalho Chehab /* This will be used to set video_device parent */
8200c0d06caSMauro Carvalho Chehab dev->vdev.v4l2_dev = &dev->v4l2_dev;
8218c3854d0SHans Verkuil dev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
8228c3854d0SHans Verkuil V4L2_CAP_READWRITE;
8230c0d06caSMauro Carvalho Chehab
8240c0d06caSMauro Carvalho Chehab /* NTSC is default */
8250c0d06caSMauro Carvalho Chehab dev->norm = V4L2_STD_NTSC_M;
8260c0d06caSMauro Carvalho Chehab dev->width = 720;
8270c0d06caSMauro Carvalho Chehab dev->height = 480;
8280c0d06caSMauro Carvalho Chehab
8290c0d06caSMauro Carvalho Chehab /* set default format */
8300c0d06caSMauro Carvalho Chehab dev->fmt = &format[0];
8310c0d06caSMauro Carvalho Chehab stk1160_set_std(dev);
8320c0d06caSMauro Carvalho Chehab
8338774bed9SLaurent Pinchart v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
8340c0d06caSMauro Carvalho Chehab dev->norm);
8350c0d06caSMauro Carvalho Chehab
8360c0d06caSMauro Carvalho Chehab video_set_drvdata(&dev->vdev, dev);
8377fbbbc78SHans Verkuil rc = video_register_device(&dev->vdev, VFL_TYPE_VIDEO, -1);
8380c0d06caSMauro Carvalho Chehab if (rc < 0) {
8390c0d06caSMauro Carvalho Chehab stk1160_err("video_register_device failed (%d)\n", rc);
8400c0d06caSMauro Carvalho Chehab return rc;
8410c0d06caSMauro Carvalho Chehab }
8420c0d06caSMauro Carvalho Chehab
8430c0d06caSMauro Carvalho Chehab v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
8440c0d06caSMauro Carvalho Chehab video_device_node_name(&dev->vdev));
8450c0d06caSMauro Carvalho Chehab
8460c0d06caSMauro Carvalho Chehab return 0;
8470c0d06caSMauro Carvalho Chehab }
848