V4L/DVB (5073): Fix OOPS on some waitqueue conditions
[linux-2.6.git] / drivers / media / video / vivi.c
1 /*
2  * Virtual Video driver - This code emulates a real video device with v4l2 api
3  *
4  * Copyright (c) 2006 by:
5  *      Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
6  *      Ted Walther <ted--a.t--enumera.com>
7  *      John Sokol <sokol--a.t--videotechnology.com>
8  *      http://v4l.videotechnology.com/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the BSD Licence, GNU General Public License
12  * as published by the Free Software Foundation; either version 2 of the
13  * License, or (at your option) any later version
14  */
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/mm.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/pci.h>
26 #include <linux/random.h>
27 #include <linux/version.h>
28 #include <linux/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         unsigned long flags;
274         spinlock_t spinlock;
275
276         spin_lock_init(&spinlock);
277
278         /* Get first addr pointed to pixel position */
279         oldpg=get_addr_pos(pos,pages,to_addr);
280         pg=pfn_to_page(sg_dma_address(to_addr[oldpg].sg) >> PAGE_SHIFT);
281         spin_lock_irqsave(&spinlock,flags);
282         basep = kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[oldpg].sg->offset;
283
284         /* We will just duplicate the second pixel at the packet */
285         wmax/=2;
286
287         /* Generate a standard color bar pattern */
288         for (w=0;w<wmax;w++) {
289                 r=bars[w*7/wmax][0];
290                 g=bars[w*7/wmax][1];
291                 b=bars[w*7/wmax][2];
292
293                 for (color=0;color<4;color++) {
294                         pgpos=get_addr_pos(pos,pages,to_addr);
295                         if (pgpos!=oldpg) {
296                                 pg=pfn_to_page(sg_dma_address(to_addr[pgpos].sg) >> PAGE_SHIFT);
297                                 kunmap_atomic(basep, KM_BOUNCE_READ);
298                                 basep= kmap_atomic(pg, KM_BOUNCE_READ)+to_addr[pgpos].sg->offset;
299                                 oldpg=pgpos;
300                         }
301                         p=basep+pos-to_addr[pgpos].pos;
302
303                         switch (color) {
304                                 case 0:
305                                 case 2:
306                                         *p=TO_Y(r,g,b);         /* Luminance */
307                                         break;
308                                 case 1:
309                                         *p=TO_U(r,g,b);         /* Cb */
310                                         break;
311                                 case 3:
312                                         *p=TO_V(r,g,b);         /* Cr */
313                                         break;
314                         }
315                         pos++;
316                 }
317         }
318
319         /* Checks if it is possible to show timestamp */
320         if (TSTAMP_MAX_Y>=hmax)
321                 goto end;
322         if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
323                 goto end;
324
325         /* Print stream time */
326         if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
327                 j=TSTAMP_MIN_X;
328                 for (s=timestr;*s;s++) {
329                         chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
330                         for (i=0;i<7;i++) {
331                                 if (chr&1<<(7-i)) { /* Font color*/
332                                         r=bars[BLUE][0];
333                                         g=bars[BLUE][1];
334                                         b=bars[BLUE][2];
335                                         r=g=b=0;
336                                         g=198;
337                                 } else { /* Background color */
338                                         r=bars[WHITE][0];
339                                         g=bars[WHITE][1];
340                                         b=bars[WHITE][2];
341                                         r=g=b=0;
342                                 }
343
344                                 pos=inipos+j*2;
345                                 for (color=0;color<4;color++) {
346                                         pgpos=get_addr_pos(pos,pages,to_addr);
347                                         if (pgpos!=oldpg) {
348                                                 pg=pfn_to_page(sg_dma_address(
349                                                                 to_addr[pgpos].sg)
350                                                                 >> PAGE_SHIFT);
351                                                 kunmap_atomic(basep,
352                                                                 KM_BOUNCE_READ);
353                                                 basep= kmap_atomic(pg,
354                                                         KM_BOUNCE_READ)+
355                                                         to_addr[pgpos].sg->offset;
356                                                 oldpg=pgpos;
357                                         }
358                                         p=basep+pos-to_addr[pgpos].pos;
359
360                                         y=TO_Y(r,g,b);
361
362                                         switch (color) {
363                                                 case 0:
364                                                 case 2:
365                                                         *p=TO_Y(r,g,b);         /* Luminance */
366                                                         break;
367                                                 case 1:
368                                                         *p=TO_U(r,g,b);         /* Cb */
369                                                         break;
370                                                 case 3:
371                                                         *p=TO_V(r,g,b);         /* Cr */
372                                                         break;
373                                         }
374                                         pos++;
375                                 }
376                                 j++;
377                         }
378                 }
379         }
380
381
382 end:
383         kunmap_atomic(basep, KM_BOUNCE_READ);
384         spin_unlock_irqrestore(&spinlock,flags);
385
386 }
387 static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
388 {
389         int h,pos=0;
390         int hmax  = buf->vb.height;
391         int wmax  = buf->vb.width;
392         struct videobuf_buffer *vb=&buf->vb;
393         struct sg_to_addr *to_addr=buf->to_addr;
394         struct timeval ts;
395
396         /* Test if DMA mapping is ready */
397         if (!sg_dma_address(&vb->dma.sglist[0]))
398                 return;
399
400         prep_to_addr(to_addr,vb);
401
402         /* Check if there is enough memory */
403         BUG_ON(buf->vb.dma.nr_pages << PAGE_SHIFT < (buf->vb.width*buf->vb.height)*2);
404
405         for (h=0;h<hmax;h++) {
406                 gen_line(to_addr,pos,vb->dma.nr_pages,wmax,hmax,h,dev->timestr);
407                 pos += wmax*2;
408         }
409
410         /* Updates stream time */
411
412         dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
413         dev->jiffies=jiffies;
414         if (dev->us>=1000000) {
415                 dev->us-=1000000;
416                 dev->s++;
417                 if (dev->s>=60) {
418                         dev->s-=60;
419                         dev->m++;
420                         if (dev->m>60) {
421                                 dev->m-=60;
422                                 dev->h++;
423                                 if (dev->h>24)
424                                         dev->h-=24;
425                         }
426                 }
427         }
428         sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
429                         dev->h,dev->m,dev->s,(dev->us+500)/1000);
430
431         dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
432                         (unsigned long)buf->vb.dma.vmalloc,pos);
433
434         /* Advice that buffer was filled */
435         buf->vb.state = STATE_DONE;
436         buf->vb.field_count++;
437         do_gettimeofday(&ts);
438         buf->vb.ts = ts;
439
440         list_del(&buf->vb.queue);
441         wake_up(&buf->vb.done);
442 }
443
444 static int restart_video_queue(struct vivi_dmaqueue *dma_q);
445
446 static void vivi_thread_tick(struct vivi_dmaqueue  *dma_q)
447 {
448         struct vivi_buffer    *buf;
449         struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
450
451         int bc;
452
453         /* Announces videobuf that all went ok */
454         for (bc = 0;; bc++) {
455                 if (list_empty(&dma_q->active)) {
456                         dprintk(1,"No active queue to serve\n");
457                         break;
458                 }
459
460                 buf = list_entry(dma_q->active.next,
461                                  struct vivi_buffer, vb.queue);
462
463                 /* Nobody is waiting something to be done, just return */
464                 if (!waitqueue_active(&buf->vb.done)) {
465                         mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
466                         return;
467                 }
468
469                 do_gettimeofday(&buf->vb.ts);
470                 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
471
472                 /* Fill buffer */
473                 vivi_fillbuff(dev,buf);
474         }
475         if (list_empty(&dma_q->active)) {
476                 del_timer(&dma_q->timeout);
477         } else {
478                 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
479         }
480         if (bc != 1)
481                 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
482 }
483
484 static void vivi_sleep(struct vivi_dmaqueue  *dma_q)
485 {
486         int timeout;
487         DECLARE_WAITQUEUE(wait, current);
488
489         dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
490
491         add_wait_queue(&dma_q->wq, &wait);
492         if (!kthread_should_stop()) {
493                 dma_q->frame++;
494
495                 /* Calculate time to wake up */
496                 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
497
498                 if (timeout <= 0) {
499                         int old=dma_q->frame;
500                         dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
501
502                         timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
503
504                         dprintk(1,"underrun, losed %d frames. "
505                                   "Now, frame is %d. Waking on %d jiffies\n",
506                                         dma_q->frame-old,dma_q->frame,timeout);
507                 } else
508                         dprintk(1,"will sleep for %i jiffies\n",timeout);
509
510                 vivi_thread_tick(dma_q);
511
512                 schedule_timeout_interruptible (timeout);
513         }
514
515         remove_wait_queue(&dma_q->wq, &wait);
516         try_to_freeze();
517 }
518
519 static int vivi_thread(void *data)
520 {
521         struct vivi_dmaqueue  *dma_q=data;
522
523         dprintk(1,"thread started\n");
524
525         for (;;) {
526                 vivi_sleep(dma_q);
527
528                 if (kthread_should_stop())
529                         break;
530         }
531         dprintk(1, "thread: exit\n");
532         return 0;
533 }
534
535 static int vivi_start_thread(struct vivi_dmaqueue  *dma_q)
536 {
537         dma_q->frame=0;
538         dma_q->ini_jiffies=jiffies;
539
540         dprintk(1,"%s\n",__FUNCTION__);
541
542         dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
543
544         if (IS_ERR(dma_q->kthread)) {
545                 printk(KERN_ERR "vivi: kernel_thread() failed\n");
546                 return PTR_ERR(dma_q->kthread);
547         }
548         dprintk(1,"returning from %s\n",__FUNCTION__);
549         return 0;
550 }
551
552 static void vivi_stop_thread(struct vivi_dmaqueue  *dma_q)
553 {
554         dprintk(1,"%s\n",__FUNCTION__);
555         /* shutdown control thread */
556         if (dma_q->kthread) {
557                 kthread_stop(dma_q->kthread);
558                 dma_q->kthread=NULL;
559         }
560 }
561
562 static int restart_video_queue(struct vivi_dmaqueue *dma_q)
563 {
564         struct vivi_buffer *buf, *prev;
565         struct list_head *item;
566
567         dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
568
569         if (!list_empty(&dma_q->active)) {
570                 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
571                 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
572                         buf, buf->vb.i);
573
574                 dprintk(1,"Restarting video dma\n");
575                 vivi_stop_thread(dma_q);
576 //              vivi_start_thread(dma_q);
577
578                 /* cancel all outstanding capture / vbi requests */
579                 list_for_each(item,&dma_q->active) {
580                         buf = list_entry(item, struct vivi_buffer, vb.queue);
581
582                         list_del(&buf->vb.queue);
583                         buf->vb.state = STATE_ERROR;
584                         wake_up(&buf->vb.done);
585                 }
586                 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
587
588                 return 0;
589         }
590
591         prev = NULL;
592         for (;;) {
593                 if (list_empty(&dma_q->queued))
594                         return 0;
595                 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
596                 if (NULL == prev) {
597                         list_del(&buf->vb.queue);
598                         list_add_tail(&buf->vb.queue,&dma_q->active);
599
600                         dprintk(1,"Restarting video dma\n");
601                         vivi_stop_thread(dma_q);
602                         vivi_start_thread(dma_q);
603
604                         buf->vb.state = STATE_ACTIVE;
605                         mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
606                         dprintk(2,"[%p/%d] restart_queue - first active\n",
607                                 buf,buf->vb.i);
608
609                 } else if (prev->vb.width  == buf->vb.width  &&
610                            prev->vb.height == buf->vb.height &&
611                            prev->fmt       == buf->fmt) {
612                         list_del(&buf->vb.queue);
613                         list_add_tail(&buf->vb.queue,&dma_q->active);
614                         buf->vb.state = STATE_ACTIVE;
615                         dprintk(2,"[%p/%d] restart_queue - move to active\n",
616                                 buf,buf->vb.i);
617                 } else {
618                         return 0;
619                 }
620                 prev = buf;
621         }
622 }
623
624 static void vivi_vid_timeout(unsigned long data)
625 {
626         struct vivi_dev      *dev  = (struct vivi_dev*)data;
627         struct vivi_dmaqueue *vidq = &dev->vidq;
628         struct vivi_buffer   *buf;
629
630         while (!list_empty(&vidq->active)) {
631                 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
632                 list_del(&buf->vb.queue);
633                 buf->vb.state = STATE_ERROR;
634                 wake_up(&buf->vb.done);
635                 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
636         }
637
638         restart_video_queue(vidq);
639 }
640
641 /* ------------------------------------------------------------------
642         Videobuf operations
643    ------------------------------------------------------------------*/
644 static int
645 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
646 {
647         struct vivi_fh *fh = vq->priv_data;
648
649         *size = fh->width*fh->height*2;
650
651         if (0 == *count)
652                 *count = 32;
653         while (*size * *count > vid_limit * 1024 * 1024)
654                 (*count)--;
655         return 0;
656 }
657
658 static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
659 {
660         dprintk(1,"%s\n",__FUNCTION__);
661
662         if (in_interrupt())
663                 BUG();
664
665         /*FIXME: Maybe a spinlock is required here */
666         kfree(buf->to_addr);
667         buf->to_addr=NULL;
668
669         videobuf_waiton(&buf->vb,0,0);
670         videobuf_dma_unmap(vq, &buf->vb.dma);
671         videobuf_dma_free(&buf->vb.dma);
672         buf->vb.state = STATE_NEEDS_INIT;
673 }
674
675 #define norm_maxw() 1024
676 #define norm_maxh() 768
677 static int
678 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
679                                                 enum v4l2_field field)
680 {
681         struct vivi_fh     *fh  = vq->priv_data;
682         struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
683         int rc, init_buffer = 0;
684
685 //      dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
686
687         BUG_ON(NULL == fh->fmt);
688         if (fh->width  < 48 || fh->width  > norm_maxw() ||
689             fh->height < 32 || fh->height > norm_maxh())
690                 return -EINVAL;
691         buf->vb.size = fh->width*fh->height*2;
692         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
693                 return -EINVAL;
694
695         if (buf->fmt       != fh->fmt    ||
696             buf->vb.width  != fh->width  ||
697             buf->vb.height != fh->height ||
698         buf->vb.field  != field) {
699                 buf->fmt       = fh->fmt;
700                 buf->vb.width  = fh->width;
701                 buf->vb.height = fh->height;
702                 buf->vb.field  = field;
703                 init_buffer = 1;
704         }
705
706         if (STATE_NEEDS_INIT == buf->vb.state) {
707                 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
708                         goto fail;
709         }
710
711         buf->vb.state = STATE_PREPARED;
712
713         if (NULL == (buf->to_addr = kmalloc(sizeof(*buf->to_addr) * vb->dma.nr_pages,GFP_KERNEL))) {
714                 rc=-ENOMEM;
715                 goto fail;
716         }
717
718         return 0;
719
720 fail:
721         free_buffer(vq,buf);
722         return rc;
723 }
724
725 static void
726 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
727 {
728         struct vivi_buffer    *buf     = container_of(vb,struct vivi_buffer,vb);
729         struct vivi_fh        *fh      = vq->priv_data;
730         struct vivi_dev       *dev     = fh->dev;
731         struct vivi_dmaqueue  *vidq    = &dev->vidq;
732         struct vivi_buffer    *prev;
733
734         if (!list_empty(&vidq->queued)) {
735                 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
736                 list_add_tail(&buf->vb.queue,&vidq->queued);
737                 buf->vb.state = STATE_QUEUED;
738                 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
739                         buf, buf->vb.i);
740         } else if (list_empty(&vidq->active)) {
741                 list_add_tail(&buf->vb.queue,&vidq->active);
742
743                 buf->vb.state = STATE_ACTIVE;
744                 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
745                 dprintk(2,"[%p/%d] buffer_queue - first active\n",
746                         buf, buf->vb.i);
747
748                 vivi_start_thread(vidq);
749         } else {
750                 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
751                 if (prev->vb.width  == buf->vb.width  &&
752                     prev->vb.height == buf->vb.height &&
753                     prev->fmt       == buf->fmt) {
754                         list_add_tail(&buf->vb.queue,&vidq->active);
755                         buf->vb.state = STATE_ACTIVE;
756                         dprintk(2,"[%p/%d] buffer_queue - append to active\n",
757                                 buf, buf->vb.i);
758
759                 } else {
760                         list_add_tail(&buf->vb.queue,&vidq->queued);
761                         buf->vb.state = STATE_QUEUED;
762                         dprintk(2,"[%p/%d] buffer_queue - first queued\n",
763                                 buf, buf->vb.i);
764                 }
765         }
766 }
767
768 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
769 {
770         struct vivi_buffer   *buf  = container_of(vb,struct vivi_buffer,vb);
771         struct vivi_fh       *fh   = vq->priv_data;
772         struct vivi_dev      *dev  = (struct vivi_dev*)fh->dev;
773         struct vivi_dmaqueue *vidq = &dev->vidq;
774
775         dprintk(1,"%s\n",__FUNCTION__);
776
777         vivi_stop_thread(vidq);
778
779         free_buffer(vq,buf);
780 }
781
782 static int vivi_map_sg(void *dev, struct scatterlist *sg, int nents,
783                        int direction)
784 {
785         int i;
786
787         dprintk(1,"%s, number of pages=%d\n",__FUNCTION__,nents);
788         BUG_ON(direction == DMA_NONE);
789
790         for (i = 0; i < nents; i++ ) {
791                 BUG_ON(!sg[i].page);
792
793                 sg_dma_address(&sg[i]) = page_to_phys(sg[i].page) + sg[i].offset;
794         }
795
796         return nents;
797 }
798
799 static int vivi_unmap_sg(void *dev,struct scatterlist *sglist,int nr_pages,
800                          int direction)
801 {
802         dprintk(1,"%s\n",__FUNCTION__);
803         return 0;
804 }
805
806 static int vivi_dma_sync_sg(void *dev,struct scatterlist *sglist, int nr_pages,
807                             int direction)
808 {
809 //      dprintk(1,"%s\n",__FUNCTION__);
810
811 //      flush_write_buffers();
812         return 0;
813 }
814
815 static struct videobuf_queue_ops vivi_video_qops = {
816         .buf_setup      = buffer_setup,
817         .buf_prepare    = buffer_prepare,
818         .buf_queue      = buffer_queue,
819         .buf_release    = buffer_release,
820
821         /* Non-pci handling routines */
822         .vb_map_sg      = vivi_map_sg,
823         .vb_dma_sync_sg = vivi_dma_sync_sg,
824         .vb_unmap_sg    = vivi_unmap_sg,
825 };
826
827 /* ------------------------------------------------------------------
828         IOCTL handling
829    ------------------------------------------------------------------*/
830
831
832 static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
833 {
834         /* is it free? */
835         down(&dev->lock);
836         if (dev->resources) {
837                 /* no, someone else uses it */
838                 up(&dev->lock);
839                 return 0;
840         }
841         /* it's free, grab it */
842         dev->resources =1;
843         dprintk(1,"res: get\n");
844         up(&dev->lock);
845         return 1;
846 }
847
848 static int res_locked(struct vivi_dev *dev)
849 {
850         return (dev->resources);
851 }
852
853 static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
854 {
855         down(&dev->lock);
856         dev->resources = 0;
857         dprintk(1,"res: put\n");
858         up(&dev->lock);
859 }
860
861 /* ------------------------------------------------------------------
862         IOCTL vidioc handling
863    ------------------------------------------------------------------*/
864 static int vidioc_querycap (struct file *file, void  *priv,
865                                         struct v4l2_capability *cap)
866 {
867         strcpy(cap->driver, "vivi");
868         strcpy(cap->card, "vivi");
869         cap->version = VIVI_VERSION;
870         cap->capabilities =     V4L2_CAP_VIDEO_CAPTURE |
871                                 V4L2_CAP_STREAMING     |
872                                 V4L2_CAP_READWRITE;
873         return 0;
874 }
875
876 static int vidioc_enum_fmt_cap (struct file *file, void  *priv,
877                                         struct v4l2_fmtdesc *f)
878 {
879         if (f->index > 0)
880                 return -EINVAL;
881
882         strlcpy(f->description,format.name,sizeof(f->description));
883         f->pixelformat = format.fourcc;
884         return 0;
885 }
886
887 static int vidioc_g_fmt_cap (struct file *file, void *priv,
888                                         struct v4l2_format *f)
889 {
890         struct vivi_fh  *fh=priv;
891
892         f->fmt.pix.width        = fh->width;
893         f->fmt.pix.height       = fh->height;
894         f->fmt.pix.field        = fh->vb_vidq.field;
895         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
896         f->fmt.pix.bytesperline =
897                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
898         f->fmt.pix.sizeimage =
899                 f->fmt.pix.height * f->fmt.pix.bytesperline;
900
901         return (0);
902 }
903
904 static int vidioc_try_fmt_cap (struct file *file, void *priv,
905                         struct v4l2_format *f)
906 {
907         struct vivi_fmt *fmt;
908         enum v4l2_field field;
909         unsigned int maxw, maxh;
910
911         if (format.fourcc != f->fmt.pix.pixelformat) {
912                 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
913                         "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
914                 return -EINVAL;
915         }
916         fmt=&format;
917
918         field = f->fmt.pix.field;
919
920         if (field == V4L2_FIELD_ANY) {
921 //              field=V4L2_FIELD_INTERLACED;
922                 field=V4L2_FIELD_SEQ_TB;
923         } else if (V4L2_FIELD_INTERLACED != field) {
924                 dprintk(1,"Field type invalid.\n");
925                 return -EINVAL;
926         }
927
928         maxw  = norm_maxw();
929         maxh  = norm_maxh();
930
931         f->fmt.pix.field = field;
932         if (f->fmt.pix.height < 32)
933                 f->fmt.pix.height = 32;
934         if (f->fmt.pix.height > maxh)
935                 f->fmt.pix.height = maxh;
936         if (f->fmt.pix.width < 48)
937                 f->fmt.pix.width = 48;
938         if (f->fmt.pix.width > maxw)
939                 f->fmt.pix.width = maxw;
940         f->fmt.pix.width &= ~0x03;
941         f->fmt.pix.bytesperline =
942                 (f->fmt.pix.width * fmt->depth) >> 3;
943         f->fmt.pix.sizeimage =
944                 f->fmt.pix.height * f->fmt.pix.bytesperline;
945
946         return 0;
947 }
948
949 /*FIXME: This seems to be generic enough to be at videodev2 */
950 static int vidioc_s_fmt_cap (struct file *file, void *priv,
951                                         struct v4l2_format *f)
952 {
953         struct vivi_fh  *fh=priv;
954         int ret = vidioc_try_fmt_cap(file,fh,f);
955         if (ret < 0)
956                 return (ret);
957
958         fh->fmt           = &format;
959         fh->width         = f->fmt.pix.width;
960         fh->height        = f->fmt.pix.height;
961         fh->vb_vidq.field = f->fmt.pix.field;
962         fh->type          = f->type;
963
964         return (0);
965 }
966
967 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
968 {
969         struct vivi_fh  *fh=priv;
970
971         return (videobuf_reqbufs(&fh->vb_vidq, p));
972 }
973
974 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
975 {
976         struct vivi_fh  *fh=priv;
977
978         return (videobuf_querybuf(&fh->vb_vidq, p));
979 }
980
981 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
982 {
983         struct vivi_fh  *fh=priv;
984
985         return (videobuf_qbuf(&fh->vb_vidq, p));
986 }
987
988 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
989 {
990         struct vivi_fh  *fh=priv;
991
992         return (videobuf_dqbuf(&fh->vb_vidq, p,
993                                 file->f_flags & O_NONBLOCK));
994 }
995
996 #ifdef CONFIG_VIDEO_V4L1_COMPAT
997 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
998 {
999         struct vivi_fh  *fh=priv;
1000         struct videobuf_queue *q=&fh->vb_vidq;
1001         struct v4l2_requestbuffers req;
1002         unsigned int i;
1003         int ret;
1004
1005         req.type   = q->type;
1006         req.count  = 8;
1007         req.memory = V4L2_MEMORY_MMAP;
1008         ret = videobuf_reqbufs(q,&req);
1009         if (ret < 0)
1010                 return (ret);
1011
1012         mbuf->frames = req.count;
1013         mbuf->size   = 0;
1014         for (i = 0; i < mbuf->frames; i++) {
1015                 mbuf->offsets[i]  = q->bufs[i]->boff;
1016                 mbuf->size       += q->bufs[i]->bsize;
1017         }
1018         return (0);
1019 }
1020 #endif
1021
1022 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1023 {
1024         struct vivi_fh  *fh=priv;
1025         struct vivi_dev *dev    = fh->dev;
1026
1027         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1028                 return -EINVAL;
1029         if (i != fh->type)
1030                 return -EINVAL;
1031
1032         if (!res_get(dev,fh))
1033                 return -EBUSY;
1034         return (videobuf_streamon(&fh->vb_vidq));
1035 }
1036
1037 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1038 {
1039         struct vivi_fh  *fh=priv;
1040         struct vivi_dev *dev    = fh->dev;
1041
1042         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1043                 return -EINVAL;
1044         if (i != fh->type)
1045                 return -EINVAL;
1046
1047         videobuf_streamoff(&fh->vb_vidq);
1048         res_free(dev,fh);
1049
1050         return (0);
1051 }
1052
1053 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
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 const 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              = V4L2_STD_NTSC_M,
1335         .current_norm         = V4L2_STD_NTSC_M,
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         init_waitqueue_head(&dev->vidq.wq);
1355
1356         /* initialize locks */
1357         init_MUTEX(&dev->lock);
1358
1359         dev->vidq.timeout.function = vivi_vid_timeout;
1360         dev->vidq.timeout.data     = (unsigned long)dev;
1361         init_timer(&dev->vidq.timeout);
1362
1363         ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
1364         printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
1365         return ret;
1366 }
1367
1368 static void __exit vivi_exit(void)
1369 {
1370         struct vivi_dev *h;
1371         struct list_head *list;
1372
1373         while (!list_empty(&vivi_devlist)) {
1374                 list = vivi_devlist.next;
1375                 list_del(list);
1376                 h = list_entry(list, struct vivi_dev, vivi_devlist);
1377                 kfree (h);
1378         }
1379         video_unregister_device(&vivi);
1380 }
1381
1382 module_init(vivi_init);
1383 module_exit(vivi_exit);
1384
1385 MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1386 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1387 MODULE_LICENSE("Dual BSD/GPL");
1388
1389 module_param(video_nr, int, 0);
1390
1391 module_param_named(debug,vivi.debug, int, 0644);
1392 MODULE_PARM_DESC(debug,"activates debug info");
1393
1394 module_param(vid_limit,int,0644);
1395 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
1396