vivi.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. 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 gen_line(char *basep,int inipos,int wmax,
  199. int hmax, int line, int count, char *timestr)
  200. {
  201. int w,i,j,pos=inipos,y;
  202. char *p,*s;
  203. u8 chr,r,g,b,color;
  204. /* We will just duplicate the second pixel at the packet */
  205. wmax/=2;
  206. /* Generate a standard color bar pattern */
  207. for (w=0;w<wmax;w++) {
  208. int colorpos=((w+count)*8/(wmax+1)) % 8;
  209. r=bars[colorpos][0];
  210. g=bars[colorpos][1];
  211. b=bars[colorpos][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. /* FIXME: move to dev struct */
  286. static int mv_count=0;
  287. if (!tmpbuf)
  288. return;
  289. for (h=0;h<hmax;h++) {
  290. gen_line(tmpbuf,0,wmax,hmax,h,mv_count,
  291. dev->timestr);
  292. /* FIXME: replacing to __copy_to_user */
  293. if (copy_to_user(vbuf+pos,tmpbuf,wmax*2)!=0)
  294. dprintk(2,"vivifill copy_to_user failed.\n");
  295. pos += wmax*2;
  296. }
  297. mv_count++;
  298. kfree(tmpbuf);
  299. /* Updates stream time */
  300. dev->us+=jiffies_to_usecs(jiffies-dev->jiffies);
  301. dev->jiffies=jiffies;
  302. if (dev->us>=1000000) {
  303. dev->us-=1000000;
  304. dev->s++;
  305. if (dev->s>=60) {
  306. dev->s-=60;
  307. dev->m++;
  308. if (dev->m>60) {
  309. dev->m-=60;
  310. dev->h++;
  311. if (dev->h>24)
  312. dev->h-=24;
  313. }
  314. }
  315. }
  316. sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
  317. dev->h,dev->m,dev->s,(dev->us+500)/1000);
  318. dprintk(2,"vivifill at %s: Buffer 0x%08lx size= %d\n",dev->timestr,
  319. (unsigned long)tmpbuf,pos);
  320. /* Advice that buffer was filled */
  321. buf->vb.state = STATE_DONE;
  322. buf->vb.field_count++;
  323. do_gettimeofday(&ts);
  324. buf->vb.ts = ts;
  325. list_del(&buf->vb.queue);
  326. wake_up(&buf->vb.done);
  327. }
  328. static int restart_video_queue(struct vivi_dmaqueue *dma_q);
  329. static void vivi_thread_tick(struct vivi_dmaqueue *dma_q)
  330. {
  331. struct vivi_buffer *buf;
  332. struct vivi_dev *dev= container_of(dma_q,struct vivi_dev,vidq);
  333. int bc;
  334. /* Announces videobuf that all went ok */
  335. for (bc = 0;; bc++) {
  336. if (list_empty(&dma_q->active)) {
  337. dprintk(1,"No active queue to serve\n");
  338. break;
  339. }
  340. buf = list_entry(dma_q->active.next,
  341. struct vivi_buffer, vb.queue);
  342. /* Nobody is waiting something to be done, just return */
  343. if (!waitqueue_active(&buf->vb.done)) {
  344. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  345. return;
  346. }
  347. do_gettimeofday(&buf->vb.ts);
  348. dprintk(2,"[%p/%d] wakeup\n",buf,buf->vb.i);
  349. /* Fill buffer */
  350. vivi_fillbuff(dev,buf);
  351. if (list_empty(&dma_q->active)) {
  352. del_timer(&dma_q->timeout);
  353. } else {
  354. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  355. }
  356. }
  357. if (bc != 1)
  358. dprintk(1,"%s: %d buffers handled (should be 1)\n",__FUNCTION__,bc);
  359. }
  360. static void vivi_sleep(struct vivi_dmaqueue *dma_q)
  361. {
  362. int timeout;
  363. DECLARE_WAITQUEUE(wait, current);
  364. dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
  365. add_wait_queue(&dma_q->wq, &wait);
  366. if (!kthread_should_stop()) {
  367. dma_q->frame++;
  368. /* Calculate time to wake up */
  369. timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
  370. if (timeout <= 0) {
  371. int old=dma_q->frame;
  372. dma_q->frame=(jiffies_to_msecs(jiffies-dma_q->ini_jiffies)*WAKE_DENOMINATOR)/(WAKE_NUMERATOR*1000)+1;
  373. timeout=dma_q->ini_jiffies+msecs_to_jiffies((dma_q->frame*WAKE_NUMERATOR*1000)/WAKE_DENOMINATOR)-jiffies;
  374. dprintk(1,"underrun, losed %d frames. "
  375. "Now, frame is %d. Waking on %d jiffies\n",
  376. dma_q->frame-old,dma_q->frame,timeout);
  377. } else
  378. dprintk(1,"will sleep for %i jiffies\n",timeout);
  379. vivi_thread_tick(dma_q);
  380. schedule_timeout_interruptible (timeout);
  381. }
  382. remove_wait_queue(&dma_q->wq, &wait);
  383. try_to_freeze();
  384. }
  385. static int vivi_thread(void *data)
  386. {
  387. struct vivi_dmaqueue *dma_q=data;
  388. dprintk(1,"thread started\n");
  389. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  390. set_freezable();
  391. for (;;) {
  392. vivi_sleep(dma_q);
  393. if (kthread_should_stop())
  394. break;
  395. }
  396. dprintk(1, "thread: exit\n");
  397. return 0;
  398. }
  399. static int vivi_start_thread(struct vivi_dmaqueue *dma_q)
  400. {
  401. dma_q->frame=0;
  402. dma_q->ini_jiffies=jiffies;
  403. dprintk(1,"%s\n",__FUNCTION__);
  404. dma_q->kthread = kthread_run(vivi_thread, dma_q, "vivi");
  405. if (IS_ERR(dma_q->kthread)) {
  406. printk(KERN_ERR "vivi: kernel_thread() failed\n");
  407. return PTR_ERR(dma_q->kthread);
  408. }
  409. /* Wakes thread */
  410. wake_up_interruptible(&dma_q->wq);
  411. dprintk(1,"returning from %s\n",__FUNCTION__);
  412. return 0;
  413. }
  414. static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
  415. {
  416. dprintk(1,"%s\n",__FUNCTION__);
  417. /* shutdown control thread */
  418. if (dma_q->kthread) {
  419. kthread_stop(dma_q->kthread);
  420. dma_q->kthread=NULL;
  421. }
  422. }
  423. static int restart_video_queue(struct vivi_dmaqueue *dma_q)
  424. {
  425. struct vivi_buffer *buf, *prev;
  426. dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
  427. if (!list_empty(&dma_q->active)) {
  428. buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
  429. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  430. buf, buf->vb.i);
  431. dprintk(1,"Restarting video dma\n");
  432. vivi_stop_thread(dma_q);
  433. // vivi_start_thread(dma_q);
  434. /* cancel all outstanding capture / vbi requests */
  435. list_for_each_entry_safe(buf, prev, &dma_q->active, vb.queue) {
  436. list_del(&buf->vb.queue);
  437. buf->vb.state = STATE_ERROR;
  438. wake_up(&buf->vb.done);
  439. }
  440. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  441. return 0;
  442. }
  443. prev = NULL;
  444. for (;;) {
  445. if (list_empty(&dma_q->queued))
  446. return 0;
  447. buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
  448. if (NULL == prev) {
  449. list_del(&buf->vb.queue);
  450. list_add_tail(&buf->vb.queue,&dma_q->active);
  451. dprintk(1,"Restarting video dma\n");
  452. vivi_stop_thread(dma_q);
  453. vivi_start_thread(dma_q);
  454. buf->vb.state = STATE_ACTIVE;
  455. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  456. dprintk(2,"[%p/%d] restart_queue - first active\n",
  457. buf,buf->vb.i);
  458. } else if (prev->vb.width == buf->vb.width &&
  459. prev->vb.height == buf->vb.height &&
  460. prev->fmt == buf->fmt) {
  461. list_del(&buf->vb.queue);
  462. list_add_tail(&buf->vb.queue,&dma_q->active);
  463. buf->vb.state = STATE_ACTIVE;
  464. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  465. buf,buf->vb.i);
  466. } else {
  467. return 0;
  468. }
  469. prev = buf;
  470. }
  471. }
  472. static void vivi_vid_timeout(unsigned long data)
  473. {
  474. struct vivi_dev *dev = (struct vivi_dev*)data;
  475. struct vivi_dmaqueue *vidq = &dev->vidq;
  476. struct vivi_buffer *buf;
  477. while (!list_empty(&vidq->active)) {
  478. buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
  479. list_del(&buf->vb.queue);
  480. buf->vb.state = STATE_ERROR;
  481. wake_up(&buf->vb.done);
  482. printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
  483. }
  484. restart_video_queue(vidq);
  485. }
  486. /* ------------------------------------------------------------------
  487. Videobuf operations
  488. ------------------------------------------------------------------*/
  489. static int
  490. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  491. {
  492. struct vivi_fh *fh = vq->priv_data;
  493. *size = fh->width*fh->height*2;
  494. if (0 == *count)
  495. *count = 32;
  496. while (*size * *count > vid_limit * 1024 * 1024)
  497. (*count)--;
  498. dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__,*count, *size);
  499. return 0;
  500. }
  501. static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
  502. {
  503. dprintk(1,"%s\n",__FUNCTION__);
  504. if (in_interrupt())
  505. BUG();
  506. videobuf_waiton(&buf->vb,0,0);
  507. videobuf_vmalloc_free(&buf->vb);
  508. buf->vb.state = STATE_NEEDS_INIT;
  509. }
  510. #define norm_maxw() 1024
  511. #define norm_maxh() 768
  512. static int
  513. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  514. enum v4l2_field field)
  515. {
  516. struct vivi_fh *fh = vq->priv_data;
  517. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  518. int rc, init_buffer = 0;
  519. dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
  520. BUG_ON(NULL == fh->fmt);
  521. if (fh->width < 48 || fh->width > norm_maxw() ||
  522. fh->height < 32 || fh->height > norm_maxh())
  523. return -EINVAL;
  524. buf->vb.size = fh->width*fh->height*2;
  525. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  526. return -EINVAL;
  527. if (buf->fmt != fh->fmt ||
  528. buf->vb.width != fh->width ||
  529. buf->vb.height != fh->height ||
  530. buf->vb.field != field) {
  531. buf->fmt = fh->fmt;
  532. buf->vb.width = fh->width;
  533. buf->vb.height = fh->height;
  534. buf->vb.field = field;
  535. init_buffer = 1;
  536. }
  537. if (STATE_NEEDS_INIT == buf->vb.state) {
  538. if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
  539. goto fail;
  540. }
  541. buf->vb.state = STATE_PREPARED;
  542. return 0;
  543. fail:
  544. free_buffer(vq,buf);
  545. return rc;
  546. }
  547. static void
  548. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  549. {
  550. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  551. struct vivi_fh *fh = vq->priv_data;
  552. struct vivi_dev *dev = fh->dev;
  553. struct vivi_dmaqueue *vidq = &dev->vidq;
  554. struct vivi_buffer *prev;
  555. if (!list_empty(&vidq->queued)) {
  556. dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
  557. list_add_tail(&buf->vb.queue,&vidq->queued);
  558. buf->vb.state = STATE_QUEUED;
  559. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  560. buf, buf->vb.i);
  561. } else if (list_empty(&vidq->active)) {
  562. list_add_tail(&buf->vb.queue,&vidq->active);
  563. buf->vb.state = STATE_ACTIVE;
  564. mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
  565. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  566. buf, buf->vb.i);
  567. vivi_start_thread(vidq);
  568. } else {
  569. prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
  570. if (prev->vb.width == buf->vb.width &&
  571. prev->vb.height == buf->vb.height &&
  572. prev->fmt == buf->fmt) {
  573. list_add_tail(&buf->vb.queue,&vidq->active);
  574. buf->vb.state = STATE_ACTIVE;
  575. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  576. buf, buf->vb.i);
  577. } else {
  578. list_add_tail(&buf->vb.queue,&vidq->queued);
  579. buf->vb.state = STATE_QUEUED;
  580. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  581. buf, buf->vb.i);
  582. }
  583. }
  584. }
  585. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  586. {
  587. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  588. struct vivi_fh *fh = vq->priv_data;
  589. struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
  590. struct vivi_dmaqueue *vidq = &dev->vidq;
  591. dprintk(1,"%s\n",__FUNCTION__);
  592. vivi_stop_thread(vidq);
  593. free_buffer(vq,buf);
  594. }
  595. static struct videobuf_queue_ops vivi_video_qops = {
  596. .buf_setup = buffer_setup,
  597. .buf_prepare = buffer_prepare,
  598. .buf_queue = buffer_queue,
  599. .buf_release = buffer_release,
  600. };
  601. /* ------------------------------------------------------------------
  602. IOCTL vidioc handling
  603. ------------------------------------------------------------------*/
  604. static int vidioc_querycap (struct file *file, void *priv,
  605. struct v4l2_capability *cap)
  606. {
  607. strcpy(cap->driver, "vivi");
  608. strcpy(cap->card, "vivi");
  609. cap->version = VIVI_VERSION;
  610. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  611. V4L2_CAP_STREAMING |
  612. V4L2_CAP_READWRITE;
  613. return 0;
  614. }
  615. static int vidioc_enum_fmt_cap (struct file *file, void *priv,
  616. struct v4l2_fmtdesc *f)
  617. {
  618. if (f->index > 0)
  619. return -EINVAL;
  620. strlcpy(f->description,format.name,sizeof(f->description));
  621. f->pixelformat = format.fourcc;
  622. return 0;
  623. }
  624. static int vidioc_g_fmt_cap (struct file *file, void *priv,
  625. struct v4l2_format *f)
  626. {
  627. struct vivi_fh *fh=priv;
  628. f->fmt.pix.width = fh->width;
  629. f->fmt.pix.height = fh->height;
  630. f->fmt.pix.field = fh->vb_vidq.field;
  631. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  632. f->fmt.pix.bytesperline =
  633. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  634. f->fmt.pix.sizeimage =
  635. f->fmt.pix.height * f->fmt.pix.bytesperline;
  636. return (0);
  637. }
  638. static int vidioc_try_fmt_cap (struct file *file, void *priv,
  639. struct v4l2_format *f)
  640. {
  641. struct vivi_fmt *fmt;
  642. enum v4l2_field field;
  643. unsigned int maxw, maxh;
  644. if (format.fourcc != f->fmt.pix.pixelformat) {
  645. dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
  646. "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
  647. return -EINVAL;
  648. }
  649. fmt=&format;
  650. field = f->fmt.pix.field;
  651. if (field == V4L2_FIELD_ANY) {
  652. field=V4L2_FIELD_INTERLACED;
  653. } else if (V4L2_FIELD_INTERLACED != field) {
  654. dprintk(1,"Field type invalid.\n");
  655. return -EINVAL;
  656. }
  657. maxw = norm_maxw();
  658. maxh = norm_maxh();
  659. f->fmt.pix.field = field;
  660. if (f->fmt.pix.height < 32)
  661. f->fmt.pix.height = 32;
  662. if (f->fmt.pix.height > maxh)
  663. f->fmt.pix.height = maxh;
  664. if (f->fmt.pix.width < 48)
  665. f->fmt.pix.width = 48;
  666. if (f->fmt.pix.width > maxw)
  667. f->fmt.pix.width = maxw;
  668. f->fmt.pix.width &= ~0x03;
  669. f->fmt.pix.bytesperline =
  670. (f->fmt.pix.width * fmt->depth) >> 3;
  671. f->fmt.pix.sizeimage =
  672. f->fmt.pix.height * f->fmt.pix.bytesperline;
  673. return 0;
  674. }
  675. /*FIXME: This seems to be generic enough to be at videodev2 */
  676. static int vidioc_s_fmt_cap (struct file *file, void *priv,
  677. struct v4l2_format *f)
  678. {
  679. struct vivi_fh *fh=priv;
  680. int ret = vidioc_try_fmt_cap(file,fh,f);
  681. if (ret < 0)
  682. return (ret);
  683. fh->fmt = &format;
  684. fh->width = f->fmt.pix.width;
  685. fh->height = f->fmt.pix.height;
  686. fh->vb_vidq.field = f->fmt.pix.field;
  687. fh->type = f->type;
  688. return (0);
  689. }
  690. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  691. {
  692. struct vivi_fh *fh=priv;
  693. return (videobuf_reqbufs(&fh->vb_vidq, p));
  694. }
  695. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  696. {
  697. struct vivi_fh *fh=priv;
  698. return (videobuf_querybuf(&fh->vb_vidq, p));
  699. }
  700. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  701. {
  702. struct vivi_fh *fh=priv;
  703. return (videobuf_qbuf(&fh->vb_vidq, p));
  704. }
  705. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  706. {
  707. struct vivi_fh *fh=priv;
  708. return (videobuf_dqbuf(&fh->vb_vidq, p,
  709. file->f_flags & O_NONBLOCK));
  710. }
  711. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  712. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  713. {
  714. struct vivi_fh *fh=priv;
  715. return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
  716. }
  717. #endif
  718. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  719. {
  720. struct vivi_fh *fh=priv;
  721. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  722. return -EINVAL;
  723. if (i != fh->type)
  724. return -EINVAL;
  725. return videobuf_streamon(&fh->vb_vidq);
  726. }
  727. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  728. {
  729. struct vivi_fh *fh=priv;
  730. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  731. return -EINVAL;
  732. if (i != fh->type)
  733. return -EINVAL;
  734. return videobuf_streamoff(&fh->vb_vidq);
  735. }
  736. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
  737. {
  738. return 0;
  739. }
  740. /* only one input in this sample driver */
  741. static int vidioc_enum_input (struct file *file, void *priv,
  742. struct v4l2_input *inp)
  743. {
  744. if (inp->index != 0)
  745. return -EINVAL;
  746. inp->type = V4L2_INPUT_TYPE_CAMERA;
  747. inp->std = V4L2_STD_NTSC_M;
  748. strcpy(inp->name,"Camera");
  749. return (0);
  750. }
  751. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  752. {
  753. *i = 0;
  754. return (0);
  755. }
  756. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  757. {
  758. if (i > 0)
  759. return -EINVAL;
  760. return (0);
  761. }
  762. /* --- controls ---------------------------------------------- */
  763. static int vidioc_queryctrl (struct file *file, void *priv,
  764. struct v4l2_queryctrl *qc)
  765. {
  766. int i;
  767. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  768. if (qc->id && qc->id == vivi_qctrl[i].id) {
  769. memcpy(qc, &(vivi_qctrl[i]),
  770. sizeof(*qc));
  771. return (0);
  772. }
  773. return -EINVAL;
  774. }
  775. static int vidioc_g_ctrl (struct file *file, void *priv,
  776. struct v4l2_control *ctrl)
  777. {
  778. int i;
  779. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  780. if (ctrl->id == vivi_qctrl[i].id) {
  781. ctrl->value=qctl_regs[i];
  782. return (0);
  783. }
  784. return -EINVAL;
  785. }
  786. static int vidioc_s_ctrl (struct file *file, void *priv,
  787. struct v4l2_control *ctrl)
  788. {
  789. int i;
  790. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  791. if (ctrl->id == vivi_qctrl[i].id) {
  792. if (ctrl->value <
  793. vivi_qctrl[i].minimum
  794. || ctrl->value >
  795. vivi_qctrl[i].maximum) {
  796. return (-ERANGE);
  797. }
  798. qctl_regs[i]=ctrl->value;
  799. return (0);
  800. }
  801. return -EINVAL;
  802. }
  803. /* ------------------------------------------------------------------
  804. File operations for the device
  805. ------------------------------------------------------------------*/
  806. #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
  807. static int vivi_open(struct inode *inode, struct file *file)
  808. {
  809. int minor = iminor(inode);
  810. struct vivi_dev *dev;
  811. struct vivi_fh *fh;
  812. int i;
  813. printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
  814. list_for_each_entry(dev, &vivi_devlist, vivi_devlist)
  815. if (dev->vfd.minor == minor)
  816. goto found;
  817. return -ENODEV;
  818. found:
  819. /* If more than one user, mutex should be added */
  820. dev->users++;
  821. dprintk(1, "open minor=%d type=%s users=%d\n", minor,
  822. v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
  823. /* allocate + initialize per filehandle data */
  824. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  825. if (NULL == fh) {
  826. dev->users--;
  827. return -ENOMEM;
  828. }
  829. file->private_data = fh;
  830. fh->dev = dev;
  831. fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  832. fh->fmt = &format;
  833. fh->width = 640;
  834. fh->height = 480;
  835. /* Put all controls at a sane state */
  836. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  837. qctl_regs[i] =vivi_qctrl[i].default_value;
  838. dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
  839. (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
  840. dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
  841. dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
  842. /* Resets frame counters */
  843. dev->h=0;
  844. dev->m=0;
  845. dev->s=0;
  846. dev->us=0;
  847. dev->jiffies=jiffies;
  848. sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
  849. dev->h,dev->m,dev->s,(dev->us+500)/1000);
  850. videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
  851. NULL, NULL,
  852. fh->type,
  853. V4L2_FIELD_INTERLACED,
  854. sizeof(struct vivi_buffer),fh);
  855. return 0;
  856. }
  857. static ssize_t
  858. vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  859. {
  860. struct vivi_fh *fh = file->private_data;
  861. if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  862. return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
  863. file->f_flags & O_NONBLOCK);
  864. }
  865. return 0;
  866. }
  867. static unsigned int
  868. vivi_poll(struct file *file, struct poll_table_struct *wait)
  869. {
  870. struct vivi_fh *fh = file->private_data;
  871. struct videobuf_queue *q = &fh->vb_vidq;
  872. dprintk(1,"%s\n",__FUNCTION__);
  873. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  874. return POLLERR;
  875. return videobuf_poll_stream(file, q, wait);
  876. }
  877. static int vivi_release(struct inode *inode, struct file *file)
  878. {
  879. struct vivi_fh *fh = file->private_data;
  880. struct vivi_dev *dev = fh->dev;
  881. struct vivi_dmaqueue *vidq = &dev->vidq;
  882. int minor = iminor(inode);
  883. vivi_stop_thread(vidq);
  884. videobuf_mmap_free(&fh->vb_vidq);
  885. kfree (fh);
  886. dev->users--;
  887. printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
  888. return 0;
  889. }
  890. static int
  891. vivi_mmap(struct file *file, struct vm_area_struct * vma)
  892. {
  893. struct vivi_fh *fh = file->private_data;
  894. int ret;
  895. dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
  896. ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
  897. dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
  898. (unsigned long)vma->vm_start,
  899. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  900. ret);
  901. return ret;
  902. }
  903. static const struct file_operations vivi_fops = {
  904. .owner = THIS_MODULE,
  905. .open = vivi_open,
  906. .release = vivi_release,
  907. .read = vivi_read,
  908. .poll = vivi_poll,
  909. .ioctl = video_ioctl2, /* V4L2 ioctl handler */
  910. .mmap = vivi_mmap,
  911. .llseek = no_llseek,
  912. };
  913. static struct video_device vivi = {
  914. .name = "vivi",
  915. .type = VID_TYPE_CAPTURE,
  916. .hardware = 0,
  917. .fops = &vivi_fops,
  918. .minor = -1,
  919. // .release = video_device_release,
  920. .vidioc_querycap = vidioc_querycap,
  921. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  922. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  923. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  924. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  925. .vidioc_reqbufs = vidioc_reqbufs,
  926. .vidioc_querybuf = vidioc_querybuf,
  927. .vidioc_qbuf = vidioc_qbuf,
  928. .vidioc_dqbuf = vidioc_dqbuf,
  929. .vidioc_s_std = vidioc_s_std,
  930. .vidioc_enum_input = vidioc_enum_input,
  931. .vidioc_g_input = vidioc_g_input,
  932. .vidioc_s_input = vidioc_s_input,
  933. .vidioc_queryctrl = vidioc_queryctrl,
  934. .vidioc_g_ctrl = vidioc_g_ctrl,
  935. .vidioc_s_ctrl = vidioc_s_ctrl,
  936. .vidioc_streamon = vidioc_streamon,
  937. .vidioc_streamoff = vidioc_streamoff,
  938. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  939. .vidiocgmbuf = vidiocgmbuf,
  940. #endif
  941. .tvnorms = V4L2_STD_NTSC_M,
  942. .current_norm = V4L2_STD_NTSC_M,
  943. };
  944. /* -----------------------------------------------------------------
  945. Initialization and module stuff
  946. ------------------------------------------------------------------*/
  947. static int __init vivi_init(void)
  948. {
  949. int ret;
  950. struct vivi_dev *dev;
  951. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  952. if (NULL == dev)
  953. return -ENOMEM;
  954. list_add_tail(&dev->vivi_devlist,&vivi_devlist);
  955. /* init video dma queues */
  956. INIT_LIST_HEAD(&dev->vidq.active);
  957. INIT_LIST_HEAD(&dev->vidq.queued);
  958. init_waitqueue_head(&dev->vidq.wq);
  959. /* initialize locks */
  960. mutex_init(&dev->lock);
  961. dev->vidq.timeout.function = vivi_vid_timeout;
  962. dev->vidq.timeout.data = (unsigned long)dev;
  963. init_timer(&dev->vidq.timeout);
  964. ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
  965. printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
  966. return ret;
  967. }
  968. static void __exit vivi_exit(void)
  969. {
  970. struct vivi_dev *h;
  971. struct list_head *list;
  972. while (!list_empty(&vivi_devlist)) {
  973. list = vivi_devlist.next;
  974. list_del(list);
  975. h = list_entry(list, struct vivi_dev, vivi_devlist);
  976. kfree (h);
  977. }
  978. video_unregister_device(&vivi);
  979. }
  980. module_init(vivi_init);
  981. module_exit(vivi_exit);
  982. MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
  983. MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
  984. MODULE_LICENSE("Dual BSD/GPL");
  985. module_param(video_nr, int, 0);
  986. module_param_named(debug,vivi.debug, int, 0644);
  987. MODULE_PARM_DESC(debug,"activates debug info");
  988. module_param(vid_limit,int,0644);
  989. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");