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