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