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