cx25821-video.c 30 KB

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