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