vivi.c 35 KB

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