cx25821-video.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  629. {
  630. struct cx25821_channel *chan = video_drvdata(file);
  631. struct cx25821_dev *dev = chan->dev;
  632. if (dev->tvnorm == tvnorms)
  633. return 0;
  634. dev->tvnorm = tvnorms;
  635. chan->width = 720;
  636. chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  637. medusa_set_videostandard(dev);
  638. return 0;
  639. }
  640. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  641. struct v4l2_input *i)
  642. {
  643. if (i->index)
  644. return -EINVAL;
  645. i->type = V4L2_INPUT_TYPE_CAMERA;
  646. i->std = CX25821_NORMS;
  647. strcpy(i->name, "Composite");
  648. return 0;
  649. }
  650. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  651. {
  652. *i = 0;
  653. return 0;
  654. }
  655. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  656. {
  657. return i ? -EINVAL : 0;
  658. }
  659. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  660. {
  661. struct cx25821_channel *chan =
  662. container_of(ctrl->handler, struct cx25821_channel, hdl);
  663. struct cx25821_dev *dev = chan->dev;
  664. switch (ctrl->id) {
  665. case V4L2_CID_BRIGHTNESS:
  666. medusa_set_brightness(dev, ctrl->val, chan->id);
  667. break;
  668. case V4L2_CID_HUE:
  669. medusa_set_hue(dev, ctrl->val, chan->id);
  670. break;
  671. case V4L2_CID_CONTRAST:
  672. medusa_set_contrast(dev, ctrl->val, chan->id);
  673. break;
  674. case V4L2_CID_SATURATION:
  675. medusa_set_saturation(dev, ctrl->val, chan->id);
  676. break;
  677. default:
  678. return -EINVAL;
  679. }
  680. return 0;
  681. }
  682. static int cx25821_vidioc_enum_output(struct file *file, void *priv,
  683. struct v4l2_output *o)
  684. {
  685. if (o->index)
  686. return -EINVAL;
  687. o->type = V4L2_INPUT_TYPE_CAMERA;
  688. o->std = CX25821_NORMS;
  689. strcpy(o->name, "Composite");
  690. return 0;
  691. }
  692. static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
  693. {
  694. *o = 0;
  695. return 0;
  696. }
  697. static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
  698. {
  699. return o ? -EINVAL : 0;
  700. }
  701. static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
  702. struct v4l2_format *f)
  703. {
  704. struct cx25821_channel *chan = video_drvdata(file);
  705. struct cx25821_dev *dev = chan->dev;
  706. const struct cx25821_fmt *fmt;
  707. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  708. if (NULL == fmt)
  709. return -EINVAL;
  710. f->fmt.pix.width = 720;
  711. f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  712. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  713. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  714. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  715. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  716. f->fmt.pix.priv = 0;
  717. return 0;
  718. }
  719. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  720. struct v4l2_format *f)
  721. {
  722. struct cx25821_channel *chan = video_drvdata(file);
  723. int err;
  724. err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
  725. if (0 != err)
  726. return err;
  727. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  728. chan->vidq.field = f->fmt.pix.field;
  729. chan->width = f->fmt.pix.width;
  730. chan->height = f->fmt.pix.height;
  731. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  732. chan->pixel_formats = PIXEL_FRMT_411;
  733. else
  734. chan->pixel_formats = PIXEL_FRMT_422;
  735. return 0;
  736. }
  737. static ssize_t video_write(struct file *file, const char __user *data, size_t count,
  738. loff_t *ppos)
  739. {
  740. struct cx25821_channel *chan = video_drvdata(file);
  741. struct cx25821_dev *dev = chan->dev;
  742. struct v4l2_fh *fh = file->private_data;
  743. int err = 0;
  744. if (mutex_lock_interruptible(&dev->lock))
  745. return -ERESTARTSYS;
  746. if (chan->streaming_fh && chan->streaming_fh != fh) {
  747. err = -EBUSY;
  748. goto unlock;
  749. }
  750. if (!chan->streaming_fh) {
  751. err = cx25821_vidupstream_init(chan, chan->pixel_formats);
  752. if (err)
  753. goto unlock;
  754. chan->streaming_fh = fh;
  755. }
  756. err = cx25821_write_frame(chan, data, count);
  757. count -= err;
  758. *ppos += err;
  759. unlock:
  760. mutex_unlock(&dev->lock);
  761. return err;
  762. }
  763. static int video_out_release(struct file *file)
  764. {
  765. struct cx25821_channel *chan = video_drvdata(file);
  766. struct cx25821_dev *dev = chan->dev;
  767. struct v4l2_fh *fh = file->private_data;
  768. mutex_lock(&dev->lock);
  769. if (chan->streaming_fh == fh) {
  770. cx25821_stop_upstream_video(chan);
  771. chan->streaming_fh = NULL;
  772. }
  773. mutex_unlock(&dev->lock);
  774. return v4l2_fh_release(file);
  775. }
  776. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  777. .s_ctrl = cx25821_s_ctrl,
  778. };
  779. static const struct v4l2_file_operations video_fops = {
  780. .owner = THIS_MODULE,
  781. .open = v4l2_fh_open,
  782. .release = video_release,
  783. .read = video_read,
  784. .poll = video_poll,
  785. .mmap = cx25821_video_mmap,
  786. .unlocked_ioctl = video_ioctl2,
  787. };
  788. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  789. .vidioc_querycap = cx25821_vidioc_querycap,
  790. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  791. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  792. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  793. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  794. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  795. .vidioc_querybuf = cx25821_vidioc_querybuf,
  796. .vidioc_qbuf = cx25821_vidioc_qbuf,
  797. .vidioc_dqbuf = vidioc_dqbuf,
  798. .vidioc_g_std = cx25821_vidioc_g_std,
  799. .vidioc_s_std = cx25821_vidioc_s_std,
  800. .vidioc_enum_input = cx25821_vidioc_enum_input,
  801. .vidioc_g_input = cx25821_vidioc_g_input,
  802. .vidioc_s_input = cx25821_vidioc_s_input,
  803. .vidioc_streamon = vidioc_streamon,
  804. .vidioc_streamoff = vidioc_streamoff,
  805. .vidioc_log_status = vidioc_log_status,
  806. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  807. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  808. };
  809. static const struct video_device cx25821_video_device = {
  810. .name = "cx25821-video",
  811. .fops = &video_fops,
  812. .release = video_device_release_empty,
  813. .minor = -1,
  814. .ioctl_ops = &video_ioctl_ops,
  815. .tvnorms = CX25821_NORMS,
  816. };
  817. static const struct v4l2_file_operations video_out_fops = {
  818. .owner = THIS_MODULE,
  819. .open = v4l2_fh_open,
  820. .write = video_write,
  821. .release = video_out_release,
  822. .unlocked_ioctl = video_ioctl2,
  823. };
  824. static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
  825. .vidioc_querycap = cx25821_vidioc_querycap,
  826. .vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
  827. .vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
  828. .vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
  829. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  830. .vidioc_g_std = cx25821_vidioc_g_std,
  831. .vidioc_s_std = cx25821_vidioc_s_std,
  832. .vidioc_enum_output = cx25821_vidioc_enum_output,
  833. .vidioc_g_output = cx25821_vidioc_g_output,
  834. .vidioc_s_output = cx25821_vidioc_s_output,
  835. .vidioc_log_status = vidioc_log_status,
  836. };
  837. static const struct video_device cx25821_video_out_device = {
  838. .name = "cx25821-video",
  839. .fops = &video_out_fops,
  840. .release = video_device_release_empty,
  841. .minor = -1,
  842. .ioctl_ops = &video_out_ioctl_ops,
  843. .tvnorms = CX25821_NORMS,
  844. };
  845. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  846. {
  847. cx_clear(PCI_INT_MSK, 1);
  848. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  849. video_unregister_device(&dev->channels[chan_num].vdev);
  850. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  851. btcx_riscmem_free(dev->pci,
  852. &dev->channels[chan_num].dma_vidq.stopper);
  853. }
  854. }
  855. int cx25821_video_register(struct cx25821_dev *dev)
  856. {
  857. int err;
  858. int i;
  859. /* initial device configuration */
  860. dev->tvnorm = V4L2_STD_NTSC_M;
  861. spin_lock_init(&dev->slock);
  862. for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
  863. struct cx25821_channel *chan = &dev->channels[i];
  864. struct video_device *vdev = &chan->vdev;
  865. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  866. bool is_output = i > SRAM_CH08;
  867. if (i == SRAM_CH08) /* audio channel */
  868. continue;
  869. if (!is_output) {
  870. v4l2_ctrl_handler_init(hdl, 4);
  871. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  872. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  873. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  874. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  875. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  876. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  877. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  878. V4L2_CID_HUE, 0, 10000, 1, 5000);
  879. if (hdl->error) {
  880. err = hdl->error;
  881. goto fail_unreg;
  882. }
  883. err = v4l2_ctrl_handler_setup(hdl);
  884. if (err)
  885. goto fail_unreg;
  886. } else {
  887. chan->out = &dev->vid_out_data[i - SRAM_CH09];
  888. chan->out->chan = chan;
  889. }
  890. cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
  891. chan->sram_channels->dma_ctl, 0x11, 0);
  892. chan->sram_channels = &cx25821_sram_channels[i];
  893. chan->width = 720;
  894. if (dev->tvnorm & V4L2_STD_625_50)
  895. chan->height = 576;
  896. else
  897. chan->height = 480;
  898. if (chan->pixel_formats == PIXEL_FRMT_411)
  899. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  900. else
  901. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  902. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  903. INIT_LIST_HEAD(&chan->dma_vidq.active);
  904. INIT_LIST_HEAD(&chan->dma_vidq.queued);
  905. chan->timeout_data.dev = dev;
  906. chan->timeout_data.channel = &cx25821_sram_channels[i];
  907. chan->dma_vidq.timeout.function = cx25821_vid_timeout;
  908. chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
  909. init_timer(&chan->dma_vidq.timeout);
  910. if (!is_output)
  911. videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
  912. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  913. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  914. chan, &dev->lock);
  915. /* register v4l devices */
  916. *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
  917. vdev->v4l2_dev = &dev->v4l2_dev;
  918. if (!is_output)
  919. vdev->ctrl_handler = hdl;
  920. else
  921. vdev->vfl_dir = VFL_DIR_TX;
  922. vdev->lock = &dev->lock;
  923. set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
  924. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  925. video_set_drvdata(vdev, chan);
  926. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  927. video_nr[dev->nr]);
  928. if (err < 0)
  929. goto fail_unreg;
  930. }
  931. /* set PCI interrupt */
  932. cx_set(PCI_INT_MSK, 0xff);
  933. return 0;
  934. fail_unreg:
  935. while (i >= 0)
  936. cx25821_video_unregister(dev, i--);
  937. return err;
  938. }