Merge branch 'master' into gfs2
[linux-2.6] / 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/videodev2.h>
29 #include <linux/dma-mapping.h>
30 #ifdef CONFIG_VIDEO_V4L1_COMPAT
31 /* Include V4L1 specific functions. Should be removed soon */
32 #include <linux/videodev.h>
33 #endif
34 #include <linux/interrupt.h>
35 #include <media/video-buf.h>
36 #include <media/v4l2-common.h>
37 #include <linux/kthread.h>
38 #include <linux/highmem.h>
39
40 /* Wake up at about 30 fps */
41 #define WAKE_NUMERATOR 30
42 #define WAKE_DENOMINATOR 1001
43 #define BUFFER_TIMEOUT     msecs_to_jiffies(500)  /* 0.5 seconds */
44
45 /* These timers are for 1 fps - used only for testing */
46 //#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
47 //#define BUFFER_TIMEOUT     msecs_to_jiffies(5000)  /* 5 seconds */
48
49 #include "font.h"
50
51 #define VIVI_MAJOR_VERSION 0
52 #define VIVI_MINOR_VERSION 4
53 #define VIVI_RELEASE 0
54 #define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
55
56 /* Declare static vars that will be used as parameters */
57 static unsigned int vid_limit = 16;     /* Video memory limit, in Mb */
58 static struct video_device vivi;        /* Video device */
59 static int video_nr = -1;               /* /dev/videoN, -1 for autodetect */
60
61 /* supported controls */
62 static struct v4l2_queryctrl vivi_qctrl[] = {
63         {
64                 .id            = V4L2_CID_AUDIO_VOLUME,
65                 .name          = "Volume",
66                 .minimum       = 0,
67                 .maximum       = 65535,
68                 .step          = 65535/100,
69                 .default_value = 65535,
70                 .flags         = 0,
71                 .type          = V4L2_CTRL_TYPE_INTEGER,
72         },{
73                 .id            = V4L2_CID_BRIGHTNESS,
74                 .type          = V4L2_CTRL_TYPE_INTEGER,
75                 .name          = "Brightness",
76                 .minimum       = 0,
77                 .maximum       = 255,
78                 .step          = 1,
79                 .default_value = 127,
80                 .flags         = 0,
81         }, {
82                 .id            = V4L2_CID_CONTRAST,
83                 .type          = V4L2_CTRL_TYPE_INTEGER,
84                 .name          = "Contrast",
85                 .minimum       = 0,
86                 .maximum       = 255,
87                 .step          = 0x1,
88                 .default_value = 0x10,
89                 .flags         = 0,
90         }, {
91                 .id            = V4L2_CID_SATURATION,
92                 .type          = V4L2_CTRL_TYPE_INTEGER,
93                 .name          = "Saturation",
94                 .minimum       = 0,
95                 .maximum       = 255,
96                 .step          = 0x1,
97                 .default_value = 127,
98                 .flags         = 0,
99         }, {
100                 .id            = V4L2_CID_HUE,
101                 .type          = V4L2_CTRL_TYPE_INTEGER,
102                 .name          = "Hue",
103                 .minimum       = -128,
104                 .maximum       = 127,
105                 .step          = 0x1,
106                 .default_value = 0,
107                 .flags         = 0,
108         }
109 };
110
111 static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
112
113 #define dprintk(level,fmt, arg...)                                      \
114         do {                                                            \
115                 if (vivi.debug >= (level))                              \
116                         printk(KERN_DEBUG "vivi: " fmt , ## arg);       \
117         } while (0)
118
119 /* ------------------------------------------------------------------
120         Basic structures
121    ------------------------------------------------------------------*/
122
123 struct vivi_fmt {
124         char  *name;
125         u32   fourcc;          /* v4l2 format id */
126         int   depth;
127 };
128
129 static struct vivi_fmt format = {
130         .name     = "4:2:2, packed, YUYV",
131         .fourcc   = V4L2_PIX_FMT_YUYV,
132         .depth    = 16,
133 };
134
135 struct sg_to_addr {
136         int pos;
137         struct scatterlist *sg;
138 };
139
140 /* buffer for one video frame */
141 struct vivi_buffer {
142         /* common v4l buffer stuff -- must be first */
143         struct videobuf_buffer vb;
144
145         struct vivi_fmt        *fmt;
146
147         struct sg_to_addr      *to_addr;
148 };
149
150 struct vivi_dmaqueue {
151         struct list_head       active;
152         struct list_head       queued;
153         struct timer_list      timeout;
154
155         /* thread for generating video stream*/
156         struct task_struct         *kthread;
157         wait_queue_head_t          wq;
158         /* Counters to control fps rate */
159         int                        frame;
160         int                        ini_jiffies;
161 };
162
163 static LIST_HEAD(vivi_devlist);
164
165 struct vivi_dev {
166         struct list_head           vivi_devlist;
167
168         struct semaphore           lock;
169
170         int                        users;
171
172         /* various device info */
173         unsigned int               resources;
174         struct video_device        vfd;
175
176         struct vivi_dmaqueue       vidq;
177
178         /* Several counters */
179         int                        h,m,s,us,jiffies;
180         char                       timestr[13];
181 };
182
183 struct vivi_fh {
184         struct vivi_dev            *dev;
185
186         /* video capture */
187         struct vivi_fmt            *fmt;
188         unsigned int               width,height;
189         struct videobuf_queue      vb_vidq;
190
191         enum v4l2_buf_type         type;
192 };
193
194 /* ------------------------------------------------------------------
195         DMA and thread functions
196    ------------------------------------------------------------------*/
197
198 /* Bars and Colors should match positions */
199
200 enum colors {
201         WHITE,
202         AMBAR,
203         CYAN,
204         GREEN,
205         MAGENTA,
206         RED,
207         BLUE
208 };
209
210 static u8 bars[8][3] = {
211         /* R   G   B */
212         {204,204,204},  /* white */
213         {208,208,  0},  /* ambar */
214         {  0,206,206},  /* cyan */
215         {  0,239,  0},  /* green */
216         {239,  0,239},  /* magenta */
217         {205,  0,  0},  /* red */
218         {  0,  0,255},  /* blue */
219         {  0,  0,  0}
220 };
221
222 #define TO_Y(r,g,b) (((16829*r +33039*g +6416*b  + 32768)>>16)+16)
223 /* RGB to  V(Cr) Color transform */
224 #define TO_V(r,g,b) (((28784*r -24103*g -4681*b  + 32768)>>16)+128)
225 /* RGB to  U(Cb) Color transform */
226 #define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
227
228 #define TSTAMP_MIN_Y 24
229 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
230 #define TSTAMP_MIN_X 64
231
232 static void prep_to_addr(struct sg_to_addr to_addr[],
233                          struct videobuf_buffer *vb)
234 {
235         int i, pos=0;
236
237         for (i=0;i<vb->dma.nr_pages;i++) {
238                 to_addr[i].sg=&vb->dma.sglist[i];
239                 to_addr[i].pos=pos;
240                 pos += vb->dma.sglist[i].length;
241         }
242 }
243
244 static int get_addr_pos(int pos, int pages, struct sg_to_addr to_addr[])
245 {
246         int p1=0,p2=pages-1,p3=pages/2;
247
248         /* Sanity test */
249         BUG_ON (pos>=to_addr[p2].pos+to_addr[p2].sg->length);
250
251         while (p1+1<p2) {
252                 if (pos < to_addr[p3].pos) {
253                         p2=p3;
254                 } else {
255                         p1=p3;
256                 }
257                 p3=(p1+p2)/2;
258         }
259         if (pos >= to_addr[p2].pos)
260                 p1=p2;
261
262         return (p1);
263 }
264
265 static void gen_line(struct sg_to_addr to_addr[],int inipos,int pages,int wmax,
266                      int hmax, int line, char *timestr)
267 {
268         int  w,i,j,pos=inipos,pgpos,oldpg,y;
269         char *p,*s,*basep;
270         struct page *pg;
271         u8   chr,r,g,b,color;
272
273         /* Get first addr pointed to pixel position */
274         oldpg=get_addr_pos(pos,pages,to_addr);
275         pg=pfn_to_page(to_addr[oldpg].sg->dma_address >> PAGE_SHIFT);
276         basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
277
278         /* We will just duplicate the second pixel at the packet */
279         wmax/=2;
280
281         /* Generate a standard color bar pattern */
282         for (w=0;w<wmax;w++) {
283                 r=bars[w*7/wmax][0];
284                 g=bars[w*7/wmax][1];
285                 b=bars[w*7/wmax][2];
286
287                 for (color=0;color<4;color++) {
288                         pgpos=get_addr_pos(pos,pages,to_addr);
289                         if (pgpos!=oldpg) {
290                                 pg=pfn_to_page(to_addr[pgpos].sg->dma_address >> PAGE_SHIFT);
291                                 kunmap_atomic(basep, KM_BOUNCE_READ);
292                                 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
293                                 oldpg=pgpos;
294                         }
295                         p=basep+pos-to_addr[pgpos].pos;
296
297                         switch (color) {
298                                 case 0:
299                                 case 2:
300                                         *p=TO_Y(r,g,b);         /* Luminance */
301                                         break;
302                                 case 1:
303                                         *p=TO_U(r,g,b);         /* Cb */
304                                         break;
305                                 case 3:
306                                         *p=TO_V(r,g,b);         /* Cr */
307                                         break;
308                         }
309                         pos++;
310                 }
311         }
312
313         /* Checks if it is possible to show timestamp */
314         if (TSTAMP_MAX_Y>=hmax)
315                 goto end;
316         if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
317                 goto end;
318
319         /* Print stream time */
320         if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
321                 j=TSTAMP_MIN_X;
322                 for (s=timestr;*s;s++) {
323                         chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
324                         for (i=0;i<7;i++) {
325                                 if (chr&1<<(7-i)) { /* Font color*/
326                                         r=bars[BLUE][0];
327                                         g=bars[BLUE][1];
328                                         b=bars[BLUE][2];
329                                         r=g=b=0;
330                                         g=198;
331                                 } else { /* Background color */
332                                         r=bars[WHITE][0];
333                                         g=bars[WHITE][1];
334                                         b=bars[WHITE][2];
335                                         r=g=b=0;
336                                 }
337
338                                 pos=inipos+j*2;
339                                 for (color=0;color<4;color++) {
340                                         pgpos=get_addr_pos(pos,pages,to_addr);
341                                         if (pgpos!=oldpg) {
342                                                 pg=pfn_to_page(to_addr[pgpos].
343                                                                 sg->dma_address
344                                                                 >> PAGE_SHIFT);
345                                                 kunmap_atomic(basep,
346                                                                 KM_BOUNCE_READ);
347                                                 basep= kmap_atomic(pg,
348                                                         KM_BOUNCE_READ)+
349                                                         to_addr[pgpos].sg->offset;
350                                                 oldpg=pgpos;
351                                         }
352                                         p=basep+pos-to_addr[pgpos].pos;
353
354                                         y=TO_Y(r,g,b);
355
356                                         switch (color) {
357                                                 case 0:
358                                                 case 2:
359                                                         *p=TO_Y(r,g,b);         /* Luminance */
360                                                         break;
361                                                 case 1:
362                                                         *p=TO_U(r,g,b);         /* Cb */
363                                                         break;
364                                                 case 3:
365                                                         *p=TO_V(r,g,b);         /* Cr */
366                                                         break;
367                                         }
368                                         pos++;
369                                 }
370                                 j++;
371                         }
372                 }
373         }
374
375
376 end:
377         kunmap_atomic(basep, KM_BOUNCE_READ);
378 }
379 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
380 {
381         int h,pos=0;
382         int hmax  = buf->vb.height;
383         int wmax  = buf->vb.width;
384         struct videobuf_buffer *vb=&buf->vb;
385         struct sg_to_addr *to_addr=buf->to_addr;
386         struct timeval ts;
387
388         /* Test if DMA mapping is ready */
389         if (!vb->dma.sglist[0].dma_address)
390                 return;
391
392         prep_to_addr(to_addr,vb);
393
394         /* Check if there is enough memory */
395         BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
396
397         for (h=0;h<hmax;h++) {
398                 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
399                 pos += wmax*2;
400         }
401
402         /* Updates stream time */
403
404         dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
405         dev->jiffies=jiffies;
406         if (dev->us>=1000000) {
407                 dev->us-=1000000;
408                 dev->s++;
409                 if (dev->s>=60) {
410                         dev->s-=60;
411                         dev->m++;
412                         if (dev->m>60) {
413                                 dev->m-=60;
414                                 dev->h++;
415                                 if (dev->h>24)
416                                         dev->h-=24;
417                         }
418                 }
419         }
420         sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
421                         dev->h,dev->m,dev->s,(dev->us+500)/1000);
422
423         dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
424                         (unsigned long)buf->vb.dma.vmalloc,pos);
425
426         /* Advice that buffer was filled */
427         buf->vb.state = STATE_DONE;
428         buf->vb.field_count++;
429         do_gettimeofday(&ts);
430         buf->vb.ts = ts;
431
432         list_del(&buf->vb.queue);
433         wake_up(&buf->vb.done);
434 }
435
436 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
437
438 static void vivi_thread_tick(struct vivi_dmaqueue  *dma_q)
439 {
440         struct vivi_buffer    *buf;
441         struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
442
443         int bc;
444
445         /* Announces videobuf that all went ok */
446         for (bc = 0;; bc++) {
447                 if (list_empty(&dma_q->active)) {
448                         dprintk(1,"No active queue to serve\n");
449                         break;
450                 }
451
452                 buf = list_entry(dma_q->active.next,
453                                  struct vivi_buffer, vb.queue);
454
455                 /* Nobody is waiting something to be done, just return */
456                 if (!waitqueue_active(&buf->vb.done)) {
457                         mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
458                         return;
459                 }
460
461                 do_gettimeofday(&buf->vb.ts);
462                 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
463
464                 /* Fill buffer */
465                 vivi_fillbuff(dev,buf);
466         }
467         if (list_empty(&dma_q->active)) {
468                 del_timer(&dma_q->timeout);
469         } else {
470                 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
471         }
472         if (bc != 1)
473                 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
474 }
475
476 static void vivi_sleep(struct vivi_dmaqueue  *dma_q)
477 {
478         int timeout;
479         DECLARE_WAITQUEUE(wait, current);
480
481         dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
482
483         add_wait_queue(&dma_q->wq, &wait);
484         if (!kthread_should_stop()) {
485                 dma_q->frame++;
486
487                 /* Calculate time to wake up */
488                 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
489
490                 if (timeout <= 0) {
491                         int old=dma_q->frame;
492                         dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
493
494                         timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
495
496                         dprintk(1,"underrun, losed %d frames. "
497                                   "Now, frame is %d. Waking on %d jiffies\n",
498                                         dma_q->frame-old,dma_q->frame,timeout);
499                 } else
500                         dprintk(1,"will sleep for %i jiffies\n",timeout);
501
502                 vivi_thread_tick(dma_q);
503
504                 schedule_timeout_interruptible (timeout);
505         }
506
507         remove_wait_queue(&dma_q->wq, &wait);
508         try_to_freeze();
509 }
510
511 static int vivi_thread(void *data)
512 {
513         struct vivi_dmaqueue  *dma_q=data;
514
515         dprintk(1,"thread started\n");
516
517         for (;;) {
518                 vivi_sleep(dma_q);
519
520                 if (kthread_should_stop())
521                         break;
522         }
523         dprintk(1, "thread: exit\n");
524         return 0;
525 }
526
527 static int vivi_start_thread(struct vivi_dmaqueue  *dma_q)
528 {
529         dma_q->frame=0;
530         dma_q->ini_jiffies=jiffies;
531
532         dprintk(1,"%s\n",__FUNCTION__);
533         init_waitqueue_head(&dma_q->wq);
534
535         dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
536
537         if (dma_q->kthread == NULL) {
538                 printk(KERN_ERR "vivi: kernel_thread() failed\n");
539                 return -EINVAL;
540         }
541         dprintk(1,"returning from %s\n",__FUNCTION__);
542         return 0;
543 }
544
545 static void vivi_stop_thread(struct vivi_dmaqueue  *dma_q)
546 {
547         dprintk(1,"%s\n",__FUNCTION__);
548         /* shutdown control thread */
549         if (dma_q->kthread) {
550                 kthread_stop(dma_q->kthread);
551                 dma_q->kthread=NULL;
552         }
553 }
554
555 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
556 {
557         struct vivi_buffer *buf, *prev;
558         struct list_head *item;
559
560         dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
561
562         if (!list_empty(&dma_q->active)) {
563                 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
564                 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
565                         buf, buf->vb.i);
566
567                 dprintk(1,"Restarting video dma\n");
568                 vivi_stop_thread(dma_q);
569 //              vivi_start_thread(dma_q);
570
571                 /* cancel all outstanding capture / vbi requests */
572                 list_for_each(item,&dma_q->active) {
573                         buf = list_entry(item, struct vivi_buffer, vb.queue);
574
575                         list_del(&buf->vb.queue);
576                         buf->vb.state = STATE_ERROR;
577                         wake_up(&buf->vb.done);
578                 }
579                 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
580
581                 return 0;
582         }
583
584         prev = NULL;
585         for (;;) {
586                 if (list_empty(&dma_q->queued))
587                         return 0;
588                 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
589                 if (NULL == prev) {
590                         list_del(&buf->vb.queue);
591                         list_add_tail(&buf->vb.queue,&dma_q->active);
592
593                         dprintk(1,"Restarting video dma\n");
594                         vivi_stop_thread(dma_q);
595                         vivi_start_thread(dma_q);
596
597                         buf->vb.state = STATE_ACTIVE;
598                         mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
599                         dprintk(2,"[%p/%d] restart_queue - first active\n",
600                                 buf,buf->vb.i);
601
602                 } else if (prev->vb.width  == buf->vb.width  &&
603                            prev->vb.height == buf->vb.height &&
604                            prev->fmt       == buf->fmt) {
605                         list_del(&buf->vb.queue);
606                         list_add_tail(&buf->vb.queue,&dma_q->active);
607                         buf->vb.state = STATE_ACTIVE;
608                         dprintk(2,"[%p/%d] restart_queue - move to active\n",
609                                 buf,buf->vb.i);
610                 } else {
611                         return 0;
612                 }
613                 prev = buf;
614         }
615 }
616
617 static void vivi_vid_timeout(unsigned long data)
618 {
619         struct vivi_dev      *dev  = (struct vivi_dev*)data;
620         struct vivi_dmaqueue *vidq = &dev->vidq;
621         struct vivi_buffer   *buf;
622
623         while (!list_empty(&vidq->active)) {
624                 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
625                 list_del(&buf->vb.queue);
626                 buf->vb.state = STATE_ERROR;
627                 wake_up(&buf->vb.done);
628                 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
629         }
630
631         restart_video_queue(vidq);
632 }
633
634 /* ------------------------------------------------------------------
635         Videobuf operations
636    ------------------------------------------------------------------*/
637 static int
638 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
639 {
640         struct vivi_fh *fh = vq->priv_data;
641
642         *size = fh->width*fh->height*2;
643
644         if (0 == *count)
645                 *count = 32;
646         while (*size * *count > vid_limit * 1024 * 1024)
647                 (*count)--;
648         return 0;
649 }
650
651 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
652 {
653         dprintk(1,"%s\n",__FUNCTION__);
654
655         if (in_interrupt())
656                 BUG();
657
658         /*FIXME: Maybe a spinlock is required here */
659         kfree(buf->to_addr);
660         buf->to_addr=NULL;
661
662         videobuf_waiton(&buf->vb,0,0);
663         videobuf_dma_unmap(vq, &buf->vb.dma);
664         videobuf_dma_free(&buf->vb.dma);
665         buf->vb.state = STATE_NEEDS_INIT;
666 }
667
668 #define norm_maxw() 1024
669 #define norm_maxh() 768
670 static int
671 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
672                                                 enum v4l2_field field)
673 {
674         struct vivi_fh     *fh  = vq->priv_data;
675         struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
676         int rc, init_buffer = 0;
677
678 //      dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
679
680         BUG_ON(NULL == fh->fmt);
681         if (fh->width  < 48 || fh->width  > norm_maxw() ||
682             fh->height < 32 || fh->height > norm_maxh())
683                 return -EINVAL;
684         buf->vb.size = fh->width*fh->height*2;
685         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
686                 return -EINVAL;
687
688         if (buf->fmt       != fh->fmt    ||
689             buf->vb.width  != fh->width  ||
690             buf->vb.height != fh->height ||
691         buf->vb.field  != field) {
692                 buf->fmt       = fh->fmt;
693                 buf->vb.width  = fh->width;
694                 buf->vb.height = fh->height;
695                 buf->vb.field  = field;
696                 init_buffer = 1;
697         }
698
699         if (STATE_NEEDS_INIT == buf->vb.state) {
700                 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
701                         goto fail;
702         }
703
704         buf->vb.state = STATE_PREPARED;
705
706         if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
707                 rc=-ENOMEM;
708                 goto fail;
709         }
710
711         return 0;
712
713 fail:
714         free_buffer(vq,buf);
715         return rc;
716 }
717
718 static void
719 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
720 {
721         struct vivi_buffer    *buf     = container_of(vb,struct vivi_buffer,vb);
722         struct vivi_fh        *fh      = vq->priv_data;
723         struct vivi_dev       *dev     = fh->dev;
724         struct vivi_dmaqueue  *vidq    = &dev->vidq;
725         struct vivi_buffer    *prev;
726
727         if (!list_empty(&vidq->queued)) {
728                 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
729                 list_add_tail(&buf->vb.queue,&vidq->queued);
730                 buf->vb.state = STATE_QUEUED;
731                 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
732                         buf, buf->vb.i);
733         } else if (list_empty(&vidq->active)) {
734                 list_add_tail(&buf->vb.queue,&vidq->active);
735
736                 buf->vb.state = STATE_ACTIVE;
737                 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
738                 dprintk(2,"[%p/%d] buffer_queue - first active\n",
739                         buf, buf->vb.i);
740
741                 vivi_start_thread(vidq);
742         } else {
743                 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
744                 if (prev->vb.width  == buf->vb.width  &&
745                     prev->vb.height == buf->vb.height &&
746                     prev->fmt       == buf->fmt) {
747                         list_add_tail(&buf->vb.queue,&vidq->active);
748                         buf->vb.state = STATE_ACTIVE;
749                         dprintk(2,"[%p/%d] buffer_queue - append to active\n",
750                                 buf, buf->vb.i);
751
752                 } else {
753                         list_add_tail(&buf->vb.queue,&vidq->queued);
754                         buf->vb.state = STATE_QUEUED;
755                         dprintk(2,"[%p/%d] buffer_queue - first queued\n",
756                                 buf, buf->vb.i);
757                 }
758         }
759 }
760
761 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
762 {
763         struct vivi_buffer   *buf  = container_of(vb,struct vivi_buffer,vb);
764         struct vivi_fh       *fh   = vq->priv_data;
765         struct vivi_dev      *dev  = (struct vivi_dev*)fh->dev;
766         struct vivi_dmaqueue *vidq = &dev->vidq;
767
768         dprintk(1,"%s\n",__FUNCTION__);
769
770         vivi_stop_thread(vidq);
771
772         free_buffer(vq,buf);
773 }
774
775 static int vivi_map_sg(void *dev, struct scatterlist *sg, int nents,
776                        int direction)
777 {
778         int i;
779
780         dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
781         BUG_ON(direction == DMA_NONE);
782
783         for (i = 0; i < nents; i++ ) {
784                 BUG_ON(!sg[i].page);
785
786                 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
787         }
788
789         return nents;
790 }
791
792 static int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
793                          int direction)
794 {
795         dprintk(1,"%s\n",__FUNCTION__);
796         return 0;
797 }
798
799 static int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist, int nr_pages,
800                             int direction)
801 {
802 //      dprintk(1,"%s\n",__FUNCTION__);
803
804 //      flush_write_buffers();
805         return 0;
806 }
807
808 static struct videobuf_queue_ops vivi_video_qops = {
809         .buf_setup      = buffer_setup,
810         .buf_prepare    = buffer_prepare,
811         .buf_queue      = buffer_queue,
812         .buf_release    = buffer_release,
813
814         /* Non-pci handling routines */
815         .vb_map_sg      = vivi_map_sg,
816         .vb_dma_sync_sg = vivi_dma_sync_sg,
817         .vb_unmap_sg    = vivi_unmap_sg,
818 };
819
820 /* ------------------------------------------------------------------
821         IOCTL handling
822    ------------------------------------------------------------------*/
823
824
825 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
826 {
827         /* is it free? */
828         down(&dev->lock);
829         if (dev->resources) {
830                 /* no, someone else uses it */
831                 up(&dev->lock);
832                 return 0;
833         }
834         /* it's free, grab it */
835         dev->resources =1;
836         dprintk(1,"res: get\n");
837         up(&dev->lock);
838         return 1;
839 }
840
841 static int res_locked(struct vivi_dev *dev)
842 {
843         return (dev->resources);
844 }
845
846 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
847 {
848         down(&dev->lock);
849         dev->resources = 0;
850         dprintk(1,"res: put\n");
851         up(&dev->lock);
852 }
853
854 /* ------------------------------------------------------------------
855         IOCTL vidioc handling
856    ------------------------------------------------------------------*/
857 static int vidioc_querycap (struct file *file, void  *priv,
858                                         struct v4l2_capability *cap)
859 {
860         strcpy(cap->driver, "vivi");
861         strcpy(cap->card, "vivi");
862         cap->version = VIVI_VERSION;
863         cap->capabilities =     V4L2_CAP_VIDEO_CAPTURE |
864                                 V4L2_CAP_STREAMING     |
865                                 V4L2_CAP_READWRITE;
866         return 0;
867 }
868
869 static int vidioc_enum_fmt_cap (struct file *file, void  *priv,
870                                         struct v4l2_fmtdesc *f)
871 {
872         if (f->index > 0)
873                 return -EINVAL;
874
875         strlcpy(f->description,format.name,sizeof(f->description));
876         f->pixelformat = format.fourcc;
877         return 0;
878 }
879
880 static int vidioc_g_fmt_cap (struct file *file, void *priv,
881                                         struct v4l2_format *f)
882 {
883         struct vivi_fh  *fh=priv;
884
885         f->fmt.pix.width        = fh->width;
886         f->fmt.pix.height       = fh->height;
887         f->fmt.pix.field        = fh->vb_vidq.field;
888         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
889         f->fmt.pix.bytesperline =
890                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
891         f->fmt.pix.sizeimage =
892                 f->fmt.pix.height * f->fmt.pix.bytesperline;
893
894         return (0);
895 }
896
897 static int vidioc_try_fmt_cap (struct file *file, void *priv,
898                         struct v4l2_format *f)
899 {
900         struct vivi_fmt *fmt;
901         enum v4l2_field field;
902         unsigned int maxw, maxh;
903
904         if (format.fourcc != f->fmt.pix.pixelformat) {
905                 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
906                         "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
907                 return -EINVAL;
908         }
909         fmt=&format;
910
911         field = f->fmt.pix.field;
912
913         if (field == V4L2_FIELD_ANY) {
914 //              field=V4L2_FIELD_INTERLACED;
915                 field=V4L2_FIELD_SEQ_TB;
916         } else if (V4L2_FIELD_INTERLACED != field) {
917                 dprintk(1,"Field type invalid.\n");
918                 return -EINVAL;
919         }
920
921         maxw  = norm_maxw();
922         maxh  = norm_maxh();
923
924         f->fmt.pix.field = field;
925         if (f->fmt.pix.height < 32)
926                 f->fmt.pix.height = 32;
927         if (f->fmt.pix.height > maxh)
928                 f->fmt.pix.height = maxh;
929         if (f->fmt.pix.width < 48)
930                 f->fmt.pix.width = 48;
931         if (f->fmt.pix.width > maxw)
932                 f->fmt.pix.width = maxw;
933         f->fmt.pix.width &= ~0x03;
934         f->fmt.pix.bytesperline =
935                 (f->fmt.pix.width * fmt->depth) >> 3;
936         f->fmt.pix.sizeimage =
937                 f->fmt.pix.height * f->fmt.pix.bytesperline;
938
939         return 0;
940 }
941
942 /*FIXME: This seems to be generic enough to be at videodev2 */
943 static int vidioc_s_fmt_cap (struct file *file, void *priv,
944                                         struct v4l2_format *f)
945 {
946         struct vivi_fh  *fh=priv;
947         int ret = vidioc_try_fmt_cap(file,fh,f);
948         if (ret < 0)
949                 return (ret);
950
951         fh->fmt           = &format;
952         fh->width         = f->fmt.pix.width;
953         fh->height        = f->fmt.pix.height;
954         fh->vb_vidq.field = f->fmt.pix.field;
955         fh->type          = f->type;
956
957         return (0);
958 }
959
960 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
961 {
962         struct vivi_fh  *fh=priv;
963
964         return (videobuf_reqbufs(&fh->vb_vidq, p));
965 }
966
967 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
968 {
969         struct vivi_fh  *fh=priv;
970
971         return (videobuf_querybuf(&fh->vb_vidq, p));
972 }
973
974 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
975 {
976         struct vivi_fh  *fh=priv;
977
978         return (videobuf_qbuf(&fh->vb_vidq, p));
979 }
980
981 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
982 {
983         struct vivi_fh  *fh=priv;
984
985         return (videobuf_dqbuf(&fh->vb_vidq, p,
986                                 file->f_flags & O_NONBLOCK));
987 }
988
989 #ifdef CONFIG_VIDEO_V4L1_COMPAT
990 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
991 {
992         struct vivi_fh  *fh=priv;
993         struct videobuf_queue *q=&fh->vb_vidq;
994         struct v4l2_requestbuffers req;
995         unsigned int i, ret;
996
997         req.type   = q->type;
998         req.count  = 8;
999         req.memory = V4L2_MEMORY_MMAP;
1000         ret = videobuf_reqbufs(q,&req);
1001         if (ret < 0)
1002                 return (ret);
1003
1004         mbuf->frames = req.count;
1005         mbuf->size   = 0;
1006         for (i = 0; i < mbuf->frames; i++) {
1007                 mbuf->offsets[i]  = q->bufs[i]->boff;
1008                 mbuf->size       += q->bufs[i]->bsize;
1009         }
1010         return (0);
1011 }
1012 #endif
1013
1014 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1015 {
1016         struct vivi_fh  *fh=priv;
1017         struct vivi_dev *dev    = fh->dev;
1018
1019         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1020                 return -EINVAL;
1021         if (i != fh->type)
1022                 return -EINVAL;
1023
1024         if (!res_get(dev,fh))
1025                 return -EBUSY;
1026         return (videobuf_streamon(&fh->vb_vidq));
1027 }
1028
1029 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1030 {
1031         struct vivi_fh  *fh=priv;
1032         struct vivi_dev *dev    = fh->dev;
1033
1034         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1035                 return -EINVAL;
1036         if (i != fh->type)
1037                 return -EINVAL;
1038
1039         videobuf_streamoff(&fh->vb_vidq);
1040         res_free(dev,fh);
1041
1042         return (0);
1043 }
1044
1045 static struct v4l2_tvnorm tvnorms[] = {
1046         {
1047                 .name      = "NTSC-M",
1048                 .id        = V4L2_STD_NTSC_M,
1049         }
1050 };
1051
1052 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id a)
1053 {
1054
1055         return 0;
1056 }
1057
1058 /* only one input in this sample driver */
1059 static int vidioc_enum_input (struct file *file, void *priv,
1060                                 struct v4l2_input *inp)
1061 {
1062         if (inp->index != 0)
1063                 return -EINVAL;
1064
1065         inp->type = V4L2_INPUT_TYPE_CAMERA;
1066         inp->std = V4L2_STD_NTSC_M;
1067         strcpy(inp->name,"Camera");
1068
1069         return (0);
1070 }
1071
1072 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1073 {
1074         *i = 0;
1075
1076         return (0);
1077 }
1078 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1079 {
1080         if (i > 0)
1081                 return -EINVAL;
1082
1083         return (0);
1084 }
1085
1086         /* --- controls ---------------------------------------------- */
1087 static int vidioc_queryctrl (struct file *file, void *priv,
1088                                 struct v4l2_queryctrl *qc)
1089 {
1090         int i;
1091
1092         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1093                 if (qc->id && qc->id == vivi_qctrl[i].id) {
1094                         memcpy(qc, &(vivi_qctrl[i]),
1095                                 sizeof(*qc));
1096                         return (0);
1097                 }
1098
1099         return -EINVAL;
1100 }
1101
1102 static int vidioc_g_ctrl (struct file *file, void *priv,
1103                                 struct v4l2_control *ctrl)
1104 {
1105         int i;
1106
1107         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1108                 if (ctrl->id == vivi_qctrl[i].id) {
1109                         ctrl->value=qctl_regs[i];
1110                         return (0);
1111                 }
1112
1113         return -EINVAL;
1114 }
1115 static int vidioc_s_ctrl (struct file *file, void *priv,
1116                                 struct v4l2_control *ctrl)
1117 {
1118         int i;
1119
1120         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1121                 if (ctrl->id == vivi_qctrl[i].id) {
1122                         if (ctrl->value <
1123                                 vivi_qctrl[i].minimum
1124                                 || ctrl->value >
1125                                 vivi_qctrl[i].maximum) {
1126                                         return (-ERANGE);
1127                                 }
1128                         qctl_regs[i]=ctrl->value;
1129                         return (0);
1130                 }
1131         return -EINVAL;
1132 }
1133
1134 /* ------------------------------------------------------------------
1135         File operations for the device
1136    ------------------------------------------------------------------*/
1137
1138 #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
1139
1140 static int vivi_open(struct inode *inode, struct file *file)
1141 {
1142         int minor = iminor(inode);
1143         struct vivi_dev *h,*dev = NULL;
1144         struct vivi_fh *fh;
1145         struct list_head *list;
1146         enum v4l2_buf_type type = 0;
1147         int i;
1148
1149         printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
1150
1151         list_for_each(list,&vivi_devlist) {
1152                 h = list_entry(list, struct vivi_dev, vivi_devlist);
1153                 if (h->vfd.minor == minor) {
1154                         dev  = h;
1155                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1156                 }
1157         }
1158         if (NULL == dev)
1159                 return -ENODEV;
1160
1161
1162
1163         /* If more than one user, mutex should be added */
1164         dev->users++;
1165
1166         dprintk(1,"open minor=%d type=%s users=%d\n",
1167                                 minor,v4l2_type_names[type],dev->users);
1168
1169         /* allocate + initialize per filehandle data */
1170         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1171         if (NULL == fh) {
1172                 dev->users--;
1173                 return -ENOMEM;
1174         }
1175
1176         file->private_data = fh;
1177         fh->dev      = dev;
1178
1179         fh->type     = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1180         fh->fmt      = &format;
1181         fh->width    = 640;
1182         fh->height   = 480;
1183
1184         /* Put all controls at a sane state */
1185         for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1186                 qctl_regs[i] =vivi_qctrl[i].default_value;
1187
1188         dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1189                 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1190         dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1191         dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1192
1193         /* Resets frame counters */
1194         dev->h=0;
1195         dev->m=0;
1196         dev->s=0;
1197         dev->us=0;
1198         dev->jiffies=jiffies;
1199         sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1200                         dev->h,dev->m,dev->s,(dev->us+500)/1000);
1201
1202         videobuf_queue_init(&fh->vb_vidq, &vivi_video_qops,
1203                         NULL, NULL,
1204                         fh->type,
1205                         V4L2_FIELD_INTERLACED,
1206                         sizeof(struct vivi_buffer),fh);
1207
1208         return 0;
1209 }
1210
1211 static ssize_t
1212 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1213 {
1214         struct vivi_fh        *fh = file->private_data;
1215
1216         if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1217                 if (res_locked(fh->dev))
1218                         return -EBUSY;
1219                 return videobuf_read_one(&fh->vb_vidq, data, count, ppos,
1220                                         file->f_flags & O_NONBLOCK);
1221         }
1222         return 0;
1223 }
1224
1225 static unsigned int
1226 vivi_poll(struct file *file, struct poll_table_struct *wait)
1227 {
1228         struct vivi_fh        *fh = file->private_data;
1229         struct vivi_buffer    *buf;
1230
1231         dprintk(1,"%s\n",__FUNCTION__);
1232
1233         if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1234                 return POLLERR;
1235
1236         if (res_get(fh->dev,fh)) {
1237                 dprintk(1,"poll: mmap interface\n");
1238                 /* streaming capture */
1239                 if (list_empty(&fh->vb_vidq.stream))
1240                         return POLLERR;
1241                 buf = list_entry(fh->vb_vidq.stream.next,struct vivi_buffer,vb.stream);
1242         } else {
1243                 dprintk(1,"poll: read() interface\n");
1244                 /* read() capture */
1245                 buf = (struct vivi_buffer*)fh->vb_vidq.read_buf;
1246                 if (NULL == buf)
1247                         return POLLERR;
1248         }
1249         poll_wait(file, &buf->vb.done, wait);
1250         if (buf->vb.state == STATE_DONE ||
1251             buf->vb.state == STATE_ERROR)
1252                 return POLLIN|POLLRDNORM;
1253         return 0;
1254 }
1255
1256 static int vivi_release(struct inode *inode, struct file *file)
1257 {
1258         struct vivi_fh         *fh = file->private_data;
1259         struct vivi_dev *dev       = fh->dev;
1260         struct vivi_dmaqueue *vidq = &dev->vidq;
1261
1262         int minor = iminor(inode);
1263
1264         vivi_stop_thread(vidq);
1265         videobuf_mmap_free(&fh->vb_vidq);
1266
1267         kfree (fh);
1268
1269         dev->users--;
1270
1271         printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1272
1273         return 0;
1274 }
1275
1276 static int
1277 vivi_mmap(struct file *file, struct vm_area_struct * vma)
1278 {
1279         struct vivi_fh        *fh = file->private_data;
1280         int ret;
1281
1282         dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1283
1284         ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1285
1286         dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1287                 (unsigned long)vma->vm_start,
1288                 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1289                 ret);
1290
1291         return ret;
1292 }
1293
1294 static struct file_operations vivi_fops = {
1295         .owner          = THIS_MODULE,
1296         .open           = vivi_open,
1297         .release        = vivi_release,
1298         .read           = vivi_read,
1299         .poll           = vivi_poll,
1300         .ioctl          = video_ioctl2, /* V4L2 ioctl handler */
1301         .mmap           = vivi_mmap,
1302         .llseek         = no_llseek,
1303 };
1304
1305 static struct video_device vivi = {
1306         .name           = "vivi",
1307         .type           = VID_TYPE_CAPTURE,
1308         .hardware       = 0,
1309         .fops           = &vivi_fops,
1310         .minor          = -1,
1311 //      .release        = video_device_release,
1312
1313         .vidioc_querycap      = vidioc_querycap,
1314         .vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
1315         .vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
1316         .vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
1317         .vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
1318         .vidioc_reqbufs       = vidioc_reqbufs,
1319         .vidioc_querybuf      = vidioc_querybuf,
1320         .vidioc_qbuf          = vidioc_qbuf,
1321         .vidioc_dqbuf         = vidioc_dqbuf,
1322         .vidioc_s_std         = vidioc_s_std,
1323         .vidioc_enum_input    = vidioc_enum_input,
1324         .vidioc_g_input       = vidioc_g_input,
1325         .vidioc_s_input       = vidioc_s_input,
1326         .vidioc_queryctrl     = vidioc_queryctrl,
1327         .vidioc_g_ctrl        = vidioc_g_ctrl,
1328         .vidioc_s_ctrl        = vidioc_s_ctrl,
1329         .vidioc_streamon      = vidioc_streamon,
1330         .vidioc_streamoff     = vidioc_streamoff,
1331 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1332         .vidiocgmbuf          = vidiocgmbuf,
1333 #endif
1334         .tvnorms              = tvnorms,
1335         .tvnormsize           = ARRAY_SIZE(tvnorms),
1336 };
1337 /* -----------------------------------------------------------------
1338         Initialization and module stuff
1339    ------------------------------------------------------------------*/
1340
1341 static int __init vivi_init(void)
1342 {
1343         int ret;
1344         struct vivi_dev *dev;
1345
1346         dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1347         if (NULL == dev)
1348                 return -ENOMEM;
1349         list_add_tail(&dev->vivi_devlist,&vivi_devlist);
1350
1351         /* init video dma queues */
1352         INIT_LIST_HEAD(&dev->vidq.active);
1353         INIT_LIST_HEAD(&dev->vidq.queued);
1354
1355         /* initialize locks */
1356         init_MUTEX(&dev->lock);
1357
1358         dev->vidq.timeout.function = vivi_vid_timeout;
1359         dev->vidq.timeout.data     = (unsigned long)dev;
1360         init_timer(&dev->vidq.timeout);
1361
1362         ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1363         printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1364         return ret;
1365 }
1366
1367 static void __exit vivi_exit(void)
1368 {
1369         struct vivi_dev *h;
1370         struct list_head *list;
1371
1372         list_for_each(list,&vivi_devlist) {
1373                 h = list_entry(list, struct vivi_dev, vivi_devlist);
1374                 kfree (h);
1375         }
1376         video_unregister_device(&vivi);
1377 }
1378
1379 module_init(vivi_init);
1380 module_exit(vivi_exit);
1381
1382 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1383 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1384 MODULE_LICENSE("Dual BSD/GPL");
1385
1386 module_param(video_nr, int, 0);
1387
1388 module_param_named(debug,vivi.debug, int, 0644);
1389 MODULE_PARM_DESC(debug,"activates debug info");
1390
1391 module_param(vid_limit,int,0644);
1392 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
1393