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