vivi.c 27 KB

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