cx25821-video.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the Conexant CX25821 PCIe bridge
  4. *
  5. * Copyright (C) 2009 Conexant Systems Inc.
  6. * Authors <[email protected]>, <[email protected]>
  7. * Based on Steven Toth <[email protected]> cx25821 driver
  8. * Parts adapted/taken from Eduardo Moscoso Rubino
  9. * Copyright (C) 2009 Eduardo Moscoso Rubino <[email protected]>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include "cx25821-video.h"
  13. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  14. MODULE_AUTHOR("Hiep Huynh <[email protected]>");
  15. MODULE_LICENSE("GPL");
  16. static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
  17. module_param_array(video_nr, int, NULL, 0444);
  18. MODULE_PARM_DESC(video_nr, "video device numbers");
  19. static unsigned int video_debug = VIDEO_DEBUG;
  20. module_param(video_debug, int, 0644);
  21. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  22. static unsigned int irq_debug;
  23. module_param(irq_debug, int, 0644);
  24. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  25. #define FORMAT_FLAGS_PACKED 0x01
  26. static const struct cx25821_fmt formats[] = {
  27. {
  28. .fourcc = V4L2_PIX_FMT_Y41P,
  29. .depth = 12,
  30. .flags = FORMAT_FLAGS_PACKED,
  31. }, {
  32. .fourcc = V4L2_PIX_FMT_YUYV,
  33. .depth = 16,
  34. .flags = FORMAT_FLAGS_PACKED,
  35. },
  36. };
  37. static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  38. {
  39. unsigned int i;
  40. for (i = 0; i < ARRAY_SIZE(formats); i++)
  41. if (formats[i].fourcc == fourcc)
  42. return formats + i;
  43. return NULL;
  44. }
  45. int cx25821_start_video_dma(struct cx25821_dev *dev,
  46. struct cx25821_dmaqueue *q,
  47. struct cx25821_buffer *buf,
  48. const struct sram_channel *channel)
  49. {
  50. int tmp = 0;
  51. /* setup fifo + format */
  52. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  53. /* reset counter */
  54. cx_write(channel->gpcnt_ctl, 3);
  55. /* enable irq */
  56. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  57. cx_set(channel->int_msk, 0x11);
  58. /* start dma */
  59. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  60. /* make sure upstream setting if any is reversed */
  61. tmp = cx_read(VID_CH_MODE_SEL);
  62. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  63. return 0;
  64. }
  65. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  66. {
  67. int handled = 0;
  68. u32 mask;
  69. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  70. mask = cx_read(channel->int_msk);
  71. if (0 == (status & mask))
  72. return handled;
  73. cx_write(channel->int_stat, status);
  74. /* risc op code error */
  75. if (status & (1 << 16)) {
  76. pr_warn("%s, %s: video risc op code error\n",
  77. dev->name, channel->name);
  78. cx_clear(channel->dma_ctl, 0x11);
  79. cx25821_sram_channel_dump(dev, channel);
  80. }
  81. /* risc1 y */
  82. if (status & FLD_VID_DST_RISC1) {
  83. struct cx25821_dmaqueue *dmaq =
  84. &dev->channels[channel->i].dma_vidq;
  85. struct cx25821_buffer *buf;
  86. spin_lock(&dev->slock);
  87. if (!list_empty(&dmaq->active)) {
  88. buf = list_entry(dmaq->active.next,
  89. struct cx25821_buffer, queue);
  90. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  91. buf->vb.sequence = dmaq->count++;
  92. list_del(&buf->queue);
  93. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  94. }
  95. spin_unlock(&dev->slock);
  96. handled++;
  97. }
  98. return handled;
  99. }
  100. static int cx25821_queue_setup(struct vb2_queue *q,
  101. unsigned int *num_buffers, unsigned int *num_planes,
  102. unsigned int sizes[], struct device *alloc_devs[])
  103. {
  104. struct cx25821_channel *chan = q->drv_priv;
  105. unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
  106. if (*num_planes)
  107. return sizes[0] < size ? -EINVAL : 0;
  108. *num_planes = 1;
  109. sizes[0] = size;
  110. return 0;
  111. }
  112. static int cx25821_buffer_prepare(struct vb2_buffer *vb)
  113. {
  114. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  115. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  116. struct cx25821_dev *dev = chan->dev;
  117. struct cx25821_buffer *buf =
  118. container_of(vbuf, struct cx25821_buffer, vb);
  119. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  120. u32 line0_offset;
  121. int bpl_local = LINE_SIZE_D1;
  122. int ret;
  123. if (chan->pixel_formats == PIXEL_FRMT_411)
  124. buf->bpl = (chan->fmt->depth * chan->width) >> 3;
  125. else
  126. buf->bpl = (chan->fmt->depth >> 3) * chan->width;
  127. if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
  128. return -EINVAL;
  129. vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
  130. buf->vb.field = chan->field;
  131. if (chan->pixel_formats == PIXEL_FRMT_411) {
  132. bpl_local = buf->bpl;
  133. } else {
  134. bpl_local = buf->bpl; /* Default */
  135. if (chan->use_cif_resolution) {
  136. if (dev->tvnorm & V4L2_STD_625_50)
  137. bpl_local = 352 << 1;
  138. else
  139. bpl_local = chan->cif_width << 1;
  140. }
  141. }
  142. switch (chan->field) {
  143. case V4L2_FIELD_TOP:
  144. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  145. sgt->sgl, 0, UNSET,
  146. buf->bpl, 0, chan->height);
  147. break;
  148. case V4L2_FIELD_BOTTOM:
  149. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  150. sgt->sgl, UNSET, 0,
  151. buf->bpl, 0, chan->height);
  152. break;
  153. case V4L2_FIELD_INTERLACED:
  154. /* All other formats are top field first */
  155. line0_offset = 0;
  156. dprintk(1, "top field first\n");
  157. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  158. sgt->sgl, line0_offset,
  159. bpl_local, bpl_local, bpl_local,
  160. chan->height >> 1);
  161. break;
  162. case V4L2_FIELD_SEQ_TB:
  163. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  164. sgt->sgl,
  165. 0, buf->bpl * (chan->height >> 1),
  166. buf->bpl, 0, chan->height >> 1);
  167. break;
  168. case V4L2_FIELD_SEQ_BT:
  169. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  170. sgt->sgl,
  171. buf->bpl * (chan->height >> 1), 0,
  172. buf->bpl, 0, chan->height >> 1);
  173. break;
  174. default:
  175. WARN_ON(1);
  176. ret = -EINVAL;
  177. break;
  178. }
  179. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp 0x%08x - dma=0x%08lx\n",
  180. buf, buf->vb.vb2_buf.index, chan->width, chan->height,
  181. chan->fmt->depth, chan->fmt->fourcc,
  182. (unsigned long)buf->risc.dma);
  183. return ret;
  184. }
  185. static void cx25821_buffer_finish(struct vb2_buffer *vb)
  186. {
  187. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  188. struct cx25821_buffer *buf =
  189. container_of(vbuf, struct cx25821_buffer, vb);
  190. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  191. struct cx25821_dev *dev = chan->dev;
  192. cx25821_free_buffer(dev, buf);
  193. }
  194. static void cx25821_buffer_queue(struct vb2_buffer *vb)
  195. {
  196. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  197. struct cx25821_buffer *buf =
  198. container_of(vbuf, struct cx25821_buffer, vb);
  199. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  200. struct cx25821_dev *dev = chan->dev;
  201. struct cx25821_buffer *prev;
  202. struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
  203. buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
  204. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
  205. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
  206. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  207. if (list_empty(&q->active)) {
  208. list_add_tail(&buf->queue, &q->active);
  209. } else {
  210. buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
  211. prev = list_entry(q->active.prev, struct cx25821_buffer,
  212. queue);
  213. list_add_tail(&buf->queue, &q->active);
  214. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  215. }
  216. }
  217. static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
  218. {
  219. struct cx25821_channel *chan = q->drv_priv;
  220. struct cx25821_dev *dev = chan->dev;
  221. struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
  222. struct cx25821_buffer *buf = list_entry(dmaq->active.next,
  223. struct cx25821_buffer, queue);
  224. dmaq->count = 0;
  225. cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
  226. return 0;
  227. }
  228. static void cx25821_stop_streaming(struct vb2_queue *q)
  229. {
  230. struct cx25821_channel *chan = q->drv_priv;
  231. struct cx25821_dev *dev = chan->dev;
  232. struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
  233. unsigned long flags;
  234. cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
  235. spin_lock_irqsave(&dev->slock, flags);
  236. while (!list_empty(&dmaq->active)) {
  237. struct cx25821_buffer *buf = list_entry(dmaq->active.next,
  238. struct cx25821_buffer, queue);
  239. list_del(&buf->queue);
  240. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  241. }
  242. spin_unlock_irqrestore(&dev->slock, flags);
  243. }
  244. static const struct vb2_ops cx25821_video_qops = {
  245. .queue_setup = cx25821_queue_setup,
  246. .buf_prepare = cx25821_buffer_prepare,
  247. .buf_finish = cx25821_buffer_finish,
  248. .buf_queue = cx25821_buffer_queue,
  249. .wait_prepare = vb2_ops_wait_prepare,
  250. .wait_finish = vb2_ops_wait_finish,
  251. .start_streaming = cx25821_start_streaming,
  252. .stop_streaming = cx25821_stop_streaming,
  253. };
  254. /* VIDEO IOCTLS */
  255. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  256. struct v4l2_fmtdesc *f)
  257. {
  258. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  259. return -EINVAL;
  260. f->pixelformat = formats[f->index].fourcc;
  261. return 0;
  262. }
  263. static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  264. struct v4l2_format *f)
  265. {
  266. struct cx25821_channel *chan = video_drvdata(file);
  267. f->fmt.pix.width = chan->width;
  268. f->fmt.pix.height = chan->height;
  269. f->fmt.pix.field = chan->field;
  270. f->fmt.pix.pixelformat = chan->fmt->fourcc;
  271. f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
  272. f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
  273. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  274. return 0;
  275. }
  276. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  277. struct v4l2_format *f)
  278. {
  279. struct cx25821_channel *chan = video_drvdata(file);
  280. struct cx25821_dev *dev = chan->dev;
  281. const struct cx25821_fmt *fmt;
  282. enum v4l2_field field = f->fmt.pix.field;
  283. unsigned int maxh;
  284. unsigned w;
  285. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  286. if (NULL == fmt)
  287. return -EINVAL;
  288. maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  289. w = f->fmt.pix.width;
  290. if (field != V4L2_FIELD_BOTTOM)
  291. field = V4L2_FIELD_TOP;
  292. if (w < 352) {
  293. w = 176;
  294. f->fmt.pix.height = maxh / 4;
  295. } else if (w < 720) {
  296. w = 352;
  297. f->fmt.pix.height = maxh / 2;
  298. } else {
  299. w = 720;
  300. f->fmt.pix.height = maxh;
  301. field = V4L2_FIELD_INTERLACED;
  302. }
  303. f->fmt.pix.field = field;
  304. f->fmt.pix.width = w;
  305. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  306. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  307. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  308. return 0;
  309. }
  310. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  311. struct v4l2_format *f)
  312. {
  313. struct cx25821_channel *chan = video_drvdata(file);
  314. struct cx25821_dev *dev = chan->dev;
  315. int pix_format = PIXEL_FRMT_422;
  316. int err;
  317. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  318. if (0 != err)
  319. return err;
  320. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  321. chan->field = f->fmt.pix.field;
  322. chan->width = f->fmt.pix.width;
  323. chan->height = f->fmt.pix.height;
  324. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  325. pix_format = PIXEL_FRMT_411;
  326. else
  327. pix_format = PIXEL_FRMT_422;
  328. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  329. /* check if cif resolution */
  330. if (chan->width == 320 || chan->width == 352)
  331. chan->use_cif_resolution = 1;
  332. else
  333. chan->use_cif_resolution = 0;
  334. chan->cif_width = chan->width;
  335. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  336. return 0;
  337. }
  338. static int vidioc_log_status(struct file *file, void *priv)
  339. {
  340. struct cx25821_channel *chan = video_drvdata(file);
  341. struct cx25821_dev *dev = chan->dev;
  342. const struct sram_channel *sram_ch = chan->sram_channels;
  343. u32 tmp = 0;
  344. tmp = cx_read(sram_ch->dma_ctl);
  345. pr_info("Video input 0 is %s\n",
  346. (tmp & 0x11) ? "streaming" : "stopped");
  347. return 0;
  348. }
  349. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  350. struct v4l2_capability *cap)
  351. {
  352. struct cx25821_channel *chan = video_drvdata(file);
  353. struct cx25821_dev *dev = chan->dev;
  354. strscpy(cap->driver, "cx25821", sizeof(cap->driver));
  355. strscpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  356. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  357. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
  358. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
  359. V4L2_CAP_DEVICE_CAPS;
  360. return 0;
  361. }
  362. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  363. {
  364. struct cx25821_channel *chan = video_drvdata(file);
  365. *tvnorms = chan->dev->tvnorm;
  366. return 0;
  367. }
  368. static int cx25821_vidioc_s_std(struct file *file, void *priv,
  369. v4l2_std_id tvnorms)
  370. {
  371. struct cx25821_channel *chan = video_drvdata(file);
  372. struct cx25821_dev *dev = chan->dev;
  373. if (dev->tvnorm == tvnorms)
  374. return 0;
  375. dev->tvnorm = tvnorms;
  376. chan->width = 720;
  377. chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  378. medusa_set_videostandard(dev);
  379. return 0;
  380. }
  381. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  382. struct v4l2_input *i)
  383. {
  384. if (i->index)
  385. return -EINVAL;
  386. i->type = V4L2_INPUT_TYPE_CAMERA;
  387. i->std = CX25821_NORMS;
  388. strscpy(i->name, "Composite", sizeof(i->name));
  389. return 0;
  390. }
  391. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  392. {
  393. *i = 0;
  394. return 0;
  395. }
  396. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  397. {
  398. return i ? -EINVAL : 0;
  399. }
  400. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  401. {
  402. struct cx25821_channel *chan =
  403. container_of(ctrl->handler, struct cx25821_channel, hdl);
  404. struct cx25821_dev *dev = chan->dev;
  405. switch (ctrl->id) {
  406. case V4L2_CID_BRIGHTNESS:
  407. medusa_set_brightness(dev, ctrl->val, chan->id);
  408. break;
  409. case V4L2_CID_HUE:
  410. medusa_set_hue(dev, ctrl->val, chan->id);
  411. break;
  412. case V4L2_CID_CONTRAST:
  413. medusa_set_contrast(dev, ctrl->val, chan->id);
  414. break;
  415. case V4L2_CID_SATURATION:
  416. medusa_set_saturation(dev, ctrl->val, chan->id);
  417. break;
  418. default:
  419. return -EINVAL;
  420. }
  421. return 0;
  422. }
  423. static int cx25821_vidioc_enum_output(struct file *file, void *priv,
  424. struct v4l2_output *o)
  425. {
  426. if (o->index)
  427. return -EINVAL;
  428. o->type = V4L2_INPUT_TYPE_CAMERA;
  429. o->std = CX25821_NORMS;
  430. strscpy(o->name, "Composite", sizeof(o->name));
  431. return 0;
  432. }
  433. static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
  434. {
  435. *o = 0;
  436. return 0;
  437. }
  438. static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
  439. {
  440. return o ? -EINVAL : 0;
  441. }
  442. static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
  443. struct v4l2_format *f)
  444. {
  445. struct cx25821_channel *chan = video_drvdata(file);
  446. struct cx25821_dev *dev = chan->dev;
  447. const struct cx25821_fmt *fmt;
  448. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  449. if (NULL == fmt)
  450. return -EINVAL;
  451. f->fmt.pix.width = 720;
  452. f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  453. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  454. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  455. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  456. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  457. return 0;
  458. }
  459. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  460. struct v4l2_format *f)
  461. {
  462. struct cx25821_channel *chan = video_drvdata(file);
  463. int err;
  464. err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
  465. if (0 != err)
  466. return err;
  467. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  468. chan->field = f->fmt.pix.field;
  469. chan->width = f->fmt.pix.width;
  470. chan->height = f->fmt.pix.height;
  471. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  472. chan->pixel_formats = PIXEL_FRMT_411;
  473. else
  474. chan->pixel_formats = PIXEL_FRMT_422;
  475. return 0;
  476. }
  477. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  478. .s_ctrl = cx25821_s_ctrl,
  479. };
  480. static const struct v4l2_file_operations video_fops = {
  481. .owner = THIS_MODULE,
  482. .open = v4l2_fh_open,
  483. .release = vb2_fop_release,
  484. .read = vb2_fop_read,
  485. .poll = vb2_fop_poll,
  486. .unlocked_ioctl = video_ioctl2,
  487. .mmap = vb2_fop_mmap,
  488. };
  489. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  490. .vidioc_querycap = cx25821_vidioc_querycap,
  491. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  492. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  493. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  494. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  495. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  496. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  497. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  498. .vidioc_querybuf = vb2_ioctl_querybuf,
  499. .vidioc_qbuf = vb2_ioctl_qbuf,
  500. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  501. .vidioc_streamon = vb2_ioctl_streamon,
  502. .vidioc_streamoff = vb2_ioctl_streamoff,
  503. .vidioc_g_std = cx25821_vidioc_g_std,
  504. .vidioc_s_std = cx25821_vidioc_s_std,
  505. .vidioc_enum_input = cx25821_vidioc_enum_input,
  506. .vidioc_g_input = cx25821_vidioc_g_input,
  507. .vidioc_s_input = cx25821_vidioc_s_input,
  508. .vidioc_log_status = vidioc_log_status,
  509. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  510. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  511. };
  512. static const struct video_device cx25821_video_device = {
  513. .name = "cx25821-video",
  514. .fops = &video_fops,
  515. .release = video_device_release_empty,
  516. .minor = -1,
  517. .ioctl_ops = &video_ioctl_ops,
  518. .tvnorms = CX25821_NORMS,
  519. .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  520. V4L2_CAP_STREAMING,
  521. };
  522. static const struct v4l2_file_operations video_out_fops = {
  523. .owner = THIS_MODULE,
  524. .open = v4l2_fh_open,
  525. .release = vb2_fop_release,
  526. .write = vb2_fop_write,
  527. .poll = vb2_fop_poll,
  528. .unlocked_ioctl = video_ioctl2,
  529. .mmap = vb2_fop_mmap,
  530. };
  531. static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
  532. .vidioc_querycap = cx25821_vidioc_querycap,
  533. .vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
  534. .vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
  535. .vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
  536. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  537. .vidioc_g_std = cx25821_vidioc_g_std,
  538. .vidioc_s_std = cx25821_vidioc_s_std,
  539. .vidioc_enum_output = cx25821_vidioc_enum_output,
  540. .vidioc_g_output = cx25821_vidioc_g_output,
  541. .vidioc_s_output = cx25821_vidioc_s_output,
  542. .vidioc_log_status = vidioc_log_status,
  543. };
  544. static const struct video_device cx25821_video_out_device = {
  545. .name = "cx25821-video",
  546. .fops = &video_out_fops,
  547. .release = video_device_release_empty,
  548. .minor = -1,
  549. .ioctl_ops = &video_out_ioctl_ops,
  550. .tvnorms = CX25821_NORMS,
  551. .device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE,
  552. };
  553. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  554. {
  555. cx_clear(PCI_INT_MSK, 1);
  556. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  557. video_unregister_device(&dev->channels[chan_num].vdev);
  558. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  559. }
  560. }
  561. int cx25821_video_register(struct cx25821_dev *dev)
  562. {
  563. int err;
  564. int i;
  565. /* initial device configuration */
  566. dev->tvnorm = V4L2_STD_NTSC_M;
  567. spin_lock_init(&dev->slock);
  568. for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
  569. struct cx25821_channel *chan = &dev->channels[i];
  570. struct video_device *vdev = &chan->vdev;
  571. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  572. struct vb2_queue *q;
  573. bool is_output = i > SRAM_CH08;
  574. if (i == SRAM_CH08) /* audio channel */
  575. continue;
  576. if (!is_output) {
  577. v4l2_ctrl_handler_init(hdl, 4);
  578. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  579. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  580. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  581. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  582. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  583. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  584. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  585. V4L2_CID_HUE, 0, 10000, 1, 5000);
  586. if (hdl->error) {
  587. err = hdl->error;
  588. goto fail_unreg;
  589. }
  590. err = v4l2_ctrl_handler_setup(hdl);
  591. if (err)
  592. goto fail_unreg;
  593. } else {
  594. chan->out = &dev->vid_out_data[i - SRAM_CH09];
  595. chan->out->chan = chan;
  596. }
  597. chan->sram_channels = &cx25821_sram_channels[i];
  598. chan->width = 720;
  599. chan->field = V4L2_FIELD_INTERLACED;
  600. if (dev->tvnorm & V4L2_STD_625_50)
  601. chan->height = 576;
  602. else
  603. chan->height = 480;
  604. if (chan->pixel_formats == PIXEL_FRMT_411)
  605. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  606. else
  607. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  608. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  609. INIT_LIST_HEAD(&chan->dma_vidq.active);
  610. q = &chan->vidq;
  611. q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
  612. V4L2_BUF_TYPE_VIDEO_CAPTURE;
  613. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  614. q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
  615. q->gfp_flags = GFP_DMA32;
  616. q->min_buffers_needed = 2;
  617. q->drv_priv = chan;
  618. q->buf_struct_size = sizeof(struct cx25821_buffer);
  619. q->ops = &cx25821_video_qops;
  620. q->mem_ops = &vb2_dma_sg_memops;
  621. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  622. q->lock = &dev->lock;
  623. q->dev = &dev->pci->dev;
  624. if (!is_output) {
  625. err = vb2_queue_init(q);
  626. if (err < 0)
  627. goto fail_unreg;
  628. }
  629. /* register v4l devices */
  630. *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
  631. vdev->v4l2_dev = &dev->v4l2_dev;
  632. if (!is_output)
  633. vdev->ctrl_handler = hdl;
  634. else
  635. vdev->vfl_dir = VFL_DIR_TX;
  636. vdev->lock = &dev->lock;
  637. vdev->queue = q;
  638. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  639. video_set_drvdata(vdev, chan);
  640. err = video_register_device(vdev, VFL_TYPE_VIDEO,
  641. video_nr[dev->nr]);
  642. if (err < 0)
  643. goto fail_unreg;
  644. }
  645. /* set PCI interrupt */
  646. cx_set(PCI_INT_MSK, 0xff);
  647. return 0;
  648. fail_unreg:
  649. while (i >= 0)
  650. cx25821_video_unregister(dev, i--);
  651. return err;
  652. }