vivi.c 27 KB

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