blob: ea9ff8a9bfdfec50da3de27eccbca2fcca6ecaf8 [file] [log] [blame]
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001/*
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>
Matthias Kaehlcke51b54022007-07-02 10:19:38 -030028#include <linux/mutex.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030029#include <linux/videodev2.h>
Andrew Morton10951362006-04-27 10:10:58 -030030#include <linux/dma-mapping.h>
Mauro Carvalho Chehabcd41e282006-04-09 15:43:41 -030031#ifdef CONFIG_VIDEO_V4L1_COMPAT
32/* Include V4L1 specific functions. Should be removed soon */
33#include <linux/videodev.h>
34#endif
Michael Krufkyf13df912006-03-23 22:01:44 -030035#include <linux/interrupt.h>
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -030036#include <media/videobuf-vmalloc.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030037#include <media/v4l2-common.h>
38#include <linux/kthread.h>
39#include <linux/highmem.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040#include <linux/freezer.h>
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030041
42/* Wake up at about 30 fps */
43#define WAKE_NUMERATOR 30
44#define WAKE_DENOMINATOR 1001
45#define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
46
47/* These timers are for 1 fps - used only for testing */
48//#define WAKE_DENOMINATOR 30 /* hack for testing purposes */
49//#define BUFFER_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */
50
51#include "font.h"
52
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030053#define VIVI_MAJOR_VERSION 0
54#define VIVI_MINOR_VERSION 4
55#define VIVI_RELEASE 0
56#define VIVI_VERSION KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
57
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -030058/* Declare static vars that will be used as parameters */
59static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
60static struct video_device vivi; /* Video device */
61static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -030062static int n_devs = 1; /* Number of virtual devices */
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -030063
64/* supported controls */
65static struct v4l2_queryctrl vivi_qctrl[] = {
66 {
67 .id = V4L2_CID_AUDIO_VOLUME,
68 .name = "Volume",
69 .minimum = 0,
70 .maximum = 65535,
71 .step = 65535/100,
72 .default_value = 65535,
73 .flags = 0,
74 .type = V4L2_CTRL_TYPE_INTEGER,
75 },{
76 .id = V4L2_CID_BRIGHTNESS,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Brightness",
79 .minimum = 0,
80 .maximum = 255,
81 .step = 1,
82 .default_value = 127,
83 .flags = 0,
84 }, {
85 .id = V4L2_CID_CONTRAST,
86 .type = V4L2_CTRL_TYPE_INTEGER,
87 .name = "Contrast",
88 .minimum = 0,
89 .maximum = 255,
90 .step = 0x1,
91 .default_value = 0x10,
92 .flags = 0,
93 }, {
94 .id = V4L2_CID_SATURATION,
95 .type = V4L2_CTRL_TYPE_INTEGER,
96 .name = "Saturation",
97 .minimum = 0,
98 .maximum = 255,
99 .step = 0x1,
100 .default_value = 127,
101 .flags = 0,
102 }, {
103 .id = V4L2_CID_HUE,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Hue",
106 .minimum = -128,
107 .maximum = 127,
108 .step = 0x1,
109 .default_value = 0,
110 .flags = 0,
111 }
112};
113
114static int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
115
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300116#define dprintk(level,fmt, arg...) \
117 do { \
118 if (vivi.debug >= (level)) \
119 printk(KERN_DEBUG "vivi: " fmt , ## arg); \
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300120 } while (0)
121
122/* ------------------------------------------------------------------
123 Basic structures
124 ------------------------------------------------------------------*/
125
126struct vivi_fmt {
127 char *name;
128 u32 fourcc; /* v4l2 format id */
129 int depth;
130};
131
132static struct vivi_fmt format = {
133 .name = "4:2:2, packed, YUYV",
134 .fourcc = V4L2_PIX_FMT_YUYV,
135 .depth = 16,
136};
137
138struct sg_to_addr {
139 int pos;
140 struct scatterlist *sg;
141};
142
143/* buffer for one video frame */
144struct vivi_buffer {
145 /* common v4l buffer stuff -- must be first */
146 struct videobuf_buffer vb;
147
148 struct vivi_fmt *fmt;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300149};
150
151struct 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
164static LIST_HEAD(vivi_devlist);
165
166struct vivi_dev {
167 struct list_head vivi_devlist;
168
Matthias Kaehlcke51b54022007-07-02 10:19:38 -0300169 struct mutex lock;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300170
171 int users;
172
173 /* various device info */
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -0300174 struct video_device *vfd;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300175
176 struct vivi_dmaqueue vidq;
177
178 /* Several counters */
179 int h,m,s,us,jiffies;
180 char timestr[13];
Mauro Carvalho Chehab025341d2007-12-10 04:43:38 -0300181
182 int mv_count; /* Controls bars movement */
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300183};
184
185struct vivi_fh {
186 struct vivi_dev *dev;
187
188 /* video capture */
189 struct vivi_fmt *fmt;
190 unsigned int width,height;
191 struct videobuf_queue vb_vidq;
192
193 enum v4l2_buf_type type;
194};
195
196/* ------------------------------------------------------------------
197 DMA and thread functions
198 ------------------------------------------------------------------*/
199
200/* Bars and Colors should match positions */
201
202enum colors {
203 WHITE,
204 AMBAR,
205 CYAN,
206 GREEN,
207 MAGENTA,
208 RED,
209 BLUE
210};
211
212static u8 bars[8][3] = {
213 /* R G B */
214 {204,204,204}, /* white */
215 {208,208, 0}, /* ambar */
216 { 0,206,206}, /* cyan */
217 { 0,239, 0}, /* green */
218 {239, 0,239}, /* magenta */
219 {205, 0, 0}, /* red */
220 { 0, 0,255}, /* blue */
221 { 0, 0, 0}
222};
223
224#define TO_Y(r,g,b) (((16829*r +33039*g +6416*b + 32768)>>16)+16)
225/* RGB to V(Cr) Color transform */
226#define TO_V(r,g,b) (((28784*r -24103*g -4681*b + 32768)>>16)+128)
227/* RGB to U(Cb) Color transform */
228#define TO_U(r,g,b) (((-9714*r -19070*g +28784*b + 32768)>>16)+128)
229
230#define TSTAMP_MIN_Y 24
231#define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
232#define TSTAMP_MIN_X 64
233
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300234static void gen_line(char *basep,int inipos,int wmax,
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300235 int hmax, int line, int count, char *timestr)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300236{
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300237 int w,i,j,pos=inipos,y;
238 char *p,*s;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300239 u8 chr,r,g,b,color;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300240
241 /* We will just duplicate the second pixel at the packet */
242 wmax/=2;
243
244 /* Generate a standard color bar pattern */
245 for (w=0;w<wmax;w++) {
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300246 int colorpos=((w+count)*8/(wmax+1)) % 8;
247 r=bars[colorpos][0];
248 g=bars[colorpos][1];
249 b=bars[colorpos][2];
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300250
251 for (color=0;color<4;color++) {
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300252 p=basep+pos;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300253
254 switch (color) {
255 case 0:
256 case 2:
257 *p=TO_Y(r,g,b); /* Luminance */
258 break;
259 case 1:
260 *p=TO_U(r,g,b); /* Cb */
261 break;
262 case 3:
263 *p=TO_V(r,g,b); /* Cr */
264 break;
265 }
266 pos++;
267 }
268 }
269
270 /* Checks if it is possible to show timestamp */
271 if (TSTAMP_MAX_Y>=hmax)
272 goto end;
273 if (TSTAMP_MIN_X+strlen(timestr)>=wmax)
274 goto end;
275
276 /* Print stream time */
277 if (line>=TSTAMP_MIN_Y && line<=TSTAMP_MAX_Y) {
278 j=TSTAMP_MIN_X;
279 for (s=timestr;*s;s++) {
280 chr=rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
281 for (i=0;i<7;i++) {
282 if (chr&1<<(7-i)) { /* Font color*/
283 r=bars[BLUE][0];
284 g=bars[BLUE][1];
285 b=bars[BLUE][2];
286 r=g=b=0;
287 g=198;
288 } else { /* Background color */
289 r=bars[WHITE][0];
290 g=bars[WHITE][1];
291 b=bars[WHITE][2];
292 r=g=b=0;
293 }
294
295 pos=inipos+j*2;
296 for (color=0;color<4;color++) {
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300297 p=basep+pos;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300298
299 y=TO_Y(r,g,b);
300
301 switch (color) {
302 case 0:
303 case 2:
304 *p=TO_Y(r,g,b); /* Luminance */
305 break;
306 case 1:
307 *p=TO_U(r,g,b); /* Cb */
308 break;
309 case 3:
310 *p=TO_V(r,g,b); /* Cr */
311 break;
312 }
313 pos++;
314 }
315 j++;
316 }
317 }
318 }
319
320
321end:
Mauro Carvalho Chehabb50e7fe2007-01-25 05:00:01 -0300322 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300323}
324static void vivi_fillbuff(struct vivi_dev *dev,struct vivi_buffer *buf)
325{
326 int h,pos=0;
327 int hmax = buf->vb.height;
328 int wmax = buf->vb.width;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300329 struct timeval ts;
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300330 char *tmpbuf = kmalloc(wmax*2,GFP_KERNEL);
331 void *vbuf=videobuf_to_vmalloc (&buf->vb);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300332
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300333 if (!tmpbuf)
334 return;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300335
336 for (h=0;h<hmax;h++) {
Mauro Carvalho Chehab025341d2007-12-10 04:43:38 -0300337 gen_line(tmpbuf, 0, wmax, hmax, h, dev->mv_count,
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300338 dev->timestr);
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300339 /* FIXME: replacing to __copy_to_user */
340 if (copy_to_user(vbuf+pos,tmpbuf,wmax*2)!=0)
341 dprintk(2,"vivifill copy_to_user failed.\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300342 pos += wmax*2;
343 }
344
Mauro Carvalho Chehab025341d2007-12-10 04:43:38 -0300345 dev->mv_count++;
Mauro Carvalho Chehab3bef5e42007-09-22 02:01:33 -0300346
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300347 kfree(tmpbuf);
348
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300349 /* Updates stream time */
350
351 dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
352 dev->jiffies=jiffies;
353 if (dev->us>=1000000) {
354 dev->us-=1000000;
355 dev->s++;
356 if (dev->s>=60) {
357 dev->s-=60;
358 dev->m++;
359 if (dev->m>60) {
360 dev->m-=60;
361 dev->h++;
362 if (dev->h>24)
363 dev->h-=24;
364 }
365 }
366 }
367 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
368 dev->h,dev->m,dev->s,(dev->us+500)/1000);
369
370 dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300371 (unsigned long)tmpbuf,pos);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300372
373 /* Advice that buffer was filled */
Brandon Philips0fc06862007-11-06 20:02:36 -0300374 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300375 buf->vb.field_count++;
376 do_gettimeofday(&ts);
377 buf->vb.ts = ts;
378
379 list_del(&buf->vb.queue);
380 wake_up(&buf->vb.done);
381}
382
383static int restart_video_queue(struct vivi_dmaqueue *dma_q);
384
385static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
386{
387 struct vivi_buffer *buf;
388 struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
389
390 int bc;
391
392 /* Announces videobuf that all went ok */
393 for (bc = 0;; bc++) {
394 if (list_empty(&dma_q->active)) {
395 dprintk(1,"No active queue to serve\n");
396 break;
397 }
398
399 buf = list_entry(dma_q->active.next,
400 struct vivi_buffer, vb.queue);
401
402 /* Nobody is waiting something to be done, just return */
403 if (!waitqueue_active(&buf->vb.done)) {
404 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
405 return;
406 }
407
408 do_gettimeofday(&buf->vb.ts);
409 dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
410
411 /* Fill buffer */
412 vivi_fillbuff(dev,buf);
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300413
414 if (list_empty(&dma_q->active)) {
415 del_timer(&dma_q->timeout);
416 } else {
417 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
418 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300419 }
420 if (bc != 1)
421 dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
422}
423
Adrian Bunk972c3512006-04-27 21:06:50 -0300424static void vivi_sleep(struct vivi_dmaqueue *dma_q)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300425{
426 int timeout;
427 DECLARE_WAITQUEUE(wait, current);
428
429 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
430
431 add_wait_queue(&dma_q->wq, &wait);
432 if (!kthread_should_stop()) {
433 dma_q->frame++;
434
435 /* Calculate time to wake up */
436 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
437
438 if (timeout <= 0) {
439 int old=dma_q->frame;
440 dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
441
442 timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
443
444 dprintk(1,"underrun, losed %d frames. "
445 "Now, frame is %d. Waking on %d jiffies\n",
446 dma_q->frame-old,dma_q->frame,timeout);
447 } else
448 dprintk(1,"will sleep for %i jiffies\n",timeout);
449
450 vivi_thread_tick(dma_q);
451
452 schedule_timeout_interruptible (timeout);
453 }
454
455 remove_wait_queue(&dma_q->wq, &wait);
456 try_to_freeze();
457}
458
Adrian Bunk972c3512006-04-27 21:06:50 -0300459static int vivi_thread(void *data)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300460{
461 struct vivi_dmaqueue *dma_q=data;
462
463 dprintk(1,"thread started\n");
464
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300465 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700466 set_freezable();
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300467
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300468 for (;;) {
469 vivi_sleep(dma_q);
470
471 if (kthread_should_stop())
472 break;
473 }
474 dprintk(1, "thread: exit\n");
475 return 0;
476}
477
Adrian Bunk972c3512006-04-27 21:06:50 -0300478static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300479{
480 dma_q->frame=0;
481 dma_q->ini_jiffies=jiffies;
482
483 dprintk(1,"%s\n",__FUNCTION__);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300484
485 dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
486
Akinobu Mita054afee2006-12-20 10:04:00 -0300487 if (IS_ERR(dma_q->kthread)) {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300488 printk(KERN_ERR "vivi: kernel_thread() failed\n");
Akinobu Mita054afee2006-12-20 10:04:00 -0300489 return PTR_ERR(dma_q->kthread);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300490 }
Mauro Carvalho Chehab0b600512007-01-14 08:33:24 -0300491 /* Wakes thread */
492 wake_up_interruptible(&dma_q->wq);
493
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300494 dprintk(1,"returning from %s\n",__FUNCTION__);
495 return 0;
496}
497
Adrian Bunk972c3512006-04-27 21:06:50 -0300498static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300499{
500 dprintk(1,"%s\n",__FUNCTION__);
501 /* shutdown control thread */
502 if (dma_q->kthread) {
503 kthread_stop(dma_q->kthread);
504 dma_q->kthread=NULL;
505 }
506}
507
508static int restart_video_queue(struct vivi_dmaqueue *dma_q)
509{
510 struct vivi_buffer *buf, *prev;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300511
512 dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
513
514 if (!list_empty(&dma_q->active)) {
515 buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
516 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
517 buf, buf->vb.i);
518
519 dprintk(1,"Restarting video dma\n");
520 vivi_stop_thread(dma_q);
521// vivi_start_thread(dma_q);
522
523 /* cancel all outstanding capture / vbi requests */
Trent Piephoa991f442007-10-10 05:37:43 -0300524 list_for_each_entry_safe(buf, prev, &dma_q->active, vb.queue) {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300525 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -0300526 buf->vb.state = VIDEOBUF_ERROR;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300527 wake_up(&buf->vb.done);
528 }
529 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
530
531 return 0;
532 }
533
534 prev = NULL;
535 for (;;) {
536 if (list_empty(&dma_q->queued))
537 return 0;
538 buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
539 if (NULL == prev) {
540 list_del(&buf->vb.queue);
541 list_add_tail(&buf->vb.queue,&dma_q->active);
542
543 dprintk(1,"Restarting video dma\n");
544 vivi_stop_thread(dma_q);
545 vivi_start_thread(dma_q);
546
Brandon Philips0fc06862007-11-06 20:02:36 -0300547 buf->vb.state = VIDEOBUF_ACTIVE;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300548 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
549 dprintk(2,"[%p/%d] restart_queue - first active\n",
550 buf,buf->vb.i);
551
552 } else if (prev->vb.width == buf->vb.width &&
553 prev->vb.height == buf->vb.height &&
554 prev->fmt == buf->fmt) {
555 list_del(&buf->vb.queue);
556 list_add_tail(&buf->vb.queue,&dma_q->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300557 buf->vb.state = VIDEOBUF_ACTIVE;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300558 dprintk(2,"[%p/%d] restart_queue - move to active\n",
559 buf,buf->vb.i);
560 } else {
561 return 0;
562 }
563 prev = buf;
564 }
565}
566
567static void vivi_vid_timeout(unsigned long data)
568{
569 struct vivi_dev *dev = (struct vivi_dev*)data;
570 struct vivi_dmaqueue *vidq = &dev->vidq;
571 struct vivi_buffer *buf;
572
573 while (!list_empty(&vidq->active)) {
574 buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
575 list_del(&buf->vb.queue);
Brandon Philips0fc06862007-11-06 20:02:36 -0300576 buf->vb.state = VIDEOBUF_ERROR;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300577 wake_up(&buf->vb.done);
578 printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
579 }
580
581 restart_video_queue(vidq);
582}
583
584/* ------------------------------------------------------------------
585 Videobuf operations
586 ------------------------------------------------------------------*/
587static int
588buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
589{
590 struct vivi_fh *fh = vq->priv_data;
591
592 *size = fh->width*fh->height*2;
593
594 if (0 == *count)
595 *count = 32;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300596
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300597 while (*size * *count > vid_limit * 1024 * 1024)
598 (*count)--;
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300599
600 dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__,*count, *size);
601
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300602 return 0;
603}
604
Adrian Bunk972c3512006-04-27 21:06:50 -0300605static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300606{
607 dprintk(1,"%s\n",__FUNCTION__);
608
609 if (in_interrupt())
610 BUG();
611
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300612 videobuf_waiton(&buf->vb,0,0);
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -0300613 videobuf_vmalloc_free(&buf->vb);
Brandon Philips0fc06862007-11-06 20:02:36 -0300614 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300615}
616
617#define norm_maxw() 1024
618#define norm_maxh() 768
619static int
620buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
621 enum v4l2_field field)
622{
623 struct vivi_fh *fh = vq->priv_data;
624 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
625 int rc, init_buffer = 0;
626
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300627 dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300628
629 BUG_ON(NULL == fh->fmt);
630 if (fh->width < 48 || fh->width > norm_maxw() ||
631 fh->height < 32 || fh->height > norm_maxh())
632 return -EINVAL;
633 buf->vb.size = fh->width*fh->height*2;
634 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
635 return -EINVAL;
636
637 if (buf->fmt != fh->fmt ||
638 buf->vb.width != fh->width ||
639 buf->vb.height != fh->height ||
640 buf->vb.field != field) {
641 buf->fmt = fh->fmt;
642 buf->vb.width = fh->width;
643 buf->vb.height = fh->height;
644 buf->vb.field = field;
645 init_buffer = 1;
646 }
647
Brandon Philips0fc06862007-11-06 20:02:36 -0300648 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300649 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
650 goto fail;
651 }
652
Brandon Philips0fc06862007-11-06 20:02:36 -0300653 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300654
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300655 return 0;
656
657fail:
658 free_buffer(vq,buf);
659 return rc;
660}
661
662static void
663buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
664{
665 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
666 struct vivi_fh *fh = vq->priv_data;
667 struct vivi_dev *dev = fh->dev;
668 struct vivi_dmaqueue *vidq = &dev->vidq;
669 struct vivi_buffer *prev;
670
671 if (!list_empty(&vidq->queued)) {
672 dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
673 list_add_tail(&buf->vb.queue,&vidq->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300674 buf->vb.state = VIDEOBUF_QUEUED;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300675 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
676 buf, buf->vb.i);
677 } else if (list_empty(&vidq->active)) {
678 list_add_tail(&buf->vb.queue,&vidq->active);
679
Brandon Philips0fc06862007-11-06 20:02:36 -0300680 buf->vb.state = VIDEOBUF_ACTIVE;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300681 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
682 dprintk(2,"[%p/%d] buffer_queue - first active\n",
683 buf, buf->vb.i);
684
685 vivi_start_thread(vidq);
686 } else {
687 prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
688 if (prev->vb.width == buf->vb.width &&
689 prev->vb.height == buf->vb.height &&
690 prev->fmt == buf->fmt) {
691 list_add_tail(&buf->vb.queue,&vidq->active);
Brandon Philips0fc06862007-11-06 20:02:36 -0300692 buf->vb.state = VIDEOBUF_ACTIVE;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300693 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
694 buf, buf->vb.i);
695
696 } else {
697 list_add_tail(&buf->vb.queue,&vidq->queued);
Brandon Philips0fc06862007-11-06 20:02:36 -0300698 buf->vb.state = VIDEOBUF_QUEUED;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300699 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
700 buf, buf->vb.i);
701 }
702 }
703}
704
705static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
706{
707 struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
708 struct vivi_fh *fh = vq->priv_data;
709 struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
710 struct vivi_dmaqueue *vidq = &dev->vidq;
711
712 dprintk(1,"%s\n",__FUNCTION__);
713
714 vivi_stop_thread(vidq);
715
716 free_buffer(vq,buf);
717}
718
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300719static struct videobuf_queue_ops vivi_video_qops = {
720 .buf_setup = buffer_setup,
721 .buf_prepare = buffer_prepare,
722 .buf_queue = buffer_queue,
723 .buf_release = buffer_release,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300724};
725
726/* ------------------------------------------------------------------
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300727 IOCTL vidioc handling
728 ------------------------------------------------------------------*/
729static int vidioc_querycap (struct file *file, void *priv,
730 struct v4l2_capability *cap)
731{
732 strcpy(cap->driver, "vivi");
733 strcpy(cap->card, "vivi");
734 cap->version = VIVI_VERSION;
735 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
736 V4L2_CAP_STREAMING |
737 V4L2_CAP_READWRITE;
738 return 0;
739}
740
741static int vidioc_enum_fmt_cap (struct file *file, void *priv,
742 struct v4l2_fmtdesc *f)
743{
744 if (f->index > 0)
745 return -EINVAL;
746
747 strlcpy(f->description,format.name,sizeof(f->description));
748 f->pixelformat = format.fourcc;
749 return 0;
750}
751
752static int vidioc_g_fmt_cap (struct file *file, void *priv,
753 struct v4l2_format *f)
754{
755 struct vivi_fh *fh=priv;
756
757 f->fmt.pix.width = fh->width;
758 f->fmt.pix.height = fh->height;
759 f->fmt.pix.field = fh->vb_vidq.field;
760 f->fmt.pix.pixelformat = fh->fmt->fourcc;
761 f->fmt.pix.bytesperline =
762 (f->fmt.pix.width * fh->fmt->depth) >> 3;
763 f->fmt.pix.sizeimage =
764 f->fmt.pix.height * f->fmt.pix.bytesperline;
765
766 return (0);
767}
768
769static int vidioc_try_fmt_cap (struct file *file, void *priv,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300770 struct v4l2_format *f)
771{
772 struct vivi_fmt *fmt;
773 enum v4l2_field field;
774 unsigned int maxw, maxh;
775
776 if (format.fourcc != f->fmt.pix.pixelformat) {
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300777 dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
778 "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300779 return -EINVAL;
780 }
781 fmt=&format;
782
783 field = f->fmt.pix.field;
784
785 if (field == V4L2_FIELD_ANY) {
Brandon Philipsa326ae12007-09-27 20:54:52 -0300786 field=V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300787 } else if (V4L2_FIELD_INTERLACED != field) {
788 dprintk(1,"Field type invalid.\n");
789 return -EINVAL;
790 }
791
792 maxw = norm_maxw();
793 maxh = norm_maxh();
794
795 f->fmt.pix.field = field;
796 if (f->fmt.pix.height < 32)
797 f->fmt.pix.height = 32;
798 if (f->fmt.pix.height > maxh)
799 f->fmt.pix.height = maxh;
800 if (f->fmt.pix.width < 48)
801 f->fmt.pix.width = 48;
802 if (f->fmt.pix.width > maxw)
803 f->fmt.pix.width = maxw;
804 f->fmt.pix.width &= ~0x03;
805 f->fmt.pix.bytesperline =
806 (f->fmt.pix.width * fmt->depth) >> 3;
807 f->fmt.pix.sizeimage =
808 f->fmt.pix.height * f->fmt.pix.bytesperline;
809
810 return 0;
811}
812
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300813/*FIXME: This seems to be generic enough to be at videodev2 */
814static int vidioc_s_fmt_cap (struct file *file, void *priv,
815 struct v4l2_format *f)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300816{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300817 struct vivi_fh *fh=priv;
818 int ret = vidioc_try_fmt_cap(file,fh,f);
819 if (ret < 0)
820 return (ret);
821
822 fh->fmt = &format;
823 fh->width = f->fmt.pix.width;
824 fh->height = f->fmt.pix.height;
825 fh->vb_vidq.field = f->fmt.pix.field;
826 fh->type = f->type;
827
828 return (0);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300829}
830
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300831static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300832{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300833 struct vivi_fh *fh=priv;
834
835 return (videobuf_reqbufs(&fh->vb_vidq, p));
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300836}
837
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300838static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300839{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300840 struct vivi_fh *fh=priv;
841
842 return (videobuf_querybuf(&fh->vb_vidq, p));
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300843}
844
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300845static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300846{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300847 struct vivi_fh *fh=priv;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300848
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300849 return (videobuf_qbuf(&fh->vb_vidq, p));
850}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300851
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300852static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
853{
854 struct vivi_fh *fh=priv;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300855
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300856 return (videobuf_dqbuf(&fh->vb_vidq, p,
857 file->f_flags & O_NONBLOCK));
858}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300859
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -0300860#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300861static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
862{
863 struct vivi_fh *fh=priv;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300864
Mauro Carvalho Chehab6bb27902007-08-23 16:41:14 -0300865 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300866}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300867#endif
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300868
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300869static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300870{
871 struct vivi_fh *fh=priv;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300872
873 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
874 return -EINVAL;
875 if (i != fh->type)
876 return -EINVAL;
877
Brandon Philipsba32bd92007-09-27 20:55:17 -0300878 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300879}
880
Adrian Bunkdc46ace2006-06-23 06:42:44 -0300881static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300882{
883 struct vivi_fh *fh=priv;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300884
885 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
886 return -EINVAL;
887 if (i != fh->type)
888 return -EINVAL;
889
Brandon Philipsba32bd92007-09-27 20:55:17 -0300890 return videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300891}
892
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -0300893static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300894{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300895 return 0;
896}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300897
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300898/* only one input in this sample driver */
899static int vidioc_enum_input (struct file *file, void *priv,
900 struct v4l2_input *inp)
901{
902 if (inp->index != 0)
903 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300904
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300905 inp->type = V4L2_INPUT_TYPE_CAMERA;
906 inp->std = V4L2_STD_NTSC_M;
907 strcpy(inp->name,"Camera");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300908
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300909 return (0);
910}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300911
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300912static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
913{
914 *i = 0;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300915
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300916 return (0);
917}
918static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
919{
920 if (i > 0)
921 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300922
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300923 return (0);
924}
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300925
926 /* --- controls ---------------------------------------------- */
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300927static int vidioc_queryctrl (struct file *file, void *priv,
928 struct v4l2_queryctrl *qc)
929{
930 int i;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300931
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300932 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
933 if (qc->id && qc->id == vivi_qctrl[i].id) {
934 memcpy(qc, &(vivi_qctrl[i]),
935 sizeof(*qc));
936 return (0);
937 }
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300938
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300939 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300940}
941
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300942static int vidioc_g_ctrl (struct file *file, void *priv,
943 struct v4l2_control *ctrl)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300944{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300945 int i;
946
947 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
948 if (ctrl->id == vivi_qctrl[i].id) {
949 ctrl->value=qctl_regs[i];
950 return (0);
951 }
952
953 return -EINVAL;
954}
955static int vidioc_s_ctrl (struct file *file, void *priv,
956 struct v4l2_control *ctrl)
957{
958 int i;
959
960 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
961 if (ctrl->id == vivi_qctrl[i].id) {
962 if (ctrl->value <
963 vivi_qctrl[i].minimum
964 || ctrl->value >
965 vivi_qctrl[i].maximum) {
966 return (-ERANGE);
967 }
968 qctl_regs[i]=ctrl->value;
969 return (0);
970 }
971 return -EINVAL;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300972}
973
974/* ------------------------------------------------------------------
975 File operations for the device
976 ------------------------------------------------------------------*/
977
978#define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
979
980static int vivi_open(struct inode *inode, struct file *file)
981{
982 int minor = iminor(inode);
Trent Piephoa991f442007-10-10 05:37:43 -0300983 struct vivi_dev *dev;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300984 struct vivi_fh *fh;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300985 int i;
986
987 printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
988
Trent Piephoa991f442007-10-10 05:37:43 -0300989 list_for_each_entry(dev, &vivi_devlist, vivi_devlist)
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -0300990 if (dev->vfd->minor == minor)
Trent Piephoa991f442007-10-10 05:37:43 -0300991 goto found;
992 return -ENODEV;
993found:
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300994
995
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -0300996
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -0300997 /* If more than one user, mutex should be added */
998 dev->users++;
999
Trent Piephoa991f442007-10-10 05:37:43 -03001000 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1001 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001002
1003 /* allocate + initialize per filehandle data */
1004 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1005 if (NULL == fh) {
1006 dev->users--;
1007 return -ENOMEM;
1008 }
1009
1010 file->private_data = fh;
1011 fh->dev = dev;
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001012
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001013 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1014 fh->fmt = &format;
1015 fh->width = 640;
1016 fh->height = 480;
1017
1018 /* Put all controls at a sane state */
1019 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1020 qctl_regs[i] =vivi_qctrl[i].default_value;
1021
1022 dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1023 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1024 dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
1025 dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
1026
1027 /* Resets frame counters */
1028 dev->h=0;
1029 dev->m=0;
1030 dev->s=0;
1031 dev->us=0;
1032 dev->jiffies=jiffies;
1033 sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
1034 dev->h,dev->m,dev->s,(dev->us+500)/1000);
1035
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -03001036 videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001037 NULL, NULL,
1038 fh->type,
1039 V4L2_FIELD_INTERLACED,
1040 sizeof(struct vivi_buffer),fh);
1041
1042 return 0;
1043}
1044
1045static ssize_t
1046vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1047{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001048 struct vivi_fh *fh = file->private_data;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001049
1050 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehabacb09af2007-07-29 22:56:11 -03001051 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001052 file->f_flags & O_NONBLOCK);
1053 }
1054 return 0;
1055}
1056
1057static unsigned int
1058vivi_poll(struct file *file, struct poll_table_struct *wait)
1059{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001060 struct vivi_fh *fh = file->private_data;
Brandon Philips85c7c70bc2007-09-27 20:55:02 -03001061 struct videobuf_queue *q = &fh->vb_vidq;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001062
1063 dprintk(1,"%s\n",__FUNCTION__);
1064
1065 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1066 return POLLERR;
1067
Brandon Philips85c7c70bc2007-09-27 20:55:02 -03001068 return videobuf_poll_stream(file, q, wait);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001069}
1070
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001071static int vivi_close(struct inode *inode, struct file *file)
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001072{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001073 struct vivi_fh *fh = file->private_data;
1074 struct vivi_dev *dev = fh->dev;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001075 struct vivi_dmaqueue *vidq = &dev->vidq;
1076
1077 int minor = iminor(inode);
1078
1079 vivi_stop_thread(vidq);
Brandon Philips053fcb62007-11-13 20:11:26 -03001080 videobuf_stop(&fh->vb_vidq);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001081 videobuf_mmap_free(&fh->vb_vidq);
1082
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001083 kfree(fh);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001084
1085 dev->users--;
1086
1087 printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
1088
1089 return 0;
1090}
1091
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001092static int vivi_release(void)
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001093{
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001094 struct vivi_dev *dev;
1095 struct list_head *list;
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001096
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001097 while (!list_empty(&vivi_devlist)) {
1098 list = vivi_devlist.next;
1099 list_del(list);
1100 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1101
1102 if (-1 != dev->vfd->minor)
1103 video_unregister_device(dev->vfd);
1104 else
1105 video_device_release(dev->vfd);
1106
1107 kfree(dev);
1108 }
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001109
1110 return 0;
1111}
1112
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001113static int
1114vivi_mmap(struct file *file, struct vm_area_struct * vma)
1115{
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001116 struct vivi_fh *fh = file->private_data;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001117 int ret;
1118
1119 dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
1120
1121 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1122
1123 dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
1124 (unsigned long)vma->vm_start,
1125 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1126 ret);
1127
1128 return ret;
1129}
1130
Arjan van de Venfa027c22007-02-12 00:55:33 -08001131static const struct file_operations vivi_fops = {
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001132 .owner = THIS_MODULE,
1133 .open = vivi_open,
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001134 .release = vivi_close,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001135 .read = vivi_read,
1136 .poll = vivi_poll,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001137 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
Mauro Carvalho Chehab5a037702007-08-02 23:31:54 -03001138 .mmap = vivi_mmap,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001139 .llseek = no_llseek,
1140};
1141
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001142static struct video_device vivi_template = {
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001143 .name = "vivi",
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001144 .type = VID_TYPE_CAPTURE,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001145 .fops = &vivi_fops,
1146 .minor = -1,
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001147 .release = video_device_release,
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001148
1149 .vidioc_querycap = vidioc_querycap,
1150 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1151 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1152 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1153 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1154 .vidioc_reqbufs = vidioc_reqbufs,
1155 .vidioc_querybuf = vidioc_querybuf,
1156 .vidioc_qbuf = vidioc_qbuf,
1157 .vidioc_dqbuf = vidioc_dqbuf,
1158 .vidioc_s_std = vidioc_s_std,
1159 .vidioc_enum_input = vidioc_enum_input,
1160 .vidioc_g_input = vidioc_g_input,
1161 .vidioc_s_input = vidioc_s_input,
1162 .vidioc_queryctrl = vidioc_queryctrl,
1163 .vidioc_g_ctrl = vidioc_g_ctrl,
1164 .vidioc_s_ctrl = vidioc_s_ctrl,
1165 .vidioc_streamon = vidioc_streamon,
1166 .vidioc_streamoff = vidioc_streamoff,
Mauro Carvalho Chehab0dfa9ab2006-08-08 09:10:10 -03001167#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001168 .vidiocgmbuf = vidiocgmbuf,
1169#endif
Mauro Carvalho Chehabe75f9ce2006-11-20 13:19:20 -03001170 .tvnorms = V4L2_STD_NTSC_M,
1171 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001172};
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001173/* -----------------------------------------------------------------
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001174 Initialization and module stuff
1175 ------------------------------------------------------------------*/
1176
1177static int __init vivi_init(void)
1178{
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001179 int ret = -ENOMEM, i;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001180 struct vivi_dev *dev;
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001181 struct video_device *vfd;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001182
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001183 for (i = 0; i < n_devs; i++) {
1184 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1185 if (NULL == dev)
1186 break;
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001187
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001188 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001189
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001190 /* init video dma queues */
1191 INIT_LIST_HEAD(&dev->vidq.active);
1192 INIT_LIST_HEAD(&dev->vidq.queued);
1193 init_waitqueue_head(&dev->vidq.wq);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001194
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001195 /* initialize locks */
1196 mutex_init(&dev->lock);
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001197
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001198 dev->vidq.timeout.function = vivi_vid_timeout;
1199 dev->vidq.timeout.data = (unsigned long)dev;
1200 init_timer(&dev->vidq.timeout);
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001201
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001202 vfd = video_device_alloc();
1203 if (NULL == vfd)
1204 break;
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001205
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001206 *vfd = vivi_template;
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001207
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001208 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1209 if (ret < 0)
1210 break;
Mauro Carvalho Chehabf905c442007-12-10 04:07:03 -03001211
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001212 snprintf(vfd->name, sizeof(vfd->name), "%s (%i)",
1213 vivi_template.name, vfd->minor);
1214
1215 if (video_nr >= 0)
1216 video_nr++;
1217
1218 dev->vfd = vfd;
1219 }
1220
1221 if (ret < 0) {
1222 vivi_release();
1223 printk(KERN_INFO "Error %d while loading vivi driver\n", ret);
1224 } else
1225 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1226 "Capture Board successfully loaded.\n");
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001227 return ret;
1228}
1229
1230static void __exit vivi_exit(void)
1231{
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001232 vivi_release();
Mauro Carvalho Chehab1e6dd652006-03-10 12:40:10 -03001233}
1234
1235module_init(vivi_init);
1236module_exit(vivi_exit);
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001237
1238MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
1239MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
1240MODULE_LICENSE("Dual BSD/GPL");
1241
1242module_param(video_nr, int, 0);
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001243MODULE_PARM_DESC(video_nr, "video iminor start number");
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001244
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001245module_param(n_devs, int, 0);
1246MODULE_PARM_DESC(n_devs, "number of video devices to create");
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001247
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001248module_param_named(debug, vivi.debug, int, 0644);
1249MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehabc820cc42006-06-04 10:34:12 -03001250
Mauro Carvalho Chehab55712ff2007-12-10 04:38:11 -03001251module_param(vid_limit, int, 0644);
1252MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");