cx25821-video.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
  7. * Parts adapted/taken from Eduardo Moscoso Rubino
  8. * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. *
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include "cx25821-video.h"
  28. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  29. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  30. MODULE_LICENSE("GPL");
  31. static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
  32. module_param_array(video_nr, int, NULL, 0444);
  33. MODULE_PARM_DESC(video_nr, "video device numbers");
  34. static unsigned int video_debug = VIDEO_DEBUG;
  35. module_param(video_debug, int, 0644);
  36. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  37. static unsigned int irq_debug;
  38. module_param(irq_debug, int, 0644);
  39. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  40. static unsigned int vid_limit = 16;
  41. module_param(vid_limit, int, 0644);
  42. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  43. #define FORMAT_FLAGS_PACKED 0x01
  44. static const struct cx25821_fmt formats[] = {
  45. {
  46. .name = "4:1:1, packed, Y41P",
  47. .fourcc = V4L2_PIX_FMT_Y41P,
  48. .depth = 12,
  49. .flags = FORMAT_FLAGS_PACKED,
  50. }, {
  51. .name = "4:2:2, packed, YUYV",
  52. .fourcc = V4L2_PIX_FMT_YUYV,
  53. .depth = 16,
  54. .flags = FORMAT_FLAGS_PACKED,
  55. },
  56. };
  57. static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  58. {
  59. unsigned int i;
  60. for (i = 0; i < ARRAY_SIZE(formats); i++)
  61. if (formats[i].fourcc == fourcc)
  62. return formats + i;
  63. return NULL;
  64. }
  65. void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
  66. u32 count)
  67. {
  68. struct cx25821_buffer *buf;
  69. int bc;
  70. for (bc = 0;; bc++) {
  71. if (list_empty(&q->active)) {
  72. dprintk(1, "bc=%d (=0: active empty)\n", bc);
  73. break;
  74. }
  75. buf = list_entry(q->active.next, struct cx25821_buffer,
  76. vb.queue);
  77. /* count comes from the hw and it is 16bit wide --
  78. * this trick handles wrap-arounds correctly for
  79. * up to 32767 buffers in flight... */
  80. if ((s16) (count - buf->count) < 0)
  81. break;
  82. v4l2_get_timestamp(&buf->vb.ts);
  83. buf->vb.state = VIDEOBUF_DONE;
  84. list_del(&buf->vb.queue);
  85. wake_up(&buf->vb.done);
  86. }
  87. if (list_empty(&q->active))
  88. del_timer(&q->timeout);
  89. else
  90. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  91. if (bc != 1)
  92. pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
  93. }
  94. int cx25821_start_video_dma(struct cx25821_dev *dev,
  95. struct cx25821_dmaqueue *q,
  96. struct cx25821_buffer *buf,
  97. const struct sram_channel *channel)
  98. {
  99. int tmp = 0;
  100. /* setup fifo + format */
  101. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  102. /* reset counter */
  103. cx_write(channel->gpcnt_ctl, 3);
  104. q->count = 1;
  105. /* enable irq */
  106. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  107. cx_set(channel->int_msk, 0x11);
  108. /* start dma */
  109. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  110. /* make sure upstream setting if any is reversed */
  111. tmp = cx_read(VID_CH_MODE_SEL);
  112. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  113. return 0;
  114. }
  115. static int cx25821_restart_video_queue(struct cx25821_dev *dev,
  116. struct cx25821_dmaqueue *q,
  117. const struct sram_channel *channel)
  118. {
  119. struct cx25821_buffer *buf, *prev;
  120. struct list_head *item;
  121. if (!list_empty(&q->active)) {
  122. buf = list_entry(q->active.next, struct cx25821_buffer,
  123. vb.queue);
  124. cx25821_start_video_dma(dev, q, buf, channel);
  125. list_for_each(item, &q->active) {
  126. buf = list_entry(item, struct cx25821_buffer, vb.queue);
  127. buf->count = q->count++;
  128. }
  129. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  130. return 0;
  131. }
  132. prev = NULL;
  133. for (;;) {
  134. if (list_empty(&q->queued))
  135. return 0;
  136. buf = list_entry(q->queued.next, struct cx25821_buffer,
  137. vb.queue);
  138. if (NULL == prev) {
  139. list_move_tail(&buf->vb.queue, &q->active);
  140. cx25821_start_video_dma(dev, q, buf, channel);
  141. buf->vb.state = VIDEOBUF_ACTIVE;
  142. buf->count = q->count++;
  143. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  144. } else if (prev->vb.width == buf->vb.width &&
  145. prev->vb.height == buf->vb.height &&
  146. prev->fmt == buf->fmt) {
  147. list_move_tail(&buf->vb.queue, &q->active);
  148. buf->vb.state = VIDEOBUF_ACTIVE;
  149. buf->count = q->count++;
  150. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  151. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  152. } else {
  153. return 0;
  154. }
  155. prev = buf;
  156. }
  157. }
  158. static void cx25821_vid_timeout(unsigned long data)
  159. {
  160. struct cx25821_data *timeout_data = (struct cx25821_data *)data;
  161. struct cx25821_dev *dev = timeout_data->dev;
  162. const struct sram_channel *channel = timeout_data->channel;
  163. struct cx25821_dmaqueue *q = &dev->channels[channel->i].dma_vidq;
  164. struct cx25821_buffer *buf;
  165. unsigned long flags;
  166. /* cx25821_sram_channel_dump(dev, channel); */
  167. cx_clear(channel->dma_ctl, 0x11);
  168. spin_lock_irqsave(&dev->slock, flags);
  169. while (!list_empty(&q->active)) {
  170. buf = list_entry(q->active.next, struct cx25821_buffer,
  171. vb.queue);
  172. list_del(&buf->vb.queue);
  173. buf->vb.state = VIDEOBUF_ERROR;
  174. wake_up(&buf->vb.done);
  175. }
  176. cx25821_restart_video_queue(dev, q, channel);
  177. spin_unlock_irqrestore(&dev->slock, flags);
  178. }
  179. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  180. {
  181. u32 count = 0;
  182. int handled = 0;
  183. u32 mask;
  184. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  185. mask = cx_read(channel->int_msk);
  186. if (0 == (status & mask))
  187. return handled;
  188. cx_write(channel->int_stat, status);
  189. /* risc op code error */
  190. if (status & (1 << 16)) {
  191. pr_warn("%s, %s: video risc op code error\n",
  192. dev->name, channel->name);
  193. cx_clear(channel->dma_ctl, 0x11);
  194. cx25821_sram_channel_dump(dev, channel);
  195. }
  196. /* risc1 y */
  197. if (status & FLD_VID_DST_RISC1) {
  198. spin_lock(&dev->slock);
  199. count = cx_read(channel->gpcnt);
  200. cx25821_video_wakeup(dev, &dev->channels[channel->i].dma_vidq,
  201. count);
  202. spin_unlock(&dev->slock);
  203. handled++;
  204. }
  205. /* risc2 y */
  206. if (status & 0x10) {
  207. dprintk(2, "stopper video\n");
  208. spin_lock(&dev->slock);
  209. cx25821_restart_video_queue(dev,
  210. &dev->channels[channel->i].dma_vidq, channel);
  211. spin_unlock(&dev->slock);
  212. handled++;
  213. }
  214. return handled;
  215. }
  216. static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  217. unsigned int *size)
  218. {
  219. struct cx25821_channel *chan = q->priv_data;
  220. *size = chan->fmt->depth * chan->width * chan->height >> 3;
  221. if (0 == *count)
  222. *count = 32;
  223. if (*size * *count > vid_limit * 1024 * 1024)
  224. *count = (vid_limit * 1024 * 1024) / *size;
  225. return 0;
  226. }
  227. static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  228. enum v4l2_field field)
  229. {
  230. struct cx25821_channel *chan = q->priv_data;
  231. struct cx25821_dev *dev = chan->dev;
  232. struct cx25821_buffer *buf =
  233. container_of(vb, struct cx25821_buffer, vb);
  234. int rc, init_buffer = 0;
  235. u32 line0_offset;
  236. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  237. int bpl_local = LINE_SIZE_D1;
  238. BUG_ON(NULL == chan->fmt);
  239. if (chan->width < 48 || chan->width > 720 ||
  240. chan->height < 32 || chan->height > 576)
  241. return -EINVAL;
  242. buf->vb.size = (chan->width * chan->height * chan->fmt->depth) >> 3;
  243. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  244. return -EINVAL;
  245. if (buf->fmt != chan->fmt ||
  246. buf->vb.width != chan->width ||
  247. buf->vb.height != chan->height || buf->vb.field != field) {
  248. buf->fmt = chan->fmt;
  249. buf->vb.width = chan->width;
  250. buf->vb.height = chan->height;
  251. buf->vb.field = field;
  252. init_buffer = 1;
  253. }
  254. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  255. init_buffer = 1;
  256. rc = videobuf_iolock(q, &buf->vb, NULL);
  257. if (0 != rc) {
  258. printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
  259. goto fail;
  260. }
  261. }
  262. dprintk(1, "init_buffer=%d\n", init_buffer);
  263. if (init_buffer) {
  264. if (chan->pixel_formats == PIXEL_FRMT_411)
  265. buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
  266. else
  267. buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
  268. if (chan->pixel_formats == PIXEL_FRMT_411) {
  269. bpl_local = buf->bpl;
  270. } else {
  271. bpl_local = buf->bpl; /* Default */
  272. if (chan->use_cif_resolution) {
  273. if (dev->tvnorm & V4L2_STD_625_50)
  274. bpl_local = 352 << 1;
  275. else
  276. bpl_local = chan->cif_width << 1;
  277. }
  278. }
  279. switch (buf->vb.field) {
  280. case V4L2_FIELD_TOP:
  281. cx25821_risc_buffer(dev->pci, &buf->risc,
  282. dma->sglist, 0, UNSET,
  283. buf->bpl, 0, buf->vb.height);
  284. break;
  285. case V4L2_FIELD_BOTTOM:
  286. cx25821_risc_buffer(dev->pci, &buf->risc,
  287. dma->sglist, UNSET, 0,
  288. buf->bpl, 0, buf->vb.height);
  289. break;
  290. case V4L2_FIELD_INTERLACED:
  291. /* All other formats are top field first */
  292. line0_offset = 0;
  293. dprintk(1, "top field first\n");
  294. cx25821_risc_buffer(dev->pci, &buf->risc,
  295. dma->sglist, line0_offset,
  296. bpl_local, bpl_local, bpl_local,
  297. buf->vb.height >> 1);
  298. break;
  299. case V4L2_FIELD_SEQ_TB:
  300. cx25821_risc_buffer(dev->pci, &buf->risc,
  301. dma->sglist,
  302. 0, buf->bpl * (buf->vb.height >> 1),
  303. buf->bpl, 0, buf->vb.height >> 1);
  304. break;
  305. case V4L2_FIELD_SEQ_BT:
  306. cx25821_risc_buffer(dev->pci, &buf->risc,
  307. dma->sglist,
  308. buf->bpl * (buf->vb.height >> 1), 0,
  309. buf->bpl, 0, buf->vb.height >> 1);
  310. break;
  311. default:
  312. BUG();
  313. }
  314. }
  315. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  316. buf, buf->vb.i, chan->width, chan->height, chan->fmt->depth,
  317. chan->fmt->name, (unsigned long)buf->risc.dma);
  318. buf->vb.state = VIDEOBUF_PREPARED;
  319. return 0;
  320. fail:
  321. cx25821_free_buffer(q, buf);
  322. return rc;
  323. }
  324. static void cx25821_buffer_release(struct videobuf_queue *q,
  325. struct videobuf_buffer *vb)
  326. {
  327. struct cx25821_buffer *buf =
  328. container_of(vb, struct cx25821_buffer, vb);
  329. cx25821_free_buffer(q, buf);
  330. }
  331. static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
  332. {
  333. struct cx25821_channel *chan = video_drvdata(file);
  334. return videobuf_mmap_mapper(&chan->vidq, vma);
  335. }
  336. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  337. {
  338. struct cx25821_buffer *buf =
  339. container_of(vb, struct cx25821_buffer, vb);
  340. struct cx25821_buffer *prev;
  341. struct cx25821_channel *chan = vq->priv_data;
  342. struct cx25821_dev *dev = chan->dev;
  343. struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
  344. /* add jump to stopper */
  345. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  346. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  347. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  348. dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
  349. if (!list_empty(&q->queued)) {
  350. list_add_tail(&buf->vb.queue, &q->queued);
  351. buf->vb.state = VIDEOBUF_QUEUED;
  352. dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
  353. buf->vb.i);
  354. } else if (list_empty(&q->active)) {
  355. list_add_tail(&buf->vb.queue, &q->active);
  356. cx25821_start_video_dma(dev, q, buf, chan->sram_channels);
  357. buf->vb.state = VIDEOBUF_ACTIVE;
  358. buf->count = q->count++;
  359. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  360. dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
  361. buf, buf->vb.i, buf->count, q->count);
  362. } else {
  363. prev = list_entry(q->active.prev, struct cx25821_buffer,
  364. vb.queue);
  365. if (prev->vb.width == buf->vb.width
  366. && prev->vb.height == buf->vb.height
  367. && prev->fmt == buf->fmt) {
  368. list_add_tail(&buf->vb.queue, &q->active);
  369. buf->vb.state = VIDEOBUF_ACTIVE;
  370. buf->count = q->count++;
  371. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  372. /* 64 bit bits 63-32 */
  373. prev->risc.jmp[2] = cpu_to_le32(0);
  374. dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
  375. buf, buf->vb.i, buf->count);
  376. } else {
  377. list_add_tail(&buf->vb.queue, &q->queued);
  378. buf->vb.state = VIDEOBUF_QUEUED;
  379. dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
  380. buf->vb.i);
  381. }
  382. }
  383. if (list_empty(&q->active))
  384. dprintk(2, "active queue empty!\n");
  385. }
  386. static struct videobuf_queue_ops cx25821_video_qops = {
  387. .buf_setup = cx25821_buffer_setup,
  388. .buf_prepare = cx25821_buffer_prepare,
  389. .buf_queue = buffer_queue,
  390. .buf_release = cx25821_buffer_release,
  391. };
  392. static ssize_t video_read(struct file *file, char __user * data, size_t count,
  393. loff_t *ppos)
  394. {
  395. struct v4l2_fh *fh = file->private_data;
  396. struct cx25821_channel *chan = video_drvdata(file);
  397. struct cx25821_dev *dev = chan->dev;
  398. int err = 0;
  399. if (mutex_lock_interruptible(&dev->lock))
  400. return -ERESTARTSYS;
  401. if (chan->streaming_fh && chan->streaming_fh != fh) {
  402. err = -EBUSY;
  403. goto unlock;
  404. }
  405. chan->streaming_fh = fh;
  406. err = videobuf_read_one(&chan->vidq, data, count, ppos,
  407. file->f_flags & O_NONBLOCK);
  408. unlock:
  409. mutex_unlock(&dev->lock);
  410. return err;
  411. }
  412. static unsigned int video_poll(struct file *file,
  413. struct poll_table_struct *wait)
  414. {
  415. struct cx25821_channel *chan = video_drvdata(file);
  416. unsigned long req_events = poll_requested_events(wait);
  417. unsigned int res = v4l2_ctrl_poll(file, wait);
  418. if (req_events & (POLLIN | POLLRDNORM))
  419. res |= videobuf_poll_stream(file, &chan->vidq, wait);
  420. return res;
  421. /* This doesn't belong in poll(). This can be done
  422. * much better with vb2. We keep this code here as a
  423. * reminder.
  424. if ((res & POLLIN) && buf->vb.state == VIDEOBUF_DONE) {
  425. struct cx25821_dev *dev = chan->dev;
  426. if (dev && chan->use_cif_resolution) {
  427. u8 cam_id = *((char *)buf->vb.baddr + 3);
  428. memcpy((char *)buf->vb.baddr,
  429. (char *)buf->vb.baddr + (chan->width * 2),
  430. (chan->width * 2));
  431. *((char *)buf->vb.baddr + 3) = cam_id;
  432. }
  433. }
  434. */
  435. }
  436. static int video_release(struct file *file)
  437. {
  438. struct cx25821_channel *chan = video_drvdata(file);
  439. struct v4l2_fh *fh = file->private_data;
  440. struct cx25821_dev *dev = chan->dev;
  441. const struct sram_channel *sram_ch =
  442. dev->channels[0].sram_channels;
  443. mutex_lock(&dev->lock);
  444. /* stop the risc engine and fifo */
  445. cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
  446. /* stop video capture */
  447. if (chan->streaming_fh == fh) {
  448. videobuf_queue_cancel(&chan->vidq);
  449. chan->streaming_fh = NULL;
  450. }
  451. if (chan->vidq.read_buf) {
  452. cx25821_buffer_release(&chan->vidq, chan->vidq.read_buf);
  453. kfree(chan->vidq.read_buf);
  454. }
  455. videobuf_mmap_free(&chan->vidq);
  456. mutex_unlock(&dev->lock);
  457. return v4l2_fh_release(file);
  458. }
  459. /* VIDEO IOCTLS */
  460. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  461. struct v4l2_fmtdesc *f)
  462. {
  463. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  464. return -EINVAL;
  465. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  466. f->pixelformat = formats[f->index].fourcc;
  467. return 0;
  468. }
  469. static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  470. struct v4l2_format *f)
  471. {
  472. struct cx25821_channel *chan = video_drvdata(file);
  473. f->fmt.pix.width = chan->width;
  474. f->fmt.pix.height = chan->height;
  475. f->fmt.pix.field = chan->vidq.field;
  476. f->fmt.pix.pixelformat = chan->fmt->fourcc;
  477. f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
  478. f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
  479. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  480. f->fmt.pix.priv = 0;
  481. return 0;
  482. }
  483. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  484. struct v4l2_format *f)
  485. {
  486. struct cx25821_channel *chan = video_drvdata(file);
  487. struct cx25821_dev *dev = chan->dev;
  488. const struct cx25821_fmt *fmt;
  489. enum v4l2_field field = f->fmt.pix.field;
  490. unsigned int maxw, maxh;
  491. unsigned w;
  492. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  493. if (NULL == fmt)
  494. return -EINVAL;
  495. maxw = 720;
  496. maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  497. w = f->fmt.pix.width;
  498. if (field != V4L2_FIELD_BOTTOM)
  499. field = V4L2_FIELD_TOP;
  500. if (w < 352) {
  501. w = 176;
  502. f->fmt.pix.height = maxh / 4;
  503. } else if (w < 720) {
  504. w = 352;
  505. f->fmt.pix.height = maxh / 2;
  506. } else {
  507. w = 720;
  508. f->fmt.pix.height = maxh;
  509. field = V4L2_FIELD_INTERLACED;
  510. }
  511. f->fmt.pix.field = field;
  512. f->fmt.pix.width = w;
  513. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  514. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  515. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  516. f->fmt.pix.priv = 0;
  517. return 0;
  518. }
  519. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  520. struct v4l2_format *f)
  521. {
  522. struct cx25821_channel *chan = video_drvdata(file);
  523. struct cx25821_dev *dev = chan->dev;
  524. int pix_format = PIXEL_FRMT_422;
  525. int err;
  526. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  527. if (0 != err)
  528. return err;
  529. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  530. chan->vidq.field = f->fmt.pix.field;
  531. chan->width = f->fmt.pix.width;
  532. chan->height = f->fmt.pix.height;
  533. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  534. pix_format = PIXEL_FRMT_411;
  535. else
  536. pix_format = PIXEL_FRMT_422;
  537. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  538. /* check if cif resolution */
  539. if (chan->width == 320 || chan->width == 352)
  540. chan->use_cif_resolution = 1;
  541. else
  542. chan->use_cif_resolution = 0;
  543. chan->cif_width = chan->width;
  544. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  545. return 0;
  546. }
  547. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  548. {
  549. struct cx25821_channel *chan = video_drvdata(file);
  550. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  551. return -EINVAL;
  552. if (chan->streaming_fh && chan->streaming_fh != priv)
  553. return -EBUSY;
  554. chan->streaming_fh = priv;
  555. return videobuf_streamon(&chan->vidq);
  556. }
  557. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  558. {
  559. struct cx25821_channel *chan = video_drvdata(file);
  560. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  561. return -EINVAL;
  562. if (chan->streaming_fh && chan->streaming_fh != priv)
  563. return -EBUSY;
  564. if (chan->streaming_fh == NULL)
  565. return 0;
  566. chan->streaming_fh = NULL;
  567. return videobuf_streamoff(&chan->vidq);
  568. }
  569. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  570. {
  571. int ret_val = 0;
  572. struct cx25821_channel *chan = video_drvdata(file);
  573. ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
  574. p->sequence = chan->dma_vidq.count;
  575. return ret_val;
  576. }
  577. static int vidioc_log_status(struct file *file, void *priv)
  578. {
  579. struct cx25821_channel *chan = video_drvdata(file);
  580. struct cx25821_dev *dev = chan->dev;
  581. const struct sram_channel *sram_ch = chan->sram_channels;
  582. u32 tmp = 0;
  583. tmp = cx_read(sram_ch->dma_ctl);
  584. pr_info("Video input 0 is %s\n",
  585. (tmp & 0x11) ? "streaming" : "stopped");
  586. return 0;
  587. }
  588. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  589. struct v4l2_capability *cap)
  590. {
  591. struct cx25821_channel *chan = video_drvdata(file);
  592. struct cx25821_dev *dev = chan->dev;
  593. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  594. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  595. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
  596. strcpy(cap->driver, "cx25821");
  597. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  598. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  599. if (chan->id >= VID_CHANNEL_NUM)
  600. cap->device_caps = cap_output;
  601. else
  602. cap->device_caps = cap_input;
  603. cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
  604. return 0;
  605. }
  606. static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  607. struct v4l2_requestbuffers *p)
  608. {
  609. struct cx25821_channel *chan = video_drvdata(file);
  610. return videobuf_reqbufs(&chan->vidq, p);
  611. }
  612. static int cx25821_vidioc_querybuf(struct file *file, void *priv,
  613. struct v4l2_buffer *p)
  614. {
  615. struct cx25821_channel *chan = video_drvdata(file);
  616. return videobuf_querybuf(&chan->vidq, p);
  617. }
  618. static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  619. {
  620. struct cx25821_channel *chan = video_drvdata(file);
  621. return videobuf_qbuf(&chan->vidq, p);
  622. }
  623. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  624. {
  625. struct cx25821_channel *chan = video_drvdata(file);
  626. *tvnorms = chan->dev->tvnorm;
  627. return 0;
  628. }
  629. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  630. {
  631. struct cx25821_channel *chan = video_drvdata(file);
  632. struct cx25821_dev *dev = chan->dev;
  633. if (dev->tvnorm == tvnorms)
  634. return 0;
  635. dev->tvnorm = tvnorms;
  636. chan->width = 720;
  637. chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  638. medusa_set_videostandard(dev);
  639. return 0;
  640. }
  641. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  642. struct v4l2_input *i)
  643. {
  644. if (i->index)
  645. return -EINVAL;
  646. i->type = V4L2_INPUT_TYPE_CAMERA;
  647. i->std = CX25821_NORMS;
  648. strcpy(i->name, "Composite");
  649. return 0;
  650. }
  651. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  652. {
  653. *i = 0;
  654. return 0;
  655. }
  656. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  657. {
  658. return i ? -EINVAL : 0;
  659. }
  660. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  661. {
  662. struct cx25821_channel *chan =
  663. container_of(ctrl->handler, struct cx25821_channel, hdl);
  664. struct cx25821_dev *dev = chan->dev;
  665. switch (ctrl->id) {
  666. case V4L2_CID_BRIGHTNESS:
  667. medusa_set_brightness(dev, ctrl->val, chan->id);
  668. break;
  669. case V4L2_CID_HUE:
  670. medusa_set_hue(dev, ctrl->val, chan->id);
  671. break;
  672. case V4L2_CID_CONTRAST:
  673. medusa_set_contrast(dev, ctrl->val, chan->id);
  674. break;
  675. case V4L2_CID_SATURATION:
  676. medusa_set_saturation(dev, ctrl->val, chan->id);
  677. break;
  678. default:
  679. return -EINVAL;
  680. }
  681. return 0;
  682. }
  683. static int cx25821_vidioc_enum_output(struct file *file, void *priv,
  684. struct v4l2_output *o)
  685. {
  686. if (o->index)
  687. return -EINVAL;
  688. o->type = V4L2_INPUT_TYPE_CAMERA;
  689. o->std = CX25821_NORMS;
  690. strcpy(o->name, "Composite");
  691. return 0;
  692. }
  693. static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
  694. {
  695. *o = 0;
  696. return 0;
  697. }
  698. static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
  699. {
  700. return o ? -EINVAL : 0;
  701. }
  702. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  703. .s_ctrl = cx25821_s_ctrl,
  704. };
  705. static const struct v4l2_file_operations video_fops = {
  706. .owner = THIS_MODULE,
  707. .open = v4l2_fh_open,
  708. .release = video_release,
  709. .read = video_read,
  710. .poll = video_poll,
  711. .mmap = cx25821_video_mmap,
  712. .unlocked_ioctl = video_ioctl2,
  713. };
  714. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  715. .vidioc_querycap = cx25821_vidioc_querycap,
  716. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  717. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  718. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  719. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  720. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  721. .vidioc_querybuf = cx25821_vidioc_querybuf,
  722. .vidioc_qbuf = cx25821_vidioc_qbuf,
  723. .vidioc_dqbuf = vidioc_dqbuf,
  724. .vidioc_g_std = cx25821_vidioc_g_std,
  725. .vidioc_s_std = cx25821_vidioc_s_std,
  726. .vidioc_enum_input = cx25821_vidioc_enum_input,
  727. .vidioc_g_input = cx25821_vidioc_g_input,
  728. .vidioc_s_input = cx25821_vidioc_s_input,
  729. .vidioc_streamon = vidioc_streamon,
  730. .vidioc_streamoff = vidioc_streamoff,
  731. .vidioc_log_status = vidioc_log_status,
  732. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  733. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  734. };
  735. static const struct video_device cx25821_video_device = {
  736. .name = "cx25821-video",
  737. .fops = &video_fops,
  738. .release = video_device_release_empty,
  739. .minor = -1,
  740. .ioctl_ops = &video_ioctl_ops,
  741. .tvnorms = CX25821_NORMS,
  742. };
  743. static const struct v4l2_file_operations video_out_fops = {
  744. .owner = THIS_MODULE,
  745. .open = v4l2_fh_open,
  746. .release = v4l2_fh_release,
  747. .unlocked_ioctl = video_ioctl2,
  748. };
  749. static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
  750. .vidioc_querycap = cx25821_vidioc_querycap,
  751. .vidioc_g_std = cx25821_vidioc_g_std,
  752. .vidioc_s_std = cx25821_vidioc_s_std,
  753. .vidioc_enum_output = cx25821_vidioc_enum_output,
  754. .vidioc_g_output = cx25821_vidioc_g_output,
  755. .vidioc_s_output = cx25821_vidioc_s_output,
  756. .vidioc_log_status = vidioc_log_status,
  757. };
  758. static const struct video_device cx25821_video_out_device = {
  759. .name = "cx25821-video",
  760. .fops = &video_out_fops,
  761. .release = video_device_release_empty,
  762. .minor = -1,
  763. .ioctl_ops = &video_out_ioctl_ops,
  764. .tvnorms = CX25821_NORMS,
  765. };
  766. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  767. {
  768. cx_clear(PCI_INT_MSK, 1);
  769. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  770. video_unregister_device(&dev->channels[chan_num].vdev);
  771. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  772. btcx_riscmem_free(dev->pci,
  773. &dev->channels[chan_num].dma_vidq.stopper);
  774. }
  775. }
  776. int cx25821_video_register(struct cx25821_dev *dev)
  777. {
  778. int err;
  779. int i;
  780. /* initial device configuration */
  781. dev->tvnorm = V4L2_STD_NTSC_M;
  782. spin_lock_init(&dev->slock);
  783. for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
  784. struct cx25821_channel *chan = &dev->channels[i];
  785. struct video_device *vdev = &chan->vdev;
  786. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  787. bool is_output = i > SRAM_CH08;
  788. if (i == SRAM_CH08) /* audio channel */
  789. continue;
  790. if (!is_output) {
  791. v4l2_ctrl_handler_init(hdl, 4);
  792. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  793. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  794. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  795. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  796. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  797. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  798. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  799. V4L2_CID_HUE, 0, 10000, 1, 5000);
  800. if (hdl->error) {
  801. err = hdl->error;
  802. goto fail_unreg;
  803. }
  804. err = v4l2_ctrl_handler_setup(hdl);
  805. if (err)
  806. goto fail_unreg;
  807. }
  808. cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
  809. chan->sram_channels->dma_ctl, 0x11, 0);
  810. chan->sram_channels = &cx25821_sram_channels[i];
  811. chan->width = 720;
  812. if (dev->tvnorm & V4L2_STD_625_50)
  813. chan->height = 576;
  814. else
  815. chan->height = 480;
  816. if (chan->pixel_formats == PIXEL_FRMT_411)
  817. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  818. else
  819. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  820. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  821. INIT_LIST_HEAD(&chan->dma_vidq.active);
  822. INIT_LIST_HEAD(&chan->dma_vidq.queued);
  823. chan->timeout_data.dev = dev;
  824. chan->timeout_data.channel = &cx25821_sram_channels[i];
  825. chan->dma_vidq.timeout.function = cx25821_vid_timeout;
  826. chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
  827. init_timer(&chan->dma_vidq.timeout);
  828. if (!is_output)
  829. videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
  830. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  831. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  832. chan, &dev->lock);
  833. /* register v4l devices */
  834. *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
  835. vdev->v4l2_dev = &dev->v4l2_dev;
  836. if (!is_output)
  837. vdev->ctrl_handler = hdl;
  838. else
  839. vdev->vfl_dir = VFL_DIR_TX;
  840. vdev->lock = &dev->lock;
  841. set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
  842. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  843. video_set_drvdata(vdev, chan);
  844. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  845. video_nr[dev->nr]);
  846. if (err < 0)
  847. goto fail_unreg;
  848. }
  849. /* set PCI interrupt */
  850. cx_set(PCI_INT_MSK, 0xff);
  851. return 0;
  852. fail_unreg:
  853. while (i >= 0)
  854. cx25821_video_unregister(dev, i--);
  855. return err;
  856. }