vivi.c 30 KB

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