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