1/* 2 cx231xx-video.c - driver for Conexant Cx23100/101/102 3 USB video capture devices 4 5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com> 6 Based on em28xx driver 7 Based on cx23885 driver 8 Based on cx88 driver 9 10 This program is free software; you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation; either version 2 of the License, or 13 (at your option) any later version. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 */ 24 25#include <linux/init.h> 26#include <linux/list.h> 27#include <linux/module.h> 28#include <linux/kernel.h> 29#include <linux/bitmap.h> 30#include <linux/usb.h> 31#include <linux/i2c.h> 32#include <linux/version.h> 33#include <linux/mm.h> 34#include <linux/mutex.h> 35#include <linux/slab.h> 36 37#include <media/v4l2-common.h> 38#include <media/v4l2-ioctl.h> 39#include <media/v4l2-chip-ident.h> 40#include <media/msp3400.h> 41#include <media/tuner.h> 42 43#include "dvb_frontend.h" 44 45#include "cx231xx.h" 46#include "cx231xx-vbi.h" 47 48#define CX231XX_VERSION_CODE KERNEL_VERSION(0, 0, 1) 49 50#define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>" 51#define DRIVER_DESC "Conexant cx231xx based USB video device driver" 52 53#define cx231xx_videodbg(fmt, arg...) do {\ 54 if (video_debug) \ 55 printk(KERN_INFO "%s %s :"fmt, \ 56 dev->name, __func__ , ##arg); } while (0) 57 58static unsigned int isoc_debug; 59module_param(isoc_debug, int, 0644); 60MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); 61 62#define cx231xx_isocdbg(fmt, arg...) \ 63do {\ 64 if (isoc_debug) { \ 65 printk(KERN_INFO "%s %s :"fmt, \ 66 dev->name, __func__ , ##arg); \ 67 } \ 68 } while (0) 69 70MODULE_AUTHOR(DRIVER_AUTHOR); 71MODULE_DESCRIPTION(DRIVER_DESC); 72MODULE_LICENSE("GPL"); 73 74static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; 75static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; 76static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; 77static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; 78 79module_param_array(card, int, NULL, 0444); 80module_param_array(video_nr, int, NULL, 0444); 81module_param_array(vbi_nr, int, NULL, 0444); 82module_param_array(radio_nr, int, NULL, 0444); 83 84MODULE_PARM_DESC(card, "card type"); 85MODULE_PARM_DESC(video_nr, "video device numbers"); 86MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); 87MODULE_PARM_DESC(radio_nr, "radio device numbers"); 88 89static unsigned int video_debug; 90module_param(video_debug, int, 0644); 91MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); 92 93/* supported video standards */ 94static struct cx231xx_fmt format[] = { 95 { 96 .name = "16bpp YUY2, 4:2:2, packed", 97 .fourcc = V4L2_PIX_FMT_YUYV, 98 .depth = 16, 99 .reg = 0, 100 }, 101}; 102 103/* supported controls */ 104/* Common to all boards */ 105 106/* ------------------------------------------------------------------- */ 107 108static const struct v4l2_queryctrl no_ctl = { 109 .name = "42", 110 .flags = V4L2_CTRL_FLAG_DISABLED, 111}; 112 113static struct cx231xx_ctrl cx231xx_ctls[] = { 114 /* --- video --- */ 115 { 116 .v = { 117 .id = V4L2_CID_BRIGHTNESS, 118 .name = "Brightness", 119 .minimum = 0x00, 120 .maximum = 0xff, 121 .step = 1, 122 .default_value = 0x7f, 123 .type = V4L2_CTRL_TYPE_INTEGER, 124 }, 125 .off = 128, 126 .reg = LUMA_CTRL, 127 .mask = 0x00ff, 128 .shift = 0, 129 }, { 130 .v = { 131 .id = V4L2_CID_CONTRAST, 132 .name = "Contrast", 133 .minimum = 0, 134 .maximum = 0xff, 135 .step = 1, 136 .default_value = 0x3f, 137 .type = V4L2_CTRL_TYPE_INTEGER, 138 }, 139 .off = 0, 140 .reg = LUMA_CTRL, 141 .mask = 0xff00, 142 .shift = 8, 143 }, { 144 .v = { 145 .id = V4L2_CID_HUE, 146 .name = "Hue", 147 .minimum = 0, 148 .maximum = 0xff, 149 .step = 1, 150 .default_value = 0x7f, 151 .type = V4L2_CTRL_TYPE_INTEGER, 152 }, 153 .off = 128, 154 .reg = CHROMA_CTRL, 155 .mask = 0xff0000, 156 .shift = 16, 157 }, { 158 /* strictly, this only describes only U saturation. 159 * V saturation is handled specially through code. 160 */ 161 .v = { 162 .id = V4L2_CID_SATURATION, 163 .name = "Saturation", 164 .minimum = 0, 165 .maximum = 0xff, 166 .step = 1, 167 .default_value = 0x7f, 168 .type = V4L2_CTRL_TYPE_INTEGER, 169 }, 170 .off = 0, 171 .reg = CHROMA_CTRL, 172 .mask = 0x00ff, 173 .shift = 0, 174 }, { 175 /* --- audio --- */ 176 .v = { 177 .id = V4L2_CID_AUDIO_MUTE, 178 .name = "Mute", 179 .minimum = 0, 180 .maximum = 1, 181 .default_value = 1, 182 .type = V4L2_CTRL_TYPE_BOOLEAN, 183 }, 184 .reg = PATH1_CTL1, 185 .mask = (0x1f << 24), 186 .shift = 24, 187 }, { 188 .v = { 189 .id = V4L2_CID_AUDIO_VOLUME, 190 .name = "Volume", 191 .minimum = 0, 192 .maximum = 0x3f, 193 .step = 1, 194 .default_value = 0x3f, 195 .type = V4L2_CTRL_TYPE_INTEGER, 196 }, 197 .reg = PATH1_VOL_CTL, 198 .mask = 0xff, 199 .shift = 0, 200 } 201}; 202static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls); 203 204static const u32 cx231xx_user_ctrls[] = { 205 V4L2_CID_USER_CLASS, 206 V4L2_CID_BRIGHTNESS, 207 V4L2_CID_CONTRAST, 208 V4L2_CID_SATURATION, 209 V4L2_CID_HUE, 210 V4L2_CID_AUDIO_VOLUME, 211 V4L2_CID_AUDIO_MUTE, 212 0 213}; 214 215static const u32 *ctrl_classes[] = { 216 cx231xx_user_ctrls, 217 NULL 218}; 219 220/* ------------------------------------------------------------------ 221 Video buffer and parser functions 222 ------------------------------------------------------------------*/ 223 224/* 225 * Announces that a buffer were filled and request the next 226 */ 227static inline void buffer_filled(struct cx231xx *dev, 228 struct cx231xx_dmaqueue *dma_q, 229 struct cx231xx_buffer *buf) 230{ 231 /* Advice that buffer was filled */ 232 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); 233 buf->vb.state = VIDEOBUF_DONE; 234 buf->vb.field_count++; 235 do_gettimeofday(&buf->vb.ts); 236 237 dev->video_mode.isoc_ctl.buf = NULL; 238 239 list_del(&buf->vb.queue); 240 wake_up(&buf->vb.done); 241} 242 243static inline void print_err_status(struct cx231xx *dev, int packet, int status) 244{ 245 char *errmsg = "Unknown"; 246 247 switch (status) { 248 case -ENOENT: 249 errmsg = "unlinked synchronuously"; 250 break; 251 case -ECONNRESET: 252 errmsg = "unlinked asynchronuously"; 253 break; 254 case -ENOSR: 255 errmsg = "Buffer error (overrun)"; 256 break; 257 case -EPIPE: 258 errmsg = "Stalled (device not responding)"; 259 break; 260 case -EOVERFLOW: 261 errmsg = "Babble (bad cable?)"; 262 break; 263 case -EPROTO: 264 errmsg = "Bit-stuff error (bad cable?)"; 265 break; 266 case -EILSEQ: 267 errmsg = "CRC/Timeout (could be anything)"; 268 break; 269 case -ETIME: 270 errmsg = "Device does not respond"; 271 break; 272 } 273 if (packet < 0) { 274 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg); 275 } else { 276 cx231xx_isocdbg("URB packet %d, status %d [%s].\n", 277 packet, status, errmsg); 278 } 279} 280 281/* 282 * video-buf generic routine to get the next available buffer 283 */ 284static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q, 285 struct cx231xx_buffer **buf) 286{ 287 struct cx231xx_video_mode *vmode = 288 container_of(dma_q, struct cx231xx_video_mode, vidq); 289 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode); 290 291 char *outp; 292 293 if (list_empty(&dma_q->active)) { 294 cx231xx_isocdbg("No active queue to serve\n"); 295 dev->video_mode.isoc_ctl.buf = NULL; 296 *buf = NULL; 297 return; 298 } 299 300 /* Get the next buffer */ 301 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue); 302 303 /* Cleans up buffer - Usefull for testing for frame/URB loss */ 304 outp = videobuf_to_vmalloc(&(*buf)->vb); 305 memset(outp, 0, (*buf)->vb.size); 306 307 dev->video_mode.isoc_ctl.buf = *buf; 308 309 return; 310} 311 312/* 313 * Controls the isoc copy of each urb packet 314 */ 315static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb) 316{ 317 struct cx231xx_buffer *buf; 318 struct cx231xx_dmaqueue *dma_q = urb->context; 319 unsigned char *outp = NULL; 320 int i, rc = 1; 321 unsigned char *p_buffer; 322 u32 bytes_parsed = 0, buffer_size = 0; 323 u8 sav_eav = 0; 324 325 if (!dev) 326 return 0; 327 328 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) 329 return 0; 330 331 if (urb->status < 0) { 332 print_err_status(dev, -1, urb->status); 333 if (urb->status == -ENOENT) 334 return 0; 335 } 336 337 buf = dev->video_mode.isoc_ctl.buf; 338 if (buf != NULL) 339 outp = videobuf_to_vmalloc(&buf->vb); 340 341 for (i = 0; i < urb->number_of_packets; i++) { 342 int status = urb->iso_frame_desc[i].status; 343 344 if (status < 0) { 345 print_err_status(dev, i, status); 346 if (urb->iso_frame_desc[i].status != -EPROTO) 347 continue; 348 } 349 350 if (urb->iso_frame_desc[i].actual_length <= 0) { 351 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */ 352 continue; 353 } 354 if (urb->iso_frame_desc[i].actual_length > 355 dev->video_mode.max_pkt_size) { 356 cx231xx_isocdbg("packet bigger than packet size"); 357 continue; 358 } 359 360 /* get buffer pointer and length */ 361 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 362 buffer_size = urb->iso_frame_desc[i].actual_length; 363 bytes_parsed = 0; 364 365 if (dma_q->is_partial_line) { 366 /* Handle the case of a partial line */ 367 sav_eav = dma_q->last_sav; 368 } else { 369 /* Check for a SAV/EAV overlapping 370 the buffer boundary */ 371 sav_eav = 372 cx231xx_find_boundary_SAV_EAV(p_buffer, 373 dma_q->partial_buf, 374 &bytes_parsed); 375 } 376 377 sav_eav &= 0xF0; 378 /* Get the first line if we have some portion of an SAV/EAV from 379 the last buffer or a partial line */ 380 if (sav_eav) { 381 bytes_parsed += cx231xx_get_video_line(dev, dma_q, 382 sav_eav, /* SAV/EAV */ 383 p_buffer + bytes_parsed, /* p_buffer */ 384 buffer_size - bytes_parsed);/* buf size */ 385 } 386 387 /* Now parse data that is completely in this buffer */ 388 /* dma_q->is_partial_line = 0; */ 389 390 while (bytes_parsed < buffer_size) { 391 u32 bytes_used = 0; 392 393 sav_eav = cx231xx_find_next_SAV_EAV( 394 p_buffer + bytes_parsed, /* p_buffer */ 395 buffer_size - bytes_parsed, /* buf size */ 396 &bytes_used);/* bytes used to get SAV/EAV */ 397 398 bytes_parsed += bytes_used; 399 400 sav_eav &= 0xF0; 401 if (sav_eav && (bytes_parsed < buffer_size)) { 402 bytes_parsed += cx231xx_get_video_line(dev, 403 dma_q, sav_eav, /* SAV/EAV */ 404 p_buffer + bytes_parsed,/* p_buffer */ 405 buffer_size - bytes_parsed);/*buf size*/ 406 } 407 } 408 409 /* Save the last four bytes of the buffer so we can check the 410 buffer boundary condition next time */ 411 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4); 412 bytes_parsed = 0; 413 414 } 415 return rc; 416} 417 418u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf, 419 u32 *p_bytes_used) 420{ 421 u32 bytes_used; 422 u8 boundary_bytes[8]; 423 u8 sav_eav = 0; 424 425 *p_bytes_used = 0; 426 427 /* Create an array of the last 4 bytes of the last buffer and the first 428 4 bytes of the current buffer. */ 429 430 memcpy(boundary_bytes, partial_buf, 4); 431 memcpy(boundary_bytes + 4, p_buffer, 4); 432 433 /* Check for the SAV/EAV in the boundary buffer */ 434 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8, 435 &bytes_used); 436 437 if (sav_eav) { 438 /* found a boundary SAV/EAV. Updates the bytes used to reflect 439 only those used in the new buffer */ 440 *p_bytes_used = bytes_used - 4; 441 } 442 443 return sav_eav; 444} 445 446u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used) 447{ 448 u32 i; 449 u8 sav_eav = 0; 450 451 /* 452 * Don't search if the buffer size is less than 4. It causes a page 453 * fault since buffer_size - 4 evaluates to a large number in that 454 * case. 455 */ 456 if (buffer_size < 4) { 457 *p_bytes_used = buffer_size; 458 return 0; 459 } 460 461 for (i = 0; i < (buffer_size - 3); i++) { 462 463 if ((p_buffer[i] == 0xFF) && 464 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) { 465 466 *p_bytes_used = i + 4; 467 sav_eav = p_buffer[i + 3]; 468 return sav_eav; 469 } 470 } 471 472 *p_bytes_used = buffer_size; 473 return 0; 474} 475 476u32 cx231xx_get_video_line(struct cx231xx *dev, 477 struct cx231xx_dmaqueue *dma_q, u8 sav_eav, 478 u8 *p_buffer, u32 buffer_size) 479{ 480 u32 bytes_copied = 0; 481 int current_field = -1; 482 483 switch (sav_eav) { 484 case SAV_ACTIVE_VIDEO_FIELD1: 485 /* looking for skipped line which occurred in PAL 720x480 mode. 486 In this case, there will be no active data contained 487 between the SAV and EAV */ 488 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) && 489 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) && 490 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || 491 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) || 492 (p_buffer[3] == EAV_VBLANK_FIELD1) || 493 (p_buffer[3] == EAV_VBLANK_FIELD2))) 494 return bytes_copied; 495 current_field = 1; 496 break; 497 498 case SAV_ACTIVE_VIDEO_FIELD2: 499 /* looking for skipped line which occurred in PAL 720x480 mode. 500 In this case, there will be no active data contained between 501 the SAV and EAV */ 502 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) && 503 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) && 504 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || 505 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) || 506 (p_buffer[3] == EAV_VBLANK_FIELD1) || 507 (p_buffer[3] == EAV_VBLANK_FIELD2))) 508 return bytes_copied; 509 current_field = 2; 510 break; 511 } 512 513 dma_q->last_sav = sav_eav; 514 515 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer, 516 buffer_size, current_field); 517 518 return bytes_copied; 519} 520 521u32 cx231xx_copy_video_line(struct cx231xx *dev, 522 struct cx231xx_dmaqueue *dma_q, u8 *p_line, 523 u32 length, int field_number) 524{ 525 u32 bytes_to_copy; 526 struct cx231xx_buffer *buf; 527 u32 _line_size = dev->width * 2; 528 529 if (dma_q->current_field != field_number) 530 cx231xx_reset_video_buffer(dev, dma_q); 531 532 /* get the buffer pointer */ 533 buf = dev->video_mode.isoc_ctl.buf; 534 535 /* Remember the field number for next time */ 536 dma_q->current_field = field_number; 537 538 bytes_to_copy = dma_q->bytes_left_in_line; 539 if (bytes_to_copy > length) 540 bytes_to_copy = length; 541 542 if (dma_q->lines_completed >= dma_q->lines_per_field) { 543 dma_q->bytes_left_in_line -= bytes_to_copy; 544 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? 545 0 : 1; 546 return 0; 547 } 548 549 dma_q->is_partial_line = 1; 550 551 /* If we don't have a buffer, just return the number of bytes we would 552 have copied if we had a buffer. */ 553 if (!buf) { 554 dma_q->bytes_left_in_line -= bytes_to_copy; 555 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) 556 ? 0 : 1; 557 return bytes_to_copy; 558 } 559 560 /* copy the data to video buffer */ 561 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy); 562 563 dma_q->pos += bytes_to_copy; 564 dma_q->bytes_left_in_line -= bytes_to_copy; 565 566 if (dma_q->bytes_left_in_line == 0) { 567 dma_q->bytes_left_in_line = _line_size; 568 dma_q->lines_completed++; 569 dma_q->is_partial_line = 0; 570 571 if (cx231xx_is_buffer_done(dev, dma_q) && buf) { 572 buffer_filled(dev, dma_q, buf); 573 574 dma_q->pos = 0; 575 buf = NULL; 576 dma_q->lines_completed = 0; 577 } 578 } 579 580 return bytes_to_copy; 581} 582 583void cx231xx_reset_video_buffer(struct cx231xx *dev, 584 struct cx231xx_dmaqueue *dma_q) 585{ 586 struct cx231xx_buffer *buf; 587 588 /* handle the switch from field 1 to field 2 */ 589 if (dma_q->current_field == 1) { 590 if (dma_q->lines_completed >= dma_q->lines_per_field) 591 dma_q->field1_done = 1; 592 else 593 dma_q->field1_done = 0; 594 } 595 596 buf = dev->video_mode.isoc_ctl.buf; 597 598 if (buf == NULL) { 599 u8 *outp = NULL; 600 /* first try to get the buffer */ 601 get_next_buf(dma_q, &buf); 602 603 if (buf) 604 outp = videobuf_to_vmalloc(&buf->vb); 605 606 dma_q->pos = 0; 607 dma_q->field1_done = 0; 608 dma_q->current_field = -1; 609 } 610 611 /* reset the counters */ 612 dma_q->bytes_left_in_line = dev->width << 1; 613 dma_q->lines_completed = 0; 614} 615 616int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q, 617 u8 *p_buffer, u32 bytes_to_copy) 618{ 619 u8 *p_out_buffer = NULL; 620 u32 current_line_bytes_copied = 0; 621 struct cx231xx_buffer *buf; 622 u32 _line_size = dev->width << 1; 623 void *startwrite; 624 int offset, lencopy; 625 626 buf = dev->video_mode.isoc_ctl.buf; 627 628 if (buf == NULL) 629 return -1; 630 631 p_out_buffer = videobuf_to_vmalloc(&buf->vb); 632 633 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line; 634 635 /* Offset field 2 one line from the top of the buffer */ 636 offset = (dma_q->current_field == 1) ? 0 : _line_size; 637 638 /* Offset for field 2 */ 639 startwrite = p_out_buffer + offset; 640 641 /* lines already completed in the current field */ 642 startwrite += (dma_q->lines_completed * _line_size * 2); 643 644 /* bytes already completed in the current line */ 645 startwrite += current_line_bytes_copied; 646 647 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ? 648 bytes_to_copy : dma_q->bytes_left_in_line; 649 650 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size)) 651 return 0; 652 653 /* The below copies the UYVY data straight into video buffer */ 654 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy); 655 656 return 0; 657} 658 659void cx231xx_swab(u16 *from, u16 *to, u16 len) 660{ 661 u16 i; 662 663 if (len <= 0) 664 return; 665 666 for (i = 0; i < len / 2; i++) 667 to[i] = (from[i] << 8) | (from[i] >> 8); 668} 669 670u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q) 671{ 672 u8 buffer_complete = 0; 673 674 /* Dual field stream */ 675 buffer_complete = ((dma_q->current_field == 2) && 676 (dma_q->lines_completed >= dma_q->lines_per_field) && 677 dma_q->field1_done); 678 679 return buffer_complete; 680} 681 682/* ------------------------------------------------------------------ 683 Videobuf operations 684 ------------------------------------------------------------------*/ 685 686static int 687buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) 688{ 689 struct cx231xx_fh *fh = vq->priv_data; 690 struct cx231xx *dev = fh->dev; 691 struct v4l2_frequency f; 692 693 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3; 694 if (0 == *count) 695 *count = CX231XX_DEF_BUF; 696 697 if (*count < CX231XX_MIN_BUF) 698 *count = CX231XX_MIN_BUF; 699 700 /* Ask tuner to go to analog mode */ 701 memset(&f, 0, sizeof(f)); 702 f.frequency = dev->ctl_freq; 703 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 704 705 call_all(dev, tuner, s_frequency, &f); 706 707 return 0; 708} 709 710/* This is called *without* dev->slock held; please keep it that way */ 711static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf) 712{ 713 struct cx231xx_fh *fh = vq->priv_data; 714 struct cx231xx *dev = fh->dev; 715 unsigned long flags = 0; 716 717 if (in_interrupt()) 718 BUG(); 719 720 /* We used to wait for the buffer to finish here, but this didn't work 721 because, as we were keeping the state as VIDEOBUF_QUEUED, 722 videobuf_queue_cancel marked it as finished for us. 723 (Also, it could wedge forever if the hardware was misconfigured.) 724 725 This should be safe; by the time we get here, the buffer isn't 726 queued anymore. If we ever start marking the buffers as 727 VIDEOBUF_ACTIVE, it won't be, though. 728 */ 729 spin_lock_irqsave(&dev->video_mode.slock, flags); 730 if (dev->video_mode.isoc_ctl.buf == buf) 731 dev->video_mode.isoc_ctl.buf = NULL; 732 spin_unlock_irqrestore(&dev->video_mode.slock, flags); 733 734 videobuf_vmalloc_free(&buf->vb); 735 buf->vb.state = VIDEOBUF_NEEDS_INIT; 736} 737 738static int 739buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, 740 enum v4l2_field field) 741{ 742 struct cx231xx_fh *fh = vq->priv_data; 743 struct cx231xx_buffer *buf = 744 container_of(vb, struct cx231xx_buffer, vb); 745 struct cx231xx *dev = fh->dev; 746 int rc = 0, urb_init = 0; 747 748 /* The only currently supported format is 16 bits/pixel */ 749 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth 750 + 7) >> 3; 751 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 752 return -EINVAL; 753 754 buf->vb.width = dev->width; 755 buf->vb.height = dev->height; 756 buf->vb.field = field; 757 758 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 759 rc = videobuf_iolock(vq, &buf->vb, NULL); 760 if (rc < 0) 761 goto fail; 762 } 763 764 if (!dev->video_mode.isoc_ctl.num_bufs) 765 urb_init = 1; 766 767 if (urb_init) { 768 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS, 769 CX231XX_NUM_BUFS, 770 dev->video_mode.max_pkt_size, 771 cx231xx_isoc_copy); 772 if (rc < 0) 773 goto fail; 774 } 775 776 buf->vb.state = VIDEOBUF_PREPARED; 777 return 0; 778 779fail: 780 free_buffer(vq, buf); 781 return rc; 782} 783 784static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 785{ 786 struct cx231xx_buffer *buf = 787 container_of(vb, struct cx231xx_buffer, vb); 788 struct cx231xx_fh *fh = vq->priv_data; 789 struct cx231xx *dev = fh->dev; 790 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq; 791 792 buf->vb.state = VIDEOBUF_QUEUED; 793 list_add_tail(&buf->vb.queue, &vidq->active); 794 795} 796 797static void buffer_release(struct videobuf_queue *vq, 798 struct videobuf_buffer *vb) 799{ 800 struct cx231xx_buffer *buf = 801 container_of(vb, struct cx231xx_buffer, vb); 802 struct cx231xx_fh *fh = vq->priv_data; 803 struct cx231xx *dev = (struct cx231xx *)fh->dev; 804 805 cx231xx_isocdbg("cx231xx: called buffer_release\n"); 806 807 free_buffer(vq, buf); 808} 809 810static struct videobuf_queue_ops cx231xx_video_qops = { 811 .buf_setup = buffer_setup, 812 .buf_prepare = buffer_prepare, 813 .buf_queue = buffer_queue, 814 .buf_release = buffer_release, 815}; 816 817/********************* v4l2 interface **************************************/ 818 819void video_mux(struct cx231xx *dev, int index) 820{ 821 dev->video_input = index; 822 dev->ctl_ainput = INPUT(index)->amux; 823 824 cx231xx_set_video_input_mux(dev, index); 825 826 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0); 827 828 cx231xx_set_audio_input(dev, dev->ctl_ainput); 829 830 cx231xx_info("video_mux : %d\n", index); 831 832 /* do mode control overrides if required */ 833 cx231xx_do_mode_ctrl_overrides(dev); 834} 835 836/* Usage lock check functions */ 837static int res_get(struct cx231xx_fh *fh) 838{ 839 struct cx231xx *dev = fh->dev; 840 int rc = 0; 841 842 /* This instance already has stream_on */ 843 if (fh->stream_on) 844 return rc; 845 846 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 847 if (dev->stream_on) 848 return -EBUSY; 849 dev->stream_on = 1; 850 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 851 if (dev->vbi_stream_on) 852 return -EBUSY; 853 dev->vbi_stream_on = 1; 854 } else 855 return -EINVAL; 856 857 fh->stream_on = 1; 858 859 return rc; 860} 861 862static int res_check(struct cx231xx_fh *fh) 863{ 864 return fh->stream_on; 865} 866 867static void res_free(struct cx231xx_fh *fh) 868{ 869 struct cx231xx *dev = fh->dev; 870 871 fh->stream_on = 0; 872 873 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 874 dev->stream_on = 0; 875 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) 876 dev->vbi_stream_on = 0; 877} 878 879static int check_dev(struct cx231xx *dev) 880{ 881 if (dev->state & DEV_DISCONNECTED) { 882 cx231xx_errdev("v4l2 ioctl: device not present\n"); 883 return -ENODEV; 884 } 885 886 if (dev->state & DEV_MISCONFIGURED) { 887 cx231xx_errdev("v4l2 ioctl: device is misconfigured; " 888 "close and open it again\n"); 889 return -EIO; 890 } 891 return 0; 892} 893 894static void get_scale(struct cx231xx *dev, 895 unsigned int width, unsigned int height, 896 unsigned int *hscale, unsigned int *vscale) 897{ 898 unsigned int maxw = norm_maxw(dev); 899 unsigned int maxh = norm_maxh(dev); 900 901 *hscale = (((unsigned long)maxw) << 12) / width - 4096L; 902 if (*hscale >= 0x4000) 903 *hscale = 0x3fff; 904 905 *vscale = (((unsigned long)maxh) << 12) / height - 4096L; 906 if (*vscale >= 0x4000) 907 *vscale = 0x3fff; 908} 909 910/* ------------------------------------------------------------------ 911 IOCTL vidioc handling 912 ------------------------------------------------------------------*/ 913 914static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 915 struct v4l2_format *f) 916{ 917 struct cx231xx_fh *fh = priv; 918 struct cx231xx *dev = fh->dev; 919 920 mutex_lock(&dev->lock); 921 922 f->fmt.pix.width = dev->width; 923 f->fmt.pix.height = dev->height; 924 f->fmt.pix.pixelformat = dev->format->fourcc; 925 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3; 926 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height; 927 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 928 929 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 930 931 mutex_unlock(&dev->lock); 932 933 return 0; 934} 935 936static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc) 937{ 938 unsigned int i; 939 940 for (i = 0; i < ARRAY_SIZE(format); i++) 941 if (format[i].fourcc == fourcc) 942 return &format[i]; 943 944 return NULL; 945} 946 947static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 948 struct v4l2_format *f) 949{ 950 struct cx231xx_fh *fh = priv; 951 struct cx231xx *dev = fh->dev; 952 unsigned int width = f->fmt.pix.width; 953 unsigned int height = f->fmt.pix.height; 954 unsigned int maxw = norm_maxw(dev); 955 unsigned int maxh = norm_maxh(dev); 956 unsigned int hscale, vscale; 957 struct cx231xx_fmt *fmt; 958 959 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 960 if (!fmt) { 961 cx231xx_videodbg("Fourcc format (%08x) invalid.\n", 962 f->fmt.pix.pixelformat); 963 return -EINVAL; 964 } 965 966 /* width must even because of the YUYV format 967 height must be even because of interlacing */ 968 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0); 969 970 get_scale(dev, width, height, &hscale, &vscale); 971 972 width = (((unsigned long)maxw) << 12) / (hscale + 4096L); 973 height = (((unsigned long)maxh) << 12) / (vscale + 4096L); 974 975 f->fmt.pix.width = width; 976 f->fmt.pix.height = height; 977 f->fmt.pix.pixelformat = fmt->fourcc; 978 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3; 979 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height; 980 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 981 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 982 983 return 0; 984} 985 986static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 987 struct v4l2_format *f) 988{ 989 struct cx231xx_fh *fh = priv; 990 struct cx231xx *dev = fh->dev; 991 int rc; 992 struct cx231xx_fmt *fmt; 993 struct v4l2_mbus_framefmt mbus_fmt; 994 995 rc = check_dev(dev); 996 if (rc < 0) 997 return rc; 998 999 mutex_lock(&dev->lock); 1000 1001 vidioc_try_fmt_vid_cap(file, priv, f); 1002 1003 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 1004 if (!fmt) { 1005 rc = -EINVAL; 1006 goto out; 1007 } 1008 1009 if (videobuf_queue_is_busy(&fh->vb_vidq)) { 1010 cx231xx_errdev("%s queue busy\n", __func__); 1011 rc = -EBUSY; 1012 goto out; 1013 } 1014 1015 if (dev->stream_on && !fh->stream_on) { 1016 cx231xx_errdev("%s device in use by another fh\n", __func__); 1017 rc = -EBUSY; 1018 goto out; 1019 } 1020 1021 /* set new image size */ 1022 dev->width = f->fmt.pix.width; 1023 dev->height = f->fmt.pix.height; 1024 dev->format = fmt; 1025 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 1026 1027 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED); 1028 call_all(dev, video, s_mbus_fmt, &mbus_fmt); 1029 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt); 1030 1031 /* Set the correct alternate setting for this resolution */ 1032 cx231xx_resolution_set(dev); 1033 1034out: 1035 mutex_unlock(&dev->lock); 1036 return rc; 1037} 1038 1039static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id) 1040{ 1041 struct cx231xx_fh *fh = priv; 1042 struct cx231xx *dev = fh->dev; 1043 1044 *id = dev->norm; 1045 return 0; 1046} 1047 1048static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) 1049{ 1050 struct cx231xx_fh *fh = priv; 1051 struct cx231xx *dev = fh->dev; 1052 struct v4l2_format f; 1053 int rc; 1054 1055 rc = check_dev(dev); 1056 if (rc < 0) 1057 return rc; 1058 1059 cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm); 1060 1061 mutex_lock(&dev->lock); 1062 dev->norm = *norm; 1063 1064 /* Adjusts width/height, if needed */ 1065 f.fmt.pix.width = dev->width; 1066 f.fmt.pix.height = dev->height; 1067 vidioc_try_fmt_vid_cap(file, priv, &f); 1068 1069 /* set new image size */ 1070 dev->width = f.fmt.pix.width; 1071 dev->height = f.fmt.pix.height; 1072 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 1073 1074 call_all(dev, core, s_std, dev->norm); 1075 1076 mutex_unlock(&dev->lock); 1077 1078 cx231xx_resolution_set(dev); 1079 1080 /* do mode control overrides */ 1081 cx231xx_do_mode_ctrl_overrides(dev); 1082 1083 return 0; 1084} 1085 1086static const char *iname[] = { 1087 [CX231XX_VMUX_COMPOSITE1] = "Composite1", 1088 [CX231XX_VMUX_SVIDEO] = "S-Video", 1089 [CX231XX_VMUX_TELEVISION] = "Television", 1090 [CX231XX_VMUX_CABLE] = "Cable TV", 1091 [CX231XX_VMUX_DVB] = "DVB", 1092 [CX231XX_VMUX_DEBUG] = "for debug only", 1093}; 1094 1095static int vidioc_enum_input(struct file *file, void *priv, 1096 struct v4l2_input *i) 1097{ 1098 struct cx231xx_fh *fh = priv; 1099 struct cx231xx *dev = fh->dev; 1100 unsigned int n; 1101 1102 n = i->index; 1103 if (n >= MAX_CX231XX_INPUT) 1104 return -EINVAL; 1105 if (0 == INPUT(n)->type) 1106 return -EINVAL; 1107 1108 i->index = n; 1109 i->type = V4L2_INPUT_TYPE_CAMERA; 1110 1111 strcpy(i->name, iname[INPUT(n)->type]); 1112 1113 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) || 1114 (CX231XX_VMUX_CABLE == INPUT(n)->type)) 1115 i->type = V4L2_INPUT_TYPE_TUNER; 1116 1117 i->std = dev->vdev->tvnorms; 1118 1119 return 0; 1120} 1121 1122static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1123{ 1124 struct cx231xx_fh *fh = priv; 1125 struct cx231xx *dev = fh->dev; 1126 1127 *i = dev->video_input; 1128 1129 return 0; 1130} 1131 1132static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1133{ 1134 struct cx231xx_fh *fh = priv; 1135 struct cx231xx *dev = fh->dev; 1136 int rc; 1137 1138 rc = check_dev(dev); 1139 if (rc < 0) 1140 return rc; 1141 1142 if (i >= MAX_CX231XX_INPUT) 1143 return -EINVAL; 1144 if (0 == INPUT(i)->type) 1145 return -EINVAL; 1146 1147 mutex_lock(&dev->lock); 1148 1149 video_mux(dev, i); 1150 1151 mutex_unlock(&dev->lock); 1152 return 0; 1153} 1154 1155static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) 1156{ 1157 struct cx231xx_fh *fh = priv; 1158 struct cx231xx *dev = fh->dev; 1159 1160 switch (a->index) { 1161 case CX231XX_AMUX_VIDEO: 1162 strcpy(a->name, "Television"); 1163 break; 1164 case CX231XX_AMUX_LINE_IN: 1165 strcpy(a->name, "Line In"); 1166 break; 1167 default: 1168 return -EINVAL; 1169 } 1170 1171 a->index = dev->ctl_ainput; 1172 a->capability = V4L2_AUDCAP_STEREO; 1173 1174 return 0; 1175} 1176 1177static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a) 1178{ 1179 struct cx231xx_fh *fh = priv; 1180 struct cx231xx *dev = fh->dev; 1181 int status = 0; 1182 1183 /* Doesn't allow manual routing */ 1184 if (a->index != dev->ctl_ainput) 1185 return -EINVAL; 1186 1187 dev->ctl_ainput = INPUT(a->index)->amux; 1188 status = cx231xx_set_audio_input(dev, dev->ctl_ainput); 1189 1190 return status; 1191} 1192 1193static int vidioc_queryctrl(struct file *file, void *priv, 1194 struct v4l2_queryctrl *qc) 1195{ 1196 struct cx231xx_fh *fh = priv; 1197 struct cx231xx *dev = fh->dev; 1198 int id = qc->id; 1199 int i; 1200 int rc; 1201 1202 rc = check_dev(dev); 1203 if (rc < 0) 1204 return rc; 1205 1206 qc->id = v4l2_ctrl_next(ctrl_classes, qc->id); 1207 if (unlikely(qc->id == 0)) 1208 return -EINVAL; 1209 1210 memset(qc, 0, sizeof(*qc)); 1211 1212 qc->id = id; 1213 1214 if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1) 1215 return -EINVAL; 1216 1217 for (i = 0; i < CX231XX_CTLS; i++) 1218 if (cx231xx_ctls[i].v.id == qc->id) 1219 break; 1220 1221 if (i == CX231XX_CTLS) { 1222 *qc = no_ctl; 1223 return 0; 1224 } 1225 *qc = cx231xx_ctls[i].v; 1226 1227 mutex_lock(&dev->lock); 1228 call_all(dev, core, queryctrl, qc); 1229 mutex_unlock(&dev->lock); 1230 1231 if (qc->type) 1232 return 0; 1233 else 1234 return -EINVAL; 1235} 1236 1237static int vidioc_g_ctrl(struct file *file, void *priv, 1238 struct v4l2_control *ctrl) 1239{ 1240 struct cx231xx_fh *fh = priv; 1241 struct cx231xx *dev = fh->dev; 1242 int rc; 1243 1244 rc = check_dev(dev); 1245 if (rc < 0) 1246 return rc; 1247 1248 mutex_lock(&dev->lock); 1249 call_all(dev, core, g_ctrl, ctrl); 1250 mutex_unlock(&dev->lock); 1251 return rc; 1252} 1253 1254static int vidioc_s_ctrl(struct file *file, void *priv, 1255 struct v4l2_control *ctrl) 1256{ 1257 struct cx231xx_fh *fh = priv; 1258 struct cx231xx *dev = fh->dev; 1259 int rc; 1260 1261 rc = check_dev(dev); 1262 if (rc < 0) 1263 return rc; 1264 1265 mutex_lock(&dev->lock); 1266 call_all(dev, core, s_ctrl, ctrl); 1267 mutex_unlock(&dev->lock); 1268 return rc; 1269} 1270 1271static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) 1272{ 1273 struct cx231xx_fh *fh = priv; 1274 struct cx231xx *dev = fh->dev; 1275 int rc; 1276 1277 rc = check_dev(dev); 1278 if (rc < 0) 1279 return rc; 1280 1281 if (0 != t->index) 1282 return -EINVAL; 1283 1284 strcpy(t->name, "Tuner"); 1285 1286 t->type = V4L2_TUNER_ANALOG_TV; 1287 t->capability = V4L2_TUNER_CAP_NORM; 1288 t->rangehigh = 0xffffffffUL; 1289 t->signal = 0xffff; /* LOCKED */ 1290 1291 return 0; 1292} 1293 1294static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t) 1295{ 1296 struct cx231xx_fh *fh = priv; 1297 struct cx231xx *dev = fh->dev; 1298 int rc; 1299 1300 rc = check_dev(dev); 1301 if (rc < 0) 1302 return rc; 1303 1304 if (0 != t->index) 1305 return -EINVAL; 1306 return 0; 1307} 1308 1309static int vidioc_g_frequency(struct file *file, void *priv, 1310 struct v4l2_frequency *f) 1311{ 1312 struct cx231xx_fh *fh = priv; 1313 struct cx231xx *dev = fh->dev; 1314 1315 mutex_lock(&dev->lock); 1316 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1317 f->frequency = dev->ctl_freq; 1318 1319 call_all(dev, tuner, g_frequency, f); 1320 1321 mutex_unlock(&dev->lock); 1322 1323 return 0; 1324} 1325 1326static int vidioc_s_frequency(struct file *file, void *priv, 1327 struct v4l2_frequency *f) 1328{ 1329 struct cx231xx_fh *fh = priv; 1330 struct cx231xx *dev = fh->dev; 1331 int rc; 1332 1333 rc = check_dev(dev); 1334 if (rc < 0) 1335 return rc; 1336 1337 if (0 != f->tuner) 1338 return -EINVAL; 1339 1340 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)) 1341 return -EINVAL; 1342 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO)) 1343 return -EINVAL; 1344 1345 /* set pre channel change settings in DIF first */ 1346 rc = cx231xx_tuner_pre_channel_change(dev); 1347 1348 mutex_lock(&dev->lock); 1349 1350 dev->ctl_freq = f->frequency; 1351 1352 if (dev->tuner_type == TUNER_XC5000) { 1353 if (dev->cx231xx_set_analog_freq != NULL) 1354 dev->cx231xx_set_analog_freq(dev, f->frequency); 1355 } else 1356 call_all(dev, tuner, s_frequency, f); 1357 1358 mutex_unlock(&dev->lock); 1359 1360 /* set post channel change settings in DIF first */ 1361 rc = cx231xx_tuner_post_channel_change(dev); 1362 1363 cx231xx_info("Set New FREQUENCY to %d\n", f->frequency); 1364 1365 return rc; 1366} 1367 1368#ifdef CONFIG_VIDEO_ADV_DEBUG 1369 1370/* 1371 -R, --list-registers=type=<host/i2cdrv/i2caddr>, 1372 chip=<chip>[,min=<addr>,max=<addr>] 1373 dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER] 1374 -r, --set-register=type=<host/i2cdrv/i2caddr>, 1375 chip=<chip>,reg=<addr>,val=<val> 1376 set the register [VIDIOC_DBG_S_REGISTER] 1377 1378 if type == host, then <chip> is the hosts chip ID (default 0) 1379 if type == i2cdrv (default), then <chip> is the I2C driver name or ID 1380 if type == i2caddr, then <chip> is the 7-bit I2C address 1381*/ 1382 1383static int vidioc_g_register(struct file *file, void *priv, 1384 struct v4l2_dbg_register *reg) 1385{ 1386 struct cx231xx_fh *fh = priv; 1387 struct cx231xx *dev = fh->dev; 1388 int ret = 0; 1389 u8 value[4] = { 0, 0, 0, 0 }; 1390 u32 data = 0; 1391 1392 switch (reg->match.type) { 1393 case V4L2_CHIP_MATCH_HOST: 1394 switch (reg->match.addr) { 1395 case 0: /* Cx231xx - internal registers */ 1396 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, 1397 (u16)reg->reg, value, 4); 1398 reg->val = value[0] | value[1] << 8 | 1399 value[2] << 16 | value[3] << 24; 1400 break; 1401 case 1: /* AFE - read byte */ 1402 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS, 1403 (u16)reg->reg, 2, &data, 1); 1404 reg->val = le32_to_cpu(data & 0xff); 1405 break; 1406 case 14: /* AFE - read dword */ 1407 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS, 1408 (u16)reg->reg, 2, &data, 4); 1409 reg->val = le32_to_cpu(data); 1410 break; 1411 case 2: /* Video Block - read byte */ 1412 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS, 1413 (u16)reg->reg, 2, &data, 1); 1414 reg->val = le32_to_cpu(data & 0xff); 1415 break; 1416 case 24: /* Video Block - read dword */ 1417 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS, 1418 (u16)reg->reg, 2, &data, 4); 1419 reg->val = le32_to_cpu(data); 1420 break; 1421 case 3: /* I2S block - read byte */ 1422 ret = cx231xx_read_i2c_data(dev, 1423 I2S_BLK_DEVICE_ADDRESS, 1424 (u16)reg->reg, 1, 1425 &data, 1); 1426 reg->val = le32_to_cpu(data & 0xff); 1427 break; 1428 case 34: /* I2S Block - read dword */ 1429 ret = 1430 cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS, 1431 (u16)reg->reg, 1, &data, 4); 1432 reg->val = le32_to_cpu(data); 1433 break; 1434 } 1435 return ret < 0 ? ret : 0; 1436 1437 case V4L2_CHIP_MATCH_I2C_DRIVER: 1438 call_all(dev, core, g_register, reg); 1439 return 0; 1440 case V4L2_CHIP_MATCH_I2C_ADDR: 1441 /* Not supported yet */ 1442 return -EINVAL; 1443 default: 1444 if (!v4l2_chip_match_host(®->match)) 1445 return -EINVAL; 1446 } 1447 1448 mutex_lock(&dev->lock); 1449 call_all(dev, core, g_register, reg); 1450 mutex_unlock(&dev->lock); 1451 1452 return ret; 1453} 1454 1455static int vidioc_s_register(struct file *file, void *priv, 1456 struct v4l2_dbg_register *reg) 1457{ 1458 struct cx231xx_fh *fh = priv; 1459 struct cx231xx *dev = fh->dev; 1460 int ret = 0; 1461 __le64 buf; 1462 u32 value; 1463 u8 data[4] = { 0, 0, 0, 0 }; 1464 1465 buf = cpu_to_le64(reg->val); 1466 1467 switch (reg->match.type) { 1468 case V4L2_CHIP_MATCH_HOST: 1469 { 1470 value = (u32) buf & 0xffffffff; 1471 1472 switch (reg->match.addr) { 1473 case 0: /* cx231xx internal registers */ 1474 data[0] = (u8) value; 1475 data[1] = (u8) (value >> 8); 1476 data[2] = (u8) (value >> 16); 1477 data[3] = (u8) (value >> 24); 1478 ret = cx231xx_write_ctrl_reg(dev, 1479 VRT_SET_REGISTER, 1480 (u16)reg->reg, data, 1481 4); 1482 break; 1483 case 1: /* AFE - read byte */ 1484 ret = cx231xx_write_i2c_data(dev, 1485 AFE_DEVICE_ADDRESS, 1486 (u16)reg->reg, 2, 1487 value, 1); 1488 break; 1489 case 14: /* AFE - read dword */ 1490 ret = cx231xx_write_i2c_data(dev, 1491 AFE_DEVICE_ADDRESS, 1492 (u16)reg->reg, 2, 1493 value, 4); 1494 break; 1495 case 2: /* Video Block - read byte */ 1496 ret = 1497 cx231xx_write_i2c_data(dev, 1498 VID_BLK_I2C_ADDRESS, 1499 (u16)reg->reg, 2, 1500 value, 1); 1501 break; 1502 case 24: /* Video Block - read dword */ 1503 ret = 1504 cx231xx_write_i2c_data(dev, 1505 VID_BLK_I2C_ADDRESS, 1506 (u16)reg->reg, 2, 1507 value, 4); 1508 break; 1509 case 3: /* I2S block - read byte */ 1510 ret = 1511 cx231xx_write_i2c_data(dev, 1512 I2S_BLK_DEVICE_ADDRESS, 1513 (u16)reg->reg, 1, 1514 value, 1); 1515 break; 1516 case 34: /* I2S block - read dword */ 1517 ret = 1518 cx231xx_write_i2c_data(dev, 1519 I2S_BLK_DEVICE_ADDRESS, 1520 (u16)reg->reg, 1, 1521 value, 4); 1522 break; 1523 } 1524 } 1525 return ret < 0 ? ret : 0; 1526 1527 default: 1528 break; 1529 } 1530 1531 mutex_lock(&dev->lock); 1532 call_all(dev, core, s_register, reg); 1533 mutex_unlock(&dev->lock); 1534 1535 return ret; 1536} 1537#endif 1538 1539static int vidioc_cropcap(struct file *file, void *priv, 1540 struct v4l2_cropcap *cc) 1541{ 1542 struct cx231xx_fh *fh = priv; 1543 struct cx231xx *dev = fh->dev; 1544 1545 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1546 return -EINVAL; 1547 1548 cc->bounds.left = 0; 1549 cc->bounds.top = 0; 1550 cc->bounds.width = dev->width; 1551 cc->bounds.height = dev->height; 1552 cc->defrect = cc->bounds; 1553 cc->pixelaspect.numerator = 54; 1554 cc->pixelaspect.denominator = 59; 1555 1556 return 0; 1557} 1558 1559static int vidioc_streamon(struct file *file, void *priv, 1560 enum v4l2_buf_type type) 1561{ 1562 struct cx231xx_fh *fh = priv; 1563 struct cx231xx *dev = fh->dev; 1564 int rc; 1565 1566 rc = check_dev(dev); 1567 if (rc < 0) 1568 return rc; 1569 1570 mutex_lock(&dev->lock); 1571 rc = res_get(fh); 1572 1573 if (likely(rc >= 0)) 1574 rc = videobuf_streamon(&fh->vb_vidq); 1575 1576 call_all(dev, video, s_stream, 1); 1577 1578 mutex_unlock(&dev->lock); 1579 1580 return rc; 1581} 1582 1583static int vidioc_streamoff(struct file *file, void *priv, 1584 enum v4l2_buf_type type) 1585{ 1586 struct cx231xx_fh *fh = priv; 1587 struct cx231xx *dev = fh->dev; 1588 int rc; 1589 1590 rc = check_dev(dev); 1591 if (rc < 0) 1592 return rc; 1593 1594 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) && 1595 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)) 1596 return -EINVAL; 1597 if (type != fh->type) 1598 return -EINVAL; 1599 1600 mutex_lock(&dev->lock); 1601 1602 cx25840_call(dev, video, s_stream, 0); 1603 1604 videobuf_streamoff(&fh->vb_vidq); 1605 res_free(fh); 1606 1607 mutex_unlock(&dev->lock); 1608 1609 return 0; 1610} 1611 1612static int vidioc_querycap(struct file *file, void *priv, 1613 struct v4l2_capability *cap) 1614{ 1615 struct cx231xx_fh *fh = priv; 1616 struct cx231xx *dev = fh->dev; 1617 1618 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver)); 1619 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card)); 1620 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 1621 1622 cap->version = CX231XX_VERSION_CODE; 1623 1624 cap->capabilities = V4L2_CAP_VBI_CAPTURE | 1625 V4L2_CAP_VIDEO_CAPTURE | 1626 V4L2_CAP_AUDIO | 1627 V4L2_CAP_READWRITE | 1628 V4L2_CAP_STREAMING; 1629 1630 if (dev->tuner_type != TUNER_ABSENT) 1631 cap->capabilities |= V4L2_CAP_TUNER; 1632 1633 return 0; 1634} 1635 1636static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 1637 struct v4l2_fmtdesc *f) 1638{ 1639 if (unlikely(f->index >= ARRAY_SIZE(format))) 1640 return -EINVAL; 1641 1642 strlcpy(f->description, format[f->index].name, sizeof(f->description)); 1643 f->pixelformat = format[f->index].fourcc; 1644 1645 return 0; 1646} 1647 1648/* Sliced VBI ioctls */ 1649static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv, 1650 struct v4l2_format *f) 1651{ 1652 struct cx231xx_fh *fh = priv; 1653 struct cx231xx *dev = fh->dev; 1654 int rc; 1655 1656 rc = check_dev(dev); 1657 if (rc < 0) 1658 return rc; 1659 1660 mutex_lock(&dev->lock); 1661 1662 f->fmt.sliced.service_set = 0; 1663 1664 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced); 1665 1666 if (f->fmt.sliced.service_set == 0) 1667 rc = -EINVAL; 1668 1669 mutex_unlock(&dev->lock); 1670 return rc; 1671} 1672 1673static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv, 1674 struct v4l2_format *f) 1675{ 1676 struct cx231xx_fh *fh = priv; 1677 struct cx231xx *dev = fh->dev; 1678 int rc; 1679 1680 rc = check_dev(dev); 1681 if (rc < 0) 1682 return rc; 1683 1684 mutex_lock(&dev->lock); 1685 call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced); 1686 mutex_unlock(&dev->lock); 1687 1688 if (f->fmt.sliced.service_set == 0) 1689 return -EINVAL; 1690 1691 return 0; 1692} 1693 1694/* RAW VBI ioctls */ 1695 1696static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, 1697 struct v4l2_format *f) 1698{ 1699 struct cx231xx_fh *fh = priv; 1700 struct cx231xx *dev = fh->dev; 1701 1702 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ? 1703 35468950 : 28636363; 1704 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH; 1705 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; 1706 f->fmt.vbi.offset = 64 * 4; 1707 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ? 1708 PAL_VBI_START_LINE : NTSC_VBI_START_LINE; 1709 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ? 1710 PAL_VBI_LINES : NTSC_VBI_LINES; 1711 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ? 1712 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263; 1713 f->fmt.vbi.count[1] = f->fmt.vbi.count[0]; 1714 1715 return 0; 1716 1717} 1718 1719static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv, 1720 struct v4l2_format *f) 1721{ 1722 struct cx231xx_fh *fh = priv; 1723 struct cx231xx *dev = fh->dev; 1724 1725 if (dev->vbi_stream_on && !fh->stream_on) { 1726 cx231xx_errdev("%s device in use by another fh\n", __func__); 1727 return -EBUSY; 1728 } 1729 1730 f->type = V4L2_BUF_TYPE_VBI_CAPTURE; 1731 f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ? 1732 35468950 : 28636363; 1733 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH; 1734 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; 1735 f->fmt.vbi.offset = 244; 1736 f->fmt.vbi.flags = 0; 1737 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ? 1738 PAL_VBI_START_LINE : NTSC_VBI_START_LINE; 1739 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ? 1740 PAL_VBI_LINES : NTSC_VBI_LINES; 1741 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ? 1742 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263; 1743 f->fmt.vbi.count[1] = f->fmt.vbi.count[0]; 1744 1745 return 0; 1746 1747} 1748 1749static int vidioc_reqbufs(struct file *file, void *priv, 1750 struct v4l2_requestbuffers *rb) 1751{ 1752 struct cx231xx_fh *fh = priv; 1753 struct cx231xx *dev = fh->dev; 1754 int rc; 1755 1756 rc = check_dev(dev); 1757 if (rc < 0) 1758 return rc; 1759 1760 return videobuf_reqbufs(&fh->vb_vidq, rb); 1761} 1762 1763static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b) 1764{ 1765 struct cx231xx_fh *fh = priv; 1766 struct cx231xx *dev = fh->dev; 1767 int rc; 1768 1769 rc = check_dev(dev); 1770 if (rc < 0) 1771 return rc; 1772 1773 return videobuf_querybuf(&fh->vb_vidq, b); 1774} 1775 1776static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1777{ 1778 struct cx231xx_fh *fh = priv; 1779 struct cx231xx *dev = fh->dev; 1780 int rc; 1781 1782 rc = check_dev(dev); 1783 if (rc < 0) 1784 return rc; 1785 1786 return videobuf_qbuf(&fh->vb_vidq, b); 1787} 1788 1789static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1790{ 1791 struct cx231xx_fh *fh = priv; 1792 struct cx231xx *dev = fh->dev; 1793 int rc; 1794 1795 rc = check_dev(dev); 1796 if (rc < 0) 1797 return rc; 1798 1799 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK); 1800} 1801 1802#ifdef CONFIG_VIDEO_V4L1_COMPAT 1803static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf) 1804{ 1805 struct cx231xx_fh *fh = priv; 1806 1807 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8); 1808} 1809#endif 1810 1811/* ----------------------------------------------------------- */ 1812/* RADIO ESPECIFIC IOCTLS */ 1813/* ----------------------------------------------------------- */ 1814 1815static int radio_querycap(struct file *file, void *priv, 1816 struct v4l2_capability *cap) 1817{ 1818 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev; 1819 1820 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver)); 1821 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card)); 1822 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 1823 1824 cap->version = CX231XX_VERSION_CODE; 1825 cap->capabilities = V4L2_CAP_TUNER; 1826 return 0; 1827} 1828 1829static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) 1830{ 1831 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev; 1832 1833 if (unlikely(t->index > 0)) 1834 return -EINVAL; 1835 1836 strcpy(t->name, "Radio"); 1837 t->type = V4L2_TUNER_RADIO; 1838 1839 mutex_lock(&dev->lock); 1840 call_all(dev, tuner, s_tuner, t); 1841 mutex_unlock(&dev->lock); 1842 1843 return 0; 1844} 1845 1846static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i) 1847{ 1848 if (i->index != 0) 1849 return -EINVAL; 1850 strcpy(i->name, "Radio"); 1851 i->type = V4L2_INPUT_TYPE_TUNER; 1852 1853 return 0; 1854} 1855 1856static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a) 1857{ 1858 if (unlikely(a->index)) 1859 return -EINVAL; 1860 1861 strcpy(a->name, "Radio"); 1862 return 0; 1863} 1864 1865static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t) 1866{ 1867 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev; 1868 1869 if (0 != t->index) 1870 return -EINVAL; 1871 1872 mutex_lock(&dev->lock); 1873 call_all(dev, tuner, s_tuner, t); 1874 mutex_unlock(&dev->lock); 1875 1876 return 0; 1877} 1878 1879static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a) 1880{ 1881 return 0; 1882} 1883 1884static int radio_s_input(struct file *file, void *fh, unsigned int i) 1885{ 1886 return 0; 1887} 1888 1889static int radio_queryctrl(struct file *file, void *priv, 1890 struct v4l2_queryctrl *c) 1891{ 1892 int i; 1893 1894 if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1) 1895 return -EINVAL; 1896 if (c->id == V4L2_CID_AUDIO_MUTE) { 1897 for (i = 0; i < CX231XX_CTLS; i++) { 1898 if (cx231xx_ctls[i].v.id == c->id) 1899 break; 1900 } 1901 if (i == CX231XX_CTLS) 1902 return -EINVAL; 1903 *c = cx231xx_ctls[i].v; 1904 } else 1905 *c = no_ctl; 1906 return 0; 1907} 1908 1909/* 1910 * cx231xx_v4l2_open() 1911 * inits the device and starts isoc transfer 1912 */ 1913static int cx231xx_v4l2_open(struct file *filp) 1914{ 1915 int errCode = 0, radio = 0; 1916 struct video_device *vdev = video_devdata(filp); 1917 struct cx231xx *dev = video_drvdata(filp); 1918 struct cx231xx_fh *fh; 1919 enum v4l2_buf_type fh_type = 0; 1920 1921 switch (vdev->vfl_type) { 1922 case VFL_TYPE_GRABBER: 1923 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1924 break; 1925 case VFL_TYPE_VBI: 1926 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; 1927 break; 1928 case VFL_TYPE_RADIO: 1929 radio = 1; 1930 break; 1931 } 1932 1933 mutex_lock(&dev->lock); 1934 1935 cx231xx_videodbg("open dev=%s type=%s users=%d\n", 1936 video_device_node_name(vdev), v4l2_type_names[fh_type], 1937 dev->users); 1938 1939 1940 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL); 1941 if (!fh) { 1942 cx231xx_errdev("cx231xx-video.c: Out of memory?!\n"); 1943 mutex_unlock(&dev->lock); 1944 return -ENOMEM; 1945 } 1946 fh->dev = dev; 1947 fh->radio = radio; 1948 fh->type = fh_type; 1949 filp->private_data = fh; 1950 1951 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { 1952 dev->width = norm_maxw(dev); 1953 dev->height = norm_maxh(dev); 1954 dev->hscale = 0; 1955 dev->vscale = 0; 1956 1957 /* Power up in Analog TV mode */ 1958 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV); 1959 1960 cx231xx_resolution_set(dev); 1961 1962 /* set video alternate setting */ 1963 cx231xx_set_video_alternate(dev); 1964 1965 /* Needed, since GPIO might have disabled power of 1966 some i2c device */ 1967 cx231xx_config_i2c(dev); 1968 1969 /* device needs to be initialized before isoc transfer */ 1970 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input; 1971 video_mux(dev, dev->video_input); 1972 1973 } 1974 if (fh->radio) { 1975 cx231xx_videodbg("video_open: setting radio device\n"); 1976 1977 /* cx231xx_start_radio(dev); */ 1978 1979 call_all(dev, tuner, s_radio); 1980 } 1981 1982 dev->users++; 1983 1984 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1985 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops, 1986 NULL, &dev->video_mode.slock, 1987 fh->type, V4L2_FIELD_INTERLACED, 1988 sizeof(struct cx231xx_buffer), fh); 1989 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 1990 /* Set the required alternate setting VBI interface works in 1991 Bulk mode only */ 1992 cx231xx_set_alt_setting(dev, INDEX_VANC, 0); 1993 1994 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops, 1995 NULL, &dev->vbi_mode.slock, 1996 fh->type, V4L2_FIELD_SEQ_TB, 1997 sizeof(struct cx231xx_buffer), fh); 1998 } 1999 2000 mutex_unlock(&dev->lock); 2001 2002 return errCode; 2003} 2004 2005/* 2006 * cx231xx_realease_resources() 2007 * unregisters the v4l2,i2c and usb devices 2008 * called when the device gets disconected or at module unload 2009*/ 2010void cx231xx_release_analog_resources(struct cx231xx *dev) 2011{ 2012 2013 2014 if (dev->radio_dev) { 2015 if (video_is_registered(dev->radio_dev)) 2016 video_unregister_device(dev->radio_dev); 2017 else 2018 video_device_release(dev->radio_dev); 2019 dev->radio_dev = NULL; 2020 } 2021 if (dev->vbi_dev) { 2022 cx231xx_info("V4L2 device %s deregistered\n", 2023 video_device_node_name(dev->vbi_dev)); 2024 if (video_is_registered(dev->vbi_dev)) 2025 video_unregister_device(dev->vbi_dev); 2026 else 2027 video_device_release(dev->vbi_dev); 2028 dev->vbi_dev = NULL; 2029 } 2030 if (dev->vdev) { 2031 cx231xx_info("V4L2 device %s deregistered\n", 2032 video_device_node_name(dev->vdev)); 2033 if (video_is_registered(dev->vdev)) 2034 video_unregister_device(dev->vdev); 2035 else 2036 video_device_release(dev->vdev); 2037 dev->vdev = NULL; 2038 } 2039} 2040 2041/* 2042 * cx231xx_v4l2_close() 2043 * stops streaming and deallocates all resources allocated by the v4l2 2044 * calls and ioctls 2045 */ 2046static int cx231xx_v4l2_close(struct file *filp) 2047{ 2048 struct cx231xx_fh *fh = filp->private_data; 2049 struct cx231xx *dev = fh->dev; 2050 2051 cx231xx_videodbg("users=%d\n", dev->users); 2052 2053 mutex_lock(&dev->lock); 2054 2055 if (res_check(fh)) 2056 res_free(fh); 2057 2058 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 2059 videobuf_stop(&fh->vb_vidq); 2060 videobuf_mmap_free(&fh->vb_vidq); 2061 2062 /* the device is already disconnect, 2063 free the remaining resources */ 2064 if (dev->state & DEV_DISCONNECTED) { 2065 cx231xx_release_resources(dev); 2066 mutex_unlock(&dev->lock); 2067 kfree(dev); 2068 return 0; 2069 } 2070 2071 /* do this before setting alternate! */ 2072 cx231xx_uninit_vbi_isoc(dev); 2073 2074 /* set alternate 0 */ 2075 if (!dev->vbi_or_sliced_cc_mode) 2076 cx231xx_set_alt_setting(dev, INDEX_VANC, 0); 2077 else 2078 cx231xx_set_alt_setting(dev, INDEX_HANC, 0); 2079 2080 kfree(fh); 2081 dev->users--; 2082 wake_up_interruptible_nr(&dev->open, 1); 2083 mutex_unlock(&dev->lock); 2084 return 0; 2085 } 2086 2087 if (dev->users == 1) { 2088 videobuf_stop(&fh->vb_vidq); 2089 videobuf_mmap_free(&fh->vb_vidq); 2090 2091 /* the device is already disconnect, 2092 free the remaining resources */ 2093 if (dev->state & DEV_DISCONNECTED) { 2094 cx231xx_release_resources(dev); 2095 mutex_unlock(&dev->lock); 2096 kfree(dev); 2097 return 0; 2098 } 2099 2100 /* Save some power by putting tuner to sleep */ 2101 call_all(dev, core, s_power, 0); 2102 2103 /* do this before setting alternate! */ 2104 cx231xx_uninit_isoc(dev); 2105 cx231xx_set_mode(dev, CX231XX_SUSPEND); 2106 2107 /* set alternate 0 */ 2108 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0); 2109 } 2110 kfree(fh); 2111 dev->users--; 2112 wake_up_interruptible_nr(&dev->open, 1); 2113 mutex_unlock(&dev->lock); 2114 return 0; 2115} 2116 2117/* 2118 * cx231xx_v4l2_read() 2119 * will allocate buffers when called for the first time 2120 */ 2121static ssize_t 2122cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count, 2123 loff_t *pos) 2124{ 2125 struct cx231xx_fh *fh = filp->private_data; 2126 struct cx231xx *dev = fh->dev; 2127 int rc; 2128 2129 rc = check_dev(dev); 2130 if (rc < 0) 2131 return rc; 2132 2133 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) || 2134 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) { 2135 mutex_lock(&dev->lock); 2136 rc = res_get(fh); 2137 mutex_unlock(&dev->lock); 2138 2139 if (unlikely(rc < 0)) 2140 return rc; 2141 2142 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0, 2143 filp->f_flags & O_NONBLOCK); 2144 } 2145 return 0; 2146} 2147 2148/* 2149 * cx231xx_v4l2_poll() 2150 * will allocate buffers when called for the first time 2151 */ 2152static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait) 2153{ 2154 struct cx231xx_fh *fh = filp->private_data; 2155 struct cx231xx *dev = fh->dev; 2156 int rc; 2157 2158 rc = check_dev(dev); 2159 if (rc < 0) 2160 return rc; 2161 2162 mutex_lock(&dev->lock); 2163 rc = res_get(fh); 2164 mutex_unlock(&dev->lock); 2165 2166 if (unlikely(rc < 0)) 2167 return POLLERR; 2168 2169 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) || 2170 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) 2171 return videobuf_poll_stream(filp, &fh->vb_vidq, wait); 2172 else 2173 return POLLERR; 2174} 2175 2176/* 2177 * cx231xx_v4l2_mmap() 2178 */ 2179static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) 2180{ 2181 struct cx231xx_fh *fh = filp->private_data; 2182 struct cx231xx *dev = fh->dev; 2183 int rc; 2184 2185 rc = check_dev(dev); 2186 if (rc < 0) 2187 return rc; 2188 2189 mutex_lock(&dev->lock); 2190 rc = res_get(fh); 2191 mutex_unlock(&dev->lock); 2192 2193 if (unlikely(rc < 0)) 2194 return rc; 2195 2196 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma); 2197 2198 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n", 2199 (unsigned long)vma->vm_start, 2200 (unsigned long)vma->vm_end - 2201 (unsigned long)vma->vm_start, rc); 2202 2203 return rc; 2204} 2205 2206static const struct v4l2_file_operations cx231xx_v4l_fops = { 2207 .owner = THIS_MODULE, 2208 .open = cx231xx_v4l2_open, 2209 .release = cx231xx_v4l2_close, 2210 .read = cx231xx_v4l2_read, 2211 .poll = cx231xx_v4l2_poll, 2212 .mmap = cx231xx_v4l2_mmap, 2213 .ioctl = video_ioctl2, 2214}; 2215 2216static const struct v4l2_ioctl_ops video_ioctl_ops = { 2217 .vidioc_querycap = vidioc_querycap, 2218 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 2219 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 2220 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 2221 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 2222 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 2223 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap, 2224 .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap, 2225 .vidioc_g_audio = vidioc_g_audio, 2226 .vidioc_s_audio = vidioc_s_audio, 2227 .vidioc_cropcap = vidioc_cropcap, 2228 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap, 2229 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, 2230 .vidioc_reqbufs = vidioc_reqbufs, 2231 .vidioc_querybuf = vidioc_querybuf, 2232 .vidioc_qbuf = vidioc_qbuf, 2233 .vidioc_dqbuf = vidioc_dqbuf, 2234 .vidioc_s_std = vidioc_s_std, 2235 .vidioc_g_std = vidioc_g_std, 2236 .vidioc_enum_input = vidioc_enum_input, 2237 .vidioc_g_input = vidioc_g_input, 2238 .vidioc_s_input = vidioc_s_input, 2239 .vidioc_queryctrl = vidioc_queryctrl, 2240 .vidioc_g_ctrl = vidioc_g_ctrl, 2241 .vidioc_s_ctrl = vidioc_s_ctrl, 2242 .vidioc_streamon = vidioc_streamon, 2243 .vidioc_streamoff = vidioc_streamoff, 2244 .vidioc_g_tuner = vidioc_g_tuner, 2245 .vidioc_s_tuner = vidioc_s_tuner, 2246 .vidioc_g_frequency = vidioc_g_frequency, 2247 .vidioc_s_frequency = vidioc_s_frequency, 2248#ifdef CONFIG_VIDEO_ADV_DEBUG 2249 .vidioc_g_register = vidioc_g_register, 2250 .vidioc_s_register = vidioc_s_register, 2251#endif 2252#ifdef CONFIG_VIDEO_V4L1_COMPAT 2253 .vidiocgmbuf = vidiocgmbuf, 2254#endif 2255}; 2256 2257static struct video_device cx231xx_vbi_template; 2258 2259static const struct video_device cx231xx_video_template = { 2260 .fops = &cx231xx_v4l_fops, 2261 .release = video_device_release, 2262 .ioctl_ops = &video_ioctl_ops, 2263 .tvnorms = V4L2_STD_ALL, 2264 .current_norm = V4L2_STD_PAL, 2265}; 2266 2267static const struct v4l2_file_operations radio_fops = { 2268 .owner = THIS_MODULE, 2269 .open = cx231xx_v4l2_open, 2270 .release = cx231xx_v4l2_close, 2271 .ioctl = video_ioctl2, 2272}; 2273 2274static const struct v4l2_ioctl_ops radio_ioctl_ops = { 2275 .vidioc_querycap = radio_querycap, 2276 .vidioc_g_tuner = radio_g_tuner, 2277 .vidioc_enum_input = radio_enum_input, 2278 .vidioc_g_audio = radio_g_audio, 2279 .vidioc_s_tuner = radio_s_tuner, 2280 .vidioc_s_audio = radio_s_audio, 2281 .vidioc_s_input = radio_s_input, 2282 .vidioc_queryctrl = radio_queryctrl, 2283 .vidioc_g_ctrl = vidioc_g_ctrl, 2284 .vidioc_s_ctrl = vidioc_s_ctrl, 2285 .vidioc_g_frequency = vidioc_g_frequency, 2286 .vidioc_s_frequency = vidioc_s_frequency, 2287#ifdef CONFIG_VIDEO_ADV_DEBUG 2288 .vidioc_g_register = vidioc_g_register, 2289 .vidioc_s_register = vidioc_s_register, 2290#endif 2291}; 2292 2293static struct video_device cx231xx_radio_template = { 2294 .name = "cx231xx-radio", 2295 .fops = &radio_fops, 2296 .ioctl_ops = &radio_ioctl_ops, 2297}; 2298 2299/******************************** usb interface ******************************/ 2300 2301static struct video_device *cx231xx_vdev_init(struct cx231xx *dev, 2302 const struct video_device 2303 *template, const char *type_name) 2304{ 2305 struct video_device *vfd; 2306 2307 vfd = video_device_alloc(); 2308 if (NULL == vfd) 2309 return NULL; 2310 2311 *vfd = *template; 2312 vfd->v4l2_dev = &dev->v4l2_dev; 2313 vfd->release = video_device_release; 2314 vfd->debug = video_debug; 2315 2316 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name); 2317 2318 video_set_drvdata(vfd, dev); 2319 return vfd; 2320} 2321 2322int cx231xx_register_analog_devices(struct cx231xx *dev) 2323{ 2324 int ret; 2325 2326 cx231xx_info("%s: v4l2 driver version %d.%d.%d\n", 2327 dev->name, 2328 (CX231XX_VERSION_CODE >> 16) & 0xff, 2329 (CX231XX_VERSION_CODE >> 8) & 0xff, 2330 CX231XX_VERSION_CODE & 0xff); 2331 2332 /* set default norm */ 2333 /*dev->norm = cx231xx_video_template.current_norm; */ 2334 dev->width = norm_maxw(dev); 2335 dev->height = norm_maxh(dev); 2336 dev->interlaced = 0; 2337 dev->hscale = 0; 2338 dev->vscale = 0; 2339 2340 /* Analog specific initialization */ 2341 dev->format = &format[0]; 2342 /* video_mux(dev, dev->video_input); */ 2343 2344 /* Audio defaults */ 2345 dev->mute = 1; 2346 dev->volume = 0x1f; 2347 2348 /* enable vbi capturing */ 2349 /* write code here... */ 2350 2351 /* allocate and fill video video_device struct */ 2352 dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video"); 2353 if (!dev->vdev) { 2354 cx231xx_errdev("cannot allocate video_device.\n"); 2355 return -ENODEV; 2356 } 2357 2358 /* register v4l2 video video_device */ 2359 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER, 2360 video_nr[dev->devno]); 2361 if (ret) { 2362 cx231xx_errdev("unable to register video device (error=%i).\n", 2363 ret); 2364 return ret; 2365 } 2366 2367 cx231xx_info("%s/0: registered device %s [v4l2]\n", 2368 dev->name, video_device_node_name(dev->vdev)); 2369 2370 /* Initialize VBI template */ 2371 memcpy(&cx231xx_vbi_template, &cx231xx_video_template, 2372 sizeof(cx231xx_vbi_template)); 2373 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi"); 2374 2375 /* Allocate and fill vbi video_device struct */ 2376 dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi"); 2377 2378 /* register v4l2 vbi video_device */ 2379 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, 2380 vbi_nr[dev->devno]); 2381 if (ret < 0) { 2382 cx231xx_errdev("unable to register vbi device\n"); 2383 return ret; 2384 } 2385 2386 cx231xx_info("%s/0: registered device %s\n", 2387 dev->name, video_device_node_name(dev->vbi_dev)); 2388 2389 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) { 2390 dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template, 2391 "radio"); 2392 if (!dev->radio_dev) { 2393 cx231xx_errdev("cannot allocate video_device.\n"); 2394 return -ENODEV; 2395 } 2396 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO, 2397 radio_nr[dev->devno]); 2398 if (ret < 0) { 2399 cx231xx_errdev("can't register radio device\n"); 2400 return ret; 2401 } 2402 cx231xx_info("Registered radio device as %s\n", 2403 video_device_node_name(dev->radio_dev)); 2404 } 2405 2406 cx231xx_info("V4L2 device registered as %s and %s\n", 2407 video_device_node_name(dev->vdev), 2408 video_device_node_name(dev->vbi_dev)); 2409 2410 return 0; 2411} 2412