V4L/DVB (10647): vivi: add slider flag to controls.
[pandora-kernel.git] / drivers / media / video / vivi.c
1 /*
2  * Virtual Video driver - This code emulates a real video device with v4l2 api
3  *
4  * Copyright (c) 2006 by:
5  *      Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
6  *      Ted Walther <ted--a.t--enumera.com>
7  *      John Sokol <sokol--a.t--videotechnology.com>
8  *      http://v4l.videotechnology.com/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the BSD Licence, GNU General Public License
12  * as published by the Free Software Foundation; either version 2 of the
13  * License, or (at your option) any later version
14  */
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/mm.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/pci.h>
26 #include <linux/random.h>
27 #include <linux/version.h>
28 #include <linux/mutex.h>
29 #include <linux/videodev2.h>
30 #include <linux/dma-mapping.h>
31 #ifdef CONFIG_VIDEO_V4L1_COMPAT
32 /* Include V4L1 specific functions. Should be removed soon */
33 #include <linux/videodev.h>
34 #endif
35 #include <linux/interrupt.h>
36 #include <linux/kthread.h>
37 #include <linux/highmem.h>
38 #include <linux/freezer.h>
39 #include <media/videobuf-vmalloc.h>
40 #include <media/v4l2-device.h>
41 #include <media/v4l2-ioctl.h>
42 #include "font.h"
43
44 #define VIVI_MODULE_NAME "vivi"
45
46 /* Wake up at about 30 fps */
47 #define WAKE_NUMERATOR 30
48 #define WAKE_DENOMINATOR 1001
49 #define BUFFER_TIMEOUT     msecs_to_jiffies(500)  /* 0.5 seconds */
50
51 #define VIVI_MAJOR_VERSION 0
52 #define VIVI_MINOR_VERSION 6
53 #define VIVI_RELEASE 0
54 #define VIVI_VERSION \
55         KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
56
57 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
58 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
59 MODULE_LICENSE("Dual BSD/GPL");
60
61 static unsigned video_nr = -1;
62 module_param(video_nr, uint, 0644);
63 MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
64
65 static unsigned n_devs = 1;
66 module_param(n_devs, uint, 0644);
67 MODULE_PARM_DESC(n_devs, "number of video devices to create");
68
69 static unsigned debug;
70 module_param(debug, uint, 0644);
71 MODULE_PARM_DESC(debug, "activates debug info");
72
73 static unsigned int vid_limit = 16;
74 module_param(vid_limit, uint, 0644);
75 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
76
77
78 /* supported controls */
79 static struct v4l2_queryctrl vivi_qctrl[] = {
80         {
81                 .id            = V4L2_CID_AUDIO_VOLUME,
82                 .name          = "Volume",
83                 .minimum       = 0,
84                 .maximum       = 65535,
85                 .step          = 65535/100,
86                 .default_value = 65535,
87                 .flags         = V4L2_CTRL_FLAG_SLIDER,
88                 .type          = V4L2_CTRL_TYPE_INTEGER,
89         }, {
90                 .id            = V4L2_CID_BRIGHTNESS,
91                 .type          = V4L2_CTRL_TYPE_INTEGER,
92                 .name          = "Brightness",
93                 .minimum       = 0,
94                 .maximum       = 255,
95                 .step          = 1,
96                 .default_value = 127,
97                 .flags         = V4L2_CTRL_FLAG_SLIDER,
98         }, {
99                 .id            = V4L2_CID_CONTRAST,
100                 .type          = V4L2_CTRL_TYPE_INTEGER,
101                 .name          = "Contrast",
102                 .minimum       = 0,
103                 .maximum       = 255,
104                 .step          = 0x1,
105                 .default_value = 0x10,
106                 .flags         = V4L2_CTRL_FLAG_SLIDER,
107         }, {
108                 .id            = V4L2_CID_SATURATION,
109                 .type          = V4L2_CTRL_TYPE_INTEGER,
110                 .name          = "Saturation",
111                 .minimum       = 0,
112                 .maximum       = 255,
113                 .step          = 0x1,
114                 .default_value = 127,
115                 .flags         = V4L2_CTRL_FLAG_SLIDER,
116         }, {
117                 .id            = V4L2_CID_HUE,
118                 .type          = V4L2_CTRL_TYPE_INTEGER,
119                 .name          = "Hue",
120                 .minimum       = -128,
121                 .maximum       = 127,
122                 .step          = 0x1,
123                 .default_value = 0,
124                 .flags         = V4L2_CTRL_FLAG_SLIDER,
125         }
126 };
127
128 #define dprintk(dev, level, fmt, arg...) \
129         v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
130
131 /* ------------------------------------------------------------------
132         Basic structures
133    ------------------------------------------------------------------*/
134
135 struct vivi_fmt {
136         char  *name;
137         u32   fourcc;          /* v4l2 format id */
138         int   depth;
139 };
140
141 static struct vivi_fmt formats[] = {
142         {
143                 .name     = "4:2:2, packed, YUYV",
144                 .fourcc   = V4L2_PIX_FMT_YUYV,
145                 .depth    = 16,
146         },
147         {
148                 .name     = "4:2:2, packed, UYVY",
149                 .fourcc   = V4L2_PIX_FMT_UYVY,
150                 .depth    = 16,
151         },
152         {
153                 .name     = "RGB565 (LE)",
154                 .fourcc   = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
155                 .depth    = 16,
156         },
157         {
158                 .name     = "RGB565 (BE)",
159                 .fourcc   = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
160                 .depth    = 16,
161         },
162         {
163                 .name     = "RGB555 (LE)",
164                 .fourcc   = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
165                 .depth    = 16,
166         },
167         {
168                 .name     = "RGB555 (BE)",
169                 .fourcc   = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
170                 .depth    = 16,
171         },
172 };
173
174 static struct vivi_fmt *get_format(struct v4l2_format *f)
175 {
176         struct vivi_fmt *fmt;
177         unsigned int k;
178
179         for (k = 0; k < ARRAY_SIZE(formats); k++) {
180                 fmt = &formats[k];
181                 if (fmt->fourcc == f->fmt.pix.pixelformat)
182                         break;
183         }
184
185         if (k == ARRAY_SIZE(formats))
186                 return NULL;
187
188         return &formats[k];
189 }
190
191 struct sg_to_addr {
192         int pos;
193         struct scatterlist *sg;
194 };
195
196 /* buffer for one video frame */
197 struct vivi_buffer {
198         /* common v4l buffer stuff -- must be first */
199         struct videobuf_buffer vb;
200
201         struct vivi_fmt        *fmt;
202 };
203
204 struct vivi_dmaqueue {
205         struct list_head       active;
206
207         /* thread for generating video stream*/
208         struct task_struct         *kthread;
209         wait_queue_head_t          wq;
210         /* Counters to control fps rate */
211         int                        frame;
212         int                        ini_jiffies;
213 };
214
215 static LIST_HEAD(vivi_devlist);
216
217 struct vivi_dev {
218         struct list_head           vivi_devlist;
219         struct v4l2_device         v4l2_dev;
220
221         spinlock_t                 slock;
222         struct mutex               mutex;
223
224         int                        users;
225
226         /* various device info */
227         struct video_device        *vfd;
228
229         struct vivi_dmaqueue       vidq;
230
231         /* Several counters */
232         int                        h, m, s, ms;
233         unsigned long              jiffies;
234         char                       timestr[13];
235
236         int                        mv_count;    /* Controls bars movement */
237
238         /* Input Number */
239         int                        input;
240
241         /* Control 'registers' */
242         int                        qctl_regs[ARRAY_SIZE(vivi_qctrl)];
243 };
244
245 struct vivi_fh {
246         struct vivi_dev            *dev;
247
248         /* video capture */
249         struct vivi_fmt            *fmt;
250         unsigned int               width, height;
251         struct videobuf_queue      vb_vidq;
252
253         enum v4l2_buf_type         type;
254         unsigned char              bars[8][3];
255         int                        input;       /* Input Number on bars */
256 };
257
258 /* ------------------------------------------------------------------
259         DMA and thread functions
260    ------------------------------------------------------------------*/
261
262 /* Bars and Colors should match positions */
263
264 enum colors {
265         WHITE,
266         AMBAR,
267         CYAN,
268         GREEN,
269         MAGENTA,
270         RED,
271         BLUE,
272         BLACK,
273 };
274
275         /* R   G   B */
276 #define COLOR_WHITE     {204, 204, 204}
277 #define COLOR_AMBAR     {208, 208,   0}
278 #define COLOR_CIAN      {  0, 206, 206}
279 #define COLOR_GREEN     {  0, 239,   0}
280 #define COLOR_MAGENTA   {239,   0, 239}
281 #define COLOR_RED       {205,   0,   0}
282 #define COLOR_BLUE      {  0,   0, 255}
283 #define COLOR_BLACK     {  0,   0,   0}
284
285 struct bar_std {
286         u8 bar[8][3];
287 };
288
289 /* Maximum number of bars are 10 - otherwise, the input print code
290    should be modified */
291 static struct bar_std bars[] = {
292         {       /* Standard ITU-R color bar sequence */
293                 {
294                         COLOR_WHITE,
295                         COLOR_AMBAR,
296                         COLOR_CIAN,
297                         COLOR_GREEN,
298                         COLOR_MAGENTA,
299                         COLOR_RED,
300                         COLOR_BLUE,
301                         COLOR_BLACK,
302                 }
303         }, {
304                 {
305                         COLOR_WHITE,
306                         COLOR_AMBAR,
307                         COLOR_BLACK,
308                         COLOR_WHITE,
309                         COLOR_AMBAR,
310                         COLOR_BLACK,
311                         COLOR_WHITE,
312                         COLOR_AMBAR,
313                 }
314         }, {
315                 {
316                         COLOR_WHITE,
317                         COLOR_CIAN,
318                         COLOR_BLACK,
319                         COLOR_WHITE,
320                         COLOR_CIAN,
321                         COLOR_BLACK,
322                         COLOR_WHITE,
323                         COLOR_CIAN,
324                 }
325         }, {
326                 {
327                         COLOR_WHITE,
328                         COLOR_GREEN,
329                         COLOR_BLACK,
330                         COLOR_WHITE,
331                         COLOR_GREEN,
332                         COLOR_BLACK,
333                         COLOR_WHITE,
334                         COLOR_GREEN,
335                 }
336         },
337 };
338
339 #define NUM_INPUTS ARRAY_SIZE(bars)
340
341 #define TO_Y(r, g, b) \
342         (((16829 * r + 33039 * g + 6416 * b  + 32768) >> 16) + 16)
343 /* RGB to  V(Cr) Color transform */
344 #define TO_V(r, g, b) \
345         (((28784 * r - 24103 * g - 4681 * b  + 32768) >> 16) + 128)
346 /* RGB to  U(Cb) Color transform */
347 #define TO_U(r, g, b) \
348         (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
349
350 #define TSTAMP_MIN_Y    24
351 #define TSTAMP_MAX_Y    (TSTAMP_MIN_Y + 15)
352 #define TSTAMP_INPUT_X  10
353 #define TSTAMP_MIN_X    (54 + TSTAMP_INPUT_X)
354
355 static void gen_twopix(struct vivi_fh *fh, unsigned char *buf, int colorpos)
356 {
357         unsigned char r_y, g_u, b_v;
358         unsigned char *p;
359         int color;
360
361         r_y = fh->bars[colorpos][0]; /* R or precalculated Y */
362         g_u = fh->bars[colorpos][1]; /* G or precalculated U */
363         b_v = fh->bars[colorpos][2]; /* B or precalculated V */
364
365         for (color = 0; color < 4; color++) {
366                 p = buf + color;
367
368                 switch (fh->fmt->fourcc) {
369                 case V4L2_PIX_FMT_YUYV:
370                         switch (color) {
371                         case 0:
372                         case 2:
373                                 *p = r_y;
374                                 break;
375                         case 1:
376                                 *p = g_u;
377                                 break;
378                         case 3:
379                                 *p = b_v;
380                                 break;
381                         }
382                         break;
383                 case V4L2_PIX_FMT_UYVY:
384                         switch (color) {
385                         case 1:
386                         case 3:
387                                 *p = r_y;
388                                 break;
389                         case 0:
390                                 *p = g_u;
391                                 break;
392                         case 2:
393                                 *p = b_v;
394                                 break;
395                         }
396                         break;
397                 case V4L2_PIX_FMT_RGB565:
398                         switch (color) {
399                         case 0:
400                         case 2:
401                                 *p = (g_u << 5) | b_v;
402                                 break;
403                         case 1:
404                         case 3:
405                                 *p = (r_y << 3) | (g_u >> 3);
406                                 break;
407                         }
408                         break;
409                 case V4L2_PIX_FMT_RGB565X:
410                         switch (color) {
411                         case 0:
412                         case 2:
413                                 *p = (r_y << 3) | (g_u >> 3);
414                                 break;
415                         case 1:
416                         case 3:
417                                 *p = (g_u << 5) | b_v;
418                                 break;
419                         }
420                         break;
421                 case V4L2_PIX_FMT_RGB555:
422                         switch (color) {
423                         case 0:
424                         case 2:
425                                 *p = (g_u << 5) | b_v;
426                                 break;
427                         case 1:
428                         case 3:
429                                 *p = (r_y << 2) | (g_u >> 3);
430                                 break;
431                         }
432                         break;
433                 case V4L2_PIX_FMT_RGB555X:
434                         switch (color) {
435                         case 0:
436                         case 2:
437                                 *p = (r_y << 2) | (g_u >> 3);
438                                 break;
439                         case 1:
440                         case 3:
441                                 *p = (g_u << 5) | b_v;
442                                 break;
443                         }
444                         break;
445                 }
446         }
447 }
448
449 static void gen_line(struct vivi_fh *fh, char *basep, int inipos, int wmax,
450                 int hmax, int line, int count, char *timestr)
451 {
452         int  w, i, j;
453         int pos = inipos;
454         char *s;
455         u8 chr;
456
457         /* We will just duplicate the second pixel at the packet */
458         wmax /= 2;
459
460         /* Generate a standard color bar pattern */
461         for (w = 0; w < wmax; w++) {
462                 int colorpos = ((w + count) * 8/(wmax + 1)) % 8;
463
464                 gen_twopix(fh, basep + pos, colorpos);
465                 pos += 4; /* only 16 bpp supported for now */
466         }
467
468         /* Prints input entry number */
469
470         /* Checks if it is possible to input number */
471         if (TSTAMP_MAX_Y >= hmax)
472                 goto end;
473
474         if (TSTAMP_INPUT_X + strlen(timestr) >= wmax)
475                 goto end;
476
477         if (line >= TSTAMP_MIN_Y && line <= TSTAMP_MAX_Y) {
478                 chr = rom8x16_bits[fh->input * 16 + line - TSTAMP_MIN_Y];
479                 pos = TSTAMP_INPUT_X;
480                 for (i = 0; i < 7; i++) {
481                         /* Draw white font on black background */
482                         if (chr & 1 << (7 - i))
483                                 gen_twopix(fh, basep + pos, WHITE);
484                         else
485                                 gen_twopix(fh, basep + pos, BLACK);
486                         pos += 2;
487                 }
488         }
489
490         /* Checks if it is possible to show timestamp */
491         if (TSTAMP_MIN_X + strlen(timestr) >= wmax)
492                 goto end;
493
494         /* Print stream time */
495         if (line >= TSTAMP_MIN_Y && line <= TSTAMP_MAX_Y) {
496                 j = TSTAMP_MIN_X;
497                 for (s = timestr; *s; s++) {
498                         chr = rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
499                         for (i = 0; i < 7; i++) {
500                                 pos = inipos + j * 2;
501                                 /* Draw white font on black background */
502                                 if (chr & 1 << (7 - i))
503                                         gen_twopix(fh, basep + pos, WHITE);
504                                 else
505                                         gen_twopix(fh, basep + pos, BLACK);
506                                 j++;
507                         }
508                 }
509         }
510
511 end:
512         return;
513 }
514
515 static void vivi_fillbuff(struct vivi_fh *fh, struct vivi_buffer *buf)
516 {
517         struct vivi_dev *dev = fh->dev;
518         int h , pos = 0;
519         int hmax  = buf->vb.height;
520         int wmax  = buf->vb.width;
521         struct timeval ts;
522         char *tmpbuf;
523         void *vbuf = videobuf_to_vmalloc(&buf->vb);
524
525         if (!vbuf)
526                 return;
527
528         tmpbuf = kmalloc(wmax * 2, GFP_ATOMIC);
529         if (!tmpbuf)
530                 return;
531
532         for (h = 0; h < hmax; h++) {
533                 gen_line(fh, tmpbuf, 0, wmax, hmax, h, dev->mv_count,
534                          dev->timestr);
535                 memcpy(vbuf + pos, tmpbuf, wmax * 2);
536                 pos += wmax*2;
537         }
538
539         dev->mv_count++;
540
541         kfree(tmpbuf);
542
543         /* Updates stream time */
544
545         dev->ms += jiffies_to_msecs(jiffies-dev->jiffies);
546         dev->jiffies = jiffies;
547         if (dev->ms >= 1000) {
548                 dev->ms -= 1000;
549                 dev->s++;
550                 if (dev->s >= 60) {
551                         dev->s -= 60;
552                         dev->m++;
553                         if (dev->m > 60) {
554                                 dev->m -= 60;
555                                 dev->h++;
556                                 if (dev->h > 24)
557                                         dev->h -= 24;
558                         }
559                 }
560         }
561         sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
562                         dev->h, dev->m, dev->s, dev->ms);
563
564         dprintk(dev, 2, "vivifill at %s: Buffer 0x%08lx size= %d\n",
565                         dev->timestr, (unsigned long)tmpbuf, pos);
566
567         /* Advice that buffer was filled */
568         buf->vb.field_count++;
569         do_gettimeofday(&ts);
570         buf->vb.ts = ts;
571         buf->vb.state = VIDEOBUF_DONE;
572 }
573
574 static void vivi_thread_tick(struct vivi_fh *fh)
575 {
576         struct vivi_buffer *buf;
577         struct vivi_dev *dev = fh->dev;
578         struct vivi_dmaqueue *dma_q = &dev->vidq;
579
580         unsigned long flags = 0;
581
582         dprintk(dev, 1, "Thread tick\n");
583
584         spin_lock_irqsave(&dev->slock, flags);
585         if (list_empty(&dma_q->active)) {
586                 dprintk(dev, 1, "No active queue to serve\n");
587                 goto unlock;
588         }
589
590         buf = list_entry(dma_q->active.next,
591                          struct vivi_buffer, vb.queue);
592
593         /* Nobody is waiting on this buffer, return */
594         if (!waitqueue_active(&buf->vb.done))
595                 goto unlock;
596
597         list_del(&buf->vb.queue);
598
599         do_gettimeofday(&buf->vb.ts);
600
601         /* Fill buffer */
602         vivi_fillbuff(fh, buf);
603         dprintk(dev, 1, "filled buffer %p\n", buf);
604
605         wake_up(&buf->vb.done);
606         dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i);
607 unlock:
608         spin_unlock_irqrestore(&dev->slock, flags);
609         return;
610 }
611
612 #define frames_to_ms(frames)                                    \
613         ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
614
615 static void vivi_sleep(struct vivi_fh *fh)
616 {
617         struct vivi_dev *dev = fh->dev;
618         struct vivi_dmaqueue *dma_q = &dev->vidq;
619         int timeout;
620         DECLARE_WAITQUEUE(wait, current);
621
622         dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
623                 (unsigned long)dma_q);
624
625         add_wait_queue(&dma_q->wq, &wait);
626         if (kthread_should_stop())
627                 goto stop_task;
628
629         /* Calculate time to wake up */
630         timeout = msecs_to_jiffies(frames_to_ms(1));
631
632         vivi_thread_tick(fh);
633
634         schedule_timeout_interruptible(timeout);
635
636 stop_task:
637         remove_wait_queue(&dma_q->wq, &wait);
638         try_to_freeze();
639 }
640
641 static int vivi_thread(void *data)
642 {
643         struct vivi_fh  *fh = data;
644         struct vivi_dev *dev = fh->dev;
645
646         dprintk(dev, 1, "thread started\n");
647
648         set_freezable();
649
650         for (;;) {
651                 vivi_sleep(fh);
652
653                 if (kthread_should_stop())
654                         break;
655         }
656         dprintk(dev, 1, "thread: exit\n");
657         return 0;
658 }
659
660 static int vivi_start_thread(struct vivi_fh *fh)
661 {
662         struct vivi_dev *dev = fh->dev;
663         struct vivi_dmaqueue *dma_q = &dev->vidq;
664
665         dma_q->frame = 0;
666         dma_q->ini_jiffies = jiffies;
667
668         dprintk(dev, 1, "%s\n", __func__);
669
670         dma_q->kthread = kthread_run(vivi_thread, fh, "vivi");
671
672         if (IS_ERR(dma_q->kthread)) {
673                 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
674                 return PTR_ERR(dma_q->kthread);
675         }
676         /* Wakes thread */
677         wake_up_interruptible(&dma_q->wq);
678
679         dprintk(dev, 1, "returning from %s\n", __func__);
680         return 0;
681 }
682
683 static void vivi_stop_thread(struct vivi_dmaqueue  *dma_q)
684 {
685         struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
686
687         dprintk(dev, 1, "%s\n", __func__);
688         /* shutdown control thread */
689         if (dma_q->kthread) {
690                 kthread_stop(dma_q->kthread);
691                 dma_q->kthread = NULL;
692         }
693 }
694
695 /* ------------------------------------------------------------------
696         Videobuf operations
697    ------------------------------------------------------------------*/
698 static int
699 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
700 {
701         struct vivi_fh  *fh = vq->priv_data;
702         struct vivi_dev *dev  = fh->dev;
703
704         *size = fh->width*fh->height*2;
705
706         if (0 == *count)
707                 *count = 32;
708
709         while (*size * *count > vid_limit * 1024 * 1024)
710                 (*count)--;
711
712         dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__,
713                 *count, *size);
714
715         return 0;
716 }
717
718 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
719 {
720         struct vivi_fh  *fh = vq->priv_data;
721         struct vivi_dev *dev  = fh->dev;
722
723         dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state);
724
725         if (in_interrupt())
726                 BUG();
727
728         videobuf_vmalloc_free(&buf->vb);
729         dprintk(dev, 1, "free_buffer: freed\n");
730         buf->vb.state = VIDEOBUF_NEEDS_INIT;
731 }
732
733 #define norm_maxw() 1024
734 #define norm_maxh() 768
735 static int
736 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
737                                                 enum v4l2_field field)
738 {
739         struct vivi_fh     *fh  = vq->priv_data;
740         struct vivi_dev    *dev = fh->dev;
741         struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
742         int rc;
743
744         dprintk(dev, 1, "%s, field=%d\n", __func__, field);
745
746         BUG_ON(NULL == fh->fmt);
747
748         if (fh->width  < 48 || fh->width  > norm_maxw() ||
749             fh->height < 32 || fh->height > norm_maxh())
750                 return -EINVAL;
751
752         buf->vb.size = fh->width*fh->height*2;
753         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
754                 return -EINVAL;
755
756         /* These properties only change when queue is idle, see s_fmt */
757         buf->fmt       = fh->fmt;
758         buf->vb.width  = fh->width;
759         buf->vb.height = fh->height;
760         buf->vb.field  = field;
761
762         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
763                 rc = videobuf_iolock(vq, &buf->vb, NULL);
764                 if (rc < 0)
765                         goto fail;
766         }
767
768         buf->vb.state = VIDEOBUF_PREPARED;
769
770         return 0;
771
772 fail:
773         free_buffer(vq, buf);
774         return rc;
775 }
776
777 static void
778 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
779 {
780         struct vivi_buffer    *buf  = container_of(vb, struct vivi_buffer, vb);
781         struct vivi_fh        *fh   = vq->priv_data;
782         struct vivi_dev       *dev  = fh->dev;
783         struct vivi_dmaqueue *vidq = &dev->vidq;
784
785         dprintk(dev, 1, "%s\n", __func__);
786
787         buf->vb.state = VIDEOBUF_QUEUED;
788         list_add_tail(&buf->vb.queue, &vidq->active);
789 }
790
791 static void buffer_release(struct videobuf_queue *vq,
792                            struct videobuf_buffer *vb)
793 {
794         struct vivi_buffer   *buf  = container_of(vb, struct vivi_buffer, vb);
795         struct vivi_fh       *fh   = vq->priv_data;
796         struct vivi_dev      *dev  = (struct vivi_dev *)fh->dev;
797
798         dprintk(dev, 1, "%s\n", __func__);
799
800         free_buffer(vq, buf);
801 }
802
803 static struct videobuf_queue_ops vivi_video_qops = {
804         .buf_setup      = buffer_setup,
805         .buf_prepare    = buffer_prepare,
806         .buf_queue      = buffer_queue,
807         .buf_release    = buffer_release,
808 };
809
810 /* ------------------------------------------------------------------
811         IOCTL vidioc handling
812    ------------------------------------------------------------------*/
813 static int vidioc_querycap(struct file *file, void  *priv,
814                                         struct v4l2_capability *cap)
815 {
816         struct vivi_fh  *fh  = priv;
817         struct vivi_dev *dev = fh->dev;
818
819         strcpy(cap->driver, "vivi");
820         strcpy(cap->card, "vivi");
821         strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
822         cap->version = VIVI_VERSION;
823         cap->capabilities =     V4L2_CAP_VIDEO_CAPTURE |
824                                 V4L2_CAP_STREAMING     |
825                                 V4L2_CAP_READWRITE;
826         return 0;
827 }
828
829 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
830                                         struct v4l2_fmtdesc *f)
831 {
832         struct vivi_fmt *fmt;
833
834         if (f->index >= ARRAY_SIZE(formats))
835                 return -EINVAL;
836
837         fmt = &formats[f->index];
838
839         strlcpy(f->description, fmt->name, sizeof(f->description));
840         f->pixelformat = fmt->fourcc;
841         return 0;
842 }
843
844 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
845                                         struct v4l2_format *f)
846 {
847         struct vivi_fh *fh = priv;
848
849         f->fmt.pix.width        = fh->width;
850         f->fmt.pix.height       = fh->height;
851         f->fmt.pix.field        = fh->vb_vidq.field;
852         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
853         f->fmt.pix.bytesperline =
854                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
855         f->fmt.pix.sizeimage =
856                 f->fmt.pix.height * f->fmt.pix.bytesperline;
857
858         return (0);
859 }
860
861 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
862                         struct v4l2_format *f)
863 {
864         struct vivi_fh  *fh  = priv;
865         struct vivi_dev *dev = fh->dev;
866         struct vivi_fmt *fmt;
867         enum v4l2_field field;
868         unsigned int maxw, maxh;
869
870         fmt = get_format(f);
871         if (!fmt) {
872                 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
873                         f->fmt.pix.pixelformat);
874                 return -EINVAL;
875         }
876
877         field = f->fmt.pix.field;
878
879         if (field == V4L2_FIELD_ANY) {
880                 field = V4L2_FIELD_INTERLACED;
881         } else if (V4L2_FIELD_INTERLACED != field) {
882                 dprintk(dev, 1, "Field type invalid.\n");
883                 return -EINVAL;
884         }
885
886         maxw  = norm_maxw();
887         maxh  = norm_maxh();
888
889         f->fmt.pix.field = field;
890         if (f->fmt.pix.height < 32)
891                 f->fmt.pix.height = 32;
892         if (f->fmt.pix.height > maxh)
893                 f->fmt.pix.height = maxh;
894         if (f->fmt.pix.width < 48)
895                 f->fmt.pix.width = 48;
896         if (f->fmt.pix.width > maxw)
897                 f->fmt.pix.width = maxw;
898         f->fmt.pix.width &= ~0x03;
899         f->fmt.pix.bytesperline =
900                 (f->fmt.pix.width * fmt->depth) >> 3;
901         f->fmt.pix.sizeimage =
902                 f->fmt.pix.height * f->fmt.pix.bytesperline;
903
904         return 0;
905 }
906
907 /* precalculate color bar values to speed up rendering */
908 static void precalculate_bars(struct vivi_fh *fh)
909 {
910         struct vivi_dev *dev = fh->dev;
911         unsigned char r, g, b;
912         int k, is_yuv;
913
914         fh->input = dev->input;
915
916         for (k = 0; k < 8; k++) {
917                 r = bars[fh->input].bar[k][0];
918                 g = bars[fh->input].bar[k][1];
919                 b = bars[fh->input].bar[k][2];
920                 is_yuv = 0;
921
922                 switch (fh->fmt->fourcc) {
923                 case V4L2_PIX_FMT_YUYV:
924                 case V4L2_PIX_FMT_UYVY:
925                         is_yuv = 1;
926                         break;
927                 case V4L2_PIX_FMT_RGB565:
928                 case V4L2_PIX_FMT_RGB565X:
929                         r >>= 3;
930                         g >>= 2;
931                         b >>= 3;
932                         break;
933                 case V4L2_PIX_FMT_RGB555:
934                 case V4L2_PIX_FMT_RGB555X:
935                         r >>= 3;
936                         g >>= 3;
937                         b >>= 3;
938                         break;
939                 }
940
941                 if (is_yuv) {
942                         fh->bars[k][0] = TO_Y(r, g, b); /* Luma */
943                         fh->bars[k][1] = TO_U(r, g, b); /* Cb */
944                         fh->bars[k][2] = TO_V(r, g, b); /* Cr */
945                 } else {
946                         fh->bars[k][0] = r;
947                         fh->bars[k][1] = g;
948                         fh->bars[k][2] = b;
949                 }
950         }
951
952 }
953
954 /*FIXME: This seems to be generic enough to be at videodev2 */
955 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
956                                         struct v4l2_format *f)
957 {
958         struct vivi_fh *fh = priv;
959         struct videobuf_queue *q = &fh->vb_vidq;
960
961         int ret = vidioc_try_fmt_vid_cap(file, fh, f);
962         if (ret < 0)
963                 return ret;
964
965         mutex_lock(&q->vb_lock);
966
967         if (videobuf_queue_is_busy(&fh->vb_vidq)) {
968                 dprintk(fh->dev, 1, "%s queue busy\n", __func__);
969                 ret = -EBUSY;
970                 goto out;
971         }
972
973         fh->fmt           = get_format(f);
974         fh->width         = f->fmt.pix.width;
975         fh->height        = f->fmt.pix.height;
976         fh->vb_vidq.field = f->fmt.pix.field;
977         fh->type          = f->type;
978
979         precalculate_bars(fh);
980
981         ret = 0;
982 out:
983         mutex_unlock(&q->vb_lock);
984
985         return ret;
986 }
987
988 static int vidioc_reqbufs(struct file *file, void *priv,
989                           struct v4l2_requestbuffers *p)
990 {
991         struct vivi_fh  *fh = priv;
992
993         return (videobuf_reqbufs(&fh->vb_vidq, p));
994 }
995
996 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
997 {
998         struct vivi_fh  *fh = priv;
999
1000         return (videobuf_querybuf(&fh->vb_vidq, p));
1001 }
1002
1003 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1004 {
1005         struct vivi_fh *fh = priv;
1006
1007         return (videobuf_qbuf(&fh->vb_vidq, p));
1008 }
1009
1010 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1011 {
1012         struct vivi_fh  *fh = priv;
1013
1014         return (videobuf_dqbuf(&fh->vb_vidq, p,
1015                                 file->f_flags & O_NONBLOCK));
1016 }
1017
1018 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1019 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1020 {
1021         struct vivi_fh  *fh = priv;
1022
1023         return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1024 }
1025 #endif
1026
1027 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1028 {
1029         struct vivi_fh  *fh = priv;
1030
1031         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1032                 return -EINVAL;
1033         if (i != fh->type)
1034                 return -EINVAL;
1035
1036         return videobuf_streamon(&fh->vb_vidq);
1037 }
1038
1039 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1040 {
1041         struct vivi_fh  *fh = priv;
1042
1043         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1044                 return -EINVAL;
1045         if (i != fh->type)
1046                 return -EINVAL;
1047
1048         return videobuf_streamoff(&fh->vb_vidq);
1049 }
1050
1051 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1052 {
1053         return 0;
1054 }
1055
1056 /* only one input in this sample driver */
1057 static int vidioc_enum_input(struct file *file, void *priv,
1058                                 struct v4l2_input *inp)
1059 {
1060         if (inp->index >= NUM_INPUTS)
1061                 return -EINVAL;
1062
1063         inp->type = V4L2_INPUT_TYPE_CAMERA;
1064         inp->std = V4L2_STD_525_60;
1065         sprintf(inp->name, "Camera %u", inp->index);
1066
1067         return (0);
1068 }
1069
1070 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1071 {
1072         struct vivi_fh *fh = priv;
1073         struct vivi_dev *dev = fh->dev;
1074
1075         *i = dev->input;
1076
1077         return (0);
1078 }
1079 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1080 {
1081         struct vivi_fh *fh = priv;
1082         struct vivi_dev *dev = fh->dev;
1083
1084         if (i >= NUM_INPUTS)
1085                 return -EINVAL;
1086
1087         dev->input = i;
1088         precalculate_bars(fh);
1089
1090         return (0);
1091 }
1092
1093         /* --- controls ---------------------------------------------- */
1094 static int vidioc_queryctrl(struct file *file, void *priv,
1095                             struct v4l2_queryctrl *qc)
1096 {
1097         int i;
1098
1099         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1100                 if (qc->id && qc->id == vivi_qctrl[i].id) {
1101                         memcpy(qc, &(vivi_qctrl[i]),
1102                                 sizeof(*qc));
1103                         return (0);
1104                 }
1105
1106         return -EINVAL;
1107 }
1108
1109 static int vidioc_g_ctrl(struct file *file, void *priv,
1110                          struct v4l2_control *ctrl)
1111 {
1112         struct vivi_fh *fh = priv;
1113         struct vivi_dev *dev = fh->dev;
1114         int i;
1115
1116         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1117                 if (ctrl->id == vivi_qctrl[i].id) {
1118                         ctrl->value = dev->qctl_regs[i];
1119                         return 0;
1120                 }
1121
1122         return -EINVAL;
1123 }
1124 static int vidioc_s_ctrl(struct file *file, void *priv,
1125                                 struct v4l2_control *ctrl)
1126 {
1127         struct vivi_fh *fh = priv;
1128         struct vivi_dev *dev = fh->dev;
1129         int i;
1130
1131         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1132                 if (ctrl->id == vivi_qctrl[i].id) {
1133                         if (ctrl->value < vivi_qctrl[i].minimum ||
1134                             ctrl->value > vivi_qctrl[i].maximum) {
1135                                 return -ERANGE;
1136                         }
1137                         dev->qctl_regs[i] = ctrl->value;
1138                         return 0;
1139                 }
1140         return -EINVAL;
1141 }
1142
1143 /* ------------------------------------------------------------------
1144         File operations for the device
1145    ------------------------------------------------------------------*/
1146
1147 static int vivi_open(struct file *file)
1148 {
1149         struct vivi_dev *dev = video_drvdata(file);
1150         struct vivi_fh *fh = NULL;
1151         int retval = 0;
1152
1153         mutex_lock(&dev->mutex);
1154         dev->users++;
1155
1156         if (dev->users > 1) {
1157                 dev->users--;
1158                 mutex_unlock(&dev->mutex);
1159                 return -EBUSY;
1160         }
1161
1162         dprintk(dev, 1, "open /dev/video%d type=%s users=%d\n", dev->vfd->num,
1163                 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1164
1165         /* allocate + initialize per filehandle data */
1166         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1167         if (NULL == fh) {
1168                 dev->users--;
1169                 retval = -ENOMEM;
1170         }
1171         mutex_unlock(&dev->mutex);
1172
1173         if (retval)
1174                 return retval;
1175
1176         file->private_data = fh;
1177         fh->dev      = dev;
1178
1179         fh->type     = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1180         fh->fmt      = &formats[0];
1181         fh->width    = 640;
1182         fh->height   = 480;
1183
1184         /* Resets frame counters */
1185         dev->h = 0;
1186         dev->m = 0;
1187         dev->s = 0;
1188         dev->ms = 0;
1189         dev->mv_count = 0;
1190         dev->jiffies = jiffies;
1191         sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
1192                         dev->h, dev->m, dev->s, dev->ms);
1193
1194         videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
1195                         NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1196                         sizeof(struct vivi_buffer), fh);
1197
1198         vivi_start_thread(fh);
1199
1200         return 0;
1201 }
1202
1203 static ssize_t
1204 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1205 {
1206         struct vivi_fh *fh = file->private_data;
1207
1208         if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1209                 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
1210                                         file->f_flags & O_NONBLOCK);
1211         }
1212         return 0;
1213 }
1214
1215 static unsigned int
1216 vivi_poll(struct file *file, struct poll_table_struct *wait)
1217 {
1218         struct vivi_fh        *fh = file->private_data;
1219         struct vivi_dev       *dev = fh->dev;
1220         struct videobuf_queue *q = &fh->vb_vidq;
1221
1222         dprintk(dev, 1, "%s\n", __func__);
1223
1224         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1225                 return POLLERR;
1226
1227         return videobuf_poll_stream(file, q, wait);
1228 }
1229
1230 static int vivi_close(struct file *file)
1231 {
1232         struct vivi_fh         *fh = file->private_data;
1233         struct vivi_dev *dev       = fh->dev;
1234         struct vivi_dmaqueue *vidq = &dev->vidq;
1235
1236         int minor = video_devdata(file)->minor;
1237
1238         vivi_stop_thread(vidq);
1239         videobuf_stop(&fh->vb_vidq);
1240         videobuf_mmap_free(&fh->vb_vidq);
1241
1242         kfree(fh);
1243
1244         mutex_lock(&dev->mutex);
1245         dev->users--;
1246         mutex_unlock(&dev->mutex);
1247
1248         dprintk(dev, 1, "close called (minor=%d, users=%d)\n",
1249                 minor, dev->users);
1250
1251         return 0;
1252 }
1253
1254 static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1255 {
1256         struct vivi_fh  *fh = file->private_data;
1257         struct vivi_dev *dev = fh->dev;
1258         int ret;
1259
1260         dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1261
1262         ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1263
1264         dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1265                 (unsigned long)vma->vm_start,
1266                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1267                 ret);
1268
1269         return ret;
1270 }
1271
1272 static const struct v4l2_file_operations vivi_fops = {
1273         .owner          = THIS_MODULE,
1274         .open           = vivi_open,
1275         .release        = vivi_close,
1276         .read           = vivi_read,
1277         .poll           = vivi_poll,
1278         .ioctl          = video_ioctl2, /* V4L2 ioctl handler */
1279         .mmap           = vivi_mmap,
1280 };
1281
1282 static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
1283         .vidioc_querycap      = vidioc_querycap,
1284         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1285         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1286         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1287         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1288         .vidioc_reqbufs       = vidioc_reqbufs,
1289         .vidioc_querybuf      = vidioc_querybuf,
1290         .vidioc_qbuf          = vidioc_qbuf,
1291         .vidioc_dqbuf         = vidioc_dqbuf,
1292         .vidioc_s_std         = vidioc_s_std,
1293         .vidioc_enum_input    = vidioc_enum_input,
1294         .vidioc_g_input       = vidioc_g_input,
1295         .vidioc_s_input       = vidioc_s_input,
1296         .vidioc_queryctrl     = vidioc_queryctrl,
1297         .vidioc_g_ctrl        = vidioc_g_ctrl,
1298         .vidioc_s_ctrl        = vidioc_s_ctrl,
1299         .vidioc_streamon      = vidioc_streamon,
1300         .vidioc_streamoff     = vidioc_streamoff,
1301 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1302         .vidiocgmbuf          = vidiocgmbuf,
1303 #endif
1304 };
1305
1306 static struct video_device vivi_template = {
1307         .name           = "vivi",
1308         .fops           = &vivi_fops,
1309         .ioctl_ops      = &vivi_ioctl_ops,
1310         .minor          = -1,
1311         .release        = video_device_release,
1312
1313         .tvnorms              = V4L2_STD_525_60,
1314         .current_norm         = V4L2_STD_NTSC_M,
1315 };
1316
1317 /* -----------------------------------------------------------------
1318         Initialization and module stuff
1319    ------------------------------------------------------------------*/
1320
1321 static int vivi_release(void)
1322 {
1323         struct vivi_dev *dev;
1324         struct list_head *list;
1325
1326         while (!list_empty(&vivi_devlist)) {
1327                 list = vivi_devlist.next;
1328                 list_del(list);
1329                 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1330
1331                 v4l2_info(&dev->v4l2_dev, "unregistering /dev/video%d\n",
1332                         dev->vfd->num);
1333                 video_unregister_device(dev->vfd);
1334                 v4l2_device_unregister(&dev->v4l2_dev);
1335                 kfree(dev);
1336         }
1337
1338         return 0;
1339 }
1340
1341 static int __init vivi_create_instance(int inst)
1342 {
1343         struct vivi_dev *dev;
1344         struct video_device *vfd;
1345         int ret, i;
1346
1347         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1348         if (!dev)
1349                 return -ENOMEM;
1350
1351         snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1352                         "%s-%03d", VIVI_MODULE_NAME, inst);
1353         ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1354         if (ret)
1355                 goto free_dev;
1356
1357         /* init video dma queues */
1358         INIT_LIST_HEAD(&dev->vidq.active);
1359         init_waitqueue_head(&dev->vidq.wq);
1360
1361         /* initialize locks */
1362         spin_lock_init(&dev->slock);
1363         mutex_init(&dev->mutex);
1364
1365         ret = -ENOMEM;
1366         vfd = video_device_alloc();
1367         if (!vfd)
1368                 goto unreg_dev;
1369
1370         *vfd = vivi_template;
1371
1372         ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1373         if (ret < 0)
1374                 goto rel_vdev;
1375
1376         video_set_drvdata(vfd, dev);
1377
1378         /* Set all controls to their default value. */
1379         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1380                 dev->qctl_regs[i] = vivi_qctrl[i].default_value;
1381
1382         /* Now that everything is fine, let's add it to device list */
1383         list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1384
1385         snprintf(vfd->name, sizeof(vfd->name), "%s (%i)",
1386                         vivi_template.name, vfd->num);
1387
1388         if (video_nr >= 0)
1389                 video_nr++;
1390
1391         dev->vfd = vfd;
1392         v4l2_info(&dev->v4l2_dev, "V4L2 device registered as /dev/video%d\n",
1393                         vfd->num);
1394         return 0;
1395
1396 rel_vdev:
1397         video_device_release(vfd);
1398 unreg_dev:
1399         v4l2_device_unregister(&dev->v4l2_dev);
1400 free_dev:
1401         kfree(dev);
1402         return ret;
1403 }
1404
1405 /* This routine allocates from 1 to n_devs virtual drivers.
1406
1407    The real maximum number of virtual drivers will depend on how many drivers
1408    will succeed. This is limited to the maximum number of devices that
1409    videodev supports, which is equal to VIDEO_NUM_DEVICES.
1410  */
1411 static int __init vivi_init(void)
1412 {
1413         int ret, i;
1414
1415         if (n_devs <= 0)
1416                 n_devs = 1;
1417
1418         for (i = 0; i < n_devs; i++) {
1419                 ret = vivi_create_instance(i);
1420                 if (ret) {
1421                         /* If some instantiations succeeded, keep driver */
1422                         if (i)
1423                                 ret = 0;
1424                         break;
1425                 }
1426         }
1427
1428         if (ret < 0) {
1429                 printk(KERN_INFO "Error %d while loading vivi driver\n", ret);
1430                 return ret;
1431         }
1432
1433         printk(KERN_INFO "Video Technology Magazine Virtual Video "
1434                         "Capture Board ver %u.%u.%u successfully loaded.\n",
1435                         (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF,
1436                         VIVI_VERSION & 0xFF);
1437
1438         /* n_devs will reflect the actual number of allocated devices */
1439         n_devs = i;
1440
1441         return ret;
1442 }
1443
1444 static void __exit vivi_exit(void)
1445 {
1446         vivi_release();
1447 }
1448
1449 module_init(vivi_init);
1450 module_exit(vivi_exit);