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