vivi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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, 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 = STATE_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. struct list_head *item;
  428. dprintk(1,"%s dma_q=0x%08lx\n",__FUNCTION__,(unsigned long)dma_q);
  429. if (!list_empty(&dma_q->active)) {
  430. buf = list_entry(dma_q->active.next, struct vivi_buffer, vb.queue);
  431. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  432. buf, buf->vb.i);
  433. dprintk(1,"Restarting video dma\n");
  434. vivi_stop_thread(dma_q);
  435. // vivi_start_thread(dma_q);
  436. /* cancel all outstanding capture / vbi requests */
  437. list_for_each(item,&dma_q->active) {
  438. buf = list_entry(item, struct vivi_buffer, vb.queue);
  439. list_del(&buf->vb.queue);
  440. buf->vb.state = STATE_ERROR;
  441. wake_up(&buf->vb.done);
  442. }
  443. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  444. return 0;
  445. }
  446. prev = NULL;
  447. for (;;) {
  448. if (list_empty(&dma_q->queued))
  449. return 0;
  450. buf = list_entry(dma_q->queued.next, struct vivi_buffer, vb.queue);
  451. if (NULL == prev) {
  452. list_del(&buf->vb.queue);
  453. list_add_tail(&buf->vb.queue,&dma_q->active);
  454. dprintk(1,"Restarting video dma\n");
  455. vivi_stop_thread(dma_q);
  456. vivi_start_thread(dma_q);
  457. buf->vb.state = STATE_ACTIVE;
  458. mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
  459. dprintk(2,"[%p/%d] restart_queue - first active\n",
  460. buf,buf->vb.i);
  461. } else if (prev->vb.width == buf->vb.width &&
  462. prev->vb.height == buf->vb.height &&
  463. prev->fmt == buf->fmt) {
  464. list_del(&buf->vb.queue);
  465. list_add_tail(&buf->vb.queue,&dma_q->active);
  466. buf->vb.state = STATE_ACTIVE;
  467. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  468. buf,buf->vb.i);
  469. } else {
  470. return 0;
  471. }
  472. prev = buf;
  473. }
  474. }
  475. static void vivi_vid_timeout(unsigned long data)
  476. {
  477. struct vivi_dev *dev = (struct vivi_dev*)data;
  478. struct vivi_dmaqueue *vidq = &dev->vidq;
  479. struct vivi_buffer *buf;
  480. while (!list_empty(&vidq->active)) {
  481. buf = list_entry(vidq->active.next, struct vivi_buffer, vb.queue);
  482. list_del(&buf->vb.queue);
  483. buf->vb.state = STATE_ERROR;
  484. wake_up(&buf->vb.done);
  485. printk("vivi/0: [%p/%d] timeout\n", buf, buf->vb.i);
  486. }
  487. restart_video_queue(vidq);
  488. }
  489. /* ------------------------------------------------------------------
  490. Videobuf operations
  491. ------------------------------------------------------------------*/
  492. static int
  493. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  494. {
  495. struct vivi_fh *fh = vq->priv_data;
  496. *size = fh->width*fh->height*2;
  497. if (0 == *count)
  498. *count = 32;
  499. while (*size * *count > vid_limit * 1024 * 1024)
  500. (*count)--;
  501. dprintk(1,"%s, count=%d, size=%d\n",__FUNCTION__,*count, *size);
  502. return 0;
  503. }
  504. static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
  505. {
  506. dprintk(1,"%s\n",__FUNCTION__);
  507. if (in_interrupt())
  508. BUG();
  509. videobuf_waiton(&buf->vb,0,0);
  510. videobuf_vmalloc_free(&buf->vb);
  511. buf->vb.state = STATE_NEEDS_INIT;
  512. }
  513. #define norm_maxw() 1024
  514. #define norm_maxh() 768
  515. static int
  516. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  517. enum v4l2_field field)
  518. {
  519. struct vivi_fh *fh = vq->priv_data;
  520. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  521. int rc, init_buffer = 0;
  522. dprintk(1,"%s, field=%d\n",__FUNCTION__,field);
  523. BUG_ON(NULL == fh->fmt);
  524. if (fh->width < 48 || fh->width > norm_maxw() ||
  525. fh->height < 32 || fh->height > norm_maxh())
  526. return -EINVAL;
  527. buf->vb.size = fh->width*fh->height*2;
  528. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  529. return -EINVAL;
  530. if (buf->fmt != fh->fmt ||
  531. buf->vb.width != fh->width ||
  532. buf->vb.height != fh->height ||
  533. buf->vb.field != field) {
  534. buf->fmt = fh->fmt;
  535. buf->vb.width = fh->width;
  536. buf->vb.height = fh->height;
  537. buf->vb.field = field;
  538. init_buffer = 1;
  539. }
  540. if (STATE_NEEDS_INIT == buf->vb.state) {
  541. if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
  542. goto fail;
  543. }
  544. buf->vb.state = STATE_PREPARED;
  545. return 0;
  546. fail:
  547. free_buffer(vq,buf);
  548. return rc;
  549. }
  550. static void
  551. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  552. {
  553. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  554. struct vivi_fh *fh = vq->priv_data;
  555. struct vivi_dev *dev = fh->dev;
  556. struct vivi_dmaqueue *vidq = &dev->vidq;
  557. struct vivi_buffer *prev;
  558. if (!list_empty(&vidq->queued)) {
  559. dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue);
  560. list_add_tail(&buf->vb.queue,&vidq->queued);
  561. buf->vb.state = STATE_QUEUED;
  562. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  563. buf, buf->vb.i);
  564. } else if (list_empty(&vidq->active)) {
  565. list_add_tail(&buf->vb.queue,&vidq->active);
  566. buf->vb.state = STATE_ACTIVE;
  567. mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
  568. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  569. buf, buf->vb.i);
  570. vivi_start_thread(vidq);
  571. } else {
  572. prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue);
  573. if (prev->vb.width == buf->vb.width &&
  574. prev->vb.height == buf->vb.height &&
  575. prev->fmt == buf->fmt) {
  576. list_add_tail(&buf->vb.queue,&vidq->active);
  577. buf->vb.state = STATE_ACTIVE;
  578. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  579. buf, buf->vb.i);
  580. } else {
  581. list_add_tail(&buf->vb.queue,&vidq->queued);
  582. buf->vb.state = STATE_QUEUED;
  583. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  584. buf, buf->vb.i);
  585. }
  586. }
  587. }
  588. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  589. {
  590. struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb);
  591. struct vivi_fh *fh = vq->priv_data;
  592. struct vivi_dev *dev = (struct vivi_dev*)fh->dev;
  593. struct vivi_dmaqueue *vidq = &dev->vidq;
  594. dprintk(1,"%s\n",__FUNCTION__);
  595. vivi_stop_thread(vidq);
  596. free_buffer(vq,buf);
  597. }
  598. static struct videobuf_queue_ops vivi_video_qops = {
  599. .buf_setup = buffer_setup,
  600. .buf_prepare = buffer_prepare,
  601. .buf_queue = buffer_queue,
  602. .buf_release = buffer_release,
  603. };
  604. /* ------------------------------------------------------------------
  605. IOCTL handling
  606. ------------------------------------------------------------------*/
  607. static int res_get(struct vivi_dev *dev, struct vivi_fh *fh)
  608. {
  609. /* is it free? */
  610. mutex_lock(&dev->lock);
  611. if (dev->resources) {
  612. /* no, someone else uses it */
  613. mutex_unlock(&dev->lock);
  614. return 0;
  615. }
  616. /* it's free, grab it */
  617. dev->resources =1;
  618. dprintk(1,"res: get\n");
  619. mutex_unlock(&dev->lock);
  620. return 1;
  621. }
  622. static int res_locked(struct vivi_dev *dev)
  623. {
  624. return (dev->resources);
  625. }
  626. static void res_free(struct vivi_dev *dev, struct vivi_fh *fh)
  627. {
  628. mutex_lock(&dev->lock);
  629. dev->resources = 0;
  630. dprintk(1,"res: put\n");
  631. mutex_lock(&dev->lock);
  632. }
  633. /* ------------------------------------------------------------------
  634. IOCTL vidioc handling
  635. ------------------------------------------------------------------*/
  636. static int vidioc_querycap (struct file *file, void *priv,
  637. struct v4l2_capability *cap)
  638. {
  639. strcpy(cap->driver, "vivi");
  640. strcpy(cap->card, "vivi");
  641. cap->version = VIVI_VERSION;
  642. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  643. V4L2_CAP_STREAMING |
  644. V4L2_CAP_READWRITE;
  645. return 0;
  646. }
  647. static int vidioc_enum_fmt_cap (struct file *file, void *priv,
  648. struct v4l2_fmtdesc *f)
  649. {
  650. if (f->index > 0)
  651. return -EINVAL;
  652. strlcpy(f->description,format.name,sizeof(f->description));
  653. f->pixelformat = format.fourcc;
  654. return 0;
  655. }
  656. static int vidioc_g_fmt_cap (struct file *file, void *priv,
  657. struct v4l2_format *f)
  658. {
  659. struct vivi_fh *fh=priv;
  660. f->fmt.pix.width = fh->width;
  661. f->fmt.pix.height = fh->height;
  662. f->fmt.pix.field = fh->vb_vidq.field;
  663. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  664. f->fmt.pix.bytesperline =
  665. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  666. f->fmt.pix.sizeimage =
  667. f->fmt.pix.height * f->fmt.pix.bytesperline;
  668. return (0);
  669. }
  670. static int vidioc_try_fmt_cap (struct file *file, void *priv,
  671. struct v4l2_format *f)
  672. {
  673. struct vivi_fmt *fmt;
  674. enum v4l2_field field;
  675. unsigned int maxw, maxh;
  676. if (format.fourcc != f->fmt.pix.pixelformat) {
  677. dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts "
  678. "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc);
  679. return -EINVAL;
  680. }
  681. fmt=&format;
  682. field = f->fmt.pix.field;
  683. if (field == V4L2_FIELD_ANY) {
  684. field=V4L2_FIELD_INTERLACED;
  685. } else if (V4L2_FIELD_INTERLACED != field) {
  686. dprintk(1,"Field type invalid.\n");
  687. return -EINVAL;
  688. }
  689. maxw = norm_maxw();
  690. maxh = norm_maxh();
  691. f->fmt.pix.field = field;
  692. if (f->fmt.pix.height < 32)
  693. f->fmt.pix.height = 32;
  694. if (f->fmt.pix.height > maxh)
  695. f->fmt.pix.height = maxh;
  696. if (f->fmt.pix.width < 48)
  697. f->fmt.pix.width = 48;
  698. if (f->fmt.pix.width > maxw)
  699. f->fmt.pix.width = maxw;
  700. f->fmt.pix.width &= ~0x03;
  701. f->fmt.pix.bytesperline =
  702. (f->fmt.pix.width * fmt->depth) >> 3;
  703. f->fmt.pix.sizeimage =
  704. f->fmt.pix.height * f->fmt.pix.bytesperline;
  705. return 0;
  706. }
  707. /*FIXME: This seems to be generic enough to be at videodev2 */
  708. static int vidioc_s_fmt_cap (struct file *file, void *priv,
  709. struct v4l2_format *f)
  710. {
  711. struct vivi_fh *fh=priv;
  712. int ret = vidioc_try_fmt_cap(file,fh,f);
  713. if (ret < 0)
  714. return (ret);
  715. fh->fmt = &format;
  716. fh->width = f->fmt.pix.width;
  717. fh->height = f->fmt.pix.height;
  718. fh->vb_vidq.field = f->fmt.pix.field;
  719. fh->type = f->type;
  720. return (0);
  721. }
  722. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  723. {
  724. struct vivi_fh *fh=priv;
  725. return (videobuf_reqbufs(&fh->vb_vidq, p));
  726. }
  727. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  728. {
  729. struct vivi_fh *fh=priv;
  730. return (videobuf_querybuf(&fh->vb_vidq, p));
  731. }
  732. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  733. {
  734. struct vivi_fh *fh=priv;
  735. return (videobuf_qbuf(&fh->vb_vidq, p));
  736. }
  737. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  738. {
  739. struct vivi_fh *fh=priv;
  740. return (videobuf_dqbuf(&fh->vb_vidq, p,
  741. file->f_flags & O_NONBLOCK));
  742. }
  743. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  744. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  745. {
  746. struct vivi_fh *fh=priv;
  747. return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
  748. }
  749. #endif
  750. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  751. {
  752. struct vivi_fh *fh=priv;
  753. struct vivi_dev *dev = fh->dev;
  754. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  755. return -EINVAL;
  756. if (i != fh->type)
  757. return -EINVAL;
  758. if (!res_get(dev,fh))
  759. return -EBUSY;
  760. return (videobuf_streamon(&fh->vb_vidq));
  761. }
  762. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  763. {
  764. struct vivi_fh *fh=priv;
  765. struct vivi_dev *dev = fh->dev;
  766. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  767. return -EINVAL;
  768. if (i != fh->type)
  769. return -EINVAL;
  770. videobuf_streamoff(&fh->vb_vidq);
  771. res_free(dev,fh);
  772. return (0);
  773. }
  774. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i)
  775. {
  776. return 0;
  777. }
  778. /* only one input in this sample driver */
  779. static int vidioc_enum_input (struct file *file, void *priv,
  780. struct v4l2_input *inp)
  781. {
  782. if (inp->index != 0)
  783. return -EINVAL;
  784. inp->type = V4L2_INPUT_TYPE_CAMERA;
  785. inp->std = V4L2_STD_NTSC_M;
  786. strcpy(inp->name,"Camera");
  787. return (0);
  788. }
  789. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  790. {
  791. *i = 0;
  792. return (0);
  793. }
  794. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  795. {
  796. if (i > 0)
  797. return -EINVAL;
  798. return (0);
  799. }
  800. /* --- controls ---------------------------------------------- */
  801. static int vidioc_queryctrl (struct file *file, void *priv,
  802. struct v4l2_queryctrl *qc)
  803. {
  804. int i;
  805. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  806. if (qc->id && qc->id == vivi_qctrl[i].id) {
  807. memcpy(qc, &(vivi_qctrl[i]),
  808. sizeof(*qc));
  809. return (0);
  810. }
  811. return -EINVAL;
  812. }
  813. static int vidioc_g_ctrl (struct file *file, void *priv,
  814. struct v4l2_control *ctrl)
  815. {
  816. int i;
  817. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  818. if (ctrl->id == vivi_qctrl[i].id) {
  819. ctrl->value=qctl_regs[i];
  820. return (0);
  821. }
  822. return -EINVAL;
  823. }
  824. static int vidioc_s_ctrl (struct file *file, void *priv,
  825. struct v4l2_control *ctrl)
  826. {
  827. int i;
  828. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  829. if (ctrl->id == vivi_qctrl[i].id) {
  830. if (ctrl->value <
  831. vivi_qctrl[i].minimum
  832. || ctrl->value >
  833. vivi_qctrl[i].maximum) {
  834. return (-ERANGE);
  835. }
  836. qctl_regs[i]=ctrl->value;
  837. return (0);
  838. }
  839. return -EINVAL;
  840. }
  841. /* ------------------------------------------------------------------
  842. File operations for the device
  843. ------------------------------------------------------------------*/
  844. #define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
  845. static int vivi_open(struct inode *inode, struct file *file)
  846. {
  847. int minor = iminor(inode);
  848. struct vivi_dev *h,*dev = NULL;
  849. struct vivi_fh *fh;
  850. struct list_head *list;
  851. enum v4l2_buf_type type = 0;
  852. int i;
  853. printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor);
  854. list_for_each(list,&vivi_devlist) {
  855. h = list_entry(list, struct vivi_dev, vivi_devlist);
  856. if (h->vfd.minor == minor) {
  857. dev = h;
  858. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  859. }
  860. }
  861. if (NULL == dev)
  862. return -ENODEV;
  863. /* If more than one user, mutex should be added */
  864. dev->users++;
  865. dprintk(1,"open minor=%d type=%s users=%d\n",
  866. minor,v4l2_type_names[type],dev->users);
  867. /* allocate + initialize per filehandle data */
  868. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  869. if (NULL == fh) {
  870. dev->users--;
  871. return -ENOMEM;
  872. }
  873. file->private_data = fh;
  874. fh->dev = dev;
  875. fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  876. fh->fmt = &format;
  877. fh->width = 640;
  878. fh->height = 480;
  879. /* Put all controls at a sane state */
  880. for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
  881. qctl_regs[i] =vivi_qctrl[i].default_value;
  882. dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
  883. (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
  884. dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued));
  885. dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active));
  886. /* Resets frame counters */
  887. dev->h=0;
  888. dev->m=0;
  889. dev->s=0;
  890. dev->us=0;
  891. dev->jiffies=jiffies;
  892. sprintf(dev->timestr,"%02d:%02d:%02d:%03d",
  893. dev->h,dev->m,dev->s,(dev->us+500)/1000);
  894. videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
  895. NULL, NULL,
  896. fh->type,
  897. V4L2_FIELD_INTERLACED,
  898. sizeof(struct vivi_buffer),fh);
  899. return 0;
  900. }
  901. static ssize_t
  902. vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  903. {
  904. struct vivi_fh *fh = file->private_data;
  905. if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  906. if (res_locked(fh->dev))
  907. return -EBUSY;
  908. return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
  909. file->f_flags & O_NONBLOCK);
  910. }
  911. return 0;
  912. }
  913. static unsigned int
  914. vivi_poll(struct file *file, struct poll_table_struct *wait)
  915. {
  916. struct vivi_fh *fh = file->private_data;
  917. struct vivi_buffer *buf;
  918. struct videobuf_queue *q = &fh->vb_vidq;
  919. dprintk(1,"%s\n",__FUNCTION__);
  920. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  921. return POLLERR;
  922. return videobuf_poll_stream(file, q, wait);
  923. }
  924. static int vivi_release(struct inode *inode, struct file *file)
  925. {
  926. struct vivi_fh *fh = file->private_data;
  927. struct vivi_dev *dev = fh->dev;
  928. struct vivi_dmaqueue *vidq = &dev->vidq;
  929. int minor = iminor(inode);
  930. vivi_stop_thread(vidq);
  931. videobuf_mmap_free(&fh->vb_vidq);
  932. kfree (fh);
  933. dev->users--;
  934. printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users);
  935. return 0;
  936. }
  937. static int
  938. vivi_mmap(struct file *file, struct vm_area_struct * vma)
  939. {
  940. struct vivi_fh *fh = file->private_data;
  941. int ret;
  942. dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma);
  943. ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
  944. dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n",
  945. (unsigned long)vma->vm_start,
  946. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  947. ret);
  948. return ret;
  949. }
  950. static const struct file_operations vivi_fops = {
  951. .owner = THIS_MODULE,
  952. .open = vivi_open,
  953. .release = vivi_release,
  954. .read = vivi_read,
  955. .poll = vivi_poll,
  956. .ioctl = video_ioctl2, /* V4L2 ioctl handler */
  957. .mmap = vivi_mmap,
  958. .llseek = no_llseek,
  959. };
  960. static struct video_device vivi = {
  961. .name = "vivi",
  962. .type = VID_TYPE_CAPTURE,
  963. .hardware = 0,
  964. .fops = &vivi_fops,
  965. .minor = -1,
  966. // .release = video_device_release,
  967. .vidioc_querycap = vidioc_querycap,
  968. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  969. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  970. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  971. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  972. .vidioc_reqbufs = vidioc_reqbufs,
  973. .vidioc_querybuf = vidioc_querybuf,
  974. .vidioc_qbuf = vidioc_qbuf,
  975. .vidioc_dqbuf = vidioc_dqbuf,
  976. .vidioc_s_std = vidioc_s_std,
  977. .vidioc_enum_input = vidioc_enum_input,
  978. .vidioc_g_input = vidioc_g_input,
  979. .vidioc_s_input = vidioc_s_input,
  980. .vidioc_queryctrl = vidioc_queryctrl,
  981. .vidioc_g_ctrl = vidioc_g_ctrl,
  982. .vidioc_s_ctrl = vidioc_s_ctrl,
  983. .vidioc_streamon = vidioc_streamon,
  984. .vidioc_streamoff = vidioc_streamoff,
  985. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  986. .vidiocgmbuf = vidiocgmbuf,
  987. #endif
  988. .tvnorms = V4L2_STD_NTSC_M,
  989. .current_norm = V4L2_STD_NTSC_M,
  990. };
  991. /* -----------------------------------------------------------------
  992. Initialization and module stuff
  993. ------------------------------------------------------------------*/
  994. static int __init vivi_init(void)
  995. {
  996. int ret;
  997. struct vivi_dev *dev;
  998. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  999. if (NULL == dev)
  1000. return -ENOMEM;
  1001. list_add_tail(&dev->vivi_devlist,&vivi_devlist);
  1002. /* init video dma queues */
  1003. INIT_LIST_HEAD(&dev->vidq.active);
  1004. INIT_LIST_HEAD(&dev->vidq.queued);
  1005. init_waitqueue_head(&dev->vidq.wq);
  1006. /* initialize locks */
  1007. mutex_init(&dev->lock);
  1008. dev->vidq.timeout.function = vivi_vid_timeout;
  1009. dev->vidq.timeout.data = (unsigned long)dev;
  1010. init_timer(&dev->vidq.timeout);
  1011. ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr);
  1012. printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret);
  1013. return ret;
  1014. }
  1015. static void __exit vivi_exit(void)
  1016. {
  1017. struct vivi_dev *h;
  1018. struct list_head *list;
  1019. while (!list_empty(&vivi_devlist)) {
  1020. list = vivi_devlist.next;
  1021. list_del(list);
  1022. h = list_entry(list, struct vivi_dev, vivi_devlist);
  1023. kfree (h);
  1024. }
  1025. video_unregister_device(&vivi);
  1026. }
  1027. module_init(vivi_init);
  1028. module_exit(vivi_exit);
  1029. MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
  1030. MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
  1031. MODULE_LICENSE("Dual BSD/GPL");
  1032. module_param(video_nr, int, 0);
  1033. module_param_named(debug,vivi.debug, int, 0644);
  1034. MODULE_PARM_DESC(debug,"activates debug info");
  1035. module_param(vid_limit,int,0644);
  1036. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");