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