xref: /OK3568_Linux_fs/kernel/drivers/media/usb/s2255/s2255drv.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  s2255drv.c - a driver for the Sensoray 2255 USB video capture device
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *   Copyright (C) 2007-2014 by Sensoray Company Inc.
6*4882a593Smuzhiyun  *                              Dean Anderson
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Some video buffer code based on vivi driver:
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Sensoray 2255 device supports 4 simultaneous channels.
11*4882a593Smuzhiyun  * The channels are not "crossbar" inputs, they are physically
12*4882a593Smuzhiyun  * attached to separate video decoders.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Because of USB2.0 bandwidth limitations. There is only a
15*4882a593Smuzhiyun  * certain amount of data which may be transferred at one time.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * Example maximum bandwidth utilization:
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * -full size, color mode YUYV or YUV422P: 2 channels at once
20*4882a593Smuzhiyun  * -full or half size Grey scale: all 4 channels at once
21*4882a593Smuzhiyun  * -half size, color mode YUYV or YUV422P: all 4 channels at once
22*4882a593Smuzhiyun  * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels
23*4882a593Smuzhiyun  *  at once.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/firmware.h>
28*4882a593Smuzhiyun #include <linux/kernel.h>
29*4882a593Smuzhiyun #include <linux/mutex.h>
30*4882a593Smuzhiyun #include <linux/slab.h>
31*4882a593Smuzhiyun #include <linux/videodev2.h>
32*4882a593Smuzhiyun #include <linux/mm.h>
33*4882a593Smuzhiyun #include <linux/vmalloc.h>
34*4882a593Smuzhiyun #include <linux/usb.h>
35*4882a593Smuzhiyun #include <media/videobuf2-v4l2.h>
36*4882a593Smuzhiyun #include <media/videobuf2-vmalloc.h>
37*4882a593Smuzhiyun #include <media/v4l2-common.h>
38*4882a593Smuzhiyun #include <media/v4l2-device.h>
39*4882a593Smuzhiyun #include <media/v4l2-ioctl.h>
40*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
41*4882a593Smuzhiyun #include <media/v4l2-event.h>
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define S2255_VERSION		"1.25.1"
44*4882a593Smuzhiyun #define FIRMWARE_FILE_NAME "f2255usb.bin"
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* default JPEG quality */
47*4882a593Smuzhiyun #define S2255_DEF_JPEG_QUAL     50
48*4882a593Smuzhiyun /* vendor request in */
49*4882a593Smuzhiyun #define S2255_VR_IN		0
50*4882a593Smuzhiyun /* vendor request out */
51*4882a593Smuzhiyun #define S2255_VR_OUT		1
52*4882a593Smuzhiyun /* firmware query */
53*4882a593Smuzhiyun #define S2255_VR_FW		0x30
54*4882a593Smuzhiyun /* USB endpoint number for configuring the device */
55*4882a593Smuzhiyun #define S2255_CONFIG_EP         2
56*4882a593Smuzhiyun /* maximum time for DSP to start responding after last FW word loaded(ms) */
57*4882a593Smuzhiyun #define S2255_DSP_BOOTTIME      800
58*4882a593Smuzhiyun /* maximum time to wait for firmware to load (ms) */
59*4882a593Smuzhiyun #define S2255_LOAD_TIMEOUT      (5000 + S2255_DSP_BOOTTIME)
60*4882a593Smuzhiyun #define S2255_MIN_BUFS          2
61*4882a593Smuzhiyun #define S2255_SETMODE_TIMEOUT   500
62*4882a593Smuzhiyun #define S2255_VIDSTATUS_TIMEOUT 350
63*4882a593Smuzhiyun #define S2255_MARKER_FRAME	cpu_to_le32(0x2255DA4AL)
64*4882a593Smuzhiyun #define S2255_MARKER_RESPONSE	cpu_to_le32(0x2255ACACL)
65*4882a593Smuzhiyun #define S2255_RESPONSE_SETMODE  cpu_to_le32(0x01)
66*4882a593Smuzhiyun #define S2255_RESPONSE_FW       cpu_to_le32(0x10)
67*4882a593Smuzhiyun #define S2255_RESPONSE_STATUS   cpu_to_le32(0x20)
68*4882a593Smuzhiyun #define S2255_USB_XFER_SIZE	(16 * 1024)
69*4882a593Smuzhiyun #define MAX_CHANNELS		4
70*4882a593Smuzhiyun #define SYS_FRAMES		4
71*4882a593Smuzhiyun /* maximum size is PAL full size plus room for the marker header(s) */
72*4882a593Smuzhiyun #define SYS_FRAMES_MAXSIZE	(720*288*2*2 + 4096)
73*4882a593Smuzhiyun #define DEF_USB_BLOCK		S2255_USB_XFER_SIZE
74*4882a593Smuzhiyun #define LINE_SZ_4CIFS_NTSC	640
75*4882a593Smuzhiyun #define LINE_SZ_2CIFS_NTSC	640
76*4882a593Smuzhiyun #define LINE_SZ_1CIFS_NTSC	320
77*4882a593Smuzhiyun #define LINE_SZ_4CIFS_PAL	704
78*4882a593Smuzhiyun #define LINE_SZ_2CIFS_PAL	704
79*4882a593Smuzhiyun #define LINE_SZ_1CIFS_PAL	352
80*4882a593Smuzhiyun #define NUM_LINES_4CIFS_NTSC	240
81*4882a593Smuzhiyun #define NUM_LINES_2CIFS_NTSC	240
82*4882a593Smuzhiyun #define NUM_LINES_1CIFS_NTSC	240
83*4882a593Smuzhiyun #define NUM_LINES_4CIFS_PAL	288
84*4882a593Smuzhiyun #define NUM_LINES_2CIFS_PAL	288
85*4882a593Smuzhiyun #define NUM_LINES_1CIFS_PAL	288
86*4882a593Smuzhiyun #define LINE_SZ_DEF		640
87*4882a593Smuzhiyun #define NUM_LINES_DEF		240
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* predefined settings */
91*4882a593Smuzhiyun #define FORMAT_NTSC	1
92*4882a593Smuzhiyun #define FORMAT_PAL	2
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun #define SCALE_4CIFS	1	/* 640x480(NTSC) or 704x576(PAL) */
95*4882a593Smuzhiyun #define SCALE_2CIFS	2	/* 640x240(NTSC) or 704x288(PAL) */
96*4882a593Smuzhiyun #define SCALE_1CIFS	3	/* 320x240(NTSC) or 352x288(PAL) */
97*4882a593Smuzhiyun /* SCALE_4CIFSI is the 2 fields interpolated into one */
98*4882a593Smuzhiyun #define SCALE_4CIFSI	4	/* 640x480(NTSC) or 704x576(PAL) high quality */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #define COLOR_YUVPL	1	/* YUV planar */
101*4882a593Smuzhiyun #define COLOR_YUVPK	2	/* YUV packed */
102*4882a593Smuzhiyun #define COLOR_Y8	4	/* monochrome */
103*4882a593Smuzhiyun #define COLOR_JPG       5       /* JPEG */
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #define MASK_COLOR       0x000000ff
106*4882a593Smuzhiyun #define MASK_JPG_QUALITY 0x0000ff00
107*4882a593Smuzhiyun #define MASK_INPUT_TYPE  0x000f0000
108*4882a593Smuzhiyun /* frame decimation. */
109*4882a593Smuzhiyun #define FDEC_1		1	/* capture every frame. default */
110*4882a593Smuzhiyun #define FDEC_2		2	/* capture every 2nd frame */
111*4882a593Smuzhiyun #define FDEC_3		3	/* capture every 3rd frame */
112*4882a593Smuzhiyun #define FDEC_5		5	/* capture every 5th frame */
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /*-------------------------------------------------------
115*4882a593Smuzhiyun  * Default mode parameters.
116*4882a593Smuzhiyun  *-------------------------------------------------------*/
117*4882a593Smuzhiyun #define DEF_SCALE	SCALE_4CIFS
118*4882a593Smuzhiyun #define DEF_COLOR	COLOR_YUVPL
119*4882a593Smuzhiyun #define DEF_FDEC	FDEC_1
120*4882a593Smuzhiyun #define DEF_BRIGHT	0
121*4882a593Smuzhiyun #define DEF_CONTRAST	0x5c
122*4882a593Smuzhiyun #define DEF_SATURATION	0x80
123*4882a593Smuzhiyun #define DEF_HUE		0
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /* usb config commands */
126*4882a593Smuzhiyun #define IN_DATA_TOKEN	cpu_to_le32(0x2255c0de)
127*4882a593Smuzhiyun #define CMD_2255	0xc2255000
128*4882a593Smuzhiyun #define CMD_SET_MODE	cpu_to_le32((CMD_2255 | 0x10))
129*4882a593Smuzhiyun #define CMD_START	cpu_to_le32((CMD_2255 | 0x20))
130*4882a593Smuzhiyun #define CMD_STOP	cpu_to_le32((CMD_2255 | 0x30))
131*4882a593Smuzhiyun #define CMD_STATUS	cpu_to_le32((CMD_2255 | 0x40))
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun struct s2255_mode {
134*4882a593Smuzhiyun 	u32 format;	/* input video format (NTSC, PAL) */
135*4882a593Smuzhiyun 	u32 scale;	/* output video scale */
136*4882a593Smuzhiyun 	u32 color;	/* output video color format */
137*4882a593Smuzhiyun 	u32 fdec;	/* frame decimation */
138*4882a593Smuzhiyun 	u32 bright;	/* brightness */
139*4882a593Smuzhiyun 	u32 contrast;	/* contrast */
140*4882a593Smuzhiyun 	u32 saturation;	/* saturation */
141*4882a593Smuzhiyun 	u32 hue;	/* hue (NTSC only)*/
142*4882a593Smuzhiyun 	u32 single;	/* capture 1 frame at a time (!=0), continuously (==0)*/
143*4882a593Smuzhiyun 	u32 usb_block;	/* block size. should be 4096 of DEF_USB_BLOCK */
144*4882a593Smuzhiyun 	u32 restart;	/* if DSP requires restart */
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun #define S2255_READ_IDLE		0
149*4882a593Smuzhiyun #define S2255_READ_FRAME	1
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /* frame structure */
152*4882a593Smuzhiyun struct s2255_framei {
153*4882a593Smuzhiyun 	unsigned long size;
154*4882a593Smuzhiyun 	unsigned long ulState;	/* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
155*4882a593Smuzhiyun 	void *lpvbits;		/* image data */
156*4882a593Smuzhiyun 	unsigned long cur_size;	/* current data copied to it */
157*4882a593Smuzhiyun };
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /* image buffer structure */
160*4882a593Smuzhiyun struct s2255_bufferi {
161*4882a593Smuzhiyun 	unsigned long dwFrames;			/* number of frames in buffer */
162*4882a593Smuzhiyun 	struct s2255_framei frame[SYS_FRAMES];	/* array of FRAME structures */
163*4882a593Smuzhiyun };
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun #define DEF_MODEI_NTSC_CONT	{FORMAT_NTSC, DEF_SCALE, DEF_COLOR,	\
166*4882a593Smuzhiyun 			DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \
167*4882a593Smuzhiyun 			DEF_HUE, 0, DEF_USB_BLOCK, 0}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /* for firmware loading, fw_state */
170*4882a593Smuzhiyun #define S2255_FW_NOTLOADED	0
171*4882a593Smuzhiyun #define S2255_FW_LOADED_DSPWAIT	1
172*4882a593Smuzhiyun #define S2255_FW_SUCCESS	2
173*4882a593Smuzhiyun #define S2255_FW_FAILED		3
174*4882a593Smuzhiyun #define S2255_FW_DISCONNECTING  4
175*4882a593Smuzhiyun #define S2255_FW_MARKER		cpu_to_le32(0x22552f2f)
176*4882a593Smuzhiyun /* 2255 read states */
177*4882a593Smuzhiyun #define S2255_READ_IDLE         0
178*4882a593Smuzhiyun #define S2255_READ_FRAME        1
179*4882a593Smuzhiyun struct s2255_fw {
180*4882a593Smuzhiyun 	int		      fw_loaded;
181*4882a593Smuzhiyun 	int		      fw_size;
182*4882a593Smuzhiyun 	struct urb	      *fw_urb;
183*4882a593Smuzhiyun 	atomic_t	      fw_state;
184*4882a593Smuzhiyun 	void		      *pfw_data;
185*4882a593Smuzhiyun 	wait_queue_head_t     wait_fw;
186*4882a593Smuzhiyun 	const struct firmware *fw;
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun struct s2255_pipeinfo {
190*4882a593Smuzhiyun 	u32 max_transfer_size;
191*4882a593Smuzhiyun 	u32 cur_transfer_size;
192*4882a593Smuzhiyun 	u8 *transfer_buffer;
193*4882a593Smuzhiyun 	u32 state;
194*4882a593Smuzhiyun 	void *stream_urb;
195*4882a593Smuzhiyun 	void *dev;	/* back pointer to s2255_dev struct*/
196*4882a593Smuzhiyun 	u32 err_count;
197*4882a593Smuzhiyun 	u32 idx;
198*4882a593Smuzhiyun };
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun struct s2255_fmt; /*forward declaration */
201*4882a593Smuzhiyun struct s2255_dev;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /* 2255 video channel */
204*4882a593Smuzhiyun struct s2255_vc {
205*4882a593Smuzhiyun 	struct s2255_dev        *dev;
206*4882a593Smuzhiyun 	struct video_device	vdev;
207*4882a593Smuzhiyun 	struct v4l2_ctrl_handler hdl;
208*4882a593Smuzhiyun 	struct v4l2_ctrl	*jpegqual_ctrl;
209*4882a593Smuzhiyun 	int			resources;
210*4882a593Smuzhiyun 	struct list_head        buf_list;
211*4882a593Smuzhiyun 	struct s2255_bufferi	buffer;
212*4882a593Smuzhiyun 	struct s2255_mode	mode;
213*4882a593Smuzhiyun 	v4l2_std_id		std;
214*4882a593Smuzhiyun 	/* jpeg compression */
215*4882a593Smuzhiyun 	unsigned		jpegqual;
216*4882a593Smuzhiyun 	/* capture parameters (for high quality mode full size) */
217*4882a593Smuzhiyun 	struct v4l2_captureparm cap_parm;
218*4882a593Smuzhiyun 	int			cur_frame;
219*4882a593Smuzhiyun 	int			last_frame;
220*4882a593Smuzhiyun 	/* allocated image size */
221*4882a593Smuzhiyun 	unsigned long		req_image_size;
222*4882a593Smuzhiyun 	/* received packet size */
223*4882a593Smuzhiyun 	unsigned long		pkt_size;
224*4882a593Smuzhiyun 	int			bad_payload;
225*4882a593Smuzhiyun 	unsigned long		frame_count;
226*4882a593Smuzhiyun 	/* if JPEG image */
227*4882a593Smuzhiyun 	int                     jpg_size;
228*4882a593Smuzhiyun 	/* if channel configured to default state */
229*4882a593Smuzhiyun 	int                     configured;
230*4882a593Smuzhiyun 	wait_queue_head_t       wait_setmode;
231*4882a593Smuzhiyun 	int                     setmode_ready;
232*4882a593Smuzhiyun 	/* video status items */
233*4882a593Smuzhiyun 	int                     vidstatus;
234*4882a593Smuzhiyun 	wait_queue_head_t       wait_vidstatus;
235*4882a593Smuzhiyun 	int                     vidstatus_ready;
236*4882a593Smuzhiyun 	unsigned int		width;
237*4882a593Smuzhiyun 	unsigned int		height;
238*4882a593Smuzhiyun 	enum v4l2_field         field;
239*4882a593Smuzhiyun 	const struct s2255_fmt	*fmt;
240*4882a593Smuzhiyun 	int idx; /* channel number on device, 0-3 */
241*4882a593Smuzhiyun 	struct vb2_queue vb_vidq;
242*4882a593Smuzhiyun 	struct mutex vb_lock; /* streaming lock */
243*4882a593Smuzhiyun 	spinlock_t qlock;
244*4882a593Smuzhiyun };
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun struct s2255_dev {
248*4882a593Smuzhiyun 	struct s2255_vc         vc[MAX_CHANNELS];
249*4882a593Smuzhiyun 	struct v4l2_device      v4l2_dev;
250*4882a593Smuzhiyun 	atomic_t                num_channels;
251*4882a593Smuzhiyun 	int			frames;
252*4882a593Smuzhiyun 	struct mutex		lock;	/* channels[].vdev.lock */
253*4882a593Smuzhiyun 	struct mutex		cmdlock; /* protects cmdbuf */
254*4882a593Smuzhiyun 	struct usb_device	*udev;
255*4882a593Smuzhiyun 	struct usb_interface	*interface;
256*4882a593Smuzhiyun 	u8			read_endpoint;
257*4882a593Smuzhiyun 	struct timer_list	timer;
258*4882a593Smuzhiyun 	struct s2255_fw	*fw_data;
259*4882a593Smuzhiyun 	struct s2255_pipeinfo	pipe;
260*4882a593Smuzhiyun 	u32			cc;	/* current channel */
261*4882a593Smuzhiyun 	int			frame_ready;
262*4882a593Smuzhiyun 	int                     chn_ready;
263*4882a593Smuzhiyun 	/* dsp firmware version (f2255usb.bin) */
264*4882a593Smuzhiyun 	int                     dsp_fw_ver;
265*4882a593Smuzhiyun 	u16                     pid; /* product id */
266*4882a593Smuzhiyun #define S2255_CMDBUF_SIZE 512
267*4882a593Smuzhiyun 	__le32                  *cmdbuf;
268*4882a593Smuzhiyun };
269*4882a593Smuzhiyun 
to_s2255_dev(struct v4l2_device * v4l2_dev)270*4882a593Smuzhiyun static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun 	return container_of(v4l2_dev, struct s2255_dev, v4l2_dev);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun struct s2255_fmt {
276*4882a593Smuzhiyun 	u32 fourcc;
277*4882a593Smuzhiyun 	int depth;
278*4882a593Smuzhiyun };
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /* buffer for one video frame */
281*4882a593Smuzhiyun struct s2255_buffer {
282*4882a593Smuzhiyun 	/* common v4l buffer stuff -- must be first */
283*4882a593Smuzhiyun 	struct vb2_v4l2_buffer vb;
284*4882a593Smuzhiyun 	struct list_head list;
285*4882a593Smuzhiyun };
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /* current cypress EEPROM firmware version */
289*4882a593Smuzhiyun #define S2255_CUR_USB_FWVER	((3 << 8) | 12)
290*4882a593Smuzhiyun /* current DSP FW version */
291*4882a593Smuzhiyun #define S2255_CUR_DSP_FWVER     10104
292*4882a593Smuzhiyun /* Need DSP version 5+ for video status feature */
293*4882a593Smuzhiyun #define S2255_MIN_DSP_STATUS      5
294*4882a593Smuzhiyun #define S2255_MIN_DSP_COLORFILTER 8
295*4882a593Smuzhiyun #define S2255_NORMS		(V4L2_STD_ALL)
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun /* private V4L2 controls */
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun  * The following chart displays how COLORFILTER should be set
301*4882a593Smuzhiyun  *  =========================================================
302*4882a593Smuzhiyun  *  =     fourcc              =     COLORFILTER             =
303*4882a593Smuzhiyun  *  =                         ===============================
304*4882a593Smuzhiyun  *  =                         =   0             =    1      =
305*4882a593Smuzhiyun  *  =========================================================
306*4882a593Smuzhiyun  *  =  V4L2_PIX_FMT_GREY(Y8)  = monochrome from = monochrome=
307*4882a593Smuzhiyun  *  =                         = s-video or      = composite =
308*4882a593Smuzhiyun  *  =                         = B/W camera      = input     =
309*4882a593Smuzhiyun  *  =========================================================
310*4882a593Smuzhiyun  *  =    other                = color, svideo   = color,    =
311*4882a593Smuzhiyun  *  =                         =                 = composite =
312*4882a593Smuzhiyun  *  =========================================================
313*4882a593Smuzhiyun  *
314*4882a593Smuzhiyun  * Notes:
315*4882a593Smuzhiyun  *   channels 0-3 on 2255 are composite
316*4882a593Smuzhiyun  *   channels 0-1 on 2257 are composite, 2-3 are s-video
317*4882a593Smuzhiyun  * If COLORFILTER is 0 with a composite color camera connected,
318*4882a593Smuzhiyun  * the output will appear monochrome but hatching
319*4882a593Smuzhiyun  * will occur.
320*4882a593Smuzhiyun  * COLORFILTER is different from "color killer" and "color effects"
321*4882a593Smuzhiyun  * for reasons above.
322*4882a593Smuzhiyun  */
323*4882a593Smuzhiyun #define S2255_V4L2_YC_ON  1
324*4882a593Smuzhiyun #define S2255_V4L2_YC_OFF 0
325*4882a593Smuzhiyun #define V4L2_CID_S2255_COLORFILTER (V4L2_CID_USER_S2255_BASE + 0)
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun /* frame prefix size (sent once every frame) */
328*4882a593Smuzhiyun #define PREFIX_SIZE		512
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun /* Channels on box are in reverse order */
331*4882a593Smuzhiyun static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0};
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun static int debug;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun static int s2255_start_readpipe(struct s2255_dev *dev);
336*4882a593Smuzhiyun static void s2255_stop_readpipe(struct s2255_dev *dev);
337*4882a593Smuzhiyun static int s2255_start_acquire(struct s2255_vc *vc);
338*4882a593Smuzhiyun static int s2255_stop_acquire(struct s2255_vc *vc);
339*4882a593Smuzhiyun static void s2255_fillbuff(struct s2255_vc *vc, struct s2255_buffer *buf,
340*4882a593Smuzhiyun 			   int jpgsize);
341*4882a593Smuzhiyun static int s2255_set_mode(struct s2255_vc *vc, struct s2255_mode *mode);
342*4882a593Smuzhiyun static int s2255_board_shutdown(struct s2255_dev *dev);
343*4882a593Smuzhiyun static void s2255_fwload_start(struct s2255_dev *dev);
344*4882a593Smuzhiyun static void s2255_destroy(struct s2255_dev *dev);
345*4882a593Smuzhiyun static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
346*4882a593Smuzhiyun 			     u16 index, u16 value, void *buf,
347*4882a593Smuzhiyun 			     s32 buf_len, int bOut);
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /* dev_err macro with driver name */
350*4882a593Smuzhiyun #define S2255_DRIVER_NAME "s2255"
351*4882a593Smuzhiyun #define s2255_dev_err(dev, fmt, arg...)					\
352*4882a593Smuzhiyun 		dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg)
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun #define dprintk(dev, level, fmt, arg...) \
355*4882a593Smuzhiyun 	v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun static struct usb_driver s2255_driver;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun /* start video number */
360*4882a593Smuzhiyun static int video_nr = -1;	/* /dev/videoN, -1 for autodetect */
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun /* Enable jpeg capture. */
363*4882a593Smuzhiyun static int jpeg_enable = 1;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun module_param(debug, int, 0644);
366*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Debug level(0-100) default 0");
367*4882a593Smuzhiyun module_param(video_nr, int, 0644);
368*4882a593Smuzhiyun MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)");
369*4882a593Smuzhiyun module_param(jpeg_enable, int, 0644);
370*4882a593Smuzhiyun MODULE_PARM_DESC(jpeg_enable, "Jpeg enable(1-on 0-off) default 1");
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun /* USB device table */
373*4882a593Smuzhiyun #define USB_SENSORAY_VID	0x1943
374*4882a593Smuzhiyun static const struct usb_device_id s2255_table[] = {
375*4882a593Smuzhiyun 	{USB_DEVICE(USB_SENSORAY_VID, 0x2255)},
376*4882a593Smuzhiyun 	{USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/
377*4882a593Smuzhiyun 	{ }			/* Terminating entry */
378*4882a593Smuzhiyun };
379*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, s2255_table);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun #define BUFFER_TIMEOUT msecs_to_jiffies(400)
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun /* image formats.  */
384*4882a593Smuzhiyun /* JPEG formats must be defined last to support jpeg_enable parameter */
385*4882a593Smuzhiyun static const struct s2255_fmt formats[] = {
386*4882a593Smuzhiyun 	{
387*4882a593Smuzhiyun 		.fourcc = V4L2_PIX_FMT_YUYV,
388*4882a593Smuzhiyun 		.depth = 16
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	}, {
391*4882a593Smuzhiyun 		.fourcc = V4L2_PIX_FMT_UYVY,
392*4882a593Smuzhiyun 		.depth = 16
393*4882a593Smuzhiyun 	}, {
394*4882a593Smuzhiyun 		.fourcc = V4L2_PIX_FMT_YUV422P,
395*4882a593Smuzhiyun 		.depth = 16
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	}, {
398*4882a593Smuzhiyun 		.fourcc = V4L2_PIX_FMT_GREY,
399*4882a593Smuzhiyun 		.depth = 8
400*4882a593Smuzhiyun 	}, {
401*4882a593Smuzhiyun 		.fourcc = V4L2_PIX_FMT_JPEG,
402*4882a593Smuzhiyun 		.depth = 24
403*4882a593Smuzhiyun 	}, {
404*4882a593Smuzhiyun 		.fourcc = V4L2_PIX_FMT_MJPEG,
405*4882a593Smuzhiyun 		.depth = 24
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun };
408*4882a593Smuzhiyun 
norm_maxw(struct s2255_vc * vc)409*4882a593Smuzhiyun static int norm_maxw(struct s2255_vc *vc)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	return (vc->std & V4L2_STD_525_60) ?
412*4882a593Smuzhiyun 	    LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
norm_maxh(struct s2255_vc * vc)415*4882a593Smuzhiyun static int norm_maxh(struct s2255_vc *vc)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	return (vc->std & V4L2_STD_525_60) ?
418*4882a593Smuzhiyun 	    (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
norm_minw(struct s2255_vc * vc)421*4882a593Smuzhiyun static int norm_minw(struct s2255_vc *vc)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	return (vc->std & V4L2_STD_525_60) ?
424*4882a593Smuzhiyun 	    LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
norm_minh(struct s2255_vc * vc)427*4882a593Smuzhiyun static int norm_minh(struct s2255_vc *vc)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	return (vc->std & V4L2_STD_525_60) ?
430*4882a593Smuzhiyun 	    (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun /*
435*4882a593Smuzhiyun  * TODO: fixme: move YUV reordering to hardware
436*4882a593Smuzhiyun  * converts 2255 planar format to yuyv or uyvy
437*4882a593Smuzhiyun  */
planar422p_to_yuv_packed(const unsigned char * in,unsigned char * out,int width,int height,int fmt)438*4882a593Smuzhiyun static void planar422p_to_yuv_packed(const unsigned char *in,
439*4882a593Smuzhiyun 				     unsigned char *out,
440*4882a593Smuzhiyun 				     int width, int height,
441*4882a593Smuzhiyun 				     int fmt)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	unsigned char *pY;
444*4882a593Smuzhiyun 	unsigned char *pCb;
445*4882a593Smuzhiyun 	unsigned char *pCr;
446*4882a593Smuzhiyun 	unsigned long size = height * width;
447*4882a593Smuzhiyun 	unsigned int i;
448*4882a593Smuzhiyun 	pY = (unsigned char *)in;
449*4882a593Smuzhiyun 	pCr = (unsigned char *)in + height * width;
450*4882a593Smuzhiyun 	pCb = (unsigned char *)in + height * width + (height * width / 2);
451*4882a593Smuzhiyun 	for (i = 0; i < size * 2; i += 4) {
452*4882a593Smuzhiyun 		out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
453*4882a593Smuzhiyun 		out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
454*4882a593Smuzhiyun 		out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
455*4882a593Smuzhiyun 		out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 	return;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
s2255_reset_dsppower(struct s2255_dev * dev)460*4882a593Smuzhiyun static void s2255_reset_dsppower(struct s2255_dev *dev)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 	s2255_vendor_req(dev, 0x40, 0x0000, 0x0001, NULL, 0, 1);
463*4882a593Smuzhiyun 	msleep(50);
464*4882a593Smuzhiyun 	s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
465*4882a593Smuzhiyun 	msleep(600);
466*4882a593Smuzhiyun 	s2255_vendor_req(dev, 0x10, 0x0000, 0x0000, NULL, 0, 1);
467*4882a593Smuzhiyun 	return;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun /* kickstarts the firmware loading. from probe
471*4882a593Smuzhiyun  */
s2255_timer(struct timer_list * t)472*4882a593Smuzhiyun static void s2255_timer(struct timer_list *t)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun 	struct s2255_dev *dev = from_timer(dev, t, timer);
475*4882a593Smuzhiyun 	struct s2255_fw *data = dev->fw_data;
476*4882a593Smuzhiyun 	if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
477*4882a593Smuzhiyun 		pr_err("s2255: can't submit urb\n");
478*4882a593Smuzhiyun 		atomic_set(&data->fw_state, S2255_FW_FAILED);
479*4882a593Smuzhiyun 		/* wake up anything waiting for the firmware */
480*4882a593Smuzhiyun 		wake_up(&data->wait_fw);
481*4882a593Smuzhiyun 		return;
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun /* this loads the firmware asynchronously.
487*4882a593Smuzhiyun    Originally this was done synchronously in probe.
488*4882a593Smuzhiyun    But it is better to load it asynchronously here than block
489*4882a593Smuzhiyun    inside the probe function. Blocking inside probe affects boot time.
490*4882a593Smuzhiyun    FW loading is triggered by the timer in the probe function
491*4882a593Smuzhiyun */
s2255_fwchunk_complete(struct urb * urb)492*4882a593Smuzhiyun static void s2255_fwchunk_complete(struct urb *urb)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	struct s2255_fw *data = urb->context;
495*4882a593Smuzhiyun 	struct usb_device *udev = urb->dev;
496*4882a593Smuzhiyun 	int len;
497*4882a593Smuzhiyun 	if (urb->status) {
498*4882a593Smuzhiyun 		dev_err(&udev->dev, "URB failed with status %d\n", urb->status);
499*4882a593Smuzhiyun 		atomic_set(&data->fw_state, S2255_FW_FAILED);
500*4882a593Smuzhiyun 		/* wake up anything waiting for the firmware */
501*4882a593Smuzhiyun 		wake_up(&data->wait_fw);
502*4882a593Smuzhiyun 		return;
503*4882a593Smuzhiyun 	}
504*4882a593Smuzhiyun 	if (data->fw_urb == NULL) {
505*4882a593Smuzhiyun 		s2255_dev_err(&udev->dev, "disconnected\n");
506*4882a593Smuzhiyun 		atomic_set(&data->fw_state, S2255_FW_FAILED);
507*4882a593Smuzhiyun 		/* wake up anything waiting for the firmware */
508*4882a593Smuzhiyun 		wake_up(&data->wait_fw);
509*4882a593Smuzhiyun 		return;
510*4882a593Smuzhiyun 	}
511*4882a593Smuzhiyun #define CHUNK_SIZE 512
512*4882a593Smuzhiyun 	/* all USB transfers must be done with continuous kernel memory.
513*4882a593Smuzhiyun 	   can't allocate more than 128k in current linux kernel, so
514*4882a593Smuzhiyun 	   upload the firmware in chunks
515*4882a593Smuzhiyun 	 */
516*4882a593Smuzhiyun 	if (data->fw_loaded < data->fw_size) {
517*4882a593Smuzhiyun 		len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
518*4882a593Smuzhiyun 		    data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 		if (len < CHUNK_SIZE)
521*4882a593Smuzhiyun 			memset(data->pfw_data, 0, CHUNK_SIZE);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 		memcpy(data->pfw_data,
524*4882a593Smuzhiyun 		       (char *) data->fw->data + data->fw_loaded, len);
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 		usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
527*4882a593Smuzhiyun 				  data->pfw_data, CHUNK_SIZE,
528*4882a593Smuzhiyun 				  s2255_fwchunk_complete, data);
529*4882a593Smuzhiyun 		if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
530*4882a593Smuzhiyun 			dev_err(&udev->dev, "failed submit URB\n");
531*4882a593Smuzhiyun 			atomic_set(&data->fw_state, S2255_FW_FAILED);
532*4882a593Smuzhiyun 			/* wake up anything waiting for the firmware */
533*4882a593Smuzhiyun 			wake_up(&data->wait_fw);
534*4882a593Smuzhiyun 			return;
535*4882a593Smuzhiyun 		}
536*4882a593Smuzhiyun 		data->fw_loaded += len;
537*4882a593Smuzhiyun 	} else
538*4882a593Smuzhiyun 		atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
539*4882a593Smuzhiyun 	return;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
s2255_got_frame(struct s2255_vc * vc,int jpgsize)543*4882a593Smuzhiyun static void s2255_got_frame(struct s2255_vc *vc, int jpgsize)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun 	struct s2255_buffer *buf;
546*4882a593Smuzhiyun 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
547*4882a593Smuzhiyun 	unsigned long flags = 0;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	spin_lock_irqsave(&vc->qlock, flags);
550*4882a593Smuzhiyun 	if (list_empty(&vc->buf_list)) {
551*4882a593Smuzhiyun 		dprintk(dev, 1, "No active queue to serve\n");
552*4882a593Smuzhiyun 		spin_unlock_irqrestore(&vc->qlock, flags);
553*4882a593Smuzhiyun 		return;
554*4882a593Smuzhiyun 	}
555*4882a593Smuzhiyun 	buf = list_entry(vc->buf_list.next,
556*4882a593Smuzhiyun 			 struct s2255_buffer, list);
557*4882a593Smuzhiyun 	list_del(&buf->list);
558*4882a593Smuzhiyun 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
559*4882a593Smuzhiyun 	buf->vb.field = vc->field;
560*4882a593Smuzhiyun 	buf->vb.sequence = vc->frame_count;
561*4882a593Smuzhiyun 	spin_unlock_irqrestore(&vc->qlock, flags);
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	s2255_fillbuff(vc, buf, jpgsize);
564*4882a593Smuzhiyun 	/* tell v4l buffer was filled */
565*4882a593Smuzhiyun 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
566*4882a593Smuzhiyun 	dprintk(dev, 2, "%s: [buf] [%p]\n", __func__, buf);
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
format_by_fourcc(int fourcc)569*4882a593Smuzhiyun static const struct s2255_fmt *format_by_fourcc(int fourcc)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	unsigned int i;
572*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(formats); i++) {
573*4882a593Smuzhiyun 		if (-1 == formats[i].fourcc)
574*4882a593Smuzhiyun 			continue;
575*4882a593Smuzhiyun 		if (!jpeg_enable && ((formats[i].fourcc == V4L2_PIX_FMT_JPEG) ||
576*4882a593Smuzhiyun 				     (formats[i].fourcc == V4L2_PIX_FMT_MJPEG)))
577*4882a593Smuzhiyun 			continue;
578*4882a593Smuzhiyun 		if (formats[i].fourcc == fourcc)
579*4882a593Smuzhiyun 			return formats + i;
580*4882a593Smuzhiyun 	}
581*4882a593Smuzhiyun 	return NULL;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun /* video buffer vmalloc implementation based partly on VIVI driver which is
585*4882a593Smuzhiyun  *          Copyright (c) 2006 by
586*4882a593Smuzhiyun  *                  Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
587*4882a593Smuzhiyun  *                  Ted Walther <ted--a.t--enumera.com>
588*4882a593Smuzhiyun  *                  John Sokol <sokol--a.t--videotechnology.com>
589*4882a593Smuzhiyun  *                  http://v4l.videotechnology.com/
590*4882a593Smuzhiyun  *
591*4882a593Smuzhiyun  */
s2255_fillbuff(struct s2255_vc * vc,struct s2255_buffer * buf,int jpgsize)592*4882a593Smuzhiyun static void s2255_fillbuff(struct s2255_vc *vc,
593*4882a593Smuzhiyun 			   struct s2255_buffer *buf, int jpgsize)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	int pos = 0;
596*4882a593Smuzhiyun 	const char *tmpbuf;
597*4882a593Smuzhiyun 	char *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
598*4882a593Smuzhiyun 	unsigned long last_frame;
599*4882a593Smuzhiyun 	struct s2255_dev *dev = vc->dev;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	if (!vbuf)
602*4882a593Smuzhiyun 		return;
603*4882a593Smuzhiyun 	last_frame = vc->last_frame;
604*4882a593Smuzhiyun 	if (last_frame != -1) {
605*4882a593Smuzhiyun 		tmpbuf =
606*4882a593Smuzhiyun 		    (const char *)vc->buffer.frame[last_frame].lpvbits;
607*4882a593Smuzhiyun 		switch (vc->fmt->fourcc) {
608*4882a593Smuzhiyun 		case V4L2_PIX_FMT_YUYV:
609*4882a593Smuzhiyun 		case V4L2_PIX_FMT_UYVY:
610*4882a593Smuzhiyun 			planar422p_to_yuv_packed((const unsigned char *)tmpbuf,
611*4882a593Smuzhiyun 						 vbuf, vc->width,
612*4882a593Smuzhiyun 						 vc->height,
613*4882a593Smuzhiyun 						 vc->fmt->fourcc);
614*4882a593Smuzhiyun 			break;
615*4882a593Smuzhiyun 		case V4L2_PIX_FMT_GREY:
616*4882a593Smuzhiyun 			memcpy(vbuf, tmpbuf, vc->width * vc->height);
617*4882a593Smuzhiyun 			break;
618*4882a593Smuzhiyun 		case V4L2_PIX_FMT_JPEG:
619*4882a593Smuzhiyun 		case V4L2_PIX_FMT_MJPEG:
620*4882a593Smuzhiyun 			vb2_set_plane_payload(&buf->vb.vb2_buf, 0, jpgsize);
621*4882a593Smuzhiyun 			memcpy(vbuf, tmpbuf, jpgsize);
622*4882a593Smuzhiyun 			break;
623*4882a593Smuzhiyun 		case V4L2_PIX_FMT_YUV422P:
624*4882a593Smuzhiyun 			memcpy(vbuf, tmpbuf,
625*4882a593Smuzhiyun 			       vc->width * vc->height * 2);
626*4882a593Smuzhiyun 			break;
627*4882a593Smuzhiyun 		default:
628*4882a593Smuzhiyun 			pr_info("s2255: unknown format?\n");
629*4882a593Smuzhiyun 		}
630*4882a593Smuzhiyun 		vc->last_frame = -1;
631*4882a593Smuzhiyun 	} else {
632*4882a593Smuzhiyun 		pr_err("s2255: =======no frame\n");
633*4882a593Smuzhiyun 		return;
634*4882a593Smuzhiyun 	}
635*4882a593Smuzhiyun 	dprintk(dev, 2, "s2255fill at : Buffer %p size= %d\n",
636*4882a593Smuzhiyun 		vbuf, pos);
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun /* ------------------------------------------------------------------
641*4882a593Smuzhiyun    Videobuf operations
642*4882a593Smuzhiyun    ------------------------------------------------------------------*/
643*4882a593Smuzhiyun 
queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])644*4882a593Smuzhiyun static int queue_setup(struct vb2_queue *vq,
645*4882a593Smuzhiyun 		       unsigned int *nbuffers, unsigned int *nplanes,
646*4882a593Smuzhiyun 		       unsigned int sizes[], struct device *alloc_devs[])
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun 	struct s2255_vc *vc = vb2_get_drv_priv(vq);
649*4882a593Smuzhiyun 	if (*nbuffers < S2255_MIN_BUFS)
650*4882a593Smuzhiyun 		*nbuffers = S2255_MIN_BUFS;
651*4882a593Smuzhiyun 	*nplanes = 1;
652*4882a593Smuzhiyun 	sizes[0] = vc->width * vc->height * (vc->fmt->depth >> 3);
653*4882a593Smuzhiyun 	return 0;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun 
buffer_prepare(struct vb2_buffer * vb)656*4882a593Smuzhiyun static int buffer_prepare(struct vb2_buffer *vb)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun 	struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue);
659*4882a593Smuzhiyun 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
660*4882a593Smuzhiyun 	struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb);
661*4882a593Smuzhiyun 	int w = vc->width;
662*4882a593Smuzhiyun 	int h = vc->height;
663*4882a593Smuzhiyun 	unsigned long size;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	dprintk(vc->dev, 4, "%s\n", __func__);
666*4882a593Smuzhiyun 	if (vc->fmt == NULL)
667*4882a593Smuzhiyun 		return -EINVAL;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	if ((w < norm_minw(vc)) ||
670*4882a593Smuzhiyun 	    (w > norm_maxw(vc)) ||
671*4882a593Smuzhiyun 	    (h < norm_minh(vc)) ||
672*4882a593Smuzhiyun 	    (h > norm_maxh(vc))) {
673*4882a593Smuzhiyun 		dprintk(vc->dev, 4, "invalid buffer prepare\n");
674*4882a593Smuzhiyun 		return -EINVAL;
675*4882a593Smuzhiyun 	}
676*4882a593Smuzhiyun 	size = w * h * (vc->fmt->depth >> 3);
677*4882a593Smuzhiyun 	if (vb2_plane_size(vb, 0) < size) {
678*4882a593Smuzhiyun 		dprintk(vc->dev, 4, "invalid buffer prepare\n");
679*4882a593Smuzhiyun 		return -EINVAL;
680*4882a593Smuzhiyun 	}
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
683*4882a593Smuzhiyun 	return 0;
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
buffer_queue(struct vb2_buffer * vb)686*4882a593Smuzhiyun static void buffer_queue(struct vb2_buffer *vb)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
689*4882a593Smuzhiyun 	struct s2255_buffer *buf = container_of(vbuf, struct s2255_buffer, vb);
690*4882a593Smuzhiyun 	struct s2255_vc *vc = vb2_get_drv_priv(vb->vb2_queue);
691*4882a593Smuzhiyun 	unsigned long flags = 0;
692*4882a593Smuzhiyun 	dprintk(vc->dev, 1, "%s\n", __func__);
693*4882a593Smuzhiyun 	spin_lock_irqsave(&vc->qlock, flags);
694*4882a593Smuzhiyun 	list_add_tail(&buf->list, &vc->buf_list);
695*4882a593Smuzhiyun 	spin_unlock_irqrestore(&vc->qlock, flags);
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun static int start_streaming(struct vb2_queue *vq, unsigned int count);
699*4882a593Smuzhiyun static void stop_streaming(struct vb2_queue *vq);
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun static const struct vb2_ops s2255_video_qops = {
702*4882a593Smuzhiyun 	.queue_setup = queue_setup,
703*4882a593Smuzhiyun 	.buf_prepare = buffer_prepare,
704*4882a593Smuzhiyun 	.buf_queue = buffer_queue,
705*4882a593Smuzhiyun 	.start_streaming = start_streaming,
706*4882a593Smuzhiyun 	.stop_streaming = stop_streaming,
707*4882a593Smuzhiyun 	.wait_prepare = vb2_ops_wait_prepare,
708*4882a593Smuzhiyun 	.wait_finish = vb2_ops_wait_finish,
709*4882a593Smuzhiyun };
710*4882a593Smuzhiyun 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)711*4882a593Smuzhiyun static int vidioc_querycap(struct file *file, void *priv,
712*4882a593Smuzhiyun 			   struct v4l2_capability *cap)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
715*4882a593Smuzhiyun 	struct s2255_dev *dev = vc->dev;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	strscpy(cap->driver, "s2255", sizeof(cap->driver));
718*4882a593Smuzhiyun 	strscpy(cap->card, "s2255", sizeof(cap->card));
719*4882a593Smuzhiyun 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
720*4882a593Smuzhiyun 	return 0;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)723*4882a593Smuzhiyun static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
724*4882a593Smuzhiyun 			       struct v4l2_fmtdesc *f)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun 	int index = f->index;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (index >= ARRAY_SIZE(formats))
729*4882a593Smuzhiyun 		return -EINVAL;
730*4882a593Smuzhiyun 	if (!jpeg_enable && ((formats[index].fourcc == V4L2_PIX_FMT_JPEG) ||
731*4882a593Smuzhiyun 			(formats[index].fourcc == V4L2_PIX_FMT_MJPEG)))
732*4882a593Smuzhiyun 		return -EINVAL;
733*4882a593Smuzhiyun 	f->pixelformat = formats[index].fourcc;
734*4882a593Smuzhiyun 	return 0;
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun 
vidioc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)737*4882a593Smuzhiyun static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
738*4882a593Smuzhiyun 			    struct v4l2_format *f)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
741*4882a593Smuzhiyun 	int is_ntsc = vc->std & V4L2_STD_525_60;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	f->fmt.pix.width = vc->width;
744*4882a593Smuzhiyun 	f->fmt.pix.height = vc->height;
745*4882a593Smuzhiyun 	if (f->fmt.pix.height >=
746*4882a593Smuzhiyun 	    (is_ntsc ? NUM_LINES_1CIFS_NTSC : NUM_LINES_1CIFS_PAL) * 2)
747*4882a593Smuzhiyun 		f->fmt.pix.field = V4L2_FIELD_INTERLACED;
748*4882a593Smuzhiyun 	else
749*4882a593Smuzhiyun 		f->fmt.pix.field = V4L2_FIELD_TOP;
750*4882a593Smuzhiyun 	f->fmt.pix.pixelformat = vc->fmt->fourcc;
751*4882a593Smuzhiyun 	f->fmt.pix.bytesperline = f->fmt.pix.width * (vc->fmt->depth >> 3);
752*4882a593Smuzhiyun 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
753*4882a593Smuzhiyun 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
754*4882a593Smuzhiyun 	return 0;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun 
vidioc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)757*4882a593Smuzhiyun static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
758*4882a593Smuzhiyun 			      struct v4l2_format *f)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	const struct s2255_fmt *fmt;
761*4882a593Smuzhiyun 	enum v4l2_field field;
762*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
763*4882a593Smuzhiyun 	int is_ntsc = vc->std & V4L2_STD_525_60;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (fmt == NULL)
768*4882a593Smuzhiyun 		return -EINVAL;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	field = f->fmt.pix.field;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	dprintk(vc->dev, 50, "%s NTSC: %d suggested width: %d, height: %d\n",
773*4882a593Smuzhiyun 		__func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height);
774*4882a593Smuzhiyun 	if (is_ntsc) {
775*4882a593Smuzhiyun 		/* NTSC */
776*4882a593Smuzhiyun 		if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) {
777*4882a593Smuzhiyun 			f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2;
778*4882a593Smuzhiyun 			field = V4L2_FIELD_INTERLACED;
779*4882a593Smuzhiyun 		} else {
780*4882a593Smuzhiyun 			f->fmt.pix.height = NUM_LINES_1CIFS_NTSC;
781*4882a593Smuzhiyun 			field = V4L2_FIELD_TOP;
782*4882a593Smuzhiyun 		}
783*4882a593Smuzhiyun 		if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC)
784*4882a593Smuzhiyun 			f->fmt.pix.width = LINE_SZ_4CIFS_NTSC;
785*4882a593Smuzhiyun 		else
786*4882a593Smuzhiyun 			f->fmt.pix.width = LINE_SZ_1CIFS_NTSC;
787*4882a593Smuzhiyun 	} else {
788*4882a593Smuzhiyun 		/* PAL */
789*4882a593Smuzhiyun 		if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) {
790*4882a593Smuzhiyun 			f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2;
791*4882a593Smuzhiyun 			field = V4L2_FIELD_INTERLACED;
792*4882a593Smuzhiyun 		} else {
793*4882a593Smuzhiyun 			f->fmt.pix.height = NUM_LINES_1CIFS_PAL;
794*4882a593Smuzhiyun 			field = V4L2_FIELD_TOP;
795*4882a593Smuzhiyun 		}
796*4882a593Smuzhiyun 		if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL)
797*4882a593Smuzhiyun 			f->fmt.pix.width = LINE_SZ_4CIFS_PAL;
798*4882a593Smuzhiyun 		else
799*4882a593Smuzhiyun 			f->fmt.pix.width = LINE_SZ_1CIFS_PAL;
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 	f->fmt.pix.field = field;
802*4882a593Smuzhiyun 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
803*4882a593Smuzhiyun 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
804*4882a593Smuzhiyun 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
805*4882a593Smuzhiyun 	dprintk(vc->dev, 50, "%s: set width %d height %d field %d\n", __func__,
806*4882a593Smuzhiyun 		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
807*4882a593Smuzhiyun 	return 0;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
vidioc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)810*4882a593Smuzhiyun static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
811*4882a593Smuzhiyun 			    struct v4l2_format *f)
812*4882a593Smuzhiyun {
813*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
814*4882a593Smuzhiyun 	const struct s2255_fmt *fmt;
815*4882a593Smuzhiyun 	struct vb2_queue *q = &vc->vb_vidq;
816*4882a593Smuzhiyun 	struct s2255_mode mode;
817*4882a593Smuzhiyun 	int ret;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	ret = vidioc_try_fmt_vid_cap(file, vc, f);
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	if (ret < 0)
822*4882a593Smuzhiyun 		return ret;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	if (fmt == NULL)
827*4882a593Smuzhiyun 		return -EINVAL;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	if (vb2_is_busy(q)) {
830*4882a593Smuzhiyun 		dprintk(vc->dev, 1, "queue busy\n");
831*4882a593Smuzhiyun 		return -EBUSY;
832*4882a593Smuzhiyun 	}
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	mode = vc->mode;
835*4882a593Smuzhiyun 	vc->fmt = fmt;
836*4882a593Smuzhiyun 	vc->width = f->fmt.pix.width;
837*4882a593Smuzhiyun 	vc->height = f->fmt.pix.height;
838*4882a593Smuzhiyun 	vc->field = f->fmt.pix.field;
839*4882a593Smuzhiyun 	if (vc->width > norm_minw(vc)) {
840*4882a593Smuzhiyun 		if (vc->height > norm_minh(vc)) {
841*4882a593Smuzhiyun 			if (vc->cap_parm.capturemode &
842*4882a593Smuzhiyun 			    V4L2_MODE_HIGHQUALITY)
843*4882a593Smuzhiyun 				mode.scale = SCALE_4CIFSI;
844*4882a593Smuzhiyun 			else
845*4882a593Smuzhiyun 				mode.scale = SCALE_4CIFS;
846*4882a593Smuzhiyun 		} else
847*4882a593Smuzhiyun 			mode.scale = SCALE_2CIFS;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	} else {
850*4882a593Smuzhiyun 		mode.scale = SCALE_1CIFS;
851*4882a593Smuzhiyun 	}
852*4882a593Smuzhiyun 	/* color mode */
853*4882a593Smuzhiyun 	switch (vc->fmt->fourcc) {
854*4882a593Smuzhiyun 	case V4L2_PIX_FMT_GREY:
855*4882a593Smuzhiyun 		mode.color &= ~MASK_COLOR;
856*4882a593Smuzhiyun 		mode.color |= COLOR_Y8;
857*4882a593Smuzhiyun 		break;
858*4882a593Smuzhiyun 	case V4L2_PIX_FMT_JPEG:
859*4882a593Smuzhiyun 	case V4L2_PIX_FMT_MJPEG:
860*4882a593Smuzhiyun 		mode.color &= ~MASK_COLOR;
861*4882a593Smuzhiyun 		mode.color |= COLOR_JPG;
862*4882a593Smuzhiyun 		mode.color |= (vc->jpegqual << 8);
863*4882a593Smuzhiyun 		break;
864*4882a593Smuzhiyun 	case V4L2_PIX_FMT_YUV422P:
865*4882a593Smuzhiyun 		mode.color &= ~MASK_COLOR;
866*4882a593Smuzhiyun 		mode.color |= COLOR_YUVPL;
867*4882a593Smuzhiyun 		break;
868*4882a593Smuzhiyun 	case V4L2_PIX_FMT_YUYV:
869*4882a593Smuzhiyun 	case V4L2_PIX_FMT_UYVY:
870*4882a593Smuzhiyun 	default:
871*4882a593Smuzhiyun 		mode.color &= ~MASK_COLOR;
872*4882a593Smuzhiyun 		mode.color |= COLOR_YUVPK;
873*4882a593Smuzhiyun 		break;
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 	if ((mode.color & MASK_COLOR) != (vc->mode.color & MASK_COLOR))
876*4882a593Smuzhiyun 		mode.restart = 1;
877*4882a593Smuzhiyun 	else if (mode.scale != vc->mode.scale)
878*4882a593Smuzhiyun 		mode.restart = 1;
879*4882a593Smuzhiyun 	else if (mode.format != vc->mode.format)
880*4882a593Smuzhiyun 		mode.restart = 1;
881*4882a593Smuzhiyun 	vc->mode = mode;
882*4882a593Smuzhiyun 	(void) s2255_set_mode(vc, &mode);
883*4882a593Smuzhiyun 	return 0;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun /* write to the configuration pipe, synchronously */
s2255_write_config(struct usb_device * udev,unsigned char * pbuf,int size)888*4882a593Smuzhiyun static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf,
889*4882a593Smuzhiyun 			      int size)
890*4882a593Smuzhiyun {
891*4882a593Smuzhiyun 	int pipe;
892*4882a593Smuzhiyun 	int done;
893*4882a593Smuzhiyun 	long retval = -1;
894*4882a593Smuzhiyun 	if (udev) {
895*4882a593Smuzhiyun 		pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP);
896*4882a593Smuzhiyun 		retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500);
897*4882a593Smuzhiyun 	}
898*4882a593Smuzhiyun 	return retval;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun 
get_transfer_size(struct s2255_mode * mode)901*4882a593Smuzhiyun static u32 get_transfer_size(struct s2255_mode *mode)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun 	int linesPerFrame = LINE_SZ_DEF;
904*4882a593Smuzhiyun 	int pixelsPerLine = NUM_LINES_DEF;
905*4882a593Smuzhiyun 	u32 outImageSize;
906*4882a593Smuzhiyun 	u32 usbInSize;
907*4882a593Smuzhiyun 	unsigned int mask_mult;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	if (mode == NULL)
910*4882a593Smuzhiyun 		return 0;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	if (mode->format == FORMAT_NTSC) {
913*4882a593Smuzhiyun 		switch (mode->scale) {
914*4882a593Smuzhiyun 		case SCALE_4CIFS:
915*4882a593Smuzhiyun 		case SCALE_4CIFSI:
916*4882a593Smuzhiyun 			linesPerFrame = NUM_LINES_4CIFS_NTSC * 2;
917*4882a593Smuzhiyun 			pixelsPerLine = LINE_SZ_4CIFS_NTSC;
918*4882a593Smuzhiyun 			break;
919*4882a593Smuzhiyun 		case SCALE_2CIFS:
920*4882a593Smuzhiyun 			linesPerFrame = NUM_LINES_2CIFS_NTSC;
921*4882a593Smuzhiyun 			pixelsPerLine = LINE_SZ_2CIFS_NTSC;
922*4882a593Smuzhiyun 			break;
923*4882a593Smuzhiyun 		case SCALE_1CIFS:
924*4882a593Smuzhiyun 			linesPerFrame = NUM_LINES_1CIFS_NTSC;
925*4882a593Smuzhiyun 			pixelsPerLine = LINE_SZ_1CIFS_NTSC;
926*4882a593Smuzhiyun 			break;
927*4882a593Smuzhiyun 		default:
928*4882a593Smuzhiyun 			break;
929*4882a593Smuzhiyun 		}
930*4882a593Smuzhiyun 	} else if (mode->format == FORMAT_PAL) {
931*4882a593Smuzhiyun 		switch (mode->scale) {
932*4882a593Smuzhiyun 		case SCALE_4CIFS:
933*4882a593Smuzhiyun 		case SCALE_4CIFSI:
934*4882a593Smuzhiyun 			linesPerFrame = NUM_LINES_4CIFS_PAL * 2;
935*4882a593Smuzhiyun 			pixelsPerLine = LINE_SZ_4CIFS_PAL;
936*4882a593Smuzhiyun 			break;
937*4882a593Smuzhiyun 		case SCALE_2CIFS:
938*4882a593Smuzhiyun 			linesPerFrame = NUM_LINES_2CIFS_PAL;
939*4882a593Smuzhiyun 			pixelsPerLine = LINE_SZ_2CIFS_PAL;
940*4882a593Smuzhiyun 			break;
941*4882a593Smuzhiyun 		case SCALE_1CIFS:
942*4882a593Smuzhiyun 			linesPerFrame = NUM_LINES_1CIFS_PAL;
943*4882a593Smuzhiyun 			pixelsPerLine = LINE_SZ_1CIFS_PAL;
944*4882a593Smuzhiyun 			break;
945*4882a593Smuzhiyun 		default:
946*4882a593Smuzhiyun 			break;
947*4882a593Smuzhiyun 		}
948*4882a593Smuzhiyun 	}
949*4882a593Smuzhiyun 	outImageSize = linesPerFrame * pixelsPerLine;
950*4882a593Smuzhiyun 	if ((mode->color & MASK_COLOR) != COLOR_Y8) {
951*4882a593Smuzhiyun 		/* 2 bytes/pixel if not monochrome */
952*4882a593Smuzhiyun 		outImageSize *= 2;
953*4882a593Smuzhiyun 	}
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	/* total bytes to send including prefix and 4K padding;
956*4882a593Smuzhiyun 	   must be a multiple of USB_READ_SIZE */
957*4882a593Smuzhiyun 	usbInSize = outImageSize + PREFIX_SIZE;	/* always send prefix */
958*4882a593Smuzhiyun 	mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1;
959*4882a593Smuzhiyun 	/* if size not a multiple of USB_READ_SIZE */
960*4882a593Smuzhiyun 	if (usbInSize & ~mask_mult)
961*4882a593Smuzhiyun 		usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK);
962*4882a593Smuzhiyun 	return usbInSize;
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun 
s2255_print_cfg(struct s2255_dev * sdev,struct s2255_mode * mode)965*4882a593Smuzhiyun static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode)
966*4882a593Smuzhiyun {
967*4882a593Smuzhiyun 	struct device *dev = &sdev->udev->dev;
968*4882a593Smuzhiyun 	dev_info(dev, "------------------------------------------------\n");
969*4882a593Smuzhiyun 	dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale);
970*4882a593Smuzhiyun 	dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color);
971*4882a593Smuzhiyun 	dev_info(dev, "bright: 0x%x\n", mode->bright);
972*4882a593Smuzhiyun 	dev_info(dev, "------------------------------------------------\n");
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun /*
976*4882a593Smuzhiyun  * set mode is the function which controls the DSP.
977*4882a593Smuzhiyun  * the restart parameter in struct s2255_mode should be set whenever
978*4882a593Smuzhiyun  * the image size could change via color format, video system or image
979*4882a593Smuzhiyun  * size.
980*4882a593Smuzhiyun  * When the restart parameter is set, we sleep for ONE frame to allow the
981*4882a593Smuzhiyun  * DSP time to get the new frame
982*4882a593Smuzhiyun  */
s2255_set_mode(struct s2255_vc * vc,struct s2255_mode * mode)983*4882a593Smuzhiyun static int s2255_set_mode(struct s2255_vc *vc,
984*4882a593Smuzhiyun 			  struct s2255_mode *mode)
985*4882a593Smuzhiyun {
986*4882a593Smuzhiyun 	int res;
987*4882a593Smuzhiyun 	unsigned long chn_rev;
988*4882a593Smuzhiyun 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
989*4882a593Smuzhiyun 	int i;
990*4882a593Smuzhiyun 	__le32 *buffer = dev->cmdbuf;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	mutex_lock(&dev->cmdlock);
993*4882a593Smuzhiyun 	chn_rev = G_chnmap[vc->idx];
994*4882a593Smuzhiyun 	dprintk(dev, 3, "%s channel: %d\n", __func__, vc->idx);
995*4882a593Smuzhiyun 	/* if JPEG, set the quality */
996*4882a593Smuzhiyun 	if ((mode->color & MASK_COLOR) == COLOR_JPG) {
997*4882a593Smuzhiyun 		mode->color &= ~MASK_COLOR;
998*4882a593Smuzhiyun 		mode->color |= COLOR_JPG;
999*4882a593Smuzhiyun 		mode->color &= ~MASK_JPG_QUALITY;
1000*4882a593Smuzhiyun 		mode->color |= (vc->jpegqual << 8);
1001*4882a593Smuzhiyun 	}
1002*4882a593Smuzhiyun 	/* save the mode */
1003*4882a593Smuzhiyun 	vc->mode = *mode;
1004*4882a593Smuzhiyun 	vc->req_image_size = get_transfer_size(mode);
1005*4882a593Smuzhiyun 	dprintk(dev, 1, "%s: reqsize %ld\n", __func__, vc->req_image_size);
1006*4882a593Smuzhiyun 	/* set the mode */
1007*4882a593Smuzhiyun 	buffer[0] = IN_DATA_TOKEN;
1008*4882a593Smuzhiyun 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
1009*4882a593Smuzhiyun 	buffer[2] = CMD_SET_MODE;
1010*4882a593Smuzhiyun 	for (i = 0; i < sizeof(struct s2255_mode) / sizeof(u32); i++)
1011*4882a593Smuzhiyun 		buffer[3 + i] = cpu_to_le32(((u32 *)&vc->mode)[i]);
1012*4882a593Smuzhiyun 	vc->setmode_ready = 0;
1013*4882a593Smuzhiyun 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1014*4882a593Smuzhiyun 	if (debug)
1015*4882a593Smuzhiyun 		s2255_print_cfg(dev, mode);
1016*4882a593Smuzhiyun 	/* wait at least 3 frames before continuing */
1017*4882a593Smuzhiyun 	if (mode->restart) {
1018*4882a593Smuzhiyun 		wait_event_timeout(vc->wait_setmode,
1019*4882a593Smuzhiyun 				   (vc->setmode_ready != 0),
1020*4882a593Smuzhiyun 				   msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1021*4882a593Smuzhiyun 		if (vc->setmode_ready != 1) {
1022*4882a593Smuzhiyun 			dprintk(dev, 0, "s2255: no set mode response\n");
1023*4882a593Smuzhiyun 			res = -EFAULT;
1024*4882a593Smuzhiyun 		}
1025*4882a593Smuzhiyun 	}
1026*4882a593Smuzhiyun 	/* clear the restart flag */
1027*4882a593Smuzhiyun 	vc->mode.restart = 0;
1028*4882a593Smuzhiyun 	dprintk(dev, 1, "%s chn %d, result: %d\n", __func__, vc->idx, res);
1029*4882a593Smuzhiyun 	mutex_unlock(&dev->cmdlock);
1030*4882a593Smuzhiyun 	return res;
1031*4882a593Smuzhiyun }
1032*4882a593Smuzhiyun 
s2255_cmd_status(struct s2255_vc * vc,u32 * pstatus)1033*4882a593Smuzhiyun static int s2255_cmd_status(struct s2255_vc *vc, u32 *pstatus)
1034*4882a593Smuzhiyun {
1035*4882a593Smuzhiyun 	int res;
1036*4882a593Smuzhiyun 	u32 chn_rev;
1037*4882a593Smuzhiyun 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
1038*4882a593Smuzhiyun 	__le32 *buffer = dev->cmdbuf;
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	mutex_lock(&dev->cmdlock);
1041*4882a593Smuzhiyun 	chn_rev = G_chnmap[vc->idx];
1042*4882a593Smuzhiyun 	dprintk(dev, 4, "%s chan %d\n", __func__, vc->idx);
1043*4882a593Smuzhiyun 	/* form the get vid status command */
1044*4882a593Smuzhiyun 	buffer[0] = IN_DATA_TOKEN;
1045*4882a593Smuzhiyun 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
1046*4882a593Smuzhiyun 	buffer[2] = CMD_STATUS;
1047*4882a593Smuzhiyun 	*pstatus = 0;
1048*4882a593Smuzhiyun 	vc->vidstatus_ready = 0;
1049*4882a593Smuzhiyun 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
1050*4882a593Smuzhiyun 	wait_event_timeout(vc->wait_vidstatus,
1051*4882a593Smuzhiyun 			   (vc->vidstatus_ready != 0),
1052*4882a593Smuzhiyun 			   msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT));
1053*4882a593Smuzhiyun 	if (vc->vidstatus_ready != 1) {
1054*4882a593Smuzhiyun 		dprintk(dev, 0, "s2255: no vidstatus response\n");
1055*4882a593Smuzhiyun 		res = -EFAULT;
1056*4882a593Smuzhiyun 	}
1057*4882a593Smuzhiyun 	*pstatus = vc->vidstatus;
1058*4882a593Smuzhiyun 	dprintk(dev, 4, "%s, vid status %d\n", __func__, *pstatus);
1059*4882a593Smuzhiyun 	mutex_unlock(&dev->cmdlock);
1060*4882a593Smuzhiyun 	return res;
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun 
start_streaming(struct vb2_queue * vq,unsigned int count)1063*4882a593Smuzhiyun static int start_streaming(struct vb2_queue *vq, unsigned int count)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun 	struct s2255_vc *vc = vb2_get_drv_priv(vq);
1066*4882a593Smuzhiyun 	int j;
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	vc->last_frame = -1;
1069*4882a593Smuzhiyun 	vc->bad_payload = 0;
1070*4882a593Smuzhiyun 	vc->cur_frame = 0;
1071*4882a593Smuzhiyun 	vc->frame_count = 0;
1072*4882a593Smuzhiyun 	for (j = 0; j < SYS_FRAMES; j++) {
1073*4882a593Smuzhiyun 		vc->buffer.frame[j].ulState = S2255_READ_IDLE;
1074*4882a593Smuzhiyun 		vc->buffer.frame[j].cur_size = 0;
1075*4882a593Smuzhiyun 	}
1076*4882a593Smuzhiyun 	return s2255_start_acquire(vc);
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun /* abort streaming and wait for last buffer */
stop_streaming(struct vb2_queue * vq)1080*4882a593Smuzhiyun static void stop_streaming(struct vb2_queue *vq)
1081*4882a593Smuzhiyun {
1082*4882a593Smuzhiyun 	struct s2255_vc *vc = vb2_get_drv_priv(vq);
1083*4882a593Smuzhiyun 	struct s2255_buffer *buf, *node;
1084*4882a593Smuzhiyun 	unsigned long flags;
1085*4882a593Smuzhiyun 	(void) s2255_stop_acquire(vc);
1086*4882a593Smuzhiyun 	spin_lock_irqsave(&vc->qlock, flags);
1087*4882a593Smuzhiyun 	list_for_each_entry_safe(buf, node, &vc->buf_list, list) {
1088*4882a593Smuzhiyun 		list_del(&buf->list);
1089*4882a593Smuzhiyun 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1090*4882a593Smuzhiyun 		dprintk(vc->dev, 2, "[%p/%d] done\n",
1091*4882a593Smuzhiyun 			buf, buf->vb.vb2_buf.index);
1092*4882a593Smuzhiyun 	}
1093*4882a593Smuzhiyun 	spin_unlock_irqrestore(&vc->qlock, flags);
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun 
vidioc_s_std(struct file * file,void * priv,v4l2_std_id i)1096*4882a593Smuzhiyun static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id i)
1097*4882a593Smuzhiyun {
1098*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1099*4882a593Smuzhiyun 	struct s2255_mode mode;
1100*4882a593Smuzhiyun 	struct vb2_queue *q = &vc->vb_vidq;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	/*
1103*4882a593Smuzhiyun 	 * Changing the standard implies a format change, which is not allowed
1104*4882a593Smuzhiyun 	 * while buffers for use with streaming have already been allocated.
1105*4882a593Smuzhiyun 	 */
1106*4882a593Smuzhiyun 	if (vb2_is_busy(q))
1107*4882a593Smuzhiyun 		return -EBUSY;
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	mode = vc->mode;
1110*4882a593Smuzhiyun 	if (i & V4L2_STD_525_60) {
1111*4882a593Smuzhiyun 		dprintk(vc->dev, 4, "%s 60 Hz\n", __func__);
1112*4882a593Smuzhiyun 		/* if changing format, reset frame decimation/intervals */
1113*4882a593Smuzhiyun 		if (mode.format != FORMAT_NTSC) {
1114*4882a593Smuzhiyun 			mode.restart = 1;
1115*4882a593Smuzhiyun 			mode.format = FORMAT_NTSC;
1116*4882a593Smuzhiyun 			mode.fdec = FDEC_1;
1117*4882a593Smuzhiyun 			vc->width = LINE_SZ_4CIFS_NTSC;
1118*4882a593Smuzhiyun 			vc->height = NUM_LINES_4CIFS_NTSC * 2;
1119*4882a593Smuzhiyun 		}
1120*4882a593Smuzhiyun 	} else if (i & V4L2_STD_625_50) {
1121*4882a593Smuzhiyun 		dprintk(vc->dev, 4, "%s 50 Hz\n", __func__);
1122*4882a593Smuzhiyun 		if (mode.format != FORMAT_PAL) {
1123*4882a593Smuzhiyun 			mode.restart = 1;
1124*4882a593Smuzhiyun 			mode.format = FORMAT_PAL;
1125*4882a593Smuzhiyun 			mode.fdec = FDEC_1;
1126*4882a593Smuzhiyun 			vc->width = LINE_SZ_4CIFS_PAL;
1127*4882a593Smuzhiyun 			vc->height = NUM_LINES_4CIFS_PAL * 2;
1128*4882a593Smuzhiyun 		}
1129*4882a593Smuzhiyun 	} else
1130*4882a593Smuzhiyun 		return -EINVAL;
1131*4882a593Smuzhiyun 	vc->std = i;
1132*4882a593Smuzhiyun 	if (mode.restart)
1133*4882a593Smuzhiyun 		s2255_set_mode(vc, &mode);
1134*4882a593Smuzhiyun 	return 0;
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun 
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * i)1137*4882a593Smuzhiyun static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *i)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	*i = vc->std;
1142*4882a593Smuzhiyun 	return 0;
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun /* Sensoray 2255 is a multiple channel capture device.
1146*4882a593Smuzhiyun    It does not have a "crossbar" of inputs.
1147*4882a593Smuzhiyun    We use one V4L device per channel. The user must
1148*4882a593Smuzhiyun    be aware that certain combinations are not allowed.
1149*4882a593Smuzhiyun    For instance, you cannot do full FPS on more than 2 channels(2 videodevs)
1150*4882a593Smuzhiyun    at once in color(you can do full fps on 4 channels with greyscale.
1151*4882a593Smuzhiyun */
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * inp)1152*4882a593Smuzhiyun static int vidioc_enum_input(struct file *file, void *priv,
1153*4882a593Smuzhiyun 			     struct v4l2_input *inp)
1154*4882a593Smuzhiyun {
1155*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1156*4882a593Smuzhiyun 	struct s2255_dev *dev = vc->dev;
1157*4882a593Smuzhiyun 	u32 status = 0;
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	if (inp->index != 0)
1160*4882a593Smuzhiyun 		return -EINVAL;
1161*4882a593Smuzhiyun 	inp->type = V4L2_INPUT_TYPE_CAMERA;
1162*4882a593Smuzhiyun 	inp->std = S2255_NORMS;
1163*4882a593Smuzhiyun 	inp->status = 0;
1164*4882a593Smuzhiyun 	if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) {
1165*4882a593Smuzhiyun 		int rc;
1166*4882a593Smuzhiyun 		rc = s2255_cmd_status(vc, &status);
1167*4882a593Smuzhiyun 		dprintk(dev, 4, "s2255_cmd_status rc: %d status %x\n",
1168*4882a593Smuzhiyun 			rc, status);
1169*4882a593Smuzhiyun 		if (rc == 0)
1170*4882a593Smuzhiyun 			inp->status =  (status & 0x01) ? 0
1171*4882a593Smuzhiyun 				: V4L2_IN_ST_NO_SIGNAL;
1172*4882a593Smuzhiyun 	}
1173*4882a593Smuzhiyun 	switch (dev->pid) {
1174*4882a593Smuzhiyun 	case 0x2255:
1175*4882a593Smuzhiyun 	default:
1176*4882a593Smuzhiyun 		strscpy(inp->name, "Composite", sizeof(inp->name));
1177*4882a593Smuzhiyun 		break;
1178*4882a593Smuzhiyun 	case 0x2257:
1179*4882a593Smuzhiyun 		strscpy(inp->name, (vc->idx < 2) ? "Composite" : "S-Video",
1180*4882a593Smuzhiyun 			sizeof(inp->name));
1181*4882a593Smuzhiyun 		break;
1182*4882a593Smuzhiyun 	}
1183*4882a593Smuzhiyun 	return 0;
1184*4882a593Smuzhiyun }
1185*4882a593Smuzhiyun 
vidioc_g_input(struct file * file,void * priv,unsigned int * i)1186*4882a593Smuzhiyun static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun 	*i = 0;
1189*4882a593Smuzhiyun 	return 0;
1190*4882a593Smuzhiyun }
vidioc_s_input(struct file * file,void * priv,unsigned int i)1191*4882a593Smuzhiyun static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1192*4882a593Smuzhiyun {
1193*4882a593Smuzhiyun 	if (i > 0)
1194*4882a593Smuzhiyun 		return -EINVAL;
1195*4882a593Smuzhiyun 	return 0;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun 
s2255_s_ctrl(struct v4l2_ctrl * ctrl)1198*4882a593Smuzhiyun static int s2255_s_ctrl(struct v4l2_ctrl *ctrl)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun 	struct s2255_vc *vc =
1201*4882a593Smuzhiyun 		container_of(ctrl->handler, struct s2255_vc, hdl);
1202*4882a593Smuzhiyun 	struct s2255_mode mode;
1203*4882a593Smuzhiyun 	mode = vc->mode;
1204*4882a593Smuzhiyun 	/* update the mode to the corresponding value */
1205*4882a593Smuzhiyun 	switch (ctrl->id) {
1206*4882a593Smuzhiyun 	case V4L2_CID_BRIGHTNESS:
1207*4882a593Smuzhiyun 		mode.bright = ctrl->val;
1208*4882a593Smuzhiyun 		break;
1209*4882a593Smuzhiyun 	case V4L2_CID_CONTRAST:
1210*4882a593Smuzhiyun 		mode.contrast = ctrl->val;
1211*4882a593Smuzhiyun 		break;
1212*4882a593Smuzhiyun 	case V4L2_CID_HUE:
1213*4882a593Smuzhiyun 		mode.hue = ctrl->val;
1214*4882a593Smuzhiyun 		break;
1215*4882a593Smuzhiyun 	case V4L2_CID_SATURATION:
1216*4882a593Smuzhiyun 		mode.saturation = ctrl->val;
1217*4882a593Smuzhiyun 		break;
1218*4882a593Smuzhiyun 	case V4L2_CID_S2255_COLORFILTER:
1219*4882a593Smuzhiyun 		mode.color &= ~MASK_INPUT_TYPE;
1220*4882a593Smuzhiyun 		mode.color |= !ctrl->val << 16;
1221*4882a593Smuzhiyun 		break;
1222*4882a593Smuzhiyun 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1223*4882a593Smuzhiyun 		vc->jpegqual = ctrl->val;
1224*4882a593Smuzhiyun 		return 0;
1225*4882a593Smuzhiyun 	default:
1226*4882a593Smuzhiyun 		return -EINVAL;
1227*4882a593Smuzhiyun 	}
1228*4882a593Smuzhiyun 	mode.restart = 0;
1229*4882a593Smuzhiyun 	/* set mode here.  Note: stream does not need restarted.
1230*4882a593Smuzhiyun 	   some V4L programs restart stream unnecessarily
1231*4882a593Smuzhiyun 	   after a s_crtl.
1232*4882a593Smuzhiyun 	*/
1233*4882a593Smuzhiyun 	s2255_set_mode(vc, &mode);
1234*4882a593Smuzhiyun 	return 0;
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun 
vidioc_g_jpegcomp(struct file * file,void * priv,struct v4l2_jpegcompression * jc)1237*4882a593Smuzhiyun static int vidioc_g_jpegcomp(struct file *file, void *priv,
1238*4882a593Smuzhiyun 			 struct v4l2_jpegcompression *jc)
1239*4882a593Smuzhiyun {
1240*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 	memset(jc, 0, sizeof(*jc));
1243*4882a593Smuzhiyun 	jc->quality = vc->jpegqual;
1244*4882a593Smuzhiyun 	dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality);
1245*4882a593Smuzhiyun 	return 0;
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun 
vidioc_s_jpegcomp(struct file * file,void * priv,const struct v4l2_jpegcompression * jc)1248*4882a593Smuzhiyun static int vidioc_s_jpegcomp(struct file *file, void *priv,
1249*4882a593Smuzhiyun 			 const struct v4l2_jpegcompression *jc)
1250*4882a593Smuzhiyun {
1251*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	if (jc->quality < 0 || jc->quality > 100)
1254*4882a593Smuzhiyun 		return -EINVAL;
1255*4882a593Smuzhiyun 	v4l2_ctrl_s_ctrl(vc->jpegqual_ctrl, jc->quality);
1256*4882a593Smuzhiyun 	dprintk(vc->dev, 2, "%s: quality %d\n", __func__, jc->quality);
1257*4882a593Smuzhiyun 	return 0;
1258*4882a593Smuzhiyun }
1259*4882a593Smuzhiyun 
vidioc_g_parm(struct file * file,void * priv,struct v4l2_streamparm * sp)1260*4882a593Smuzhiyun static int vidioc_g_parm(struct file *file, void *priv,
1261*4882a593Smuzhiyun 			 struct v4l2_streamparm *sp)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun 	__u32 def_num, def_dem;
1264*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1267*4882a593Smuzhiyun 		return -EINVAL;
1268*4882a593Smuzhiyun 	sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1269*4882a593Smuzhiyun 	sp->parm.capture.capturemode = vc->cap_parm.capturemode;
1270*4882a593Smuzhiyun 	sp->parm.capture.readbuffers = S2255_MIN_BUFS;
1271*4882a593Smuzhiyun 	def_num = (vc->mode.format == FORMAT_NTSC) ? 1001 : 1000;
1272*4882a593Smuzhiyun 	def_dem = (vc->mode.format == FORMAT_NTSC) ? 30000 : 25000;
1273*4882a593Smuzhiyun 	sp->parm.capture.timeperframe.denominator = def_dem;
1274*4882a593Smuzhiyun 	switch (vc->mode.fdec) {
1275*4882a593Smuzhiyun 	default:
1276*4882a593Smuzhiyun 	case FDEC_1:
1277*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num;
1278*4882a593Smuzhiyun 		break;
1279*4882a593Smuzhiyun 	case FDEC_2:
1280*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num * 2;
1281*4882a593Smuzhiyun 		break;
1282*4882a593Smuzhiyun 	case FDEC_3:
1283*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num * 3;
1284*4882a593Smuzhiyun 		break;
1285*4882a593Smuzhiyun 	case FDEC_5:
1286*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num * 5;
1287*4882a593Smuzhiyun 		break;
1288*4882a593Smuzhiyun 	}
1289*4882a593Smuzhiyun 	dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d\n",
1290*4882a593Smuzhiyun 		__func__,
1291*4882a593Smuzhiyun 		sp->parm.capture.capturemode,
1292*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator,
1293*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.denominator);
1294*4882a593Smuzhiyun 	return 0;
1295*4882a593Smuzhiyun }
1296*4882a593Smuzhiyun 
vidioc_s_parm(struct file * file,void * priv,struct v4l2_streamparm * sp)1297*4882a593Smuzhiyun static int vidioc_s_parm(struct file *file, void *priv,
1298*4882a593Smuzhiyun 			 struct v4l2_streamparm *sp)
1299*4882a593Smuzhiyun {
1300*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1301*4882a593Smuzhiyun 	struct s2255_mode mode;
1302*4882a593Smuzhiyun 	int fdec = FDEC_1;
1303*4882a593Smuzhiyun 	__u32 def_num, def_dem;
1304*4882a593Smuzhiyun 	if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1305*4882a593Smuzhiyun 		return -EINVAL;
1306*4882a593Smuzhiyun 	mode = vc->mode;
1307*4882a593Smuzhiyun 	/* high quality capture mode requires a stream restart */
1308*4882a593Smuzhiyun 	if ((vc->cap_parm.capturemode != sp->parm.capture.capturemode)
1309*4882a593Smuzhiyun 	    && vb2_is_streaming(&vc->vb_vidq))
1310*4882a593Smuzhiyun 		return -EBUSY;
1311*4882a593Smuzhiyun 	def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000;
1312*4882a593Smuzhiyun 	def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000;
1313*4882a593Smuzhiyun 	if (def_dem != sp->parm.capture.timeperframe.denominator)
1314*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num;
1315*4882a593Smuzhiyun 	else if (sp->parm.capture.timeperframe.numerator <= def_num)
1316*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num;
1317*4882a593Smuzhiyun 	else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) {
1318*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num * 2;
1319*4882a593Smuzhiyun 		fdec = FDEC_2;
1320*4882a593Smuzhiyun 	} else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) {
1321*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num * 3;
1322*4882a593Smuzhiyun 		fdec = FDEC_3;
1323*4882a593Smuzhiyun 	} else {
1324*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator = def_num * 5;
1325*4882a593Smuzhiyun 		fdec = FDEC_5;
1326*4882a593Smuzhiyun 	}
1327*4882a593Smuzhiyun 	mode.fdec = fdec;
1328*4882a593Smuzhiyun 	sp->parm.capture.timeperframe.denominator = def_dem;
1329*4882a593Smuzhiyun 	sp->parm.capture.readbuffers = S2255_MIN_BUFS;
1330*4882a593Smuzhiyun 	s2255_set_mode(vc, &mode);
1331*4882a593Smuzhiyun 	dprintk(vc->dev, 4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n",
1332*4882a593Smuzhiyun 		__func__,
1333*4882a593Smuzhiyun 		sp->parm.capture.capturemode,
1334*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.numerator,
1335*4882a593Smuzhiyun 		sp->parm.capture.timeperframe.denominator, fdec);
1336*4882a593Smuzhiyun 	return 0;
1337*4882a593Smuzhiyun }
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun #define NUM_SIZE_ENUMS 3
1340*4882a593Smuzhiyun static const struct v4l2_frmsize_discrete ntsc_sizes[] = {
1341*4882a593Smuzhiyun 	{ 640, 480 },
1342*4882a593Smuzhiyun 	{ 640, 240 },
1343*4882a593Smuzhiyun 	{ 320, 240 },
1344*4882a593Smuzhiyun };
1345*4882a593Smuzhiyun static const struct v4l2_frmsize_discrete pal_sizes[] = {
1346*4882a593Smuzhiyun 	{ 704, 576 },
1347*4882a593Smuzhiyun 	{ 704, 288 },
1348*4882a593Smuzhiyun 	{ 352, 288 },
1349*4882a593Smuzhiyun };
1350*4882a593Smuzhiyun 
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fe)1351*4882a593Smuzhiyun static int vidioc_enum_framesizes(struct file *file, void *priv,
1352*4882a593Smuzhiyun 			    struct v4l2_frmsizeenum *fe)
1353*4882a593Smuzhiyun {
1354*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1355*4882a593Smuzhiyun 	int is_ntsc = vc->std & V4L2_STD_525_60;
1356*4882a593Smuzhiyun 	const struct s2255_fmt *fmt;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	if (fe->index >= NUM_SIZE_ENUMS)
1359*4882a593Smuzhiyun 		return -EINVAL;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	fmt = format_by_fourcc(fe->pixel_format);
1362*4882a593Smuzhiyun 	if (fmt == NULL)
1363*4882a593Smuzhiyun 		return -EINVAL;
1364*4882a593Smuzhiyun 	fe->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1365*4882a593Smuzhiyun 	fe->discrete = is_ntsc ?  ntsc_sizes[fe->index] : pal_sizes[fe->index];
1366*4882a593Smuzhiyun 	return 0;
1367*4882a593Smuzhiyun }
1368*4882a593Smuzhiyun 
vidioc_enum_frameintervals(struct file * file,void * priv,struct v4l2_frmivalenum * fe)1369*4882a593Smuzhiyun static int vidioc_enum_frameintervals(struct file *file, void *priv,
1370*4882a593Smuzhiyun 			    struct v4l2_frmivalenum *fe)
1371*4882a593Smuzhiyun {
1372*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1373*4882a593Smuzhiyun 	const struct s2255_fmt *fmt;
1374*4882a593Smuzhiyun 	const struct v4l2_frmsize_discrete *sizes;
1375*4882a593Smuzhiyun 	int is_ntsc = vc->std & V4L2_STD_525_60;
1376*4882a593Smuzhiyun #define NUM_FRAME_ENUMS 4
1377*4882a593Smuzhiyun 	int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5};
1378*4882a593Smuzhiyun 	int i;
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	if (fe->index >= NUM_FRAME_ENUMS)
1381*4882a593Smuzhiyun 		return -EINVAL;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	fmt = format_by_fourcc(fe->pixel_format);
1384*4882a593Smuzhiyun 	if (fmt == NULL)
1385*4882a593Smuzhiyun 		return -EINVAL;
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	sizes = is_ntsc ? ntsc_sizes : pal_sizes;
1388*4882a593Smuzhiyun 	for (i = 0; i < NUM_SIZE_ENUMS; i++, sizes++)
1389*4882a593Smuzhiyun 		if (fe->width == sizes->width &&
1390*4882a593Smuzhiyun 		    fe->height == sizes->height)
1391*4882a593Smuzhiyun 			break;
1392*4882a593Smuzhiyun 	if (i == NUM_SIZE_ENUMS)
1393*4882a593Smuzhiyun 		return -EINVAL;
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 	fe->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1396*4882a593Smuzhiyun 	fe->discrete.denominator = is_ntsc ? 30000 : 25000;
1397*4882a593Smuzhiyun 	fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index];
1398*4882a593Smuzhiyun 	dprintk(vc->dev, 4, "%s discrete %d/%d\n", __func__,
1399*4882a593Smuzhiyun 		fe->discrete.numerator,
1400*4882a593Smuzhiyun 		fe->discrete.denominator);
1401*4882a593Smuzhiyun 	return 0;
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun 
s2255_open(struct file * file)1404*4882a593Smuzhiyun static int s2255_open(struct file *file)
1405*4882a593Smuzhiyun {
1406*4882a593Smuzhiyun 	struct s2255_vc *vc = video_drvdata(file);
1407*4882a593Smuzhiyun 	struct s2255_dev *dev = vc->dev;
1408*4882a593Smuzhiyun 	int state;
1409*4882a593Smuzhiyun 	int rc = 0;
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	rc = v4l2_fh_open(file);
1412*4882a593Smuzhiyun 	if (rc != 0)
1413*4882a593Smuzhiyun 		return rc;
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 	dprintk(dev, 1, "s2255: %s\n", __func__);
1416*4882a593Smuzhiyun 	state = atomic_read(&dev->fw_data->fw_state);
1417*4882a593Smuzhiyun 	switch (state) {
1418*4882a593Smuzhiyun 	case S2255_FW_DISCONNECTING:
1419*4882a593Smuzhiyun 		return -ENODEV;
1420*4882a593Smuzhiyun 	case S2255_FW_FAILED:
1421*4882a593Smuzhiyun 		s2255_dev_err(&dev->udev->dev,
1422*4882a593Smuzhiyun 			"firmware load failed. retrying.\n");
1423*4882a593Smuzhiyun 		s2255_fwload_start(dev);
1424*4882a593Smuzhiyun 		wait_event_timeout(dev->fw_data->wait_fw,
1425*4882a593Smuzhiyun 				   ((atomic_read(&dev->fw_data->fw_state)
1426*4882a593Smuzhiyun 				     == S2255_FW_SUCCESS) ||
1427*4882a593Smuzhiyun 				    (atomic_read(&dev->fw_data->fw_state)
1428*4882a593Smuzhiyun 				     == S2255_FW_DISCONNECTING)),
1429*4882a593Smuzhiyun 				   msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1430*4882a593Smuzhiyun 		/* state may have changed, re-read */
1431*4882a593Smuzhiyun 		state = atomic_read(&dev->fw_data->fw_state);
1432*4882a593Smuzhiyun 		break;
1433*4882a593Smuzhiyun 	case S2255_FW_NOTLOADED:
1434*4882a593Smuzhiyun 	case S2255_FW_LOADED_DSPWAIT:
1435*4882a593Smuzhiyun 		/* give S2255_LOAD_TIMEOUT time for firmware to load in case
1436*4882a593Smuzhiyun 		   driver loaded and then device immediately opened */
1437*4882a593Smuzhiyun 		pr_info("%s waiting for firmware load\n", __func__);
1438*4882a593Smuzhiyun 		wait_event_timeout(dev->fw_data->wait_fw,
1439*4882a593Smuzhiyun 				   ((atomic_read(&dev->fw_data->fw_state)
1440*4882a593Smuzhiyun 				     == S2255_FW_SUCCESS) ||
1441*4882a593Smuzhiyun 				    (atomic_read(&dev->fw_data->fw_state)
1442*4882a593Smuzhiyun 				     == S2255_FW_DISCONNECTING)),
1443*4882a593Smuzhiyun 				   msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1444*4882a593Smuzhiyun 		/* state may have changed, re-read */
1445*4882a593Smuzhiyun 		state = atomic_read(&dev->fw_data->fw_state);
1446*4882a593Smuzhiyun 		break;
1447*4882a593Smuzhiyun 	case S2255_FW_SUCCESS:
1448*4882a593Smuzhiyun 	default:
1449*4882a593Smuzhiyun 		break;
1450*4882a593Smuzhiyun 	}
1451*4882a593Smuzhiyun 	/* state may have changed in above switch statement */
1452*4882a593Smuzhiyun 	switch (state) {
1453*4882a593Smuzhiyun 	case S2255_FW_SUCCESS:
1454*4882a593Smuzhiyun 		break;
1455*4882a593Smuzhiyun 	case S2255_FW_FAILED:
1456*4882a593Smuzhiyun 		pr_info("2255 firmware load failed.\n");
1457*4882a593Smuzhiyun 		return -ENODEV;
1458*4882a593Smuzhiyun 	case S2255_FW_DISCONNECTING:
1459*4882a593Smuzhiyun 		pr_info("%s: disconnecting\n", __func__);
1460*4882a593Smuzhiyun 		return -ENODEV;
1461*4882a593Smuzhiyun 	case S2255_FW_LOADED_DSPWAIT:
1462*4882a593Smuzhiyun 	case S2255_FW_NOTLOADED:
1463*4882a593Smuzhiyun 		pr_info("%s: firmware not loaded, please retry\n",
1464*4882a593Smuzhiyun 			__func__);
1465*4882a593Smuzhiyun 		/*
1466*4882a593Smuzhiyun 		 * Timeout on firmware load means device unusable.
1467*4882a593Smuzhiyun 		 * Set firmware failure state.
1468*4882a593Smuzhiyun 		 * On next s2255_open the firmware will be reloaded.
1469*4882a593Smuzhiyun 		 */
1470*4882a593Smuzhiyun 		atomic_set(&dev->fw_data->fw_state,
1471*4882a593Smuzhiyun 			   S2255_FW_FAILED);
1472*4882a593Smuzhiyun 		return -EAGAIN;
1473*4882a593Smuzhiyun 	default:
1474*4882a593Smuzhiyun 		pr_info("%s: unknown state\n", __func__);
1475*4882a593Smuzhiyun 		return -EFAULT;
1476*4882a593Smuzhiyun 	}
1477*4882a593Smuzhiyun 	if (!vc->configured) {
1478*4882a593Smuzhiyun 		/* configure channel to default state */
1479*4882a593Smuzhiyun 		vc->fmt = &formats[0];
1480*4882a593Smuzhiyun 		s2255_set_mode(vc, &vc->mode);
1481*4882a593Smuzhiyun 		vc->configured = 1;
1482*4882a593Smuzhiyun 	}
1483*4882a593Smuzhiyun 	return 0;
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun 
s2255_destroy(struct s2255_dev * dev)1486*4882a593Smuzhiyun static void s2255_destroy(struct s2255_dev *dev)
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun 	dprintk(dev, 1, "%s", __func__);
1489*4882a593Smuzhiyun 	/* board shutdown stops the read pipe if it is running */
1490*4882a593Smuzhiyun 	s2255_board_shutdown(dev);
1491*4882a593Smuzhiyun 	/* make sure firmware still not trying to load */
1492*4882a593Smuzhiyun 	del_timer_sync(&dev->timer);  /* only started in .probe and .open */
1493*4882a593Smuzhiyun 	if (dev->fw_data->fw_urb) {
1494*4882a593Smuzhiyun 		usb_kill_urb(dev->fw_data->fw_urb);
1495*4882a593Smuzhiyun 		usb_free_urb(dev->fw_data->fw_urb);
1496*4882a593Smuzhiyun 		dev->fw_data->fw_urb = NULL;
1497*4882a593Smuzhiyun 	}
1498*4882a593Smuzhiyun 	release_firmware(dev->fw_data->fw);
1499*4882a593Smuzhiyun 	kfree(dev->fw_data->pfw_data);
1500*4882a593Smuzhiyun 	kfree(dev->fw_data);
1501*4882a593Smuzhiyun 	/* reset the DSP so firmware can be reloaded next time */
1502*4882a593Smuzhiyun 	s2255_reset_dsppower(dev);
1503*4882a593Smuzhiyun 	mutex_destroy(&dev->lock);
1504*4882a593Smuzhiyun 	usb_put_dev(dev->udev);
1505*4882a593Smuzhiyun 	v4l2_device_unregister(&dev->v4l2_dev);
1506*4882a593Smuzhiyun 	kfree(dev->cmdbuf);
1507*4882a593Smuzhiyun 	kfree(dev);
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun static const struct v4l2_file_operations s2255_fops_v4l = {
1511*4882a593Smuzhiyun 	.owner = THIS_MODULE,
1512*4882a593Smuzhiyun 	.open = s2255_open,
1513*4882a593Smuzhiyun 	.release = vb2_fop_release,
1514*4882a593Smuzhiyun 	.poll = vb2_fop_poll,
1515*4882a593Smuzhiyun 	.unlocked_ioctl = video_ioctl2,	/* V4L2 ioctl handler */
1516*4882a593Smuzhiyun 	.mmap = vb2_fop_mmap,
1517*4882a593Smuzhiyun 	.read = vb2_fop_read,
1518*4882a593Smuzhiyun };
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun static const struct v4l2_ioctl_ops s2255_ioctl_ops = {
1521*4882a593Smuzhiyun 	.vidioc_querycap = vidioc_querycap,
1522*4882a593Smuzhiyun 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1523*4882a593Smuzhiyun 	.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1524*4882a593Smuzhiyun 	.vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1525*4882a593Smuzhiyun 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1526*4882a593Smuzhiyun 	.vidioc_reqbufs = vb2_ioctl_reqbufs,
1527*4882a593Smuzhiyun 	.vidioc_querybuf = vb2_ioctl_querybuf,
1528*4882a593Smuzhiyun 	.vidioc_qbuf = vb2_ioctl_qbuf,
1529*4882a593Smuzhiyun 	.vidioc_dqbuf = vb2_ioctl_dqbuf,
1530*4882a593Smuzhiyun 	.vidioc_s_std = vidioc_s_std,
1531*4882a593Smuzhiyun 	.vidioc_g_std = vidioc_g_std,
1532*4882a593Smuzhiyun 	.vidioc_enum_input = vidioc_enum_input,
1533*4882a593Smuzhiyun 	.vidioc_g_input = vidioc_g_input,
1534*4882a593Smuzhiyun 	.vidioc_s_input = vidioc_s_input,
1535*4882a593Smuzhiyun 	.vidioc_streamon = vb2_ioctl_streamon,
1536*4882a593Smuzhiyun 	.vidioc_streamoff = vb2_ioctl_streamoff,
1537*4882a593Smuzhiyun 	.vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1538*4882a593Smuzhiyun 	.vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1539*4882a593Smuzhiyun 	.vidioc_s_parm = vidioc_s_parm,
1540*4882a593Smuzhiyun 	.vidioc_g_parm = vidioc_g_parm,
1541*4882a593Smuzhiyun 	.vidioc_enum_framesizes = vidioc_enum_framesizes,
1542*4882a593Smuzhiyun 	.vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1543*4882a593Smuzhiyun 	.vidioc_log_status  = v4l2_ctrl_log_status,
1544*4882a593Smuzhiyun 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1545*4882a593Smuzhiyun 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1546*4882a593Smuzhiyun };
1547*4882a593Smuzhiyun 
s2255_video_device_release(struct video_device * vdev)1548*4882a593Smuzhiyun static void s2255_video_device_release(struct video_device *vdev)
1549*4882a593Smuzhiyun {
1550*4882a593Smuzhiyun 	struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev);
1551*4882a593Smuzhiyun 	struct s2255_vc *vc =
1552*4882a593Smuzhiyun 		container_of(vdev, struct s2255_vc, vdev);
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 	dprintk(dev, 4, "%s, chnls: %d\n", __func__,
1555*4882a593Smuzhiyun 		atomic_read(&dev->num_channels));
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(&vc->hdl);
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 	if (atomic_dec_and_test(&dev->num_channels))
1560*4882a593Smuzhiyun 		s2255_destroy(dev);
1561*4882a593Smuzhiyun 	return;
1562*4882a593Smuzhiyun }
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun static const struct video_device template = {
1565*4882a593Smuzhiyun 	.name = "s2255v",
1566*4882a593Smuzhiyun 	.fops = &s2255_fops_v4l,
1567*4882a593Smuzhiyun 	.ioctl_ops = &s2255_ioctl_ops,
1568*4882a593Smuzhiyun 	.release = s2255_video_device_release,
1569*4882a593Smuzhiyun 	.tvnorms = S2255_NORMS,
1570*4882a593Smuzhiyun };
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun static const struct v4l2_ctrl_ops s2255_ctrl_ops = {
1573*4882a593Smuzhiyun 	.s_ctrl = s2255_s_ctrl,
1574*4882a593Smuzhiyun };
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun static const struct v4l2_ctrl_config color_filter_ctrl = {
1577*4882a593Smuzhiyun 	.ops = &s2255_ctrl_ops,
1578*4882a593Smuzhiyun 	.name = "Color Filter",
1579*4882a593Smuzhiyun 	.id = V4L2_CID_S2255_COLORFILTER,
1580*4882a593Smuzhiyun 	.type = V4L2_CTRL_TYPE_BOOLEAN,
1581*4882a593Smuzhiyun 	.max = 1,
1582*4882a593Smuzhiyun 	.step = 1,
1583*4882a593Smuzhiyun 	.def = 1,
1584*4882a593Smuzhiyun };
1585*4882a593Smuzhiyun 
s2255_probe_v4l(struct s2255_dev * dev)1586*4882a593Smuzhiyun static int s2255_probe_v4l(struct s2255_dev *dev)
1587*4882a593Smuzhiyun {
1588*4882a593Smuzhiyun 	int ret;
1589*4882a593Smuzhiyun 	int i;
1590*4882a593Smuzhiyun 	int cur_nr = video_nr;
1591*4882a593Smuzhiyun 	struct s2255_vc *vc;
1592*4882a593Smuzhiyun 	struct vb2_queue *q;
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev);
1595*4882a593Smuzhiyun 	if (ret)
1596*4882a593Smuzhiyun 		return ret;
1597*4882a593Smuzhiyun 	/* initialize all video 4 linux */
1598*4882a593Smuzhiyun 	/* register 4 video devices */
1599*4882a593Smuzhiyun 	for (i = 0; i < MAX_CHANNELS; i++) {
1600*4882a593Smuzhiyun 		vc = &dev->vc[i];
1601*4882a593Smuzhiyun 		INIT_LIST_HEAD(&vc->buf_list);
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 		v4l2_ctrl_handler_init(&vc->hdl, 6);
1604*4882a593Smuzhiyun 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1605*4882a593Smuzhiyun 				V4L2_CID_BRIGHTNESS, -127, 127, 1, DEF_BRIGHT);
1606*4882a593Smuzhiyun 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1607*4882a593Smuzhiyun 				V4L2_CID_CONTRAST, 0, 255, 1, DEF_CONTRAST);
1608*4882a593Smuzhiyun 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1609*4882a593Smuzhiyun 				V4L2_CID_SATURATION, 0, 255, 1, DEF_SATURATION);
1610*4882a593Smuzhiyun 		v4l2_ctrl_new_std(&vc->hdl, &s2255_ctrl_ops,
1611*4882a593Smuzhiyun 				V4L2_CID_HUE, 0, 255, 1, DEF_HUE);
1612*4882a593Smuzhiyun 		vc->jpegqual_ctrl = v4l2_ctrl_new_std(&vc->hdl,
1613*4882a593Smuzhiyun 				&s2255_ctrl_ops,
1614*4882a593Smuzhiyun 				V4L2_CID_JPEG_COMPRESSION_QUALITY,
1615*4882a593Smuzhiyun 				0, 100, 1, S2255_DEF_JPEG_QUAL);
1616*4882a593Smuzhiyun 		if (dev->dsp_fw_ver >= S2255_MIN_DSP_COLORFILTER &&
1617*4882a593Smuzhiyun 		    (dev->pid != 0x2257 || vc->idx <= 1))
1618*4882a593Smuzhiyun 			v4l2_ctrl_new_custom(&vc->hdl, &color_filter_ctrl,
1619*4882a593Smuzhiyun 					     NULL);
1620*4882a593Smuzhiyun 		if (vc->hdl.error) {
1621*4882a593Smuzhiyun 			ret = vc->hdl.error;
1622*4882a593Smuzhiyun 			v4l2_ctrl_handler_free(&vc->hdl);
1623*4882a593Smuzhiyun 			dev_err(&dev->udev->dev, "couldn't register control\n");
1624*4882a593Smuzhiyun 			break;
1625*4882a593Smuzhiyun 		}
1626*4882a593Smuzhiyun 		q = &vc->vb_vidq;
1627*4882a593Smuzhiyun 		q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1628*4882a593Smuzhiyun 		q->io_modes = VB2_MMAP | VB2_READ | VB2_USERPTR;
1629*4882a593Smuzhiyun 		q->drv_priv = vc;
1630*4882a593Smuzhiyun 		q->lock = &vc->vb_lock;
1631*4882a593Smuzhiyun 		q->buf_struct_size = sizeof(struct s2255_buffer);
1632*4882a593Smuzhiyun 		q->mem_ops = &vb2_vmalloc_memops;
1633*4882a593Smuzhiyun 		q->ops = &s2255_video_qops;
1634*4882a593Smuzhiyun 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1635*4882a593Smuzhiyun 		ret = vb2_queue_init(q);
1636*4882a593Smuzhiyun 		if (ret != 0) {
1637*4882a593Smuzhiyun 			dev_err(&dev->udev->dev,
1638*4882a593Smuzhiyun 				"%s vb2_queue_init 0x%x\n", __func__, ret);
1639*4882a593Smuzhiyun 			break;
1640*4882a593Smuzhiyun 		}
1641*4882a593Smuzhiyun 		/* register video devices */
1642*4882a593Smuzhiyun 		vc->vdev = template;
1643*4882a593Smuzhiyun 		vc->vdev.queue = q;
1644*4882a593Smuzhiyun 		vc->vdev.ctrl_handler = &vc->hdl;
1645*4882a593Smuzhiyun 		vc->vdev.lock = &dev->lock;
1646*4882a593Smuzhiyun 		vc->vdev.v4l2_dev = &dev->v4l2_dev;
1647*4882a593Smuzhiyun 		vc->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1648*4882a593Smuzhiyun 				       V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1649*4882a593Smuzhiyun 		video_set_drvdata(&vc->vdev, vc);
1650*4882a593Smuzhiyun 		if (video_nr == -1)
1651*4882a593Smuzhiyun 			ret = video_register_device(&vc->vdev,
1652*4882a593Smuzhiyun 						    VFL_TYPE_VIDEO,
1653*4882a593Smuzhiyun 						    video_nr);
1654*4882a593Smuzhiyun 		else
1655*4882a593Smuzhiyun 			ret = video_register_device(&vc->vdev,
1656*4882a593Smuzhiyun 						    VFL_TYPE_VIDEO,
1657*4882a593Smuzhiyun 						    cur_nr + i);
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun 		if (ret) {
1660*4882a593Smuzhiyun 			dev_err(&dev->udev->dev,
1661*4882a593Smuzhiyun 				"failed to register video device!\n");
1662*4882a593Smuzhiyun 			break;
1663*4882a593Smuzhiyun 		}
1664*4882a593Smuzhiyun 		atomic_inc(&dev->num_channels);
1665*4882a593Smuzhiyun 		v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1666*4882a593Smuzhiyun 			  video_device_node_name(&vc->vdev));
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun 	}
1669*4882a593Smuzhiyun 	pr_info("Sensoray 2255 V4L driver Revision: %s\n",
1670*4882a593Smuzhiyun 		S2255_VERSION);
1671*4882a593Smuzhiyun 	/* if no channels registered, return error and probe will fail*/
1672*4882a593Smuzhiyun 	if (atomic_read(&dev->num_channels) == 0) {
1673*4882a593Smuzhiyun 		v4l2_device_unregister(&dev->v4l2_dev);
1674*4882a593Smuzhiyun 		return ret;
1675*4882a593Smuzhiyun 	}
1676*4882a593Smuzhiyun 	if (atomic_read(&dev->num_channels) != MAX_CHANNELS)
1677*4882a593Smuzhiyun 		pr_warn("s2255: Not all channels available.\n");
1678*4882a593Smuzhiyun 	return 0;
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun /* this function moves the usb stream read pipe data
1682*4882a593Smuzhiyun  * into the system buffers.
1683*4882a593Smuzhiyun  * returns 0 on success, EAGAIN if more data to process( call this
1684*4882a593Smuzhiyun  * function again).
1685*4882a593Smuzhiyun  *
1686*4882a593Smuzhiyun  * Received frame structure:
1687*4882a593Smuzhiyun  * bytes 0-3:  marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1688*4882a593Smuzhiyun  * bytes 4-7:  channel: 0-3
1689*4882a593Smuzhiyun  * bytes 8-11: payload size:  size of the frame
1690*4882a593Smuzhiyun  * bytes 12-payloadsize+12:  frame data
1691*4882a593Smuzhiyun  */
save_frame(struct s2255_dev * dev,struct s2255_pipeinfo * pipe_info)1692*4882a593Smuzhiyun static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1693*4882a593Smuzhiyun {
1694*4882a593Smuzhiyun 	char *pdest;
1695*4882a593Smuzhiyun 	u32 offset = 0;
1696*4882a593Smuzhiyun 	int bframe = 0;
1697*4882a593Smuzhiyun 	char *psrc;
1698*4882a593Smuzhiyun 	unsigned long copy_size;
1699*4882a593Smuzhiyun 	unsigned long size;
1700*4882a593Smuzhiyun 	s32 idx = -1;
1701*4882a593Smuzhiyun 	struct s2255_framei *frm;
1702*4882a593Smuzhiyun 	unsigned char *pdata;
1703*4882a593Smuzhiyun 	struct s2255_vc *vc;
1704*4882a593Smuzhiyun 	dprintk(dev, 100, "buffer to user\n");
1705*4882a593Smuzhiyun 	vc = &dev->vc[dev->cc];
1706*4882a593Smuzhiyun 	idx = vc->cur_frame;
1707*4882a593Smuzhiyun 	frm = &vc->buffer.frame[idx];
1708*4882a593Smuzhiyun 	if (frm->ulState == S2255_READ_IDLE) {
1709*4882a593Smuzhiyun 		int jj;
1710*4882a593Smuzhiyun 		unsigned int cc;
1711*4882a593Smuzhiyun 		__le32 *pdword; /*data from dsp is little endian */
1712*4882a593Smuzhiyun 		int payload;
1713*4882a593Smuzhiyun 		/* search for marker codes */
1714*4882a593Smuzhiyun 		pdata = (unsigned char *)pipe_info->transfer_buffer;
1715*4882a593Smuzhiyun 		pdword = (__le32 *)pdata;
1716*4882a593Smuzhiyun 		for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1717*4882a593Smuzhiyun 			switch (*pdword) {
1718*4882a593Smuzhiyun 			case S2255_MARKER_FRAME:
1719*4882a593Smuzhiyun 				dprintk(dev, 4, "marker @ offset: %d [%x %x]\n",
1720*4882a593Smuzhiyun 					jj, pdata[0], pdata[1]);
1721*4882a593Smuzhiyun 				offset = jj + PREFIX_SIZE;
1722*4882a593Smuzhiyun 				bframe = 1;
1723*4882a593Smuzhiyun 				cc = le32_to_cpu(pdword[1]);
1724*4882a593Smuzhiyun 				if (cc >= MAX_CHANNELS) {
1725*4882a593Smuzhiyun 					dprintk(dev, 0,
1726*4882a593Smuzhiyun 						"bad channel\n");
1727*4882a593Smuzhiyun 					return -EINVAL;
1728*4882a593Smuzhiyun 				}
1729*4882a593Smuzhiyun 				/* reverse it */
1730*4882a593Smuzhiyun 				dev->cc = G_chnmap[cc];
1731*4882a593Smuzhiyun 				vc = &dev->vc[dev->cc];
1732*4882a593Smuzhiyun 				payload =  le32_to_cpu(pdword[3]);
1733*4882a593Smuzhiyun 				if (payload > vc->req_image_size) {
1734*4882a593Smuzhiyun 					vc->bad_payload++;
1735*4882a593Smuzhiyun 					/* discard the bad frame */
1736*4882a593Smuzhiyun 					return -EINVAL;
1737*4882a593Smuzhiyun 				}
1738*4882a593Smuzhiyun 				vc->pkt_size = payload;
1739*4882a593Smuzhiyun 				vc->jpg_size = le32_to_cpu(pdword[4]);
1740*4882a593Smuzhiyun 				break;
1741*4882a593Smuzhiyun 			case S2255_MARKER_RESPONSE:
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 				pdata += DEF_USB_BLOCK;
1744*4882a593Smuzhiyun 				jj += DEF_USB_BLOCK;
1745*4882a593Smuzhiyun 				if (le32_to_cpu(pdword[1]) >= MAX_CHANNELS)
1746*4882a593Smuzhiyun 					break;
1747*4882a593Smuzhiyun 				cc = G_chnmap[le32_to_cpu(pdword[1])];
1748*4882a593Smuzhiyun 				if (cc >= MAX_CHANNELS)
1749*4882a593Smuzhiyun 					break;
1750*4882a593Smuzhiyun 				vc = &dev->vc[cc];
1751*4882a593Smuzhiyun 				switch (pdword[2]) {
1752*4882a593Smuzhiyun 				case S2255_RESPONSE_SETMODE:
1753*4882a593Smuzhiyun 					/* check if channel valid */
1754*4882a593Smuzhiyun 					/* set mode ready */
1755*4882a593Smuzhiyun 					vc->setmode_ready = 1;
1756*4882a593Smuzhiyun 					wake_up(&vc->wait_setmode);
1757*4882a593Smuzhiyun 					dprintk(dev, 5, "setmode rdy %d\n", cc);
1758*4882a593Smuzhiyun 					break;
1759*4882a593Smuzhiyun 				case S2255_RESPONSE_FW:
1760*4882a593Smuzhiyun 					dev->chn_ready |= (1 << cc);
1761*4882a593Smuzhiyun 					if ((dev->chn_ready & 0x0f) != 0x0f)
1762*4882a593Smuzhiyun 						break;
1763*4882a593Smuzhiyun 					/* all channels ready */
1764*4882a593Smuzhiyun 					pr_info("s2255: fw loaded\n");
1765*4882a593Smuzhiyun 					atomic_set(&dev->fw_data->fw_state,
1766*4882a593Smuzhiyun 						   S2255_FW_SUCCESS);
1767*4882a593Smuzhiyun 					wake_up(&dev->fw_data->wait_fw);
1768*4882a593Smuzhiyun 					break;
1769*4882a593Smuzhiyun 				case S2255_RESPONSE_STATUS:
1770*4882a593Smuzhiyun 					vc->vidstatus = le32_to_cpu(pdword[3]);
1771*4882a593Smuzhiyun 					vc->vidstatus_ready = 1;
1772*4882a593Smuzhiyun 					wake_up(&vc->wait_vidstatus);
1773*4882a593Smuzhiyun 					dprintk(dev, 5, "vstat %x chan %d\n",
1774*4882a593Smuzhiyun 						le32_to_cpu(pdword[3]), cc);
1775*4882a593Smuzhiyun 					break;
1776*4882a593Smuzhiyun 				default:
1777*4882a593Smuzhiyun 					pr_info("s2255 unknown resp\n");
1778*4882a593Smuzhiyun 				}
1779*4882a593Smuzhiyun 				pdata++;
1780*4882a593Smuzhiyun 				break;
1781*4882a593Smuzhiyun 			default:
1782*4882a593Smuzhiyun 				pdata++;
1783*4882a593Smuzhiyun 				break;
1784*4882a593Smuzhiyun 			}
1785*4882a593Smuzhiyun 			if (bframe)
1786*4882a593Smuzhiyun 				break;
1787*4882a593Smuzhiyun 		} /* for */
1788*4882a593Smuzhiyun 		if (!bframe)
1789*4882a593Smuzhiyun 			return -EINVAL;
1790*4882a593Smuzhiyun 	}
1791*4882a593Smuzhiyun 	vc = &dev->vc[dev->cc];
1792*4882a593Smuzhiyun 	idx = vc->cur_frame;
1793*4882a593Smuzhiyun 	frm = &vc->buffer.frame[idx];
1794*4882a593Smuzhiyun 	/* search done.  now find out if should be acquiring on this channel */
1795*4882a593Smuzhiyun 	if (!vb2_is_streaming(&vc->vb_vidq)) {
1796*4882a593Smuzhiyun 		/* we found a frame, but this channel is turned off */
1797*4882a593Smuzhiyun 		frm->ulState = S2255_READ_IDLE;
1798*4882a593Smuzhiyun 		return -EINVAL;
1799*4882a593Smuzhiyun 	}
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	if (frm->ulState == S2255_READ_IDLE) {
1802*4882a593Smuzhiyun 		frm->ulState = S2255_READ_FRAME;
1803*4882a593Smuzhiyun 		frm->cur_size = 0;
1804*4882a593Smuzhiyun 	}
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	/* skip the marker 512 bytes (and offset if out of sync) */
1807*4882a593Smuzhiyun 	psrc = (u8 *)pipe_info->transfer_buffer + offset;
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun 	if (frm->lpvbits == NULL) {
1811*4882a593Smuzhiyun 		dprintk(dev, 1, "s2255 frame buffer == NULL.%p %p %d %d",
1812*4882a593Smuzhiyun 			frm, dev, dev->cc, idx);
1813*4882a593Smuzhiyun 		return -ENOMEM;
1814*4882a593Smuzhiyun 	}
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun 	pdest = frm->lpvbits + frm->cur_size;
1817*4882a593Smuzhiyun 
1818*4882a593Smuzhiyun 	copy_size = (pipe_info->cur_transfer_size - offset);
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun 	size = vc->pkt_size - PREFIX_SIZE;
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 	/* sanity check on pdest */
1823*4882a593Smuzhiyun 	if ((copy_size + frm->cur_size) < vc->req_image_size)
1824*4882a593Smuzhiyun 		memcpy(pdest, psrc, copy_size);
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	frm->cur_size += copy_size;
1827*4882a593Smuzhiyun 	dprintk(dev, 4, "cur_size: %lu, size: %lu\n", frm->cur_size, size);
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 	if (frm->cur_size >= size) {
1830*4882a593Smuzhiyun 		dprintk(dev, 2, "******[%d]Buffer[%d]full*******\n",
1831*4882a593Smuzhiyun 			dev->cc, idx);
1832*4882a593Smuzhiyun 		vc->last_frame = vc->cur_frame;
1833*4882a593Smuzhiyun 		vc->cur_frame++;
1834*4882a593Smuzhiyun 		/* end of system frame ring buffer, start at zero */
1835*4882a593Smuzhiyun 		if ((vc->cur_frame == SYS_FRAMES) ||
1836*4882a593Smuzhiyun 		    (vc->cur_frame == vc->buffer.dwFrames))
1837*4882a593Smuzhiyun 			vc->cur_frame = 0;
1838*4882a593Smuzhiyun 		/* frame ready */
1839*4882a593Smuzhiyun 		if (vb2_is_streaming(&vc->vb_vidq))
1840*4882a593Smuzhiyun 			s2255_got_frame(vc, vc->jpg_size);
1841*4882a593Smuzhiyun 		vc->frame_count++;
1842*4882a593Smuzhiyun 		frm->ulState = S2255_READ_IDLE;
1843*4882a593Smuzhiyun 		frm->cur_size = 0;
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 	}
1846*4882a593Smuzhiyun 	/* done successfully */
1847*4882a593Smuzhiyun 	return 0;
1848*4882a593Smuzhiyun }
1849*4882a593Smuzhiyun 
s2255_read_video_callback(struct s2255_dev * dev,struct s2255_pipeinfo * pipe_info)1850*4882a593Smuzhiyun static void s2255_read_video_callback(struct s2255_dev *dev,
1851*4882a593Smuzhiyun 				      struct s2255_pipeinfo *pipe_info)
1852*4882a593Smuzhiyun {
1853*4882a593Smuzhiyun 	int res;
1854*4882a593Smuzhiyun 	dprintk(dev, 50, "callback read video\n");
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 	if (dev->cc >= MAX_CHANNELS) {
1857*4882a593Smuzhiyun 		dev->cc = 0;
1858*4882a593Smuzhiyun 		dev_err(&dev->udev->dev, "invalid channel\n");
1859*4882a593Smuzhiyun 		return;
1860*4882a593Smuzhiyun 	}
1861*4882a593Smuzhiyun 	/* otherwise copy to the system buffers */
1862*4882a593Smuzhiyun 	res = save_frame(dev, pipe_info);
1863*4882a593Smuzhiyun 	if (res != 0)
1864*4882a593Smuzhiyun 		dprintk(dev, 4, "s2255: read callback failed\n");
1865*4882a593Smuzhiyun 
1866*4882a593Smuzhiyun 	dprintk(dev, 50, "callback read video done\n");
1867*4882a593Smuzhiyun 	return;
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun 
s2255_vendor_req(struct s2255_dev * dev,unsigned char Request,u16 Index,u16 Value,void * TransferBuffer,s32 TransferBufferLength,int bOut)1870*4882a593Smuzhiyun static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request,
1871*4882a593Smuzhiyun 			     u16 Index, u16 Value, void *TransferBuffer,
1872*4882a593Smuzhiyun 			     s32 TransferBufferLength, int bOut)
1873*4882a593Smuzhiyun {
1874*4882a593Smuzhiyun 	int r;
1875*4882a593Smuzhiyun 	unsigned char *buf;
1876*4882a593Smuzhiyun 
1877*4882a593Smuzhiyun 	buf = kmalloc(TransferBufferLength, GFP_KERNEL);
1878*4882a593Smuzhiyun 	if (!buf)
1879*4882a593Smuzhiyun 		return -ENOMEM;
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun 	if (!bOut) {
1882*4882a593Smuzhiyun 		r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1883*4882a593Smuzhiyun 				    Request,
1884*4882a593Smuzhiyun 				    USB_TYPE_VENDOR | USB_RECIP_DEVICE |
1885*4882a593Smuzhiyun 				    USB_DIR_IN,
1886*4882a593Smuzhiyun 				    Value, Index, buf,
1887*4882a593Smuzhiyun 				    TransferBufferLength, USB_CTRL_SET_TIMEOUT);
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun 		if (r >= 0)
1890*4882a593Smuzhiyun 			memcpy(TransferBuffer, buf, TransferBufferLength);
1891*4882a593Smuzhiyun 	} else {
1892*4882a593Smuzhiyun 		memcpy(buf, TransferBuffer, TransferBufferLength);
1893*4882a593Smuzhiyun 		r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1894*4882a593Smuzhiyun 				    Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1895*4882a593Smuzhiyun 				    Value, Index, buf,
1896*4882a593Smuzhiyun 				    TransferBufferLength, USB_CTRL_SET_TIMEOUT);
1897*4882a593Smuzhiyun 	}
1898*4882a593Smuzhiyun 	kfree(buf);
1899*4882a593Smuzhiyun 	return r;
1900*4882a593Smuzhiyun }
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun /*
1903*4882a593Smuzhiyun  * retrieve FX2 firmware version. future use.
1904*4882a593Smuzhiyun  * @param dev pointer to device extension
1905*4882a593Smuzhiyun  * @return -1 for fail, else returns firmware version as an int(16 bits)
1906*4882a593Smuzhiyun  */
s2255_get_fx2fw(struct s2255_dev * dev)1907*4882a593Smuzhiyun static int s2255_get_fx2fw(struct s2255_dev *dev)
1908*4882a593Smuzhiyun {
1909*4882a593Smuzhiyun 	int fw;
1910*4882a593Smuzhiyun 	int ret;
1911*4882a593Smuzhiyun 	unsigned char transBuffer[64];
1912*4882a593Smuzhiyun 	ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2,
1913*4882a593Smuzhiyun 			       S2255_VR_IN);
1914*4882a593Smuzhiyun 	if (ret < 0)
1915*4882a593Smuzhiyun 		dprintk(dev, 2, "get fw error: %x\n", ret);
1916*4882a593Smuzhiyun 	fw = transBuffer[0] + (transBuffer[1] << 8);
1917*4882a593Smuzhiyun 	dprintk(dev, 2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]);
1918*4882a593Smuzhiyun 	return fw;
1919*4882a593Smuzhiyun }
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun /*
1922*4882a593Smuzhiyun  * Create the system ring buffer to copy frames into from the
1923*4882a593Smuzhiyun  * usb read pipe.
1924*4882a593Smuzhiyun  */
s2255_create_sys_buffers(struct s2255_vc * vc)1925*4882a593Smuzhiyun static int s2255_create_sys_buffers(struct s2255_vc *vc)
1926*4882a593Smuzhiyun {
1927*4882a593Smuzhiyun 	unsigned long i;
1928*4882a593Smuzhiyun 	unsigned long reqsize;
1929*4882a593Smuzhiyun 	vc->buffer.dwFrames = SYS_FRAMES;
1930*4882a593Smuzhiyun 	/* always allocate maximum size(PAL) for system buffers */
1931*4882a593Smuzhiyun 	reqsize = SYS_FRAMES_MAXSIZE;
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	if (reqsize > SYS_FRAMES_MAXSIZE)
1934*4882a593Smuzhiyun 		reqsize = SYS_FRAMES_MAXSIZE;
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	for (i = 0; i < SYS_FRAMES; i++) {
1937*4882a593Smuzhiyun 		/* allocate the frames */
1938*4882a593Smuzhiyun 		vc->buffer.frame[i].lpvbits = vmalloc(reqsize);
1939*4882a593Smuzhiyun 		vc->buffer.frame[i].size = reqsize;
1940*4882a593Smuzhiyun 		if (vc->buffer.frame[i].lpvbits == NULL) {
1941*4882a593Smuzhiyun 			pr_info("out of memory.  using less frames\n");
1942*4882a593Smuzhiyun 			vc->buffer.dwFrames = i;
1943*4882a593Smuzhiyun 			break;
1944*4882a593Smuzhiyun 		}
1945*4882a593Smuzhiyun 	}
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun 	/* make sure internal states are set */
1948*4882a593Smuzhiyun 	for (i = 0; i < SYS_FRAMES; i++) {
1949*4882a593Smuzhiyun 		vc->buffer.frame[i].ulState = 0;
1950*4882a593Smuzhiyun 		vc->buffer.frame[i].cur_size = 0;
1951*4882a593Smuzhiyun 	}
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	vc->cur_frame = 0;
1954*4882a593Smuzhiyun 	vc->last_frame = -1;
1955*4882a593Smuzhiyun 	return 0;
1956*4882a593Smuzhiyun }
1957*4882a593Smuzhiyun 
s2255_release_sys_buffers(struct s2255_vc * vc)1958*4882a593Smuzhiyun static int s2255_release_sys_buffers(struct s2255_vc *vc)
1959*4882a593Smuzhiyun {
1960*4882a593Smuzhiyun 	unsigned long i;
1961*4882a593Smuzhiyun 	for (i = 0; i < SYS_FRAMES; i++) {
1962*4882a593Smuzhiyun 		vfree(vc->buffer.frame[i].lpvbits);
1963*4882a593Smuzhiyun 		vc->buffer.frame[i].lpvbits = NULL;
1964*4882a593Smuzhiyun 	}
1965*4882a593Smuzhiyun 	return 0;
1966*4882a593Smuzhiyun }
1967*4882a593Smuzhiyun 
s2255_board_init(struct s2255_dev * dev)1968*4882a593Smuzhiyun static int s2255_board_init(struct s2255_dev *dev)
1969*4882a593Smuzhiyun {
1970*4882a593Smuzhiyun 	struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT;
1971*4882a593Smuzhiyun 	int fw_ver;
1972*4882a593Smuzhiyun 	int j;
1973*4882a593Smuzhiyun 	struct s2255_pipeinfo *pipe = &dev->pipe;
1974*4882a593Smuzhiyun 	dprintk(dev, 4, "board init: %p", dev);
1975*4882a593Smuzhiyun 	memset(pipe, 0, sizeof(*pipe));
1976*4882a593Smuzhiyun 	pipe->dev = dev;
1977*4882a593Smuzhiyun 	pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
1978*4882a593Smuzhiyun 	pipe->max_transfer_size = S2255_USB_XFER_SIZE;
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
1981*4882a593Smuzhiyun 					GFP_KERNEL);
1982*4882a593Smuzhiyun 	if (pipe->transfer_buffer == NULL) {
1983*4882a593Smuzhiyun 		dprintk(dev, 1, "out of memory!\n");
1984*4882a593Smuzhiyun 		return -ENOMEM;
1985*4882a593Smuzhiyun 	}
1986*4882a593Smuzhiyun 	/* query the firmware */
1987*4882a593Smuzhiyun 	fw_ver = s2255_get_fx2fw(dev);
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	pr_info("s2255: usb firmware version %d.%d\n",
1990*4882a593Smuzhiyun 		(fw_ver >> 8) & 0xff,
1991*4882a593Smuzhiyun 		fw_ver & 0xff);
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 	if (fw_ver < S2255_CUR_USB_FWVER)
1994*4882a593Smuzhiyun 		pr_info("s2255: newer USB firmware available\n");
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 	for (j = 0; j < MAX_CHANNELS; j++) {
1997*4882a593Smuzhiyun 		struct s2255_vc *vc = &dev->vc[j];
1998*4882a593Smuzhiyun 		vc->mode = mode_def;
1999*4882a593Smuzhiyun 		if (dev->pid == 0x2257 && j > 1)
2000*4882a593Smuzhiyun 			vc->mode.color |= (1 << 16);
2001*4882a593Smuzhiyun 		vc->jpegqual = S2255_DEF_JPEG_QUAL;
2002*4882a593Smuzhiyun 		vc->width = LINE_SZ_4CIFS_NTSC;
2003*4882a593Smuzhiyun 		vc->height = NUM_LINES_4CIFS_NTSC * 2;
2004*4882a593Smuzhiyun 		vc->std = V4L2_STD_NTSC_M;
2005*4882a593Smuzhiyun 		vc->fmt = &formats[0];
2006*4882a593Smuzhiyun 		vc->mode.restart = 1;
2007*4882a593Smuzhiyun 		vc->req_image_size = get_transfer_size(&mode_def);
2008*4882a593Smuzhiyun 		vc->frame_count = 0;
2009*4882a593Smuzhiyun 		/* create the system buffers */
2010*4882a593Smuzhiyun 		s2255_create_sys_buffers(vc);
2011*4882a593Smuzhiyun 	}
2012*4882a593Smuzhiyun 	/* start read pipe */
2013*4882a593Smuzhiyun 	s2255_start_readpipe(dev);
2014*4882a593Smuzhiyun 	dprintk(dev, 1, "%s: success\n", __func__);
2015*4882a593Smuzhiyun 	return 0;
2016*4882a593Smuzhiyun }
2017*4882a593Smuzhiyun 
s2255_board_shutdown(struct s2255_dev * dev)2018*4882a593Smuzhiyun static int s2255_board_shutdown(struct s2255_dev *dev)
2019*4882a593Smuzhiyun {
2020*4882a593Smuzhiyun 	u32 i;
2021*4882a593Smuzhiyun 	dprintk(dev, 1, "%s: dev: %p", __func__,  dev);
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 	for (i = 0; i < MAX_CHANNELS; i++) {
2024*4882a593Smuzhiyun 		if (vb2_is_streaming(&dev->vc[i].vb_vidq))
2025*4882a593Smuzhiyun 			s2255_stop_acquire(&dev->vc[i]);
2026*4882a593Smuzhiyun 	}
2027*4882a593Smuzhiyun 	s2255_stop_readpipe(dev);
2028*4882a593Smuzhiyun 	for (i = 0; i < MAX_CHANNELS; i++)
2029*4882a593Smuzhiyun 		s2255_release_sys_buffers(&dev->vc[i]);
2030*4882a593Smuzhiyun 	/* release transfer buffer */
2031*4882a593Smuzhiyun 	kfree(dev->pipe.transfer_buffer);
2032*4882a593Smuzhiyun 	return 0;
2033*4882a593Smuzhiyun }
2034*4882a593Smuzhiyun 
read_pipe_completion(struct urb * purb)2035*4882a593Smuzhiyun static void read_pipe_completion(struct urb *purb)
2036*4882a593Smuzhiyun {
2037*4882a593Smuzhiyun 	struct s2255_pipeinfo *pipe_info;
2038*4882a593Smuzhiyun 	struct s2255_dev *dev;
2039*4882a593Smuzhiyun 	int status;
2040*4882a593Smuzhiyun 	int pipe;
2041*4882a593Smuzhiyun 	pipe_info = purb->context;
2042*4882a593Smuzhiyun 	if (pipe_info == NULL) {
2043*4882a593Smuzhiyun 		dev_err(&purb->dev->dev, "no context!\n");
2044*4882a593Smuzhiyun 		return;
2045*4882a593Smuzhiyun 	}
2046*4882a593Smuzhiyun 	dev = pipe_info->dev;
2047*4882a593Smuzhiyun 	if (dev == NULL) {
2048*4882a593Smuzhiyun 		dev_err(&purb->dev->dev, "no context!\n");
2049*4882a593Smuzhiyun 		return;
2050*4882a593Smuzhiyun 	}
2051*4882a593Smuzhiyun 	status = purb->status;
2052*4882a593Smuzhiyun 	/* if shutting down, do not resubmit, exit immediately */
2053*4882a593Smuzhiyun 	if (status == -ESHUTDOWN) {
2054*4882a593Smuzhiyun 		dprintk(dev, 2, "%s: err shutdown\n", __func__);
2055*4882a593Smuzhiyun 		pipe_info->err_count++;
2056*4882a593Smuzhiyun 		return;
2057*4882a593Smuzhiyun 	}
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 	if (pipe_info->state == 0) {
2060*4882a593Smuzhiyun 		dprintk(dev, 2, "%s: exiting USB pipe", __func__);
2061*4882a593Smuzhiyun 		return;
2062*4882a593Smuzhiyun 	}
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun 	if (status == 0)
2065*4882a593Smuzhiyun 		s2255_read_video_callback(dev, pipe_info);
2066*4882a593Smuzhiyun 	else {
2067*4882a593Smuzhiyun 		pipe_info->err_count++;
2068*4882a593Smuzhiyun 		dprintk(dev, 1, "%s: failed URB %d\n", __func__, status);
2069*4882a593Smuzhiyun 	}
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun 	pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2072*4882a593Smuzhiyun 	/* reuse urb */
2073*4882a593Smuzhiyun 	usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2074*4882a593Smuzhiyun 			  pipe,
2075*4882a593Smuzhiyun 			  pipe_info->transfer_buffer,
2076*4882a593Smuzhiyun 			  pipe_info->cur_transfer_size,
2077*4882a593Smuzhiyun 			  read_pipe_completion, pipe_info);
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 	if (pipe_info->state != 0) {
2080*4882a593Smuzhiyun 		if (usb_submit_urb(pipe_info->stream_urb, GFP_ATOMIC))
2081*4882a593Smuzhiyun 			dev_err(&dev->udev->dev, "error submitting urb\n");
2082*4882a593Smuzhiyun 	} else {
2083*4882a593Smuzhiyun 		dprintk(dev, 2, "%s :complete state 0\n", __func__);
2084*4882a593Smuzhiyun 	}
2085*4882a593Smuzhiyun 	return;
2086*4882a593Smuzhiyun }
2087*4882a593Smuzhiyun 
s2255_start_readpipe(struct s2255_dev * dev)2088*4882a593Smuzhiyun static int s2255_start_readpipe(struct s2255_dev *dev)
2089*4882a593Smuzhiyun {
2090*4882a593Smuzhiyun 	int pipe;
2091*4882a593Smuzhiyun 	int retval;
2092*4882a593Smuzhiyun 	struct s2255_pipeinfo *pipe_info = &dev->pipe;
2093*4882a593Smuzhiyun 	pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint);
2094*4882a593Smuzhiyun 	dprintk(dev, 2, "%s: IN %d\n", __func__, dev->read_endpoint);
2095*4882a593Smuzhiyun 	pipe_info->state = 1;
2096*4882a593Smuzhiyun 	pipe_info->err_count = 0;
2097*4882a593Smuzhiyun 	pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
2098*4882a593Smuzhiyun 	if (!pipe_info->stream_urb)
2099*4882a593Smuzhiyun 		return -ENOMEM;
2100*4882a593Smuzhiyun 	/* transfer buffer allocated in board_init */
2101*4882a593Smuzhiyun 	usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev,
2102*4882a593Smuzhiyun 			  pipe,
2103*4882a593Smuzhiyun 			  pipe_info->transfer_buffer,
2104*4882a593Smuzhiyun 			  pipe_info->cur_transfer_size,
2105*4882a593Smuzhiyun 			  read_pipe_completion, pipe_info);
2106*4882a593Smuzhiyun 	retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
2107*4882a593Smuzhiyun 	if (retval) {
2108*4882a593Smuzhiyun 		pr_err("s2255: start read pipe failed\n");
2109*4882a593Smuzhiyun 		return retval;
2110*4882a593Smuzhiyun 	}
2111*4882a593Smuzhiyun 	return 0;
2112*4882a593Smuzhiyun }
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun /* starts acquisition process */
s2255_start_acquire(struct s2255_vc * vc)2115*4882a593Smuzhiyun static int s2255_start_acquire(struct s2255_vc *vc)
2116*4882a593Smuzhiyun {
2117*4882a593Smuzhiyun 	int res;
2118*4882a593Smuzhiyun 	unsigned long chn_rev;
2119*4882a593Smuzhiyun 	int j;
2120*4882a593Smuzhiyun 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
2121*4882a593Smuzhiyun 	__le32 *buffer = dev->cmdbuf;
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 	mutex_lock(&dev->cmdlock);
2124*4882a593Smuzhiyun 	chn_rev = G_chnmap[vc->idx];
2125*4882a593Smuzhiyun 	vc->last_frame = -1;
2126*4882a593Smuzhiyun 	vc->bad_payload = 0;
2127*4882a593Smuzhiyun 	vc->cur_frame = 0;
2128*4882a593Smuzhiyun 	for (j = 0; j < SYS_FRAMES; j++) {
2129*4882a593Smuzhiyun 		vc->buffer.frame[j].ulState = 0;
2130*4882a593Smuzhiyun 		vc->buffer.frame[j].cur_size = 0;
2131*4882a593Smuzhiyun 	}
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun 	/* send the start command */
2134*4882a593Smuzhiyun 	buffer[0] = IN_DATA_TOKEN;
2135*4882a593Smuzhiyun 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
2136*4882a593Smuzhiyun 	buffer[2] = CMD_START;
2137*4882a593Smuzhiyun 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2138*4882a593Smuzhiyun 	if (res != 0)
2139*4882a593Smuzhiyun 		dev_err(&dev->udev->dev, "CMD_START error\n");
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun 	dprintk(dev, 2, "start acquire exit[%d] %d\n", vc->idx, res);
2142*4882a593Smuzhiyun 	mutex_unlock(&dev->cmdlock);
2143*4882a593Smuzhiyun 	return res;
2144*4882a593Smuzhiyun }
2145*4882a593Smuzhiyun 
s2255_stop_acquire(struct s2255_vc * vc)2146*4882a593Smuzhiyun static int s2255_stop_acquire(struct s2255_vc *vc)
2147*4882a593Smuzhiyun {
2148*4882a593Smuzhiyun 	int res;
2149*4882a593Smuzhiyun 	unsigned long chn_rev;
2150*4882a593Smuzhiyun 	struct s2255_dev *dev = to_s2255_dev(vc->vdev.v4l2_dev);
2151*4882a593Smuzhiyun 	__le32 *buffer = dev->cmdbuf;
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun 	mutex_lock(&dev->cmdlock);
2154*4882a593Smuzhiyun 	chn_rev = G_chnmap[vc->idx];
2155*4882a593Smuzhiyun 	/* send the stop command */
2156*4882a593Smuzhiyun 	buffer[0] = IN_DATA_TOKEN;
2157*4882a593Smuzhiyun 	buffer[1] = (__le32) cpu_to_le32(chn_rev);
2158*4882a593Smuzhiyun 	buffer[2] = CMD_STOP;
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun 	res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512);
2161*4882a593Smuzhiyun 	if (res != 0)
2162*4882a593Smuzhiyun 		dev_err(&dev->udev->dev, "CMD_STOP error\n");
2163*4882a593Smuzhiyun 
2164*4882a593Smuzhiyun 	dprintk(dev, 4, "%s: chn %d, res %d\n", __func__, vc->idx, res);
2165*4882a593Smuzhiyun 	mutex_unlock(&dev->cmdlock);
2166*4882a593Smuzhiyun 	return res;
2167*4882a593Smuzhiyun }
2168*4882a593Smuzhiyun 
s2255_stop_readpipe(struct s2255_dev * dev)2169*4882a593Smuzhiyun static void s2255_stop_readpipe(struct s2255_dev *dev)
2170*4882a593Smuzhiyun {
2171*4882a593Smuzhiyun 	struct s2255_pipeinfo *pipe = &dev->pipe;
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 	pipe->state = 0;
2174*4882a593Smuzhiyun 	if (pipe->stream_urb) {
2175*4882a593Smuzhiyun 		/* cancel urb */
2176*4882a593Smuzhiyun 		usb_kill_urb(pipe->stream_urb);
2177*4882a593Smuzhiyun 		usb_free_urb(pipe->stream_urb);
2178*4882a593Smuzhiyun 		pipe->stream_urb = NULL;
2179*4882a593Smuzhiyun 	}
2180*4882a593Smuzhiyun 	dprintk(dev, 4, "%s", __func__);
2181*4882a593Smuzhiyun 	return;
2182*4882a593Smuzhiyun }
2183*4882a593Smuzhiyun 
s2255_fwload_start(struct s2255_dev * dev)2184*4882a593Smuzhiyun static void s2255_fwload_start(struct s2255_dev *dev)
2185*4882a593Smuzhiyun {
2186*4882a593Smuzhiyun 	s2255_reset_dsppower(dev);
2187*4882a593Smuzhiyun 	dev->fw_data->fw_size = dev->fw_data->fw->size;
2188*4882a593Smuzhiyun 	atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2189*4882a593Smuzhiyun 	memcpy(dev->fw_data->pfw_data,
2190*4882a593Smuzhiyun 	       dev->fw_data->fw->data, CHUNK_SIZE);
2191*4882a593Smuzhiyun 	dev->fw_data->fw_loaded = CHUNK_SIZE;
2192*4882a593Smuzhiyun 	usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev,
2193*4882a593Smuzhiyun 			  usb_sndbulkpipe(dev->udev, 2),
2194*4882a593Smuzhiyun 			  dev->fw_data->pfw_data,
2195*4882a593Smuzhiyun 			  CHUNK_SIZE, s2255_fwchunk_complete,
2196*4882a593Smuzhiyun 			  dev->fw_data);
2197*4882a593Smuzhiyun 	mod_timer(&dev->timer, jiffies + HZ);
2198*4882a593Smuzhiyun }
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun /* standard usb probe function */
s2255_probe(struct usb_interface * interface,const struct usb_device_id * id)2201*4882a593Smuzhiyun static int s2255_probe(struct usb_interface *interface,
2202*4882a593Smuzhiyun 		       const struct usb_device_id *id)
2203*4882a593Smuzhiyun {
2204*4882a593Smuzhiyun 	struct s2255_dev *dev = NULL;
2205*4882a593Smuzhiyun 	struct usb_host_interface *iface_desc;
2206*4882a593Smuzhiyun 	struct usb_endpoint_descriptor *endpoint;
2207*4882a593Smuzhiyun 	int i;
2208*4882a593Smuzhiyun 	int retval = -ENOMEM;
2209*4882a593Smuzhiyun 	__le32 *pdata;
2210*4882a593Smuzhiyun 	int fw_size;
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	/* allocate memory for our device state and initialize it to zero */
2213*4882a593Smuzhiyun 	dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL);
2214*4882a593Smuzhiyun 	if (dev == NULL) {
2215*4882a593Smuzhiyun 		s2255_dev_err(&interface->dev, "out of memory\n");
2216*4882a593Smuzhiyun 		return -ENOMEM;
2217*4882a593Smuzhiyun 	}
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	dev->cmdbuf = kzalloc(S2255_CMDBUF_SIZE, GFP_KERNEL);
2220*4882a593Smuzhiyun 	if (dev->cmdbuf == NULL) {
2221*4882a593Smuzhiyun 		s2255_dev_err(&interface->dev, "out of memory\n");
2222*4882a593Smuzhiyun 		goto errorFWDATA1;
2223*4882a593Smuzhiyun 	}
2224*4882a593Smuzhiyun 
2225*4882a593Smuzhiyun 	atomic_set(&dev->num_channels, 0);
2226*4882a593Smuzhiyun 	dev->pid = id->idProduct;
2227*4882a593Smuzhiyun 	dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
2228*4882a593Smuzhiyun 	if (!dev->fw_data)
2229*4882a593Smuzhiyun 		goto errorFWDATA1;
2230*4882a593Smuzhiyun 	mutex_init(&dev->lock);
2231*4882a593Smuzhiyun 	mutex_init(&dev->cmdlock);
2232*4882a593Smuzhiyun 	/* grab usb_device and save it */
2233*4882a593Smuzhiyun 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
2234*4882a593Smuzhiyun 	if (dev->udev == NULL) {
2235*4882a593Smuzhiyun 		dev_err(&interface->dev, "null usb device\n");
2236*4882a593Smuzhiyun 		retval = -ENODEV;
2237*4882a593Smuzhiyun 		goto errorUDEV;
2238*4882a593Smuzhiyun 	}
2239*4882a593Smuzhiyun 	dev_dbg(&interface->dev, "dev: %p, udev %p interface %p\n",
2240*4882a593Smuzhiyun 		dev, dev->udev, interface);
2241*4882a593Smuzhiyun 	dev->interface = interface;
2242*4882a593Smuzhiyun 	/* set up the endpoint information  */
2243*4882a593Smuzhiyun 	iface_desc = interface->cur_altsetting;
2244*4882a593Smuzhiyun 	dev_dbg(&interface->dev, "num EP: %d\n",
2245*4882a593Smuzhiyun 		iface_desc->desc.bNumEndpoints);
2246*4882a593Smuzhiyun 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2247*4882a593Smuzhiyun 		endpoint = &iface_desc->endpoint[i].desc;
2248*4882a593Smuzhiyun 		if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
2249*4882a593Smuzhiyun 			/* we found the bulk in endpoint */
2250*4882a593Smuzhiyun 			dev->read_endpoint = endpoint->bEndpointAddress;
2251*4882a593Smuzhiyun 		}
2252*4882a593Smuzhiyun 	}
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun 	if (!dev->read_endpoint) {
2255*4882a593Smuzhiyun 		dev_err(&interface->dev, "Could not find bulk-in endpoint\n");
2256*4882a593Smuzhiyun 		goto errorEP;
2257*4882a593Smuzhiyun 	}
2258*4882a593Smuzhiyun 	timer_setup(&dev->timer, s2255_timer, 0);
2259*4882a593Smuzhiyun 	init_waitqueue_head(&dev->fw_data->wait_fw);
2260*4882a593Smuzhiyun 	for (i = 0; i < MAX_CHANNELS; i++) {
2261*4882a593Smuzhiyun 		struct s2255_vc *vc = &dev->vc[i];
2262*4882a593Smuzhiyun 		vc->idx = i;
2263*4882a593Smuzhiyun 		vc->dev = dev;
2264*4882a593Smuzhiyun 		init_waitqueue_head(&vc->wait_setmode);
2265*4882a593Smuzhiyun 		init_waitqueue_head(&vc->wait_vidstatus);
2266*4882a593Smuzhiyun 		spin_lock_init(&vc->qlock);
2267*4882a593Smuzhiyun 		mutex_init(&vc->vb_lock);
2268*4882a593Smuzhiyun 	}
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun 	dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
2271*4882a593Smuzhiyun 	if (!dev->fw_data->fw_urb)
2272*4882a593Smuzhiyun 		goto errorFWURB;
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun 	dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL);
2275*4882a593Smuzhiyun 	if (!dev->fw_data->pfw_data) {
2276*4882a593Smuzhiyun 		dev_err(&interface->dev, "out of memory!\n");
2277*4882a593Smuzhiyun 		goto errorFWDATA2;
2278*4882a593Smuzhiyun 	}
2279*4882a593Smuzhiyun 	/* load the first chunk */
2280*4882a593Smuzhiyun 	if (request_firmware(&dev->fw_data->fw,
2281*4882a593Smuzhiyun 			     FIRMWARE_FILE_NAME, &dev->udev->dev)) {
2282*4882a593Smuzhiyun 		dev_err(&interface->dev, "sensoray 2255 failed to get firmware\n");
2283*4882a593Smuzhiyun 		goto errorREQFW;
2284*4882a593Smuzhiyun 	}
2285*4882a593Smuzhiyun 	/* check the firmware is valid */
2286*4882a593Smuzhiyun 	fw_size = dev->fw_data->fw->size;
2287*4882a593Smuzhiyun 	pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 	if (*pdata != S2255_FW_MARKER) {
2290*4882a593Smuzhiyun 		dev_err(&interface->dev, "Firmware invalid.\n");
2291*4882a593Smuzhiyun 		retval = -ENODEV;
2292*4882a593Smuzhiyun 		goto errorFWMARKER;
2293*4882a593Smuzhiyun 	} else {
2294*4882a593Smuzhiyun 		/* make sure firmware is the latest */
2295*4882a593Smuzhiyun 		__le32 *pRel;
2296*4882a593Smuzhiyun 		pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2297*4882a593Smuzhiyun 		pr_info("s2255 dsp fw version %x\n", le32_to_cpu(*pRel));
2298*4882a593Smuzhiyun 		dev->dsp_fw_ver = le32_to_cpu(*pRel);
2299*4882a593Smuzhiyun 		if (dev->dsp_fw_ver < S2255_CUR_DSP_FWVER)
2300*4882a593Smuzhiyun 			pr_info("s2255: f2255usb.bin out of date.\n");
2301*4882a593Smuzhiyun 		if (dev->pid == 0x2257 &&
2302*4882a593Smuzhiyun 				dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER)
2303*4882a593Smuzhiyun 			pr_warn("2257 needs firmware %d or above.\n",
2304*4882a593Smuzhiyun 				S2255_MIN_DSP_COLORFILTER);
2305*4882a593Smuzhiyun 	}
2306*4882a593Smuzhiyun 	usb_reset_device(dev->udev);
2307*4882a593Smuzhiyun 	/* load 2255 board specific */
2308*4882a593Smuzhiyun 	retval = s2255_board_init(dev);
2309*4882a593Smuzhiyun 	if (retval)
2310*4882a593Smuzhiyun 		goto errorBOARDINIT;
2311*4882a593Smuzhiyun 	s2255_fwload_start(dev);
2312*4882a593Smuzhiyun 	/* loads v4l specific */
2313*4882a593Smuzhiyun 	retval = s2255_probe_v4l(dev);
2314*4882a593Smuzhiyun 	if (retval)
2315*4882a593Smuzhiyun 		goto errorBOARDINIT;
2316*4882a593Smuzhiyun 	dev_info(&interface->dev, "Sensoray 2255 detected\n");
2317*4882a593Smuzhiyun 	return 0;
2318*4882a593Smuzhiyun errorBOARDINIT:
2319*4882a593Smuzhiyun 	s2255_board_shutdown(dev);
2320*4882a593Smuzhiyun errorFWMARKER:
2321*4882a593Smuzhiyun 	release_firmware(dev->fw_data->fw);
2322*4882a593Smuzhiyun errorREQFW:
2323*4882a593Smuzhiyun 	kfree(dev->fw_data->pfw_data);
2324*4882a593Smuzhiyun errorFWDATA2:
2325*4882a593Smuzhiyun 	usb_free_urb(dev->fw_data->fw_urb);
2326*4882a593Smuzhiyun errorFWURB:
2327*4882a593Smuzhiyun 	del_timer_sync(&dev->timer);
2328*4882a593Smuzhiyun errorEP:
2329*4882a593Smuzhiyun 	usb_put_dev(dev->udev);
2330*4882a593Smuzhiyun errorUDEV:
2331*4882a593Smuzhiyun 	kfree(dev->fw_data);
2332*4882a593Smuzhiyun 	mutex_destroy(&dev->lock);
2333*4882a593Smuzhiyun errorFWDATA1:
2334*4882a593Smuzhiyun 	kfree(dev->cmdbuf);
2335*4882a593Smuzhiyun 	kfree(dev);
2336*4882a593Smuzhiyun 	pr_warn("Sensoray 2255 driver load failed: 0x%x\n", retval);
2337*4882a593Smuzhiyun 	return retval;
2338*4882a593Smuzhiyun }
2339*4882a593Smuzhiyun 
2340*4882a593Smuzhiyun /* disconnect routine. when board is removed physically or with rmmod */
s2255_disconnect(struct usb_interface * interface)2341*4882a593Smuzhiyun static void s2255_disconnect(struct usb_interface *interface)
2342*4882a593Smuzhiyun {
2343*4882a593Smuzhiyun 	struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
2344*4882a593Smuzhiyun 	int i;
2345*4882a593Smuzhiyun 	int channels = atomic_read(&dev->num_channels);
2346*4882a593Smuzhiyun 	mutex_lock(&dev->lock);
2347*4882a593Smuzhiyun 	v4l2_device_disconnect(&dev->v4l2_dev);
2348*4882a593Smuzhiyun 	mutex_unlock(&dev->lock);
2349*4882a593Smuzhiyun 	/*see comments in the uvc_driver.c usb disconnect function */
2350*4882a593Smuzhiyun 	atomic_inc(&dev->num_channels);
2351*4882a593Smuzhiyun 	/* unregister each video device. */
2352*4882a593Smuzhiyun 	for (i = 0; i < channels; i++)
2353*4882a593Smuzhiyun 		video_unregister_device(&dev->vc[i].vdev);
2354*4882a593Smuzhiyun 	/* wake up any of our timers */
2355*4882a593Smuzhiyun 	atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2356*4882a593Smuzhiyun 	wake_up(&dev->fw_data->wait_fw);
2357*4882a593Smuzhiyun 	for (i = 0; i < MAX_CHANNELS; i++) {
2358*4882a593Smuzhiyun 		dev->vc[i].setmode_ready = 1;
2359*4882a593Smuzhiyun 		wake_up(&dev->vc[i].wait_setmode);
2360*4882a593Smuzhiyun 		dev->vc[i].vidstatus_ready = 1;
2361*4882a593Smuzhiyun 		wake_up(&dev->vc[i].wait_vidstatus);
2362*4882a593Smuzhiyun 	}
2363*4882a593Smuzhiyun 	if (atomic_dec_and_test(&dev->num_channels))
2364*4882a593Smuzhiyun 		s2255_destroy(dev);
2365*4882a593Smuzhiyun 	dev_info(&interface->dev, "%s\n", __func__);
2366*4882a593Smuzhiyun }
2367*4882a593Smuzhiyun 
2368*4882a593Smuzhiyun static struct usb_driver s2255_driver = {
2369*4882a593Smuzhiyun 	.name = S2255_DRIVER_NAME,
2370*4882a593Smuzhiyun 	.probe = s2255_probe,
2371*4882a593Smuzhiyun 	.disconnect = s2255_disconnect,
2372*4882a593Smuzhiyun 	.id_table = s2255_table,
2373*4882a593Smuzhiyun };
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun module_usb_driver(s2255_driver);
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver");
2378*4882a593Smuzhiyun MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)");
2379*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2380*4882a593Smuzhiyun MODULE_VERSION(S2255_VERSION);
2381*4882a593Smuzhiyun MODULE_FIRMWARE(FIRMWARE_FILE_NAME);
2382