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