cx23885-video.c revision 33cdeb35f559270d2c51ed641df69a9ac659bc22
1/* 2 * Driver for the Conexant CX23885 PCIe bridge 3 * 4 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22#include <linux/init.h> 23#include <linux/list.h> 24#include <linux/module.h> 25#include <linux/moduleparam.h> 26#include <linux/kmod.h> 27#include <linux/kernel.h> 28#include <linux/slab.h> 29#include <linux/interrupt.h> 30#include <linux/delay.h> 31#include <linux/kthread.h> 32#include <asm/div64.h> 33 34#include "cx23885.h" 35#include <media/v4l2-common.h> 36#include <media/v4l2-ioctl.h> 37#include "cx23885-ioctl.h" 38#include "tuner-xc2028.h" 39 40#include <media/cx25840.h> 41 42MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards"); 43MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>"); 44MODULE_LICENSE("GPL"); 45 46/* ------------------------------------------------------------------ */ 47 48static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET }; 49static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET }; 50static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET }; 51 52module_param_array(video_nr, int, NULL, 0444); 53module_param_array(vbi_nr, int, NULL, 0444); 54module_param_array(radio_nr, int, NULL, 0444); 55 56MODULE_PARM_DESC(video_nr, "video device numbers"); 57MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); 58MODULE_PARM_DESC(radio_nr, "radio device numbers"); 59 60static unsigned int video_debug; 61module_param(video_debug, int, 0644); 62MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); 63 64static unsigned int irq_debug; 65module_param(irq_debug, int, 0644); 66MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]"); 67 68static unsigned int vid_limit = 16; 69module_param(vid_limit, int, 0644); 70MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes"); 71 72#define dprintk(level, fmt, arg...)\ 73 do { if (video_debug >= level)\ 74 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\ 75 } while (0) 76 77/* ------------------------------------------------------------------- */ 78/* static data */ 79 80#define FORMAT_FLAGS_PACKED 0x01 81#if 0 82static struct cx23885_fmt formats[] = { 83 { 84 .name = "8 bpp, gray", 85 .fourcc = V4L2_PIX_FMT_GREY, 86 .depth = 8, 87 .flags = FORMAT_FLAGS_PACKED, 88 }, { 89 .name = "15 bpp RGB, le", 90 .fourcc = V4L2_PIX_FMT_RGB555, 91 .depth = 16, 92 .flags = FORMAT_FLAGS_PACKED, 93 }, { 94 .name = "15 bpp RGB, be", 95 .fourcc = V4L2_PIX_FMT_RGB555X, 96 .depth = 16, 97 .flags = FORMAT_FLAGS_PACKED, 98 }, { 99 .name = "16 bpp RGB, le", 100 .fourcc = V4L2_PIX_FMT_RGB565, 101 .depth = 16, 102 .flags = FORMAT_FLAGS_PACKED, 103 }, { 104 .name = "16 bpp RGB, be", 105 .fourcc = V4L2_PIX_FMT_RGB565X, 106 .depth = 16, 107 .flags = FORMAT_FLAGS_PACKED, 108 }, { 109 .name = "24 bpp RGB, le", 110 .fourcc = V4L2_PIX_FMT_BGR24, 111 .depth = 24, 112 .flags = FORMAT_FLAGS_PACKED, 113 }, { 114 .name = "32 bpp RGB, le", 115 .fourcc = V4L2_PIX_FMT_BGR32, 116 .depth = 32, 117 .flags = FORMAT_FLAGS_PACKED, 118 }, { 119 .name = "32 bpp RGB, be", 120 .fourcc = V4L2_PIX_FMT_RGB32, 121 .depth = 32, 122 .flags = FORMAT_FLAGS_PACKED, 123 }, { 124 .name = "4:2:2, packed, YUYV", 125 .fourcc = V4L2_PIX_FMT_YUYV, 126 .depth = 16, 127 .flags = FORMAT_FLAGS_PACKED, 128 }, { 129 .name = "4:2:2, packed, UYVY", 130 .fourcc = V4L2_PIX_FMT_UYVY, 131 .depth = 16, 132 .flags = FORMAT_FLAGS_PACKED, 133 }, 134}; 135#else 136static struct cx23885_fmt formats[] = { 137 { 138#if 0 139 .name = "4:2:2, packed, UYVY", 140 .fourcc = V4L2_PIX_FMT_UYVY, 141 .depth = 16, 142 .flags = FORMAT_FLAGS_PACKED, 143 }, { 144#endif 145 .name = "4:2:2, packed, YUYV", 146 .fourcc = V4L2_PIX_FMT_YUYV, 147 .depth = 16, 148 .flags = FORMAT_FLAGS_PACKED, 149 } 150}; 151#endif 152 153static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc) 154{ 155 unsigned int i; 156 157 for (i = 0; i < ARRAY_SIZE(formats); i++) 158 if (formats[i].fourcc == fourcc) 159 return formats+i; 160 161 printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__, 162 (fourcc & 0xff), 163 ((fourcc >> 8) & 0xff), 164 ((fourcc >> 16) & 0xff), 165 ((fourcc >> 24) & 0xff) 166 ); 167 return NULL; 168} 169 170/* ------------------------------------------------------------------- */ 171 172static const struct v4l2_queryctrl no_ctl = { 173 .name = "42", 174 .flags = V4L2_CTRL_FLAG_DISABLED, 175}; 176 177static struct cx23885_ctrl cx23885_ctls[] = { 178 /* --- video --- */ 179 { 180 .v = { 181 .id = V4L2_CID_BRIGHTNESS, 182 .name = "Brightness", 183 .minimum = 0x00, 184 .maximum = 0xff, 185 .step = 1, 186 .default_value = 0x7f, 187 .type = V4L2_CTRL_TYPE_INTEGER, 188 }, 189 .off = 128, 190 .reg = LUMA_CTRL, 191 .mask = 0x00ff, 192 .shift = 0, 193 }, { 194 .v = { 195 .id = V4L2_CID_CONTRAST, 196 .name = "Contrast", 197 .minimum = 0, 198 .maximum = 0x7f, 199 .step = 1, 200 .default_value = 0x3f, 201 .type = V4L2_CTRL_TYPE_INTEGER, 202 }, 203 .off = 0, 204 .reg = LUMA_CTRL, 205 .mask = 0xff00, 206 .shift = 8, 207 }, { 208 .v = { 209 .id = V4L2_CID_HUE, 210 .name = "Hue", 211 .minimum = -127, 212 .maximum = 128, 213 .step = 1, 214 .default_value = 0x0, 215 .type = V4L2_CTRL_TYPE_INTEGER, 216 }, 217 .off = 128, 218 .reg = CHROMA_CTRL, 219 .mask = 0xff0000, 220 .shift = 16, 221 }, { 222 /* strictly, this only describes only U saturation. 223 * V saturation is handled specially through code. 224 */ 225 .v = { 226 .id = V4L2_CID_SATURATION, 227 .name = "Saturation", 228 .minimum = 0, 229 .maximum = 0x7f, 230 .step = 1, 231 .default_value = 0x3f, 232 .type = V4L2_CTRL_TYPE_INTEGER, 233 }, 234 .off = 0, 235 .reg = CHROMA_CTRL, 236 .mask = 0x00ff, 237 .shift = 0, 238 }, { 239 /* --- audio --- */ 240 .v = { 241 .id = V4L2_CID_AUDIO_MUTE, 242 .name = "Mute", 243 .minimum = 0, 244 .maximum = 1, 245 .default_value = 1, 246 .type = V4L2_CTRL_TYPE_BOOLEAN, 247 }, 248 .reg = PATH1_CTL1, 249 .mask = (0x1f << 24), 250 .shift = 24, 251 }, { 252 .v = { 253 .id = V4L2_CID_AUDIO_VOLUME, 254 .name = "Volume", 255 .minimum = 0, 256 .maximum = 0x3f, 257 .step = 1, 258 .default_value = 0x3f, 259 .type = V4L2_CTRL_TYPE_INTEGER, 260 }, 261 .reg = PATH1_VOL_CTL, 262 .mask = 0xff, 263 .shift = 0, 264 } 265}; 266static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls); 267 268/* Must be sorted from low to high control ID! */ 269static const u32 cx23885_user_ctrls[] = { 270 V4L2_CID_USER_CLASS, 271 V4L2_CID_BRIGHTNESS, 272 V4L2_CID_CONTRAST, 273 V4L2_CID_SATURATION, 274 V4L2_CID_HUE, 275 V4L2_CID_AUDIO_VOLUME, 276 V4L2_CID_AUDIO_MUTE, 277 0 278}; 279 280static const u32 *ctrl_classes[] = { 281 cx23885_user_ctrls, 282 NULL 283}; 284 285void cx23885_video_wakeup(struct cx23885_dev *dev, 286 struct cx23885_dmaqueue *q, u32 count) 287{ 288 struct cx23885_buffer *buf; 289 int bc; 290 291 for (bc = 0;; bc++) { 292 if (list_empty(&q->active)) 293 break; 294 buf = list_entry(q->active.next, 295 struct cx23885_buffer, vb.queue); 296 297 /* count comes from the hw and is is 16bit wide -- 298 * this trick handles wrap-arounds correctly for 299 * up to 32767 buffers in flight... */ 300 if ((s16) (count - buf->count) < 0) 301 break; 302 303 do_gettimeofday(&buf->vb.ts); 304 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i, 305 count, buf->count); 306 buf->vb.state = VIDEOBUF_DONE; 307 list_del(&buf->vb.queue); 308 wake_up(&buf->vb.done); 309 } 310 if (list_empty(&q->active)) 311 del_timer(&q->timeout); 312 else 313 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 314 if (bc != 1) 315 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n", 316 __func__, bc); 317} 318 319static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm) 320{ 321 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", 322 __func__, 323 (unsigned int)norm, 324 v4l2_norm_to_name(norm)); 325 326 dev->tvnorm = norm; 327 328 call_all(dev, core, s_std, norm); 329 330 return 0; 331} 332 333static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev, 334 struct pci_dev *pci, 335 struct video_device *template, 336 char *type) 337{ 338 struct video_device *vfd; 339 dprintk(1, "%s()\n", __func__); 340 341 vfd = video_device_alloc(); 342 if (NULL == vfd) 343 return NULL; 344 *vfd = *template; 345 vfd->v4l2_dev = &dev->v4l2_dev; 346 vfd->release = video_device_release; 347 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", 348 dev->name, type, cx23885_boards[dev->board].name); 349 video_set_drvdata(vfd, dev); 350 return vfd; 351} 352 353static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl) 354{ 355 int i; 356 357 if (qctrl->id < V4L2_CID_BASE || 358 qctrl->id >= V4L2_CID_LASTP1) 359 return -EINVAL; 360 for (i = 0; i < CX23885_CTLS; i++) 361 if (cx23885_ctls[i].v.id == qctrl->id) 362 break; 363 if (i == CX23885_CTLS) { 364 *qctrl = no_ctl; 365 return 0; 366 } 367 *qctrl = cx23885_ctls[i].v; 368 return 0; 369} 370 371/* ------------------------------------------------------------------- */ 372/* resource management */ 373 374static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh, 375 unsigned int bit) 376{ 377 dprintk(1, "%s()\n", __func__); 378 if (fh->resources & bit) 379 /* have it already allocated */ 380 return 1; 381 382 /* is it free? */ 383 mutex_lock(&dev->lock); 384 if (dev->resources & bit) { 385 /* no, someone else uses it */ 386 mutex_unlock(&dev->lock); 387 return 0; 388 } 389 /* it's free, grab it */ 390 fh->resources |= bit; 391 dev->resources |= bit; 392 dprintk(1, "res: get %d\n", bit); 393 mutex_unlock(&dev->lock); 394 return 1; 395} 396 397static int res_check(struct cx23885_fh *fh, unsigned int bit) 398{ 399 return fh->resources & bit; 400} 401 402static int res_locked(struct cx23885_dev *dev, unsigned int bit) 403{ 404 return dev->resources & bit; 405} 406 407static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh, 408 unsigned int bits) 409{ 410 BUG_ON((fh->resources & bits) != bits); 411 dprintk(1, "%s()\n", __func__); 412 413 mutex_lock(&dev->lock); 414 fh->resources &= ~bits; 415 dev->resources &= ~bits; 416 dprintk(1, "res: put %d\n", bits); 417 mutex_unlock(&dev->lock); 418} 419 420static int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data) 421{ 422 /* 8 bit registers, 8 bit values */ 423 u8 buf[] = { reg, data }; 424 425 struct i2c_msg msg = { .addr = 0x98 >> 1, 426 .flags = 0, .buf = buf, .len = 2 }; 427 428 return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1); 429} 430 431static u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg) 432{ 433 /* 8 bit registers, 8 bit values */ 434 int ret; 435 u8 b0[] = { reg }; 436 u8 b1[] = { 0 }; 437 438 struct i2c_msg msg[] = { 439 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 }, 440 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 } 441 }; 442 443 ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2); 444 if (ret != 2) 445 printk(KERN_ERR "%s() error\n", __func__); 446 447 return b1[0]; 448} 449 450static void cx23885_flatiron_dump(struct cx23885_dev *dev) 451{ 452 int i; 453 dprintk(1, "Flatiron dump\n"); 454 for (i = 0; i < 0x24; i++) { 455 dprintk(1, "FI[%02x] = %02x\n", i, 456 cx23885_flatiron_read(dev, i)); 457 } 458} 459 460static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input) 461{ 462 u8 val; 463 dprintk(1, "%s(input = %d)\n", __func__, input); 464 465 if (input == 1) 466 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL; 467 else if (input == 2) 468 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL; 469 else 470 return -EINVAL; 471 472 val |= 0x20; /* Enable clock to delta-sigma and dec filter */ 473 474 cx23885_flatiron_write(dev, CH_PWR_CTRL1, val); 475 476 /* Wake up */ 477 cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0); 478 479 if (video_debug) 480 cx23885_flatiron_dump(dev); 481 482 return 0; 483} 484 485static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input) 486{ 487 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n", 488 __func__, 489 input, INPUT(input)->vmux, 490 INPUT(input)->gpio0, INPUT(input)->gpio1, 491 INPUT(input)->gpio2, INPUT(input)->gpio3); 492 dev->input = input; 493 494 if (dev->board == CX23885_BOARD_MYGICA_X8506 || 495 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) { 496 /* Select Analog TV */ 497 if (INPUT(input)->type == CX23885_VMUX_TELEVISION) 498 cx23885_gpio_clear(dev, GPIO_0); 499 } 500 501 /* Tell the internal A/V decoder */ 502 v4l2_subdev_call(dev->sd_cx25840, video, s_routing, 503 INPUT(input)->vmux, 0, 0); 504 505 if (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) { 506 /* Configure audio routing */ 507 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing, 508 INPUT(input)->amux, 0, 0); 509 510 if (INPUT(input)->amux == CX25840_AUDIO7) 511 cx23885_flatiron_mux(dev, 1); 512 else if (INPUT(input)->amux == CX25840_AUDIO6) 513 cx23885_flatiron_mux(dev, 2); 514 } 515 516 return 0; 517} 518 519/* ------------------------------------------------------------------ */ 520static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width, 521 unsigned int height, enum v4l2_field field) 522{ 523 dprintk(1, "%s()\n", __func__); 524 return 0; 525} 526 527static int cx23885_start_video_dma(struct cx23885_dev *dev, 528 struct cx23885_dmaqueue *q, 529 struct cx23885_buffer *buf) 530{ 531 dprintk(1, "%s()\n", __func__); 532 533 /* setup fifo + format */ 534 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01], 535 buf->bpl, buf->risc.dma); 536 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field); 537 538 /* reset counter */ 539 cx_write(VID_A_GPCNT_CTL, 3); 540 q->count = 1; 541 542 /* enable irq */ 543 cx23885_irq_add_enable(dev, 0x01); 544 cx_set(VID_A_INT_MSK, 0x000011); 545 546 /* start dma */ 547 cx_set(DEV_CNTRL2, (1<<5)); 548 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */ 549 550 return 0; 551} 552 553 554static int cx23885_restart_video_queue(struct cx23885_dev *dev, 555 struct cx23885_dmaqueue *q) 556{ 557 struct cx23885_buffer *buf, *prev; 558 struct list_head *item; 559 dprintk(1, "%s()\n", __func__); 560 561 if (!list_empty(&q->active)) { 562 buf = list_entry(q->active.next, struct cx23885_buffer, 563 vb.queue); 564 dprintk(2, "restart_queue [%p/%d]: restart dma\n", 565 buf, buf->vb.i); 566 cx23885_start_video_dma(dev, q, buf); 567 list_for_each(item, &q->active) { 568 buf = list_entry(item, struct cx23885_buffer, 569 vb.queue); 570 buf->count = q->count++; 571 } 572 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 573 return 0; 574 } 575 576 prev = NULL; 577 for (;;) { 578 if (list_empty(&q->queued)) 579 return 0; 580 buf = list_entry(q->queued.next, struct cx23885_buffer, 581 vb.queue); 582 if (NULL == prev) { 583 list_move_tail(&buf->vb.queue, &q->active); 584 cx23885_start_video_dma(dev, q, buf); 585 buf->vb.state = VIDEOBUF_ACTIVE; 586 buf->count = q->count++; 587 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 588 dprintk(2, "[%p/%d] restart_queue - first active\n", 589 buf, buf->vb.i); 590 591 } else if (prev->vb.width == buf->vb.width && 592 prev->vb.height == buf->vb.height && 593 prev->fmt == buf->fmt) { 594 list_move_tail(&buf->vb.queue, &q->active); 595 buf->vb.state = VIDEOBUF_ACTIVE; 596 buf->count = q->count++; 597 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 598 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */ 599 dprintk(2, "[%p/%d] restart_queue - move to active\n", 600 buf, buf->vb.i); 601 } else { 602 return 0; 603 } 604 prev = buf; 605 } 606} 607 608static int buffer_setup(struct videobuf_queue *q, unsigned int *count, 609 unsigned int *size) 610{ 611 struct cx23885_fh *fh = q->priv_data; 612 613 *size = fh->fmt->depth*fh->width*fh->height >> 3; 614 if (0 == *count) 615 *count = 32; 616 if (*size * *count > vid_limit * 1024 * 1024) 617 *count = (vid_limit * 1024 * 1024) / *size; 618 return 0; 619} 620 621static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, 622 enum v4l2_field field) 623{ 624 struct cx23885_fh *fh = q->priv_data; 625 struct cx23885_dev *dev = fh->dev; 626 struct cx23885_buffer *buf = 627 container_of(vb, struct cx23885_buffer, vb); 628 int rc, init_buffer = 0; 629 u32 line0_offset, line1_offset; 630 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); 631 632 BUG_ON(NULL == fh->fmt); 633 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) || 634 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm)) 635 return -EINVAL; 636 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3; 637 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 638 return -EINVAL; 639 640 if (buf->fmt != fh->fmt || 641 buf->vb.width != fh->width || 642 buf->vb.height != fh->height || 643 buf->vb.field != field) { 644 buf->fmt = fh->fmt; 645 buf->vb.width = fh->width; 646 buf->vb.height = fh->height; 647 buf->vb.field = field; 648 init_buffer = 1; 649 } 650 651 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 652 init_buffer = 1; 653 rc = videobuf_iolock(q, &buf->vb, NULL); 654 if (0 != rc) 655 goto fail; 656 } 657 658 if (init_buffer) { 659 buf->bpl = buf->vb.width * buf->fmt->depth >> 3; 660 switch (buf->vb.field) { 661 case V4L2_FIELD_TOP: 662 cx23885_risc_buffer(dev->pci, &buf->risc, 663 dma->sglist, 0, UNSET, 664 buf->bpl, 0, buf->vb.height); 665 break; 666 case V4L2_FIELD_BOTTOM: 667 cx23885_risc_buffer(dev->pci, &buf->risc, 668 dma->sglist, UNSET, 0, 669 buf->bpl, 0, buf->vb.height); 670 break; 671 case V4L2_FIELD_INTERLACED: 672 if (dev->tvnorm & V4L2_STD_NTSC) { 673 /* cx25840 transmits NTSC bottom field first */ 674 dprintk(1, "%s() Creating NTSC risc\n", 675 __func__); 676 line0_offset = buf->bpl; 677 line1_offset = 0; 678 } else { 679 /* All other formats are top field first */ 680 dprintk(1, "%s() Creating PAL/SECAM risc\n", 681 __func__); 682 line0_offset = 0; 683 line1_offset = buf->bpl; 684 } 685 cx23885_risc_buffer(dev->pci, &buf->risc, 686 dma->sglist, line0_offset, 687 line1_offset, 688 buf->bpl, buf->bpl, 689 buf->vb.height >> 1); 690 break; 691 case V4L2_FIELD_SEQ_TB: 692 cx23885_risc_buffer(dev->pci, &buf->risc, 693 dma->sglist, 694 0, buf->bpl * (buf->vb.height >> 1), 695 buf->bpl, 0, 696 buf->vb.height >> 1); 697 break; 698 case V4L2_FIELD_SEQ_BT: 699 cx23885_risc_buffer(dev->pci, &buf->risc, 700 dma->sglist, 701 buf->bpl * (buf->vb.height >> 1), 0, 702 buf->bpl, 0, 703 buf->vb.height >> 1); 704 break; 705 default: 706 BUG(); 707 } 708 } 709 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n", 710 buf, buf->vb.i, 711 fh->width, fh->height, fh->fmt->depth, fh->fmt->name, 712 (unsigned long)buf->risc.dma); 713 714 buf->vb.state = VIDEOBUF_PREPARED; 715 return 0; 716 717 fail: 718 cx23885_free_buffer(q, buf); 719 return rc; 720} 721 722static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 723{ 724 struct cx23885_buffer *buf = container_of(vb, 725 struct cx23885_buffer, vb); 726 struct cx23885_buffer *prev; 727 struct cx23885_fh *fh = vq->priv_data; 728 struct cx23885_dev *dev = fh->dev; 729 struct cx23885_dmaqueue *q = &dev->vidq; 730 731 /* add jump to stopper */ 732 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC); 733 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma); 734 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ 735 736 if (!list_empty(&q->queued)) { 737 list_add_tail(&buf->vb.queue, &q->queued); 738 buf->vb.state = VIDEOBUF_QUEUED; 739 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", 740 buf, buf->vb.i); 741 742 } else if (list_empty(&q->active)) { 743 list_add_tail(&buf->vb.queue, &q->active); 744 cx23885_start_video_dma(dev, q, buf); 745 buf->vb.state = VIDEOBUF_ACTIVE; 746 buf->count = q->count++; 747 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); 748 dprintk(2, "[%p/%d] buffer_queue - first active\n", 749 buf, buf->vb.i); 750 751 } else { 752 prev = list_entry(q->active.prev, struct cx23885_buffer, 753 vb.queue); 754 if (prev->vb.width == buf->vb.width && 755 prev->vb.height == buf->vb.height && 756 prev->fmt == buf->fmt) { 757 list_add_tail(&buf->vb.queue, &q->active); 758 buf->vb.state = VIDEOBUF_ACTIVE; 759 buf->count = q->count++; 760 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); 761 /* 64 bit bits 63-32 */ 762 prev->risc.jmp[2] = cpu_to_le32(0); 763 dprintk(2, "[%p/%d] buffer_queue - append to active\n", 764 buf, buf->vb.i); 765 766 } else { 767 list_add_tail(&buf->vb.queue, &q->queued); 768 buf->vb.state = VIDEOBUF_QUEUED; 769 dprintk(2, "[%p/%d] buffer_queue - first queued\n", 770 buf, buf->vb.i); 771 } 772 } 773} 774 775static void buffer_release(struct videobuf_queue *q, 776 struct videobuf_buffer *vb) 777{ 778 struct cx23885_buffer *buf = container_of(vb, 779 struct cx23885_buffer, vb); 780 781 cx23885_free_buffer(q, buf); 782} 783 784static struct videobuf_queue_ops cx23885_video_qops = { 785 .buf_setup = buffer_setup, 786 .buf_prepare = buffer_prepare, 787 .buf_queue = buffer_queue, 788 .buf_release = buffer_release, 789}; 790 791static struct videobuf_queue *get_queue(struct cx23885_fh *fh) 792{ 793 switch (fh->type) { 794 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 795 return &fh->vidq; 796 case V4L2_BUF_TYPE_VBI_CAPTURE: 797 return &fh->vbiq; 798 default: 799 BUG(); 800 return NULL; 801 } 802} 803 804static int get_resource(struct cx23885_fh *fh) 805{ 806 switch (fh->type) { 807 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 808 return RESOURCE_VIDEO; 809 case V4L2_BUF_TYPE_VBI_CAPTURE: 810 return RESOURCE_VBI; 811 default: 812 BUG(); 813 return 0; 814 } 815} 816 817static int video_open(struct file *file) 818{ 819 struct video_device *vdev = video_devdata(file); 820 struct cx23885_dev *dev = video_drvdata(file); 821 struct cx23885_fh *fh; 822 enum v4l2_buf_type type = 0; 823 int radio = 0; 824 825 switch (vdev->vfl_type) { 826 case VFL_TYPE_GRABBER: 827 type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 828 break; 829 case VFL_TYPE_VBI: 830 type = V4L2_BUF_TYPE_VBI_CAPTURE; 831 break; 832 case VFL_TYPE_RADIO: 833 radio = 1; 834 break; 835 } 836 837 dprintk(1, "open dev=%s radio=%d type=%s\n", 838 video_device_node_name(vdev), radio, v4l2_type_names[type]); 839 840 /* allocate + initialize per filehandle data */ 841 fh = kzalloc(sizeof(*fh), GFP_KERNEL); 842 if (NULL == fh) 843 return -ENOMEM; 844 845 file->private_data = fh; 846 fh->dev = dev; 847 fh->radio = radio; 848 fh->type = type; 849 fh->width = 320; 850 fh->height = 240; 851 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV); 852 853 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops, 854 &dev->pci->dev, &dev->slock, 855 V4L2_BUF_TYPE_VIDEO_CAPTURE, 856 V4L2_FIELD_INTERLACED, 857 sizeof(struct cx23885_buffer), 858 fh, NULL); 859 860 videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops, 861 &dev->pci->dev, &dev->slock, 862 V4L2_BUF_TYPE_VBI_CAPTURE, 863 V4L2_FIELD_SEQ_TB, 864 sizeof(struct cx23885_buffer), 865 fh, NULL); 866 867 868 dprintk(1, "post videobuf_queue_init()\n"); 869 870 return 0; 871} 872 873static ssize_t video_read(struct file *file, char __user *data, 874 size_t count, loff_t *ppos) 875{ 876 struct cx23885_fh *fh = file->private_data; 877 878 switch (fh->type) { 879 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 880 if (res_locked(fh->dev, RESOURCE_VIDEO)) 881 return -EBUSY; 882 return videobuf_read_one(&fh->vidq, data, count, ppos, 883 file->f_flags & O_NONBLOCK); 884 case V4L2_BUF_TYPE_VBI_CAPTURE: 885 if (!res_get(fh->dev, fh, RESOURCE_VBI)) 886 return -EBUSY; 887 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1, 888 file->f_flags & O_NONBLOCK); 889 default: 890 BUG(); 891 return 0; 892 } 893} 894 895static unsigned int video_poll(struct file *file, 896 struct poll_table_struct *wait) 897{ 898 struct cx23885_fh *fh = file->private_data; 899 struct cx23885_buffer *buf; 900 unsigned int rc = POLLERR; 901 902 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) { 903 if (!res_get(fh->dev, fh, RESOURCE_VBI)) 904 return POLLERR; 905 return videobuf_poll_stream(file, &fh->vbiq, wait); 906 } 907 908 mutex_lock(&fh->vidq.vb_lock); 909 if (res_check(fh, RESOURCE_VIDEO)) { 910 /* streaming capture */ 911 if (list_empty(&fh->vidq.stream)) 912 goto done; 913 buf = list_entry(fh->vidq.stream.next, 914 struct cx23885_buffer, vb.stream); 915 } else { 916 /* read() capture */ 917 buf = (struct cx23885_buffer *)fh->vidq.read_buf; 918 if (NULL == buf) 919 goto done; 920 } 921 poll_wait(file, &buf->vb.done, wait); 922 if (buf->vb.state == VIDEOBUF_DONE || 923 buf->vb.state == VIDEOBUF_ERROR) 924 rc = POLLIN|POLLRDNORM; 925 else 926 rc = 0; 927done: 928 mutex_unlock(&fh->vidq.vb_lock); 929 return rc; 930} 931 932static int video_release(struct file *file) 933{ 934 struct cx23885_fh *fh = file->private_data; 935 struct cx23885_dev *dev = fh->dev; 936 937 /* turn off overlay */ 938 if (res_check(fh, RESOURCE_OVERLAY)) { 939 /* FIXME */ 940 res_free(dev, fh, RESOURCE_OVERLAY); 941 } 942 943 /* stop video capture */ 944 if (res_check(fh, RESOURCE_VIDEO)) { 945 videobuf_queue_cancel(&fh->vidq); 946 res_free(dev, fh, RESOURCE_VIDEO); 947 } 948 if (fh->vidq.read_buf) { 949 buffer_release(&fh->vidq, fh->vidq.read_buf); 950 kfree(fh->vidq.read_buf); 951 } 952 953 /* stop vbi capture */ 954 if (res_check(fh, RESOURCE_VBI)) { 955 if (fh->vbiq.streaming) 956 videobuf_streamoff(&fh->vbiq); 957 if (fh->vbiq.reading) 958 videobuf_read_stop(&fh->vbiq); 959 res_free(dev, fh, RESOURCE_VBI); 960 } 961 962 videobuf_mmap_free(&fh->vidq); 963 file->private_data = NULL; 964 kfree(fh); 965 966 /* We are not putting the tuner to sleep here on exit, because 967 * we want to use the mpeg encoder in another session to capture 968 * tuner video. Closing this will result in no video to the encoder. 969 */ 970 971 return 0; 972} 973 974static int video_mmap(struct file *file, struct vm_area_struct *vma) 975{ 976 struct cx23885_fh *fh = file->private_data; 977 978 return videobuf_mmap_mapper(get_queue(fh), vma); 979} 980 981/* ------------------------------------------------------------------ */ 982/* VIDEO CTRL IOCTLS */ 983 984static int cx23885_get_control(struct cx23885_dev *dev, 985 struct v4l2_control *ctl) 986{ 987 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__); 988 call_all(dev, core, g_ctrl, ctl); 989 return 0; 990} 991 992static int cx23885_set_control(struct cx23885_dev *dev, 993 struct v4l2_control *ctl) 994{ 995 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__); 996 call_all(dev, core, s_ctrl, ctl); 997 998 return 0; 999} 1000 1001static void init_controls(struct cx23885_dev *dev) 1002{ 1003 struct v4l2_control ctrl; 1004 int i; 1005 1006 for (i = 0; i < CX23885_CTLS; i++) { 1007 ctrl.id = cx23885_ctls[i].v.id; 1008 ctrl.value = cx23885_ctls[i].v.default_value; 1009 1010 cx23885_set_control(dev, &ctrl); 1011 } 1012} 1013 1014/* ------------------------------------------------------------------ */ 1015/* VIDEO IOCTLS */ 1016 1017static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 1018 struct v4l2_format *f) 1019{ 1020 struct cx23885_fh *fh = priv; 1021 1022 f->fmt.pix.width = fh->width; 1023 f->fmt.pix.height = fh->height; 1024 f->fmt.pix.field = fh->vidq.field; 1025 f->fmt.pix.pixelformat = fh->fmt->fourcc; 1026 f->fmt.pix.bytesperline = 1027 (f->fmt.pix.width * fh->fmt->depth) >> 3; 1028 f->fmt.pix.sizeimage = 1029 f->fmt.pix.height * f->fmt.pix.bytesperline; 1030 1031 return 0; 1032} 1033 1034static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 1035 struct v4l2_format *f) 1036{ 1037 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1038 struct cx23885_fmt *fmt; 1039 enum v4l2_field field; 1040 unsigned int maxw, maxh; 1041 1042 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 1043 if (NULL == fmt) 1044 return -EINVAL; 1045 1046 field = f->fmt.pix.field; 1047 maxw = norm_maxw(dev->tvnorm); 1048 maxh = norm_maxh(dev->tvnorm); 1049 1050 if (V4L2_FIELD_ANY == field) { 1051 field = (f->fmt.pix.height > maxh/2) 1052 ? V4L2_FIELD_INTERLACED 1053 : V4L2_FIELD_BOTTOM; 1054 } 1055 1056 switch (field) { 1057 case V4L2_FIELD_TOP: 1058 case V4L2_FIELD_BOTTOM: 1059 maxh = maxh / 2; 1060 break; 1061 case V4L2_FIELD_INTERLACED: 1062 break; 1063 default: 1064 return -EINVAL; 1065 } 1066 1067 f->fmt.pix.field = field; 1068 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2, 1069 &f->fmt.pix.height, 32, maxh, 0, 0); 1070 f->fmt.pix.bytesperline = 1071 (f->fmt.pix.width * fmt->depth) >> 3; 1072 f->fmt.pix.sizeimage = 1073 f->fmt.pix.height * f->fmt.pix.bytesperline; 1074 1075 return 0; 1076} 1077 1078static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 1079 struct v4l2_format *f) 1080{ 1081 struct cx23885_fh *fh = priv; 1082 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1083 struct v4l2_mbus_framefmt mbus_fmt; 1084 int err; 1085 1086 dprintk(2, "%s()\n", __func__); 1087 err = vidioc_try_fmt_vid_cap(file, priv, f); 1088 1089 if (0 != err) 1090 return err; 1091 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat); 1092 fh->width = f->fmt.pix.width; 1093 fh->height = f->fmt.pix.height; 1094 fh->vidq.field = f->fmt.pix.field; 1095 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, 1096 fh->width, fh->height, fh->vidq.field); 1097 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED); 1098 call_all(dev, video, s_mbus_fmt, &mbus_fmt); 1099 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt); 1100 return 0; 1101} 1102 1103static int vidioc_querycap(struct file *file, void *priv, 1104 struct v4l2_capability *cap) 1105{ 1106 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1107 1108 strcpy(cap->driver, "cx23885"); 1109 strlcpy(cap->card, cx23885_boards[dev->board].name, 1110 sizeof(cap->card)); 1111 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci)); 1112 cap->capabilities = 1113 V4L2_CAP_VIDEO_CAPTURE | 1114 V4L2_CAP_READWRITE | 1115 V4L2_CAP_STREAMING | 1116 V4L2_CAP_VBI_CAPTURE; 1117 if (UNSET != dev->tuner_type) 1118 cap->capabilities |= V4L2_CAP_TUNER; 1119 return 0; 1120} 1121 1122static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 1123 struct v4l2_fmtdesc *f) 1124{ 1125 if (unlikely(f->index >= ARRAY_SIZE(formats))) 1126 return -EINVAL; 1127 1128 strlcpy(f->description, formats[f->index].name, 1129 sizeof(f->description)); 1130 f->pixelformat = formats[f->index].fourcc; 1131 1132 return 0; 1133} 1134 1135static int vidioc_reqbufs(struct file *file, void *priv, 1136 struct v4l2_requestbuffers *p) 1137{ 1138 struct cx23885_fh *fh = priv; 1139 return videobuf_reqbufs(get_queue(fh), p); 1140} 1141 1142static int vidioc_querybuf(struct file *file, void *priv, 1143 struct v4l2_buffer *p) 1144{ 1145 struct cx23885_fh *fh = priv; 1146 return videobuf_querybuf(get_queue(fh), p); 1147} 1148 1149static int vidioc_qbuf(struct file *file, void *priv, 1150 struct v4l2_buffer *p) 1151{ 1152 struct cx23885_fh *fh = priv; 1153 return videobuf_qbuf(get_queue(fh), p); 1154} 1155 1156static int vidioc_dqbuf(struct file *file, void *priv, 1157 struct v4l2_buffer *p) 1158{ 1159 struct cx23885_fh *fh = priv; 1160 return videobuf_dqbuf(get_queue(fh), p, 1161 file->f_flags & O_NONBLOCK); 1162} 1163 1164static int vidioc_streamon(struct file *file, void *priv, 1165 enum v4l2_buf_type i) 1166{ 1167 struct cx23885_fh *fh = priv; 1168 struct cx23885_dev *dev = fh->dev; 1169 dprintk(1, "%s()\n", __func__); 1170 1171 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) && 1172 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)) 1173 return -EINVAL; 1174 if (unlikely(i != fh->type)) 1175 return -EINVAL; 1176 1177 if (unlikely(!res_get(dev, fh, get_resource(fh)))) 1178 return -EBUSY; 1179 1180 /* Don't start VBI streaming unless vida streaming 1181 * has already started. 1182 */ 1183 if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) && 1184 ((cx_read(VID_A_DMA_CTL) & 0x11) == 0)) 1185 return -EINVAL; 1186 1187 return videobuf_streamon(get_queue(fh)); 1188} 1189 1190static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) 1191{ 1192 struct cx23885_fh *fh = priv; 1193 struct cx23885_dev *dev = fh->dev; 1194 int err, res; 1195 dprintk(1, "%s()\n", __func__); 1196 1197 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) && 1198 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)) 1199 return -EINVAL; 1200 if (i != fh->type) 1201 return -EINVAL; 1202 1203 res = get_resource(fh); 1204 err = videobuf_streamoff(get_queue(fh)); 1205 if (err < 0) 1206 return err; 1207 res_free(dev, fh, res); 1208 return 0; 1209} 1210 1211static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms) 1212{ 1213 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1214 dprintk(1, "%s()\n", __func__); 1215 1216 mutex_lock(&dev->lock); 1217 cx23885_set_tvnorm(dev, *tvnorms); 1218 mutex_unlock(&dev->lock); 1219 1220 return 0; 1221} 1222 1223static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i) 1224{ 1225 static const char *iname[] = { 1226 [CX23885_VMUX_COMPOSITE1] = "Composite1", 1227 [CX23885_VMUX_COMPOSITE2] = "Composite2", 1228 [CX23885_VMUX_COMPOSITE3] = "Composite3", 1229 [CX23885_VMUX_COMPOSITE4] = "Composite4", 1230 [CX23885_VMUX_SVIDEO] = "S-Video", 1231 [CX23885_VMUX_COMPONENT] = "Component", 1232 [CX23885_VMUX_TELEVISION] = "Television", 1233 [CX23885_VMUX_CABLE] = "Cable TV", 1234 [CX23885_VMUX_DVB] = "DVB", 1235 [CX23885_VMUX_DEBUG] = "for debug only", 1236 }; 1237 unsigned int n; 1238 dprintk(1, "%s()\n", __func__); 1239 1240 n = i->index; 1241 if (n >= 4) 1242 return -EINVAL; 1243 1244 if (0 == INPUT(n)->type) 1245 return -EINVAL; 1246 1247 i->index = n; 1248 i->type = V4L2_INPUT_TYPE_CAMERA; 1249 strcpy(i->name, iname[INPUT(n)->type]); 1250 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) || 1251 (CX23885_VMUX_CABLE == INPUT(n)->type)) { 1252 i->type = V4L2_INPUT_TYPE_TUNER; 1253 i->std = CX23885_NORMS; 1254 } 1255 return 0; 1256} 1257 1258static int vidioc_enum_input(struct file *file, void *priv, 1259 struct v4l2_input *i) 1260{ 1261 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1262 dprintk(1, "%s()\n", __func__); 1263 return cx23885_enum_input(dev, i); 1264} 1265 1266static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1267{ 1268 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1269 1270 *i = dev->input; 1271 dprintk(1, "%s() returns %d\n", __func__, *i); 1272 return 0; 1273} 1274 1275static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1276{ 1277 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1278 1279 dprintk(1, "%s(%d)\n", __func__, i); 1280 1281 if (i >= 4) { 1282 dprintk(1, "%s() -EINVAL\n", __func__); 1283 return -EINVAL; 1284 } 1285 1286 mutex_lock(&dev->lock); 1287 cx23885_video_mux(dev, i); 1288 mutex_unlock(&dev->lock); 1289 return 0; 1290} 1291 1292static int vidioc_log_status(struct file *file, void *priv) 1293{ 1294 struct cx23885_fh *fh = priv; 1295 struct cx23885_dev *dev = fh->dev; 1296 1297 printk(KERN_INFO 1298 "%s/0: ============ START LOG STATUS ============\n", 1299 dev->name); 1300 call_all(dev, core, log_status); 1301 printk(KERN_INFO 1302 "%s/0: ============= END LOG STATUS =============\n", 1303 dev->name); 1304 return 0; 1305} 1306 1307static int vidioc_queryctrl(struct file *file, void *priv, 1308 struct v4l2_queryctrl *qctrl) 1309{ 1310 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id); 1311 if (unlikely(qctrl->id == 0)) 1312 return -EINVAL; 1313 return cx23885_ctrl_query(qctrl); 1314} 1315 1316static int vidioc_g_ctrl(struct file *file, void *priv, 1317 struct v4l2_control *ctl) 1318{ 1319 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1320 1321 return cx23885_get_control(dev, ctl); 1322} 1323 1324static int vidioc_s_ctrl(struct file *file, void *priv, 1325 struct v4l2_control *ctl) 1326{ 1327 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1328 1329 return cx23885_set_control(dev, ctl); 1330} 1331 1332static int vidioc_g_tuner(struct file *file, void *priv, 1333 struct v4l2_tuner *t) 1334{ 1335 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1336 1337 if (unlikely(UNSET == dev->tuner_type)) 1338 return -EINVAL; 1339 if (0 != t->index) 1340 return -EINVAL; 1341 1342 strcpy(t->name, "Television"); 1343 1344 call_all(dev, tuner, g_tuner, t); 1345 return 0; 1346} 1347 1348static int vidioc_s_tuner(struct file *file, void *priv, 1349 struct v4l2_tuner *t) 1350{ 1351 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; 1352 1353 if (UNSET == dev->tuner_type) 1354 return -EINVAL; 1355 if (0 != t->index) 1356 return -EINVAL; 1357 /* Update the A/V core */ 1358 call_all(dev, tuner, s_tuner, t); 1359 1360 return 0; 1361} 1362 1363static int vidioc_g_frequency(struct file *file, void *priv, 1364 struct v4l2_frequency *f) 1365{ 1366 struct cx23885_fh *fh = priv; 1367 struct cx23885_dev *dev = fh->dev; 1368 1369 if (unlikely(UNSET == dev->tuner_type)) 1370 return -EINVAL; 1371 1372 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */ 1373 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1374 f->frequency = dev->freq; 1375 1376 call_all(dev, tuner, g_frequency, f); 1377 1378 return 0; 1379} 1380 1381static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f) 1382{ 1383 if (unlikely(UNSET == dev->tuner_type)) 1384 return -EINVAL; 1385 if (unlikely(f->tuner != 0)) 1386 return -EINVAL; 1387 1388 mutex_lock(&dev->lock); 1389 dev->freq = f->frequency; 1390 1391 call_all(dev, tuner, s_frequency, f); 1392 1393 /* When changing channels it is required to reset TVAUDIO */ 1394 msleep(10); 1395 1396 mutex_unlock(&dev->lock); 1397 1398 return 0; 1399} 1400 1401static int vidioc_s_frequency(struct file *file, void *priv, 1402 struct v4l2_frequency *f) 1403{ 1404 struct cx23885_fh *fh = priv; 1405 struct cx23885_dev *dev = fh->dev; 1406 1407 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)) 1408 return -EINVAL; 1409 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO)) 1410 return -EINVAL; 1411 1412 return 1413 cx23885_set_freq(dev, f); 1414} 1415 1416/* ----------------------------------------------------------- */ 1417 1418static void cx23885_vid_timeout(unsigned long data) 1419{ 1420 struct cx23885_dev *dev = (struct cx23885_dev *)data; 1421 struct cx23885_dmaqueue *q = &dev->vidq; 1422 struct cx23885_buffer *buf; 1423 unsigned long flags; 1424 1425 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]); 1426 1427 cx_clear(VID_A_DMA_CTL, 0x11); 1428 1429 spin_lock_irqsave(&dev->slock, flags); 1430 while (!list_empty(&q->active)) { 1431 buf = list_entry(q->active.next, 1432 struct cx23885_buffer, vb.queue); 1433 list_del(&buf->vb.queue); 1434 buf->vb.state = VIDEOBUF_ERROR; 1435 wake_up(&buf->vb.done); 1436 printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n", 1437 dev->name, buf, buf->vb.i, 1438 (unsigned long)buf->risc.dma); 1439 } 1440 cx23885_restart_video_queue(dev, q); 1441 spin_unlock_irqrestore(&dev->slock, flags); 1442} 1443 1444int cx23885_video_irq(struct cx23885_dev *dev, u32 status) 1445{ 1446 u32 mask, count; 1447 int handled = 0; 1448 1449 mask = cx_read(VID_A_INT_MSK); 1450 if (0 == (status & mask)) 1451 return handled; 1452 1453 cx_write(VID_A_INT_STAT, status); 1454 1455 /* risc op code error */ 1456 if ((status & VID_BC_MSK_OPC_ERR) || 1457 (status & VID_BC_MSK_SYNC) || 1458 (status & VID_BC_MSK_OF)) { 1459 1460 if (status & VID_BC_MSK_OPC_ERR) 1461 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n", 1462 VID_BC_MSK_OPC_ERR); 1463 1464 if (status & VID_BC_MSK_SYNC) 1465 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x)\n", 1466 VID_BC_MSK_SYNC); 1467 1468 if (status & VID_BC_MSK_OF) 1469 dprintk(7, " (VID_BC_MSK_OF 0x%08x)\n", 1470 VID_BC_MSK_OF); 1471 1472 printk(KERN_WARNING "%s: video risc op code error\n", 1473 dev->name); 1474 1475 cx_clear(VID_A_DMA_CTL, 0x11); 1476 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]); 1477 1478 } 1479 1480 /* Video */ 1481 if (status & VID_BC_MSK_RISCI1) { 1482 spin_lock(&dev->slock); 1483 count = cx_read(VID_A_GPCNT); 1484 cx23885_video_wakeup(dev, &dev->vidq, count); 1485 spin_unlock(&dev->slock); 1486 handled++; 1487 } 1488 if (status & VID_BC_MSK_RISCI2) { 1489 dprintk(2, "stopper video\n"); 1490 spin_lock(&dev->slock); 1491 cx23885_restart_video_queue(dev, &dev->vidq); 1492 spin_unlock(&dev->slock); 1493 handled++; 1494 } 1495 1496 /* Allow the VBI framework to process it's payload */ 1497 handled += cx23885_vbi_irq(dev, status); 1498 1499 return handled; 1500} 1501 1502/* ----------------------------------------------------------- */ 1503/* exported stuff */ 1504 1505static const struct v4l2_file_operations video_fops = { 1506 .owner = THIS_MODULE, 1507 .open = video_open, 1508 .release = video_release, 1509 .read = video_read, 1510 .poll = video_poll, 1511 .mmap = video_mmap, 1512 .ioctl = video_ioctl2, 1513}; 1514 1515static const struct v4l2_ioctl_ops video_ioctl_ops = { 1516 .vidioc_querycap = vidioc_querycap, 1517 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1518 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1519 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1520 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1521 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt, 1522 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt, 1523 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt, 1524 .vidioc_reqbufs = vidioc_reqbufs, 1525 .vidioc_querybuf = vidioc_querybuf, 1526 .vidioc_qbuf = vidioc_qbuf, 1527 .vidioc_dqbuf = vidioc_dqbuf, 1528 .vidioc_s_std = vidioc_s_std, 1529 .vidioc_enum_input = vidioc_enum_input, 1530 .vidioc_g_input = vidioc_g_input, 1531 .vidioc_s_input = vidioc_s_input, 1532 .vidioc_log_status = vidioc_log_status, 1533 .vidioc_queryctrl = vidioc_queryctrl, 1534 .vidioc_g_ctrl = vidioc_g_ctrl, 1535 .vidioc_s_ctrl = vidioc_s_ctrl, 1536 .vidioc_streamon = vidioc_streamon, 1537 .vidioc_streamoff = vidioc_streamoff, 1538 .vidioc_g_tuner = vidioc_g_tuner, 1539 .vidioc_s_tuner = vidioc_s_tuner, 1540 .vidioc_g_frequency = vidioc_g_frequency, 1541 .vidioc_s_frequency = vidioc_s_frequency, 1542 .vidioc_g_chip_ident = cx23885_g_chip_ident, 1543#ifdef CONFIG_VIDEO_ADV_DEBUG 1544 .vidioc_g_register = cx23885_g_register, 1545 .vidioc_s_register = cx23885_s_register, 1546#endif 1547}; 1548 1549static struct video_device cx23885_vbi_template; 1550static struct video_device cx23885_video_template = { 1551 .name = "cx23885-video", 1552 .fops = &video_fops, 1553 .ioctl_ops = &video_ioctl_ops, 1554 .tvnorms = CX23885_NORMS, 1555 .current_norm = V4L2_STD_NTSC_M, 1556}; 1557 1558static const struct v4l2_file_operations radio_fops = { 1559 .owner = THIS_MODULE, 1560 .open = video_open, 1561 .release = video_release, 1562 .ioctl = video_ioctl2, 1563}; 1564 1565 1566void cx23885_video_unregister(struct cx23885_dev *dev) 1567{ 1568 dprintk(1, "%s()\n", __func__); 1569 cx23885_irq_remove(dev, 0x01); 1570 1571 if (dev->vbi_dev) { 1572 if (video_is_registered(dev->vbi_dev)) 1573 video_unregister_device(dev->vbi_dev); 1574 else 1575 video_device_release(dev->vbi_dev); 1576 dev->vbi_dev = NULL; 1577 btcx_riscmem_free(dev->pci, &dev->vbiq.stopper); 1578 } 1579 if (dev->video_dev) { 1580 if (video_is_registered(dev->video_dev)) 1581 video_unregister_device(dev->video_dev); 1582 else 1583 video_device_release(dev->video_dev); 1584 dev->video_dev = NULL; 1585 1586 btcx_riscmem_free(dev->pci, &dev->vidq.stopper); 1587 } 1588 1589 if (dev->audio_dev) 1590 cx23885_audio_unregister(dev); 1591} 1592 1593int cx23885_video_register(struct cx23885_dev *dev) 1594{ 1595 int err; 1596 1597 dprintk(1, "%s()\n", __func__); 1598 spin_lock_init(&dev->slock); 1599 1600 /* Initialize VBI template */ 1601 memcpy(&cx23885_vbi_template, &cx23885_video_template, 1602 sizeof(cx23885_vbi_template)); 1603 strcpy(cx23885_vbi_template.name, "cx23885-vbi"); 1604 1605 dev->tvnorm = cx23885_video_template.current_norm; 1606 1607 /* init video dma queues */ 1608 INIT_LIST_HEAD(&dev->vidq.active); 1609 INIT_LIST_HEAD(&dev->vidq.queued); 1610 dev->vidq.timeout.function = cx23885_vid_timeout; 1611 dev->vidq.timeout.data = (unsigned long)dev; 1612 init_timer(&dev->vidq.timeout); 1613 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper, 1614 VID_A_DMA_CTL, 0x11, 0x00); 1615 1616 /* init vbi dma queues */ 1617 INIT_LIST_HEAD(&dev->vbiq.active); 1618 INIT_LIST_HEAD(&dev->vbiq.queued); 1619 dev->vbiq.timeout.function = cx23885_vbi_timeout; 1620 dev->vbiq.timeout.data = (unsigned long)dev; 1621 init_timer(&dev->vbiq.timeout); 1622 cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper, 1623 VID_A_DMA_CTL, 0x22, 0x00); 1624 1625 cx23885_irq_add_enable(dev, 0x01); 1626 1627 if ((TUNER_ABSENT != dev->tuner_type) && 1628 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) { 1629 struct v4l2_subdev *sd = NULL; 1630 1631 if (dev->tuner_addr) 1632 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, 1633 &dev->i2c_bus[dev->tuner_bus].i2c_adap, 1634 "tuner", dev->tuner_addr, NULL); 1635 else 1636 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, 1637 &dev->i2c_bus[dev->tuner_bus].i2c_adap, 1638 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV)); 1639 if (sd) { 1640 struct tuner_setup tun_setup; 1641 1642 memset(&tun_setup, 0, sizeof(tun_setup)); 1643 tun_setup.mode_mask = T_ANALOG_TV; 1644 tun_setup.type = dev->tuner_type; 1645 tun_setup.addr = v4l2_i2c_subdev_addr(sd); 1646 tun_setup.tuner_callback = cx23885_tuner_callback; 1647 1648 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup); 1649 1650 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) { 1651 struct xc2028_ctrl ctrl = { 1652 .fname = XC2028_DEFAULT_FIRMWARE, 1653 .max_len = 64 1654 }; 1655 struct v4l2_priv_tun_config cfg = { 1656 .tuner = dev->tuner_type, 1657 .priv = &ctrl 1658 }; 1659 v4l2_subdev_call(sd, tuner, s_config, &cfg); 1660 } 1661 } 1662 } 1663 1664 /* register Video device */ 1665 dev->video_dev = cx23885_vdev_init(dev, dev->pci, 1666 &cx23885_video_template, "video"); 1667 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, 1668 video_nr[dev->nr]); 1669 if (err < 0) { 1670 printk(KERN_INFO "%s: can't register video device\n", 1671 dev->name); 1672 goto fail_unreg; 1673 } 1674 printk(KERN_INFO "%s: registered device %s [v4l2]\n", 1675 dev->name, video_device_node_name(dev->video_dev)); 1676 1677 /* register VBI device */ 1678 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci, 1679 &cx23885_vbi_template, "vbi"); 1680 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, 1681 vbi_nr[dev->nr]); 1682 if (err < 0) { 1683 printk(KERN_INFO "%s: can't register vbi device\n", 1684 dev->name); 1685 goto fail_unreg; 1686 } 1687 printk(KERN_INFO "%s: registered device %s\n", 1688 dev->name, video_device_node_name(dev->vbi_dev)); 1689 1690 /* Register ALSA audio device */ 1691 dev->audio_dev = cx23885_audio_register(dev); 1692 1693 /* initial device configuration */ 1694 mutex_lock(&dev->lock); 1695 cx23885_set_tvnorm(dev, dev->tvnorm); 1696 init_controls(dev); 1697 cx23885_video_mux(dev, 0); 1698 mutex_unlock(&dev->lock); 1699 1700 return 0; 1701 1702fail_unreg: 1703 cx23885_video_unregister(dev); 1704 return err; 1705} 1706 1707