vivi.c 31 KB

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