10c0d06caSMauro Carvalho Chehab /* 20c0d06caSMauro Carvalho Chehab * STK1160 driver 30c0d06caSMauro Carvalho Chehab * 40c0d06caSMauro Carvalho Chehab * Copyright (C) 2012 Ezequiel Garcia 50c0d06caSMauro Carvalho Chehab * <elezegarcia--a.t--gmail.com> 60c0d06caSMauro Carvalho Chehab * 70c0d06caSMauro Carvalho Chehab * Based on Easycap driver by R.M. Thomas 80c0d06caSMauro Carvalho Chehab * Copyright (C) 2010 R.M. Thomas 90c0d06caSMauro Carvalho Chehab * <rmthomas--a.t--sciolus.org> 100c0d06caSMauro Carvalho Chehab * 110c0d06caSMauro Carvalho Chehab * This program is free software; you can redistribute it and/or modify 120c0d06caSMauro Carvalho Chehab * it under the terms of the GNU General Public License as published by 130c0d06caSMauro Carvalho Chehab * the Free Software Foundation; either version 2 of the License, or 140c0d06caSMauro Carvalho Chehab * (at your option) any later version. 150c0d06caSMauro Carvalho Chehab * 160c0d06caSMauro Carvalho Chehab * This program is distributed in the hope that it will be useful, 170c0d06caSMauro Carvalho Chehab * but WITHOUT ANY WARRANTY; without even the implied warranty of 180c0d06caSMauro Carvalho Chehab * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 190c0d06caSMauro Carvalho Chehab * GNU General Public License for more details. 200c0d06caSMauro Carvalho Chehab * 210c0d06caSMauro Carvalho Chehab */ 220c0d06caSMauro Carvalho Chehab 230c0d06caSMauro Carvalho Chehab #include <linux/module.h> 240c0d06caSMauro Carvalho Chehab #include <linux/usb.h> 250c0d06caSMauro Carvalho Chehab #include <linux/mm.h> 260c0d06caSMauro Carvalho Chehab #include <linux/slab.h> 270c0d06caSMauro Carvalho Chehab 280c0d06caSMauro Carvalho Chehab #include <linux/videodev2.h> 290c0d06caSMauro Carvalho Chehab #include <media/v4l2-device.h> 300c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h> 310c0d06caSMauro Carvalho Chehab #include <media/v4l2-ioctl.h> 320c0d06caSMauro Carvalho Chehab #include <media/v4l2-fh.h> 330c0d06caSMauro Carvalho Chehab #include <media/v4l2-event.h> 340c0d06caSMauro Carvalho Chehab #include <media/videobuf2-vmalloc.h> 350c0d06caSMauro Carvalho Chehab 36b5dcee22SMauro Carvalho Chehab #include <media/i2c/saa7115.h> 370c0d06caSMauro Carvalho Chehab 380c0d06caSMauro Carvalho Chehab #include "stk1160.h" 390c0d06caSMauro Carvalho Chehab #include "stk1160-reg.h" 400c0d06caSMauro Carvalho Chehab 410c0d06caSMauro Carvalho Chehab static bool keep_buffers; 420c0d06caSMauro Carvalho Chehab module_param(keep_buffers, bool, 0644); 430c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming"); 440c0d06caSMauro Carvalho Chehab 45d3194520SEzequiel Garcia enum stk1160_decimate_mode { 46d3194520SEzequiel Garcia STK1160_DECIMATE_MORE_THAN_HALF, 47d3194520SEzequiel Garcia STK1160_DECIMATE_LESS_THAN_HALF, 48d3194520SEzequiel Garcia }; 49d3194520SEzequiel Garcia 50d3194520SEzequiel Garcia struct stk1160_decimate_ctrl { 51d3194520SEzequiel Garcia bool col_en, row_en; 52d3194520SEzequiel Garcia enum stk1160_decimate_mode col_mode, row_mode; 53d3194520SEzequiel Garcia unsigned int col_n, row_n; 54d3194520SEzequiel Garcia }; 55d3194520SEzequiel Garcia 560c0d06caSMauro Carvalho Chehab /* supported video standards */ 570c0d06caSMauro Carvalho Chehab static struct stk1160_fmt format[] = { 580c0d06caSMauro Carvalho Chehab { 590c0d06caSMauro Carvalho Chehab .name = "16 bpp YUY2, 4:2:2, packed", 600c0d06caSMauro Carvalho Chehab .fourcc = V4L2_PIX_FMT_UYVY, 610c0d06caSMauro Carvalho Chehab .depth = 16, 620c0d06caSMauro Carvalho Chehab } 630c0d06caSMauro Carvalho Chehab }; 640c0d06caSMauro Carvalho Chehab 65d3194520SEzequiel Garcia /* 66d3194520SEzequiel Garcia * Helper to find the next divisor that results in modulo being zero. 67d3194520SEzequiel Garcia * This is required to guarantee valid decimation unit counts. 68d3194520SEzequiel Garcia */ 69d3194520SEzequiel Garcia static unsigned int 70d3194520SEzequiel Garcia div_round_integer(unsigned int x, unsigned int y) 71d3194520SEzequiel Garcia { 72d3194520SEzequiel Garcia for (;; y++) { 73d3194520SEzequiel Garcia if (x % y == 0) 74d3194520SEzequiel Garcia return x / y; 75d3194520SEzequiel Garcia } 76d3194520SEzequiel Garcia } 77d3194520SEzequiel Garcia 780c0d06caSMauro Carvalho Chehab static void stk1160_set_std(struct stk1160 *dev) 790c0d06caSMauro Carvalho Chehab { 800c0d06caSMauro Carvalho Chehab int i; 810c0d06caSMauro Carvalho Chehab 820c0d06caSMauro Carvalho Chehab static struct regval std525[] = { 830c0d06caSMauro Carvalho Chehab 840c0d06caSMauro Carvalho Chehab /* 720x480 */ 850c0d06caSMauro Carvalho Chehab 860c0d06caSMauro Carvalho Chehab /* Frame start */ 870c0d06caSMauro Carvalho Chehab {STK116_CFSPO_STX_L, 0x0000}, 880c0d06caSMauro Carvalho Chehab {STK116_CFSPO_STX_H, 0x0000}, 890c0d06caSMauro Carvalho Chehab {STK116_CFSPO_STY_L, 0x0003}, 900c0d06caSMauro Carvalho Chehab {STK116_CFSPO_STY_H, 0x0000}, 910c0d06caSMauro Carvalho Chehab 920c0d06caSMauro Carvalho Chehab /* Frame end */ 930c0d06caSMauro Carvalho Chehab {STK116_CFEPO_ENX_L, 0x05a0}, 940c0d06caSMauro Carvalho Chehab {STK116_CFEPO_ENX_H, 0x0005}, 950c0d06caSMauro Carvalho Chehab {STK116_CFEPO_ENY_L, 0x00f3}, 960c0d06caSMauro Carvalho Chehab {STK116_CFEPO_ENY_H, 0x0000}, 970c0d06caSMauro Carvalho Chehab 980c0d06caSMauro Carvalho Chehab {0xffff, 0xffff} 990c0d06caSMauro Carvalho Chehab }; 1000c0d06caSMauro Carvalho Chehab 1010c0d06caSMauro Carvalho Chehab static struct regval std625[] = { 1020c0d06caSMauro Carvalho Chehab 1030c0d06caSMauro Carvalho Chehab /* 720x576 */ 1040c0d06caSMauro Carvalho Chehab 1050c0d06caSMauro Carvalho Chehab /* TODO: Each line of frame has some junk at the end */ 1060c0d06caSMauro Carvalho Chehab /* Frame start */ 1070c0d06caSMauro Carvalho Chehab {STK116_CFSPO, 0x0000}, 1080c0d06caSMauro Carvalho Chehab {STK116_CFSPO+1, 0x0000}, 1090c0d06caSMauro Carvalho Chehab {STK116_CFSPO+2, 0x0001}, 1100c0d06caSMauro Carvalho Chehab {STK116_CFSPO+3, 0x0000}, 1110c0d06caSMauro Carvalho Chehab 1120c0d06caSMauro Carvalho Chehab /* Frame end */ 1130c0d06caSMauro Carvalho Chehab {STK116_CFEPO, 0x05a0}, 1140c0d06caSMauro Carvalho Chehab {STK116_CFEPO+1, 0x0005}, 1150c0d06caSMauro Carvalho Chehab {STK116_CFEPO+2, 0x0121}, 1160c0d06caSMauro Carvalho Chehab {STK116_CFEPO+3, 0x0001}, 1170c0d06caSMauro Carvalho Chehab 1180c0d06caSMauro Carvalho Chehab {0xffff, 0xffff} 1190c0d06caSMauro Carvalho Chehab }; 1200c0d06caSMauro Carvalho Chehab 1210c0d06caSMauro Carvalho Chehab if (dev->norm & V4L2_STD_525_60) { 1220c0d06caSMauro Carvalho Chehab stk1160_dbg("registers to NTSC like standard\n"); 1230c0d06caSMauro Carvalho Chehab for (i = 0; std525[i].reg != 0xffff; i++) 1240c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, std525[i].reg, std525[i].val); 1250c0d06caSMauro Carvalho Chehab } else { 1260c0d06caSMauro Carvalho Chehab stk1160_dbg("registers to PAL like standard\n"); 1270c0d06caSMauro Carvalho Chehab for (i = 0; std625[i].reg != 0xffff; i++) 1280c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, std625[i].reg, std625[i].val); 1290c0d06caSMauro Carvalho Chehab } 1300c0d06caSMauro Carvalho Chehab 1310c0d06caSMauro Carvalho Chehab } 1320c0d06caSMauro Carvalho Chehab 133d3194520SEzequiel Garcia static void stk1160_set_fmt(struct stk1160 *dev, 134d3194520SEzequiel Garcia struct stk1160_decimate_ctrl *ctrl) 135d3194520SEzequiel Garcia { 136d3194520SEzequiel Garcia u32 val = 0; 137d3194520SEzequiel Garcia 138d3194520SEzequiel Garcia if (ctrl) { 139d3194520SEzequiel Garcia /* 140d3194520SEzequiel Garcia * Since the format is UYVY, the device must skip or send 141d3194520SEzequiel Garcia * a number of rows/columns multiple of four. This way, the 142d3194520SEzequiel Garcia * colour format is preserved. The STK1160_DEC_UNIT_SIZE bit 143d3194520SEzequiel Garcia * does exactly this. 144d3194520SEzequiel Garcia */ 145d3194520SEzequiel Garcia val |= STK1160_DEC_UNIT_SIZE; 146d3194520SEzequiel Garcia val |= ctrl->col_en ? STK1160_H_DEC_EN : 0; 147d3194520SEzequiel Garcia val |= ctrl->row_en ? STK1160_V_DEC_EN : 0; 148d3194520SEzequiel Garcia val |= ctrl->col_mode == 149d3194520SEzequiel Garcia STK1160_DECIMATE_MORE_THAN_HALF ? 150d3194520SEzequiel Garcia STK1160_H_DEC_MODE : 0; 151d3194520SEzequiel Garcia val |= ctrl->row_mode == 152d3194520SEzequiel Garcia STK1160_DECIMATE_MORE_THAN_HALF ? 153d3194520SEzequiel Garcia STK1160_V_DEC_MODE : 0; 154d3194520SEzequiel Garcia 155d3194520SEzequiel Garcia /* Horizontal count units */ 156d3194520SEzequiel Garcia stk1160_write_reg(dev, STK1160_DMCTRL_H_UNITS, ctrl->col_n); 157d3194520SEzequiel Garcia /* Vertical count units */ 158d3194520SEzequiel Garcia stk1160_write_reg(dev, STK1160_DMCTRL_V_UNITS, ctrl->row_n); 159d3194520SEzequiel Garcia 160d3194520SEzequiel Garcia stk1160_dbg("decimate 0x%x, column units %d, row units %d\n", 161d3194520SEzequiel Garcia val, ctrl->col_n, ctrl->row_n); 162d3194520SEzequiel Garcia } 163d3194520SEzequiel Garcia 164d3194520SEzequiel Garcia /* Decimation control */ 165d3194520SEzequiel Garcia stk1160_write_reg(dev, STK1160_DMCTRL, val); 166d3194520SEzequiel Garcia } 167d3194520SEzequiel Garcia 1680c0d06caSMauro Carvalho Chehab /* 1690c0d06caSMauro Carvalho Chehab * Set a new alternate setting. 1700c0d06caSMauro Carvalho Chehab * Returns true is dev->max_pkt_size has changed, false otherwise. 1710c0d06caSMauro Carvalho Chehab */ 1720c0d06caSMauro Carvalho Chehab static bool stk1160_set_alternate(struct stk1160 *dev) 1730c0d06caSMauro Carvalho Chehab { 1740c0d06caSMauro Carvalho Chehab int i, prev_alt = dev->alt; 1750c0d06caSMauro Carvalho Chehab unsigned int min_pkt_size; 1760c0d06caSMauro Carvalho Chehab bool new_pkt_size; 1770c0d06caSMauro Carvalho Chehab 1780c0d06caSMauro Carvalho Chehab /* 1790c0d06caSMauro Carvalho Chehab * If we don't set right alternate, 1800c0d06caSMauro Carvalho Chehab * then we will get a green screen with junk. 1810c0d06caSMauro Carvalho Chehab */ 1820c0d06caSMauro Carvalho Chehab min_pkt_size = STK1160_MIN_PKT_SIZE; 1830c0d06caSMauro Carvalho Chehab 1840c0d06caSMauro Carvalho Chehab for (i = 0; i < dev->num_alt; i++) { 1850c0d06caSMauro Carvalho Chehab /* stop when the selected alt setting offers enough bandwidth */ 1860c0d06caSMauro Carvalho Chehab if (dev->alt_max_pkt_size[i] >= min_pkt_size) { 1870c0d06caSMauro Carvalho Chehab dev->alt = i; 1880c0d06caSMauro Carvalho Chehab break; 1890c0d06caSMauro Carvalho Chehab /* 1900c0d06caSMauro Carvalho Chehab * otherwise make sure that we end up with the maximum bandwidth 1910c0d06caSMauro Carvalho Chehab * because the min_pkt_size equation might be wrong... 1920c0d06caSMauro Carvalho Chehab */ 1930c0d06caSMauro Carvalho Chehab } else if (dev->alt_max_pkt_size[i] > 1940c0d06caSMauro Carvalho Chehab dev->alt_max_pkt_size[dev->alt]) 1950c0d06caSMauro Carvalho Chehab dev->alt = i; 1960c0d06caSMauro Carvalho Chehab } 1970c0d06caSMauro Carvalho Chehab 198890024adSEzequiel Garcia stk1160_dbg("setting alternate %d\n", dev->alt); 1990c0d06caSMauro Carvalho Chehab 2000c0d06caSMauro Carvalho Chehab if (dev->alt != prev_alt) { 2010c0d06caSMauro Carvalho Chehab stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n", 2020c0d06caSMauro Carvalho Chehab min_pkt_size, dev->alt); 2030c0d06caSMauro Carvalho Chehab stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n", 2040c0d06caSMauro Carvalho Chehab dev->alt, dev->alt_max_pkt_size[dev->alt]); 2050c0d06caSMauro Carvalho Chehab usb_set_interface(dev->udev, 0, dev->alt); 2060c0d06caSMauro Carvalho Chehab } 2070c0d06caSMauro Carvalho Chehab 2080c0d06caSMauro Carvalho Chehab new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt]; 2090c0d06caSMauro Carvalho Chehab dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt]; 2100c0d06caSMauro Carvalho Chehab 2110c0d06caSMauro Carvalho Chehab return new_pkt_size; 2120c0d06caSMauro Carvalho Chehab } 2130c0d06caSMauro Carvalho Chehab 2140c0d06caSMauro Carvalho Chehab static int stk1160_start_streaming(struct stk1160 *dev) 2150c0d06caSMauro Carvalho Chehab { 2160c0d06caSMauro Carvalho Chehab bool new_pkt_size; 217f367cc1eSDan Carpenter int rc = 0; 218f367cc1eSDan Carpenter int i; 2190c0d06caSMauro Carvalho Chehab 2200c0d06caSMauro Carvalho Chehab /* Check device presence */ 2210c0d06caSMauro Carvalho Chehab if (!dev->udev) 2220c0d06caSMauro Carvalho Chehab return -ENODEV; 2230c0d06caSMauro Carvalho Chehab 2240c0d06caSMauro Carvalho Chehab if (mutex_lock_interruptible(&dev->v4l_lock)) 2250c0d06caSMauro Carvalho Chehab return -ERESTARTSYS; 2260c0d06caSMauro Carvalho Chehab /* 2270c0d06caSMauro Carvalho Chehab * For some reason it is mandatory to set alternate *first* 2280c0d06caSMauro Carvalho Chehab * and only *then* initialize isoc urbs. 2290c0d06caSMauro Carvalho Chehab * Someone please explain me why ;) 2300c0d06caSMauro Carvalho Chehab */ 2310c0d06caSMauro Carvalho Chehab new_pkt_size = stk1160_set_alternate(dev); 2320c0d06caSMauro Carvalho Chehab 2330c0d06caSMauro Carvalho Chehab /* 2340c0d06caSMauro Carvalho Chehab * We (re)allocate isoc urbs if: 2350c0d06caSMauro Carvalho Chehab * there is no allocated isoc urbs, OR 2360c0d06caSMauro Carvalho Chehab * a new dev->max_pkt_size is detected 2370c0d06caSMauro Carvalho Chehab */ 2380c0d06caSMauro Carvalho Chehab if (!dev->isoc_ctl.num_bufs || new_pkt_size) { 2390c0d06caSMauro Carvalho Chehab rc = stk1160_alloc_isoc(dev); 2400c0d06caSMauro Carvalho Chehab if (rc < 0) 2418ac45649SEzequiel Garcia goto out_stop_hw; 2420c0d06caSMauro Carvalho Chehab } 2430c0d06caSMauro Carvalho Chehab 2440c0d06caSMauro Carvalho Chehab /* submit urbs and enables IRQ */ 2450c0d06caSMauro Carvalho Chehab for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { 2460c0d06caSMauro Carvalho Chehab rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL); 2470c0d06caSMauro Carvalho Chehab if (rc) { 2480c0d06caSMauro Carvalho Chehab stk1160_err("cannot submit urb[%d] (%d)\n", i, rc); 2498ac45649SEzequiel Garcia goto out_uninit; 2500c0d06caSMauro Carvalho Chehab } 2510c0d06caSMauro Carvalho Chehab } 2520c0d06caSMauro Carvalho Chehab 2530c0d06caSMauro Carvalho Chehab /* Start saa711x */ 2540c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1); 2550c0d06caSMauro Carvalho Chehab 256e3e30f63SHans Verkuil dev->sequence = 0; 257e3e30f63SHans Verkuil 2580c0d06caSMauro Carvalho Chehab /* Start stk1160 */ 2590c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL, 0xb3); 2600c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); 2610c0d06caSMauro Carvalho Chehab 2620c0d06caSMauro Carvalho Chehab stk1160_dbg("streaming started\n"); 2630c0d06caSMauro Carvalho Chehab 2648ac45649SEzequiel Garcia mutex_unlock(&dev->v4l_lock); 2658ac45649SEzequiel Garcia 2668ac45649SEzequiel Garcia return 0; 2678ac45649SEzequiel Garcia 2688ac45649SEzequiel Garcia out_uninit: 2698ac45649SEzequiel Garcia stk1160_uninit_isoc(dev); 2708ac45649SEzequiel Garcia out_stop_hw: 2718ac45649SEzequiel Garcia usb_set_interface(dev->udev, 0, 0); 2728ac45649SEzequiel Garcia stk1160_clear_queue(dev); 2738ac45649SEzequiel Garcia 2740c0d06caSMauro Carvalho Chehab mutex_unlock(&dev->v4l_lock); 2750c0d06caSMauro Carvalho Chehab 276f367cc1eSDan Carpenter return rc; 2770c0d06caSMauro Carvalho Chehab } 2780c0d06caSMauro Carvalho Chehab 2790c0d06caSMauro Carvalho Chehab /* Must be called with v4l_lock hold */ 2800c0d06caSMauro Carvalho Chehab static void stk1160_stop_hw(struct stk1160 *dev) 2810c0d06caSMauro Carvalho Chehab { 2820c0d06caSMauro Carvalho Chehab /* If the device is not physically present, there is nothing to do */ 2830c0d06caSMauro Carvalho Chehab if (!dev->udev) 2840c0d06caSMauro Carvalho Chehab return; 2850c0d06caSMauro Carvalho Chehab 2860c0d06caSMauro Carvalho Chehab /* set alternate 0 */ 2870c0d06caSMauro Carvalho Chehab dev->alt = 0; 288890024adSEzequiel Garcia stk1160_dbg("setting alternate %d\n", dev->alt); 2890c0d06caSMauro Carvalho Chehab usb_set_interface(dev->udev, 0, 0); 2900c0d06caSMauro Carvalho Chehab 2910c0d06caSMauro Carvalho Chehab /* Stop stk1160 */ 2920c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL, 0x00); 2930c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); 2940c0d06caSMauro Carvalho Chehab 2950c0d06caSMauro Carvalho Chehab /* Stop saa711x */ 2960c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); 2970c0d06caSMauro Carvalho Chehab } 2980c0d06caSMauro Carvalho Chehab 2990c0d06caSMauro Carvalho Chehab static int stk1160_stop_streaming(struct stk1160 *dev) 3000c0d06caSMauro Carvalho Chehab { 3010c0d06caSMauro Carvalho Chehab if (mutex_lock_interruptible(&dev->v4l_lock)) 3020c0d06caSMauro Carvalho Chehab return -ERESTARTSYS; 3030c0d06caSMauro Carvalho Chehab 304aeff0927SEzequiel Garcia /* 305aeff0927SEzequiel Garcia * Once URBs are cancelled, the URB complete handler 306aeff0927SEzequiel Garcia * won't be running. This is required to safely release the 307aeff0927SEzequiel Garcia * current buffer (dev->isoc_ctl.buf). 308aeff0927SEzequiel Garcia */ 3090c0d06caSMauro Carvalho Chehab stk1160_cancel_isoc(dev); 3100c0d06caSMauro Carvalho Chehab 3110c0d06caSMauro Carvalho Chehab /* 3120c0d06caSMauro Carvalho Chehab * It is possible to keep buffers around using a module parameter. 3130c0d06caSMauro Carvalho Chehab * This is intended to avoid memory fragmentation. 3140c0d06caSMauro Carvalho Chehab */ 3150c0d06caSMauro Carvalho Chehab if (!keep_buffers) 3160c0d06caSMauro Carvalho Chehab stk1160_free_isoc(dev); 3170c0d06caSMauro Carvalho Chehab 3180c0d06caSMauro Carvalho Chehab stk1160_stop_hw(dev); 3190c0d06caSMauro Carvalho Chehab 3200c0d06caSMauro Carvalho Chehab stk1160_clear_queue(dev); 3210c0d06caSMauro Carvalho Chehab 3220c0d06caSMauro Carvalho Chehab stk1160_dbg("streaming stopped\n"); 3230c0d06caSMauro Carvalho Chehab 3240c0d06caSMauro Carvalho Chehab mutex_unlock(&dev->v4l_lock); 3250c0d06caSMauro Carvalho Chehab 3260c0d06caSMauro Carvalho Chehab return 0; 3270c0d06caSMauro Carvalho Chehab } 3280c0d06caSMauro Carvalho Chehab 329*ff05c984SBhumika Goyal static const struct v4l2_file_operations stk1160_fops = { 3300c0d06caSMauro Carvalho Chehab .owner = THIS_MODULE, 3310c0d06caSMauro Carvalho Chehab .open = v4l2_fh_open, 3320c0d06caSMauro Carvalho Chehab .release = vb2_fop_release, 3330c0d06caSMauro Carvalho Chehab .read = vb2_fop_read, 3340c0d06caSMauro Carvalho Chehab .poll = vb2_fop_poll, 3350c0d06caSMauro Carvalho Chehab .mmap = vb2_fop_mmap, 3360c0d06caSMauro Carvalho Chehab .unlocked_ioctl = video_ioctl2, 3370c0d06caSMauro Carvalho Chehab }; 3380c0d06caSMauro Carvalho Chehab 3390c0d06caSMauro Carvalho Chehab /* 3400c0d06caSMauro Carvalho Chehab * vidioc ioctls 3410c0d06caSMauro Carvalho Chehab */ 3420c0d06caSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, 3430c0d06caSMauro Carvalho Chehab void *priv, struct v4l2_capability *cap) 3440c0d06caSMauro Carvalho Chehab { 3450c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 3460c0d06caSMauro Carvalho Chehab 3470c0d06caSMauro Carvalho Chehab strcpy(cap->driver, "stk1160"); 3480c0d06caSMauro Carvalho Chehab strcpy(cap->card, "stk1160"); 3490c0d06caSMauro Carvalho Chehab usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 3500c0d06caSMauro Carvalho Chehab cap->device_caps = 3510c0d06caSMauro Carvalho Chehab V4L2_CAP_VIDEO_CAPTURE | 3520c0d06caSMauro Carvalho Chehab V4L2_CAP_STREAMING | 3530c0d06caSMauro Carvalho Chehab V4L2_CAP_READWRITE; 3540c0d06caSMauro Carvalho Chehab cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 3550c0d06caSMauro Carvalho Chehab return 0; 3560c0d06caSMauro Carvalho Chehab } 3570c0d06caSMauro Carvalho Chehab 3580c0d06caSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 3590c0d06caSMauro Carvalho Chehab struct v4l2_fmtdesc *f) 3600c0d06caSMauro Carvalho Chehab { 3610c0d06caSMauro Carvalho Chehab if (f->index != 0) 3620c0d06caSMauro Carvalho Chehab return -EINVAL; 3630c0d06caSMauro Carvalho Chehab 3640c0d06caSMauro Carvalho Chehab strlcpy(f->description, format[f->index].name, sizeof(f->description)); 3650c0d06caSMauro Carvalho Chehab f->pixelformat = format[f->index].fourcc; 3660c0d06caSMauro Carvalho Chehab return 0; 3670c0d06caSMauro Carvalho Chehab } 3680c0d06caSMauro Carvalho Chehab 3690c0d06caSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 3700c0d06caSMauro Carvalho Chehab struct v4l2_format *f) 3710c0d06caSMauro Carvalho Chehab { 3720c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 3730c0d06caSMauro Carvalho Chehab 3740c0d06caSMauro Carvalho Chehab f->fmt.pix.width = dev->width; 3750c0d06caSMauro Carvalho Chehab f->fmt.pix.height = dev->height; 3760c0d06caSMauro Carvalho Chehab f->fmt.pix.field = V4L2_FIELD_INTERLACED; 3770c0d06caSMauro Carvalho Chehab f->fmt.pix.pixelformat = dev->fmt->fourcc; 3780c0d06caSMauro Carvalho Chehab f->fmt.pix.bytesperline = dev->width * 2; 3790c0d06caSMauro Carvalho Chehab f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; 3800c0d06caSMauro Carvalho Chehab f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 3810c0d06caSMauro Carvalho Chehab 3820c0d06caSMauro Carvalho Chehab return 0; 3830c0d06caSMauro Carvalho Chehab } 3840c0d06caSMauro Carvalho Chehab 385d3194520SEzequiel Garcia static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f, 386d3194520SEzequiel Garcia struct stk1160_decimate_ctrl *ctrl) 387d3194520SEzequiel Garcia { 388d3194520SEzequiel Garcia unsigned int width, height; 389d3194520SEzequiel Garcia unsigned int base_width, base_height; 390d3194520SEzequiel Garcia unsigned int col_n, row_n; 391d3194520SEzequiel Garcia enum stk1160_decimate_mode col_mode, row_mode; 392d3194520SEzequiel Garcia bool col_en, row_en; 393d3194520SEzequiel Garcia 394d3194520SEzequiel Garcia base_width = 720; 395d3194520SEzequiel Garcia base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576; 396d3194520SEzequiel Garcia 397d3194520SEzequiel Garcia /* Minimum width and height is 5% the frame size */ 398d3194520SEzequiel Garcia width = clamp_t(unsigned int, f->fmt.pix.width, 399d3194520SEzequiel Garcia base_width / 20, base_width); 400d3194520SEzequiel Garcia height = clamp_t(unsigned int, f->fmt.pix.height, 401d3194520SEzequiel Garcia base_height / 20, base_height); 402d3194520SEzequiel Garcia 403d3194520SEzequiel Garcia /* Let's set default no decimation values */ 404d3194520SEzequiel Garcia col_n = 0; 405d3194520SEzequiel Garcia row_n = 0; 406d3194520SEzequiel Garcia col_en = false; 407d3194520SEzequiel Garcia row_en = false; 408d3194520SEzequiel Garcia f->fmt.pix.width = base_width; 409d3194520SEzequiel Garcia f->fmt.pix.height = base_height; 410d3194520SEzequiel Garcia row_mode = STK1160_DECIMATE_LESS_THAN_HALF; 411d3194520SEzequiel Garcia col_mode = STK1160_DECIMATE_LESS_THAN_HALF; 412d3194520SEzequiel Garcia 413d3194520SEzequiel Garcia if (width < base_width && width > base_width / 2) { 414d3194520SEzequiel Garcia /* 415d3194520SEzequiel Garcia * The device will send count units for each 416d3194520SEzequiel Garcia * unit skipped. This means count unit is: 417d3194520SEzequiel Garcia * 418d3194520SEzequiel Garcia * n = width / (frame width - width) 419d3194520SEzequiel Garcia * 420d3194520SEzequiel Garcia * And the width is: 421d3194520SEzequiel Garcia * 422d3194520SEzequiel Garcia * width = (n / n + 1) * frame width 423d3194520SEzequiel Garcia */ 424d3194520SEzequiel Garcia col_n = div_round_integer(width, base_width - width); 425d3194520SEzequiel Garcia if (col_n > 0 && col_n <= 255) { 426d3194520SEzequiel Garcia col_en = true; 427d3194520SEzequiel Garcia col_mode = STK1160_DECIMATE_LESS_THAN_HALF; 428d3194520SEzequiel Garcia f->fmt.pix.width = (base_width * col_n) / (col_n + 1); 429d3194520SEzequiel Garcia } 430d3194520SEzequiel Garcia 431d3194520SEzequiel Garcia } else if (width <= base_width / 2) { 432d3194520SEzequiel Garcia 433d3194520SEzequiel Garcia /* 434d3194520SEzequiel Garcia * The device will skip count units for each 435d3194520SEzequiel Garcia * unit sent. This means count is: 436d3194520SEzequiel Garcia * 437d3194520SEzequiel Garcia * n = (frame width / width) - 1 438d3194520SEzequiel Garcia * 439d3194520SEzequiel Garcia * And the width is: 440d3194520SEzequiel Garcia * 441d3194520SEzequiel Garcia * width = frame width / (n + 1) 442d3194520SEzequiel Garcia */ 443d3194520SEzequiel Garcia col_n = div_round_integer(base_width, width) - 1; 444d3194520SEzequiel Garcia if (col_n > 0 && col_n <= 255) { 445d3194520SEzequiel Garcia col_en = true; 446d3194520SEzequiel Garcia col_mode = STK1160_DECIMATE_MORE_THAN_HALF; 447d3194520SEzequiel Garcia f->fmt.pix.width = base_width / (col_n + 1); 448d3194520SEzequiel Garcia } 449d3194520SEzequiel Garcia } 450d3194520SEzequiel Garcia 451d3194520SEzequiel Garcia if (height < base_height && height > base_height / 2) { 452d3194520SEzequiel Garcia row_n = div_round_integer(height, base_height - height); 453d3194520SEzequiel Garcia if (row_n > 0 && row_n <= 255) { 454d3194520SEzequiel Garcia row_en = true; 455d3194520SEzequiel Garcia row_mode = STK1160_DECIMATE_LESS_THAN_HALF; 456d3194520SEzequiel Garcia f->fmt.pix.height = (base_height * row_n) / (row_n + 1); 457d3194520SEzequiel Garcia } 458d3194520SEzequiel Garcia 459d3194520SEzequiel Garcia } else if (height <= base_height / 2) { 460d3194520SEzequiel Garcia row_n = div_round_integer(base_height, height) - 1; 461d3194520SEzequiel Garcia if (row_n > 0 && row_n <= 255) { 462d3194520SEzequiel Garcia row_en = true; 463d3194520SEzequiel Garcia row_mode = STK1160_DECIMATE_MORE_THAN_HALF; 464d3194520SEzequiel Garcia f->fmt.pix.height = base_height / (row_n + 1); 465d3194520SEzequiel Garcia } 466d3194520SEzequiel Garcia } 467d3194520SEzequiel Garcia 468d3194520SEzequiel Garcia f->fmt.pix.pixelformat = dev->fmt->fourcc; 469d3194520SEzequiel Garcia f->fmt.pix.field = V4L2_FIELD_INTERLACED; 470d3194520SEzequiel Garcia f->fmt.pix.bytesperline = f->fmt.pix.width * 2; 471d3194520SEzequiel Garcia f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; 472d3194520SEzequiel Garcia f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 473d3194520SEzequiel Garcia 474d3194520SEzequiel Garcia if (ctrl) { 475d3194520SEzequiel Garcia ctrl->col_en = col_en; 476d3194520SEzequiel Garcia ctrl->col_n = col_n; 477d3194520SEzequiel Garcia ctrl->col_mode = col_mode; 478d3194520SEzequiel Garcia ctrl->row_en = row_en; 479d3194520SEzequiel Garcia ctrl->row_n = row_n; 480d3194520SEzequiel Garcia ctrl->row_mode = row_mode; 481d3194520SEzequiel Garcia } 482d3194520SEzequiel Garcia 483d3194520SEzequiel Garcia stk1160_dbg("width %d, height %d\n", 484d3194520SEzequiel Garcia f->fmt.pix.width, f->fmt.pix.height); 485d3194520SEzequiel Garcia return 0; 486d3194520SEzequiel Garcia } 487d3194520SEzequiel Garcia 4880c0d06caSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 4890c0d06caSMauro Carvalho Chehab struct v4l2_format *f) 4900c0d06caSMauro Carvalho Chehab { 4910c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 4920c0d06caSMauro Carvalho Chehab 493d3194520SEzequiel Garcia return stk1160_try_fmt(dev, f, NULL); 4940c0d06caSMauro Carvalho Chehab } 4950c0d06caSMauro Carvalho Chehab 4960c0d06caSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 4970c0d06caSMauro Carvalho Chehab struct v4l2_format *f) 4980c0d06caSMauro Carvalho Chehab { 4990c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 5000c0d06caSMauro Carvalho Chehab struct vb2_queue *q = &dev->vb_vidq; 501d3194520SEzequiel Garcia struct stk1160_decimate_ctrl ctrl; 502d3194520SEzequiel Garcia int rc; 5030c0d06caSMauro Carvalho Chehab 5040c0d06caSMauro Carvalho Chehab if (vb2_is_busy(q)) 5050c0d06caSMauro Carvalho Chehab return -EBUSY; 5060c0d06caSMauro Carvalho Chehab 507d3194520SEzequiel Garcia rc = stk1160_try_fmt(dev, f, &ctrl); 508d3194520SEzequiel Garcia if (rc < 0) 509d3194520SEzequiel Garcia return rc; 510d3194520SEzequiel Garcia dev->width = f->fmt.pix.width; 511d3194520SEzequiel Garcia dev->height = f->fmt.pix.height; 512d3194520SEzequiel Garcia stk1160_set_fmt(dev, &ctrl); 5130c0d06caSMauro Carvalho Chehab 5140c0d06caSMauro Carvalho Chehab return 0; 5150c0d06caSMauro Carvalho Chehab } 5160c0d06caSMauro Carvalho Chehab 5170c0d06caSMauro Carvalho Chehab static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm) 5180c0d06caSMauro Carvalho Chehab { 5190c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 5200c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm); 5210c0d06caSMauro Carvalho Chehab return 0; 5220c0d06caSMauro Carvalho Chehab } 5230c0d06caSMauro Carvalho Chehab 5240c0d06caSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) 5250c0d06caSMauro Carvalho Chehab { 5260c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 5270c0d06caSMauro Carvalho Chehab 5280c0d06caSMauro Carvalho Chehab *norm = dev->norm; 5290c0d06caSMauro Carvalho Chehab return 0; 5300c0d06caSMauro Carvalho Chehab } 5310c0d06caSMauro Carvalho Chehab 532314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm) 5330c0d06caSMauro Carvalho Chehab { 5340c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 5350c0d06caSMauro Carvalho Chehab struct vb2_queue *q = &dev->vb_vidq; 5360c0d06caSMauro Carvalho Chehab 537b3ba8fa6SEzequiel Garcia if (dev->norm == norm) 538b3ba8fa6SEzequiel Garcia return 0; 539b3ba8fa6SEzequiel Garcia 5400c0d06caSMauro Carvalho Chehab if (vb2_is_busy(q)) 5410c0d06caSMauro Carvalho Chehab return -EBUSY; 5420c0d06caSMauro Carvalho Chehab 5430c0d06caSMauro Carvalho Chehab /* Check device presence */ 5440c0d06caSMauro Carvalho Chehab if (!dev->udev) 5450c0d06caSMauro Carvalho Chehab return -ENODEV; 5460c0d06caSMauro Carvalho Chehab 5470c0d06caSMauro Carvalho Chehab /* We need to set this now, before we call stk1160_set_std */ 548d3194520SEzequiel Garcia dev->width = 720; 549d3194520SEzequiel Garcia dev->height = (norm & V4L2_STD_525_60) ? 480 : 576; 550314527acSHans Verkuil dev->norm = norm; 5510c0d06caSMauro Carvalho Chehab 5520c0d06caSMauro Carvalho Chehab stk1160_set_std(dev); 5530c0d06caSMauro Carvalho Chehab 554d3194520SEzequiel Garcia /* Calling with NULL disables frame decimation */ 555d3194520SEzequiel Garcia stk1160_set_fmt(dev, NULL); 556d3194520SEzequiel Garcia 5578774bed9SLaurent Pinchart v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, 5580c0d06caSMauro Carvalho Chehab dev->norm); 5590c0d06caSMauro Carvalho Chehab 5600c0d06caSMauro Carvalho Chehab return 0; 5610c0d06caSMauro Carvalho Chehab } 5620c0d06caSMauro Carvalho Chehab 5630c0d06caSMauro Carvalho Chehab 5640c0d06caSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv, 5650c0d06caSMauro Carvalho Chehab struct v4l2_input *i) 5660c0d06caSMauro Carvalho Chehab { 5670c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 5680c0d06caSMauro Carvalho Chehab 5690c0d06caSMauro Carvalho Chehab if (i->index > STK1160_MAX_INPUT) 5700c0d06caSMauro Carvalho Chehab return -EINVAL; 5710c0d06caSMauro Carvalho Chehab 57256a960bfSEzequiel Garcia /* S-Video special handling */ 57356a960bfSEzequiel Garcia if (i->index == STK1160_SVIDEO_INPUT) 57456a960bfSEzequiel Garcia sprintf(i->name, "S-Video"); 57556a960bfSEzequiel Garcia else 5760c0d06caSMauro Carvalho Chehab sprintf(i->name, "Composite%d", i->index); 57756a960bfSEzequiel Garcia 5780c0d06caSMauro Carvalho Chehab i->type = V4L2_INPUT_TYPE_CAMERA; 5790c0d06caSMauro Carvalho Chehab i->std = dev->vdev.tvnorms; 5800c0d06caSMauro Carvalho Chehab return 0; 5810c0d06caSMauro Carvalho Chehab } 5820c0d06caSMauro Carvalho Chehab 5830c0d06caSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 5840c0d06caSMauro Carvalho Chehab { 5850c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 5860c0d06caSMauro Carvalho Chehab *i = dev->ctl_input; 5870c0d06caSMauro Carvalho Chehab return 0; 5880c0d06caSMauro Carvalho Chehab } 5890c0d06caSMauro Carvalho Chehab 5900c0d06caSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 5910c0d06caSMauro Carvalho Chehab { 5920c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 5930c0d06caSMauro Carvalho Chehab 5940c0d06caSMauro Carvalho Chehab if (i > STK1160_MAX_INPUT) 5950c0d06caSMauro Carvalho Chehab return -EINVAL; 5960c0d06caSMauro Carvalho Chehab 5970c0d06caSMauro Carvalho Chehab dev->ctl_input = i; 5980c0d06caSMauro Carvalho Chehab 5990c0d06caSMauro Carvalho Chehab stk1160_select_input(dev); 6000c0d06caSMauro Carvalho Chehab 6010c0d06caSMauro Carvalho Chehab return 0; 6020c0d06caSMauro Carvalho Chehab } 6030c0d06caSMauro Carvalho Chehab 6040c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG 6050c0d06caSMauro Carvalho Chehab static int vidioc_g_register(struct file *file, void *priv, 6060c0d06caSMauro Carvalho Chehab struct v4l2_dbg_register *reg) 6070c0d06caSMauro Carvalho Chehab { 6080c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 6090c0d06caSMauro Carvalho Chehab int rc; 6100c0d06caSMauro Carvalho Chehab u8 val; 6110c0d06caSMauro Carvalho Chehab 6120c0d06caSMauro Carvalho Chehab /* Match host */ 6130c0d06caSMauro Carvalho Chehab rc = stk1160_read_reg(dev, reg->reg, &val); 6140c0d06caSMauro Carvalho Chehab reg->val = val; 6150c0d06caSMauro Carvalho Chehab reg->size = 1; 6160c0d06caSMauro Carvalho Chehab 6170c0d06caSMauro Carvalho Chehab return rc; 6180c0d06caSMauro Carvalho Chehab } 6190c0d06caSMauro Carvalho Chehab 6200c0d06caSMauro Carvalho Chehab static int vidioc_s_register(struct file *file, void *priv, 621977ba3b1SHans Verkuil const struct v4l2_dbg_register *reg) 6220c0d06caSMauro Carvalho Chehab { 6230c0d06caSMauro Carvalho Chehab struct stk1160 *dev = video_drvdata(file); 6240c0d06caSMauro Carvalho Chehab 6250c0d06caSMauro Carvalho Chehab /* Match host */ 6264580278fSHans Verkuil return stk1160_write_reg(dev, reg->reg, reg->val); 6270c0d06caSMauro Carvalho Chehab } 6280c0d06caSMauro Carvalho Chehab #endif 6290c0d06caSMauro Carvalho Chehab 6300c0d06caSMauro Carvalho Chehab static const struct v4l2_ioctl_ops stk1160_ioctl_ops = { 6310c0d06caSMauro Carvalho Chehab .vidioc_querycap = vidioc_querycap, 6320c0d06caSMauro Carvalho Chehab .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 6330c0d06caSMauro Carvalho Chehab .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 6340c0d06caSMauro Carvalho Chehab .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 6350c0d06caSMauro Carvalho Chehab .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 6360c0d06caSMauro Carvalho Chehab .vidioc_querystd = vidioc_querystd, 6370c0d06caSMauro Carvalho Chehab .vidioc_g_std = vidioc_g_std, 6380c0d06caSMauro Carvalho Chehab .vidioc_s_std = vidioc_s_std, 6390c0d06caSMauro Carvalho Chehab .vidioc_enum_input = vidioc_enum_input, 6400c0d06caSMauro Carvalho Chehab .vidioc_g_input = vidioc_g_input, 6410c0d06caSMauro Carvalho Chehab .vidioc_s_input = vidioc_s_input, 6420c0d06caSMauro Carvalho Chehab 6430c0d06caSMauro Carvalho Chehab /* vb2 takes care of these */ 6440c0d06caSMauro Carvalho Chehab .vidioc_reqbufs = vb2_ioctl_reqbufs, 6450c0d06caSMauro Carvalho Chehab .vidioc_querybuf = vb2_ioctl_querybuf, 6460c0d06caSMauro Carvalho Chehab .vidioc_qbuf = vb2_ioctl_qbuf, 6470c0d06caSMauro Carvalho Chehab .vidioc_dqbuf = vb2_ioctl_dqbuf, 6480c0d06caSMauro Carvalho Chehab .vidioc_streamon = vb2_ioctl_streamon, 6490c0d06caSMauro Carvalho Chehab .vidioc_streamoff = vb2_ioctl_streamoff, 650ebbb5637SHans Verkuil .vidioc_expbuf = vb2_ioctl_expbuf, 6510c0d06caSMauro Carvalho Chehab 6520c0d06caSMauro Carvalho Chehab .vidioc_log_status = v4l2_ctrl_log_status, 6530c0d06caSMauro Carvalho Chehab .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 6540c0d06caSMauro Carvalho Chehab .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 6550c0d06caSMauro Carvalho Chehab 6560c0d06caSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG 6570c0d06caSMauro Carvalho Chehab .vidioc_g_register = vidioc_g_register, 6580c0d06caSMauro Carvalho Chehab .vidioc_s_register = vidioc_s_register, 6590c0d06caSMauro Carvalho Chehab #endif 6600c0d06caSMauro Carvalho Chehab }; 6610c0d06caSMauro Carvalho Chehab 6620c0d06caSMauro Carvalho Chehab /********************************************************************/ 6630c0d06caSMauro Carvalho Chehab 6640c0d06caSMauro Carvalho Chehab /* 6650c0d06caSMauro Carvalho Chehab * Videobuf2 operations 6660c0d06caSMauro Carvalho Chehab */ 667df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *vq, 6680c0d06caSMauro Carvalho Chehab unsigned int *nbuffers, unsigned int *nplanes, 66936c0f8b3SHans Verkuil unsigned int sizes[], struct device *alloc_devs[]) 6700c0d06caSMauro Carvalho Chehab { 6710c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vq); 6720c0d06caSMauro Carvalho Chehab unsigned long size; 6730c0d06caSMauro Carvalho Chehab 6740c0d06caSMauro Carvalho Chehab size = dev->width * dev->height * 2; 6750c0d06caSMauro Carvalho Chehab 6760c0d06caSMauro Carvalho Chehab /* 6770c0d06caSMauro Carvalho Chehab * Here we can change the number of buffers being requested. 6780c0d06caSMauro Carvalho Chehab * So, we set a minimum and a maximum like this: 6790c0d06caSMauro Carvalho Chehab */ 6800c0d06caSMauro Carvalho Chehab *nbuffers = clamp_t(unsigned int, *nbuffers, 6810c0d06caSMauro Carvalho Chehab STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS); 6820c0d06caSMauro Carvalho Chehab 683d27d5f1aSHelen Fornazier if (*nplanes) 684d27d5f1aSHelen Fornazier return sizes[0] < size ? -EINVAL : 0; 685d27d5f1aSHelen Fornazier 6860c0d06caSMauro Carvalho Chehab /* This means a packed colorformat */ 6870c0d06caSMauro Carvalho Chehab *nplanes = 1; 6880c0d06caSMauro Carvalho Chehab 6890c0d06caSMauro Carvalho Chehab sizes[0] = size; 6900c0d06caSMauro Carvalho Chehab 691890024adSEzequiel Garcia stk1160_dbg("%s: buffer count %d, each %ld bytes\n", 6920c0d06caSMauro Carvalho Chehab __func__, *nbuffers, size); 6930c0d06caSMauro Carvalho Chehab 6940c0d06caSMauro Carvalho Chehab return 0; 6950c0d06caSMauro Carvalho Chehab } 6960c0d06caSMauro Carvalho Chehab 6970c0d06caSMauro Carvalho Chehab static void buffer_queue(struct vb2_buffer *vb) 6980c0d06caSMauro Carvalho Chehab { 6990c0d06caSMauro Carvalho Chehab unsigned long flags; 7000c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue); 7012d700715SJunghak Sung struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 7020c0d06caSMauro Carvalho Chehab struct stk1160_buffer *buf = 7032d700715SJunghak Sung container_of(vbuf, struct stk1160_buffer, vb); 7040c0d06caSMauro Carvalho Chehab 7050c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&dev->buf_lock, flags); 7060c0d06caSMauro Carvalho Chehab if (!dev->udev) { 7070c0d06caSMauro Carvalho Chehab /* 7080c0d06caSMauro Carvalho Chehab * If the device is disconnected return the buffer to userspace 7090c0d06caSMauro Carvalho Chehab * directly. The next QBUF call will fail with -ENODEV. 7100c0d06caSMauro Carvalho Chehab */ 7112d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 7120c0d06caSMauro Carvalho Chehab } else { 7130c0d06caSMauro Carvalho Chehab 7140c0d06caSMauro Carvalho Chehab buf->mem = vb2_plane_vaddr(vb, 0); 7150c0d06caSMauro Carvalho Chehab buf->length = vb2_plane_size(vb, 0); 7160c0d06caSMauro Carvalho Chehab buf->bytesused = 0; 7170c0d06caSMauro Carvalho Chehab buf->pos = 0; 7180c0d06caSMauro Carvalho Chehab 7190c0d06caSMauro Carvalho Chehab /* 7200c0d06caSMauro Carvalho Chehab * If buffer length is less from expected then we return 7210c0d06caSMauro Carvalho Chehab * the buffer to userspace directly. 7220c0d06caSMauro Carvalho Chehab */ 7230c0d06caSMauro Carvalho Chehab if (buf->length < dev->width * dev->height * 2) 7242d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 7250c0d06caSMauro Carvalho Chehab else 7260c0d06caSMauro Carvalho Chehab list_add_tail(&buf->list, &dev->avail_bufs); 7270c0d06caSMauro Carvalho Chehab 7280c0d06caSMauro Carvalho Chehab } 7290c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&dev->buf_lock, flags); 7300c0d06caSMauro Carvalho Chehab } 7310c0d06caSMauro Carvalho Chehab 7320c0d06caSMauro Carvalho Chehab static int start_streaming(struct vb2_queue *vq, unsigned int count) 7330c0d06caSMauro Carvalho Chehab { 7340c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vq); 7350c0d06caSMauro Carvalho Chehab return stk1160_start_streaming(dev); 7360c0d06caSMauro Carvalho Chehab } 7370c0d06caSMauro Carvalho Chehab 7380c0d06caSMauro Carvalho Chehab /* abort streaming and wait for last buffer */ 739e37559b2SHans Verkuil static void stop_streaming(struct vb2_queue *vq) 7400c0d06caSMauro Carvalho Chehab { 7410c0d06caSMauro Carvalho Chehab struct stk1160 *dev = vb2_get_drv_priv(vq); 742e37559b2SHans Verkuil stk1160_stop_streaming(dev); 7430c0d06caSMauro Carvalho Chehab } 7440c0d06caSMauro Carvalho Chehab 7451bc17717SJulia Lawall static const struct vb2_ops stk1160_video_qops = { 7460c0d06caSMauro Carvalho Chehab .queue_setup = queue_setup, 7470c0d06caSMauro Carvalho Chehab .buf_queue = buffer_queue, 7480c0d06caSMauro Carvalho Chehab .start_streaming = start_streaming, 7490c0d06caSMauro Carvalho Chehab .stop_streaming = stop_streaming, 7500c0d06caSMauro Carvalho Chehab .wait_prepare = vb2_ops_wait_prepare, 7510c0d06caSMauro Carvalho Chehab .wait_finish = vb2_ops_wait_finish, 7520c0d06caSMauro Carvalho Chehab }; 7530c0d06caSMauro Carvalho Chehab 7540c0d06caSMauro Carvalho Chehab static struct video_device v4l_template = { 7550c0d06caSMauro Carvalho Chehab .name = "stk1160", 7560c0d06caSMauro Carvalho Chehab .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50, 7570c0d06caSMauro Carvalho Chehab .fops = &stk1160_fops, 7580c0d06caSMauro Carvalho Chehab .ioctl_ops = &stk1160_ioctl_ops, 7590c0d06caSMauro Carvalho Chehab .release = video_device_release_empty, 7600c0d06caSMauro Carvalho Chehab }; 7610c0d06caSMauro Carvalho Chehab 7620c0d06caSMauro Carvalho Chehab /********************************************************************/ 7630c0d06caSMauro Carvalho Chehab 7640c0d06caSMauro Carvalho Chehab /* Must be called with both v4l_lock and vb_queue_lock hold */ 7650c0d06caSMauro Carvalho Chehab void stk1160_clear_queue(struct stk1160 *dev) 7660c0d06caSMauro Carvalho Chehab { 7670c0d06caSMauro Carvalho Chehab struct stk1160_buffer *buf; 7680c0d06caSMauro Carvalho Chehab unsigned long flags; 7690c0d06caSMauro Carvalho Chehab 7700c0d06caSMauro Carvalho Chehab /* Release all active buffers */ 7710c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&dev->buf_lock, flags); 7720c0d06caSMauro Carvalho Chehab while (!list_empty(&dev->avail_bufs)) { 7730c0d06caSMauro Carvalho Chehab buf = list_first_entry(&dev->avail_bufs, 7740c0d06caSMauro Carvalho Chehab struct stk1160_buffer, list); 7750c0d06caSMauro Carvalho Chehab list_del(&buf->list); 7762d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 777890024adSEzequiel Garcia stk1160_dbg("buffer [%p/%d] aborted\n", 7782d700715SJunghak Sung buf, buf->vb.vb2_buf.index); 7790c0d06caSMauro Carvalho Chehab } 780aeff0927SEzequiel Garcia 781aeff0927SEzequiel Garcia /* It's important to release the current buffer */ 782aeff0927SEzequiel Garcia if (dev->isoc_ctl.buf) { 783aeff0927SEzequiel Garcia buf = dev->isoc_ctl.buf; 7840c0d06caSMauro Carvalho Chehab dev->isoc_ctl.buf = NULL; 785aeff0927SEzequiel Garcia 7862d700715SJunghak Sung vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 787890024adSEzequiel Garcia stk1160_dbg("buffer [%p/%d] aborted\n", 7882d700715SJunghak Sung buf, buf->vb.vb2_buf.index); 789aeff0927SEzequiel Garcia } 7900c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&dev->buf_lock, flags); 7910c0d06caSMauro Carvalho Chehab } 7920c0d06caSMauro Carvalho Chehab 7930c0d06caSMauro Carvalho Chehab int stk1160_vb2_setup(struct stk1160 *dev) 7940c0d06caSMauro Carvalho Chehab { 7950c0d06caSMauro Carvalho Chehab int rc; 7960c0d06caSMauro Carvalho Chehab struct vb2_queue *q; 7970c0d06caSMauro Carvalho Chehab 7980c0d06caSMauro Carvalho Chehab q = &dev->vb_vidq; 7990c0d06caSMauro Carvalho Chehab q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 800ebbb5637SHans Verkuil q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 8010c0d06caSMauro Carvalho Chehab q->drv_priv = dev; 8020c0d06caSMauro Carvalho Chehab q->buf_struct_size = sizeof(struct stk1160_buffer); 8030c0d06caSMauro Carvalho Chehab q->ops = &stk1160_video_qops; 8040c0d06caSMauro Carvalho Chehab q->mem_ops = &vb2_vmalloc_memops; 805ade48681SSakari Ailus q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 8060c0d06caSMauro Carvalho Chehab 8070c0d06caSMauro Carvalho Chehab rc = vb2_queue_init(q); 8080c0d06caSMauro Carvalho Chehab if (rc < 0) 8090c0d06caSMauro Carvalho Chehab return rc; 8100c0d06caSMauro Carvalho Chehab 8110c0d06caSMauro Carvalho Chehab /* initialize video dma queue */ 8120c0d06caSMauro Carvalho Chehab INIT_LIST_HEAD(&dev->avail_bufs); 8130c0d06caSMauro Carvalho Chehab 8140c0d06caSMauro Carvalho Chehab return 0; 8150c0d06caSMauro Carvalho Chehab } 8160c0d06caSMauro Carvalho Chehab 8170c0d06caSMauro Carvalho Chehab int stk1160_video_register(struct stk1160 *dev) 8180c0d06caSMauro Carvalho Chehab { 8190c0d06caSMauro Carvalho Chehab int rc; 8200c0d06caSMauro Carvalho Chehab 8210c0d06caSMauro Carvalho Chehab /* Initialize video_device with a template structure */ 8220c0d06caSMauro Carvalho Chehab dev->vdev = v4l_template; 8230c0d06caSMauro Carvalho Chehab dev->vdev.queue = &dev->vb_vidq; 8240c0d06caSMauro Carvalho Chehab 8250c0d06caSMauro Carvalho Chehab /* 8260c0d06caSMauro Carvalho Chehab * Provide mutexes for v4l2 core and for videobuf2 queue. 8270c0d06caSMauro Carvalho Chehab * It will be used to protect *only* v4l2 ioctls. 8280c0d06caSMauro Carvalho Chehab */ 8290c0d06caSMauro Carvalho Chehab dev->vdev.lock = &dev->v4l_lock; 8300c0d06caSMauro Carvalho Chehab dev->vdev.queue->lock = &dev->vb_queue_lock; 8310c0d06caSMauro Carvalho Chehab 8320c0d06caSMauro Carvalho Chehab /* This will be used to set video_device parent */ 8330c0d06caSMauro Carvalho Chehab dev->vdev.v4l2_dev = &dev->v4l2_dev; 8340c0d06caSMauro Carvalho Chehab 8350c0d06caSMauro Carvalho Chehab /* NTSC is default */ 8360c0d06caSMauro Carvalho Chehab dev->norm = V4L2_STD_NTSC_M; 8370c0d06caSMauro Carvalho Chehab dev->width = 720; 8380c0d06caSMauro Carvalho Chehab dev->height = 480; 8390c0d06caSMauro Carvalho Chehab 8400c0d06caSMauro Carvalho Chehab /* set default format */ 8410c0d06caSMauro Carvalho Chehab dev->fmt = &format[0]; 8420c0d06caSMauro Carvalho Chehab stk1160_set_std(dev); 8430c0d06caSMauro Carvalho Chehab 8448774bed9SLaurent Pinchart v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, 8450c0d06caSMauro Carvalho Chehab dev->norm); 8460c0d06caSMauro Carvalho Chehab 8470c0d06caSMauro Carvalho Chehab video_set_drvdata(&dev->vdev, dev); 8480c0d06caSMauro Carvalho Chehab rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); 8490c0d06caSMauro Carvalho Chehab if (rc < 0) { 8500c0d06caSMauro Carvalho Chehab stk1160_err("video_register_device failed (%d)\n", rc); 8510c0d06caSMauro Carvalho Chehab return rc; 8520c0d06caSMauro Carvalho Chehab } 8530c0d06caSMauro Carvalho Chehab 8540c0d06caSMauro Carvalho Chehab v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", 8550c0d06caSMauro Carvalho Chehab video_device_node_name(&dev->vdev)); 8560c0d06caSMauro Carvalho Chehab 8570c0d06caSMauro Carvalho Chehab return 0; 8580c0d06caSMauro Carvalho Chehab } 859