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