1 /* 2 * Auvitek AU0828 USB Bridge (Analog video support) 3 * 4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org> 5 * Copyright (C) 2005-2008 Auvitek International, Ltd. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * As published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 */ 17 18 /* Developer Notes: 19 * 20 * The hardware scaler supported is unimplemented 21 * AC97 audio support is unimplemented (only i2s audio mode) 22 * 23 */ 24 25 #include "au0828.h" 26 #include "au8522.h" 27 28 #include <linux/module.h> 29 #include <linux/slab.h> 30 #include <linux/init.h> 31 #include <linux/device.h> 32 #include <media/v4l2-common.h> 33 #include <media/v4l2-mc.h> 34 #include <media/v4l2-ioctl.h> 35 #include <media/v4l2-event.h> 36 #include <media/tuner.h> 37 #include "au0828-reg.h" 38 39 static DEFINE_MUTEX(au0828_sysfs_lock); 40 41 /* ------------------------------------------------------------------ 42 Videobuf operations 43 ------------------------------------------------------------------*/ 44 45 static unsigned int isoc_debug; 46 module_param(isoc_debug, int, 0644); 47 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); 48 49 #define au0828_isocdbg(fmt, arg...) \ 50 do {\ 51 if (isoc_debug) { \ 52 pr_info("au0828 %s :"fmt, \ 53 __func__ , ##arg); \ 54 } \ 55 } while (0) 56 57 static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val) 58 { 59 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl) 60 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val); 61 } 62 63 static inline void print_err_status(struct au0828_dev *dev, 64 int packet, int status) 65 { 66 char *errmsg = "Unknown"; 67 68 switch (status) { 69 case -ENOENT: 70 errmsg = "unlinked synchronously"; 71 break; 72 case -ECONNRESET: 73 errmsg = "unlinked asynchronously"; 74 break; 75 case -ENOSR: 76 errmsg = "Buffer error (overrun)"; 77 break; 78 case -EPIPE: 79 errmsg = "Stalled (device not responding)"; 80 break; 81 case -EOVERFLOW: 82 errmsg = "Babble (bad cable?)"; 83 break; 84 case -EPROTO: 85 errmsg = "Bit-stuff error (bad cable?)"; 86 break; 87 case -EILSEQ: 88 errmsg = "CRC/Timeout (could be anything)"; 89 break; 90 case -ETIME: 91 errmsg = "Device does not respond"; 92 break; 93 } 94 if (packet < 0) { 95 au0828_isocdbg("URB status %d [%s].\n", status, errmsg); 96 } else { 97 au0828_isocdbg("URB packet %d, status %d [%s].\n", 98 packet, status, errmsg); 99 } 100 } 101 102 static int check_dev(struct au0828_dev *dev) 103 { 104 if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) { 105 pr_info("v4l2 ioctl: device not present\n"); 106 return -ENODEV; 107 } 108 109 if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) { 110 pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n"); 111 return -EIO; 112 } 113 return 0; 114 } 115 116 /* 117 * IRQ callback, called by URB callback 118 */ 119 static void au0828_irq_callback(struct urb *urb) 120 { 121 struct au0828_dmaqueue *dma_q = urb->context; 122 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq); 123 unsigned long flags = 0; 124 int i; 125 126 switch (urb->status) { 127 case 0: /* success */ 128 case -ETIMEDOUT: /* NAK */ 129 break; 130 case -ECONNRESET: /* kill */ 131 case -ENOENT: 132 case -ESHUTDOWN: 133 au0828_isocdbg("au0828_irq_callback called: status kill\n"); 134 return; 135 default: /* unknown error */ 136 au0828_isocdbg("urb completion error %d.\n", urb->status); 137 break; 138 } 139 140 /* Copy data from URB */ 141 spin_lock_irqsave(&dev->slock, flags); 142 dev->isoc_ctl.isoc_copy(dev, urb); 143 spin_unlock_irqrestore(&dev->slock, flags); 144 145 /* Reset urb buffers */ 146 for (i = 0; i < urb->number_of_packets; i++) { 147 urb->iso_frame_desc[i].status = 0; 148 urb->iso_frame_desc[i].actual_length = 0; 149 } 150 urb->status = 0; 151 152 urb->status = usb_submit_urb(urb, GFP_ATOMIC); 153 if (urb->status) { 154 au0828_isocdbg("urb resubmit failed (error=%i)\n", 155 urb->status); 156 } 157 dev->stream_state = STREAM_ON; 158 } 159 160 /* 161 * Stop and Deallocate URBs 162 */ 163 static void au0828_uninit_isoc(struct au0828_dev *dev) 164 { 165 struct urb *urb; 166 int i; 167 168 au0828_isocdbg("au0828: called au0828_uninit_isoc\n"); 169 170 dev->isoc_ctl.nfields = -1; 171 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { 172 urb = dev->isoc_ctl.urb[i]; 173 if (urb) { 174 if (!irqs_disabled()) 175 usb_kill_urb(urb); 176 else 177 usb_unlink_urb(urb); 178 179 if (dev->isoc_ctl.transfer_buffer[i]) { 180 usb_free_coherent(dev->usbdev, 181 urb->transfer_buffer_length, 182 dev->isoc_ctl.transfer_buffer[i], 183 urb->transfer_dma); 184 } 185 usb_free_urb(urb); 186 dev->isoc_ctl.urb[i] = NULL; 187 } 188 dev->isoc_ctl.transfer_buffer[i] = NULL; 189 } 190 191 kfree(dev->isoc_ctl.urb); 192 kfree(dev->isoc_ctl.transfer_buffer); 193 194 dev->isoc_ctl.urb = NULL; 195 dev->isoc_ctl.transfer_buffer = NULL; 196 dev->isoc_ctl.num_bufs = 0; 197 198 dev->stream_state = STREAM_OFF; 199 } 200 201 /* 202 * Allocate URBs and start IRQ 203 */ 204 static int au0828_init_isoc(struct au0828_dev *dev, int max_packets, 205 int num_bufs, int max_pkt_size, 206 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb)) 207 { 208 struct au0828_dmaqueue *dma_q = &dev->vidq; 209 int i; 210 int sb_size, pipe; 211 struct urb *urb; 212 int j, k; 213 int rc; 214 215 au0828_isocdbg("au0828: called au0828_prepare_isoc\n"); 216 217 dev->isoc_ctl.isoc_copy = isoc_copy; 218 dev->isoc_ctl.num_bufs = num_bufs; 219 220 dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL); 221 if (!dev->isoc_ctl.urb) { 222 au0828_isocdbg("cannot alloc memory for usb buffers\n"); 223 return -ENOMEM; 224 } 225 226 dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *), 227 GFP_KERNEL); 228 if (!dev->isoc_ctl.transfer_buffer) { 229 au0828_isocdbg("cannot allocate memory for usb transfer\n"); 230 kfree(dev->isoc_ctl.urb); 231 return -ENOMEM; 232 } 233 234 dev->isoc_ctl.max_pkt_size = max_pkt_size; 235 dev->isoc_ctl.buf = NULL; 236 237 sb_size = max_packets * dev->isoc_ctl.max_pkt_size; 238 239 /* allocate urbs and transfer buffers */ 240 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { 241 urb = usb_alloc_urb(max_packets, GFP_KERNEL); 242 if (!urb) { 243 au0828_uninit_isoc(dev); 244 return -ENOMEM; 245 } 246 dev->isoc_ctl.urb[i] = urb; 247 248 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev, 249 sb_size, GFP_KERNEL, &urb->transfer_dma); 250 if (!dev->isoc_ctl.transfer_buffer[i]) { 251 printk("unable to allocate %i bytes for transfer buffer %i%s\n", 252 sb_size, i, 253 in_interrupt() ? " while in int" : ""); 254 au0828_uninit_isoc(dev); 255 return -ENOMEM; 256 } 257 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size); 258 259 pipe = usb_rcvisocpipe(dev->usbdev, 260 dev->isoc_in_endpointaddr), 261 262 usb_fill_int_urb(urb, dev->usbdev, pipe, 263 dev->isoc_ctl.transfer_buffer[i], sb_size, 264 au0828_irq_callback, dma_q, 1); 265 266 urb->number_of_packets = max_packets; 267 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; 268 269 k = 0; 270 for (j = 0; j < max_packets; j++) { 271 urb->iso_frame_desc[j].offset = k; 272 urb->iso_frame_desc[j].length = 273 dev->isoc_ctl.max_pkt_size; 274 k += dev->isoc_ctl.max_pkt_size; 275 } 276 } 277 278 /* submit urbs and enables IRQ */ 279 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { 280 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); 281 if (rc) { 282 au0828_isocdbg("submit of urb %i failed (error=%i)\n", 283 i, rc); 284 au0828_uninit_isoc(dev); 285 return rc; 286 } 287 } 288 289 return 0; 290 } 291 292 /* 293 * Announces that a buffer were filled and request the next 294 */ 295 static inline void buffer_filled(struct au0828_dev *dev, 296 struct au0828_dmaqueue *dma_q, 297 struct au0828_buffer *buf) 298 { 299 struct vb2_v4l2_buffer *vb = &buf->vb; 300 struct vb2_queue *q = vb->vb2_buf.vb2_queue; 301 302 /* Advice that buffer was filled */ 303 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field); 304 305 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 306 vb->sequence = dev->frame_count++; 307 else 308 vb->sequence = dev->vbi_frame_count++; 309 310 vb->field = V4L2_FIELD_INTERLACED; 311 vb->vb2_buf.timestamp = ktime_get_ns(); 312 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE); 313 } 314 315 /* 316 * Identify the buffer header type and properly handles 317 */ 318 static void au0828_copy_video(struct au0828_dev *dev, 319 struct au0828_dmaqueue *dma_q, 320 struct au0828_buffer *buf, 321 unsigned char *p, 322 unsigned char *outp, unsigned long len) 323 { 324 void *fieldstart, *startwrite, *startread; 325 int linesdone, currlinedone, offset, lencopy, remain; 326 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */ 327 328 if (len == 0) 329 return; 330 331 if (dma_q->pos + len > buf->length) 332 len = buf->length - dma_q->pos; 333 334 startread = p; 335 remain = len; 336 337 /* Interlaces frame */ 338 if (buf->top_field) 339 fieldstart = outp; 340 else 341 fieldstart = outp + bytesperline; 342 343 linesdone = dma_q->pos / bytesperline; 344 currlinedone = dma_q->pos % bytesperline; 345 offset = linesdone * bytesperline * 2 + currlinedone; 346 startwrite = fieldstart + offset; 347 lencopy = bytesperline - currlinedone; 348 lencopy = lencopy > remain ? remain : lencopy; 349 350 if ((char *)startwrite + lencopy > (char *)outp + buf->length) { 351 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n", 352 ((char *)startwrite + lencopy) - 353 ((char *)outp + buf->length)); 354 remain = (char *)outp + buf->length - (char *)startwrite; 355 lencopy = remain; 356 } 357 if (lencopy <= 0) 358 return; 359 memcpy(startwrite, startread, lencopy); 360 361 remain -= lencopy; 362 363 while (remain > 0) { 364 startwrite += lencopy + bytesperline; 365 startread += lencopy; 366 if (bytesperline > remain) 367 lencopy = remain; 368 else 369 lencopy = bytesperline; 370 371 if ((char *)startwrite + lencopy > (char *)outp + 372 buf->length) { 373 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n", 374 ((char *)startwrite + lencopy) - 375 ((char *)outp + buf->length)); 376 lencopy = remain = (char *)outp + buf->length - 377 (char *)startwrite; 378 } 379 if (lencopy <= 0) 380 break; 381 382 memcpy(startwrite, startread, lencopy); 383 384 remain -= lencopy; 385 } 386 387 if (offset > 1440) { 388 /* We have enough data to check for greenscreen */ 389 if (outp[0] < 0x60 && outp[1440] < 0x60) 390 dev->greenscreen_detected = 1; 391 } 392 393 dma_q->pos += len; 394 } 395 396 /* 397 * video-buf generic routine to get the next available buffer 398 */ 399 static inline void get_next_buf(struct au0828_dmaqueue *dma_q, 400 struct au0828_buffer **buf) 401 { 402 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq); 403 404 if (list_empty(&dma_q->active)) { 405 au0828_isocdbg("No active queue to serve\n"); 406 dev->isoc_ctl.buf = NULL; 407 *buf = NULL; 408 return; 409 } 410 411 /* Get the next buffer */ 412 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list); 413 /* Cleans up buffer - Useful for testing for frame/URB loss */ 414 list_del(&(*buf)->list); 415 dma_q->pos = 0; 416 (*buf)->vb_buf = (*buf)->mem; 417 dev->isoc_ctl.buf = *buf; 418 419 return; 420 } 421 422 static void au0828_copy_vbi(struct au0828_dev *dev, 423 struct au0828_dmaqueue *dma_q, 424 struct au0828_buffer *buf, 425 unsigned char *p, 426 unsigned char *outp, unsigned long len) 427 { 428 unsigned char *startwrite, *startread; 429 int bytesperline; 430 int i, j = 0; 431 432 if (dev == NULL) { 433 au0828_isocdbg("dev is null\n"); 434 return; 435 } 436 437 if (dma_q == NULL) { 438 au0828_isocdbg("dma_q is null\n"); 439 return; 440 } 441 if (buf == NULL) 442 return; 443 if (p == NULL) { 444 au0828_isocdbg("p is null\n"); 445 return; 446 } 447 if (outp == NULL) { 448 au0828_isocdbg("outp is null\n"); 449 return; 450 } 451 452 bytesperline = dev->vbi_width; 453 454 if (dma_q->pos + len > buf->length) 455 len = buf->length - dma_q->pos; 456 457 startread = p; 458 startwrite = outp + (dma_q->pos / 2); 459 460 /* Make sure the bottom field populates the second half of the frame */ 461 if (buf->top_field == 0) 462 startwrite += bytesperline * dev->vbi_height; 463 464 for (i = 0; i < len; i += 2) 465 startwrite[j++] = startread[i+1]; 466 467 dma_q->pos += len; 468 } 469 470 471 /* 472 * video-buf generic routine to get the next available VBI buffer 473 */ 474 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q, 475 struct au0828_buffer **buf) 476 { 477 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq); 478 479 if (list_empty(&dma_q->active)) { 480 au0828_isocdbg("No active queue to serve\n"); 481 dev->isoc_ctl.vbi_buf = NULL; 482 *buf = NULL; 483 return; 484 } 485 486 /* Get the next buffer */ 487 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list); 488 /* Cleans up buffer - Useful for testing for frame/URB loss */ 489 list_del(&(*buf)->list); 490 dma_q->pos = 0; 491 (*buf)->vb_buf = (*buf)->mem; 492 dev->isoc_ctl.vbi_buf = *buf; 493 return; 494 } 495 496 /* 497 * Controls the isoc copy of each urb packet 498 */ 499 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb) 500 { 501 struct au0828_buffer *buf; 502 struct au0828_buffer *vbi_buf; 503 struct au0828_dmaqueue *dma_q = urb->context; 504 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq; 505 unsigned char *outp = NULL; 506 unsigned char *vbioutp = NULL; 507 int i, len = 0, rc = 1; 508 unsigned char *p; 509 unsigned char fbyte; 510 unsigned int vbi_field_size; 511 unsigned int remain, lencopy; 512 513 if (!dev) 514 return 0; 515 516 if (test_bit(DEV_DISCONNECTED, &dev->dev_state) || 517 test_bit(DEV_MISCONFIGURED, &dev->dev_state)) 518 return 0; 519 520 if (urb->status < 0) { 521 print_err_status(dev, -1, urb->status); 522 if (urb->status == -ENOENT) 523 return 0; 524 } 525 526 buf = dev->isoc_ctl.buf; 527 if (buf != NULL) 528 outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); 529 530 vbi_buf = dev->isoc_ctl.vbi_buf; 531 if (vbi_buf != NULL) 532 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0); 533 534 for (i = 0; i < urb->number_of_packets; i++) { 535 int status = urb->iso_frame_desc[i].status; 536 537 if (status < 0) { 538 print_err_status(dev, i, status); 539 if (urb->iso_frame_desc[i].status != -EPROTO) 540 continue; 541 } 542 543 if (urb->iso_frame_desc[i].actual_length <= 0) 544 continue; 545 546 if (urb->iso_frame_desc[i].actual_length > 547 dev->max_pkt_size) { 548 au0828_isocdbg("packet bigger than packet size"); 549 continue; 550 } 551 552 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 553 fbyte = p[0]; 554 len = urb->iso_frame_desc[i].actual_length - 4; 555 p += 4; 556 557 if (fbyte & 0x80) { 558 len -= 4; 559 p += 4; 560 au0828_isocdbg("Video frame %s\n", 561 (fbyte & 0x40) ? "odd" : "even"); 562 if (fbyte & 0x40) { 563 /* VBI */ 564 if (vbi_buf != NULL) 565 buffer_filled(dev, vbi_dma_q, vbi_buf); 566 vbi_get_next_buf(vbi_dma_q, &vbi_buf); 567 if (vbi_buf == NULL) 568 vbioutp = NULL; 569 else 570 vbioutp = vb2_plane_vaddr( 571 &vbi_buf->vb.vb2_buf, 0); 572 573 /* Video */ 574 if (buf != NULL) 575 buffer_filled(dev, dma_q, buf); 576 get_next_buf(dma_q, &buf); 577 if (buf == NULL) 578 outp = NULL; 579 else 580 outp = vb2_plane_vaddr( 581 &buf->vb.vb2_buf, 0); 582 583 /* As long as isoc traffic is arriving, keep 584 resetting the timer */ 585 if (dev->vid_timeout_running) 586 mod_timer(&dev->vid_timeout, 587 jiffies + (HZ / 10)); 588 if (dev->vbi_timeout_running) 589 mod_timer(&dev->vbi_timeout, 590 jiffies + (HZ / 10)); 591 } 592 593 if (buf != NULL) { 594 if (fbyte & 0x40) 595 buf->top_field = 1; 596 else 597 buf->top_field = 0; 598 } 599 600 if (vbi_buf != NULL) { 601 if (fbyte & 0x40) 602 vbi_buf->top_field = 1; 603 else 604 vbi_buf->top_field = 0; 605 } 606 607 dev->vbi_read = 0; 608 vbi_dma_q->pos = 0; 609 dma_q->pos = 0; 610 } 611 612 vbi_field_size = dev->vbi_width * dev->vbi_height * 2; 613 if (dev->vbi_read < vbi_field_size) { 614 remain = vbi_field_size - dev->vbi_read; 615 if (len < remain) 616 lencopy = len; 617 else 618 lencopy = remain; 619 620 if (vbi_buf != NULL) 621 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p, 622 vbioutp, len); 623 624 len -= lencopy; 625 p += lencopy; 626 dev->vbi_read += lencopy; 627 } 628 629 if (dev->vbi_read >= vbi_field_size && buf != NULL) 630 au0828_copy_video(dev, dma_q, buf, p, outp, len); 631 } 632 return rc; 633 } 634 635 void au0828_usb_v4l2_media_release(struct au0828_dev *dev) 636 { 637 #ifdef CONFIG_MEDIA_CONTROLLER 638 int i; 639 640 for (i = 0; i < AU0828_MAX_INPUT; i++) { 641 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED) 642 return; 643 media_device_unregister_entity(&dev->input_ent[i]); 644 } 645 #endif 646 } 647 648 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev) 649 { 650 struct au0828_dev *dev = 651 container_of(v4l2_dev, struct au0828_dev, v4l2_dev); 652 653 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl); 654 v4l2_device_unregister(&dev->v4l2_dev); 655 au0828_usb_v4l2_media_release(dev); 656 au0828_usb_release(dev); 657 } 658 659 int au0828_v4l2_device_register(struct usb_interface *interface, 660 struct au0828_dev *dev) 661 { 662 int retval; 663 664 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED) 665 return 0; 666 667 /* Create the v4l2_device */ 668 #ifdef CONFIG_MEDIA_CONTROLLER 669 dev->v4l2_dev.mdev = dev->media_dev; 670 #endif 671 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev); 672 if (retval) { 673 pr_err("%s() v4l2_device_register failed\n", 674 __func__); 675 return retval; 676 } 677 678 dev->v4l2_dev.release = au0828_usb_v4l2_release; 679 680 /* This control handler will inherit the controls from au8522 */ 681 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4); 682 if (retval) { 683 pr_err("%s() v4l2_ctrl_handler_init failed\n", 684 __func__); 685 return retval; 686 } 687 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl; 688 689 return 0; 690 } 691 692 static int queue_setup(struct vb2_queue *vq, 693 unsigned int *nbuffers, unsigned int *nplanes, 694 unsigned int sizes[], struct device *alloc_devs[]) 695 { 696 struct au0828_dev *dev = vb2_get_drv_priv(vq); 697 unsigned long size = dev->height * dev->bytesperline; 698 699 if (*nplanes) 700 return sizes[0] < size ? -EINVAL : 0; 701 *nplanes = 1; 702 sizes[0] = size; 703 return 0; 704 } 705 706 static int 707 buffer_prepare(struct vb2_buffer *vb) 708 { 709 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 710 struct au0828_buffer *buf = container_of(vbuf, 711 struct au0828_buffer, vb); 712 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 713 714 buf->length = dev->height * dev->bytesperline; 715 716 if (vb2_plane_size(vb, 0) < buf->length) { 717 pr_err("%s data will not fit into plane (%lu < %lu)\n", 718 __func__, vb2_plane_size(vb, 0), buf->length); 719 return -EINVAL; 720 } 721 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length); 722 return 0; 723 } 724 725 static void 726 buffer_queue(struct vb2_buffer *vb) 727 { 728 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 729 struct au0828_buffer *buf = container_of(vbuf, 730 struct au0828_buffer, 731 vb); 732 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 733 struct au0828_dmaqueue *vidq = &dev->vidq; 734 unsigned long flags = 0; 735 736 buf->mem = vb2_plane_vaddr(vb, 0); 737 buf->length = vb2_plane_size(vb, 0); 738 739 spin_lock_irqsave(&dev->slock, flags); 740 list_add_tail(&buf->list, &vidq->active); 741 spin_unlock_irqrestore(&dev->slock, flags); 742 } 743 744 static int au0828_i2s_init(struct au0828_dev *dev) 745 { 746 /* Enable i2s mode */ 747 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01); 748 return 0; 749 } 750 751 /* 752 * Auvitek au0828 analog stream enable 753 */ 754 static int au0828_analog_stream_enable(struct au0828_dev *d) 755 { 756 struct usb_interface *iface; 757 int ret, h, w; 758 759 dprintk(1, "au0828_analog_stream_enable called\n"); 760 761 if (test_bit(DEV_DISCONNECTED, &d->dev_state)) 762 return -ENODEV; 763 764 iface = usb_ifnum_to_if(d->usbdev, 0); 765 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) { 766 dprintk(1, "Changing intf#0 to alt 5\n"); 767 /* set au0828 interface0 to AS5 here again */ 768 ret = usb_set_interface(d->usbdev, 0, 5); 769 if (ret < 0) { 770 pr_info("Au0828 can't set alt setting to 5!\n"); 771 return -EBUSY; 772 } 773 } 774 775 h = d->height / 2 + 2; 776 w = d->width * 2; 777 778 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00); 779 au0828_writereg(d, 0x106, 0x00); 780 /* set x position */ 781 au0828_writereg(d, 0x110, 0x00); 782 au0828_writereg(d, 0x111, 0x00); 783 au0828_writereg(d, 0x114, w & 0xff); 784 au0828_writereg(d, 0x115, w >> 8); 785 /* set y position */ 786 au0828_writereg(d, 0x112, 0x00); 787 au0828_writereg(d, 0x113, 0x00); 788 au0828_writereg(d, 0x116, h & 0xff); 789 au0828_writereg(d, 0x117, h >> 8); 790 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3); 791 792 return 0; 793 } 794 795 static int au0828_analog_stream_disable(struct au0828_dev *d) 796 { 797 dprintk(1, "au0828_analog_stream_disable called\n"); 798 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0); 799 return 0; 800 } 801 802 static void au0828_analog_stream_reset(struct au0828_dev *dev) 803 { 804 dprintk(1, "au0828_analog_stream_reset called\n"); 805 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0); 806 mdelay(30); 807 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3); 808 } 809 810 /* 811 * Some operations needs to stop current streaming 812 */ 813 static int au0828_stream_interrupt(struct au0828_dev *dev) 814 { 815 dev->stream_state = STREAM_INTERRUPT; 816 if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) 817 return -ENODEV; 818 return 0; 819 } 820 821 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count) 822 { 823 struct au0828_dev *dev = vb2_get_drv_priv(vq); 824 int rc = 0; 825 826 dprintk(1, "au0828_start_analog_streaming called %d\n", 827 dev->streaming_users); 828 829 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 830 dev->frame_count = 0; 831 else 832 dev->vbi_frame_count = 0; 833 834 if (dev->streaming_users == 0) { 835 /* If we were doing ac97 instead of i2s, it would go here...*/ 836 au0828_i2s_init(dev); 837 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB, 838 AU0828_MAX_ISO_BUFS, dev->max_pkt_size, 839 au0828_isoc_copy); 840 if (rc < 0) { 841 pr_info("au0828_init_isoc failed\n"); 842 return rc; 843 } 844 845 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1); 846 847 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 848 dev->vid_timeout_running = 1; 849 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); 850 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 851 dev->vbi_timeout_running = 1; 852 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); 853 } 854 } 855 dev->streaming_users++; 856 return rc; 857 } 858 859 static void au0828_stop_streaming(struct vb2_queue *vq) 860 { 861 struct au0828_dev *dev = vb2_get_drv_priv(vq); 862 struct au0828_dmaqueue *vidq = &dev->vidq; 863 unsigned long flags = 0; 864 865 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users); 866 867 if (dev->streaming_users-- == 1) { 868 au0828_uninit_isoc(dev); 869 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); 870 } 871 872 dev->vid_timeout_running = 0; 873 del_timer_sync(&dev->vid_timeout); 874 875 spin_lock_irqsave(&dev->slock, flags); 876 if (dev->isoc_ctl.buf != NULL) { 877 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf, 878 VB2_BUF_STATE_ERROR); 879 dev->isoc_ctl.buf = NULL; 880 } 881 while (!list_empty(&vidq->active)) { 882 struct au0828_buffer *buf; 883 884 buf = list_entry(vidq->active.next, struct au0828_buffer, list); 885 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 886 list_del(&buf->list); 887 } 888 spin_unlock_irqrestore(&dev->slock, flags); 889 } 890 891 void au0828_stop_vbi_streaming(struct vb2_queue *vq) 892 { 893 struct au0828_dev *dev = vb2_get_drv_priv(vq); 894 struct au0828_dmaqueue *vbiq = &dev->vbiq; 895 unsigned long flags = 0; 896 897 dprintk(1, "au0828_stop_vbi_streaming called %d\n", 898 dev->streaming_users); 899 900 if (dev->streaming_users-- == 1) { 901 au0828_uninit_isoc(dev); 902 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); 903 } 904 905 spin_lock_irqsave(&dev->slock, flags); 906 if (dev->isoc_ctl.vbi_buf != NULL) { 907 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf, 908 VB2_BUF_STATE_ERROR); 909 dev->isoc_ctl.vbi_buf = NULL; 910 } 911 while (!list_empty(&vbiq->active)) { 912 struct au0828_buffer *buf; 913 914 buf = list_entry(vbiq->active.next, struct au0828_buffer, list); 915 list_del(&buf->list); 916 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 917 } 918 spin_unlock_irqrestore(&dev->slock, flags); 919 920 dev->vbi_timeout_running = 0; 921 del_timer_sync(&dev->vbi_timeout); 922 } 923 924 static const struct vb2_ops au0828_video_qops = { 925 .queue_setup = queue_setup, 926 .buf_prepare = buffer_prepare, 927 .buf_queue = buffer_queue, 928 .start_streaming = au0828_start_analog_streaming, 929 .stop_streaming = au0828_stop_streaming, 930 .wait_prepare = vb2_ops_wait_prepare, 931 .wait_finish = vb2_ops_wait_finish, 932 }; 933 934 /* ------------------------------------------------------------------ 935 V4L2 interface 936 ------------------------------------------------------------------*/ 937 /* 938 * au0828_analog_unregister 939 * unregister v4l2 devices 940 */ 941 int au0828_analog_unregister(struct au0828_dev *dev) 942 { 943 dprintk(1, "au0828_analog_unregister called\n"); 944 945 /* No analog TV */ 946 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED) 947 return 0; 948 949 mutex_lock(&au0828_sysfs_lock); 950 video_unregister_device(&dev->vdev); 951 video_unregister_device(&dev->vbi_dev); 952 mutex_unlock(&au0828_sysfs_lock); 953 954 v4l2_device_disconnect(&dev->v4l2_dev); 955 v4l2_device_put(&dev->v4l2_dev); 956 957 return 1; 958 } 959 960 /* This function ensures that video frames continue to be delivered even if 961 the ITU-656 input isn't receiving any data (thereby preventing applications 962 such as tvtime from hanging) */ 963 static void au0828_vid_buffer_timeout(struct timer_list *t) 964 { 965 struct au0828_dev *dev = from_timer(dev, t, vid_timeout); 966 struct au0828_dmaqueue *dma_q = &dev->vidq; 967 struct au0828_buffer *buf; 968 unsigned char *vid_data; 969 unsigned long flags = 0; 970 971 spin_lock_irqsave(&dev->slock, flags); 972 973 buf = dev->isoc_ctl.buf; 974 if (buf != NULL) { 975 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); 976 memset(vid_data, 0x00, buf->length); /* Blank green frame */ 977 buffer_filled(dev, dma_q, buf); 978 } 979 get_next_buf(dma_q, &buf); 980 981 if (dev->vid_timeout_running == 1) 982 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); 983 984 spin_unlock_irqrestore(&dev->slock, flags); 985 } 986 987 static void au0828_vbi_buffer_timeout(struct timer_list *t) 988 { 989 struct au0828_dev *dev = from_timer(dev, t, vbi_timeout); 990 struct au0828_dmaqueue *dma_q = &dev->vbiq; 991 struct au0828_buffer *buf; 992 unsigned char *vbi_data; 993 unsigned long flags = 0; 994 995 spin_lock_irqsave(&dev->slock, flags); 996 997 buf = dev->isoc_ctl.vbi_buf; 998 if (buf != NULL) { 999 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); 1000 memset(vbi_data, 0x00, buf->length); 1001 buffer_filled(dev, dma_q, buf); 1002 } 1003 vbi_get_next_buf(dma_q, &buf); 1004 1005 if (dev->vbi_timeout_running == 1) 1006 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); 1007 spin_unlock_irqrestore(&dev->slock, flags); 1008 } 1009 1010 static int au0828_v4l2_open(struct file *filp) 1011 { 1012 struct au0828_dev *dev = video_drvdata(filp); 1013 int ret; 1014 1015 dprintk(1, 1016 "%s called std_set %d dev_state %ld stream users %d users %d\n", 1017 __func__, dev->std_set_in_tuner_core, dev->dev_state, 1018 dev->streaming_users, dev->users); 1019 1020 if (mutex_lock_interruptible(&dev->lock)) 1021 return -ERESTARTSYS; 1022 1023 ret = v4l2_fh_open(filp); 1024 if (ret) { 1025 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n", 1026 __func__, ret); 1027 mutex_unlock(&dev->lock); 1028 return ret; 1029 } 1030 1031 if (dev->users == 0) { 1032 au0828_analog_stream_enable(dev); 1033 au0828_analog_stream_reset(dev); 1034 dev->stream_state = STREAM_OFF; 1035 set_bit(DEV_INITIALIZED, &dev->dev_state); 1036 } 1037 dev->users++; 1038 mutex_unlock(&dev->lock); 1039 return ret; 1040 } 1041 1042 static int au0828_v4l2_close(struct file *filp) 1043 { 1044 int ret; 1045 struct au0828_dev *dev = video_drvdata(filp); 1046 struct video_device *vdev = video_devdata(filp); 1047 1048 dprintk(1, 1049 "%s called std_set %d dev_state %ld stream users %d users %d\n", 1050 __func__, dev->std_set_in_tuner_core, dev->dev_state, 1051 dev->streaming_users, dev->users); 1052 1053 mutex_lock(&dev->lock); 1054 if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) { 1055 /* Cancel timeout thread in case they didn't call streamoff */ 1056 dev->vid_timeout_running = 0; 1057 del_timer_sync(&dev->vid_timeout); 1058 } else if (vdev->vfl_type == VFL_TYPE_VBI && 1059 dev->vbi_timeout_running) { 1060 /* Cancel timeout thread in case they didn't call streamoff */ 1061 dev->vbi_timeout_running = 0; 1062 del_timer_sync(&dev->vbi_timeout); 1063 } 1064 1065 if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) 1066 goto end; 1067 1068 if (dev->users == 1) { 1069 /* 1070 * Avoid putting tuner in sleep if DVB or ALSA are 1071 * streaming. 1072 * 1073 * On most USB devices like au0828 the tuner can 1074 * be safely put in sleep state here if ALSA isn't 1075 * streaming. Exceptions are some very old USB tuner 1076 * models such as em28xx-based WinTV USB2 which have 1077 * a separate audio output jack. The devices that have 1078 * a separate audio output jack have analog tuners, 1079 * like Philips FM1236. Those devices are always on, 1080 * so the s_power callback are silently ignored. 1081 * So, the current logic here does the following: 1082 * Disable (put tuner to sleep) when 1083 * - ALSA and DVB aren't streaming. 1084 * - the last V4L2 file handler is closed. 1085 * 1086 * FIXME: 1087 * 1088 * Additionally, this logic could be improved to 1089 * disable the media source if the above conditions 1090 * are met and if the device: 1091 * - doesn't have a separate audio out plug (or 1092 * - doesn't use a silicon tuner like xc2028/3028/4000/5000). 1093 * 1094 * Once this additional logic is in place, a callback 1095 * is needed to enable the media source and power on 1096 * the tuner, for radio to work. 1097 */ 1098 ret = v4l_enable_media_source(vdev); 1099 if (ret == 0) 1100 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, 1101 standby); 1102 dev->std_set_in_tuner_core = 0; 1103 1104 /* When close the device, set the usb intf0 into alt0 to free 1105 USB bandwidth */ 1106 ret = usb_set_interface(dev->usbdev, 0, 0); 1107 if (ret < 0) 1108 pr_info("Au0828 can't set alternate to 0!\n"); 1109 } 1110 end: 1111 _vb2_fop_release(filp, NULL); 1112 dev->users--; 1113 mutex_unlock(&dev->lock); 1114 return 0; 1115 } 1116 1117 /* Must be called with dev->lock held */ 1118 static void au0828_init_tuner(struct au0828_dev *dev) 1119 { 1120 struct v4l2_frequency f = { 1121 .frequency = dev->ctrl_freq, 1122 .type = V4L2_TUNER_ANALOG_TV, 1123 }; 1124 1125 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1126 dev->std_set_in_tuner_core, dev->dev_state); 1127 1128 if (dev->std_set_in_tuner_core) 1129 return; 1130 dev->std_set_in_tuner_core = 1; 1131 i2c_gate_ctrl(dev, 1); 1132 /* If we've never sent the standard in tuner core, do so now. 1133 We don't do this at device probe because we don't want to 1134 incur the cost of a firmware load */ 1135 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std); 1136 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f); 1137 i2c_gate_ctrl(dev, 0); 1138 } 1139 1140 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd, 1141 struct v4l2_format *format) 1142 { 1143 int ret; 1144 int width = format->fmt.pix.width; 1145 int height = format->fmt.pix.height; 1146 1147 /* If they are demanding a format other than the one we support, 1148 bail out (tvtime asks for UYVY and then retries with YUYV) */ 1149 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY) 1150 return -EINVAL; 1151 1152 /* format->fmt.pix.width only support 720 and height 480 */ 1153 if (width != 720) 1154 width = 720; 1155 if (height != 480) 1156 height = 480; 1157 1158 format->fmt.pix.width = width; 1159 format->fmt.pix.height = height; 1160 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; 1161 format->fmt.pix.bytesperline = width * 2; 1162 format->fmt.pix.sizeimage = width * height * 2; 1163 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 1164 format->fmt.pix.field = V4L2_FIELD_INTERLACED; 1165 format->fmt.pix.priv = 0; 1166 1167 if (cmd == VIDIOC_TRY_FMT) 1168 return 0; 1169 1170 /* maybe set new image format, driver current only support 720*480 */ 1171 dev->width = width; 1172 dev->height = height; 1173 dev->frame_size = width * height * 2; 1174 dev->field_size = width * height; 1175 dev->bytesperline = width * 2; 1176 1177 if (dev->stream_state == STREAM_ON) { 1178 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n"); 1179 ret = au0828_stream_interrupt(dev); 1180 if (ret != 0) { 1181 dprintk(1, "error interrupting video stream!\n"); 1182 return ret; 1183 } 1184 } 1185 1186 au0828_analog_stream_enable(dev); 1187 1188 return 0; 1189 } 1190 1191 static int vidioc_querycap(struct file *file, void *priv, 1192 struct v4l2_capability *cap) 1193 { 1194 struct video_device *vdev = video_devdata(file); 1195 struct au0828_dev *dev = video_drvdata(file); 1196 1197 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1198 dev->std_set_in_tuner_core, dev->dev_state); 1199 1200 strscpy(cap->driver, "au0828", sizeof(cap->driver)); 1201 strscpy(cap->card, dev->board.name, sizeof(cap->card)); 1202 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info)); 1203 1204 /* set the device capabilities */ 1205 cap->device_caps = V4L2_CAP_AUDIO | 1206 V4L2_CAP_READWRITE | 1207 V4L2_CAP_STREAMING | 1208 V4L2_CAP_TUNER; 1209 if (vdev->vfl_type == VFL_TYPE_GRABBER) 1210 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE; 1211 else 1212 cap->device_caps |= V4L2_CAP_VBI_CAPTURE; 1213 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS | 1214 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE; 1215 return 0; 1216 } 1217 1218 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 1219 struct v4l2_fmtdesc *f) 1220 { 1221 if (f->index) 1222 return -EINVAL; 1223 1224 dprintk(1, "%s called\n", __func__); 1225 1226 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1227 strscpy(f->description, "Packed YUV2", sizeof(f->description)); 1228 1229 f->flags = 0; 1230 f->pixelformat = V4L2_PIX_FMT_UYVY; 1231 1232 return 0; 1233 } 1234 1235 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 1236 struct v4l2_format *f) 1237 { 1238 struct au0828_dev *dev = video_drvdata(file); 1239 1240 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1241 dev->std_set_in_tuner_core, dev->dev_state); 1242 1243 f->fmt.pix.width = dev->width; 1244 f->fmt.pix.height = dev->height; 1245 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; 1246 f->fmt.pix.bytesperline = dev->bytesperline; 1247 f->fmt.pix.sizeimage = dev->frame_size; 1248 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */ 1249 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 1250 f->fmt.pix.priv = 0; 1251 return 0; 1252 } 1253 1254 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 1255 struct v4l2_format *f) 1256 { 1257 struct au0828_dev *dev = video_drvdata(file); 1258 1259 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1260 dev->std_set_in_tuner_core, dev->dev_state); 1261 1262 return au0828_set_format(dev, VIDIOC_TRY_FMT, f); 1263 } 1264 1265 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 1266 struct v4l2_format *f) 1267 { 1268 struct au0828_dev *dev = video_drvdata(file); 1269 int rc; 1270 1271 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1272 dev->std_set_in_tuner_core, dev->dev_state); 1273 1274 rc = check_dev(dev); 1275 if (rc < 0) 1276 return rc; 1277 1278 if (vb2_is_busy(&dev->vb_vidq)) { 1279 pr_info("%s queue busy\n", __func__); 1280 rc = -EBUSY; 1281 goto out; 1282 } 1283 1284 rc = au0828_set_format(dev, VIDIOC_S_FMT, f); 1285 out: 1286 return rc; 1287 } 1288 1289 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm) 1290 { 1291 struct au0828_dev *dev = video_drvdata(file); 1292 1293 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1294 dev->std_set_in_tuner_core, dev->dev_state); 1295 1296 if (norm == dev->std) 1297 return 0; 1298 1299 if (dev->streaming_users > 0) 1300 return -EBUSY; 1301 1302 dev->std = norm; 1303 1304 au0828_init_tuner(dev); 1305 1306 i2c_gate_ctrl(dev, 1); 1307 1308 /* 1309 * FIXME: when we support something other than 60Hz standards, 1310 * we are going to have to make the au0828 bridge adjust the size 1311 * of its capture buffer, which is currently hardcoded at 720x480 1312 */ 1313 1314 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm); 1315 1316 i2c_gate_ctrl(dev, 0); 1317 1318 return 0; 1319 } 1320 1321 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) 1322 { 1323 struct au0828_dev *dev = video_drvdata(file); 1324 1325 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1326 dev->std_set_in_tuner_core, dev->dev_state); 1327 1328 *norm = dev->std; 1329 return 0; 1330 } 1331 1332 static int vidioc_enum_input(struct file *file, void *priv, 1333 struct v4l2_input *input) 1334 { 1335 struct au0828_dev *dev = video_drvdata(file); 1336 unsigned int tmp; 1337 1338 static const char *inames[] = { 1339 [AU0828_VMUX_UNDEFINED] = "Undefined", 1340 [AU0828_VMUX_COMPOSITE] = "Composite", 1341 [AU0828_VMUX_SVIDEO] = "S-Video", 1342 [AU0828_VMUX_CABLE] = "Cable TV", 1343 [AU0828_VMUX_TELEVISION] = "Television", 1344 [AU0828_VMUX_DVB] = "DVB", 1345 }; 1346 1347 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1348 dev->std_set_in_tuner_core, dev->dev_state); 1349 1350 tmp = input->index; 1351 1352 if (tmp >= AU0828_MAX_INPUT) 1353 return -EINVAL; 1354 if (AUVI_INPUT(tmp).type == 0) 1355 return -EINVAL; 1356 1357 input->index = tmp; 1358 strscpy(input->name, inames[AUVI_INPUT(tmp).type], sizeof(input->name)); 1359 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) || 1360 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) { 1361 input->type |= V4L2_INPUT_TYPE_TUNER; 1362 input->audioset = 1; 1363 } else { 1364 input->type |= V4L2_INPUT_TYPE_CAMERA; 1365 input->audioset = 2; 1366 } 1367 1368 input->std = dev->vdev.tvnorms; 1369 1370 return 0; 1371 } 1372 1373 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1374 { 1375 struct au0828_dev *dev = video_drvdata(file); 1376 1377 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1378 dev->std_set_in_tuner_core, dev->dev_state); 1379 1380 *i = dev->ctrl_input; 1381 return 0; 1382 } 1383 1384 static void au0828_s_input(struct au0828_dev *dev, int index) 1385 { 1386 int i; 1387 1388 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1389 dev->std_set_in_tuner_core, dev->dev_state); 1390 1391 switch (AUVI_INPUT(index).type) { 1392 case AU0828_VMUX_SVIDEO: 1393 dev->input_type = AU0828_VMUX_SVIDEO; 1394 dev->ctrl_ainput = 1; 1395 break; 1396 case AU0828_VMUX_COMPOSITE: 1397 dev->input_type = AU0828_VMUX_COMPOSITE; 1398 dev->ctrl_ainput = 1; 1399 break; 1400 case AU0828_VMUX_TELEVISION: 1401 dev->input_type = AU0828_VMUX_TELEVISION; 1402 dev->ctrl_ainput = 0; 1403 break; 1404 default: 1405 dprintk(1, "unknown input type set [%d]\n", 1406 AUVI_INPUT(index).type); 1407 return; 1408 } 1409 1410 dev->ctrl_input = index; 1411 1412 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing, 1413 AUVI_INPUT(index).vmux, 0, 0); 1414 1415 for (i = 0; i < AU0828_MAX_INPUT; i++) { 1416 int enable = 0; 1417 if (AUVI_INPUT(i).audio_setup == NULL) 1418 continue; 1419 1420 if (i == index) 1421 enable = 1; 1422 else 1423 enable = 0; 1424 if (enable) { 1425 (AUVI_INPUT(i).audio_setup)(dev, enable); 1426 } else { 1427 /* Make sure we leave it turned on if some 1428 other input is routed to this callback */ 1429 if ((AUVI_INPUT(i).audio_setup) != 1430 ((AUVI_INPUT(index).audio_setup))) { 1431 (AUVI_INPUT(i).audio_setup)(dev, enable); 1432 } 1433 } 1434 } 1435 1436 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing, 1437 AUVI_INPUT(index).amux, 0, 0); 1438 } 1439 1440 static int vidioc_s_input(struct file *file, void *priv, unsigned int index) 1441 { 1442 struct au0828_dev *dev = video_drvdata(file); 1443 struct video_device *vfd = video_devdata(file); 1444 1445 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__, 1446 index); 1447 if (index >= AU0828_MAX_INPUT) 1448 return -EINVAL; 1449 if (AUVI_INPUT(index).type == 0) 1450 return -EINVAL; 1451 1452 if (dev->ctrl_input == index) 1453 return 0; 1454 1455 au0828_s_input(dev, index); 1456 1457 /* 1458 * Input has been changed. Disable the media source 1459 * associated with the old input and enable source 1460 * for the newly set input 1461 */ 1462 v4l_disable_media_source(vfd); 1463 return v4l_enable_media_source(vfd); 1464 } 1465 1466 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a) 1467 { 1468 if (a->index > 1) 1469 return -EINVAL; 1470 1471 dprintk(1, "%s called\n", __func__); 1472 1473 if (a->index == 0) 1474 strscpy(a->name, "Television", sizeof(a->name)); 1475 else 1476 strscpy(a->name, "Line in", sizeof(a->name)); 1477 1478 a->capability = V4L2_AUDCAP_STEREO; 1479 return 0; 1480 } 1481 1482 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) 1483 { 1484 struct au0828_dev *dev = video_drvdata(file); 1485 1486 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1487 dev->std_set_in_tuner_core, dev->dev_state); 1488 1489 a->index = dev->ctrl_ainput; 1490 if (a->index == 0) 1491 strscpy(a->name, "Television", sizeof(a->name)); 1492 else 1493 strscpy(a->name, "Line in", sizeof(a->name)); 1494 1495 a->capability = V4L2_AUDCAP_STEREO; 1496 return 0; 1497 } 1498 1499 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a) 1500 { 1501 struct au0828_dev *dev = video_drvdata(file); 1502 1503 if (a->index != dev->ctrl_ainput) 1504 return -EINVAL; 1505 1506 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1507 dev->std_set_in_tuner_core, dev->dev_state); 1508 return 0; 1509 } 1510 1511 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) 1512 { 1513 struct au0828_dev *dev = video_drvdata(file); 1514 struct video_device *vfd = video_devdata(file); 1515 int ret; 1516 1517 if (t->index != 0) 1518 return -EINVAL; 1519 1520 ret = v4l_enable_media_source(vfd); 1521 if (ret) 1522 return ret; 1523 1524 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1525 dev->std_set_in_tuner_core, dev->dev_state); 1526 1527 strscpy(t->name, "Auvitek tuner", sizeof(t->name)); 1528 1529 au0828_init_tuner(dev); 1530 i2c_gate_ctrl(dev, 1); 1531 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); 1532 i2c_gate_ctrl(dev, 0); 1533 return 0; 1534 } 1535 1536 static int vidioc_s_tuner(struct file *file, void *priv, 1537 const struct v4l2_tuner *t) 1538 { 1539 struct au0828_dev *dev = video_drvdata(file); 1540 1541 if (t->index != 0) 1542 return -EINVAL; 1543 1544 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1545 dev->std_set_in_tuner_core, dev->dev_state); 1546 1547 au0828_init_tuner(dev); 1548 i2c_gate_ctrl(dev, 1); 1549 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t); 1550 i2c_gate_ctrl(dev, 0); 1551 1552 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal, 1553 t->afc); 1554 1555 return 0; 1556 1557 } 1558 1559 static int vidioc_g_frequency(struct file *file, void *priv, 1560 struct v4l2_frequency *freq) 1561 { 1562 struct au0828_dev *dev = video_drvdata(file); 1563 1564 if (freq->tuner != 0) 1565 return -EINVAL; 1566 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1567 dev->std_set_in_tuner_core, dev->dev_state); 1568 freq->frequency = dev->ctrl_freq; 1569 return 0; 1570 } 1571 1572 static int vidioc_s_frequency(struct file *file, void *priv, 1573 const struct v4l2_frequency *freq) 1574 { 1575 struct au0828_dev *dev = video_drvdata(file); 1576 struct v4l2_frequency new_freq = *freq; 1577 1578 if (freq->tuner != 0) 1579 return -EINVAL; 1580 1581 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1582 dev->std_set_in_tuner_core, dev->dev_state); 1583 1584 au0828_init_tuner(dev); 1585 i2c_gate_ctrl(dev, 1); 1586 1587 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq); 1588 /* Get the actual set (and possibly clamped) frequency */ 1589 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq); 1590 dev->ctrl_freq = new_freq.frequency; 1591 1592 i2c_gate_ctrl(dev, 0); 1593 1594 au0828_analog_stream_reset(dev); 1595 1596 return 0; 1597 } 1598 1599 1600 /* RAW VBI ioctls */ 1601 1602 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, 1603 struct v4l2_format *format) 1604 { 1605 struct au0828_dev *dev = video_drvdata(file); 1606 1607 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1608 dev->std_set_in_tuner_core, dev->dev_state); 1609 1610 format->fmt.vbi.samples_per_line = dev->vbi_width; 1611 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; 1612 format->fmt.vbi.offset = 0; 1613 format->fmt.vbi.flags = 0; 1614 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1615 1616 format->fmt.vbi.count[0] = dev->vbi_height; 1617 format->fmt.vbi.count[1] = dev->vbi_height; 1618 format->fmt.vbi.start[0] = 21; 1619 format->fmt.vbi.start[1] = 284; 1620 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved)); 1621 1622 return 0; 1623 } 1624 1625 static int vidioc_g_pixelaspect(struct file *file, void *priv, 1626 int type, struct v4l2_fract *f) 1627 { 1628 struct au0828_dev *dev = video_drvdata(file); 1629 1630 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1631 return -EINVAL; 1632 1633 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1634 dev->std_set_in_tuner_core, dev->dev_state); 1635 1636 f->numerator = 54; 1637 f->denominator = 59; 1638 1639 return 0; 1640 } 1641 1642 static int vidioc_g_selection(struct file *file, void *priv, 1643 struct v4l2_selection *s) 1644 { 1645 struct au0828_dev *dev = video_drvdata(file); 1646 1647 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1648 return -EINVAL; 1649 1650 switch (s->target) { 1651 case V4L2_SEL_TGT_CROP_BOUNDS: 1652 case V4L2_SEL_TGT_CROP_DEFAULT: 1653 s->r.left = 0; 1654 s->r.top = 0; 1655 s->r.width = dev->width; 1656 s->r.height = dev->height; 1657 break; 1658 default: 1659 return -EINVAL; 1660 } 1661 return 0; 1662 } 1663 1664 #ifdef CONFIG_VIDEO_ADV_DEBUG 1665 static int vidioc_g_register(struct file *file, void *priv, 1666 struct v4l2_dbg_register *reg) 1667 { 1668 struct au0828_dev *dev = video_drvdata(file); 1669 1670 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1671 dev->std_set_in_tuner_core, dev->dev_state); 1672 1673 reg->val = au0828_read(dev, reg->reg); 1674 reg->size = 1; 1675 return 0; 1676 } 1677 1678 static int vidioc_s_register(struct file *file, void *priv, 1679 const struct v4l2_dbg_register *reg) 1680 { 1681 struct au0828_dev *dev = video_drvdata(file); 1682 1683 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, 1684 dev->std_set_in_tuner_core, dev->dev_state); 1685 1686 return au0828_writereg(dev, reg->reg, reg->val); 1687 } 1688 #endif 1689 1690 static int vidioc_log_status(struct file *file, void *fh) 1691 { 1692 struct video_device *vdev = video_devdata(file); 1693 1694 dprintk(1, "%s called\n", __func__); 1695 1696 v4l2_ctrl_log_status(file, fh); 1697 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status); 1698 return 0; 1699 } 1700 1701 void au0828_v4l2_suspend(struct au0828_dev *dev) 1702 { 1703 struct urb *urb; 1704 int i; 1705 1706 pr_info("stopping V4L2\n"); 1707 1708 if (dev->stream_state == STREAM_ON) { 1709 pr_info("stopping V4L2 active URBs\n"); 1710 au0828_analog_stream_disable(dev); 1711 /* stop urbs */ 1712 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { 1713 urb = dev->isoc_ctl.urb[i]; 1714 if (urb) { 1715 if (!irqs_disabled()) 1716 usb_kill_urb(urb); 1717 else 1718 usb_unlink_urb(urb); 1719 } 1720 } 1721 } 1722 1723 if (dev->vid_timeout_running) 1724 del_timer_sync(&dev->vid_timeout); 1725 if (dev->vbi_timeout_running) 1726 del_timer_sync(&dev->vbi_timeout); 1727 } 1728 1729 void au0828_v4l2_resume(struct au0828_dev *dev) 1730 { 1731 int i, rc; 1732 1733 pr_info("restarting V4L2\n"); 1734 1735 if (dev->stream_state == STREAM_ON) { 1736 au0828_stream_interrupt(dev); 1737 au0828_init_tuner(dev); 1738 } 1739 1740 if (dev->vid_timeout_running) 1741 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); 1742 if (dev->vbi_timeout_running) 1743 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); 1744 1745 /* If we were doing ac97 instead of i2s, it would go here...*/ 1746 au0828_i2s_init(dev); 1747 1748 au0828_analog_stream_enable(dev); 1749 1750 if (!(dev->stream_state == STREAM_ON)) { 1751 au0828_analog_stream_reset(dev); 1752 /* submit urbs */ 1753 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { 1754 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); 1755 if (rc) { 1756 au0828_isocdbg("submit of urb %i failed (error=%i)\n", 1757 i, rc); 1758 au0828_uninit_isoc(dev); 1759 } 1760 } 1761 } 1762 } 1763 1764 static const struct v4l2_file_operations au0828_v4l_fops = { 1765 .owner = THIS_MODULE, 1766 .open = au0828_v4l2_open, 1767 .release = au0828_v4l2_close, 1768 .read = vb2_fop_read, 1769 .poll = vb2_fop_poll, 1770 .mmap = vb2_fop_mmap, 1771 .unlocked_ioctl = video_ioctl2, 1772 }; 1773 1774 static const struct v4l2_ioctl_ops video_ioctl_ops = { 1775 .vidioc_querycap = vidioc_querycap, 1776 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1777 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1778 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1779 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1780 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 1781 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 1782 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 1783 .vidioc_enumaudio = vidioc_enumaudio, 1784 .vidioc_g_audio = vidioc_g_audio, 1785 .vidioc_s_audio = vidioc_s_audio, 1786 .vidioc_g_pixelaspect = vidioc_g_pixelaspect, 1787 .vidioc_g_selection = vidioc_g_selection, 1788 1789 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1790 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1791 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1792 .vidioc_querybuf = vb2_ioctl_querybuf, 1793 .vidioc_qbuf = vb2_ioctl_qbuf, 1794 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1795 .vidioc_expbuf = vb2_ioctl_expbuf, 1796 1797 .vidioc_s_std = vidioc_s_std, 1798 .vidioc_g_std = vidioc_g_std, 1799 .vidioc_enum_input = vidioc_enum_input, 1800 .vidioc_g_input = vidioc_g_input, 1801 .vidioc_s_input = vidioc_s_input, 1802 1803 .vidioc_streamon = vb2_ioctl_streamon, 1804 .vidioc_streamoff = vb2_ioctl_streamoff, 1805 1806 .vidioc_g_tuner = vidioc_g_tuner, 1807 .vidioc_s_tuner = vidioc_s_tuner, 1808 .vidioc_g_frequency = vidioc_g_frequency, 1809 .vidioc_s_frequency = vidioc_s_frequency, 1810 #ifdef CONFIG_VIDEO_ADV_DEBUG 1811 .vidioc_g_register = vidioc_g_register, 1812 .vidioc_s_register = vidioc_s_register, 1813 #endif 1814 .vidioc_log_status = vidioc_log_status, 1815 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1816 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1817 }; 1818 1819 static const struct video_device au0828_video_template = { 1820 .fops = &au0828_v4l_fops, 1821 .release = video_device_release_empty, 1822 .ioctl_ops = &video_ioctl_ops, 1823 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M, 1824 }; 1825 1826 static int au0828_vb2_setup(struct au0828_dev *dev) 1827 { 1828 int rc; 1829 struct vb2_queue *q; 1830 1831 /* Setup Videobuf2 for Video capture */ 1832 q = &dev->vb_vidq; 1833 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1834 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1835 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1836 q->drv_priv = dev; 1837 q->buf_struct_size = sizeof(struct au0828_buffer); 1838 q->ops = &au0828_video_qops; 1839 q->mem_ops = &vb2_vmalloc_memops; 1840 1841 rc = vb2_queue_init(q); 1842 if (rc < 0) 1843 return rc; 1844 1845 /* Setup Videobuf2 for VBI capture */ 1846 q = &dev->vb_vbiq; 1847 q->type = V4L2_BUF_TYPE_VBI_CAPTURE; 1848 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1849 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1850 q->drv_priv = dev; 1851 q->buf_struct_size = sizeof(struct au0828_buffer); 1852 q->ops = &au0828_vbi_qops; 1853 q->mem_ops = &vb2_vmalloc_memops; 1854 1855 rc = vb2_queue_init(q); 1856 if (rc < 0) 1857 return rc; 1858 1859 return 0; 1860 } 1861 1862 static void au0828_analog_create_entities(struct au0828_dev *dev) 1863 { 1864 #if defined(CONFIG_MEDIA_CONTROLLER) 1865 static const char * const inames[] = { 1866 [AU0828_VMUX_COMPOSITE] = "Composite", 1867 [AU0828_VMUX_SVIDEO] = "S-Video", 1868 [AU0828_VMUX_CABLE] = "Cable TV", 1869 [AU0828_VMUX_TELEVISION] = "Television", 1870 [AU0828_VMUX_DVB] = "DVB", 1871 }; 1872 int ret, i; 1873 1874 /* Initialize Video and VBI pads */ 1875 dev->video_pad.flags = MEDIA_PAD_FL_SINK; 1876 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad); 1877 if (ret < 0) 1878 pr_err("failed to initialize video media entity!\n"); 1879 1880 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK; 1881 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad); 1882 if (ret < 0) 1883 pr_err("failed to initialize vbi media entity!\n"); 1884 1885 /* Create entities for each input connector */ 1886 for (i = 0; i < AU0828_MAX_INPUT; i++) { 1887 struct media_entity *ent = &dev->input_ent[i]; 1888 1889 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED) 1890 break; 1891 1892 ent->name = inames[AUVI_INPUT(i).type]; 1893 ent->flags = MEDIA_ENT_FL_CONNECTOR; 1894 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE; 1895 1896 switch (AUVI_INPUT(i).type) { 1897 case AU0828_VMUX_COMPOSITE: 1898 ent->function = MEDIA_ENT_F_CONN_COMPOSITE; 1899 break; 1900 case AU0828_VMUX_SVIDEO: 1901 ent->function = MEDIA_ENT_F_CONN_SVIDEO; 1902 break; 1903 case AU0828_VMUX_CABLE: 1904 case AU0828_VMUX_TELEVISION: 1905 case AU0828_VMUX_DVB: 1906 default: /* Just to shut up a warning */ 1907 ent->function = MEDIA_ENT_F_CONN_RF; 1908 break; 1909 } 1910 1911 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]); 1912 if (ret < 0) 1913 pr_err("failed to initialize input pad[%d]!\n", i); 1914 1915 ret = media_device_register_entity(dev->media_dev, ent); 1916 if (ret < 0) 1917 pr_err("failed to register input entity %d!\n", i); 1918 } 1919 #endif 1920 } 1921 1922 /**************************************************************************/ 1923 1924 int au0828_analog_register(struct au0828_dev *dev, 1925 struct usb_interface *interface) 1926 { 1927 int retval = -ENOMEM; 1928 struct usb_host_interface *iface_desc; 1929 struct usb_endpoint_descriptor *endpoint; 1930 int i, ret; 1931 1932 dprintk(1, "au0828_analog_register called for intf#%d!\n", 1933 interface->cur_altsetting->desc.bInterfaceNumber); 1934 1935 /* No analog TV */ 1936 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED) 1937 return 0; 1938 1939 /* set au0828 usb interface0 to as5 */ 1940 retval = usb_set_interface(dev->usbdev, 1941 interface->cur_altsetting->desc.bInterfaceNumber, 5); 1942 if (retval != 0) { 1943 pr_info("Failure setting usb interface0 to as5\n"); 1944 return retval; 1945 } 1946 1947 /* Figure out which endpoint has the isoc interface */ 1948 iface_desc = interface->cur_altsetting; 1949 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { 1950 endpoint = &iface_desc->endpoint[i].desc; 1951 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) 1952 == USB_DIR_IN) && 1953 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 1954 == USB_ENDPOINT_XFER_ISOC)) { 1955 1956 /* we find our isoc in endpoint */ 1957 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize); 1958 dev->max_pkt_size = (tmp & 0x07ff) * 1959 (((tmp & 0x1800) >> 11) + 1); 1960 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress; 1961 dprintk(1, 1962 "Found isoc endpoint 0x%02x, max size = %d\n", 1963 dev->isoc_in_endpointaddr, dev->max_pkt_size); 1964 } 1965 } 1966 if (!(dev->isoc_in_endpointaddr)) { 1967 pr_info("Could not locate isoc endpoint\n"); 1968 return -ENODEV; 1969 } 1970 1971 init_waitqueue_head(&dev->open); 1972 spin_lock_init(&dev->slock); 1973 1974 /* init video dma queues */ 1975 INIT_LIST_HEAD(&dev->vidq.active); 1976 INIT_LIST_HEAD(&dev->vbiq.active); 1977 1978 timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0); 1979 timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0); 1980 1981 dev->width = NTSC_STD_W; 1982 dev->height = NTSC_STD_H; 1983 dev->field_size = dev->width * dev->height; 1984 dev->frame_size = dev->field_size << 1; 1985 dev->bytesperline = dev->width << 1; 1986 dev->vbi_width = 720; 1987 dev->vbi_height = 1; 1988 dev->ctrl_ainput = 0; 1989 dev->ctrl_freq = 960; 1990 dev->std = V4L2_STD_NTSC_M; 1991 /* Default input is TV Tuner */ 1992 au0828_s_input(dev, 0); 1993 1994 mutex_init(&dev->vb_queue_lock); 1995 mutex_init(&dev->vb_vbi_queue_lock); 1996 1997 /* Fill the video capture device struct */ 1998 dev->vdev = au0828_video_template; 1999 dev->vdev.v4l2_dev = &dev->v4l2_dev; 2000 dev->vdev.lock = &dev->lock; 2001 dev->vdev.queue = &dev->vb_vidq; 2002 dev->vdev.queue->lock = &dev->vb_queue_lock; 2003 strscpy(dev->vdev.name, "au0828a video", sizeof(dev->vdev.name)); 2004 2005 /* Setup the VBI device */ 2006 dev->vbi_dev = au0828_video_template; 2007 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev; 2008 dev->vbi_dev.lock = &dev->lock; 2009 dev->vbi_dev.queue = &dev->vb_vbiq; 2010 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock; 2011 strscpy(dev->vbi_dev.name, "au0828a vbi", sizeof(dev->vbi_dev.name)); 2012 2013 /* Init entities at the Media Controller */ 2014 au0828_analog_create_entities(dev); 2015 2016 /* initialize videobuf2 stuff */ 2017 retval = au0828_vb2_setup(dev); 2018 if (retval != 0) { 2019 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n", 2020 retval); 2021 return -ENODEV; 2022 } 2023 2024 /* Register the v4l2 device */ 2025 video_set_drvdata(&dev->vdev, dev); 2026 retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); 2027 if (retval != 0) { 2028 dprintk(1, "unable to register video device (error = %d).\n", 2029 retval); 2030 ret = -ENODEV; 2031 goto err_reg_vdev; 2032 } 2033 2034 /* Register the vbi device */ 2035 video_set_drvdata(&dev->vbi_dev, dev); 2036 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1); 2037 if (retval != 0) { 2038 dprintk(1, "unable to register vbi device (error = %d).\n", 2039 retval); 2040 ret = -ENODEV; 2041 goto err_reg_vbi_dev; 2042 } 2043 2044 #ifdef CONFIG_MEDIA_CONTROLLER 2045 retval = v4l2_mc_create_media_graph(dev->media_dev); 2046 if (retval) { 2047 pr_err("%s() au0282_dev_register failed to create graph\n", 2048 __func__); 2049 ret = -ENODEV; 2050 goto err_reg_vbi_dev; 2051 } 2052 #endif 2053 2054 dprintk(1, "%s completed!\n", __func__); 2055 2056 return 0; 2057 2058 err_reg_vbi_dev: 2059 video_unregister_device(&dev->vdev); 2060 err_reg_vdev: 2061 vb2_queue_release(&dev->vb_vidq); 2062 vb2_queue_release(&dev->vb_vbiq); 2063 return ret; 2064 } 2065 2066