vivi.c 30 KB

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