cx25821-video.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 maxh;
  491. unsigned w;
  492. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  493. if (NULL == fmt)
  494. return -EINVAL;
  495. maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  496. w = f->fmt.pix.width;
  497. if (field != V4L2_FIELD_BOTTOM)
  498. field = V4L2_FIELD_TOP;
  499. if (w < 352) {
  500. w = 176;
  501. f->fmt.pix.height = maxh / 4;
  502. } else if (w < 720) {
  503. w = 352;
  504. f->fmt.pix.height = maxh / 2;
  505. } else {
  506. w = 720;
  507. f->fmt.pix.height = maxh;
  508. field = V4L2_FIELD_INTERLACED;
  509. }
  510. f->fmt.pix.field = field;
  511. f->fmt.pix.width = w;
  512. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  513. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  514. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  515. f->fmt.pix.priv = 0;
  516. return 0;
  517. }
  518. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  519. struct v4l2_format *f)
  520. {
  521. struct cx25821_channel *chan = video_drvdata(file);
  522. struct cx25821_dev *dev = chan->dev;
  523. int pix_format = PIXEL_FRMT_422;
  524. int err;
  525. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  526. if (0 != err)
  527. return err;
  528. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  529. chan->vidq.field = f->fmt.pix.field;
  530. chan->width = f->fmt.pix.width;
  531. chan->height = f->fmt.pix.height;
  532. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  533. pix_format = PIXEL_FRMT_411;
  534. else
  535. pix_format = PIXEL_FRMT_422;
  536. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  537. /* check if cif resolution */
  538. if (chan->width == 320 || chan->width == 352)
  539. chan->use_cif_resolution = 1;
  540. else
  541. chan->use_cif_resolution = 0;
  542. chan->cif_width = chan->width;
  543. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  544. return 0;
  545. }
  546. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  547. {
  548. struct cx25821_channel *chan = video_drvdata(file);
  549. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  550. return -EINVAL;
  551. if (chan->streaming_fh && chan->streaming_fh != priv)
  552. return -EBUSY;
  553. chan->streaming_fh = priv;
  554. return videobuf_streamon(&chan->vidq);
  555. }
  556. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  557. {
  558. struct cx25821_channel *chan = video_drvdata(file);
  559. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  560. return -EINVAL;
  561. if (chan->streaming_fh && chan->streaming_fh != priv)
  562. return -EBUSY;
  563. if (chan->streaming_fh == NULL)
  564. return 0;
  565. chan->streaming_fh = NULL;
  566. return videobuf_streamoff(&chan->vidq);
  567. }
  568. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  569. {
  570. int ret_val = 0;
  571. struct cx25821_channel *chan = video_drvdata(file);
  572. ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
  573. p->sequence = chan->dma_vidq.count;
  574. return ret_val;
  575. }
  576. static int vidioc_log_status(struct file *file, void *priv)
  577. {
  578. struct cx25821_channel *chan = video_drvdata(file);
  579. struct cx25821_dev *dev = chan->dev;
  580. const struct sram_channel *sram_ch = chan->sram_channels;
  581. u32 tmp = 0;
  582. tmp = cx_read(sram_ch->dma_ctl);
  583. pr_info("Video input 0 is %s\n",
  584. (tmp & 0x11) ? "streaming" : "stopped");
  585. return 0;
  586. }
  587. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  588. struct v4l2_capability *cap)
  589. {
  590. struct cx25821_channel *chan = video_drvdata(file);
  591. struct cx25821_dev *dev = chan->dev;
  592. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  593. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  594. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
  595. strcpy(cap->driver, "cx25821");
  596. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  597. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  598. if (chan->id >= VID_CHANNEL_NUM)
  599. cap->device_caps = cap_output;
  600. else
  601. cap->device_caps = cap_input;
  602. cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
  603. return 0;
  604. }
  605. static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  606. struct v4l2_requestbuffers *p)
  607. {
  608. struct cx25821_channel *chan = video_drvdata(file);
  609. return videobuf_reqbufs(&chan->vidq, p);
  610. }
  611. static int cx25821_vidioc_querybuf(struct file *file, void *priv,
  612. struct v4l2_buffer *p)
  613. {
  614. struct cx25821_channel *chan = video_drvdata(file);
  615. return videobuf_querybuf(&chan->vidq, p);
  616. }
  617. static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  618. {
  619. struct cx25821_channel *chan = video_drvdata(file);
  620. return videobuf_qbuf(&chan->vidq, p);
  621. }
  622. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  623. {
  624. struct cx25821_channel *chan = video_drvdata(file);
  625. *tvnorms = chan->dev->tvnorm;
  626. return 0;
  627. }
  628. static int cx25821_vidioc_s_std(struct file *file, void *priv,
  629. 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 int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
  703. struct v4l2_format *f)
  704. {
  705. struct cx25821_channel *chan = video_drvdata(file);
  706. struct cx25821_dev *dev = chan->dev;
  707. const struct cx25821_fmt *fmt;
  708. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  709. if (NULL == fmt)
  710. return -EINVAL;
  711. f->fmt.pix.width = 720;
  712. f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  713. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  714. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  715. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  716. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  717. f->fmt.pix.priv = 0;
  718. return 0;
  719. }
  720. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  721. struct v4l2_format *f)
  722. {
  723. struct cx25821_channel *chan = video_drvdata(file);
  724. int err;
  725. err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
  726. if (0 != err)
  727. return err;
  728. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  729. chan->vidq.field = f->fmt.pix.field;
  730. chan->width = f->fmt.pix.width;
  731. chan->height = f->fmt.pix.height;
  732. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  733. chan->pixel_formats = PIXEL_FRMT_411;
  734. else
  735. chan->pixel_formats = PIXEL_FRMT_422;
  736. return 0;
  737. }
  738. static ssize_t video_write(struct file *file, const char __user *data, size_t count,
  739. loff_t *ppos)
  740. {
  741. struct cx25821_channel *chan = video_drvdata(file);
  742. struct cx25821_dev *dev = chan->dev;
  743. struct v4l2_fh *fh = file->private_data;
  744. int err = 0;
  745. if (mutex_lock_interruptible(&dev->lock))
  746. return -ERESTARTSYS;
  747. if (chan->streaming_fh && chan->streaming_fh != fh) {
  748. err = -EBUSY;
  749. goto unlock;
  750. }
  751. if (!chan->streaming_fh) {
  752. err = cx25821_vidupstream_init(chan, chan->pixel_formats);
  753. if (err)
  754. goto unlock;
  755. chan->streaming_fh = fh;
  756. }
  757. err = cx25821_write_frame(chan, data, count);
  758. count -= err;
  759. *ppos += err;
  760. unlock:
  761. mutex_unlock(&dev->lock);
  762. return err;
  763. }
  764. static int video_out_release(struct file *file)
  765. {
  766. struct cx25821_channel *chan = video_drvdata(file);
  767. struct cx25821_dev *dev = chan->dev;
  768. struct v4l2_fh *fh = file->private_data;
  769. mutex_lock(&dev->lock);
  770. if (chan->streaming_fh == fh) {
  771. cx25821_stop_upstream_video(chan);
  772. chan->streaming_fh = NULL;
  773. }
  774. mutex_unlock(&dev->lock);
  775. return v4l2_fh_release(file);
  776. }
  777. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  778. .s_ctrl = cx25821_s_ctrl,
  779. };
  780. static const struct v4l2_file_operations video_fops = {
  781. .owner = THIS_MODULE,
  782. .open = v4l2_fh_open,
  783. .release = video_release,
  784. .read = video_read,
  785. .poll = video_poll,
  786. .mmap = cx25821_video_mmap,
  787. .unlocked_ioctl = video_ioctl2,
  788. };
  789. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  790. .vidioc_querycap = cx25821_vidioc_querycap,
  791. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  792. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  793. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  794. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  795. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  796. .vidioc_querybuf = cx25821_vidioc_querybuf,
  797. .vidioc_qbuf = cx25821_vidioc_qbuf,
  798. .vidioc_dqbuf = vidioc_dqbuf,
  799. .vidioc_g_std = cx25821_vidioc_g_std,
  800. .vidioc_s_std = cx25821_vidioc_s_std,
  801. .vidioc_enum_input = cx25821_vidioc_enum_input,
  802. .vidioc_g_input = cx25821_vidioc_g_input,
  803. .vidioc_s_input = cx25821_vidioc_s_input,
  804. .vidioc_streamon = vidioc_streamon,
  805. .vidioc_streamoff = vidioc_streamoff,
  806. .vidioc_log_status = vidioc_log_status,
  807. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  808. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  809. };
  810. static const struct video_device cx25821_video_device = {
  811. .name = "cx25821-video",
  812. .fops = &video_fops,
  813. .release = video_device_release_empty,
  814. .minor = -1,
  815. .ioctl_ops = &video_ioctl_ops,
  816. .tvnorms = CX25821_NORMS,
  817. };
  818. static const struct v4l2_file_operations video_out_fops = {
  819. .owner = THIS_MODULE,
  820. .open = v4l2_fh_open,
  821. .write = video_write,
  822. .release = video_out_release,
  823. .unlocked_ioctl = video_ioctl2,
  824. };
  825. static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
  826. .vidioc_querycap = cx25821_vidioc_querycap,
  827. .vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
  828. .vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
  829. .vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
  830. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  831. .vidioc_g_std = cx25821_vidioc_g_std,
  832. .vidioc_s_std = cx25821_vidioc_s_std,
  833. .vidioc_enum_output = cx25821_vidioc_enum_output,
  834. .vidioc_g_output = cx25821_vidioc_g_output,
  835. .vidioc_s_output = cx25821_vidioc_s_output,
  836. .vidioc_log_status = vidioc_log_status,
  837. };
  838. static const struct video_device cx25821_video_out_device = {
  839. .name = "cx25821-video",
  840. .fops = &video_out_fops,
  841. .release = video_device_release_empty,
  842. .minor = -1,
  843. .ioctl_ops = &video_out_ioctl_ops,
  844. .tvnorms = CX25821_NORMS,
  845. };
  846. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  847. {
  848. cx_clear(PCI_INT_MSK, 1);
  849. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  850. video_unregister_device(&dev->channels[chan_num].vdev);
  851. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  852. btcx_riscmem_free(dev->pci,
  853. &dev->channels[chan_num].dma_vidq.stopper);
  854. }
  855. }
  856. int cx25821_video_register(struct cx25821_dev *dev)
  857. {
  858. int err;
  859. int i;
  860. /* initial device configuration */
  861. dev->tvnorm = V4L2_STD_NTSC_M;
  862. spin_lock_init(&dev->slock);
  863. for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
  864. struct cx25821_channel *chan = &dev->channels[i];
  865. struct video_device *vdev = &chan->vdev;
  866. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  867. bool is_output = i > SRAM_CH08;
  868. if (i == SRAM_CH08) /* audio channel */
  869. continue;
  870. if (!is_output) {
  871. v4l2_ctrl_handler_init(hdl, 4);
  872. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  873. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  874. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  875. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  876. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  877. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  878. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  879. V4L2_CID_HUE, 0, 10000, 1, 5000);
  880. if (hdl->error) {
  881. err = hdl->error;
  882. goto fail_unreg;
  883. }
  884. err = v4l2_ctrl_handler_setup(hdl);
  885. if (err)
  886. goto fail_unreg;
  887. } else {
  888. chan->out = &dev->vid_out_data[i - SRAM_CH09];
  889. chan->out->chan = chan;
  890. }
  891. cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
  892. chan->sram_channels->dma_ctl, 0x11, 0);
  893. chan->sram_channels = &cx25821_sram_channels[i];
  894. chan->width = 720;
  895. if (dev->tvnorm & V4L2_STD_625_50)
  896. chan->height = 576;
  897. else
  898. chan->height = 480;
  899. if (chan->pixel_formats == PIXEL_FRMT_411)
  900. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  901. else
  902. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  903. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  904. INIT_LIST_HEAD(&chan->dma_vidq.active);
  905. INIT_LIST_HEAD(&chan->dma_vidq.queued);
  906. chan->timeout_data.dev = dev;
  907. chan->timeout_data.channel = &cx25821_sram_channels[i];
  908. chan->dma_vidq.timeout.function = cx25821_vid_timeout;
  909. chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
  910. init_timer(&chan->dma_vidq.timeout);
  911. if (!is_output)
  912. videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
  913. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  914. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  915. chan, &dev->lock);
  916. /* register v4l devices */
  917. *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
  918. vdev->v4l2_dev = &dev->v4l2_dev;
  919. if (!is_output)
  920. vdev->ctrl_handler = hdl;
  921. else
  922. vdev->vfl_dir = VFL_DIR_TX;
  923. vdev->lock = &dev->lock;
  924. set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
  925. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  926. video_set_drvdata(vdev, chan);
  927. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  928. video_nr[dev->nr]);
  929. if (err < 0)
  930. goto fail_unreg;
  931. }
  932. /* set PCI interrupt */
  933. cx_set(PCI_INT_MSK, 0xff);
  934. return 0;
  935. fail_unreg:
  936. while (i >= 0)
  937. cx25821_video_unregister(dev, i--);
  938. return err;
  939. }