xref: /openbmc/linux/drivers/media/usb/s2255/s2255drv.c (revision 7fb2e072d41b1da5ddf29a1ba62f0e380d94a855)
10aa77f6cSMauro Carvalho Chehab /*
20aa77f6cSMauro Carvalho Chehab  *  s2255drv.c - a driver for the Sensoray 2255 USB video capture device
30aa77f6cSMauro Carvalho Chehab  *
4d86c6a8cSDean Anderson  *   Copyright (C) 2007-2014 by Sensoray Company Inc.
50aa77f6cSMauro Carvalho Chehab  *                              Dean Anderson
60aa77f6cSMauro Carvalho Chehab  *
70aa77f6cSMauro Carvalho Chehab  * Some video buffer code based on vivi driver:
80aa77f6cSMauro Carvalho Chehab  *
90aa77f6cSMauro Carvalho Chehab  * Sensoray 2255 device supports 4 simultaneous channels.
100aa77f6cSMauro Carvalho Chehab  * The channels are not "crossbar" inputs, they are physically
110aa77f6cSMauro Carvalho Chehab  * attached to separate video decoders.
120aa77f6cSMauro Carvalho Chehab  *
130aa77f6cSMauro Carvalho Chehab  * Because of USB2.0 bandwidth limitations. There is only a
140aa77f6cSMauro Carvalho Chehab  * certain amount of data which may be transferred at one time.
150aa77f6cSMauro Carvalho Chehab  *
160aa77f6cSMauro Carvalho Chehab  * Example maximum bandwidth utilization:
170aa77f6cSMauro Carvalho Chehab  *
180aa77f6cSMauro Carvalho Chehab  * -full size, color mode YUYV or YUV422P: 2 channels at once
190aa77f6cSMauro Carvalho Chehab  * -full or half size Grey scale: all 4 channels at once
200aa77f6cSMauro Carvalho Chehab  * -half size, color mode YUYV or YUV422P: all 4 channels at once
210aa77f6cSMauro Carvalho Chehab  * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
220aa77f6cSMauro Carvalho Chehab  *  at once.
230aa77f6cSMauro Carvalho Chehab  *
240aa77f6cSMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or modify
250aa77f6cSMauro Carvalho Chehab  * it under the terms of the GNU General Public License as published by
260aa77f6cSMauro Carvalho Chehab  * the Free Software Foundation; either version 2 of the License, or
270aa77f6cSMauro Carvalho Chehab  * (at your option) any later version.
280aa77f6cSMauro Carvalho Chehab  *
290aa77f6cSMauro Carvalho Chehab  * This program is distributed in the hope that it will be useful,
300aa77f6cSMauro Carvalho Chehab  * but WITHOUT ANY WARRANTY; without even the implied warranty of
310aa77f6cSMauro Carvalho Chehab  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
320aa77f6cSMauro Carvalho Chehab  * GNU General Public License for more details.
330aa77f6cSMauro Carvalho Chehab  */
340aa77f6cSMauro Carvalho Chehab 
350aa77f6cSMauro Carvalho Chehab #include <linux/module.h>
360aa77f6cSMauro Carvalho Chehab #include <linux/firmware.h>
370aa77f6cSMauro Carvalho Chehab #include <linux/kernel.h>
380aa77f6cSMauro Carvalho Chehab #include <linux/mutex.h>
390aa77f6cSMauro Carvalho Chehab #include <linux/slab.h>
400aa77f6cSMauro Carvalho Chehab #include <linux/videodev2.h>
410aa77f6cSMauro Carvalho Chehab #include <linux/mm.h>
4244d06d82SHans Verkuil #include <linux/vmalloc.h>
4344d06d82SHans Verkuil #include <linux/usb.h>
442d700715SJunghak Sung #include <media/videobuf2-v4l2.h>
45340a30c5Ssensoray-dev #include <media/videobuf2-vmalloc.h>
460aa77f6cSMauro Carvalho Chehab #include <media/v4l2-common.h>
470aa77f6cSMauro Carvalho Chehab #include <media/v4l2-device.h>
480aa77f6cSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
49192f1e78SHans Verkuil #include <media/v4l2-ctrls.h>
5044d06d82SHans Verkuil #include <media/v4l2-event.h>
510aa77f6cSMauro Carvalho Chehab 
52340a30c5Ssensoray-dev #define S2255_VERSION		"1.25.1"
530aa77f6cSMauro Carvalho Chehab #define FIRMWARE_FILE_NAME "f2255usb.bin"
540aa77f6cSMauro Carvalho Chehab 
550aa77f6cSMauro Carvalho Chehab /* default JPEG quality */
560aa77f6cSMauro Carvalho Chehab #define S2255_DEF_JPEG_QUAL     50
570aa77f6cSMauro Carvalho Chehab /* vendor request in */
580aa77f6cSMauro Carvalho Chehab #define S2255_VR_IN		0
590aa77f6cSMauro Carvalho Chehab /* vendor request out */
600aa77f6cSMauro Carvalho Chehab #define S2255_VR_OUT		1
610aa77f6cSMauro Carvalho Chehab /* firmware query */
620aa77f6cSMauro Carvalho Chehab #define S2255_VR_FW		0x30
630aa77f6cSMauro Carvalho Chehab /* USB endpoint number for configuring the device */
640aa77f6cSMauro Carvalho Chehab #define S2255_CONFIG_EP         2
650aa77f6cSMauro Carvalho Chehab /* maximum time for DSP to start responding after last FW word loaded(ms) */
660aa77f6cSMauro Carvalho Chehab #define S2255_DSP_BOOTTIME      800
670aa77f6cSMauro Carvalho Chehab /* maximum time to wait for firmware to load (ms) */
680aa77f6cSMauro Carvalho Chehab #define S2255_LOAD_TIMEOUT      (5000 + S2255_DSP_BOOTTIME)
699da62eb0SDean Anderson #define S2255_MIN_BUFS          2
700aa77f6cSMauro Carvalho Chehab #define S2255_SETMODE_TIMEOUT   500
710aa77f6cSMauro Carvalho Chehab #define S2255_VIDSTATUS_TIMEOUT 350
720aa77f6cSMauro Carvalho Chehab #define S2255_MARKER_FRAME	cpu_to_le32(0x2255DA4AL)
730aa77f6cSMauro Carvalho Chehab #define S2255_MARKER_RESPONSE	cpu_to_le32(0x2255ACACL)
740aa77f6cSMauro Carvalho Chehab #define S2255_RESPONSE_SETMODE  cpu_to_le32(0x01)
750aa77f6cSMauro Carvalho Chehab #define S2255_RESPONSE_FW       cpu_to_le32(0x10)
760aa77f6cSMauro Carvalho Chehab #define S2255_RESPONSE_STATUS   cpu_to_le32(0x20)
770aa77f6cSMauro Carvalho Chehab #define S2255_USB_XFER_SIZE	(16 * 1024)
780aa77f6cSMauro Carvalho Chehab #define MAX_CHANNELS		4
790aa77f6cSMauro Carvalho Chehab #define SYS_FRAMES		4
800aa77f6cSMauro Carvalho Chehab /* maximum size is PAL full size plus room for the marker header(s) */
810aa77f6cSMauro Carvalho Chehab #define SYS_FRAMES_MAXSIZE	(720*288*2*2 + 4096)
820aa77f6cSMauro Carvalho Chehab #define DEF_USB_BLOCK		S2255_USB_XFER_SIZE
830aa77f6cSMauro Carvalho Chehab #define LINE_SZ_4CIFS_NTSC	640
840aa77f6cSMauro Carvalho Chehab #define LINE_SZ_2CIFS_NTSC	640
850aa77f6cSMauro Carvalho Chehab #define LINE_SZ_1CIFS_NTSC	320
860aa77f6cSMauro Carvalho Chehab #define LINE_SZ_4CIFS_PAL	704
870aa77f6cSMauro Carvalho Chehab #define LINE_SZ_2CIFS_PAL	704
880aa77f6cSMauro Carvalho Chehab #define LINE_SZ_1CIFS_PAL	352
890aa77f6cSMauro Carvalho Chehab #define NUM_LINES_4CIFS_NTSC	240
900aa77f6cSMauro Carvalho Chehab #define NUM_LINES_2CIFS_NTSC	240
910aa77f6cSMauro Carvalho Chehab #define NUM_LINES_1CIFS_NTSC	240
920aa77f6cSMauro Carvalho Chehab #define NUM_LINES_4CIFS_PAL	288
930aa77f6cSMauro Carvalho Chehab #define NUM_LINES_2CIFS_PAL	288
940aa77f6cSMauro Carvalho Chehab #define NUM_LINES_1CIFS_PAL	288
950aa77f6cSMauro Carvalho Chehab #define LINE_SZ_DEF		640
960aa77f6cSMauro Carvalho Chehab #define NUM_LINES_DEF		240
970aa77f6cSMauro Carvalho Chehab 
980aa77f6cSMauro Carvalho Chehab 
990aa77f6cSMauro Carvalho Chehab /* predefined settings */
1000aa77f6cSMauro Carvalho Chehab #define FORMAT_NTSC	1
1010aa77f6cSMauro Carvalho Chehab #define FORMAT_PAL	2
1020aa77f6cSMauro Carvalho Chehab 
1030aa77f6cSMauro Carvalho Chehab #define SCALE_4CIFS	1	/* 640x480(NTSC) or 704x576(PAL) */
1040aa77f6cSMauro Carvalho Chehab #define SCALE_2CIFS	2	/* 640x240(NTSC) or 704x288(PAL) */
1050aa77f6cSMauro Carvalho Chehab #define SCALE_1CIFS	3	/* 320x240(NTSC) or 352x288(PAL) */
1060aa77f6cSMauro Carvalho Chehab /* SCALE_4CIFSI is the 2 fields interpolated into one */
1070aa77f6cSMauro Carvalho Chehab #define SCALE_4CIFSI	4	/* 640x480(NTSC) or 704x576(PAL) high quality */
1080aa77f6cSMauro Carvalho Chehab 
1090aa77f6cSMauro Carvalho Chehab #define COLOR_YUVPL	1	/* YUV planar */
1100aa77f6cSMauro Carvalho Chehab #define COLOR_YUVPK	2	/* YUV packed */
1110aa77f6cSMauro Carvalho Chehab #define COLOR_Y8	4	/* monochrome */
1120aa77f6cSMauro Carvalho Chehab #define COLOR_JPG       5       /* JPEG */
1130aa77f6cSMauro Carvalho Chehab 
1140aa77f6cSMauro Carvalho Chehab #define MASK_COLOR       0x000000ff
1150aa77f6cSMauro Carvalho Chehab #define MASK_JPG_QUALITY 0x0000ff00
1160aa77f6cSMauro Carvalho Chehab #define MASK_INPUT_TYPE  0x000f0000
1170aa77f6cSMauro Carvalho Chehab /* frame decimation. */
1180aa77f6cSMauro Carvalho Chehab #define FDEC_1		1	/* capture every frame. default */
1190aa77f6cSMauro Carvalho Chehab #define FDEC_2		2	/* capture every 2nd frame */
1200aa77f6cSMauro Carvalho Chehab #define FDEC_3		3	/* capture every 3rd frame */
1210aa77f6cSMauro Carvalho Chehab #define FDEC_5		5	/* capture every 5th frame */
1220aa77f6cSMauro Carvalho Chehab 
1230aa77f6cSMauro Carvalho Chehab /*-------------------------------------------------------
1240aa77f6cSMauro Carvalho Chehab  * Default mode parameters.
1250aa77f6cSMauro Carvalho Chehab  *-------------------------------------------------------*/
1260aa77f6cSMauro Carvalho Chehab #define DEF_SCALE	SCALE_4CIFS
1270aa77f6cSMauro Carvalho Chehab #define DEF_COLOR	COLOR_YUVPL
1280aa77f6cSMauro Carvalho Chehab #define DEF_FDEC	FDEC_1
1290aa77f6cSMauro Carvalho Chehab #define DEF_BRIGHT	0
1300aa77f6cSMauro Carvalho Chehab #define DEF_CONTRAST	0x5c
1310aa77f6cSMauro Carvalho Chehab #define DEF_SATURATION	0x80
1320aa77f6cSMauro Carvalho Chehab #define DEF_HUE		0
1330aa77f6cSMauro Carvalho Chehab 
1340aa77f6cSMauro Carvalho Chehab /* usb config commands */
1350aa77f6cSMauro Carvalho Chehab #define IN_DATA_TOKEN	cpu_to_le32(0x2255c0de)
1360aa77f6cSMauro Carvalho Chehab #define CMD_2255	0xc2255000
1370aa77f6cSMauro Carvalho Chehab #define CMD_SET_MODE	cpu_to_le32((CMD_2255 | 0x10))
1380aa77f6cSMauro Carvalho Chehab #define CMD_START	cpu_to_le32((CMD_2255 | 0x20))
1390aa77f6cSMauro Carvalho Chehab #define CMD_STOP	cpu_to_le32((CMD_2255 | 0x30))
1400aa77f6cSMauro Carvalho Chehab #define CMD_STATUS	cpu_to_le32((CMD_2255 | 0x40))
1410aa77f6cSMauro Carvalho Chehab 
1420aa77f6cSMauro Carvalho Chehab struct s2255_mode {
1430aa77f6cSMauro Carvalho Chehab 	u32 format;	/* input video format (NTSC, PAL) */
1440aa77f6cSMauro Carvalho Chehab 	u32 scale;	/* output video scale */
1450aa77f6cSMauro Carvalho Chehab 	u32 color;	/* output video color format */
1460aa77f6cSMauro Carvalho Chehab 	u32 fdec;	/* frame decimation */
1470aa77f6cSMauro Carvalho Chehab 	u32 bright;	/* brightness */
1480aa77f6cSMauro Carvalho Chehab 	u32 contrast;	/* contrast */
1490aa77f6cSMauro Carvalho Chehab 	u32 saturation;	/* saturation */
1500aa77f6cSMauro Carvalho Chehab 	u32 hue;	/* hue (NTSC only)*/
1510aa77f6cSMauro Carvalho Chehab 	u32 single;	/* capture 1 frame at a time (!=0), continuously (==0)*/
1520aa77f6cSMauro Carvalho Chehab 	u32 usb_block;	/* block size. should be 4096 of DEF_USB_BLOCK */
1530aa77f6cSMauro Carvalho Chehab 	u32 restart;	/* if DSP requires restart */
1540aa77f6cSMauro Carvalho Chehab };
1550aa77f6cSMauro Carvalho Chehab 
1560aa77f6cSMauro Carvalho Chehab 
1570aa77f6cSMauro Carvalho Chehab #define S2255_READ_IDLE		0
1580aa77f6cSMauro Carvalho Chehab #define S2255_READ_FRAME	1
1590aa77f6cSMauro Carvalho Chehab 
1600aa77f6cSMauro Carvalho Chehab /* frame structure */
1610aa77f6cSMauro Carvalho Chehab struct s2255_framei {
1620aa77f6cSMauro Carvalho Chehab 	unsigned long size;
1630aa77f6cSMauro Carvalho Chehab 	unsigned long ulState;	/* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
1640aa77f6cSMauro Carvalho Chehab 	void *lpvbits;		/* image data */
1650aa77f6cSMauro Carvalho Chehab 	unsigned long cur_size;	/* current data copied to it */
1660aa77f6cSMauro Carvalho Chehab };
1670aa77f6cSMauro Carvalho Chehab 
1680aa77f6cSMauro Carvalho Chehab /* image buffer structure */
1690aa77f6cSMauro Carvalho Chehab struct s2255_bufferi {
1700aa77f6cSMauro Carvalho Chehab 	unsigned long dwFrames;			/* number of frames in buffer */
1710aa77f6cSMauro Carvalho Chehab 	struct s2255_framei frame[SYS_FRAMES];	/* array of FRAME structures */
1720aa77f6cSMauro Carvalho Chehab };
1730aa77f6cSMauro Carvalho Chehab 
1740aa77f6cSMauro Carvalho Chehab #define DEF_MODEI_NTSC_CONT	{FORMAT_NTSC, DEF_SCALE, DEF_COLOR,	\
1750aa77f6cSMauro Carvalho Chehab 			DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
1760aa77f6cSMauro Carvalho Chehab 			DEF_HUE, 0, DEF_USB_BLOCK, 0}
1770aa77f6cSMauro Carvalho Chehab 
1780aa77f6cSMauro Carvalho Chehab /* for firmware loading, fw_state */
1790aa77f6cSMauro Carvalho Chehab #define S2255_FW_NOTLOADED	0
1800aa77f6cSMauro Carvalho Chehab #define S2255_FW_LOADED_DSPWAIT	1
1810aa77f6cSMauro Carvalho Chehab #define S2255_FW_SUCCESS	2
1820aa77f6cSMauro Carvalho Chehab #define S2255_FW_FAILED		3
1830aa77f6cSMauro Carvalho Chehab #define S2255_FW_DISCONNECTING  4
1840aa77f6cSMauro Carvalho Chehab #define S2255_FW_MARKER		cpu_to_le32(0x22552f2f)
1850aa77f6cSMauro Carvalho Chehab /* 2255 read states */
1860aa77f6cSMauro Carvalho Chehab #define S2255_READ_IDLE         0
1870aa77f6cSMauro Carvalho Chehab #define S2255_READ_FRAME        1
1880aa77f6cSMauro Carvalho Chehab struct s2255_fw {
1890aa77f6cSMauro Carvalho Chehab 	int		      fw_loaded;
1900aa77f6cSMauro Carvalho Chehab 	int		      fw_size;
1910aa77f6cSMauro Carvalho Chehab 	struct urb	      *fw_urb;
1920aa77f6cSMauro Carvalho Chehab 	atomic_t	      fw_state;
1930aa77f6cSMauro Carvalho Chehab 	void		      *pfw_data;
1940aa77f6cSMauro Carvalho Chehab 	wait_queue_head_t     wait_fw;
1950aa77f6cSMauro Carvalho Chehab 	const struct firmware *fw;
1960aa77f6cSMauro Carvalho Chehab };
1970aa77f6cSMauro Carvalho Chehab 
1980aa77f6cSMauro Carvalho Chehab struct s2255_pipeinfo {
1990aa77f6cSMauro Carvalho Chehab 	u32 max_transfer_size;
2000aa77f6cSMauro Carvalho Chehab 	u32 cur_transfer_size;
2010aa77f6cSMauro Carvalho Chehab 	u8 *transfer_buffer;
2020aa77f6cSMauro Carvalho Chehab 	u32 state;
2030aa77f6cSMauro Carvalho Chehab 	void *stream_urb;
2040aa77f6cSMauro Carvalho Chehab 	void *dev;	/* back pointer to s2255_dev struct*/
2050aa77f6cSMauro Carvalho Chehab 	u32 err_count;
2060aa77f6cSMauro Carvalho Chehab 	u32 idx;
2070aa77f6cSMauro Carvalho Chehab };
2080aa77f6cSMauro Carvalho Chehab 
2090aa77f6cSMauro Carvalho Chehab struct s2255_fmt; /*forward declaration */
2100aa77f6cSMauro Carvalho Chehab struct s2255_dev;
2110aa77f6cSMauro Carvalho Chehab 
2125e950fafSDean Anderson /* 2255 video channel */
2135e950fafSDean Anderson struct s2255_vc {
214f5402007Ssensoray-dev 	struct s2255_dev        *dev;
2150aa77f6cSMauro Carvalho Chehab 	struct video_device	vdev;
216192f1e78SHans Verkuil 	struct v4l2_ctrl_handler hdl;
2177041dec7SHans Verkuil 	struct v4l2_ctrl	*jpegqual_ctrl;
2180aa77f6cSMauro Carvalho Chehab 	int			resources;
219d86c6a8cSDean Anderson 	struct list_head        buf_list;
2200aa77f6cSMauro Carvalho Chehab 	struct s2255_bufferi	buffer;
2210aa77f6cSMauro Carvalho Chehab 	struct s2255_mode	mode;
222469af77aSHans Verkuil 	v4l2_std_id		std;
2230aa77f6cSMauro Carvalho Chehab 	/* jpeg compression */
2247041dec7SHans Verkuil 	unsigned		jpegqual;
2250aa77f6cSMauro Carvalho Chehab 	/* capture parameters (for high quality mode full size) */
2260aa77f6cSMauro Carvalho Chehab 	struct v4l2_captureparm cap_parm;
2270aa77f6cSMauro Carvalho Chehab 	int			cur_frame;
2280aa77f6cSMauro Carvalho Chehab 	int			last_frame;
2290aa77f6cSMauro Carvalho Chehab 	/* allocated image size */
2300aa77f6cSMauro Carvalho Chehab 	unsigned long		req_image_size;
2310aa77f6cSMauro Carvalho Chehab 	/* received packet size */
2320aa77f6cSMauro Carvalho Chehab 	unsigned long		pkt_size;
2330aa77f6cSMauro Carvalho Chehab 	int			bad_payload;
2340aa77f6cSMauro Carvalho Chehab 	unsigned long		frame_count;
2350aa77f6cSMauro Carvalho Chehab 	/* if JPEG image */
2360aa77f6cSMauro Carvalho Chehab 	int                     jpg_size;
2370aa77f6cSMauro Carvalho Chehab 	/* if channel configured to default state */
2380aa77f6cSMauro Carvalho Chehab 	int                     configured;
2390aa77f6cSMauro Carvalho Chehab 	wait_queue_head_t       wait_setmode;
2400aa77f6cSMauro Carvalho Chehab 	int                     setmode_ready;
2410aa77f6cSMauro Carvalho Chehab 	/* video status items */
2420aa77f6cSMauro Carvalho Chehab 	int                     vidstatus;
2430aa77f6cSMauro Carvalho Chehab 	wait_queue_head_t       wait_vidstatus;
2440aa77f6cSMauro Carvalho Chehab 	int                     vidstatus_ready;
2450aa77f6cSMauro Carvalho Chehab 	unsigned int		width;
2460aa77f6cSMauro Carvalho Chehab 	unsigned int		height;
247340a30c5Ssensoray-dev 	enum v4l2_field         field;
2480aa77f6cSMauro Carvalho Chehab 	const struct s2255_fmt	*fmt;
2490aa77f6cSMauro Carvalho Chehab 	int idx; /* channel number on device, 0-3 */
250340a30c5Ssensoray-dev 	struct vb2_queue vb_vidq;
251340a30c5Ssensoray-dev 	struct mutex vb_lock; /* streaming lock */
252340a30c5Ssensoray-dev 	spinlock_t qlock;
2530aa77f6cSMauro Carvalho Chehab };
2540aa77f6cSMauro Carvalho Chehab 
2550aa77f6cSMauro Carvalho Chehab 
2560aa77f6cSMauro Carvalho Chehab struct s2255_dev {
2575e950fafSDean Anderson 	struct s2255_vc         vc[MAX_CHANNELS];
2580aa77f6cSMauro Carvalho Chehab 	struct v4l2_device      v4l2_dev;
2590aa77f6cSMauro Carvalho Chehab 	atomic_t                num_channels;
2600aa77f6cSMauro Carvalho Chehab 	int			frames;
2610aa77f6cSMauro Carvalho Chehab 	struct mutex		lock;	/* channels[].vdev.lock */
26247d8c881SDean Anderson 	struct mutex		cmdlock; /* protects cmdbuf */
2630aa77f6cSMauro Carvalho Chehab 	struct usb_device	*udev;
2640aa77f6cSMauro Carvalho Chehab 	struct usb_interface	*interface;
2650aa77f6cSMauro Carvalho Chehab 	u8			read_endpoint;
2660aa77f6cSMauro Carvalho Chehab 	struct timer_list	timer;
2670aa77f6cSMauro Carvalho Chehab 	struct s2255_fw	*fw_data;
2680aa77f6cSMauro Carvalho Chehab 	struct s2255_pipeinfo	pipe;
2690aa77f6cSMauro Carvalho Chehab 	u32			cc;	/* current channel */
2700aa77f6cSMauro Carvalho Chehab 	int			frame_ready;
2710aa77f6cSMauro Carvalho Chehab 	int                     chn_ready;
2720aa77f6cSMauro Carvalho Chehab 	/* dsp firmware version (f2255usb.bin) */
2730aa77f6cSMauro Carvalho Chehab 	int                     dsp_fw_ver;
2740aa77f6cSMauro Carvalho Chehab 	u16                     pid; /* product id */
27547d8c881SDean Anderson #define S2255_CMDBUF_SIZE 512
27647d8c881SDean Anderson 	__le32                  *cmdbuf;
2770aa77f6cSMauro Carvalho Chehab };
2780aa77f6cSMauro Carvalho Chehab 
2790aa77f6cSMauro Carvalho Chehab static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
2800aa77f6cSMauro Carvalho Chehab {
2810aa77f6cSMauro Carvalho Chehab 	return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
2820aa77f6cSMauro Carvalho Chehab }
2830aa77f6cSMauro Carvalho Chehab 
2840aa77f6cSMauro Carvalho Chehab struct s2255_fmt {
2850aa77f6cSMauro Carvalho Chehab 	char *name;
2860aa77f6cSMauro Carvalho Chehab 	u32 fourcc;
2870aa77f6cSMauro Carvalho Chehab 	int depth;
2880aa77f6cSMauro Carvalho Chehab };
2890aa77f6cSMauro Carvalho Chehab 
2900aa77f6cSMauro Carvalho Chehab /* buffer for one video frame */
2910aa77f6cSMauro Carvalho Chehab struct s2255_buffer {
2920aa77f6cSMauro Carvalho Chehab 	/* common v4l buffer stuff -- must be first */
2932d700715SJunghak Sung 	struct vb2_v4l2_buffer vb;
294340a30c5Ssensoray-dev 	struct list_head list;
2950aa77f6cSMauro Carvalho Chehab };
2960aa77f6cSMauro Carvalho Chehab 
2970aa77f6cSMauro Carvalho Chehab 
2980aa77f6cSMauro Carvalho Chehab /* current cypress EEPROM firmware version */
2990aa77f6cSMauro Carvalho Chehab #define S2255_CUR_USB_FWVER	((3 << 8) | 12)
3000aa77f6cSMauro Carvalho Chehab /* current DSP FW version */
3010aa77f6cSMauro Carvalho Chehab #define S2255_CUR_DSP_FWVER     10104
3020aa77f6cSMauro Carvalho Chehab /* Need DSP version 5+ for video status feature */
3030aa77f6cSMauro Carvalho Chehab #define S2255_MIN_DSP_STATUS      5
3040aa77f6cSMauro Carvalho Chehab #define S2255_MIN_DSP_COLORFILTER 8
305469af77aSHans Verkuil #define S2255_NORMS		(V4L2_STD_ALL)
3060aa77f6cSMauro Carvalho Chehab 
3070aa77f6cSMauro Carvalho Chehab /* private V4L2 controls */
3080aa77f6cSMauro Carvalho Chehab 
3090aa77f6cSMauro Carvalho Chehab /*
3100aa77f6cSMauro Carvalho Chehab  * The following chart displays how COLORFILTER should be set
3110aa77f6cSMauro Carvalho Chehab  *  =========================================================
3120aa77f6cSMauro Carvalho Chehab  *  =     fourcc              =     COLORFILTER             =
3130aa77f6cSMauro Carvalho Chehab  *  =                         ===============================
3140aa77f6cSMauro Carvalho Chehab  *  =                         =   0             =    1      =
3150aa77f6cSMauro Carvalho Chehab  *  =========================================================
3160aa77f6cSMauro Carvalho Chehab  *  =  V4L2_PIX_FMT_GREY(Y8)  = monochrome from = monochrome=
3170aa77f6cSMauro Carvalho Chehab  *  =                         = s-video or      = composite =
3180aa77f6cSMauro Carvalho Chehab  *  =                         = B/W camera      = input     =
3190aa77f6cSMauro Carvalho Chehab  *  =========================================================
3200aa77f6cSMauro Carvalho Chehab  *  =    other                = color, svideo   = color,    =
3210aa77f6cSMauro Carvalho Chehab  *  =                         =                 = composite =
3220aa77f6cSMauro Carvalho Chehab  *  =========================================================
3230aa77f6cSMauro Carvalho Chehab  *
3240aa77f6cSMauro Carvalho Chehab  * Notes:
3250aa77f6cSMauro Carvalho Chehab  *   channels 0-3 on 2255 are composite
3260aa77f6cSMauro Carvalho Chehab  *   channels 0-1 on 2257 are composite, 2-3 are s-video
3270aa77f6cSMauro Carvalho Chehab  * If COLORFILTER is 0 with a composite color camera connected,
3280aa77f6cSMauro Carvalho Chehab  * the output will appear monochrome but hatching
3290aa77f6cSMauro Carvalho Chehab  * will occur.
3300aa77f6cSMauro Carvalho Chehab  * COLORFILTER is different from "color killer" and "color effects"
3310aa77f6cSMauro Carvalho Chehab  * for reasons above.
3320aa77f6cSMauro Carvalho Chehab  */
3330aa77f6cSMauro Carvalho Chehab #define S2255_V4L2_YC_ON  1
3340aa77f6cSMauro Carvalho Chehab #define S2255_V4L2_YC_OFF 0
335192f1e78SHans Verkuil #define V4L2_CID_S2255_COLORFILTER (V4L2_CID_USER_S2255_BASE + 0)
3360aa77f6cSMauro Carvalho Chehab 
3370aa77f6cSMauro Carvalho Chehab /* frame prefix size (sent once every frame) */
3380aa77f6cSMauro Carvalho Chehab #define PREFIX_SIZE		512
3390aa77f6cSMauro Carvalho Chehab 
3400aa77f6cSMauro Carvalho Chehab /* Channels on box are in reverse order */
3410aa77f6cSMauro Carvalho Chehab static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
3420aa77f6cSMauro Carvalho Chehab 
3430aa77f6cSMauro Carvalho Chehab static int debug;
3440aa77f6cSMauro Carvalho Chehab 
3450aa77f6cSMauro Carvalho Chehab static int s2255_start_readpipe(struct s2255_dev *dev);
3460aa77f6cSMauro Carvalho Chehab static void s2255_stop_readpipe(struct s2255_dev *dev);
3475e950fafSDean Anderson static int s2255_start_acquire(struct s2255_vc *vc);
3485e950fafSDean Anderson static int s2255_stop_acquire(struct s2255_vc *vc);
3495e950fafSDean Anderson static void s2255_fillbuff(struct s2255_vc *vc, struct s2255_buffer *buf,
3500aa77f6cSMauro Carvalho Chehab 			   int jpgsize);
3515e950fafSDean Anderson static int s2255_set_mode(struct s2255_vc *vc, struct s2255_mode *mode);
3520aa77f6cSMauro Carvalho Chehab static int s2255_board_shutdown(struct s2255_dev *dev);
3530aa77f6cSMauro Carvalho Chehab static void s2255_fwload_start(struct s2255_dev *dev, int reset);
3540aa77f6cSMauro Carvalho Chehab static void s2255_destroy(struct s2255_dev *dev);
3550aa77f6cSMauro Carvalho Chehab static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
3560aa77f6cSMauro Carvalho Chehab 			     u16 index, u16 value, void *buf,
3570aa77f6cSMauro Carvalho Chehab 			     s32 buf_len, int bOut);
3580aa77f6cSMauro Carvalho Chehab 
3590aa77f6cSMauro Carvalho Chehab /* dev_err macro with driver name */
3600aa77f6cSMauro Carvalho Chehab #define S2255_DRIVER_NAME "s2255"
3610aa77f6cSMauro Carvalho Chehab #define s2255_dev_err(dev, fmt, arg...)					\
3620aa77f6cSMauro Carvalho Chehab 		dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
3630aa77f6cSMauro Carvalho Chehab 
364f5402007Ssensoray-dev #define dprintk(dev, level, fmt, arg...) \
365f5402007Ssensoray-dev 	v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
3660aa77f6cSMauro Carvalho Chehab 
3670aa77f6cSMauro Carvalho Chehab static struct usb_driver s2255_driver;
3680aa77f6cSMauro Carvalho Chehab 
3690aa77f6cSMauro Carvalho Chehab /* start video number */
3700aa77f6cSMauro Carvalho Chehab static int video_nr = -1;	/* /dev/videoN, -1 for autodetect */
3710aa77f6cSMauro Carvalho Chehab 
3720aa77f6cSMauro Carvalho Chehab /* Enable jpeg capture. */
3730aa77f6cSMauro Carvalho Chehab static int jpeg_enable = 1;
3740aa77f6cSMauro Carvalho Chehab 
3750aa77f6cSMauro Carvalho Chehab module_param(debug, int, 0644);
3760aa77f6cSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
3770aa77f6cSMauro Carvalho Chehab module_param(video_nr, int, 0644);
3780aa77f6cSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
3790aa77f6cSMauro Carvalho Chehab module_param(jpeg_enable, int, 0644);
3800aa77f6cSMauro Carvalho Chehab MODULE_PARM_DESC(jpeg_enable, "Jpeg enable(1-on 0-off) default 1");
3810aa77f6cSMauro Carvalho Chehab 
3820aa77f6cSMauro Carvalho Chehab /* USB device table */
3830aa77f6cSMauro Carvalho Chehab #define USB_SENSORAY_VID	0x1943
384*7fb2e072SArvind Yadav static const struct usb_device_id s2255_table[] = {
3850aa77f6cSMauro Carvalho Chehab 	{USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
3860aa77f6cSMauro Carvalho Chehab 	{USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
3870aa77f6cSMauro Carvalho Chehab 	{ }			/* Terminating entry */
3880aa77f6cSMauro Carvalho Chehab };
3890aa77f6cSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, s2255_table);
3900aa77f6cSMauro Carvalho Chehab 
3910aa77f6cSMauro Carvalho Chehab #define BUFFER_TIMEOUT msecs_to_jiffies(400)
3920aa77f6cSMauro Carvalho Chehab 
3930aa77f6cSMauro Carvalho Chehab /* image formats.  */
3940aa77f6cSMauro Carvalho Chehab /* JPEG formats must be defined last to support jpeg_enable parameter */
3950aa77f6cSMauro Carvalho Chehab static const struct s2255_fmt formats[] = {
3960aa77f6cSMauro Carvalho Chehab 	{
3970aa77f6cSMauro Carvalho Chehab 		.name = "4:2:2, packed, YUYV",
3980aa77f6cSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_YUYV,
3990aa77f6cSMauro Carvalho Chehab 		.depth = 16
4000aa77f6cSMauro Carvalho Chehab 
4010aa77f6cSMauro Carvalho Chehab 	}, {
4020aa77f6cSMauro Carvalho Chehab 		.name = "4:2:2, packed, UYVY",
4030aa77f6cSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_UYVY,
4040aa77f6cSMauro Carvalho Chehab 		.depth = 16
4050aa77f6cSMauro Carvalho Chehab 	}, {
4065c632b22SHans Verkuil 		.name = "4:2:2, planar, YUV422P",
4075c632b22SHans Verkuil 		.fourcc = V4L2_PIX_FMT_YUV422P,
4085c632b22SHans Verkuil 		.depth = 16
4095c632b22SHans Verkuil 
4105c632b22SHans Verkuil 	}, {
4110aa77f6cSMauro Carvalho Chehab 		.name = "8bpp GREY",
4120aa77f6cSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_GREY,
4130aa77f6cSMauro Carvalho Chehab 		.depth = 8
4140aa77f6cSMauro Carvalho Chehab 	}, {
4150aa77f6cSMauro Carvalho Chehab 		.name = "JPG",
4160aa77f6cSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_JPEG,
4170aa77f6cSMauro Carvalho Chehab 		.depth = 24
4180aa77f6cSMauro Carvalho Chehab 	}, {
4190aa77f6cSMauro Carvalho Chehab 		.name = "MJPG",
4200aa77f6cSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_MJPEG,
4210aa77f6cSMauro Carvalho Chehab 		.depth = 24
4220aa77f6cSMauro Carvalho Chehab 	}
4230aa77f6cSMauro Carvalho Chehab };
4240aa77f6cSMauro Carvalho Chehab 
4255e950fafSDean Anderson static int norm_maxw(struct s2255_vc *vc)
4260aa77f6cSMauro Carvalho Chehab {
4275e950fafSDean Anderson 	return (vc->std & V4L2_STD_525_60) ?
4280aa77f6cSMauro Carvalho Chehab 	    LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
4290aa77f6cSMauro Carvalho Chehab }
4300aa77f6cSMauro Carvalho Chehab 
4315e950fafSDean Anderson static int norm_maxh(struct s2255_vc *vc)
4320aa77f6cSMauro Carvalho Chehab {
4335e950fafSDean Anderson 	return (vc->std & V4L2_STD_525_60) ?
4340aa77f6cSMauro Carvalho Chehab 	    (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
4350aa77f6cSMauro Carvalho Chehab }
4360aa77f6cSMauro Carvalho Chehab 
4375e950fafSDean Anderson static int norm_minw(struct s2255_vc *vc)
4380aa77f6cSMauro Carvalho Chehab {
4395e950fafSDean Anderson 	return (vc->std & V4L2_STD_525_60) ?
4400aa77f6cSMauro Carvalho Chehab 	    LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
4410aa77f6cSMauro Carvalho Chehab }
4420aa77f6cSMauro Carvalho Chehab 
4435e950fafSDean Anderson static int norm_minh(struct s2255_vc *vc)
4440aa77f6cSMauro Carvalho Chehab {
4455e950fafSDean Anderson 	return (vc->std & V4L2_STD_525_60) ?
4460aa77f6cSMauro Carvalho Chehab 	    (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
4470aa77f6cSMauro Carvalho Chehab }
4480aa77f6cSMauro Carvalho Chehab 
4490aa77f6cSMauro Carvalho Chehab 
4500aa77f6cSMauro Carvalho Chehab /*
4510aa77f6cSMauro Carvalho Chehab  * TODO: fixme: move YUV reordering to hardware
4520aa77f6cSMauro Carvalho Chehab  * converts 2255 planar format to yuyv or uyvy
4530aa77f6cSMauro Carvalho Chehab  */
4540aa77f6cSMauro Carvalho Chehab static void planar422p_to_yuv_packed(const unsigned char *in,
4550aa77f6cSMauro Carvalho Chehab 				     unsigned char *out,
4560aa77f6cSMauro Carvalho Chehab 				     int width, int height,
4570aa77f6cSMauro Carvalho Chehab 				     int fmt)
4580aa77f6cSMauro Carvalho Chehab {
4590aa77f6cSMauro Carvalho Chehab 	unsigned char *pY;
4600aa77f6cSMauro Carvalho Chehab 	unsigned char *pCb;
4610aa77f6cSMauro Carvalho Chehab 	unsigned char *pCr;
4620aa77f6cSMauro Carvalho Chehab 	unsigned long size = height * width;
4630aa77f6cSMauro Carvalho Chehab 	unsigned int i;
4640aa77f6cSMauro Carvalho Chehab 	pY = (unsigned char *)in;
4650aa77f6cSMauro Carvalho Chehab 	pCr = (unsigned char *)in + height * width;
4660aa77f6cSMauro Carvalho Chehab 	pCb = (unsigned char *)in + height * width + (height * width / 2);
4670aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < size * 2; i += 4) {
4680aa77f6cSMauro Carvalho Chehab 		out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
4690aa77f6cSMauro Carvalho Chehab 		out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
4700aa77f6cSMauro Carvalho Chehab 		out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
4710aa77f6cSMauro Carvalho Chehab 		out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
4720aa77f6cSMauro Carvalho Chehab 	}
4730aa77f6cSMauro Carvalho Chehab 	return;
4740aa77f6cSMauro Carvalho Chehab }
4750aa77f6cSMauro Carvalho Chehab 
4760aa77f6cSMauro Carvalho Chehab static void s2255_reset_dsppower(struct s2255_dev *dev)
4770aa77f6cSMauro Carvalho Chehab {
4780aa77f6cSMauro Carvalho Chehab 	s2255_vendor_req(dev, 0x40, 0x0000, 0x0001, NULL, 0, 1);
479f5402007Ssensoray-dev 	msleep(20);
4800aa77f6cSMauro Carvalho Chehab 	s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
4810aa77f6cSMauro Carvalho Chehab 	msleep(600);
4820aa77f6cSMauro Carvalho Chehab 	s2255_vendor_req(dev, 0x10, 0x0000, 0x0000, NULL, 0, 1);
4830aa77f6cSMauro Carvalho Chehab 	return;
4840aa77f6cSMauro Carvalho Chehab }
4850aa77f6cSMauro Carvalho Chehab 
4860aa77f6cSMauro Carvalho Chehab /* kickstarts the firmware loading. from probe
4870aa77f6cSMauro Carvalho Chehab  */
4880aa77f6cSMauro Carvalho Chehab static void s2255_timer(unsigned long user_data)
4890aa77f6cSMauro Carvalho Chehab {
4900aa77f6cSMauro Carvalho Chehab 	struct s2255_fw *data = (struct s2255_fw *)user_data;
4910aa77f6cSMauro Carvalho Chehab 	if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
492f5402007Ssensoray-dev 		pr_err("s2255: can't submit urb\n");
4930aa77f6cSMauro Carvalho Chehab 		atomic_set(&data->fw_state, S2255_FW_FAILED);
4940aa77f6cSMauro Carvalho Chehab 		/* wake up anything waiting for the firmware */
4950aa77f6cSMauro Carvalho Chehab 		wake_up(&data->wait_fw);
4960aa77f6cSMauro Carvalho Chehab 		return;
4970aa77f6cSMauro Carvalho Chehab 	}
4980aa77f6cSMauro Carvalho Chehab }
4990aa77f6cSMauro Carvalho Chehab 
5000aa77f6cSMauro Carvalho Chehab 
5010aa77f6cSMauro Carvalho Chehab /* this loads the firmware asynchronously.
5020b84caabSHans Verkuil    Originally this was done synchronously in probe.
5030aa77f6cSMauro Carvalho Chehab    But it is better to load it asynchronously here than block
5040aa77f6cSMauro Carvalho Chehab    inside the probe function. Blocking inside probe affects boot time.
5050aa77f6cSMauro Carvalho Chehab    FW loading is triggered by the timer in the probe function
5060aa77f6cSMauro Carvalho Chehab */
5070aa77f6cSMauro Carvalho Chehab static void s2255_fwchunk_complete(struct urb *urb)
5080aa77f6cSMauro Carvalho Chehab {
5090aa77f6cSMauro Carvalho Chehab 	struct s2255_fw *data = urb->context;
5100aa77f6cSMauro Carvalho Chehab 	struct usb_device *udev = urb->dev;
5110aa77f6cSMauro Carvalho Chehab 	int len;
5120aa77f6cSMauro Carvalho Chehab 	if (urb->status) {
5130aa77f6cSMauro Carvalho Chehab 		dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
5140aa77f6cSMauro Carvalho Chehab 		atomic_set(&data->fw_state, S2255_FW_FAILED);
5150aa77f6cSMauro Carvalho Chehab 		/* wake up anything waiting for the firmware */
5160aa77f6cSMauro Carvalho Chehab 		wake_up(&data->wait_fw);
5170aa77f6cSMauro Carvalho Chehab 		return;
5180aa77f6cSMauro Carvalho Chehab 	}
5190aa77f6cSMauro Carvalho Chehab 	if (data->fw_urb == NULL) {
5200aa77f6cSMauro Carvalho Chehab 		s2255_dev_err(&udev->dev, "disconnected\n");
5210aa77f6cSMauro Carvalho Chehab 		atomic_set(&data->fw_state, S2255_FW_FAILED);
5220aa77f6cSMauro Carvalho Chehab 		/* wake up anything waiting for the firmware */
5230aa77f6cSMauro Carvalho Chehab 		wake_up(&data->wait_fw);
5240aa77f6cSMauro Carvalho Chehab 		return;
5250aa77f6cSMauro Carvalho Chehab 	}
5260aa77f6cSMauro Carvalho Chehab #define CHUNK_SIZE 512
5270aa77f6cSMauro Carvalho Chehab 	/* all USB transfers must be done with continuous kernel memory.
5280aa77f6cSMauro Carvalho Chehab 	   can't allocate more than 128k in current linux kernel, so
5290aa77f6cSMauro Carvalho Chehab 	   upload the firmware in chunks
5300aa77f6cSMauro Carvalho Chehab 	 */
5310aa77f6cSMauro Carvalho Chehab 	if (data->fw_loaded < data->fw_size) {
5320aa77f6cSMauro Carvalho Chehab 		len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
5330aa77f6cSMauro Carvalho Chehab 		    data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
5340aa77f6cSMauro Carvalho Chehab 
5350aa77f6cSMauro Carvalho Chehab 		if (len < CHUNK_SIZE)
5360aa77f6cSMauro Carvalho Chehab 			memset(data->pfw_data, 0, CHUNK_SIZE);
5370aa77f6cSMauro Carvalho Chehab 
5380aa77f6cSMauro Carvalho Chehab 		memcpy(data->pfw_data,
5390aa77f6cSMauro Carvalho Chehab 		       (char *) data->fw->data + data->fw_loaded, len);
5400aa77f6cSMauro Carvalho Chehab 
5410aa77f6cSMauro Carvalho Chehab 		usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
5420aa77f6cSMauro Carvalho Chehab 				  data->pfw_data, CHUNK_SIZE,
5430aa77f6cSMauro Carvalho Chehab 				  s2255_fwchunk_complete, data);
5440aa77f6cSMauro Carvalho Chehab 		if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
5450aa77f6cSMauro Carvalho Chehab 			dev_err(&udev->dev, "failed submit URB\n");
5460aa77f6cSMauro Carvalho Chehab 			atomic_set(&data->fw_state, S2255_FW_FAILED);
5470aa77f6cSMauro Carvalho Chehab 			/* wake up anything waiting for the firmware */
5480aa77f6cSMauro Carvalho Chehab 			wake_up(&data->wait_fw);
5490aa77f6cSMauro Carvalho Chehab 			return;
5500aa77f6cSMauro Carvalho Chehab 		}
5510aa77f6cSMauro Carvalho Chehab 		data->fw_loaded += len;
552f5402007Ssensoray-dev 	} else
5530aa77f6cSMauro Carvalho Chehab 		atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
5540aa77f6cSMauro Carvalho Chehab 	return;
5550aa77f6cSMauro Carvalho Chehab 
5560aa77f6cSMauro Carvalho Chehab }
5570aa77f6cSMauro Carvalho Chehab 
5589694fbecSsensoray-dev static void s2255_got_frame(struct s2255_vc *vc, int jpgsize)
5590aa77f6cSMauro Carvalho Chehab {
5600aa77f6cSMauro Carvalho Chehab 	struct s2255_buffer *buf;
5615e950fafSDean Anderson 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
5620aa77f6cSMauro Carvalho Chehab 	unsigned long flags = 0;
5639694fbecSsensoray-dev 
564340a30c5Ssensoray-dev 	spin_lock_irqsave(&vc->qlock, flags);
5655e950fafSDean Anderson 	if (list_empty(&vc->buf_list)) {
566f5402007Ssensoray-dev 		dprintk(dev, 1, "No active queue to serve\n");
5679694fbecSsensoray-dev 		spin_unlock_irqrestore(&vc->qlock, flags);
5689694fbecSsensoray-dev 		return;
5690aa77f6cSMauro Carvalho Chehab 	}
5705e950fafSDean Anderson 	buf = list_entry(vc->buf_list.next,
571340a30c5Ssensoray-dev 			 struct s2255_buffer, list);
572340a30c5Ssensoray-dev 	list_del(&buf->list);
573d6dd645eSJunghak Sung 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
5742d700715SJunghak Sung 	buf->vb.field = vc->field;
5752d700715SJunghak Sung 	buf->vb.sequence = vc->frame_count;
576340a30c5Ssensoray-dev 	spin_unlock_irqrestore(&vc->qlock, flags);
5779694fbecSsensoray-dev 
5789694fbecSsensoray-dev 	s2255_fillbuff(vc, buf, jpgsize);
5799694fbecSsensoray-dev 	/* tell v4l buffer was filled */
5802d700715SJunghak Sung 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
5819694fbecSsensoray-dev 	dprintk(dev, 2, "%s: [buf] [%p]\n", __func__, buf);
5820aa77f6cSMauro Carvalho Chehab }
5830aa77f6cSMauro Carvalho Chehab 
5840aa77f6cSMauro Carvalho Chehab static const struct s2255_fmt *format_by_fourcc(int fourcc)
5850aa77f6cSMauro Carvalho Chehab {
5860aa77f6cSMauro Carvalho Chehab 	unsigned int i;
5870aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++) {
5880aa77f6cSMauro Carvalho Chehab 		if (-1 == formats[i].fourcc)
5890aa77f6cSMauro Carvalho Chehab 			continue;
5900aa77f6cSMauro Carvalho Chehab 		if (!jpeg_enable && ((formats[i].fourcc == V4L2_PIX_FMT_JPEG) ||
5910aa77f6cSMauro Carvalho Chehab 				     (formats[i].fourcc == V4L2_PIX_FMT_MJPEG)))
5920aa77f6cSMauro Carvalho Chehab 			continue;
5930aa77f6cSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
5940aa77f6cSMauro Carvalho Chehab 			return formats + i;
5950aa77f6cSMauro Carvalho Chehab 	}
5960aa77f6cSMauro Carvalho Chehab 	return NULL;
5970aa77f6cSMauro Carvalho Chehab }
5980aa77f6cSMauro Carvalho Chehab 
5990aa77f6cSMauro Carvalho Chehab /* video buffer vmalloc implementation based partly on VIVI driver which is
6000aa77f6cSMauro Carvalho Chehab  *          Copyright (c) 2006 by
6010aa77f6cSMauro Carvalho Chehab  *                  Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
6020aa77f6cSMauro Carvalho Chehab  *                  Ted Walther <ted--a.t--enumera.com>
6030aa77f6cSMauro Carvalho Chehab  *                  John Sokol <sokol--a.t--videotechnology.com>
6040aa77f6cSMauro Carvalho Chehab  *                  http://v4l.videotechnology.com/
6050aa77f6cSMauro Carvalho Chehab  *
6060aa77f6cSMauro Carvalho Chehab  */
6075e950fafSDean Anderson static void s2255_fillbuff(struct s2255_vc *vc,
6080aa77f6cSMauro Carvalho Chehab 			   struct s2255_buffer *buf, int jpgsize)
6090aa77f6cSMauro Carvalho Chehab {
6100aa77f6cSMauro Carvalho Chehab 	int pos = 0;
6110aa77f6cSMauro Carvalho Chehab 	const char *tmpbuf;
6122d700715SJunghak Sung 	char *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
6130aa77f6cSMauro Carvalho Chehab 	unsigned long last_frame;
6145e950fafSDean Anderson 	struct s2255_dev *dev = vc->dev;
6150aa77f6cSMauro Carvalho Chehab 
6160aa77f6cSMauro Carvalho Chehab 	if (!vbuf)
6170aa77f6cSMauro Carvalho Chehab 		return;
6185e950fafSDean Anderson 	last_frame = vc->last_frame;
6190aa77f6cSMauro Carvalho Chehab 	if (last_frame != -1) {
6200aa77f6cSMauro Carvalho Chehab 		tmpbuf =
6215e950fafSDean Anderson 		    (const char *)vc->buffer.frame[last_frame].lpvbits;
6228bf405a0SDean Anderson 		switch (vc->fmt->fourcc) {
6230aa77f6cSMauro Carvalho Chehab 		case V4L2_PIX_FMT_YUYV:
6240aa77f6cSMauro Carvalho Chehab 		case V4L2_PIX_FMT_UYVY:
6250aa77f6cSMauro Carvalho Chehab 			planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
626340a30c5Ssensoray-dev 						 vbuf, vc->width,
627340a30c5Ssensoray-dev 						 vc->height,
6288bf405a0SDean Anderson 						 vc->fmt->fourcc);
6290aa77f6cSMauro Carvalho Chehab 			break;
6300aa77f6cSMauro Carvalho Chehab 		case V4L2_PIX_FMT_GREY:
631340a30c5Ssensoray-dev 			memcpy(vbuf, tmpbuf, vc->width * vc->height);
6320aa77f6cSMauro Carvalho Chehab 			break;
6330aa77f6cSMauro Carvalho Chehab 		case V4L2_PIX_FMT_JPEG:
6340aa77f6cSMauro Carvalho Chehab 		case V4L2_PIX_FMT_MJPEG:
6352d700715SJunghak Sung 			vb2_set_plane_payload(&buf->vb.vb2_buf, 0, jpgsize);
636340a30c5Ssensoray-dev 			memcpy(vbuf, tmpbuf, jpgsize);
6370aa77f6cSMauro Carvalho Chehab 			break;
6380aa77f6cSMauro Carvalho Chehab 		case V4L2_PIX_FMT_YUV422P:
6390aa77f6cSMauro Carvalho Chehab 			memcpy(vbuf, tmpbuf,
640340a30c5Ssensoray-dev 			       vc->width * vc->height * 2);
6410aa77f6cSMauro Carvalho Chehab 			break;
6420aa77f6cSMauro Carvalho Chehab 		default:
643f5402007Ssensoray-dev 			pr_info("s2255: unknown format?\n");
6440aa77f6cSMauro Carvalho Chehab 		}
6455e950fafSDean Anderson 		vc->last_frame = -1;
6460aa77f6cSMauro Carvalho Chehab 	} else {
647f5402007Ssensoray-dev 		pr_err("s2255: =======no frame\n");
6480aa77f6cSMauro Carvalho Chehab 		return;
6490aa77f6cSMauro Carvalho Chehab 	}
650f5402007Ssensoray-dev 	dprintk(dev, 2, "s2255fill at : Buffer 0x%08lx size= %d\n",
6510aa77f6cSMauro Carvalho Chehab 		(unsigned long)vbuf, pos);
6520aa77f6cSMauro Carvalho Chehab }
6530aa77f6cSMauro Carvalho Chehab 
6540aa77f6cSMauro Carvalho Chehab 
6550aa77f6cSMauro Carvalho Chehab /* ------------------------------------------------------------------
6560aa77f6cSMauro Carvalho Chehab    Videobuf operations
6570aa77f6cSMauro Carvalho Chehab    ------------------------------------------------------------------*/
6580aa77f6cSMauro Carvalho Chehab 
659df9ecb0cSHans Verkuil static int queue_setup(struct vb2_queue *vq,
660340a30c5Ssensoray-dev 		       unsigned int *nbuffers, unsigned int *nplanes,
66136c0f8b3SHans Verkuil 		       unsigned int sizes[], struct device *alloc_devs[])
6620aa77f6cSMauro Carvalho Chehab {
663340a30c5Ssensoray-dev 	struct s2255_vc *vc = vb2_get_drv_priv(vq);
6649da62eb0SDean Anderson 	if (*nbuffers < S2255_MIN_BUFS)
6659da62eb0SDean Anderson 		*nbuffers = S2255_MIN_BUFS;
666340a30c5Ssensoray-dev 	*nplanes = 1;
667340a30c5Ssensoray-dev 	sizes[0] = vc->width * vc->height * (vc->fmt->depth >> 3);
6680aa77f6cSMauro Carvalho Chehab 	return 0;
6690aa77f6cSMauro Carvalho Chehab }
6700aa77f6cSMauro Carvalho Chehab 
671340a30c5Ssensoray-dev static int buffer_prepare(struct vb2_buffer *vb)
6720aa77f6cSMauro Carvalho Chehab {
673340a30c5Ssensoray-dev 	struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue);
6742d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
6752d700715SJunghak Sung 	struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb);
6765e950fafSDean Anderson 	int w = vc->width;
6775e950fafSDean Anderson 	int h = vc->height;
678340a30c5Ssensoray-dev 	unsigned long size;
679340a30c5Ssensoray-dev 
680340a30c5Ssensoray-dev 	dprintk(vc->dev, 4, "%s\n", __func__);
6815e950fafSDean Anderson 	if (vc->fmt == NULL)
6820aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
6830aa77f6cSMauro Carvalho Chehab 
6845e950fafSDean Anderson 	if ((w < norm_minw(vc)) ||
6855e950fafSDean Anderson 	    (w > norm_maxw(vc)) ||
6865e950fafSDean Anderson 	    (h < norm_minh(vc)) ||
6875e950fafSDean Anderson 	    (h > norm_maxh(vc))) {
68892cde477SDean Anderson 		dprintk(vc->dev, 4, "invalid buffer prepare\n");
6890aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
6900aa77f6cSMauro Carvalho Chehab 	}
691340a30c5Ssensoray-dev 	size = w * h * (vc->fmt->depth >> 3);
692340a30c5Ssensoray-dev 	if (vb2_plane_size(vb, 0) < size) {
69392cde477SDean Anderson 		dprintk(vc->dev, 4, "invalid buffer prepare\n");
6940aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
6950aa77f6cSMauro Carvalho Chehab 	}
6960aa77f6cSMauro Carvalho Chehab 
6972d700715SJunghak Sung 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
6980aa77f6cSMauro Carvalho Chehab 	return 0;
6990aa77f6cSMauro Carvalho Chehab }
7000aa77f6cSMauro Carvalho Chehab 
701340a30c5Ssensoray-dev static void buffer_queue(struct vb2_buffer *vb)
7020aa77f6cSMauro Carvalho Chehab {
7032d700715SJunghak Sung 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
7042d700715SJunghak Sung 	struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb);
705340a30c5Ssensoray-dev 	struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue);
706340a30c5Ssensoray-dev 	unsigned long flags = 0;
70792cde477SDean Anderson 	dprintk(vc->dev, 1, "%s\n", __func__);
708340a30c5Ssensoray-dev 	spin_lock_irqsave(&vc->qlock, flags);
709340a30c5Ssensoray-dev 	list_add_tail(&buf->list, &vc->buf_list);
710340a30c5Ssensoray-dev 	spin_unlock_irqrestore(&vc->qlock, flags);
7110aa77f6cSMauro Carvalho Chehab }
7120aa77f6cSMauro Carvalho Chehab 
713340a30c5Ssensoray-dev static int start_streaming(struct vb2_queue *vq, unsigned int count);
714e37559b2SHans Verkuil static void stop_streaming(struct vb2_queue *vq);
7150aa77f6cSMauro Carvalho Chehab 
7161bc17717SJulia Lawall static const struct vb2_ops s2255_video_qops = {
717340a30c5Ssensoray-dev 	.queue_setup = queue_setup,
7180aa77f6cSMauro Carvalho Chehab 	.buf_prepare = buffer_prepare,
7190aa77f6cSMauro Carvalho Chehab 	.buf_queue = buffer_queue,
720340a30c5Ssensoray-dev 	.start_streaming = start_streaming,
721340a30c5Ssensoray-dev 	.stop_streaming = stop_streaming,
722340a30c5Ssensoray-dev 	.wait_prepare = vb2_ops_wait_prepare,
723340a30c5Ssensoray-dev 	.wait_finish = vb2_ops_wait_finish,
7240aa77f6cSMauro Carvalho Chehab };
7250aa77f6cSMauro Carvalho Chehab 
7260aa77f6cSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void *priv,
7270aa77f6cSMauro Carvalho Chehab 			   struct v4l2_capability *cap)
7280aa77f6cSMauro Carvalho Chehab {
729340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
730340a30c5Ssensoray-dev 	struct s2255_dev *dev = vc->dev;
73139696009SHans Verkuil 
7320aa77f6cSMauro Carvalho Chehab 	strlcpy(cap->driver, "s2255", sizeof(cap->driver));
7330aa77f6cSMauro Carvalho Chehab 	strlcpy(cap->card, "s2255", sizeof(cap->card));
7340aa77f6cSMauro Carvalho Chehab 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
735340a30c5Ssensoray-dev 	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
736340a30c5Ssensoray-dev 		V4L2_CAP_READWRITE;
73739696009SHans Verkuil 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
7380aa77f6cSMauro Carvalho Chehab 	return 0;
7390aa77f6cSMauro Carvalho Chehab }
7400aa77f6cSMauro Carvalho Chehab 
7410aa77f6cSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
7420aa77f6cSMauro Carvalho Chehab 			       struct v4l2_fmtdesc *f)
7430aa77f6cSMauro Carvalho Chehab {
7440aa77f6cSMauro Carvalho Chehab 	int index = f->index;
7450aa77f6cSMauro Carvalho Chehab 
7460aa77f6cSMauro Carvalho Chehab 	if (index >= ARRAY_SIZE(formats))
7470aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
7480aa77f6cSMauro Carvalho Chehab 	if (!jpeg_enable && ((formats[index].fourcc == V4L2_PIX_FMT_JPEG) ||
7490aa77f6cSMauro Carvalho Chehab 			(formats[index].fourcc == V4L2_PIX_FMT_MJPEG)))
7500aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
7510aa77f6cSMauro Carvalho Chehab 	strlcpy(f->description, formats[index].name, sizeof(f->description));
7520aa77f6cSMauro Carvalho Chehab 	f->pixelformat = formats[index].fourcc;
7530aa77f6cSMauro Carvalho Chehab 	return 0;
7540aa77f6cSMauro Carvalho Chehab }
7550aa77f6cSMauro Carvalho Chehab 
7560aa77f6cSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
7570aa77f6cSMauro Carvalho Chehab 			    struct v4l2_format *f)
7580aa77f6cSMauro Carvalho Chehab {
759340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
7605e950fafSDean Anderson 	int is_ntsc = vc->std & V4L2_STD_525_60;
7610aa77f6cSMauro Carvalho Chehab 
7625e950fafSDean Anderson 	f->fmt.pix.width = vc->width;
7635e950fafSDean Anderson 	f->fmt.pix.height = vc->height;
76492513611SHans Verkuil 	if (f->fmt.pix.height >=
76592513611SHans Verkuil 	    (is_ntsc ? NUM_LINES_1CIFS_NTSC : NUM_LINES_1CIFS_PAL) * 2)
76692513611SHans Verkuil 		f->fmt.pix.field = V4L2_FIELD_INTERLACED;
76792513611SHans Verkuil 	else
76892513611SHans Verkuil 		f->fmt.pix.field = V4L2_FIELD_TOP;
7695e950fafSDean Anderson 	f->fmt.pix.pixelformat = vc->fmt->fourcc;
7705e950fafSDean Anderson 	f->fmt.pix.bytesperline = f->fmt.pix.width * (vc->fmt->depth >> 3);
7710aa77f6cSMauro Carvalho Chehab 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
77229ceb110SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
77329ceb110SHans Verkuil 	f->fmt.pix.priv = 0;
7740aa77f6cSMauro Carvalho Chehab 	return 0;
7750aa77f6cSMauro Carvalho Chehab }
7760aa77f6cSMauro Carvalho Chehab 
7770aa77f6cSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
7780aa77f6cSMauro Carvalho Chehab 			      struct v4l2_format *f)
7790aa77f6cSMauro Carvalho Chehab {
7800aa77f6cSMauro Carvalho Chehab 	const struct s2255_fmt *fmt;
7810aa77f6cSMauro Carvalho Chehab 	enum v4l2_field field;
782340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
7835e950fafSDean Anderson 	int is_ntsc = vc->std & V4L2_STD_525_60;
7840aa77f6cSMauro Carvalho Chehab 
7850aa77f6cSMauro Carvalho Chehab 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
7860aa77f6cSMauro Carvalho Chehab 
7870aa77f6cSMauro Carvalho Chehab 	if (fmt == NULL)
7880aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
7890aa77f6cSMauro Carvalho Chehab 
7900aa77f6cSMauro Carvalho Chehab 	field = f->fmt.pix.field;
7910aa77f6cSMauro Carvalho Chehab 
79292cde477SDean Anderson 	dprintk(vc->dev, 50, "%s NTSC: %d suggested width: %d, height: %d\n",
7930aa77f6cSMauro Carvalho Chehab 		__func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
7940aa77f6cSMauro Carvalho Chehab 	if (is_ntsc) {
7950aa77f6cSMauro Carvalho Chehab 		/* NTSC */
7960aa77f6cSMauro Carvalho Chehab 		if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
7970aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
79892513611SHans Verkuil 			field = V4L2_FIELD_INTERLACED;
7990aa77f6cSMauro Carvalho Chehab 		} else {
8000aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
8010aa77f6cSMauro Carvalho Chehab 			field = V4L2_FIELD_TOP;
8020aa77f6cSMauro Carvalho Chehab 		}
8030aa77f6cSMauro Carvalho Chehab 		if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
8040aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
8050aa77f6cSMauro Carvalho Chehab 		else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC)
8060aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_2CIFS_NTSC;
8070aa77f6cSMauro Carvalho Chehab 		else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC)
8080aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
8090aa77f6cSMauro Carvalho Chehab 		else
8100aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
8110aa77f6cSMauro Carvalho Chehab 	} else {
8120aa77f6cSMauro Carvalho Chehab 		/* PAL */
8130aa77f6cSMauro Carvalho Chehab 		if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
8140aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
81592513611SHans Verkuil 			field = V4L2_FIELD_INTERLACED;
8160aa77f6cSMauro Carvalho Chehab 		} else {
8170aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
8180aa77f6cSMauro Carvalho Chehab 			field = V4L2_FIELD_TOP;
8190aa77f6cSMauro Carvalho Chehab 		}
82092513611SHans Verkuil 		if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL)
8210aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
82292513611SHans Verkuil 		else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL)
8230aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_2CIFS_PAL;
82492513611SHans Verkuil 		else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL)
8250aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
82692513611SHans Verkuil 		else
8270aa77f6cSMauro Carvalho Chehab 			f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
8280aa77f6cSMauro Carvalho Chehab 	}
8290aa77f6cSMauro Carvalho Chehab 	f->fmt.pix.field = field;
8300aa77f6cSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
8310aa77f6cSMauro Carvalho Chehab 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
83229ceb110SHans Verkuil 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
83329ceb110SHans Verkuil 	f->fmt.pix.priv = 0;
83492cde477SDean Anderson 	dprintk(vc->dev, 50, "%s: set width %d height %d field %d\n", __func__,
8350aa77f6cSMauro Carvalho Chehab 		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
8360aa77f6cSMauro Carvalho Chehab 	return 0;
8370aa77f6cSMauro Carvalho Chehab }
8380aa77f6cSMauro Carvalho Chehab 
8390aa77f6cSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
8400aa77f6cSMauro Carvalho Chehab 			    struct v4l2_format *f)
8410aa77f6cSMauro Carvalho Chehab {
842340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
8430aa77f6cSMauro Carvalho Chehab 	const struct s2255_fmt *fmt;
844340a30c5Ssensoray-dev 	struct vb2_queue *q = &vc->vb_vidq;
8450aa77f6cSMauro Carvalho Chehab 	struct s2255_mode mode;
8460aa77f6cSMauro Carvalho Chehab 	int ret;
8470aa77f6cSMauro Carvalho Chehab 
848340a30c5Ssensoray-dev 	ret = vidioc_try_fmt_vid_cap(file, vc, f);
8490aa77f6cSMauro Carvalho Chehab 
8500aa77f6cSMauro Carvalho Chehab 	if (ret < 0)
8510aa77f6cSMauro Carvalho Chehab 		return ret;
8520aa77f6cSMauro Carvalho Chehab 
8530aa77f6cSMauro Carvalho Chehab 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
8540aa77f6cSMauro Carvalho Chehab 
8550aa77f6cSMauro Carvalho Chehab 	if (fmt == NULL)
8560aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
8570aa77f6cSMauro Carvalho Chehab 
858340a30c5Ssensoray-dev 	if (vb2_is_busy(q)) {
85992cde477SDean Anderson 		dprintk(vc->dev, 1, "queue busy\n");
860340a30c5Ssensoray-dev 		return -EBUSY;
8610aa77f6cSMauro Carvalho Chehab 	}
8620aa77f6cSMauro Carvalho Chehab 
8635e950fafSDean Anderson 	mode = vc->mode;
8645e950fafSDean Anderson 	vc->fmt = fmt;
8655e950fafSDean Anderson 	vc->width = f->fmt.pix.width;
8665e950fafSDean Anderson 	vc->height = f->fmt.pix.height;
867340a30c5Ssensoray-dev 	vc->field = f->fmt.pix.field;
8685e950fafSDean Anderson 	if (vc->width > norm_minw(vc)) {
8695e950fafSDean Anderson 		if (vc->height > norm_minh(vc)) {
8705e950fafSDean Anderson 			if (vc->cap_parm.capturemode &
8710aa77f6cSMauro Carvalho Chehab 			    V4L2_MODE_HIGHQUALITY)
8720aa77f6cSMauro Carvalho Chehab 				mode.scale = SCALE_4CIFSI;
8730aa77f6cSMauro Carvalho Chehab 			else
8740aa77f6cSMauro Carvalho Chehab 				mode.scale = SCALE_4CIFS;
8750aa77f6cSMauro Carvalho Chehab 		} else
8760aa77f6cSMauro Carvalho Chehab 			mode.scale = SCALE_2CIFS;
8770aa77f6cSMauro Carvalho Chehab 
8780aa77f6cSMauro Carvalho Chehab 	} else {
8790aa77f6cSMauro Carvalho Chehab 		mode.scale = SCALE_1CIFS;
8800aa77f6cSMauro Carvalho Chehab 	}
8810aa77f6cSMauro Carvalho Chehab 	/* color mode */
8825e950fafSDean Anderson 	switch (vc->fmt->fourcc) {
8830aa77f6cSMauro Carvalho Chehab 	case V4L2_PIX_FMT_GREY:
8840aa77f6cSMauro Carvalho Chehab 		mode.color &= ~MASK_COLOR;
8850aa77f6cSMauro Carvalho Chehab 		mode.color |= COLOR_Y8;
8860aa77f6cSMauro Carvalho Chehab 		break;
8870aa77f6cSMauro Carvalho Chehab 	case V4L2_PIX_FMT_JPEG:
8880aa77f6cSMauro Carvalho Chehab 	case V4L2_PIX_FMT_MJPEG:
8890aa77f6cSMauro Carvalho Chehab 		mode.color &= ~MASK_COLOR;
8900aa77f6cSMauro Carvalho Chehab 		mode.color |= COLOR_JPG;
8915e950fafSDean Anderson 		mode.color |= (vc->jpegqual << 8);
8920aa77f6cSMauro Carvalho Chehab 		break;
8930aa77f6cSMauro Carvalho Chehab 	case V4L2_PIX_FMT_YUV422P:
8940aa77f6cSMauro Carvalho Chehab 		mode.color &= ~MASK_COLOR;
8950aa77f6cSMauro Carvalho Chehab 		mode.color |= COLOR_YUVPL;
8960aa77f6cSMauro Carvalho Chehab 		break;
8970aa77f6cSMauro Carvalho Chehab 	case V4L2_PIX_FMT_YUYV:
8980aa77f6cSMauro Carvalho Chehab 	case V4L2_PIX_FMT_UYVY:
8990aa77f6cSMauro Carvalho Chehab 	default:
9000aa77f6cSMauro Carvalho Chehab 		mode.color &= ~MASK_COLOR;
9010aa77f6cSMauro Carvalho Chehab 		mode.color |= COLOR_YUVPK;
9020aa77f6cSMauro Carvalho Chehab 		break;
9030aa77f6cSMauro Carvalho Chehab 	}
9045e950fafSDean Anderson 	if ((mode.color & MASK_COLOR) != (vc->mode.color & MASK_COLOR))
9050aa77f6cSMauro Carvalho Chehab 		mode.restart = 1;
9065e950fafSDean Anderson 	else if (mode.scale != vc->mode.scale)
9070aa77f6cSMauro Carvalho Chehab 		mode.restart = 1;
9085e950fafSDean Anderson 	else if (mode.format != vc->mode.format)
9090aa77f6cSMauro Carvalho Chehab 		mode.restart = 1;
9105e950fafSDean Anderson 	vc->mode = mode;
9115e950fafSDean Anderson 	(void) s2255_set_mode(vc, &mode);
912340a30c5Ssensoray-dev 	return 0;
9130aa77f6cSMauro Carvalho Chehab }
9140aa77f6cSMauro Carvalho Chehab 
9150aa77f6cSMauro Carvalho Chehab 
9160aa77f6cSMauro Carvalho Chehab /* write to the configuration pipe, synchronously */
9170aa77f6cSMauro Carvalho Chehab static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
9180aa77f6cSMauro Carvalho Chehab 			      int size)
9190aa77f6cSMauro Carvalho Chehab {
9200aa77f6cSMauro Carvalho Chehab 	int pipe;
9210aa77f6cSMauro Carvalho Chehab 	int done;
9220aa77f6cSMauro Carvalho Chehab 	long retval = -1;
9230aa77f6cSMauro Carvalho Chehab 	if (udev) {
9240aa77f6cSMauro Carvalho Chehab 		pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
9250aa77f6cSMauro Carvalho Chehab 		retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
9260aa77f6cSMauro Carvalho Chehab 	}
9270aa77f6cSMauro Carvalho Chehab 	return retval;
9280aa77f6cSMauro Carvalho Chehab }
9290aa77f6cSMauro Carvalho Chehab 
9300aa77f6cSMauro Carvalho Chehab static u32 get_transfer_size(struct s2255_mode *mode)
9310aa77f6cSMauro Carvalho Chehab {
9320aa77f6cSMauro Carvalho Chehab 	int linesPerFrame = LINE_SZ_DEF;
9330aa77f6cSMauro Carvalho Chehab 	int pixelsPerLine = NUM_LINES_DEF;
9340aa77f6cSMauro Carvalho Chehab 	u32 outImageSize;
9350aa77f6cSMauro Carvalho Chehab 	u32 usbInSize;
9360aa77f6cSMauro Carvalho Chehab 	unsigned int mask_mult;
9370aa77f6cSMauro Carvalho Chehab 
9380aa77f6cSMauro Carvalho Chehab 	if (mode == NULL)
9390aa77f6cSMauro Carvalho Chehab 		return 0;
9400aa77f6cSMauro Carvalho Chehab 
9410aa77f6cSMauro Carvalho Chehab 	if (mode->format == FORMAT_NTSC) {
9420aa77f6cSMauro Carvalho Chehab 		switch (mode->scale) {
9430aa77f6cSMauro Carvalho Chehab 		case SCALE_4CIFS:
9440aa77f6cSMauro Carvalho Chehab 		case SCALE_4CIFSI:
9450aa77f6cSMauro Carvalho Chehab 			linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
9460aa77f6cSMauro Carvalho Chehab 			pixelsPerLine = LINE_SZ_4CIFS_NTSC;
9470aa77f6cSMauro Carvalho Chehab 			break;
9480aa77f6cSMauro Carvalho Chehab 		case SCALE_2CIFS:
9490aa77f6cSMauro Carvalho Chehab 			linesPerFrame = NUM_LINES_2CIFS_NTSC;
9500aa77f6cSMauro Carvalho Chehab 			pixelsPerLine = LINE_SZ_2CIFS_NTSC;
9510aa77f6cSMauro Carvalho Chehab 			break;
9520aa77f6cSMauro Carvalho Chehab 		case SCALE_1CIFS:
9530aa77f6cSMauro Carvalho Chehab 			linesPerFrame = NUM_LINES_1CIFS_NTSC;
9540aa77f6cSMauro Carvalho Chehab 			pixelsPerLine = LINE_SZ_1CIFS_NTSC;
9550aa77f6cSMauro Carvalho Chehab 			break;
9560aa77f6cSMauro Carvalho Chehab 		default:
9570aa77f6cSMauro Carvalho Chehab 			break;
9580aa77f6cSMauro Carvalho Chehab 		}
9590aa77f6cSMauro Carvalho Chehab 	} else if (mode->format == FORMAT_PAL) {
9600aa77f6cSMauro Carvalho Chehab 		switch (mode->scale) {
9610aa77f6cSMauro Carvalho Chehab 		case SCALE_4CIFS:
9620aa77f6cSMauro Carvalho Chehab 		case SCALE_4CIFSI:
9630aa77f6cSMauro Carvalho Chehab 			linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
9640aa77f6cSMauro Carvalho Chehab 			pixelsPerLine = LINE_SZ_4CIFS_PAL;
9650aa77f6cSMauro Carvalho Chehab 			break;
9660aa77f6cSMauro Carvalho Chehab 		case SCALE_2CIFS:
9670aa77f6cSMauro Carvalho Chehab 			linesPerFrame = NUM_LINES_2CIFS_PAL;
9680aa77f6cSMauro Carvalho Chehab 			pixelsPerLine = LINE_SZ_2CIFS_PAL;
9690aa77f6cSMauro Carvalho Chehab 			break;
9700aa77f6cSMauro Carvalho Chehab 		case SCALE_1CIFS:
9710aa77f6cSMauro Carvalho Chehab 			linesPerFrame = NUM_LINES_1CIFS_PAL;
9720aa77f6cSMauro Carvalho Chehab 			pixelsPerLine = LINE_SZ_1CIFS_PAL;
9730aa77f6cSMauro Carvalho Chehab 			break;
9740aa77f6cSMauro Carvalho Chehab 		default:
9750aa77f6cSMauro Carvalho Chehab 			break;
9760aa77f6cSMauro Carvalho Chehab 		}
9770aa77f6cSMauro Carvalho Chehab 	}
9780aa77f6cSMauro Carvalho Chehab 	outImageSize = linesPerFrame * pixelsPerLine;
9790aa77f6cSMauro Carvalho Chehab 	if ((mode->color & MASK_COLOR) != COLOR_Y8) {
9800aa77f6cSMauro Carvalho Chehab 		/* 2 bytes/pixel if not monochrome */
9810aa77f6cSMauro Carvalho Chehab 		outImageSize *= 2;
9820aa77f6cSMauro Carvalho Chehab 	}
9830aa77f6cSMauro Carvalho Chehab 
9840aa77f6cSMauro Carvalho Chehab 	/* total bytes to send including prefix and 4K padding;
9850aa77f6cSMauro Carvalho Chehab 	   must be a multiple of USB_READ_SIZE */
9860aa77f6cSMauro Carvalho Chehab 	usbInSize = outImageSize + PREFIX_SIZE;	/* always send prefix */
9870aa77f6cSMauro Carvalho Chehab 	mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
9880aa77f6cSMauro Carvalho Chehab 	/* if size not a multiple of USB_READ_SIZE */
9890aa77f6cSMauro Carvalho Chehab 	if (usbInSize & ~mask_mult)
9900aa77f6cSMauro Carvalho Chehab 		usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
9910aa77f6cSMauro Carvalho Chehab 	return usbInSize;
9920aa77f6cSMauro Carvalho Chehab }
9930aa77f6cSMauro Carvalho Chehab 
9940aa77f6cSMauro Carvalho Chehab static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
9950aa77f6cSMauro Carvalho Chehab {
9960aa77f6cSMauro Carvalho Chehab 	struct device *dev = &sdev->udev->dev;
9970aa77f6cSMauro Carvalho Chehab 	dev_info(dev, "------------------------------------------------\n");
9980aa77f6cSMauro Carvalho Chehab 	dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
9990aa77f6cSMauro Carvalho Chehab 	dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
10000aa77f6cSMauro Carvalho Chehab 	dev_info(dev, "bright: 0x%x\n", mode->bright);
10010aa77f6cSMauro Carvalho Chehab 	dev_info(dev, "------------------------------------------------\n");
10020aa77f6cSMauro Carvalho Chehab }
10030aa77f6cSMauro Carvalho Chehab 
10040aa77f6cSMauro Carvalho Chehab /*
10050aa77f6cSMauro Carvalho Chehab  * set mode is the function which controls the DSP.
10060aa77f6cSMauro Carvalho Chehab  * the restart parameter in struct s2255_mode should be set whenever
10070aa77f6cSMauro Carvalho Chehab  * the image size could change via color format, video system or image
10080aa77f6cSMauro Carvalho Chehab  * size.
10090aa77f6cSMauro Carvalho Chehab  * When the restart parameter is set, we sleep for ONE frame to allow the
10100aa77f6cSMauro Carvalho Chehab  * DSP time to get the new frame
10110aa77f6cSMauro Carvalho Chehab  */
10125e950fafSDean Anderson static int s2255_set_mode(struct s2255_vc *vc,
10130aa77f6cSMauro Carvalho Chehab 			  struct s2255_mode *mode)
10140aa77f6cSMauro Carvalho Chehab {
10150aa77f6cSMauro Carvalho Chehab 	int res;
10160aa77f6cSMauro Carvalho Chehab 	unsigned long chn_rev;
10175e950fafSDean Anderson 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
10180b84caabSHans Verkuil 	int i;
101947d8c881SDean Anderson 	__le32 *buffer = dev->cmdbuf;
10200b84caabSHans Verkuil 
102147d8c881SDean Anderson 	mutex_lock(&dev->cmdlock);
10225e950fafSDean Anderson 	chn_rev = G_chnmap[vc->idx];
10235e950fafSDean Anderson 	dprintk(dev, 3, "%s channel: %d\n", __func__, vc->idx);
10240aa77f6cSMauro Carvalho Chehab 	/* if JPEG, set the quality */
10250aa77f6cSMauro Carvalho Chehab 	if ((mode->color & MASK_COLOR) == COLOR_JPG) {
10260aa77f6cSMauro Carvalho Chehab 		mode->color &= ~MASK_COLOR;
10270aa77f6cSMauro Carvalho Chehab 		mode->color |= COLOR_JPG;
10280aa77f6cSMauro Carvalho Chehab 		mode->color &= ~MASK_JPG_QUALITY;
10295e950fafSDean Anderson 		mode->color |= (vc->jpegqual << 8);
10300aa77f6cSMauro Carvalho Chehab 	}
10310aa77f6cSMauro Carvalho Chehab 	/* save the mode */
10325e950fafSDean Anderson 	vc->mode = *mode;
10335e950fafSDean Anderson 	vc->req_image_size = get_transfer_size(mode);
10345e950fafSDean Anderson 	dprintk(dev, 1, "%s: reqsize %ld\n", __func__, vc->req_image_size);
10350aa77f6cSMauro Carvalho Chehab 	/* set the mode */
10360aa77f6cSMauro Carvalho Chehab 	buffer[0] = IN_DATA_TOKEN;
10370aa77f6cSMauro Carvalho Chehab 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
10380aa77f6cSMauro Carvalho Chehab 	buffer[2] = CMD_SET_MODE;
10390b84caabSHans Verkuil 	for (i = 0; i < sizeof(struct s2255_mode) / sizeof(u32); i++)
10405e950fafSDean Anderson 		buffer[3 + i] = cpu_to_le32(((u32 *)&vc->mode)[i]);
10415e950fafSDean Anderson 	vc->setmode_ready = 0;
10420aa77f6cSMauro Carvalho Chehab 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
10430aa77f6cSMauro Carvalho Chehab 	if (debug)
10440aa77f6cSMauro Carvalho Chehab 		s2255_print_cfg(dev, mode);
10450aa77f6cSMauro Carvalho Chehab 	/* wait at least 3 frames before continuing */
10460aa77f6cSMauro Carvalho Chehab 	if (mode->restart) {
10475e950fafSDean Anderson 		wait_event_timeout(vc->wait_setmode,
10485e950fafSDean Anderson 				   (vc->setmode_ready != 0),
10490aa77f6cSMauro Carvalho Chehab 				   msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
10505e950fafSDean Anderson 		if (vc->setmode_ready != 1) {
1051f5402007Ssensoray-dev 			dprintk(dev, 0, "s2255: no set mode response\n");
10520aa77f6cSMauro Carvalho Chehab 			res = -EFAULT;
10530aa77f6cSMauro Carvalho Chehab 		}
10540aa77f6cSMauro Carvalho Chehab 	}
10550aa77f6cSMauro Carvalho Chehab 	/* clear the restart flag */
10565e950fafSDean Anderson 	vc->mode.restart = 0;
10575e950fafSDean Anderson 	dprintk(dev, 1, "%s chn %d, result: %d\n", __func__, vc->idx, res);
105847d8c881SDean Anderson 	mutex_unlock(&dev->cmdlock);
10590aa77f6cSMauro Carvalho Chehab 	return res;
10600aa77f6cSMauro Carvalho Chehab }
10610aa77f6cSMauro Carvalho Chehab 
10625e950fafSDean Anderson static int s2255_cmd_status(struct s2255_vc *vc, u32 *pstatus)
10630aa77f6cSMauro Carvalho Chehab {
10640aa77f6cSMauro Carvalho Chehab 	int res;
10650aa77f6cSMauro Carvalho Chehab 	u32 chn_rev;
10665e950fafSDean Anderson 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
106747d8c881SDean Anderson 	__le32 *buffer = dev->cmdbuf;
106847d8c881SDean Anderson 
106947d8c881SDean Anderson 	mutex_lock(&dev->cmdlock);
10705e950fafSDean Anderson 	chn_rev = G_chnmap[vc->idx];
10715e950fafSDean Anderson 	dprintk(dev, 4, "%s chan %d\n", __func__, vc->idx);
10720aa77f6cSMauro Carvalho Chehab 	/* form the get vid status command */
10730aa77f6cSMauro Carvalho Chehab 	buffer[0] = IN_DATA_TOKEN;
10740aa77f6cSMauro Carvalho Chehab 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
10750aa77f6cSMauro Carvalho Chehab 	buffer[2] = CMD_STATUS;
10760aa77f6cSMauro Carvalho Chehab 	*pstatus = 0;
10775e950fafSDean Anderson 	vc->vidstatus_ready = 0;
10780aa77f6cSMauro Carvalho Chehab 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
10795e950fafSDean Anderson 	wait_event_timeout(vc->wait_vidstatus,
10805e950fafSDean Anderson 			   (vc->vidstatus_ready != 0),
10810aa77f6cSMauro Carvalho Chehab 			   msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
10825e950fafSDean Anderson 	if (vc->vidstatus_ready != 1) {
1083f5402007Ssensoray-dev 		dprintk(dev, 0, "s2255: no vidstatus response\n");
10840aa77f6cSMauro Carvalho Chehab 		res = -EFAULT;
10850aa77f6cSMauro Carvalho Chehab 	}
10865e950fafSDean Anderson 	*pstatus = vc->vidstatus;
1087f5402007Ssensoray-dev 	dprintk(dev, 4, "%s, vid status %d\n", __func__, *pstatus);
108847d8c881SDean Anderson 	mutex_unlock(&dev->cmdlock);
10890aa77f6cSMauro Carvalho Chehab 	return res;
10900aa77f6cSMauro Carvalho Chehab }
10910aa77f6cSMauro Carvalho Chehab 
1092340a30c5Ssensoray-dev static int start_streaming(struct vb2_queue *vq, unsigned int count)
10930aa77f6cSMauro Carvalho Chehab {
1094340a30c5Ssensoray-dev 	struct s2255_vc *vc = vb2_get_drv_priv(vq);
10950aa77f6cSMauro Carvalho Chehab 	int j;
109692cde477SDean Anderson 
10975e950fafSDean Anderson 	vc->last_frame = -1;
10985e950fafSDean Anderson 	vc->bad_payload = 0;
10995e950fafSDean Anderson 	vc->cur_frame = 0;
11005e950fafSDean Anderson 	vc->frame_count = 0;
11010aa77f6cSMauro Carvalho Chehab 	for (j = 0; j < SYS_FRAMES; j++) {
11025e950fafSDean Anderson 		vc->buffer.frame[j].ulState = S2255_READ_IDLE;
11035e950fafSDean Anderson 		vc->buffer.frame[j].cur_size = 0;
11040aa77f6cSMauro Carvalho Chehab 	}
1105340a30c5Ssensoray-dev 	return s2255_start_acquire(vc);
11060aa77f6cSMauro Carvalho Chehab }
11070aa77f6cSMauro Carvalho Chehab 
1108340a30c5Ssensoray-dev /* abort streaming and wait for last buffer */
1109e37559b2SHans Verkuil static void stop_streaming(struct vb2_queue *vq)
11100aa77f6cSMauro Carvalho Chehab {
1111340a30c5Ssensoray-dev 	struct s2255_vc *vc = vb2_get_drv_priv(vq);
1112340a30c5Ssensoray-dev 	struct s2255_buffer *buf, *node;
1113340a30c5Ssensoray-dev 	unsigned long flags;
1114340a30c5Ssensoray-dev 	(void) s2255_stop_acquire(vc);
1115340a30c5Ssensoray-dev 	spin_lock_irqsave(&vc->qlock, flags);
1116340a30c5Ssensoray-dev 	list_for_each_entry_safe(buf, node, &vc->buf_list, list) {
1117340a30c5Ssensoray-dev 		list_del(&buf->list);
11182d700715SJunghak Sung 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1119340a30c5Ssensoray-dev 		dprintk(vc->dev, 2, "[%p/%d] done\n",
11202d700715SJunghak Sung 			buf, buf->vb.vb2_buf.index);
1121340a30c5Ssensoray-dev 	}
1122340a30c5Ssensoray-dev 	spin_unlock_irqrestore(&vc->qlock, flags);
11230aa77f6cSMauro Carvalho Chehab }
11240aa77f6cSMauro Carvalho Chehab 
1125314527acSHans Verkuil static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id i)
11260aa77f6cSMauro Carvalho Chehab {
1127340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
11280aa77f6cSMauro Carvalho Chehab 	struct s2255_mode mode;
1129340a30c5Ssensoray-dev 	struct vb2_queue *q = &vc->vb_vidq;
1130469af77aSHans Verkuil 
1131340a30c5Ssensoray-dev 	/*
1132340a30c5Ssensoray-dev 	 * Changing the standard implies a format change, which is not allowed
1133340a30c5Ssensoray-dev 	 * while buffers for use with streaming have already been allocated.
1134340a30c5Ssensoray-dev 	 */
1135340a30c5Ssensoray-dev 	if (vb2_is_busy(q))
1136340a30c5Ssensoray-dev 		return -EBUSY;
1137340a30c5Ssensoray-dev 
113892cde477SDean Anderson 	mode = vc->mode;
1139314527acSHans Verkuil 	if (i & V4L2_STD_525_60) {
114092cde477SDean Anderson 		dprintk(vc->dev, 4, "%s 60 Hz\n", __func__);
11410aa77f6cSMauro Carvalho Chehab 		/* if changing format, reset frame decimation/intervals */
11420aa77f6cSMauro Carvalho Chehab 		if (mode.format != FORMAT_NTSC) {
11430aa77f6cSMauro Carvalho Chehab 			mode.restart = 1;
11440aa77f6cSMauro Carvalho Chehab 			mode.format = FORMAT_NTSC;
11450aa77f6cSMauro Carvalho Chehab 			mode.fdec = FDEC_1;
11465e950fafSDean Anderson 			vc->width = LINE_SZ_4CIFS_NTSC;
11475e950fafSDean Anderson 			vc->height = NUM_LINES_4CIFS_NTSC * 2;
11480aa77f6cSMauro Carvalho Chehab 		}
1149314527acSHans Verkuil 	} else if (i & V4L2_STD_625_50) {
115092cde477SDean Anderson 		dprintk(vc->dev, 4, "%s 50 Hz\n", __func__);
11510aa77f6cSMauro Carvalho Chehab 		if (mode.format != FORMAT_PAL) {
11520aa77f6cSMauro Carvalho Chehab 			mode.restart = 1;
11530aa77f6cSMauro Carvalho Chehab 			mode.format = FORMAT_PAL;
11540aa77f6cSMauro Carvalho Chehab 			mode.fdec = FDEC_1;
11555e950fafSDean Anderson 			vc->width = LINE_SZ_4CIFS_PAL;
11565e950fafSDean Anderson 			vc->height = NUM_LINES_4CIFS_PAL * 2;
11570aa77f6cSMauro Carvalho Chehab 		}
1158340a30c5Ssensoray-dev 	} else
1159340a30c5Ssensoray-dev 		return -EINVAL;
116092cde477SDean Anderson 	vc->std = i;
11610aa77f6cSMauro Carvalho Chehab 	if (mode.restart)
116292cde477SDean Anderson 		s2255_set_mode(vc, &mode);
1163340a30c5Ssensoray-dev 	return 0;
11640aa77f6cSMauro Carvalho Chehab }
11650aa77f6cSMauro Carvalho Chehab 
1166469af77aSHans Verkuil static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *i)
1167469af77aSHans Verkuil {
1168340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
1169469af77aSHans Verkuil 
117092cde477SDean Anderson 	*i = vc->std;
1171469af77aSHans Verkuil 	return 0;
1172469af77aSHans Verkuil }
1173469af77aSHans Verkuil 
11740aa77f6cSMauro Carvalho Chehab /* Sensoray 2255 is a multiple channel capture device.
11750aa77f6cSMauro Carvalho Chehab    It does not have a "crossbar" of inputs.
11760aa77f6cSMauro Carvalho Chehab    We use one V4L device per channel. The user must
11770aa77f6cSMauro Carvalho Chehab    be aware that certain combinations are not allowed.
11780aa77f6cSMauro Carvalho Chehab    For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
11790aa77f6cSMauro Carvalho Chehab    at once in color(you can do full fps on 4 channels with greyscale.
11800aa77f6cSMauro Carvalho Chehab */
11810aa77f6cSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
11820aa77f6cSMauro Carvalho Chehab 			     struct v4l2_input *inp)
11830aa77f6cSMauro Carvalho Chehab {
1184340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
118592cde477SDean Anderson 	struct s2255_dev *dev = vc->dev;
11860aa77f6cSMauro Carvalho Chehab 	u32 status = 0;
118792cde477SDean Anderson 
11880aa77f6cSMauro Carvalho Chehab 	if (inp->index != 0)
11890aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
11900aa77f6cSMauro Carvalho Chehab 	inp->type = V4L2_INPUT_TYPE_CAMERA;
11910aa77f6cSMauro Carvalho Chehab 	inp->std = S2255_NORMS;
11920aa77f6cSMauro Carvalho Chehab 	inp->status = 0;
11930aa77f6cSMauro Carvalho Chehab 	if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
11940aa77f6cSMauro Carvalho Chehab 		int rc;
119592cde477SDean Anderson 		rc = s2255_cmd_status(vc, &status);
1196f5402007Ssensoray-dev 		dprintk(dev, 4, "s2255_cmd_status rc: %d status %x\n",
1197f5402007Ssensoray-dev 			rc, status);
11980aa77f6cSMauro Carvalho Chehab 		if (rc == 0)
11990aa77f6cSMauro Carvalho Chehab 			inp->status =  (status & 0x01) ? 0
12000aa77f6cSMauro Carvalho Chehab 				: V4L2_IN_ST_NO_SIGNAL;
12010aa77f6cSMauro Carvalho Chehab 	}
12020aa77f6cSMauro Carvalho Chehab 	switch (dev->pid) {
12030aa77f6cSMauro Carvalho Chehab 	case 0x2255:
12040aa77f6cSMauro Carvalho Chehab 	default:
12050aa77f6cSMauro Carvalho Chehab 		strlcpy(inp->name, "Composite", sizeof(inp->name));
12060aa77f6cSMauro Carvalho Chehab 		break;
12070aa77f6cSMauro Carvalho Chehab 	case 0x2257:
12085e950fafSDean Anderson 		strlcpy(inp->name, (vc->idx < 2) ? "Composite" : "S-Video",
12090aa77f6cSMauro Carvalho Chehab 			sizeof(inp->name));
12100aa77f6cSMauro Carvalho Chehab 		break;
12110aa77f6cSMauro Carvalho Chehab 	}
12120aa77f6cSMauro Carvalho Chehab 	return 0;
12130aa77f6cSMauro Carvalho Chehab }
12140aa77f6cSMauro Carvalho Chehab 
12150aa77f6cSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
12160aa77f6cSMauro Carvalho Chehab {
12170aa77f6cSMauro Carvalho Chehab 	*i = 0;
12180aa77f6cSMauro Carvalho Chehab 	return 0;
12190aa77f6cSMauro Carvalho Chehab }
12200aa77f6cSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
12210aa77f6cSMauro Carvalho Chehab {
12220aa77f6cSMauro Carvalho Chehab 	if (i > 0)
12230aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
12240aa77f6cSMauro Carvalho Chehab 	return 0;
12250aa77f6cSMauro Carvalho Chehab }
12260aa77f6cSMauro Carvalho Chehab 
1227192f1e78SHans Verkuil static int s2255_s_ctrl(struct v4l2_ctrl *ctrl)
12280aa77f6cSMauro Carvalho Chehab {
12295e950fafSDean Anderson 	struct s2255_vc *vc =
12305e950fafSDean Anderson 		container_of(ctrl->handler, struct s2255_vc, hdl);
12310aa77f6cSMauro Carvalho Chehab 	struct s2255_mode mode;
12325e950fafSDean Anderson 	mode = vc->mode;
12330aa77f6cSMauro Carvalho Chehab 	/* update the mode to the corresponding value */
12340aa77f6cSMauro Carvalho Chehab 	switch (ctrl->id) {
12350aa77f6cSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
1236192f1e78SHans Verkuil 		mode.bright = ctrl->val;
12370aa77f6cSMauro Carvalho Chehab 		break;
12380aa77f6cSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
1239192f1e78SHans Verkuil 		mode.contrast = ctrl->val;
12400aa77f6cSMauro Carvalho Chehab 		break;
12410aa77f6cSMauro Carvalho Chehab 	case V4L2_CID_HUE:
1242192f1e78SHans Verkuil 		mode.hue = ctrl->val;
12430aa77f6cSMauro Carvalho Chehab 		break;
12440aa77f6cSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
1245192f1e78SHans Verkuil 		mode.saturation = ctrl->val;
12460aa77f6cSMauro Carvalho Chehab 		break;
1247192f1e78SHans Verkuil 	case V4L2_CID_S2255_COLORFILTER:
12480aa77f6cSMauro Carvalho Chehab 		mode.color &= ~MASK_INPUT_TYPE;
1249192f1e78SHans Verkuil 		mode.color |= !ctrl->val << 16;
12500aa77f6cSMauro Carvalho Chehab 		break;
12517041dec7SHans Verkuil 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
12525e950fafSDean Anderson 		vc->jpegqual = ctrl->val;
12537041dec7SHans Verkuil 		return 0;
12540aa77f6cSMauro Carvalho Chehab 	default:
12550aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
12560aa77f6cSMauro Carvalho Chehab 	}
12570aa77f6cSMauro Carvalho Chehab 	mode.restart = 0;
12580aa77f6cSMauro Carvalho Chehab 	/* set mode here.  Note: stream does not need restarted.
12590aa77f6cSMauro Carvalho Chehab 	   some V4L programs restart stream unnecessarily
12600aa77f6cSMauro Carvalho Chehab 	   after a s_crtl.
12610aa77f6cSMauro Carvalho Chehab 	*/
12625e950fafSDean Anderson 	s2255_set_mode(vc, &mode);
12630aa77f6cSMauro Carvalho Chehab 	return 0;
12640aa77f6cSMauro Carvalho Chehab }
12650aa77f6cSMauro Carvalho Chehab 
12660aa77f6cSMauro Carvalho Chehab static int vidioc_g_jpegcomp(struct file *file, void *priv,
12670aa77f6cSMauro Carvalho Chehab 			 struct v4l2_jpegcompression *jc)
12680aa77f6cSMauro Carvalho Chehab {
1269340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
12707041dec7SHans Verkuil 
12717041dec7SHans Verkuil 	memset(jc, 0, sizeof(*jc));
12725e950fafSDean Anderson 	jc->quality = vc->jpegqual;
127392cde477SDean Anderson 	dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality);
12740aa77f6cSMauro Carvalho Chehab 	return 0;
12750aa77f6cSMauro Carvalho Chehab }
12760aa77f6cSMauro Carvalho Chehab 
12770aa77f6cSMauro Carvalho Chehab static int vidioc_s_jpegcomp(struct file *file, void *priv,
1278d88aab53SHans Verkuil 			 const struct v4l2_jpegcompression *jc)
12790aa77f6cSMauro Carvalho Chehab {
1280340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
1281340a30c5Ssensoray-dev 
12820aa77f6cSMauro Carvalho Chehab 	if (jc->quality < 0 || jc->quality > 100)
12830aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
12845e950fafSDean Anderson 	v4l2_ctrl_s_ctrl(vc->jpegqual_ctrl, jc->quality);
128592cde477SDean Anderson 	dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality);
12860aa77f6cSMauro Carvalho Chehab 	return 0;
12870aa77f6cSMauro Carvalho Chehab }
12880aa77f6cSMauro Carvalho Chehab 
12890aa77f6cSMauro Carvalho Chehab static int vidioc_g_parm(struct file *file, void *priv,
12900aa77f6cSMauro Carvalho Chehab 			 struct v4l2_streamparm *sp)
12910aa77f6cSMauro Carvalho Chehab {
12920aa77f6cSMauro Carvalho Chehab 	__u32 def_num, def_dem;
1293340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
1294340a30c5Ssensoray-dev 
12950aa77f6cSMauro Carvalho Chehab 	if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
12960aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
12970aa77f6cSMauro Carvalho Chehab 	sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
12985e950fafSDean Anderson 	sp->parm.capture.capturemode = vc->cap_parm.capturemode;
1299340a30c5Ssensoray-dev 	sp->parm.capture.readbuffers = S2255_MIN_BUFS;
13005e950fafSDean Anderson 	def_num = (vc->mode.format == FORMAT_NTSC) ? 1001 : 1000;
13015e950fafSDean Anderson 	def_dem = (vc->mode.format == FORMAT_NTSC) ? 30000 : 25000;
13020aa77f6cSMauro Carvalho Chehab 	sp->parm.capture.timeperframe.denominator = def_dem;
13035e950fafSDean Anderson 	switch (vc->mode.fdec) {
13040aa77f6cSMauro Carvalho Chehab 	default:
13050aa77f6cSMauro Carvalho Chehab 	case FDEC_1:
13060aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num;
13070aa77f6cSMauro Carvalho Chehab 		break;
13080aa77f6cSMauro Carvalho Chehab 	case FDEC_2:
13090aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num * 2;
13100aa77f6cSMauro Carvalho Chehab 		break;
13110aa77f6cSMauro Carvalho Chehab 	case FDEC_3:
13120aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num * 3;
13130aa77f6cSMauro Carvalho Chehab 		break;
13140aa77f6cSMauro Carvalho Chehab 	case FDEC_5:
13150aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num * 5;
13160aa77f6cSMauro Carvalho Chehab 		break;
13170aa77f6cSMauro Carvalho Chehab 	}
131892cde477SDean Anderson 	dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d\n",
1319f5402007Ssensoray-dev 		__func__,
13200aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.capturemode,
13210aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator,
13220aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.denominator);
13230aa77f6cSMauro Carvalho Chehab 	return 0;
13240aa77f6cSMauro Carvalho Chehab }
13250aa77f6cSMauro Carvalho Chehab 
13260aa77f6cSMauro Carvalho Chehab static int vidioc_s_parm(struct file *file, void *priv,
13270aa77f6cSMauro Carvalho Chehab 			 struct v4l2_streamparm *sp)
13280aa77f6cSMauro Carvalho Chehab {
1329340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
13300aa77f6cSMauro Carvalho Chehab 	struct s2255_mode mode;
13310aa77f6cSMauro Carvalho Chehab 	int fdec = FDEC_1;
13320aa77f6cSMauro Carvalho Chehab 	__u32 def_num, def_dem;
13330aa77f6cSMauro Carvalho Chehab 	if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
13340aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
13355e950fafSDean Anderson 	mode = vc->mode;
13360aa77f6cSMauro Carvalho Chehab 	/* high quality capture mode requires a stream restart */
1337340a30c5Ssensoray-dev 	if ((vc->cap_parm.capturemode != sp->parm.capture.capturemode)
1338340a30c5Ssensoray-dev 	    && vb2_is_streaming(&vc->vb_vidq))
13390aa77f6cSMauro Carvalho Chehab 		return -EBUSY;
13400aa77f6cSMauro Carvalho Chehab 	def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000;
13410aa77f6cSMauro Carvalho Chehab 	def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000;
13420aa77f6cSMauro Carvalho Chehab 	if (def_dem != sp->parm.capture.timeperframe.denominator)
13430aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num;
13440aa77f6cSMauro Carvalho Chehab 	else if (sp->parm.capture.timeperframe.numerator <= def_num)
13450aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num;
13460aa77f6cSMauro Carvalho Chehab 	else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
13470aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num * 2;
13480aa77f6cSMauro Carvalho Chehab 		fdec = FDEC_2;
13490aa77f6cSMauro Carvalho Chehab 	} else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
13500aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num * 3;
13510aa77f6cSMauro Carvalho Chehab 		fdec = FDEC_3;
13520aa77f6cSMauro Carvalho Chehab 	} else {
13530aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator = def_num * 5;
13540aa77f6cSMauro Carvalho Chehab 		fdec = FDEC_5;
13550aa77f6cSMauro Carvalho Chehab 	}
13560aa77f6cSMauro Carvalho Chehab 	mode.fdec = fdec;
13570aa77f6cSMauro Carvalho Chehab 	sp->parm.capture.timeperframe.denominator = def_dem;
1358340a30c5Ssensoray-dev 	sp->parm.capture.readbuffers = S2255_MIN_BUFS;
13595e950fafSDean Anderson 	s2255_set_mode(vc, &mode);
136092cde477SDean Anderson 	dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
13610aa77f6cSMauro Carvalho Chehab 		__func__,
13620aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.capturemode,
13630aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.numerator,
13640aa77f6cSMauro Carvalho Chehab 		sp->parm.capture.timeperframe.denominator, fdec);
13650aa77f6cSMauro Carvalho Chehab 	return 0;
13660aa77f6cSMauro Carvalho Chehab }
13670aa77f6cSMauro Carvalho Chehab 
136805e5d44bSHans Verkuil #define NUM_SIZE_ENUMS 3
136905e5d44bSHans Verkuil static const struct v4l2_frmsize_discrete ntsc_sizes[] = {
137005e5d44bSHans Verkuil 	{ 640, 480 },
137105e5d44bSHans Verkuil 	{ 640, 240 },
137205e5d44bSHans Verkuil 	{ 320, 240 },
137305e5d44bSHans Verkuil };
137405e5d44bSHans Verkuil static const struct v4l2_frmsize_discrete pal_sizes[] = {
137505e5d44bSHans Verkuil 	{ 704, 576 },
137605e5d44bSHans Verkuil 	{ 704, 288 },
137705e5d44bSHans Verkuil 	{ 352, 288 },
137805e5d44bSHans Verkuil };
137905e5d44bSHans Verkuil 
138005e5d44bSHans Verkuil static int vidioc_enum_framesizes(struct file *file, void *priv,
138105e5d44bSHans Verkuil 			    struct v4l2_frmsizeenum *fe)
138205e5d44bSHans Verkuil {
1383340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
13845e950fafSDean Anderson 	int is_ntsc = vc->std & V4L2_STD_525_60;
138505e5d44bSHans Verkuil 	const struct s2255_fmt *fmt;
138605e5d44bSHans Verkuil 
138705e5d44bSHans Verkuil 	if (fe->index >= NUM_SIZE_ENUMS)
138805e5d44bSHans Verkuil 		return -EINVAL;
138905e5d44bSHans Verkuil 
139005e5d44bSHans Verkuil 	fmt = format_by_fourcc(fe->pixel_format);
139105e5d44bSHans Verkuil 	if (fmt == NULL)
139205e5d44bSHans Verkuil 		return -EINVAL;
139305e5d44bSHans Verkuil 	fe->type = V4L2_FRMSIZE_TYPE_DISCRETE;
139405e5d44bSHans Verkuil 	fe->discrete = is_ntsc ?  ntsc_sizes[fe->index] : pal_sizes[fe->index];
139505e5d44bSHans Verkuil 	return 0;
139605e5d44bSHans Verkuil }
139705e5d44bSHans Verkuil 
13980aa77f6cSMauro Carvalho Chehab static int vidioc_enum_frameintervals(struct file *file, void *priv,
13990aa77f6cSMauro Carvalho Chehab 			    struct v4l2_frmivalenum *fe)
14000aa77f6cSMauro Carvalho Chehab {
1401340a30c5Ssensoray-dev 	struct s2255_vc *vc = video_drvdata(file);
140205e5d44bSHans Verkuil 	const struct s2255_fmt *fmt;
140305e5d44bSHans Verkuil 	const struct v4l2_frmsize_discrete *sizes;
14045e950fafSDean Anderson 	int is_ntsc = vc->std & V4L2_STD_525_60;
14050aa77f6cSMauro Carvalho Chehab #define NUM_FRAME_ENUMS 4
14060aa77f6cSMauro Carvalho Chehab 	int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
140705e5d44bSHans Verkuil 	int i;
140805e5d44bSHans Verkuil 
1409199ab8feSMauro Carvalho Chehab 	if (fe->index >= NUM_FRAME_ENUMS)
14100aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
141105e5d44bSHans Verkuil 
141205e5d44bSHans Verkuil 	fmt = format_by_fourcc(fe->pixel_format);
141305e5d44bSHans Verkuil 	if (fmt == NULL)
14140aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
141505e5d44bSHans Verkuil 
141605e5d44bSHans Verkuil 	sizes = is_ntsc ? ntsc_sizes : pal_sizes;
141705e5d44bSHans Verkuil 	for (i = 0; i < NUM_SIZE_ENUMS; i++, sizes++)
141805e5d44bSHans Verkuil 		if (fe->width == sizes->width &&
141905e5d44bSHans Verkuil 		    fe->height == sizes->height)
14200aa77f6cSMauro Carvalho Chehab 			break;
142105e5d44bSHans Verkuil 	if (i == NUM_SIZE_ENUMS)
14220aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
142305e5d44bSHans Verkuil 
14240aa77f6cSMauro Carvalho Chehab 	fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
14250aa77f6cSMauro Carvalho Chehab 	fe->discrete.denominator = is_ntsc ? 30000 : 25000;
14260aa77f6cSMauro Carvalho Chehab 	fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
142792cde477SDean Anderson 	dprintk(vc->dev, 4, "%s discrete %d/%d\n", __func__,
1428f5402007Ssensoray-dev 		fe->discrete.numerator,
14290aa77f6cSMauro Carvalho Chehab 		fe->discrete.denominator);
14300aa77f6cSMauro Carvalho Chehab 	return 0;
14310aa77f6cSMauro Carvalho Chehab }
14320aa77f6cSMauro Carvalho Chehab 
1433340a30c5Ssensoray-dev static int s2255_open(struct file *file)
14340aa77f6cSMauro Carvalho Chehab {
14355e950fafSDean Anderson 	struct s2255_vc *vc = video_drvdata(file);
1436340a30c5Ssensoray-dev 	struct s2255_dev *dev = vc->dev;
14370aa77f6cSMauro Carvalho Chehab 	int state;
1438340a30c5Ssensoray-dev 	int rc = 0;
1439340a30c5Ssensoray-dev 
1440340a30c5Ssensoray-dev 	rc = v4l2_fh_open(file);
1441340a30c5Ssensoray-dev 	if (rc != 0)
1442340a30c5Ssensoray-dev 		return rc;
1443340a30c5Ssensoray-dev 
1444340a30c5Ssensoray-dev 	dprintk(dev, 1, "s2255: %s\n", __func__);
14450aa77f6cSMauro Carvalho Chehab 	state = atomic_read(&dev->fw_data->fw_state);
14460aa77f6cSMauro Carvalho Chehab 	switch (state) {
14470aa77f6cSMauro Carvalho Chehab 	case S2255_FW_DISCONNECTING:
14480aa77f6cSMauro Carvalho Chehab 		return -ENODEV;
14490aa77f6cSMauro Carvalho Chehab 	case S2255_FW_FAILED:
14500aa77f6cSMauro Carvalho Chehab 		s2255_dev_err(&dev->udev->dev,
14510aa77f6cSMauro Carvalho Chehab 			"firmware load failed. retrying.\n");
14520aa77f6cSMauro Carvalho Chehab 		s2255_fwload_start(dev, 1);
14530aa77f6cSMauro Carvalho Chehab 		wait_event_timeout(dev->fw_data->wait_fw,
14540aa77f6cSMauro Carvalho Chehab 				   ((atomic_read(&dev->fw_data->fw_state)
14550aa77f6cSMauro Carvalho Chehab 				     == S2255_FW_SUCCESS) ||
14560aa77f6cSMauro Carvalho Chehab 				    (atomic_read(&dev->fw_data->fw_state)
14570aa77f6cSMauro Carvalho Chehab 				     == S2255_FW_DISCONNECTING)),
14580aa77f6cSMauro Carvalho Chehab 				   msecs_to_jiffies(S2255_LOAD_TIMEOUT));
14590aa77f6cSMauro Carvalho Chehab 		/* state may have changed, re-read */
14600aa77f6cSMauro Carvalho Chehab 		state = atomic_read(&dev->fw_data->fw_state);
14610aa77f6cSMauro Carvalho Chehab 		break;
14620aa77f6cSMauro Carvalho Chehab 	case S2255_FW_NOTLOADED:
14630aa77f6cSMauro Carvalho Chehab 	case S2255_FW_LOADED_DSPWAIT:
14640aa77f6cSMauro Carvalho Chehab 		/* give S2255_LOAD_TIMEOUT time for firmware to load in case
14650aa77f6cSMauro Carvalho Chehab 		   driver loaded and then device immediately opened */
1466f5402007Ssensoray-dev 		pr_info("%s waiting for firmware load\n", __func__);
14670aa77f6cSMauro Carvalho Chehab 		wait_event_timeout(dev->fw_data->wait_fw,
14680aa77f6cSMauro Carvalho Chehab 				   ((atomic_read(&dev->fw_data->fw_state)
14690aa77f6cSMauro Carvalho Chehab 				     == S2255_FW_SUCCESS) ||
14700aa77f6cSMauro Carvalho Chehab 				    (atomic_read(&dev->fw_data->fw_state)
14710aa77f6cSMauro Carvalho Chehab 				     == S2255_FW_DISCONNECTING)),
14720aa77f6cSMauro Carvalho Chehab 				   msecs_to_jiffies(S2255_LOAD_TIMEOUT));
14730aa77f6cSMauro Carvalho Chehab 		/* state may have changed, re-read */
14740aa77f6cSMauro Carvalho Chehab 		state = atomic_read(&dev->fw_data->fw_state);
14750aa77f6cSMauro Carvalho Chehab 		break;
14760aa77f6cSMauro Carvalho Chehab 	case S2255_FW_SUCCESS:
14770aa77f6cSMauro Carvalho Chehab 	default:
14780aa77f6cSMauro Carvalho Chehab 		break;
14790aa77f6cSMauro Carvalho Chehab 	}
14800aa77f6cSMauro Carvalho Chehab 	/* state may have changed in above switch statement */
14810aa77f6cSMauro Carvalho Chehab 	switch (state) {
14820aa77f6cSMauro Carvalho Chehab 	case S2255_FW_SUCCESS:
14830aa77f6cSMauro Carvalho Chehab 		break;
14840aa77f6cSMauro Carvalho Chehab 	case S2255_FW_FAILED:
1485f5402007Ssensoray-dev 		pr_info("2255 firmware load failed.\n");
14860aa77f6cSMauro Carvalho Chehab 		return -ENODEV;
14870aa77f6cSMauro Carvalho Chehab 	case S2255_FW_DISCONNECTING:
1488f5402007Ssensoray-dev 		pr_info("%s: disconnecting\n", __func__);
14890aa77f6cSMauro Carvalho Chehab 		return -ENODEV;
14900aa77f6cSMauro Carvalho Chehab 	case S2255_FW_LOADED_DSPWAIT:
14910aa77f6cSMauro Carvalho Chehab 	case S2255_FW_NOTLOADED:
1492f5402007Ssensoray-dev 		pr_info("%s: firmware not loaded, please retry\n",
14930aa77f6cSMauro Carvalho Chehab 			__func__);
14940aa77f6cSMauro Carvalho Chehab 		/*
14950aa77f6cSMauro Carvalho Chehab 		 * Timeout on firmware load means device unusable.
14960aa77f6cSMauro Carvalho Chehab 		 * Set firmware failure state.
14970aa77f6cSMauro Carvalho Chehab 		 * On next s2255_open the firmware will be reloaded.
14980aa77f6cSMauro Carvalho Chehab 		 */
14990aa77f6cSMauro Carvalho Chehab 		atomic_set(&dev->fw_data->fw_state,
15000aa77f6cSMauro Carvalho Chehab 			   S2255_FW_FAILED);
15010aa77f6cSMauro Carvalho Chehab 		return -EAGAIN;
15020aa77f6cSMauro Carvalho Chehab 	default:
1503f5402007Ssensoray-dev 		pr_info("%s: unknown state\n", __func__);
15040aa77f6cSMauro Carvalho Chehab 		return -EFAULT;
15050aa77f6cSMauro Carvalho Chehab 	}
15065e950fafSDean Anderson 	if (!vc->configured) {
15070aa77f6cSMauro Carvalho Chehab 		/* configure channel to default state */
15085e950fafSDean Anderson 		vc->fmt = &formats[0];
15095e950fafSDean Anderson 		s2255_set_mode(vc, &vc->mode);
15105e950fafSDean Anderson 		vc->configured = 1;
15110aa77f6cSMauro Carvalho Chehab 	}
15120aa77f6cSMauro Carvalho Chehab 	return 0;
15130aa77f6cSMauro Carvalho Chehab }
15140aa77f6cSMauro Carvalho Chehab 
15150aa77f6cSMauro Carvalho Chehab static void s2255_destroy(struct s2255_dev *dev)
15160aa77f6cSMauro Carvalho Chehab {
1517f5402007Ssensoray-dev 	dprintk(dev, 1, "%s", __func__);
15180aa77f6cSMauro Carvalho Chehab 	/* board shutdown stops the read pipe if it is running */
15190aa77f6cSMauro Carvalho Chehab 	s2255_board_shutdown(dev);
15200aa77f6cSMauro Carvalho Chehab 	/* make sure firmware still not trying to load */
15219f6be2bcSKirill Tkhai 	del_timer_sync(&dev->timer);  /* only started in .probe and .open */
15220aa77f6cSMauro Carvalho Chehab 	if (dev->fw_data->fw_urb) {
15230aa77f6cSMauro Carvalho Chehab 		usb_kill_urb(dev->fw_data->fw_urb);
15240aa77f6cSMauro Carvalho Chehab 		usb_free_urb(dev->fw_data->fw_urb);
15250aa77f6cSMauro Carvalho Chehab 		dev->fw_data->fw_urb = NULL;
15260aa77f6cSMauro Carvalho Chehab 	}
15270aa77f6cSMauro Carvalho Chehab 	release_firmware(dev->fw_data->fw);
15280aa77f6cSMauro Carvalho Chehab 	kfree(dev->fw_data->pfw_data);
15290aa77f6cSMauro Carvalho Chehab 	kfree(dev->fw_data);
15300aa77f6cSMauro Carvalho Chehab 	/* reset the DSP so firmware can be reloaded next time */
15310aa77f6cSMauro Carvalho Chehab 	s2255_reset_dsppower(dev);
15320aa77f6cSMauro Carvalho Chehab 	mutex_destroy(&dev->lock);
15330aa77f6cSMauro Carvalho Chehab 	usb_put_dev(dev->udev);
15340aa77f6cSMauro Carvalho Chehab 	v4l2_device_unregister(&dev->v4l2_dev);
153547d8c881SDean Anderson 	kfree(dev->cmdbuf);
15360aa77f6cSMauro Carvalho Chehab 	kfree(dev);
15370aa77f6cSMauro Carvalho Chehab }
15380aa77f6cSMauro Carvalho Chehab 
15390aa77f6cSMauro Carvalho Chehab static const struct v4l2_file_operations s2255_fops_v4l = {
15400aa77f6cSMauro Carvalho Chehab 	.owner = THIS_MODULE,
15410aa77f6cSMauro Carvalho Chehab 	.open = s2255_open,
1542340a30c5Ssensoray-dev 	.release = vb2_fop_release,
1543340a30c5Ssensoray-dev 	.poll = vb2_fop_poll,
15440aa77f6cSMauro Carvalho Chehab 	.unlocked_ioctl = video_ioctl2,	/* V4L2 ioctl handler */
1545340a30c5Ssensoray-dev 	.mmap = vb2_fop_mmap,
1546340a30c5Ssensoray-dev 	.read = vb2_fop_read,
15470aa77f6cSMauro Carvalho Chehab };
15480aa77f6cSMauro Carvalho Chehab 
15490aa77f6cSMauro Carvalho Chehab static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
15500aa77f6cSMauro Carvalho Chehab 	.vidioc_querycap = vidioc_querycap,
15510aa77f6cSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
15520aa77f6cSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
15530aa77f6cSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
15540aa77f6cSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1555340a30c5Ssensoray-dev 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
1556340a30c5Ssensoray-dev 	.vidioc_querybuf = vb2_ioctl_querybuf,
1557340a30c5Ssensoray-dev 	.vidioc_qbuf = vb2_ioctl_qbuf,
1558340a30c5Ssensoray-dev 	.vidioc_dqbuf = vb2_ioctl_dqbuf,
15590aa77f6cSMauro Carvalho Chehab 	.vidioc_s_std = vidioc_s_std,
1560469af77aSHans Verkuil 	.vidioc_g_std = vidioc_g_std,
15610aa77f6cSMauro Carvalho Chehab 	.vidioc_enum_input = vidioc_enum_input,
15620aa77f6cSMauro Carvalho Chehab 	.vidioc_g_input = vidioc_g_input,
15630aa77f6cSMauro Carvalho Chehab 	.vidioc_s_input = vidioc_s_input,
1564340a30c5Ssensoray-dev 	.vidioc_streamon = vb2_ioctl_streamon,
1565340a30c5Ssensoray-dev 	.vidioc_streamoff = vb2_ioctl_streamoff,
15660aa77f6cSMauro Carvalho Chehab 	.vidioc_s_jpegcomp = vidioc_s_jpegcomp,
15670aa77f6cSMauro Carvalho Chehab 	.vidioc_g_jpegcomp = vidioc_g_jpegcomp,
15680aa77f6cSMauro Carvalho Chehab 	.vidioc_s_parm = vidioc_s_parm,
15690aa77f6cSMauro Carvalho Chehab 	.vidioc_g_parm = vidioc_g_parm,
157005e5d44bSHans Verkuil 	.vidioc_enum_framesizes = vidioc_enum_framesizes,
15710aa77f6cSMauro Carvalho Chehab 	.vidioc_enum_frameintervals = vidioc_enum_frameintervals,
157244d06d82SHans Verkuil 	.vidioc_log_status  = v4l2_ctrl_log_status,
157344d06d82SHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
157444d06d82SHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
15750aa77f6cSMauro Carvalho Chehab };
15760aa77f6cSMauro Carvalho Chehab 
15770aa77f6cSMauro Carvalho Chehab static void s2255_video_device_release(struct video_device *vdev)
15780aa77f6cSMauro Carvalho Chehab {
15790aa77f6cSMauro Carvalho Chehab 	struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
15805e950fafSDean Anderson 	struct s2255_vc *vc =
15815e950fafSDean Anderson 		container_of(vdev, struct s2255_vc, vdev);
1582192f1e78SHans Verkuil 
1583f5402007Ssensoray-dev 	dprintk(dev, 4, "%s, chnls: %d\n", __func__,
15840aa77f6cSMauro Carvalho Chehab 		atomic_read(&dev->num_channels));
1585192f1e78SHans Verkuil 
15865e950fafSDean Anderson 	v4l2_ctrl_handler_free(&vc->hdl);
1587f5402007Ssensoray-dev 
15880aa77f6cSMauro Carvalho Chehab 	if (atomic_dec_and_test(&dev->num_channels))
15890aa77f6cSMauro Carvalho Chehab 		s2255_destroy(dev);
15900aa77f6cSMauro Carvalho Chehab 	return;
15910aa77f6cSMauro Carvalho Chehab }
15920aa77f6cSMauro Carvalho Chehab 
15930aa77f6cSMauro Carvalho Chehab static struct video_device template = {
15940aa77f6cSMauro Carvalho Chehab 	.name = "s2255v",
15950aa77f6cSMauro Carvalho Chehab 	.fops = &s2255_fops_v4l,
15960aa77f6cSMauro Carvalho Chehab 	.ioctl_ops = &s2255_ioctl_ops,
15970aa77f6cSMauro Carvalho Chehab 	.release = s2255_video_device_release,
15980aa77f6cSMauro Carvalho Chehab 	.tvnorms = S2255_NORMS,
15990aa77f6cSMauro Carvalho Chehab };
16000aa77f6cSMauro Carvalho Chehab 
1601192f1e78SHans Verkuil static const struct v4l2_ctrl_ops s2255_ctrl_ops = {
1602192f1e78SHans Verkuil 	.s_ctrl = s2255_s_ctrl,
1603192f1e78SHans Verkuil };
1604192f1e78SHans Verkuil 
1605192f1e78SHans Verkuil static const struct v4l2_ctrl_config color_filter_ctrl = {
1606192f1e78SHans Verkuil 	.ops = &s2255_ctrl_ops,
1607192f1e78SHans Verkuil 	.name = "Color Filter",
1608192f1e78SHans Verkuil 	.id = V4L2_CID_S2255_COLORFILTER,
1609192f1e78SHans Verkuil 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1610192f1e78SHans Verkuil 	.max = 1,
1611192f1e78SHans Verkuil 	.step = 1,
1612192f1e78SHans Verkuil 	.def = 1,
1613192f1e78SHans Verkuil };
1614192f1e78SHans Verkuil 
16150aa77f6cSMauro Carvalho Chehab static int s2255_probe_v4l(struct s2255_dev *dev)
16160aa77f6cSMauro Carvalho Chehab {
16170aa77f6cSMauro Carvalho Chehab 	int ret;
16180aa77f6cSMauro Carvalho Chehab 	int i;
16190aa77f6cSMauro Carvalho Chehab 	int cur_nr = video_nr;
16205e950fafSDean Anderson 	struct s2255_vc *vc;
1621340a30c5Ssensoray-dev 	struct vb2_queue *q;
1622340a30c5Ssensoray-dev 
16230aa77f6cSMauro Carvalho Chehab 	ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
16240aa77f6cSMauro Carvalho Chehab 	if (ret)
16250aa77f6cSMauro Carvalho Chehab 		return ret;
16260aa77f6cSMauro Carvalho Chehab 	/* initialize all video 4 linux */
16270aa77f6cSMauro Carvalho Chehab 	/* register 4 video devices */
16280aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < MAX_CHANNELS; i++) {
16295e950fafSDean Anderson 		vc = &dev->vc[i];
16305e950fafSDean Anderson 		INIT_LIST_HEAD(&vc->buf_list);
1631192f1e78SHans Verkuil 
16325e950fafSDean Anderson 		v4l2_ctrl_handler_init(&vc->hdl, 6);
16335e950fafSDean Anderson 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1634192f1e78SHans Verkuil 				V4L2_CID_BRIGHTNESS, -127, 127, 1, DEF_BRIGHT);
16355e950fafSDean Anderson 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1636192f1e78SHans Verkuil 				V4L2_CID_CONTRAST, 0, 255, 1, DEF_CONTRAST);
16375e950fafSDean Anderson 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1638192f1e78SHans Verkuil 				V4L2_CID_SATURATION, 0, 255, 1, DEF_SATURATION);
16395e950fafSDean Anderson 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1640192f1e78SHans Verkuil 				V4L2_CID_HUE, 0, 255, 1, DEF_HUE);
16415e950fafSDean Anderson 		vc->jpegqual_ctrl = v4l2_ctrl_new_std(&vc->hdl,
16427041dec7SHans Verkuil 				&s2255_ctrl_ops,
16437041dec7SHans Verkuil 				V4L2_CID_JPEG_COMPRESSION_QUALITY,
16447041dec7SHans Verkuil 				0, 100, 1, S2255_DEF_JPEG_QUAL);
1645192f1e78SHans Verkuil 		if (dev->dsp_fw_ver >= S2255_MIN_DSP_COLORFILTER &&
16465e950fafSDean Anderson 		    (dev->pid != 0x2257 || vc->idx <= 1))
16475e950fafSDean Anderson 			v4l2_ctrl_new_custom(&vc->hdl, &color_filter_ctrl,
1648f5402007Ssensoray-dev 					     NULL);
16495e950fafSDean Anderson 		if (vc->hdl.error) {
16505e950fafSDean Anderson 			ret = vc->hdl.error;
16515e950fafSDean Anderson 			v4l2_ctrl_handler_free(&vc->hdl);
1652192f1e78SHans Verkuil 			dev_err(&dev->udev->dev, "couldn't register control\n");
1653192f1e78SHans Verkuil 			break;
1654192f1e78SHans Verkuil 		}
1655340a30c5Ssensoray-dev 		q = &vc->vb_vidq;
1656340a30c5Ssensoray-dev 		q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1657340a30c5Ssensoray-dev 		q->io_modes = VB2_MMAP | VB2_READ | VB2_USERPTR;
1658340a30c5Ssensoray-dev 		q->drv_priv = vc;
1659340a30c5Ssensoray-dev 		q->lock = &vc->vb_lock;
1660340a30c5Ssensoray-dev 		q->buf_struct_size = sizeof(struct s2255_buffer);
1661340a30c5Ssensoray-dev 		q->mem_ops = &vb2_vmalloc_memops;
1662340a30c5Ssensoray-dev 		q->ops = &s2255_video_qops;
1663ade48681SSakari Ailus 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1664340a30c5Ssensoray-dev 		ret = vb2_queue_init(q);
1665340a30c5Ssensoray-dev 		if (ret != 0) {
1666340a30c5Ssensoray-dev 			dev_err(&dev->udev->dev,
1667340a30c5Ssensoray-dev 				"%s vb2_queue_init 0x%x\n", __func__, ret);
1668340a30c5Ssensoray-dev 			break;
1669340a30c5Ssensoray-dev 		}
1670340a30c5Ssensoray-dev 		/* register video devices */
16715e950fafSDean Anderson 		vc->vdev = template;
1672340a30c5Ssensoray-dev 		vc->vdev.queue = q;
16735e950fafSDean Anderson 		vc->vdev.ctrl_handler = &vc->hdl;
16745e950fafSDean Anderson 		vc->vdev.lock = &dev->lock;
16755e950fafSDean Anderson 		vc->vdev.v4l2_dev = &dev->v4l2_dev;
16765e950fafSDean Anderson 		video_set_drvdata(&vc->vdev, vc);
16770aa77f6cSMauro Carvalho Chehab 		if (video_nr == -1)
16785e950fafSDean Anderson 			ret = video_register_device(&vc->vdev,
16790aa77f6cSMauro Carvalho Chehab 						    VFL_TYPE_GRABBER,
16800aa77f6cSMauro Carvalho Chehab 						    video_nr);
16810aa77f6cSMauro Carvalho Chehab 		else
16825e950fafSDean Anderson 			ret = video_register_device(&vc->vdev,
16830aa77f6cSMauro Carvalho Chehab 						    VFL_TYPE_GRABBER,
16840aa77f6cSMauro Carvalho Chehab 						    cur_nr + i);
16850aa77f6cSMauro Carvalho Chehab 
16860aa77f6cSMauro Carvalho Chehab 		if (ret) {
16870aa77f6cSMauro Carvalho Chehab 			dev_err(&dev->udev->dev,
16880aa77f6cSMauro Carvalho Chehab 				"failed to register video device!\n");
16890aa77f6cSMauro Carvalho Chehab 			break;
16900aa77f6cSMauro Carvalho Chehab 		}
16910aa77f6cSMauro Carvalho Chehab 		atomic_inc(&dev->num_channels);
16920aa77f6cSMauro Carvalho Chehab 		v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
16935e950fafSDean Anderson 			  video_device_node_name(&vc->vdev));
16940aa77f6cSMauro Carvalho Chehab 
16950aa77f6cSMauro Carvalho Chehab 	}
1696f5402007Ssensoray-dev 	pr_info("Sensoray 2255 V4L driver Revision: %s\n",
16970aa77f6cSMauro Carvalho Chehab 		S2255_VERSION);
16980aa77f6cSMauro Carvalho Chehab 	/* if no channels registered, return error and probe will fail*/
16990aa77f6cSMauro Carvalho Chehab 	if (atomic_read(&dev->num_channels) == 0) {
17000aa77f6cSMauro Carvalho Chehab 		v4l2_device_unregister(&dev->v4l2_dev);
17010aa77f6cSMauro Carvalho Chehab 		return ret;
17020aa77f6cSMauro Carvalho Chehab 	}
17030aa77f6cSMauro Carvalho Chehab 	if (atomic_read(&dev->num_channels) != MAX_CHANNELS)
1704f5402007Ssensoray-dev 		pr_warn("s2255: Not all channels available.\n");
17050aa77f6cSMauro Carvalho Chehab 	return 0;
17060aa77f6cSMauro Carvalho Chehab }
17070aa77f6cSMauro Carvalho Chehab 
17080aa77f6cSMauro Carvalho Chehab /* this function moves the usb stream read pipe data
17090aa77f6cSMauro Carvalho Chehab  * into the system buffers.
17100aa77f6cSMauro Carvalho Chehab  * returns 0 on success, EAGAIN if more data to process( call this
17110aa77f6cSMauro Carvalho Chehab  * function again).
17120aa77f6cSMauro Carvalho Chehab  *
17130aa77f6cSMauro Carvalho Chehab  * Received frame structure:
17140aa77f6cSMauro Carvalho Chehab  * bytes 0-3:  marker : 0x2255DA4AL (S2255_MARKER_FRAME)
17150aa77f6cSMauro Carvalho Chehab  * bytes 4-7:  channel: 0-3
17160aa77f6cSMauro Carvalho Chehab  * bytes 8-11: payload size:  size of the frame
17170aa77f6cSMauro Carvalho Chehab  * bytes 12-payloadsize+12:  frame data
17180aa77f6cSMauro Carvalho Chehab  */
17190aa77f6cSMauro Carvalho Chehab static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
17200aa77f6cSMauro Carvalho Chehab {
17210aa77f6cSMauro Carvalho Chehab 	char *pdest;
17220aa77f6cSMauro Carvalho Chehab 	u32 offset = 0;
17230aa77f6cSMauro Carvalho Chehab 	int bframe = 0;
17240aa77f6cSMauro Carvalho Chehab 	char *psrc;
17250aa77f6cSMauro Carvalho Chehab 	unsigned long copy_size;
17260aa77f6cSMauro Carvalho Chehab 	unsigned long size;
17270aa77f6cSMauro Carvalho Chehab 	s32 idx = -1;
17280aa77f6cSMauro Carvalho Chehab 	struct s2255_framei *frm;
17290aa77f6cSMauro Carvalho Chehab 	unsigned char *pdata;
17305e950fafSDean Anderson 	struct s2255_vc *vc;
1731f5402007Ssensoray-dev 	dprintk(dev, 100, "buffer to user\n");
17325e950fafSDean Anderson 	vc = &dev->vc[dev->cc];
17335e950fafSDean Anderson 	idx = vc->cur_frame;
17345e950fafSDean Anderson 	frm = &vc->buffer.frame[idx];
17350aa77f6cSMauro Carvalho Chehab 	if (frm->ulState == S2255_READ_IDLE) {
17360aa77f6cSMauro Carvalho Chehab 		int jj;
17370aa77f6cSMauro Carvalho Chehab 		unsigned int cc;
17380aa77f6cSMauro Carvalho Chehab 		__le32 *pdword; /*data from dsp is little endian */
17390aa77f6cSMauro Carvalho Chehab 		int payload;
17400aa77f6cSMauro Carvalho Chehab 		/* search for marker codes */
17410aa77f6cSMauro Carvalho Chehab 		pdata = (unsigned char *)pipe_info->transfer_buffer;
17420aa77f6cSMauro Carvalho Chehab 		pdword = (__le32 *)pdata;
17430aa77f6cSMauro Carvalho Chehab 		for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
17440aa77f6cSMauro Carvalho Chehab 			switch (*pdword) {
17450aa77f6cSMauro Carvalho Chehab 			case S2255_MARKER_FRAME:
1746f5402007Ssensoray-dev 				dprintk(dev, 4, "marker @ offset: %d [%x %x]\n",
1747f5402007Ssensoray-dev 					jj, pdata[0], pdata[1]);
17480aa77f6cSMauro Carvalho Chehab 				offset = jj + PREFIX_SIZE;
17490aa77f6cSMauro Carvalho Chehab 				bframe = 1;
17500aa77f6cSMauro Carvalho Chehab 				cc = le32_to_cpu(pdword[1]);
17510aa77f6cSMauro Carvalho Chehab 				if (cc >= MAX_CHANNELS) {
1752f5402007Ssensoray-dev 					dprintk(dev, 0,
17530aa77f6cSMauro Carvalho Chehab 						"bad channel\n");
17540aa77f6cSMauro Carvalho Chehab 					return -EINVAL;
17550aa77f6cSMauro Carvalho Chehab 				}
17560aa77f6cSMauro Carvalho Chehab 				/* reverse it */
17570aa77f6cSMauro Carvalho Chehab 				dev->cc = G_chnmap[cc];
17585e950fafSDean Anderson 				vc = &dev->vc[dev->cc];
17590aa77f6cSMauro Carvalho Chehab 				payload =  le32_to_cpu(pdword[3]);
17605e950fafSDean Anderson 				if (payload > vc->req_image_size) {
17615e950fafSDean Anderson 					vc->bad_payload++;
17620aa77f6cSMauro Carvalho Chehab 					/* discard the bad frame */
17630aa77f6cSMauro Carvalho Chehab 					return -EINVAL;
17640aa77f6cSMauro Carvalho Chehab 				}
17655e950fafSDean Anderson 				vc->pkt_size = payload;
17665e950fafSDean Anderson 				vc->jpg_size = le32_to_cpu(pdword[4]);
17670aa77f6cSMauro Carvalho Chehab 				break;
17680aa77f6cSMauro Carvalho Chehab 			case S2255_MARKER_RESPONSE:
17690aa77f6cSMauro Carvalho Chehab 
17700aa77f6cSMauro Carvalho Chehab 				pdata += DEF_USB_BLOCK;
17710aa77f6cSMauro Carvalho Chehab 				jj += DEF_USB_BLOCK;
17720aa77f6cSMauro Carvalho Chehab 				if (le32_to_cpu(pdword[1]) >= MAX_CHANNELS)
17730aa77f6cSMauro Carvalho Chehab 					break;
17740aa77f6cSMauro Carvalho Chehab 				cc = G_chnmap[le32_to_cpu(pdword[1])];
17750aa77f6cSMauro Carvalho Chehab 				if (cc >= MAX_CHANNELS)
17760aa77f6cSMauro Carvalho Chehab 					break;
17775e950fafSDean Anderson 				vc = &dev->vc[cc];
17780aa77f6cSMauro Carvalho Chehab 				switch (pdword[2]) {
17790aa77f6cSMauro Carvalho Chehab 				case S2255_RESPONSE_SETMODE:
17800aa77f6cSMauro Carvalho Chehab 					/* check if channel valid */
17810aa77f6cSMauro Carvalho Chehab 					/* set mode ready */
17825e950fafSDean Anderson 					vc->setmode_ready = 1;
17835e950fafSDean Anderson 					wake_up(&vc->wait_setmode);
1784f5402007Ssensoray-dev 					dprintk(dev, 5, "setmode rdy %d\n", cc);
17850aa77f6cSMauro Carvalho Chehab 					break;
17860aa77f6cSMauro Carvalho Chehab 				case S2255_RESPONSE_FW:
17870aa77f6cSMauro Carvalho Chehab 					dev->chn_ready |= (1 << cc);
17880aa77f6cSMauro Carvalho Chehab 					if ((dev->chn_ready & 0x0f) != 0x0f)
17890aa77f6cSMauro Carvalho Chehab 						break;
17900aa77f6cSMauro Carvalho Chehab 					/* all channels ready */
1791f5402007Ssensoray-dev 					pr_info("s2255: fw loaded\n");
17920aa77f6cSMauro Carvalho Chehab 					atomic_set(&dev->fw_data->fw_state,
17930aa77f6cSMauro Carvalho Chehab 						   S2255_FW_SUCCESS);
17940aa77f6cSMauro Carvalho Chehab 					wake_up(&dev->fw_data->wait_fw);
17950aa77f6cSMauro Carvalho Chehab 					break;
17960aa77f6cSMauro Carvalho Chehab 				case S2255_RESPONSE_STATUS:
17975e950fafSDean Anderson 					vc->vidstatus = le32_to_cpu(pdword[3]);
17985e950fafSDean Anderson 					vc->vidstatus_ready = 1;
17995e950fafSDean Anderson 					wake_up(&vc->wait_vidstatus);
1800f5402007Ssensoray-dev 					dprintk(dev, 5, "vstat %x chan %d\n",
18010aa77f6cSMauro Carvalho Chehab 						le32_to_cpu(pdword[3]), cc);
18020aa77f6cSMauro Carvalho Chehab 					break;
18030aa77f6cSMauro Carvalho Chehab 				default:
1804f5402007Ssensoray-dev 					pr_info("s2255 unknown resp\n");
18050aa77f6cSMauro Carvalho Chehab 				}
1806ec33fbd5SMauro Carvalho Chehab 				pdata++;
1807ec33fbd5SMauro Carvalho Chehab 				break;
18080aa77f6cSMauro Carvalho Chehab 			default:
18090aa77f6cSMauro Carvalho Chehab 				pdata++;
18100aa77f6cSMauro Carvalho Chehab 				break;
18110aa77f6cSMauro Carvalho Chehab 			}
18120aa77f6cSMauro Carvalho Chehab 			if (bframe)
18130aa77f6cSMauro Carvalho Chehab 				break;
18140aa77f6cSMauro Carvalho Chehab 		} /* for */
18150aa77f6cSMauro Carvalho Chehab 		if (!bframe)
18160aa77f6cSMauro Carvalho Chehab 			return -EINVAL;
18170aa77f6cSMauro Carvalho Chehab 	}
18185e950fafSDean Anderson 	vc = &dev->vc[dev->cc];
18195e950fafSDean Anderson 	idx = vc->cur_frame;
18205e950fafSDean Anderson 	frm = &vc->buffer.frame[idx];
18210aa77f6cSMauro Carvalho Chehab 	/* search done.  now find out if should be acquiring on this channel */
1822340a30c5Ssensoray-dev 	if (!vb2_is_streaming(&vc->vb_vidq)) {
18230aa77f6cSMauro Carvalho Chehab 		/* we found a frame, but this channel is turned off */
18240aa77f6cSMauro Carvalho Chehab 		frm->ulState = S2255_READ_IDLE;
18250aa77f6cSMauro Carvalho Chehab 		return -EINVAL;
18260aa77f6cSMauro Carvalho Chehab 	}
18270aa77f6cSMauro Carvalho Chehab 
18280aa77f6cSMauro Carvalho Chehab 	if (frm->ulState == S2255_READ_IDLE) {
18290aa77f6cSMauro Carvalho Chehab 		frm->ulState = S2255_READ_FRAME;
18300aa77f6cSMauro Carvalho Chehab 		frm->cur_size = 0;
18310aa77f6cSMauro Carvalho Chehab 	}
18320aa77f6cSMauro Carvalho Chehab 
18330aa77f6cSMauro Carvalho Chehab 	/* skip the marker 512 bytes (and offset if out of sync) */
18340aa77f6cSMauro Carvalho Chehab 	psrc = (u8 *)pipe_info->transfer_buffer + offset;
18350aa77f6cSMauro Carvalho Chehab 
18360aa77f6cSMauro Carvalho Chehab 
18370aa77f6cSMauro Carvalho Chehab 	if (frm->lpvbits == NULL) {
1838f5402007Ssensoray-dev 		dprintk(dev, 1, "s2255 frame buffer == NULL.%p %p %d %d",
18390aa77f6cSMauro Carvalho Chehab 			frm, dev, dev->cc, idx);
18400aa77f6cSMauro Carvalho Chehab 		return -ENOMEM;
18410aa77f6cSMauro Carvalho Chehab 	}
18420aa77f6cSMauro Carvalho Chehab 
18430aa77f6cSMauro Carvalho Chehab 	pdest = frm->lpvbits + frm->cur_size;
18440aa77f6cSMauro Carvalho Chehab 
18450aa77f6cSMauro Carvalho Chehab 	copy_size = (pipe_info->cur_transfer_size - offset);
18460aa77f6cSMauro Carvalho Chehab 
18475e950fafSDean Anderson 	size = vc->pkt_size - PREFIX_SIZE;
18480aa77f6cSMauro Carvalho Chehab 
18490aa77f6cSMauro Carvalho Chehab 	/* sanity check on pdest */
18505e950fafSDean Anderson 	if ((copy_size + frm->cur_size) < vc->req_image_size)
18510aa77f6cSMauro Carvalho Chehab 		memcpy(pdest, psrc, copy_size);
18520aa77f6cSMauro Carvalho Chehab 
18530aa77f6cSMauro Carvalho Chehab 	frm->cur_size += copy_size;
1854f5402007Ssensoray-dev 	dprintk(dev, 4, "cur_size: %lu, size: %lu\n", frm->cur_size, size);
18550aa77f6cSMauro Carvalho Chehab 
18560aa77f6cSMauro Carvalho Chehab 	if (frm->cur_size >= size) {
1857f5402007Ssensoray-dev 		dprintk(dev, 2, "******[%d]Buffer[%d]full*******\n",
18580aa77f6cSMauro Carvalho Chehab 			dev->cc, idx);
18595e950fafSDean Anderson 		vc->last_frame = vc->cur_frame;
18605e950fafSDean Anderson 		vc->cur_frame++;
18610aa77f6cSMauro Carvalho Chehab 		/* end of system frame ring buffer, start at zero */
18625e950fafSDean Anderson 		if ((vc->cur_frame == SYS_FRAMES) ||
18635e950fafSDean Anderson 		    (vc->cur_frame == vc->buffer.dwFrames))
18645e950fafSDean Anderson 			vc->cur_frame = 0;
18650aa77f6cSMauro Carvalho Chehab 		/* frame ready */
1866340a30c5Ssensoray-dev 		if (vb2_is_streaming(&vc->vb_vidq))
18675e950fafSDean Anderson 			s2255_got_frame(vc, vc->jpg_size);
18685e950fafSDean Anderson 		vc->frame_count++;
18690aa77f6cSMauro Carvalho Chehab 		frm->ulState = S2255_READ_IDLE;
18700aa77f6cSMauro Carvalho Chehab 		frm->cur_size = 0;
18710aa77f6cSMauro Carvalho Chehab 
18720aa77f6cSMauro Carvalho Chehab 	}
18730aa77f6cSMauro Carvalho Chehab 	/* done successfully */
18740aa77f6cSMauro Carvalho Chehab 	return 0;
18750aa77f6cSMauro Carvalho Chehab }
18760aa77f6cSMauro Carvalho Chehab 
18770aa77f6cSMauro Carvalho Chehab static void s2255_read_video_callback(struct s2255_dev *dev,
18780aa77f6cSMauro Carvalho Chehab 				      struct s2255_pipeinfo *pipe_info)
18790aa77f6cSMauro Carvalho Chehab {
18800aa77f6cSMauro Carvalho Chehab 	int res;
1881f5402007Ssensoray-dev 	dprintk(dev, 50, "callback read video\n");
18820aa77f6cSMauro Carvalho Chehab 
18830aa77f6cSMauro Carvalho Chehab 	if (dev->cc >= MAX_CHANNELS) {
18840aa77f6cSMauro Carvalho Chehab 		dev->cc = 0;
18850aa77f6cSMauro Carvalho Chehab 		dev_err(&dev->udev->dev, "invalid channel\n");
18860aa77f6cSMauro Carvalho Chehab 		return;
18870aa77f6cSMauro Carvalho Chehab 	}
18880aa77f6cSMauro Carvalho Chehab 	/* otherwise copy to the system buffers */
18890aa77f6cSMauro Carvalho Chehab 	res = save_frame(dev, pipe_info);
18900aa77f6cSMauro Carvalho Chehab 	if (res != 0)
1891f5402007Ssensoray-dev 		dprintk(dev, 4, "s2255: read callback failed\n");
18920aa77f6cSMauro Carvalho Chehab 
1893f5402007Ssensoray-dev 	dprintk(dev, 50, "callback read video done\n");
18940aa77f6cSMauro Carvalho Chehab 	return;
18950aa77f6cSMauro Carvalho Chehab }
18960aa77f6cSMauro Carvalho Chehab 
18970aa77f6cSMauro Carvalho Chehab static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
18980aa77f6cSMauro Carvalho Chehab 			     u16 Index, u16 Value, void *TransferBuffer,
18990aa77f6cSMauro Carvalho Chehab 			     s32 TransferBufferLength, int bOut)
19000aa77f6cSMauro Carvalho Chehab {
19010aa77f6cSMauro Carvalho Chehab 	int r;
1902db65c49eSMauro Carvalho Chehab 	unsigned char *buf;
1903db65c49eSMauro Carvalho Chehab 
1904db65c49eSMauro Carvalho Chehab 	buf = kmalloc(TransferBufferLength, GFP_KERNEL);
1905db65c49eSMauro Carvalho Chehab 	if (!buf)
1906db65c49eSMauro Carvalho Chehab 		return -ENOMEM;
1907db65c49eSMauro Carvalho Chehab 
19080aa77f6cSMauro Carvalho Chehab 	if (!bOut) {
19090aa77f6cSMauro Carvalho Chehab 		r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
19100aa77f6cSMauro Carvalho Chehab 				    Request,
19110aa77f6cSMauro Carvalho Chehab 				    USB_TYPE_VENDOR | USB_RECIP_DEVICE |
19120aa77f6cSMauro Carvalho Chehab 				    USB_DIR_IN,
1913db65c49eSMauro Carvalho Chehab 				    Value, Index, buf,
19140aa77f6cSMauro Carvalho Chehab 				    TransferBufferLength, HZ * 5);
1915db65c49eSMauro Carvalho Chehab 
1916db65c49eSMauro Carvalho Chehab 		if (r >= 0)
1917db65c49eSMauro Carvalho Chehab 			memcpy(TransferBuffer, buf, TransferBufferLength);
19180aa77f6cSMauro Carvalho Chehab 	} else {
1919db65c49eSMauro Carvalho Chehab 		memcpy(buf, TransferBuffer, TransferBufferLength);
19200aa77f6cSMauro Carvalho Chehab 		r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
19210aa77f6cSMauro Carvalho Chehab 				    Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1922db65c49eSMauro Carvalho Chehab 				    Value, Index, buf,
19230aa77f6cSMauro Carvalho Chehab 				    TransferBufferLength, HZ * 5);
19240aa77f6cSMauro Carvalho Chehab 	}
1925db65c49eSMauro Carvalho Chehab 	kfree(buf);
19260aa77f6cSMauro Carvalho Chehab 	return r;
19270aa77f6cSMauro Carvalho Chehab }
19280aa77f6cSMauro Carvalho Chehab 
19290aa77f6cSMauro Carvalho Chehab /*
19300aa77f6cSMauro Carvalho Chehab  * retrieve FX2 firmware version. future use.
19310aa77f6cSMauro Carvalho Chehab  * @param dev pointer to device extension
19320aa77f6cSMauro Carvalho Chehab  * @return -1 for fail, else returns firmware version as an int(16 bits)
19330aa77f6cSMauro Carvalho Chehab  */
19340aa77f6cSMauro Carvalho Chehab static int s2255_get_fx2fw(struct s2255_dev *dev)
19350aa77f6cSMauro Carvalho Chehab {
19360aa77f6cSMauro Carvalho Chehab 	int fw;
19370aa77f6cSMauro Carvalho Chehab 	int ret;
19380aa77f6cSMauro Carvalho Chehab 	unsigned char transBuffer[64];
19390aa77f6cSMauro Carvalho Chehab 	ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
19400aa77f6cSMauro Carvalho Chehab 			       S2255_VR_IN);
19410aa77f6cSMauro Carvalho Chehab 	if (ret < 0)
1942f5402007Ssensoray-dev 		dprintk(dev, 2, "get fw error: %x\n", ret);
19430aa77f6cSMauro Carvalho Chehab 	fw = transBuffer[0] + (transBuffer[1] << 8);
1944f5402007Ssensoray-dev 	dprintk(dev, 2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
19450aa77f6cSMauro Carvalho Chehab 	return fw;
19460aa77f6cSMauro Carvalho Chehab }
19470aa77f6cSMauro Carvalho Chehab 
19480aa77f6cSMauro Carvalho Chehab /*
19490aa77f6cSMauro Carvalho Chehab  * Create the system ring buffer to copy frames into from the
19500aa77f6cSMauro Carvalho Chehab  * usb read pipe.
19510aa77f6cSMauro Carvalho Chehab  */
19525e950fafSDean Anderson static int s2255_create_sys_buffers(struct s2255_vc *vc)
19530aa77f6cSMauro Carvalho Chehab {
19540aa77f6cSMauro Carvalho Chehab 	unsigned long i;
19550aa77f6cSMauro Carvalho Chehab 	unsigned long reqsize;
19565e950fafSDean Anderson 	vc->buffer.dwFrames = SYS_FRAMES;
19570aa77f6cSMauro Carvalho Chehab 	/* always allocate maximum size(PAL) for system buffers */
19580aa77f6cSMauro Carvalho Chehab 	reqsize = SYS_FRAMES_MAXSIZE;
19590aa77f6cSMauro Carvalho Chehab 
19600aa77f6cSMauro Carvalho Chehab 	if (reqsize > SYS_FRAMES_MAXSIZE)
19610aa77f6cSMauro Carvalho Chehab 		reqsize = SYS_FRAMES_MAXSIZE;
19620aa77f6cSMauro Carvalho Chehab 
19630aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < SYS_FRAMES; i++) {
19640aa77f6cSMauro Carvalho Chehab 		/* allocate the frames */
19655e950fafSDean Anderson 		vc->buffer.frame[i].lpvbits = vmalloc(reqsize);
19665e950fafSDean Anderson 		vc->buffer.frame[i].size = reqsize;
19675e950fafSDean Anderson 		if (vc->buffer.frame[i].lpvbits == NULL) {
1968f5402007Ssensoray-dev 			pr_info("out of memory.  using less frames\n");
19695e950fafSDean Anderson 			vc->buffer.dwFrames = i;
19700aa77f6cSMauro Carvalho Chehab 			break;
19710aa77f6cSMauro Carvalho Chehab 		}
19720aa77f6cSMauro Carvalho Chehab 	}
19730aa77f6cSMauro Carvalho Chehab 
19740aa77f6cSMauro Carvalho Chehab 	/* make sure internal states are set */
19750aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < SYS_FRAMES; i++) {
19765e950fafSDean Anderson 		vc->buffer.frame[i].ulState = 0;
19775e950fafSDean Anderson 		vc->buffer.frame[i].cur_size = 0;
19780aa77f6cSMauro Carvalho Chehab 	}
19790aa77f6cSMauro Carvalho Chehab 
19805e950fafSDean Anderson 	vc->cur_frame = 0;
19815e950fafSDean Anderson 	vc->last_frame = -1;
19820aa77f6cSMauro Carvalho Chehab 	return 0;
19830aa77f6cSMauro Carvalho Chehab }
19840aa77f6cSMauro Carvalho Chehab 
19855e950fafSDean Anderson static int s2255_release_sys_buffers(struct s2255_vc *vc)
19860aa77f6cSMauro Carvalho Chehab {
19870aa77f6cSMauro Carvalho Chehab 	unsigned long i;
19880aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < SYS_FRAMES; i++) {
19895e950fafSDean Anderson 		vfree(vc->buffer.frame[i].lpvbits);
19905e950fafSDean Anderson 		vc->buffer.frame[i].lpvbits = NULL;
19910aa77f6cSMauro Carvalho Chehab 	}
19920aa77f6cSMauro Carvalho Chehab 	return 0;
19930aa77f6cSMauro Carvalho Chehab }
19940aa77f6cSMauro Carvalho Chehab 
19950aa77f6cSMauro Carvalho Chehab static int s2255_board_init(struct s2255_dev *dev)
19960aa77f6cSMauro Carvalho Chehab {
19970aa77f6cSMauro Carvalho Chehab 	struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
19980aa77f6cSMauro Carvalho Chehab 	int fw_ver;
19990aa77f6cSMauro Carvalho Chehab 	int j;
20000aa77f6cSMauro Carvalho Chehab 	struct s2255_pipeinfo *pipe = &dev->pipe;
2001f5402007Ssensoray-dev 	dprintk(dev, 4, "board init: %p", dev);
20020aa77f6cSMauro Carvalho Chehab 	memset(pipe, 0, sizeof(*pipe));
20030aa77f6cSMauro Carvalho Chehab 	pipe->dev = dev;
20040aa77f6cSMauro Carvalho Chehab 	pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
20050aa77f6cSMauro Carvalho Chehab 	pipe->max_transfer_size = S2255_USB_XFER_SIZE;
20060aa77f6cSMauro Carvalho Chehab 
20070aa77f6cSMauro Carvalho Chehab 	pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
20080aa77f6cSMauro Carvalho Chehab 					GFP_KERNEL);
20090aa77f6cSMauro Carvalho Chehab 	if (pipe->transfer_buffer == NULL) {
2010f5402007Ssensoray-dev 		dprintk(dev, 1, "out of memory!\n");
20110aa77f6cSMauro Carvalho Chehab 		return -ENOMEM;
20120aa77f6cSMauro Carvalho Chehab 	}
20130aa77f6cSMauro Carvalho Chehab 	/* query the firmware */
20140aa77f6cSMauro Carvalho Chehab 	fw_ver = s2255_get_fx2fw(dev);
20150aa77f6cSMauro Carvalho Chehab 
2016f5402007Ssensoray-dev 	pr_info("s2255: usb firmware version %d.%d\n",
20170aa77f6cSMauro Carvalho Chehab 		(fw_ver >> 8) & 0xff,
20180aa77f6cSMauro Carvalho Chehab 		fw_ver & 0xff);
20190aa77f6cSMauro Carvalho Chehab 
20200aa77f6cSMauro Carvalho Chehab 	if (fw_ver < S2255_CUR_USB_FWVER)
2021f5402007Ssensoray-dev 		pr_info("s2255: newer USB firmware available\n");
20220aa77f6cSMauro Carvalho Chehab 
20230aa77f6cSMauro Carvalho Chehab 	for (j = 0; j < MAX_CHANNELS; j++) {
20245e950fafSDean Anderson 		struct s2255_vc *vc = &dev->vc[j];
20255e950fafSDean Anderson 		vc->mode = mode_def;
20260aa77f6cSMauro Carvalho Chehab 		if (dev->pid == 0x2257 && j > 1)
20275e950fafSDean Anderson 			vc->mode.color |= (1 << 16);
20285e950fafSDean Anderson 		vc->jpegqual = S2255_DEF_JPEG_QUAL;
20295e950fafSDean Anderson 		vc->width = LINE_SZ_4CIFS_NTSC;
20305e950fafSDean Anderson 		vc->height = NUM_LINES_4CIFS_NTSC * 2;
20315e950fafSDean Anderson 		vc->std = V4L2_STD_NTSC_M;
20325e950fafSDean Anderson 		vc->fmt = &formats[0];
20335e950fafSDean Anderson 		vc->mode.restart = 1;
20345e950fafSDean Anderson 		vc->req_image_size = get_transfer_size(&mode_def);
20355e950fafSDean Anderson 		vc->frame_count = 0;
20360aa77f6cSMauro Carvalho Chehab 		/* create the system buffers */
20375e950fafSDean Anderson 		s2255_create_sys_buffers(vc);
20380aa77f6cSMauro Carvalho Chehab 	}
20390aa77f6cSMauro Carvalho Chehab 	/* start read pipe */
20400aa77f6cSMauro Carvalho Chehab 	s2255_start_readpipe(dev);
2041f5402007Ssensoray-dev 	dprintk(dev, 1, "%s: success\n", __func__);
20420aa77f6cSMauro Carvalho Chehab 	return 0;
20430aa77f6cSMauro Carvalho Chehab }
20440aa77f6cSMauro Carvalho Chehab 
20450aa77f6cSMauro Carvalho Chehab static int s2255_board_shutdown(struct s2255_dev *dev)
20460aa77f6cSMauro Carvalho Chehab {
20470aa77f6cSMauro Carvalho Chehab 	u32 i;
2048f5402007Ssensoray-dev 	dprintk(dev, 1, "%s: dev: %p", __func__,  dev);
20490aa77f6cSMauro Carvalho Chehab 
20500aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < MAX_CHANNELS; i++) {
2051340a30c5Ssensoray-dev 		if (vb2_is_streaming(&dev->vc[i].vb_vidq))
20525e950fafSDean Anderson 			s2255_stop_acquire(&dev->vc[i]);
20530aa77f6cSMauro Carvalho Chehab 	}
20540aa77f6cSMauro Carvalho Chehab 	s2255_stop_readpipe(dev);
20550aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < MAX_CHANNELS; i++)
20565e950fafSDean Anderson 		s2255_release_sys_buffers(&dev->vc[i]);
20570aa77f6cSMauro Carvalho Chehab 	/* release transfer buffer */
20580aa77f6cSMauro Carvalho Chehab 	kfree(dev->pipe.transfer_buffer);
20590aa77f6cSMauro Carvalho Chehab 	return 0;
20600aa77f6cSMauro Carvalho Chehab }
20610aa77f6cSMauro Carvalho Chehab 
20620aa77f6cSMauro Carvalho Chehab static void read_pipe_completion(struct urb *purb)
20630aa77f6cSMauro Carvalho Chehab {
20640aa77f6cSMauro Carvalho Chehab 	struct s2255_pipeinfo *pipe_info;
20650aa77f6cSMauro Carvalho Chehab 	struct s2255_dev *dev;
20660aa77f6cSMauro Carvalho Chehab 	int status;
20670aa77f6cSMauro Carvalho Chehab 	int pipe;
20680aa77f6cSMauro Carvalho Chehab 	pipe_info = purb->context;
20690aa77f6cSMauro Carvalho Chehab 	if (pipe_info == NULL) {
20700aa77f6cSMauro Carvalho Chehab 		dev_err(&purb->dev->dev, "no context!\n");
20710aa77f6cSMauro Carvalho Chehab 		return;
20720aa77f6cSMauro Carvalho Chehab 	}
20730aa77f6cSMauro Carvalho Chehab 	dev = pipe_info->dev;
20740aa77f6cSMauro Carvalho Chehab 	if (dev == NULL) {
20750aa77f6cSMauro Carvalho Chehab 		dev_err(&purb->dev->dev, "no context!\n");
20760aa77f6cSMauro Carvalho Chehab 		return;
20770aa77f6cSMauro Carvalho Chehab 	}
20780aa77f6cSMauro Carvalho Chehab 	status = purb->status;
20790aa77f6cSMauro Carvalho Chehab 	/* if shutting down, do not resubmit, exit immediately */
20800aa77f6cSMauro Carvalho Chehab 	if (status == -ESHUTDOWN) {
2081f5402007Ssensoray-dev 		dprintk(dev, 2, "%s: err shutdown\n", __func__);
20820aa77f6cSMauro Carvalho Chehab 		pipe_info->err_count++;
20830aa77f6cSMauro Carvalho Chehab 		return;
20840aa77f6cSMauro Carvalho Chehab 	}
20850aa77f6cSMauro Carvalho Chehab 
20860aa77f6cSMauro Carvalho Chehab 	if (pipe_info->state == 0) {
2087f5402007Ssensoray-dev 		dprintk(dev, 2, "%s: exiting USB pipe", __func__);
20880aa77f6cSMauro Carvalho Chehab 		return;
20890aa77f6cSMauro Carvalho Chehab 	}
20900aa77f6cSMauro Carvalho Chehab 
20910aa77f6cSMauro Carvalho Chehab 	if (status == 0)
20920aa77f6cSMauro Carvalho Chehab 		s2255_read_video_callback(dev, pipe_info);
20930aa77f6cSMauro Carvalho Chehab 	else {
20940aa77f6cSMauro Carvalho Chehab 		pipe_info->err_count++;
2095f5402007Ssensoray-dev 		dprintk(dev, 1, "%s: failed URB %d\n", __func__, status);
20960aa77f6cSMauro Carvalho Chehab 	}
20970aa77f6cSMauro Carvalho Chehab 
20980aa77f6cSMauro Carvalho Chehab 	pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
20990aa77f6cSMauro Carvalho Chehab 	/* reuse urb */
21000aa77f6cSMauro Carvalho Chehab 	usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
21010aa77f6cSMauro Carvalho Chehab 			  pipe,
21020aa77f6cSMauro Carvalho Chehab 			  pipe_info->transfer_buffer,
21030aa77f6cSMauro Carvalho Chehab 			  pipe_info->cur_transfer_size,
21040aa77f6cSMauro Carvalho Chehab 			  read_pipe_completion, pipe_info);
21050aa77f6cSMauro Carvalho Chehab 
21060aa77f6cSMauro Carvalho Chehab 	if (pipe_info->state != 0) {
2107f5402007Ssensoray-dev 		if (usb_submit_urb(pipe_info->stream_urb, GFP_ATOMIC))
21080aa77f6cSMauro Carvalho Chehab 			dev_err(&dev->udev->dev, "error submitting urb\n");
21090aa77f6cSMauro Carvalho Chehab 	} else {
2110f5402007Ssensoray-dev 		dprintk(dev, 2, "%s :complete state 0\n", __func__);
21110aa77f6cSMauro Carvalho Chehab 	}
21120aa77f6cSMauro Carvalho Chehab 	return;
21130aa77f6cSMauro Carvalho Chehab }
21140aa77f6cSMauro Carvalho Chehab 
21150aa77f6cSMauro Carvalho Chehab static int s2255_start_readpipe(struct s2255_dev *dev)
21160aa77f6cSMauro Carvalho Chehab {
21170aa77f6cSMauro Carvalho Chehab 	int pipe;
21180aa77f6cSMauro Carvalho Chehab 	int retval;
21190aa77f6cSMauro Carvalho Chehab 	struct s2255_pipeinfo *pipe_info = &dev->pipe;
21200aa77f6cSMauro Carvalho Chehab 	pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2121f5402007Ssensoray-dev 	dprintk(dev, 2, "%s: IN %d\n", __func__, dev->read_endpoint);
21220aa77f6cSMauro Carvalho Chehab 	pipe_info->state = 1;
21230aa77f6cSMauro Carvalho Chehab 	pipe_info->err_count = 0;
21240aa77f6cSMauro Carvalho Chehab 	pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2125fc56da79SWolfram Sang 	if (!pipe_info->stream_urb)
21260aa77f6cSMauro Carvalho Chehab 		return -ENOMEM;
21270aa77f6cSMauro Carvalho Chehab 	/* transfer buffer allocated in board_init */
21280aa77f6cSMauro Carvalho Chehab 	usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
21290aa77f6cSMauro Carvalho Chehab 			  pipe,
21300aa77f6cSMauro Carvalho Chehab 			  pipe_info->transfer_buffer,
21310aa77f6cSMauro Carvalho Chehab 			  pipe_info->cur_transfer_size,
21320aa77f6cSMauro Carvalho Chehab 			  read_pipe_completion, pipe_info);
21330aa77f6cSMauro Carvalho Chehab 	retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
21340aa77f6cSMauro Carvalho Chehab 	if (retval) {
2135f5402007Ssensoray-dev 		pr_err("s2255: start read pipe failed\n");
21360aa77f6cSMauro Carvalho Chehab 		return retval;
21370aa77f6cSMauro Carvalho Chehab 	}
21380aa77f6cSMauro Carvalho Chehab 	return 0;
21390aa77f6cSMauro Carvalho Chehab }
21400aa77f6cSMauro Carvalho Chehab 
21410aa77f6cSMauro Carvalho Chehab /* starts acquisition process */
21425e950fafSDean Anderson static int s2255_start_acquire(struct s2255_vc *vc)
21430aa77f6cSMauro Carvalho Chehab {
21440aa77f6cSMauro Carvalho Chehab 	int res;
21450aa77f6cSMauro Carvalho Chehab 	unsigned long chn_rev;
21460aa77f6cSMauro Carvalho Chehab 	int j;
21475e950fafSDean Anderson 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
214847d8c881SDean Anderson 	__le32 *buffer = dev->cmdbuf;
21490aa77f6cSMauro Carvalho Chehab 
215047d8c881SDean Anderson 	mutex_lock(&dev->cmdlock);
215147d8c881SDean Anderson 	chn_rev = G_chnmap[vc->idx];
21525e950fafSDean Anderson 	vc->last_frame = -1;
21535e950fafSDean Anderson 	vc->bad_payload = 0;
21545e950fafSDean Anderson 	vc->cur_frame = 0;
21550aa77f6cSMauro Carvalho Chehab 	for (j = 0; j < SYS_FRAMES; j++) {
21565e950fafSDean Anderson 		vc->buffer.frame[j].ulState = 0;
21575e950fafSDean Anderson 		vc->buffer.frame[j].cur_size = 0;
21580aa77f6cSMauro Carvalho Chehab 	}
21590aa77f6cSMauro Carvalho Chehab 
21600aa77f6cSMauro Carvalho Chehab 	/* send the start command */
216147d8c881SDean Anderson 	buffer[0] = IN_DATA_TOKEN;
216247d8c881SDean Anderson 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
216347d8c881SDean Anderson 	buffer[2] = CMD_START;
21640aa77f6cSMauro Carvalho Chehab 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
21650aa77f6cSMauro Carvalho Chehab 	if (res != 0)
21660aa77f6cSMauro Carvalho Chehab 		dev_err(&dev->udev->dev, "CMD_START error\n");
21670aa77f6cSMauro Carvalho Chehab 
21685e950fafSDean Anderson 	dprintk(dev, 2, "start acquire exit[%d] %d\n", vc->idx, res);
216947d8c881SDean Anderson 	mutex_unlock(&dev->cmdlock);
21706a5b63b3SDean Anderson 	return res;
21710aa77f6cSMauro Carvalho Chehab }
21720aa77f6cSMauro Carvalho Chehab 
21735e950fafSDean Anderson static int s2255_stop_acquire(struct s2255_vc *vc)
21740aa77f6cSMauro Carvalho Chehab {
21750aa77f6cSMauro Carvalho Chehab 	int res;
21760aa77f6cSMauro Carvalho Chehab 	unsigned long chn_rev;
21775e950fafSDean Anderson 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
217847d8c881SDean Anderson 	__le32 *buffer = dev->cmdbuf;
217947d8c881SDean Anderson 
218047d8c881SDean Anderson 	mutex_lock(&dev->cmdlock);
21815e950fafSDean Anderson 	chn_rev = G_chnmap[vc->idx];
21820aa77f6cSMauro Carvalho Chehab 	/* send the stop command */
218347d8c881SDean Anderson 	buffer[0] = IN_DATA_TOKEN;
218447d8c881SDean Anderson 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
218547d8c881SDean Anderson 	buffer[2] = CMD_STOP;
218647d8c881SDean Anderson 
21870aa77f6cSMauro Carvalho Chehab 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
21880aa77f6cSMauro Carvalho Chehab 	if (res != 0)
21890aa77f6cSMauro Carvalho Chehab 		dev_err(&dev->udev->dev, "CMD_STOP error\n");
219047d8c881SDean Anderson 
21915e950fafSDean Anderson 	dprintk(dev, 4, "%s: chn %d, res %d\n", __func__, vc->idx, res);
219247d8c881SDean Anderson 	mutex_unlock(&dev->cmdlock);
21930aa77f6cSMauro Carvalho Chehab 	return res;
21940aa77f6cSMauro Carvalho Chehab }
21950aa77f6cSMauro Carvalho Chehab 
21960aa77f6cSMauro Carvalho Chehab static void s2255_stop_readpipe(struct s2255_dev *dev)
21970aa77f6cSMauro Carvalho Chehab {
21980aa77f6cSMauro Carvalho Chehab 	struct s2255_pipeinfo *pipe = &dev->pipe;
21990aa77f6cSMauro Carvalho Chehab 
22000aa77f6cSMauro Carvalho Chehab 	pipe->state = 0;
22010aa77f6cSMauro Carvalho Chehab 	if (pipe->stream_urb) {
22020aa77f6cSMauro Carvalho Chehab 		/* cancel urb */
22030aa77f6cSMauro Carvalho Chehab 		usb_kill_urb(pipe->stream_urb);
22040aa77f6cSMauro Carvalho Chehab 		usb_free_urb(pipe->stream_urb);
22050aa77f6cSMauro Carvalho Chehab 		pipe->stream_urb = NULL;
22060aa77f6cSMauro Carvalho Chehab 	}
2207f5402007Ssensoray-dev 	dprintk(dev, 4, "%s", __func__);
22080aa77f6cSMauro Carvalho Chehab 	return;
22090aa77f6cSMauro Carvalho Chehab }
22100aa77f6cSMauro Carvalho Chehab 
22110aa77f6cSMauro Carvalho Chehab static void s2255_fwload_start(struct s2255_dev *dev, int reset)
22120aa77f6cSMauro Carvalho Chehab {
22130aa77f6cSMauro Carvalho Chehab 	if (reset)
22140aa77f6cSMauro Carvalho Chehab 		s2255_reset_dsppower(dev);
22150aa77f6cSMauro Carvalho Chehab 	dev->fw_data->fw_size = dev->fw_data->fw->size;
22160aa77f6cSMauro Carvalho Chehab 	atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
22170aa77f6cSMauro Carvalho Chehab 	memcpy(dev->fw_data->pfw_data,
22180aa77f6cSMauro Carvalho Chehab 	       dev->fw_data->fw->data, CHUNK_SIZE);
22190aa77f6cSMauro Carvalho Chehab 	dev->fw_data->fw_loaded = CHUNK_SIZE;
22200aa77f6cSMauro Carvalho Chehab 	usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
22210aa77f6cSMauro Carvalho Chehab 			  usb_sndbulkpipe(dev->udev, 2),
22220aa77f6cSMauro Carvalho Chehab 			  dev->fw_data->pfw_data,
22230aa77f6cSMauro Carvalho Chehab 			  CHUNK_SIZE, s2255_fwchunk_complete,
22240aa77f6cSMauro Carvalho Chehab 			  dev->fw_data);
22250aa77f6cSMauro Carvalho Chehab 	mod_timer(&dev->timer, jiffies + HZ);
22260aa77f6cSMauro Carvalho Chehab }
22270aa77f6cSMauro Carvalho Chehab 
22280aa77f6cSMauro Carvalho Chehab /* standard usb probe function */
22290aa77f6cSMauro Carvalho Chehab static int s2255_probe(struct usb_interface *interface,
22300aa77f6cSMauro Carvalho Chehab 		       const struct usb_device_id *id)
22310aa77f6cSMauro Carvalho Chehab {
22320aa77f6cSMauro Carvalho Chehab 	struct s2255_dev *dev = NULL;
22330aa77f6cSMauro Carvalho Chehab 	struct usb_host_interface *iface_desc;
22340aa77f6cSMauro Carvalho Chehab 	struct usb_endpoint_descriptor *endpoint;
22350aa77f6cSMauro Carvalho Chehab 	int i;
22360aa77f6cSMauro Carvalho Chehab 	int retval = -ENOMEM;
22370aa77f6cSMauro Carvalho Chehab 	__le32 *pdata;
22380aa77f6cSMauro Carvalho Chehab 	int fw_size;
223947d8c881SDean Anderson 
22400aa77f6cSMauro Carvalho Chehab 	/* allocate memory for our device state and initialize it to zero */
22410aa77f6cSMauro Carvalho Chehab 	dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
22420aa77f6cSMauro Carvalho Chehab 	if (dev == NULL) {
22430aa77f6cSMauro Carvalho Chehab 		s2255_dev_err(&interface->dev, "out of memory\n");
22440aa77f6cSMauro Carvalho Chehab 		return -ENOMEM;
22450aa77f6cSMauro Carvalho Chehab 	}
224647d8c881SDean Anderson 
224747d8c881SDean Anderson 	dev->cmdbuf = kzalloc(S2255_CMDBUF_SIZE, GFP_KERNEL);
224847d8c881SDean Anderson 	if (dev->cmdbuf == NULL) {
224947d8c881SDean Anderson 		s2255_dev_err(&interface->dev, "out of memory\n");
2250e21c94e7SDaeseok Youn 		goto errorFWDATA1;
225147d8c881SDean Anderson 	}
225247d8c881SDean Anderson 
22530aa77f6cSMauro Carvalho Chehab 	atomic_set(&dev->num_channels, 0);
2254ff3ec57dSHans Verkuil 	dev->pid = id->idProduct;
22550aa77f6cSMauro Carvalho Chehab 	dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
22560aa77f6cSMauro Carvalho Chehab 	if (!dev->fw_data)
22570aa77f6cSMauro Carvalho Chehab 		goto errorFWDATA1;
22580aa77f6cSMauro Carvalho Chehab 	mutex_init(&dev->lock);
225947d8c881SDean Anderson 	mutex_init(&dev->cmdlock);
22600aa77f6cSMauro Carvalho Chehab 	/* grab usb_device and save it */
22610aa77f6cSMauro Carvalho Chehab 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
22620aa77f6cSMauro Carvalho Chehab 	if (dev->udev == NULL) {
22630aa77f6cSMauro Carvalho Chehab 		dev_err(&interface->dev, "null usb device\n");
22640aa77f6cSMauro Carvalho Chehab 		retval = -ENODEV;
22650aa77f6cSMauro Carvalho Chehab 		goto errorUDEV;
22660aa77f6cSMauro Carvalho Chehab 	}
2267f5402007Ssensoray-dev 	dev_dbg(&interface->dev, "dev: %p, udev %p interface %p\n",
2268f5402007Ssensoray-dev 		dev, dev->udev, interface);
22690aa77f6cSMauro Carvalho Chehab 	dev->interface = interface;
22700aa77f6cSMauro Carvalho Chehab 	/* set up the endpoint information  */
22710aa77f6cSMauro Carvalho Chehab 	iface_desc = interface->cur_altsetting;
2272f5402007Ssensoray-dev 	dev_dbg(&interface->dev, "num EP: %d\n",
2273f5402007Ssensoray-dev 		iface_desc->desc.bNumEndpoints);
22740aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
22750aa77f6cSMauro Carvalho Chehab 		endpoint = &iface_desc->endpoint[i].desc;
22760aa77f6cSMauro Carvalho Chehab 		if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
22770aa77f6cSMauro Carvalho Chehab 			/* we found the bulk in endpoint */
22780aa77f6cSMauro Carvalho Chehab 			dev->read_endpoint = endpoint->bEndpointAddress;
22790aa77f6cSMauro Carvalho Chehab 		}
22800aa77f6cSMauro Carvalho Chehab 	}
22810aa77f6cSMauro Carvalho Chehab 
22820aa77f6cSMauro Carvalho Chehab 	if (!dev->read_endpoint) {
22830aa77f6cSMauro Carvalho Chehab 		dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
22840aa77f6cSMauro Carvalho Chehab 		goto errorEP;
22850aa77f6cSMauro Carvalho Chehab 	}
22868bf554caSJulia Lawall 	setup_timer(&dev->timer, s2255_timer, (unsigned long)dev->fw_data);
22870aa77f6cSMauro Carvalho Chehab 	init_waitqueue_head(&dev->fw_data->wait_fw);
22880aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < MAX_CHANNELS; i++) {
22895e950fafSDean Anderson 		struct s2255_vc *vc = &dev->vc[i];
22905e950fafSDean Anderson 		vc->idx = i;
22915e950fafSDean Anderson 		vc->dev = dev;
22925e950fafSDean Anderson 		init_waitqueue_head(&vc->wait_setmode);
22935e950fafSDean Anderson 		init_waitqueue_head(&vc->wait_vidstatus);
2294340a30c5Ssensoray-dev 		spin_lock_init(&vc->qlock);
2295340a30c5Ssensoray-dev 		mutex_init(&vc->vb_lock);
22960aa77f6cSMauro Carvalho Chehab 	}
22970aa77f6cSMauro Carvalho Chehab 
22980aa77f6cSMauro Carvalho Chehab 	dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2299fc56da79SWolfram Sang 	if (!dev->fw_data->fw_urb)
23000aa77f6cSMauro Carvalho Chehab 		goto errorFWURB;
23010aa77f6cSMauro Carvalho Chehab 
23020aa77f6cSMauro Carvalho Chehab 	dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
23030aa77f6cSMauro Carvalho Chehab 	if (!dev->fw_data->pfw_data) {
23040aa77f6cSMauro Carvalho Chehab 		dev_err(&interface->dev, "out of memory!\n");
23050aa77f6cSMauro Carvalho Chehab 		goto errorFWDATA2;
23060aa77f6cSMauro Carvalho Chehab 	}
23070aa77f6cSMauro Carvalho Chehab 	/* load the first chunk */
23080aa77f6cSMauro Carvalho Chehab 	if (request_firmware(&dev->fw_data->fw,
23090aa77f6cSMauro Carvalho Chehab 			     FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2310f5402007Ssensoray-dev 		dev_err(&interface->dev, "sensoray 2255 failed to get firmware\n");
23110aa77f6cSMauro Carvalho Chehab 		goto errorREQFW;
23120aa77f6cSMauro Carvalho Chehab 	}
23130aa77f6cSMauro Carvalho Chehab 	/* check the firmware is valid */
23140aa77f6cSMauro Carvalho Chehab 	fw_size = dev->fw_data->fw->size;
23150aa77f6cSMauro Carvalho Chehab 	pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
23160aa77f6cSMauro Carvalho Chehab 
23170aa77f6cSMauro Carvalho Chehab 	if (*pdata != S2255_FW_MARKER) {
2318f5402007Ssensoray-dev 		dev_err(&interface->dev, "Firmware invalid.\n");
23190aa77f6cSMauro Carvalho Chehab 		retval = -ENODEV;
23200aa77f6cSMauro Carvalho Chehab 		goto errorFWMARKER;
23210aa77f6cSMauro Carvalho Chehab 	} else {
23220aa77f6cSMauro Carvalho Chehab 		/* make sure firmware is the latest */
23230aa77f6cSMauro Carvalho Chehab 		__le32 *pRel;
23240aa77f6cSMauro Carvalho Chehab 		pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2325f5402007Ssensoray-dev 		pr_info("s2255 dsp fw version %x\n", le32_to_cpu(*pRel));
23260aa77f6cSMauro Carvalho Chehab 		dev->dsp_fw_ver = le32_to_cpu(*pRel);
23270aa77f6cSMauro Carvalho Chehab 		if (dev->dsp_fw_ver < S2255_CUR_DSP_FWVER)
2328f5402007Ssensoray-dev 			pr_info("s2255: f2255usb.bin out of date.\n");
23290aa77f6cSMauro Carvalho Chehab 		if (dev->pid == 0x2257 &&
23300aa77f6cSMauro Carvalho Chehab 				dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
2331f5402007Ssensoray-dev 			pr_warn("2257 needs firmware %d or above.\n",
2332f5402007Ssensoray-dev 				S2255_MIN_DSP_COLORFILTER);
23330aa77f6cSMauro Carvalho Chehab 	}
23340aa77f6cSMauro Carvalho Chehab 	usb_reset_device(dev->udev);
23350aa77f6cSMauro Carvalho Chehab 	/* load 2255 board specific */
23360aa77f6cSMauro Carvalho Chehab 	retval = s2255_board_init(dev);
23370aa77f6cSMauro Carvalho Chehab 	if (retval)
23380aa77f6cSMauro Carvalho Chehab 		goto errorBOARDINIT;
23390aa77f6cSMauro Carvalho Chehab 	s2255_fwload_start(dev, 0);
23400aa77f6cSMauro Carvalho Chehab 	/* loads v4l specific */
23410aa77f6cSMauro Carvalho Chehab 	retval = s2255_probe_v4l(dev);
23420aa77f6cSMauro Carvalho Chehab 	if (retval)
23430aa77f6cSMauro Carvalho Chehab 		goto errorBOARDINIT;
23440aa77f6cSMauro Carvalho Chehab 	dev_info(&interface->dev, "Sensoray 2255 detected\n");
23450aa77f6cSMauro Carvalho Chehab 	return 0;
23460aa77f6cSMauro Carvalho Chehab errorBOARDINIT:
23470aa77f6cSMauro Carvalho Chehab 	s2255_board_shutdown(dev);
23480aa77f6cSMauro Carvalho Chehab errorFWMARKER:
23490aa77f6cSMauro Carvalho Chehab 	release_firmware(dev->fw_data->fw);
23500aa77f6cSMauro Carvalho Chehab errorREQFW:
23510aa77f6cSMauro Carvalho Chehab 	kfree(dev->fw_data->pfw_data);
23520aa77f6cSMauro Carvalho Chehab errorFWDATA2:
23530aa77f6cSMauro Carvalho Chehab 	usb_free_urb(dev->fw_data->fw_urb);
23540aa77f6cSMauro Carvalho Chehab errorFWURB:
23559f6be2bcSKirill Tkhai 	del_timer_sync(&dev->timer);
23560aa77f6cSMauro Carvalho Chehab errorEP:
23570aa77f6cSMauro Carvalho Chehab 	usb_put_dev(dev->udev);
23580aa77f6cSMauro Carvalho Chehab errorUDEV:
23590aa77f6cSMauro Carvalho Chehab 	kfree(dev->fw_data);
23600aa77f6cSMauro Carvalho Chehab 	mutex_destroy(&dev->lock);
23610aa77f6cSMauro Carvalho Chehab errorFWDATA1:
236247d8c881SDean Anderson 	kfree(dev->cmdbuf);
23630aa77f6cSMauro Carvalho Chehab 	kfree(dev);
2364f5402007Ssensoray-dev 	pr_warn("Sensoray 2255 driver load failed: 0x%x\n", retval);
23650aa77f6cSMauro Carvalho Chehab 	return retval;
23660aa77f6cSMauro Carvalho Chehab }
23670aa77f6cSMauro Carvalho Chehab 
23680aa77f6cSMauro Carvalho Chehab /* disconnect routine. when board is removed physically or with rmmod */
23690aa77f6cSMauro Carvalho Chehab static void s2255_disconnect(struct usb_interface *interface)
23700aa77f6cSMauro Carvalho Chehab {
23710aa77f6cSMauro Carvalho Chehab 	struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
23720aa77f6cSMauro Carvalho Chehab 	int i;
23730aa77f6cSMauro Carvalho Chehab 	int channels = atomic_read(&dev->num_channels);
23740aa77f6cSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
23750aa77f6cSMauro Carvalho Chehab 	v4l2_device_disconnect(&dev->v4l2_dev);
23760aa77f6cSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
23770aa77f6cSMauro Carvalho Chehab 	/*see comments in the uvc_driver.c usb disconnect function */
23780aa77f6cSMauro Carvalho Chehab 	atomic_inc(&dev->num_channels);
23790aa77f6cSMauro Carvalho Chehab 	/* unregister each video device. */
23800aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < channels; i++)
23815e950fafSDean Anderson 		video_unregister_device(&dev->vc[i].vdev);
23820aa77f6cSMauro Carvalho Chehab 	/* wake up any of our timers */
23830aa77f6cSMauro Carvalho Chehab 	atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
23840aa77f6cSMauro Carvalho Chehab 	wake_up(&dev->fw_data->wait_fw);
23850aa77f6cSMauro Carvalho Chehab 	for (i = 0; i < MAX_CHANNELS; i++) {
23865e950fafSDean Anderson 		dev->vc[i].setmode_ready = 1;
23875e950fafSDean Anderson 		wake_up(&dev->vc[i].wait_setmode);
23885e950fafSDean Anderson 		dev->vc[i].vidstatus_ready = 1;
23895e950fafSDean Anderson 		wake_up(&dev->vc[i].wait_vidstatus);
23900aa77f6cSMauro Carvalho Chehab 	}
23910aa77f6cSMauro Carvalho Chehab 	if (atomic_dec_and_test(&dev->num_channels))
23920aa77f6cSMauro Carvalho Chehab 		s2255_destroy(dev);
23930aa77f6cSMauro Carvalho Chehab 	dev_info(&interface->dev, "%s\n", __func__);
23940aa77f6cSMauro Carvalho Chehab }
23950aa77f6cSMauro Carvalho Chehab 
23960aa77f6cSMauro Carvalho Chehab static struct usb_driver s2255_driver = {
23970aa77f6cSMauro Carvalho Chehab 	.name = S2255_DRIVER_NAME,
23980aa77f6cSMauro Carvalho Chehab 	.probe = s2255_probe,
23990aa77f6cSMauro Carvalho Chehab 	.disconnect = s2255_disconnect,
24000aa77f6cSMauro Carvalho Chehab 	.id_table = s2255_table,
24010aa77f6cSMauro Carvalho Chehab };
24020aa77f6cSMauro Carvalho Chehab 
24030aa77f6cSMauro Carvalho Chehab module_usb_driver(s2255_driver);
24040aa77f6cSMauro Carvalho Chehab 
24050aa77f6cSMauro Carvalho Chehab MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
24060aa77f6cSMauro Carvalho Chehab MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
24070aa77f6cSMauro Carvalho Chehab MODULE_LICENSE("GPL");
24080aa77f6cSMauro Carvalho Chehab MODULE_VERSION(S2255_VERSION);
24090aa77f6cSMauro Carvalho Chehab MODULE_FIRMWARE(FIRMWARE_FILE_NAME);
2410