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