cx25821-video.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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_g_fmt_vid_cap(struct file *file, void *priv,
  461. struct v4l2_format *f)
  462. {
  463. struct cx25821_channel *chan = video_drvdata(file);
  464. f->fmt.pix.width = chan->width;
  465. f->fmt.pix.height = chan->height;
  466. f->fmt.pix.field = chan->vidq.field;
  467. f->fmt.pix.pixelformat = chan->fmt->fourcc;
  468. f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
  469. f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
  470. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  471. f->fmt.pix.priv = 0;
  472. return 0;
  473. }
  474. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  475. struct v4l2_format *f)
  476. {
  477. struct cx25821_channel *chan = video_drvdata(file);
  478. struct cx25821_dev *dev = chan->dev;
  479. const struct cx25821_fmt *fmt;
  480. enum v4l2_field field = f->fmt.pix.field;
  481. unsigned int maxw, maxh;
  482. unsigned w;
  483. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  484. if (NULL == fmt)
  485. return -EINVAL;
  486. maxw = 720;
  487. maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  488. w = f->fmt.pix.width;
  489. if (field != V4L2_FIELD_BOTTOM)
  490. field = V4L2_FIELD_TOP;
  491. if (w < 352) {
  492. w = 176;
  493. f->fmt.pix.height = maxh / 4;
  494. } else if (w < 720) {
  495. w = 352;
  496. f->fmt.pix.height = maxh / 2;
  497. } else {
  498. w = 720;
  499. f->fmt.pix.height = maxh;
  500. field = V4L2_FIELD_INTERLACED;
  501. }
  502. f->fmt.pix.field = field;
  503. f->fmt.pix.width = w;
  504. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  505. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  506. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  507. f->fmt.pix.priv = 0;
  508. return 0;
  509. }
  510. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  511. {
  512. struct cx25821_channel *chan = video_drvdata(file);
  513. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  514. return -EINVAL;
  515. if (chan->streaming_fh && chan->streaming_fh != priv)
  516. return -EBUSY;
  517. chan->streaming_fh = priv;
  518. return videobuf_streamon(&chan->vidq);
  519. }
  520. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  521. {
  522. struct cx25821_channel *chan = video_drvdata(file);
  523. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  524. return -EINVAL;
  525. if (chan->streaming_fh && chan->streaming_fh != priv)
  526. return -EBUSY;
  527. if (chan->streaming_fh == NULL)
  528. return 0;
  529. chan->streaming_fh = NULL;
  530. return videobuf_streamoff(&chan->vidq);
  531. }
  532. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  533. struct v4l2_format *f)
  534. {
  535. struct cx25821_channel *chan = video_drvdata(file);
  536. struct cx25821_dev *dev = chan->dev;
  537. int pix_format = PIXEL_FRMT_422;
  538. int err;
  539. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  540. if (0 != err)
  541. return err;
  542. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  543. chan->vidq.field = f->fmt.pix.field;
  544. chan->width = f->fmt.pix.width;
  545. chan->height = f->fmt.pix.height;
  546. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  547. pix_format = PIXEL_FRMT_411;
  548. else
  549. pix_format = PIXEL_FRMT_422;
  550. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  551. /* check if cif resolution */
  552. if (chan->width == 320 || chan->width == 352)
  553. chan->use_cif_resolution = 1;
  554. else
  555. chan->use_cif_resolution = 0;
  556. chan->cif_width = chan->width;
  557. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  558. return 0;
  559. }
  560. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  561. {
  562. int ret_val = 0;
  563. struct cx25821_channel *chan = video_drvdata(file);
  564. ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
  565. p->sequence = chan->dma_vidq.count;
  566. return ret_val;
  567. }
  568. static int vidioc_log_status(struct file *file, void *priv)
  569. {
  570. struct cx25821_channel *chan = video_drvdata(file);
  571. struct cx25821_dev *dev = chan->dev;
  572. const struct sram_channel *sram_ch = chan->sram_channels;
  573. u32 tmp = 0;
  574. tmp = cx_read(sram_ch->dma_ctl);
  575. pr_info("Video input 0 is %s\n",
  576. (tmp & 0x11) ? "streaming" : "stopped");
  577. return 0;
  578. }
  579. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  580. struct v4l2_capability *cap)
  581. {
  582. struct cx25821_channel *chan = video_drvdata(file);
  583. struct cx25821_dev *dev = chan->dev;
  584. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  585. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  586. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
  587. strcpy(cap->driver, "cx25821");
  588. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  589. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  590. if (chan->id >= VID_CHANNEL_NUM)
  591. cap->device_caps = cap_output;
  592. else
  593. cap->device_caps = cap_input;
  594. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  595. return 0;
  596. }
  597. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  598. struct v4l2_fmtdesc *f)
  599. {
  600. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  601. return -EINVAL;
  602. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  603. f->pixelformat = formats[f->index].fourcc;
  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 long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  684. unsigned long arg)
  685. {
  686. struct cx25821_channel *chan = video_drvdata(file);
  687. struct cx25821_dev *dev = chan->dev;
  688. int command = 0;
  689. struct upstream_user_struct *data_from_user;
  690. data_from_user = (struct upstream_user_struct *)arg;
  691. if (!data_from_user) {
  692. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  693. return 0;
  694. }
  695. command = data_from_user->command;
  696. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  697. return 0;
  698. dev->input_filename = data_from_user->input_filename;
  699. dev->input_audiofilename = data_from_user->input_filename;
  700. dev->vid_stdname = data_from_user->vid_stdname;
  701. dev->pixel_format = data_from_user->pixel_format;
  702. dev->channel_select = data_from_user->channel_select;
  703. dev->command = data_from_user->command;
  704. switch (command) {
  705. case UPSTREAM_START_VIDEO:
  706. cx25821_start_upstream_video_ch1(dev, data_from_user);
  707. break;
  708. case UPSTREAM_STOP_VIDEO:
  709. cx25821_stop_upstream_video_ch1(dev);
  710. break;
  711. }
  712. return 0;
  713. }
  714. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  715. unsigned long arg)
  716. {
  717. struct cx25821_channel *chan = video_drvdata(file);
  718. struct cx25821_dev *dev = chan->dev;
  719. int command = 0;
  720. struct upstream_user_struct *data_from_user;
  721. data_from_user = (struct upstream_user_struct *)arg;
  722. if (!data_from_user) {
  723. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  724. return 0;
  725. }
  726. command = data_from_user->command;
  727. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  728. return 0;
  729. dev->input_filename_ch2 = data_from_user->input_filename;
  730. dev->input_audiofilename = data_from_user->input_filename;
  731. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  732. dev->pixel_format_ch2 = data_from_user->pixel_format;
  733. dev->channel_select_ch2 = data_from_user->channel_select;
  734. dev->command_ch2 = data_from_user->command;
  735. switch (command) {
  736. case UPSTREAM_START_VIDEO:
  737. cx25821_start_upstream_video_ch2(dev, data_from_user);
  738. break;
  739. case UPSTREAM_STOP_VIDEO:
  740. cx25821_stop_upstream_video_ch2(dev);
  741. break;
  742. }
  743. return 0;
  744. }
  745. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  746. unsigned long arg)
  747. {
  748. struct cx25821_channel *chan = video_drvdata(file);
  749. struct cx25821_dev *dev = chan->dev;
  750. int command = 0;
  751. struct upstream_user_struct *data_from_user;
  752. data_from_user = (struct upstream_user_struct *)arg;
  753. if (!data_from_user) {
  754. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  755. return 0;
  756. }
  757. command = data_from_user->command;
  758. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  759. return 0;
  760. dev->input_filename = data_from_user->input_filename;
  761. dev->input_audiofilename = data_from_user->input_filename;
  762. dev->vid_stdname = data_from_user->vid_stdname;
  763. dev->pixel_format = data_from_user->pixel_format;
  764. dev->channel_select = data_from_user->channel_select;
  765. dev->command = data_from_user->command;
  766. switch (command) {
  767. case UPSTREAM_START_AUDIO:
  768. cx25821_start_upstream_audio(dev, data_from_user);
  769. break;
  770. case UPSTREAM_STOP_AUDIO:
  771. cx25821_stop_upstream_audio(dev);
  772. break;
  773. }
  774. return 0;
  775. }
  776. static long cx25821_video_ioctl(struct file *file,
  777. unsigned int cmd, unsigned long arg)
  778. {
  779. struct cx25821_channel *chan = video_drvdata(file);
  780. /* check to see if it's the video upstream */
  781. if (chan->id == SRAM_CH09)
  782. return video_ioctl_upstream9(file, cmd, arg);
  783. if (chan->id == SRAM_CH10)
  784. return video_ioctl_upstream10(file, cmd, arg);
  785. if (chan->id == SRAM_CH11)
  786. return video_ioctl_upstream11(file, cmd, arg);
  787. return video_ioctl2(file, cmd, arg);
  788. }
  789. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  790. .s_ctrl = cx25821_s_ctrl,
  791. };
  792. static const struct v4l2_file_operations video_fops = {
  793. .owner = THIS_MODULE,
  794. .open = v4l2_fh_open,
  795. .release = video_release,
  796. .read = video_read,
  797. .poll = video_poll,
  798. .mmap = cx25821_video_mmap,
  799. .unlocked_ioctl = cx25821_video_ioctl,
  800. };
  801. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  802. .vidioc_querycap = cx25821_vidioc_querycap,
  803. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  804. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  805. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  806. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  807. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  808. .vidioc_querybuf = cx25821_vidioc_querybuf,
  809. .vidioc_qbuf = cx25821_vidioc_qbuf,
  810. .vidioc_dqbuf = vidioc_dqbuf,
  811. .vidioc_g_std = cx25821_vidioc_g_std,
  812. .vidioc_s_std = cx25821_vidioc_s_std,
  813. .vidioc_enum_input = cx25821_vidioc_enum_input,
  814. .vidioc_g_input = cx25821_vidioc_g_input,
  815. .vidioc_s_input = cx25821_vidioc_s_input,
  816. .vidioc_streamon = vidioc_streamon,
  817. .vidioc_streamoff = vidioc_streamoff,
  818. .vidioc_log_status = vidioc_log_status,
  819. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  820. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  821. };
  822. static const struct video_device cx25821_video_device = {
  823. .name = "cx25821-video",
  824. .fops = &video_fops,
  825. .release = video_device_release_empty,
  826. .minor = -1,
  827. .ioctl_ops = &video_ioctl_ops,
  828. .tvnorms = CX25821_NORMS,
  829. };
  830. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  831. {
  832. cx_clear(PCI_INT_MSK, 1);
  833. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  834. video_unregister_device(&dev->channels[chan_num].vdev);
  835. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  836. btcx_riscmem_free(dev->pci,
  837. &dev->channels[chan_num].dma_vidq.stopper);
  838. }
  839. }
  840. int cx25821_video_register(struct cx25821_dev *dev)
  841. {
  842. int err;
  843. int i;
  844. /* initial device configuration */
  845. dev->tvnorm = V4L2_STD_NTSC_M;
  846. spin_lock_init(&dev->slock);
  847. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  848. struct cx25821_channel *chan = &dev->channels[i];
  849. struct video_device *vdev = &chan->vdev;
  850. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  851. if (i == SRAM_CH08) /* audio channel */
  852. continue;
  853. v4l2_ctrl_handler_init(hdl, 4);
  854. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  855. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  856. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  857. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  858. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  859. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  860. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  861. V4L2_CID_HUE, 0, 10000, 1, 5000);
  862. if (hdl->error) {
  863. err = hdl->error;
  864. goto fail_unreg;
  865. }
  866. err = v4l2_ctrl_handler_setup(hdl);
  867. if (err)
  868. goto fail_unreg;
  869. cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
  870. chan->sram_channels->dma_ctl, 0x11, 0);
  871. chan->sram_channels = &cx25821_sram_channels[i];
  872. chan->width = 720;
  873. if (dev->tvnorm & V4L2_STD_625_50)
  874. chan->height = 576;
  875. else
  876. chan->height = 480;
  877. if (chan->pixel_formats == PIXEL_FRMT_411)
  878. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  879. else
  880. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  881. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  882. INIT_LIST_HEAD(&chan->dma_vidq.active);
  883. INIT_LIST_HEAD(&chan->dma_vidq.queued);
  884. chan->timeout_data.dev = dev;
  885. chan->timeout_data.channel = &cx25821_sram_channels[i];
  886. chan->dma_vidq.timeout.function = cx25821_vid_timeout;
  887. chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
  888. init_timer(&chan->dma_vidq.timeout);
  889. videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
  890. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  891. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  892. chan, &dev->lock);
  893. /* register v4l devices */
  894. *vdev = cx25821_video_device;
  895. vdev->v4l2_dev = &dev->v4l2_dev;
  896. vdev->ctrl_handler = hdl;
  897. vdev->lock = &dev->lock;
  898. set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
  899. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  900. video_set_drvdata(vdev, chan);
  901. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  902. video_nr[dev->nr]);
  903. if (err < 0)
  904. goto fail_unreg;
  905. }
  906. /* set PCI interrupt */
  907. cx_set(PCI_INT_MSK, 0xff);
  908. return 0;
  909. fail_unreg:
  910. while (i >= 0)
  911. cx25821_video_unregister(dev, i--);
  912. return err;
  913. }