vivi.c 33 KB

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