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