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