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