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