vivi.c 33 KB

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