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