cx25821-video.c 21 KB

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