1/* 2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB 3 video capture devices 4 5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> 6 Markus Rechberger <mrechberger@gmail.com> 7 Mauro Carvalho Chehab <mchehab@infradead.org> 8 Sascha Sommer <saschasommer@freenet.de> 9 10 Some parts based on SN9C10x PC Camera Controllers GPL driver made 11 by Luca Risolia <luca.risolia@studio.unibo.it> 12 13 This program is free software; you can redistribute it and/or modify 14 it under the terms of the GNU General Public License as published by 15 the Free Software Foundation; either version 2 of the License, or 16 (at your option) any later version. 17 18 This program is distributed in the hope that it will be useful, 19 but WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 GNU General Public License for more details. 22 23 You should have received a copy of the GNU General Public License 24 along with this program; if not, write to the Free Software 25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 26 */ 27 28#include <linux/init.h> 29#include <linux/list.h> 30#include <linux/module.h> 31#include <linux/kernel.h> 32#include <linux/bitmap.h> 33#include <linux/usb.h> 34#include <linux/i2c.h> 35#include <linux/version.h> 36#include <linux/mm.h> 37#include <linux/mutex.h> 38#include <linux/slab.h> 39 40#include "em28xx.h" 41#include <media/v4l2-common.h> 42#include <media/v4l2-ioctl.h> 43#include <media/v4l2-chip-ident.h> 44#include <media/msp3400.h> 45#include <media/tuner.h> 46 47#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ 48 "Markus Rechberger <mrechberger@gmail.com>, " \ 49 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \ 50 "Sascha Sommer <saschasommer@freenet.de>" 51 52#define DRIVER_DESC "Empia em28xx based USB video device driver" 53#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2) 54 55#define em28xx_videodbg(fmt, arg...) do {\ 56 if (video_debug) \ 57 printk(KERN_INFO "%s %s :"fmt, \ 58 dev->name, __func__ , ##arg); } while (0) 59 60static unsigned int isoc_debug; 61module_param(isoc_debug, int, 0644); 62MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); 63 64#define em28xx_isocdbg(fmt, arg...) \ 65do {\ 66 if (isoc_debug) { \ 67 printk(KERN_INFO "%s %s :"fmt, \ 68 dev->name, __func__ , ##arg); \ 69 } \ 70 } while (0) 71 72MODULE_AUTHOR(DRIVER_AUTHOR); 73MODULE_DESCRIPTION(DRIVER_DESC); 74MODULE_LICENSE("GPL"); 75 76static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 77static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 78static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 79 80module_param_array(video_nr, int, NULL, 0444); 81module_param_array(vbi_nr, int, NULL, 0444); 82module_param_array(radio_nr, int, NULL, 0444); 83MODULE_PARM_DESC(video_nr, "video device numbers"); 84MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); 85MODULE_PARM_DESC(radio_nr, "radio device numbers"); 86 87static unsigned int video_debug; 88module_param(video_debug, int, 0644); 89MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); 90 91/* supported video standards */ 92static struct em28xx_fmt format[] = { 93 { 94 .name = "16 bpp YUY2, 4:2:2, packed", 95 .fourcc = V4L2_PIX_FMT_YUYV, 96 .depth = 16, 97 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V, 98 }, { 99 .name = "16 bpp RGB 565, LE", 100 .fourcc = V4L2_PIX_FMT_RGB565, 101 .depth = 16, 102 .reg = EM28XX_OUTFMT_RGB_16_656, 103 }, { 104 .name = "8 bpp Bayer BGBG..GRGR", 105 .fourcc = V4L2_PIX_FMT_SBGGR8, 106 .depth = 8, 107 .reg = EM28XX_OUTFMT_RGB_8_BGBG, 108 }, { 109 .name = "8 bpp Bayer GRGR..BGBG", 110 .fourcc = V4L2_PIX_FMT_SGRBG8, 111 .depth = 8, 112 .reg = EM28XX_OUTFMT_RGB_8_GRGR, 113 }, { 114 .name = "8 bpp Bayer GBGB..RGRG", 115 .fourcc = V4L2_PIX_FMT_SGBRG8, 116 .depth = 8, 117 .reg = EM28XX_OUTFMT_RGB_8_GBGB, 118 }, { 119 .name = "12 bpp YUV411", 120 .fourcc = V4L2_PIX_FMT_YUV411P, 121 .depth = 12, 122 .reg = EM28XX_OUTFMT_YUV411, 123 }, 124}; 125 126/* supported controls */ 127/* Common to all boards */ 128static struct v4l2_queryctrl ac97_qctrl[] = { 129 { 130 .id = V4L2_CID_AUDIO_VOLUME, 131 .type = V4L2_CTRL_TYPE_INTEGER, 132 .name = "Volume", 133 .minimum = 0x0, 134 .maximum = 0x1f, 135 .step = 0x1, 136 .default_value = 0x1f, 137 .flags = V4L2_CTRL_FLAG_SLIDER, 138 }, { 139 .id = V4L2_CID_AUDIO_MUTE, 140 .type = V4L2_CTRL_TYPE_BOOLEAN, 141 .name = "Mute", 142 .minimum = 0, 143 .maximum = 1, 144 .step = 1, 145 .default_value = 1, 146 .flags = 0, 147 } 148}; 149 150/* ------------------------------------------------------------------ 151 DMA and thread functions 152 ------------------------------------------------------------------*/ 153 154/* 155 * Announces that a buffer were filled and request the next 156 */ 157static inline void buffer_filled(struct em28xx *dev, 158 struct em28xx_dmaqueue *dma_q, 159 struct em28xx_buffer *buf) 160{ 161 /* Advice that buffer was filled */ 162 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); 163 buf->vb.state = VIDEOBUF_DONE; 164 buf->vb.field_count++; 165 do_gettimeofday(&buf->vb.ts); 166 167 dev->isoc_ctl.vid_buf = NULL; 168 169 list_del(&buf->vb.queue); 170 wake_up(&buf->vb.done); 171} 172 173static inline void vbi_buffer_filled(struct em28xx *dev, 174 struct em28xx_dmaqueue *dma_q, 175 struct em28xx_buffer *buf) 176{ 177 /* Advice that buffer was filled */ 178 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); 179 180 buf->vb.state = VIDEOBUF_DONE; 181 buf->vb.field_count++; 182 do_gettimeofday(&buf->vb.ts); 183 184 dev->isoc_ctl.vbi_buf = NULL; 185 186 list_del(&buf->vb.queue); 187 wake_up(&buf->vb.done); 188} 189 190/* 191 * Identify the buffer header type and properly handles 192 */ 193static void em28xx_copy_video(struct em28xx *dev, 194 struct em28xx_dmaqueue *dma_q, 195 struct em28xx_buffer *buf, 196 unsigned char *p, 197 unsigned char *outp, unsigned long len) 198{ 199 void *fieldstart, *startwrite, *startread; 200 int linesdone, currlinedone, offset, lencopy, remain; 201 int bytesperline = dev->width << 1; 202 203 if (dma_q->pos + len > buf->vb.size) 204 len = buf->vb.size - dma_q->pos; 205 206 startread = p; 207 remain = len; 208 209 if (dev->progressive) 210 fieldstart = outp; 211 else { 212 /* Interlaces two half frames */ 213 if (buf->top_field) 214 fieldstart = outp; 215 else 216 fieldstart = outp + bytesperline; 217 } 218 219 linesdone = dma_q->pos / bytesperline; 220 currlinedone = dma_q->pos % bytesperline; 221 222 if (dev->progressive) 223 offset = linesdone * bytesperline + currlinedone; 224 else 225 offset = linesdone * bytesperline * 2 + currlinedone; 226 227 startwrite = fieldstart + offset; 228 lencopy = bytesperline - currlinedone; 229 lencopy = lencopy > remain ? remain : lencopy; 230 231 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { 232 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", 233 ((char *)startwrite + lencopy) - 234 ((char *)outp + buf->vb.size)); 235 remain = (char *)outp + buf->vb.size - (char *)startwrite; 236 lencopy = remain; 237 } 238 if (lencopy <= 0) 239 return; 240 memcpy(startwrite, startread, lencopy); 241 242 remain -= lencopy; 243 244 while (remain > 0) { 245 startwrite += lencopy + bytesperline; 246 startread += lencopy; 247 if (bytesperline > remain) 248 lencopy = remain; 249 else 250 lencopy = bytesperline; 251 252 if ((char *)startwrite + lencopy > (char *)outp + 253 buf->vb.size) { 254 em28xx_isocdbg("Overflow of %zi bytes past buffer end" 255 "(2)\n", 256 ((char *)startwrite + lencopy) - 257 ((char *)outp + buf->vb.size)); 258 lencopy = remain = (char *)outp + buf->vb.size - 259 (char *)startwrite; 260 } 261 if (lencopy <= 0) 262 break; 263 264 memcpy(startwrite, startread, lencopy); 265 266 remain -= lencopy; 267 } 268 269 dma_q->pos += len; 270} 271 272static void em28xx_copy_vbi(struct em28xx *dev, 273 struct em28xx_dmaqueue *dma_q, 274 struct em28xx_buffer *buf, 275 unsigned char *p, 276 unsigned char *outp, unsigned long len) 277{ 278 void *startwrite, *startread; 279 int offset; 280 int bytesperline = dev->vbi_width; 281 282 if (dev == NULL) { 283 em28xx_isocdbg("dev is null\n"); 284 return; 285 } 286 287 if (dma_q == NULL) { 288 em28xx_isocdbg("dma_q is null\n"); 289 return; 290 } 291 if (buf == NULL) { 292 return; 293 } 294 if (p == NULL) { 295 em28xx_isocdbg("p is null\n"); 296 return; 297 } 298 if (outp == NULL) { 299 em28xx_isocdbg("outp is null\n"); 300 return; 301 } 302 303 if (dma_q->pos + len > buf->vb.size) 304 len = buf->vb.size - dma_q->pos; 305 306 startread = p; 307 308 startwrite = outp + dma_q->pos; 309 offset = dma_q->pos; 310 311 /* Make sure the bottom field populates the second half of the frame */ 312 if (buf->top_field == 0) { 313 startwrite += bytesperline * dev->vbi_height; 314 offset += bytesperline * dev->vbi_height; 315 } 316 317 memcpy(startwrite, startread, len); 318 dma_q->pos += len; 319} 320 321static inline void print_err_status(struct em28xx *dev, 322 int packet, int status) 323{ 324 char *errmsg = "Unknown"; 325 326 switch (status) { 327 case -ENOENT: 328 errmsg = "unlinked synchronuously"; 329 break; 330 case -ECONNRESET: 331 errmsg = "unlinked asynchronuously"; 332 break; 333 case -ENOSR: 334 errmsg = "Buffer error (overrun)"; 335 break; 336 case -EPIPE: 337 errmsg = "Stalled (device not responding)"; 338 break; 339 case -EOVERFLOW: 340 errmsg = "Babble (bad cable?)"; 341 break; 342 case -EPROTO: 343 errmsg = "Bit-stuff error (bad cable?)"; 344 break; 345 case -EILSEQ: 346 errmsg = "CRC/Timeout (could be anything)"; 347 break; 348 case -ETIME: 349 errmsg = "Device does not respond"; 350 break; 351 } 352 if (packet < 0) { 353 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg); 354 } else { 355 em28xx_isocdbg("URB packet %d, status %d [%s].\n", 356 packet, status, errmsg); 357 } 358} 359 360/* 361 * video-buf generic routine to get the next available buffer 362 */ 363static inline void get_next_buf(struct em28xx_dmaqueue *dma_q, 364 struct em28xx_buffer **buf) 365{ 366 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); 367 char *outp; 368 369 if (list_empty(&dma_q->active)) { 370 em28xx_isocdbg("No active queue to serve\n"); 371 dev->isoc_ctl.vid_buf = NULL; 372 *buf = NULL; 373 return; 374 } 375 376 /* Get the next buffer */ 377 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); 378 379 /* Cleans up buffer - Usefull for testing for frame/URB loss */ 380 outp = videobuf_to_vmalloc(&(*buf)->vb); 381 memset(outp, 0, (*buf)->vb.size); 382 383 dev->isoc_ctl.vid_buf = *buf; 384 385 return; 386} 387 388/* 389 * video-buf generic routine to get the next available VBI buffer 390 */ 391static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q, 392 struct em28xx_buffer **buf) 393{ 394 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq); 395 char *outp; 396 397 if (list_empty(&dma_q->active)) { 398 em28xx_isocdbg("No active queue to serve\n"); 399 dev->isoc_ctl.vbi_buf = NULL; 400 *buf = NULL; 401 return; 402 } 403 404 /* Get the next buffer */ 405 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); 406 /* Cleans up buffer - Usefull for testing for frame/URB loss */ 407 outp = videobuf_to_vmalloc(&(*buf)->vb); 408 memset(outp, 0x00, (*buf)->vb.size); 409 410 dev->isoc_ctl.vbi_buf = *buf; 411 412 return; 413} 414 415/* 416 * Controls the isoc copy of each urb packet 417 */ 418static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb) 419{ 420 struct em28xx_buffer *buf; 421 struct em28xx_dmaqueue *dma_q = &dev->vidq; 422 unsigned char *outp = NULL; 423 int i, len = 0, rc = 1; 424 unsigned char *p; 425 426 if (!dev) 427 return 0; 428 429 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) 430 return 0; 431 432 if (urb->status < 0) { 433 print_err_status(dev, -1, urb->status); 434 if (urb->status == -ENOENT) 435 return 0; 436 } 437 438 buf = dev->isoc_ctl.vid_buf; 439 if (buf != NULL) 440 outp = videobuf_to_vmalloc(&buf->vb); 441 442 for (i = 0; i < urb->number_of_packets; i++) { 443 int status = urb->iso_frame_desc[i].status; 444 445 if (status < 0) { 446 print_err_status(dev, i, status); 447 if (urb->iso_frame_desc[i].status != -EPROTO) 448 continue; 449 } 450 451 len = urb->iso_frame_desc[i].actual_length - 4; 452 453 if (urb->iso_frame_desc[i].actual_length <= 0) { 454 /* em28xx_isocdbg("packet %d is empty",i); - spammy */ 455 continue; 456 } 457 if (urb->iso_frame_desc[i].actual_length > 458 dev->max_pkt_size) { 459 em28xx_isocdbg("packet bigger than packet size"); 460 continue; 461 } 462 463 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 464 465 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { 466 em28xx_isocdbg("VBI HEADER!!!\n"); 467 continue; 468 } 469 if (p[0] == 0x22 && p[1] == 0x5a) { 470 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2], 471 len, (p[2] & 1) ? "odd" : "even"); 472 473 if (dev->progressive || !(p[2] & 1)) { 474 if (buf != NULL) 475 buffer_filled(dev, dma_q, buf); 476 get_next_buf(dma_q, &buf); 477 if (buf == NULL) 478 outp = NULL; 479 else 480 outp = videobuf_to_vmalloc(&buf->vb); 481 } 482 483 if (buf != NULL) { 484 if (p[2] & 1) 485 buf->top_field = 0; 486 else 487 buf->top_field = 1; 488 } 489 490 dma_q->pos = 0; 491 } 492 if (buf != NULL) { 493 if (p[0] != 0x88 && p[0] != 0x22) { 494 em28xx_isocdbg("frame is not complete\n"); 495 len += 4; 496 } else { 497 p += 4; 498 } 499 em28xx_copy_video(dev, dma_q, buf, p, outp, len); 500 } 501 } 502 return rc; 503} 504 505/* Version of isoc handler that takes into account a mixture of video and 506 VBI data */ 507static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb) 508{ 509 struct em28xx_buffer *buf, *vbi_buf; 510 struct em28xx_dmaqueue *dma_q = &dev->vidq; 511 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq; 512 unsigned char *outp = NULL; 513 unsigned char *vbioutp = NULL; 514 int i, len = 0, rc = 1; 515 unsigned char *p; 516 int vbi_size; 517 518 if (!dev) 519 return 0; 520 521 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) 522 return 0; 523 524 if (urb->status < 0) { 525 print_err_status(dev, -1, urb->status); 526 if (urb->status == -ENOENT) 527 return 0; 528 } 529 530 buf = dev->isoc_ctl.vid_buf; 531 if (buf != NULL) 532 outp = videobuf_to_vmalloc(&buf->vb); 533 534 vbi_buf = dev->isoc_ctl.vbi_buf; 535 if (vbi_buf != NULL) 536 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb); 537 538 for (i = 0; i < urb->number_of_packets; i++) { 539 int status = urb->iso_frame_desc[i].status; 540 541 if (status < 0) { 542 print_err_status(dev, i, status); 543 if (urb->iso_frame_desc[i].status != -EPROTO) 544 continue; 545 } 546 547 len = urb->iso_frame_desc[i].actual_length; 548 if (urb->iso_frame_desc[i].actual_length <= 0) { 549 /* em28xx_isocdbg("packet %d is empty",i); - spammy */ 550 continue; 551 } 552 if (urb->iso_frame_desc[i].actual_length > 553 dev->max_pkt_size) { 554 em28xx_isocdbg("packet bigger than packet size"); 555 continue; 556 } 557 558 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 559 560 /* capture type 0 = vbi start 561 capture type 1 = video start 562 capture type 2 = video in progress */ 563 if (p[0] == 0x33 && p[1] == 0x95) { 564 dev->capture_type = 0; 565 dev->vbi_read = 0; 566 em28xx_isocdbg("VBI START HEADER!!!\n"); 567 dev->cur_field = p[2]; 568 p += 4; 569 len -= 4; 570 } else if (p[0] == 0x88 && p[1] == 0x88 && 571 p[2] == 0x88 && p[3] == 0x88) { 572 /* continuation */ 573 p += 4; 574 len -= 4; 575 } else if (p[0] == 0x22 && p[1] == 0x5a) { 576 /* start video */ 577 p += 4; 578 len -= 4; 579 } 580 581 vbi_size = dev->vbi_width * dev->vbi_height; 582 583 if (dev->capture_type == 0) { 584 if (dev->vbi_read >= vbi_size) { 585 /* We've already read all the VBI data, so 586 treat the rest as video */ 587 em28xx_isocdbg("dev->vbi_read > vbi_size\n"); 588 } else if ((dev->vbi_read + len) < vbi_size) { 589 /* This entire frame is VBI data */ 590 if (dev->vbi_read == 0 && 591 (!(dev->cur_field & 1))) { 592 /* Brand new frame */ 593 if (vbi_buf != NULL) 594 vbi_buffer_filled(dev, 595 vbi_dma_q, 596 vbi_buf); 597 vbi_get_next_buf(vbi_dma_q, &vbi_buf); 598 if (vbi_buf == NULL) 599 vbioutp = NULL; 600 else 601 vbioutp = videobuf_to_vmalloc( 602 &vbi_buf->vb); 603 } 604 605 if (dev->vbi_read == 0) { 606 vbi_dma_q->pos = 0; 607 if (vbi_buf != NULL) { 608 if (dev->cur_field & 1) 609 vbi_buf->top_field = 0; 610 else 611 vbi_buf->top_field = 1; 612 } 613 } 614 615 dev->vbi_read += len; 616 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p, 617 vbioutp, len); 618 } else { 619 /* Some of this frame is VBI data and some is 620 video data */ 621 int vbi_data_len = vbi_size - dev->vbi_read; 622 dev->vbi_read += vbi_data_len; 623 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p, 624 vbioutp, vbi_data_len); 625 dev->capture_type = 1; 626 p += vbi_data_len; 627 len -= vbi_data_len; 628 } 629 } 630 631 if (dev->capture_type == 1) { 632 dev->capture_type = 2; 633 if (dev->progressive || !(dev->cur_field & 1)) { 634 if (buf != NULL) 635 buffer_filled(dev, dma_q, buf); 636 get_next_buf(dma_q, &buf); 637 if (buf == NULL) 638 outp = NULL; 639 else 640 outp = videobuf_to_vmalloc(&buf->vb); 641 } 642 if (buf != NULL) { 643 if (dev->cur_field & 1) 644 buf->top_field = 0; 645 else 646 buf->top_field = 1; 647 } 648 649 dma_q->pos = 0; 650 } 651 652 if (buf != NULL && dev->capture_type == 2) { 653 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 && 654 p[2] == 0x88 && p[3] == 0x88) { 655 p += 4; 656 len -= 4; 657 } 658 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) { 659 em28xx_isocdbg("Video frame %d, len=%i, %s\n", 660 p[2], len, (p[2] & 1) ? 661 "odd" : "even"); 662 p += 4; 663 len -= 4; 664 } 665 666 if (len > 0) 667 em28xx_copy_video(dev, dma_q, buf, p, outp, 668 len); 669 } 670 } 671 return rc; 672} 673 674 675/* ------------------------------------------------------------------ 676 Videobuf operations 677 ------------------------------------------------------------------*/ 678 679static int 680buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) 681{ 682 struct em28xx_fh *fh = vq->priv_data; 683 struct em28xx *dev = fh->dev; 684 struct v4l2_frequency f; 685 686 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) 687 >> 3; 688 689 if (0 == *count) 690 *count = EM28XX_DEF_BUF; 691 692 if (*count < EM28XX_MIN_BUF) 693 *count = EM28XX_MIN_BUF; 694 695 /* Ask tuner to go to analog or radio mode */ 696 memset(&f, 0, sizeof(f)); 697 f.frequency = dev->ctl_freq; 698 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 699 700 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f); 701 702 return 0; 703} 704 705/* This is called *without* dev->slock held; please keep it that way */ 706static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) 707{ 708 struct em28xx_fh *fh = vq->priv_data; 709 struct em28xx *dev = fh->dev; 710 unsigned long flags = 0; 711 if (in_interrupt()) 712 BUG(); 713 714 /* We used to wait for the buffer to finish here, but this didn't work 715 because, as we were keeping the state as VIDEOBUF_QUEUED, 716 videobuf_queue_cancel marked it as finished for us. 717 (Also, it could wedge forever if the hardware was misconfigured.) 718 719 This should be safe; by the time we get here, the buffer isn't 720 queued anymore. If we ever start marking the buffers as 721 VIDEOBUF_ACTIVE, it won't be, though. 722 */ 723 spin_lock_irqsave(&dev->slock, flags); 724 if (dev->isoc_ctl.vid_buf == buf) 725 dev->isoc_ctl.vid_buf = NULL; 726 spin_unlock_irqrestore(&dev->slock, flags); 727 728 videobuf_vmalloc_free(&buf->vb); 729 buf->vb.state = VIDEOBUF_NEEDS_INIT; 730} 731 732static int 733buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, 734 enum v4l2_field field) 735{ 736 struct em28xx_fh *fh = vq->priv_data; 737 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 738 struct em28xx *dev = fh->dev; 739 int rc = 0, urb_init = 0; 740 741 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth 742 + 7) >> 3; 743 744 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 745 return -EINVAL; 746 747 buf->vb.width = dev->width; 748 buf->vb.height = dev->height; 749 buf->vb.field = field; 750 751 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 752 rc = videobuf_iolock(vq, &buf->vb, NULL); 753 if (rc < 0) 754 goto fail; 755 } 756 757 if (!dev->isoc_ctl.num_bufs) 758 urb_init = 1; 759 760 if (urb_init) { 761 if (em28xx_vbi_supported(dev) == 1) 762 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS, 763 EM28XX_NUM_BUFS, 764 dev->max_pkt_size, 765 em28xx_isoc_copy_vbi); 766 else 767 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS, 768 EM28XX_NUM_BUFS, 769 dev->max_pkt_size, 770 em28xx_isoc_copy); 771 if (rc < 0) 772 goto fail; 773 } 774 775 buf->vb.state = VIDEOBUF_PREPARED; 776 return 0; 777 778fail: 779 free_buffer(vq, buf); 780 return rc; 781} 782 783static void 784buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 785{ 786 struct em28xx_buffer *buf = container_of(vb, 787 struct em28xx_buffer, 788 vb); 789 struct em28xx_fh *fh = vq->priv_data; 790 struct em28xx *dev = fh->dev; 791 struct em28xx_dmaqueue *vidq = &dev->vidq; 792 793 buf->vb.state = VIDEOBUF_QUEUED; 794 list_add_tail(&buf->vb.queue, &vidq->active); 795 796} 797 798static void buffer_release(struct videobuf_queue *vq, 799 struct videobuf_buffer *vb) 800{ 801 struct em28xx_buffer *buf = container_of(vb, 802 struct em28xx_buffer, 803 vb); 804 struct em28xx_fh *fh = vq->priv_data; 805 struct em28xx *dev = (struct em28xx *)fh->dev; 806 807 em28xx_isocdbg("em28xx: called buffer_release\n"); 808 809 free_buffer(vq, buf); 810} 811 812static struct videobuf_queue_ops em28xx_video_qops = { 813 .buf_setup = buffer_setup, 814 .buf_prepare = buffer_prepare, 815 .buf_queue = buffer_queue, 816 .buf_release = buffer_release, 817}; 818 819/********************* v4l2 interface **************************************/ 820 821static void video_mux(struct em28xx *dev, int index) 822{ 823 dev->ctl_input = index; 824 dev->ctl_ainput = INPUT(index)->amux; 825 dev->ctl_aoutput = INPUT(index)->aout; 826 827 if (!dev->ctl_aoutput) 828 dev->ctl_aoutput = EM28XX_AOUT_MASTER; 829 830 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing, 831 INPUT(index)->vmux, 0, 0); 832 833 if (dev->board.has_msp34xx) { 834 if (dev->i2s_speed) { 835 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, 836 s_i2s_clock_freq, dev->i2s_speed); 837 } 838 /* Note: this is msp3400 specific */ 839 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing, 840 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0); 841 } 842 843 if (dev->board.adecoder != EM28XX_NOADECODER) { 844 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing, 845 dev->ctl_ainput, dev->ctl_aoutput, 0); 846 } 847 848 em28xx_audio_analog_set(dev); 849} 850 851/* Usage lock check functions */ 852static int res_get(struct em28xx_fh *fh, unsigned int bit) 853{ 854 struct em28xx *dev = fh->dev; 855 856 if (fh->resources & bit) 857 /* have it already allocated */ 858 return 1; 859 860 /* is it free? */ 861 mutex_lock(&dev->lock); 862 if (dev->resources & bit) { 863 /* no, someone else uses it */ 864 mutex_unlock(&dev->lock); 865 return 0; 866 } 867 /* it's free, grab it */ 868 fh->resources |= bit; 869 dev->resources |= bit; 870 em28xx_videodbg("res: get %d\n", bit); 871 mutex_unlock(&dev->lock); 872 return 1; 873} 874 875static int res_check(struct em28xx_fh *fh, unsigned int bit) 876{ 877 return fh->resources & bit; 878} 879 880static int res_locked(struct em28xx *dev, unsigned int bit) 881{ 882 return dev->resources & bit; 883} 884 885static void res_free(struct em28xx_fh *fh, unsigned int bits) 886{ 887 struct em28xx *dev = fh->dev; 888 889 BUG_ON((fh->resources & bits) != bits); 890 891 mutex_lock(&dev->lock); 892 fh->resources &= ~bits; 893 dev->resources &= ~bits; 894 em28xx_videodbg("res: put %d\n", bits); 895 mutex_unlock(&dev->lock); 896} 897 898static int get_ressource(struct em28xx_fh *fh) 899{ 900 switch (fh->type) { 901 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 902 return EM28XX_RESOURCE_VIDEO; 903 case V4L2_BUF_TYPE_VBI_CAPTURE: 904 return EM28XX_RESOURCE_VBI; 905 default: 906 BUG(); 907 return 0; 908 } 909} 910 911/* 912 * ac97_queryctrl() 913 * return the ac97 supported controls 914 */ 915static int ac97_queryctrl(struct v4l2_queryctrl *qc) 916{ 917 int i; 918 919 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) { 920 if (qc->id && qc->id == ac97_qctrl[i].id) { 921 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc)); 922 return 0; 923 } 924 } 925 926 /* Control is not ac97 related */ 927 return 1; 928} 929 930/* 931 * ac97_get_ctrl() 932 * return the current values for ac97 mute and volume 933 */ 934static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl) 935{ 936 switch (ctrl->id) { 937 case V4L2_CID_AUDIO_MUTE: 938 ctrl->value = dev->mute; 939 return 0; 940 case V4L2_CID_AUDIO_VOLUME: 941 ctrl->value = dev->volume; 942 return 0; 943 default: 944 /* Control is not ac97 related */ 945 return 1; 946 } 947} 948 949/* 950 * ac97_set_ctrl() 951 * set values for ac97 mute and volume 952 */ 953static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl) 954{ 955 int i; 956 957 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) 958 if (ctrl->id == ac97_qctrl[i].id) 959 goto handle; 960 961 /* Announce that hasn't handle it */ 962 return 1; 963 964handle: 965 if (ctrl->value < ac97_qctrl[i].minimum || 966 ctrl->value > ac97_qctrl[i].maximum) 967 return -ERANGE; 968 969 switch (ctrl->id) { 970 case V4L2_CID_AUDIO_MUTE: 971 dev->mute = ctrl->value; 972 break; 973 case V4L2_CID_AUDIO_VOLUME: 974 dev->volume = ctrl->value; 975 break; 976 } 977 978 return em28xx_audio_analog_set(dev); 979} 980 981static int check_dev(struct em28xx *dev) 982{ 983 if (dev->state & DEV_DISCONNECTED) { 984 em28xx_errdev("v4l2 ioctl: device not present\n"); 985 return -ENODEV; 986 } 987 988 if (dev->state & DEV_MISCONFIGURED) { 989 em28xx_errdev("v4l2 ioctl: device is misconfigured; " 990 "close and open it again\n"); 991 return -EIO; 992 } 993 return 0; 994} 995 996static void get_scale(struct em28xx *dev, 997 unsigned int width, unsigned int height, 998 unsigned int *hscale, unsigned int *vscale) 999{ 1000 unsigned int maxw = norm_maxw(dev); 1001 unsigned int maxh = norm_maxh(dev); 1002 1003 *hscale = (((unsigned long)maxw) << 12) / width - 4096L; 1004 if (*hscale >= 0x4000) 1005 *hscale = 0x3fff; 1006 1007 *vscale = (((unsigned long)maxh) << 12) / height - 4096L; 1008 if (*vscale >= 0x4000) 1009 *vscale = 0x3fff; 1010} 1011 1012/* ------------------------------------------------------------------ 1013 IOCTL vidioc handling 1014 ------------------------------------------------------------------*/ 1015 1016static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 1017 struct v4l2_format *f) 1018{ 1019 struct em28xx_fh *fh = priv; 1020 struct em28xx *dev = fh->dev; 1021 1022 mutex_lock(&dev->lock); 1023 1024 f->fmt.pix.width = dev->width; 1025 f->fmt.pix.height = dev->height; 1026 f->fmt.pix.pixelformat = dev->format->fourcc; 1027 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3; 1028 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height; 1029 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 1030 1031 if (dev->progressive) 1032 f->fmt.pix.field = V4L2_FIELD_NONE; 1033 else 1034 f->fmt.pix.field = dev->interlaced ? 1035 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; 1036 1037 mutex_unlock(&dev->lock); 1038 return 0; 1039} 1040 1041static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc) 1042{ 1043 unsigned int i; 1044 1045 for (i = 0; i < ARRAY_SIZE(format); i++) 1046 if (format[i].fourcc == fourcc) 1047 return &format[i]; 1048 1049 return NULL; 1050} 1051 1052static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 1053 struct v4l2_format *f) 1054{ 1055 struct em28xx_fh *fh = priv; 1056 struct em28xx *dev = fh->dev; 1057 unsigned int width = f->fmt.pix.width; 1058 unsigned int height = f->fmt.pix.height; 1059 unsigned int maxw = norm_maxw(dev); 1060 unsigned int maxh = norm_maxh(dev); 1061 unsigned int hscale, vscale; 1062 struct em28xx_fmt *fmt; 1063 1064 fmt = format_by_fourcc(f->fmt.pix.pixelformat); 1065 if (!fmt) { 1066 em28xx_videodbg("Fourcc format (%08x) invalid.\n", 1067 f->fmt.pix.pixelformat); 1068 return -EINVAL; 1069 } 1070 1071 if (dev->board.is_em2800) { 1072 /* the em2800 can only scale down to 50% */ 1073 height = height > (3 * maxh / 4) ? maxh : maxh / 2; 1074 width = width > (3 * maxw / 4) ? maxw : maxw / 2; 1075 } else { 1076 /* width must even because of the YUYV format 1077 height must be even because of interlacing */ 1078 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1079 1, 0); 1080 } 1081 1082 get_scale(dev, width, height, &hscale, &vscale); 1083 1084 width = (((unsigned long)maxw) << 12) / (hscale + 4096L); 1085 height = (((unsigned long)maxh) << 12) / (vscale + 4096L); 1086 1087 f->fmt.pix.width = width; 1088 f->fmt.pix.height = height; 1089 f->fmt.pix.pixelformat = fmt->fourcc; 1090 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3; 1091 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height; 1092 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 1093 if (dev->progressive) 1094 f->fmt.pix.field = V4L2_FIELD_NONE; 1095 else 1096 f->fmt.pix.field = dev->interlaced ? 1097 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; 1098 1099 return 0; 1100} 1101 1102static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc, 1103 unsigned width, unsigned height) 1104{ 1105 struct em28xx_fmt *fmt; 1106 1107 fmt = format_by_fourcc(fourcc); 1108 if (!fmt) 1109 return -EINVAL; 1110 1111 dev->format = fmt; 1112 dev->width = width; 1113 dev->height = height; 1114 1115 /* set new image size */ 1116 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 1117 1118 em28xx_set_alternate(dev); 1119 em28xx_resolution_set(dev); 1120 1121 return 0; 1122} 1123 1124static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 1125 struct v4l2_format *f) 1126{ 1127 struct em28xx_fh *fh = priv; 1128 struct em28xx *dev = fh->dev; 1129 int rc; 1130 1131 rc = check_dev(dev); 1132 if (rc < 0) 1133 return rc; 1134 1135 mutex_lock(&dev->lock); 1136 1137 vidioc_try_fmt_vid_cap(file, priv, f); 1138 1139 if (videobuf_queue_is_busy(&fh->vb_vidq)) { 1140 em28xx_errdev("%s queue busy\n", __func__); 1141 rc = -EBUSY; 1142 goto out; 1143 } 1144 1145 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat, 1146 f->fmt.pix.width, f->fmt.pix.height); 1147 1148out: 1149 mutex_unlock(&dev->lock); 1150 return rc; 1151} 1152 1153static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) 1154{ 1155 struct em28xx_fh *fh = priv; 1156 struct em28xx *dev = fh->dev; 1157 int rc; 1158 1159 rc = check_dev(dev); 1160 if (rc < 0) 1161 return rc; 1162 1163 *norm = dev->norm; 1164 1165 return 0; 1166} 1167 1168static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) 1169{ 1170 struct em28xx_fh *fh = priv; 1171 struct em28xx *dev = fh->dev; 1172 struct v4l2_format f; 1173 int rc; 1174 1175 rc = check_dev(dev); 1176 if (rc < 0) 1177 return rc; 1178 1179 mutex_lock(&dev->lock); 1180 dev->norm = *norm; 1181 1182 /* Adjusts width/height, if needed */ 1183 f.fmt.pix.width = dev->width; 1184 f.fmt.pix.height = dev->height; 1185 vidioc_try_fmt_vid_cap(file, priv, &f); 1186 1187 /* set new image size */ 1188 dev->width = f.fmt.pix.width; 1189 dev->height = f.fmt.pix.height; 1190 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 1191 1192 em28xx_resolution_set(dev); 1193 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm); 1194 1195 mutex_unlock(&dev->lock); 1196 return 0; 1197} 1198 1199static int vidioc_g_parm(struct file *file, void *priv, 1200 struct v4l2_streamparm *p) 1201{ 1202 struct em28xx_fh *fh = priv; 1203 struct em28xx *dev = fh->dev; 1204 int rc = 0; 1205 1206 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1207 return -EINVAL; 1208 1209 if (dev->board.is_webcam) 1210 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0, 1211 video, g_parm, p); 1212 else 1213 v4l2_video_std_frame_period(dev->norm, 1214 &p->parm.capture.timeperframe); 1215 1216 return rc; 1217} 1218 1219static int vidioc_s_parm(struct file *file, void *priv, 1220 struct v4l2_streamparm *p) 1221{ 1222 struct em28xx_fh *fh = priv; 1223 struct em28xx *dev = fh->dev; 1224 1225 if (!dev->board.is_webcam) 1226 return -EINVAL; 1227 1228 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1229 return -EINVAL; 1230 1231 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p); 1232} 1233 1234static const char *iname[] = { 1235 [EM28XX_VMUX_COMPOSITE1] = "Composite1", 1236 [EM28XX_VMUX_COMPOSITE2] = "Composite2", 1237 [EM28XX_VMUX_COMPOSITE3] = "Composite3", 1238 [EM28XX_VMUX_COMPOSITE4] = "Composite4", 1239 [EM28XX_VMUX_SVIDEO] = "S-Video", 1240 [EM28XX_VMUX_TELEVISION] = "Television", 1241 [EM28XX_VMUX_CABLE] = "Cable TV", 1242 [EM28XX_VMUX_DVB] = "DVB", 1243 [EM28XX_VMUX_DEBUG] = "for debug only", 1244}; 1245 1246static int vidioc_enum_input(struct file *file, void *priv, 1247 struct v4l2_input *i) 1248{ 1249 struct em28xx_fh *fh = priv; 1250 struct em28xx *dev = fh->dev; 1251 unsigned int n; 1252 1253 n = i->index; 1254 if (n >= MAX_EM28XX_INPUT) 1255 return -EINVAL; 1256 if (0 == INPUT(n)->type) 1257 return -EINVAL; 1258 1259 i->index = n; 1260 i->type = V4L2_INPUT_TYPE_CAMERA; 1261 1262 strcpy(i->name, iname[INPUT(n)->type]); 1263 1264 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) || 1265 (EM28XX_VMUX_CABLE == INPUT(n)->type)) 1266 i->type = V4L2_INPUT_TYPE_TUNER; 1267 1268 i->std = dev->vdev->tvnorms; 1269 1270 return 0; 1271} 1272 1273static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 1274{ 1275 struct em28xx_fh *fh = priv; 1276 struct em28xx *dev = fh->dev; 1277 1278 *i = dev->ctl_input; 1279 1280 return 0; 1281} 1282 1283static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 1284{ 1285 struct em28xx_fh *fh = priv; 1286 struct em28xx *dev = fh->dev; 1287 int rc; 1288 1289 rc = check_dev(dev); 1290 if (rc < 0) 1291 return rc; 1292 1293 if (i >= MAX_EM28XX_INPUT) 1294 return -EINVAL; 1295 if (0 == INPUT(i)->type) 1296 return -EINVAL; 1297 1298 dev->ctl_input = i; 1299 1300 mutex_lock(&dev->lock); 1301 video_mux(dev, dev->ctl_input); 1302 mutex_unlock(&dev->lock); 1303 return 0; 1304} 1305 1306static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) 1307{ 1308 struct em28xx_fh *fh = priv; 1309 struct em28xx *dev = fh->dev; 1310 1311 if (!dev->audio_mode.has_audio) 1312 return -EINVAL; 1313 1314 switch (a->index) { 1315 case EM28XX_AMUX_VIDEO: 1316 strcpy(a->name, "Television"); 1317 break; 1318 case EM28XX_AMUX_LINE_IN: 1319 strcpy(a->name, "Line In"); 1320 break; 1321 case EM28XX_AMUX_VIDEO2: 1322 strcpy(a->name, "Television alt"); 1323 break; 1324 case EM28XX_AMUX_PHONE: 1325 strcpy(a->name, "Phone"); 1326 break; 1327 case EM28XX_AMUX_MIC: 1328 strcpy(a->name, "Mic"); 1329 break; 1330 case EM28XX_AMUX_CD: 1331 strcpy(a->name, "CD"); 1332 break; 1333 case EM28XX_AMUX_AUX: 1334 strcpy(a->name, "Aux"); 1335 break; 1336 case EM28XX_AMUX_PCM_OUT: 1337 strcpy(a->name, "PCM"); 1338 break; 1339 default: 1340 return -EINVAL; 1341 } 1342 1343 a->index = dev->ctl_ainput; 1344 a->capability = V4L2_AUDCAP_STEREO; 1345 1346 return 0; 1347} 1348 1349static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a) 1350{ 1351 struct em28xx_fh *fh = priv; 1352 struct em28xx *dev = fh->dev; 1353 1354 1355 if (!dev->audio_mode.has_audio) 1356 return -EINVAL; 1357 1358 if (a->index >= MAX_EM28XX_INPUT) 1359 return -EINVAL; 1360 if (0 == INPUT(a->index)->type) 1361 return -EINVAL; 1362 1363 mutex_lock(&dev->lock); 1364 1365 dev->ctl_ainput = INPUT(a->index)->amux; 1366 dev->ctl_aoutput = INPUT(a->index)->aout; 1367 1368 if (!dev->ctl_aoutput) 1369 dev->ctl_aoutput = EM28XX_AOUT_MASTER; 1370 1371 mutex_unlock(&dev->lock); 1372 return 0; 1373} 1374 1375static int vidioc_queryctrl(struct file *file, void *priv, 1376 struct v4l2_queryctrl *qc) 1377{ 1378 struct em28xx_fh *fh = priv; 1379 struct em28xx *dev = fh->dev; 1380 int id = qc->id; 1381 int rc; 1382 1383 rc = check_dev(dev); 1384 if (rc < 0) 1385 return rc; 1386 1387 memset(qc, 0, sizeof(*qc)); 1388 1389 qc->id = id; 1390 1391 /* enumberate AC97 controls */ 1392 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) { 1393 rc = ac97_queryctrl(qc); 1394 if (!rc) 1395 return 0; 1396 } 1397 1398 /* enumberate V4L2 device controls */ 1399 mutex_lock(&dev->lock); 1400 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc); 1401 mutex_unlock(&dev->lock); 1402 1403 if (qc->type) 1404 return 0; 1405 else 1406 return -EINVAL; 1407} 1408 1409static int vidioc_g_ctrl(struct file *file, void *priv, 1410 struct v4l2_control *ctrl) 1411{ 1412 struct em28xx_fh *fh = priv; 1413 struct em28xx *dev = fh->dev; 1414 int rc; 1415 1416 rc = check_dev(dev); 1417 if (rc < 0) 1418 return rc; 1419 rc = 0; 1420 1421 mutex_lock(&dev->lock); 1422 1423 /* Set an AC97 control */ 1424 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) 1425 rc = ac97_get_ctrl(dev, ctrl); 1426 else 1427 rc = 1; 1428 1429 /* It were not an AC97 control. Sends it to the v4l2 dev interface */ 1430 if (rc == 1) { 1431 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl); 1432 rc = 0; 1433 } 1434 1435 mutex_unlock(&dev->lock); 1436 return rc; 1437} 1438 1439static int vidioc_s_ctrl(struct file *file, void *priv, 1440 struct v4l2_control *ctrl) 1441{ 1442 struct em28xx_fh *fh = priv; 1443 struct em28xx *dev = fh->dev; 1444 int rc; 1445 1446 rc = check_dev(dev); 1447 if (rc < 0) 1448 return rc; 1449 1450 mutex_lock(&dev->lock); 1451 1452 /* Set an AC97 control */ 1453 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) 1454 rc = ac97_set_ctrl(dev, ctrl); 1455 else 1456 rc = 1; 1457 1458 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */ 1459 if (rc == 1) { 1460 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl); 1461 1462 /* 1463 * In the case of non-AC97 volume controls, we still need 1464 * to do some setups at em28xx, in order to mute/unmute 1465 * and to adjust audio volume. However, the value ranges 1466 * should be checked by the corresponding V4L subdriver. 1467 */ 1468 switch (ctrl->id) { 1469 case V4L2_CID_AUDIO_MUTE: 1470 dev->mute = ctrl->value; 1471 rc = em28xx_audio_analog_set(dev); 1472 break; 1473 case V4L2_CID_AUDIO_VOLUME: 1474 dev->volume = ctrl->value; 1475 rc = em28xx_audio_analog_set(dev); 1476 } 1477 } 1478 1479 mutex_unlock(&dev->lock); 1480 return rc; 1481} 1482 1483static int vidioc_g_tuner(struct file *file, void *priv, 1484 struct v4l2_tuner *t) 1485{ 1486 struct em28xx_fh *fh = priv; 1487 struct em28xx *dev = fh->dev; 1488 int rc; 1489 1490 rc = check_dev(dev); 1491 if (rc < 0) 1492 return rc; 1493 1494 if (0 != t->index) 1495 return -EINVAL; 1496 1497 strcpy(t->name, "Tuner"); 1498 t->type = V4L2_TUNER_ANALOG_TV; 1499 1500 mutex_lock(&dev->lock); 1501 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); 1502 mutex_unlock(&dev->lock); 1503 1504 return 0; 1505} 1506 1507static int vidioc_s_tuner(struct file *file, void *priv, 1508 struct v4l2_tuner *t) 1509{ 1510 struct em28xx_fh *fh = priv; 1511 struct em28xx *dev = fh->dev; 1512 int rc; 1513 1514 rc = check_dev(dev); 1515 if (rc < 0) 1516 return rc; 1517 1518 if (0 != t->index) 1519 return -EINVAL; 1520 1521 mutex_lock(&dev->lock); 1522 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t); 1523 mutex_unlock(&dev->lock); 1524 1525 return 0; 1526} 1527 1528static int vidioc_g_frequency(struct file *file, void *priv, 1529 struct v4l2_frequency *f) 1530{ 1531 struct em28xx_fh *fh = priv; 1532 struct em28xx *dev = fh->dev; 1533 1534 mutex_lock(&dev->lock); 1535 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1536 f->frequency = dev->ctl_freq; 1537 mutex_unlock(&dev->lock); 1538 1539 return 0; 1540} 1541 1542static int vidioc_s_frequency(struct file *file, void *priv, 1543 struct v4l2_frequency *f) 1544{ 1545 struct em28xx_fh *fh = priv; 1546 struct em28xx *dev = fh->dev; 1547 int rc; 1548 1549 rc = check_dev(dev); 1550 if (rc < 0) 1551 return rc; 1552 1553 if (0 != f->tuner) 1554 return -EINVAL; 1555 1556 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)) 1557 return -EINVAL; 1558 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO)) 1559 return -EINVAL; 1560 1561 mutex_lock(&dev->lock); 1562 1563 dev->ctl_freq = f->frequency; 1564 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f); 1565 1566 mutex_unlock(&dev->lock); 1567 1568 return 0; 1569} 1570 1571#ifdef CONFIG_VIDEO_ADV_DEBUG 1572static int em28xx_reg_len(int reg) 1573{ 1574 switch (reg) { 1575 case EM28XX_R40_AC97LSB: 1576 case EM28XX_R30_HSCALELOW: 1577 case EM28XX_R32_VSCALELOW: 1578 return 2; 1579 default: 1580 return 1; 1581 } 1582} 1583 1584static int vidioc_g_chip_ident(struct file *file, void *priv, 1585 struct v4l2_dbg_chip_ident *chip) 1586{ 1587 struct em28xx_fh *fh = priv; 1588 struct em28xx *dev = fh->dev; 1589 1590 chip->ident = V4L2_IDENT_NONE; 1591 chip->revision = 0; 1592 1593 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip); 1594 1595 return 0; 1596} 1597 1598 1599static int vidioc_g_register(struct file *file, void *priv, 1600 struct v4l2_dbg_register *reg) 1601{ 1602 struct em28xx_fh *fh = priv; 1603 struct em28xx *dev = fh->dev; 1604 int ret; 1605 1606 switch (reg->match.type) { 1607 case V4L2_CHIP_MATCH_AC97: 1608 mutex_lock(&dev->lock); 1609 ret = em28xx_read_ac97(dev, reg->reg); 1610 mutex_unlock(&dev->lock); 1611 if (ret < 0) 1612 return ret; 1613 1614 reg->val = ret; 1615 reg->size = 1; 1616 return 0; 1617 case V4L2_CHIP_MATCH_I2C_DRIVER: 1618 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg); 1619 return 0; 1620 case V4L2_CHIP_MATCH_I2C_ADDR: 1621 /* TODO: is this correct? */ 1622 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg); 1623 return 0; 1624 default: 1625 if (!v4l2_chip_match_host(®->match)) 1626 return -EINVAL; 1627 } 1628 1629 /* Match host */ 1630 reg->size = em28xx_reg_len(reg->reg); 1631 if (reg->size == 1) { 1632 mutex_lock(&dev->lock); 1633 ret = em28xx_read_reg(dev, reg->reg); 1634 mutex_unlock(&dev->lock); 1635 1636 if (ret < 0) 1637 return ret; 1638 1639 reg->val = ret; 1640 } else { 1641 __le16 val = 0; 1642 mutex_lock(&dev->lock); 1643 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS, 1644 reg->reg, (char *)&val, 2); 1645 mutex_unlock(&dev->lock); 1646 if (ret < 0) 1647 return ret; 1648 1649 reg->val = le16_to_cpu(val); 1650 } 1651 1652 return 0; 1653} 1654 1655static int vidioc_s_register(struct file *file, void *priv, 1656 struct v4l2_dbg_register *reg) 1657{ 1658 struct em28xx_fh *fh = priv; 1659 struct em28xx *dev = fh->dev; 1660 __le16 buf; 1661 int rc; 1662 1663 switch (reg->match.type) { 1664 case V4L2_CHIP_MATCH_AC97: 1665 mutex_lock(&dev->lock); 1666 rc = em28xx_write_ac97(dev, reg->reg, reg->val); 1667 mutex_unlock(&dev->lock); 1668 1669 return rc; 1670 case V4L2_CHIP_MATCH_I2C_DRIVER: 1671 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg); 1672 return 0; 1673 case V4L2_CHIP_MATCH_I2C_ADDR: 1674 /* TODO: is this correct? */ 1675 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg); 1676 return 0; 1677 default: 1678 if (!v4l2_chip_match_host(®->match)) 1679 return -EINVAL; 1680 } 1681 1682 /* Match host */ 1683 buf = cpu_to_le16(reg->val); 1684 1685 mutex_lock(&dev->lock); 1686 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf, 1687 em28xx_reg_len(reg->reg)); 1688 mutex_unlock(&dev->lock); 1689 1690 return rc; 1691} 1692#endif 1693 1694 1695static int vidioc_cropcap(struct file *file, void *priv, 1696 struct v4l2_cropcap *cc) 1697{ 1698 struct em28xx_fh *fh = priv; 1699 struct em28xx *dev = fh->dev; 1700 1701 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1702 return -EINVAL; 1703 1704 cc->bounds.left = 0; 1705 cc->bounds.top = 0; 1706 cc->bounds.width = dev->width; 1707 cc->bounds.height = dev->height; 1708 cc->defrect = cc->bounds; 1709 cc->pixelaspect.numerator = 54; 1710 cc->pixelaspect.denominator = 59; 1711 1712 return 0; 1713} 1714 1715static int vidioc_streamon(struct file *file, void *priv, 1716 enum v4l2_buf_type type) 1717{ 1718 struct em28xx_fh *fh = priv; 1719 struct em28xx *dev = fh->dev; 1720 int rc = -EINVAL; 1721 1722 rc = check_dev(dev); 1723 if (rc < 0) 1724 return rc; 1725 1726 if (unlikely(type != fh->type)) 1727 return -EINVAL; 1728 1729 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n", 1730 fh, type, fh->resources, dev->resources); 1731 1732 if (unlikely(!res_get(fh, get_ressource(fh)))) 1733 return -EBUSY; 1734 1735 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1736 rc = videobuf_streamon(&fh->vb_vidq); 1737 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) 1738 rc = videobuf_streamon(&fh->vb_vbiq); 1739 1740 return rc; 1741} 1742 1743static int vidioc_streamoff(struct file *file, void *priv, 1744 enum v4l2_buf_type type) 1745{ 1746 struct em28xx_fh *fh = priv; 1747 struct em28xx *dev = fh->dev; 1748 int rc; 1749 1750 rc = check_dev(dev); 1751 if (rc < 0) 1752 return rc; 1753 1754 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && 1755 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) 1756 return -EINVAL; 1757 if (type != fh->type) 1758 return -EINVAL; 1759 1760 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n", 1761 fh, type, fh->resources, dev->resources); 1762 1763 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 1764 videobuf_streamoff(&fh->vb_vidq); 1765 res_free(fh, EM28XX_RESOURCE_VIDEO); 1766 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 1767 videobuf_streamoff(&fh->vb_vbiq); 1768 res_free(fh, EM28XX_RESOURCE_VBI); 1769 } 1770 1771 return 0; 1772} 1773 1774static int vidioc_querycap(struct file *file, void *priv, 1775 struct v4l2_capability *cap) 1776{ 1777 struct em28xx_fh *fh = priv; 1778 struct em28xx *dev = fh->dev; 1779 1780 strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); 1781 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); 1782 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 1783 1784 cap->version = EM28XX_VERSION_CODE; 1785 1786 cap->capabilities = 1787 V4L2_CAP_SLICED_VBI_CAPTURE | 1788 V4L2_CAP_VIDEO_CAPTURE | 1789 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; 1790 1791 if (dev->vbi_dev) 1792 cap->capabilities |= V4L2_CAP_VBI_CAPTURE; 1793 1794 if (dev->audio_mode.has_audio) 1795 cap->capabilities |= V4L2_CAP_AUDIO; 1796 1797 if (dev->tuner_type != TUNER_ABSENT) 1798 cap->capabilities |= V4L2_CAP_TUNER; 1799 1800 return 0; 1801} 1802 1803static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 1804 struct v4l2_fmtdesc *f) 1805{ 1806 if (unlikely(f->index >= ARRAY_SIZE(format))) 1807 return -EINVAL; 1808 1809 strlcpy(f->description, format[f->index].name, sizeof(f->description)); 1810 f->pixelformat = format[f->index].fourcc; 1811 1812 return 0; 1813} 1814 1815/* Sliced VBI ioctls */ 1816static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv, 1817 struct v4l2_format *f) 1818{ 1819 struct em28xx_fh *fh = priv; 1820 struct em28xx *dev = fh->dev; 1821 int rc; 1822 1823 rc = check_dev(dev); 1824 if (rc < 0) 1825 return rc; 1826 1827 mutex_lock(&dev->lock); 1828 1829 f->fmt.sliced.service_set = 0; 1830 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced); 1831 1832 if (f->fmt.sliced.service_set == 0) 1833 rc = -EINVAL; 1834 1835 mutex_unlock(&dev->lock); 1836 1837 return rc; 1838} 1839 1840static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv, 1841 struct v4l2_format *f) 1842{ 1843 struct em28xx_fh *fh = priv; 1844 struct em28xx *dev = fh->dev; 1845 int rc; 1846 1847 rc = check_dev(dev); 1848 if (rc < 0) 1849 return rc; 1850 1851 mutex_lock(&dev->lock); 1852 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced); 1853 mutex_unlock(&dev->lock); 1854 1855 if (f->fmt.sliced.service_set == 0) 1856 return -EINVAL; 1857 1858 return 0; 1859} 1860 1861/* RAW VBI ioctls */ 1862 1863static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, 1864 struct v4l2_format *format) 1865{ 1866 struct em28xx_fh *fh = priv; 1867 struct em28xx *dev = fh->dev; 1868 1869 format->fmt.vbi.samples_per_line = dev->vbi_width; 1870 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; 1871 format->fmt.vbi.offset = 0; 1872 format->fmt.vbi.flags = 0; 1873 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1874 format->fmt.vbi.count[0] = dev->vbi_height; 1875 format->fmt.vbi.count[1] = dev->vbi_height; 1876 1877 /* Varies by video standard (NTSC, PAL, etc.) */ 1878 if (dev->norm & V4L2_STD_525_60) { 1879 /* NTSC */ 1880 format->fmt.vbi.start[0] = 10; 1881 format->fmt.vbi.start[1] = 273; 1882 } else if (dev->norm & V4L2_STD_625_50) { 1883 /* PAL */ 1884 format->fmt.vbi.start[0] = 6; 1885 format->fmt.vbi.start[1] = 318; 1886 } 1887 1888 return 0; 1889} 1890 1891static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv, 1892 struct v4l2_format *format) 1893{ 1894 struct em28xx_fh *fh = priv; 1895 struct em28xx *dev = fh->dev; 1896 1897 format->fmt.vbi.samples_per_line = dev->vbi_width; 1898 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; 1899 format->fmt.vbi.offset = 0; 1900 format->fmt.vbi.flags = 0; 1901 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1902 format->fmt.vbi.count[0] = dev->vbi_height; 1903 format->fmt.vbi.count[1] = dev->vbi_height; 1904 1905 /* Varies by video standard (NTSC, PAL, etc.) */ 1906 if (dev->norm & V4L2_STD_525_60) { 1907 /* NTSC */ 1908 format->fmt.vbi.start[0] = 10; 1909 format->fmt.vbi.start[1] = 273; 1910 } else if (dev->norm & V4L2_STD_625_50) { 1911 /* PAL */ 1912 format->fmt.vbi.start[0] = 6; 1913 format->fmt.vbi.start[1] = 318; 1914 } 1915 1916 return 0; 1917} 1918 1919static int vidioc_reqbufs(struct file *file, void *priv, 1920 struct v4l2_requestbuffers *rb) 1921{ 1922 struct em28xx_fh *fh = priv; 1923 struct em28xx *dev = fh->dev; 1924 int rc; 1925 1926 rc = check_dev(dev); 1927 if (rc < 0) 1928 return rc; 1929 1930 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1931 return videobuf_reqbufs(&fh->vb_vidq, rb); 1932 else 1933 return videobuf_reqbufs(&fh->vb_vbiq, rb); 1934} 1935 1936static int vidioc_querybuf(struct file *file, void *priv, 1937 struct v4l2_buffer *b) 1938{ 1939 struct em28xx_fh *fh = priv; 1940 struct em28xx *dev = fh->dev; 1941 int rc; 1942 1943 rc = check_dev(dev); 1944 if (rc < 0) 1945 return rc; 1946 1947 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1948 return videobuf_querybuf(&fh->vb_vidq, b); 1949 else { 1950 int result = videobuf_querybuf(&fh->vb_vbiq, b); 1951 b->length = dev->vbi_width * dev->vbi_height * 2; 1952 1953 return result; 1954 } 1955} 1956 1957static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1958{ 1959 struct em28xx_fh *fh = priv; 1960 struct em28xx *dev = fh->dev; 1961 int rc; 1962 1963 rc = check_dev(dev); 1964 if (rc < 0) 1965 return rc; 1966 1967 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1968 return videobuf_qbuf(&fh->vb_vidq, b); 1969 else 1970 return videobuf_qbuf(&fh->vb_vbiq, b); 1971} 1972 1973static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1974{ 1975 struct em28xx_fh *fh = priv; 1976 struct em28xx *dev = fh->dev; 1977 int rc; 1978 1979 rc = check_dev(dev); 1980 if (rc < 0) 1981 return rc; 1982 1983 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1984 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & 1985 O_NONBLOCK); 1986 else 1987 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & 1988 O_NONBLOCK); 1989} 1990 1991#ifdef CONFIG_VIDEO_V4L1_COMPAT 1992static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf) 1993{ 1994 struct em28xx_fh *fh = priv; 1995 1996 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 1997 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8); 1998 else 1999 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8); 2000} 2001#endif 2002 2003 2004/* ----------------------------------------------------------- */ 2005/* RADIO ESPECIFIC IOCTLS */ 2006/* ----------------------------------------------------------- */ 2007 2008static int radio_querycap(struct file *file, void *priv, 2009 struct v4l2_capability *cap) 2010{ 2011 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; 2012 2013 strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); 2014 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); 2015 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 2016 2017 cap->version = EM28XX_VERSION_CODE; 2018 cap->capabilities = V4L2_CAP_TUNER; 2019 return 0; 2020} 2021 2022static int radio_g_tuner(struct file *file, void *priv, 2023 struct v4l2_tuner *t) 2024{ 2025 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; 2026 2027 if (unlikely(t->index > 0)) 2028 return -EINVAL; 2029 2030 strcpy(t->name, "Radio"); 2031 t->type = V4L2_TUNER_RADIO; 2032 2033 mutex_lock(&dev->lock); 2034 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); 2035 mutex_unlock(&dev->lock); 2036 2037 return 0; 2038} 2039 2040static int radio_enum_input(struct file *file, void *priv, 2041 struct v4l2_input *i) 2042{ 2043 if (i->index != 0) 2044 return -EINVAL; 2045 strcpy(i->name, "Radio"); 2046 i->type = V4L2_INPUT_TYPE_TUNER; 2047 2048 return 0; 2049} 2050 2051static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a) 2052{ 2053 if (unlikely(a->index)) 2054 return -EINVAL; 2055 2056 strcpy(a->name, "Radio"); 2057 return 0; 2058} 2059 2060static int radio_s_tuner(struct file *file, void *priv, 2061 struct v4l2_tuner *t) 2062{ 2063 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; 2064 2065 if (0 != t->index) 2066 return -EINVAL; 2067 2068 mutex_lock(&dev->lock); 2069 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t); 2070 mutex_unlock(&dev->lock); 2071 2072 return 0; 2073} 2074 2075static int radio_s_audio(struct file *file, void *fh, 2076 struct v4l2_audio *a) 2077{ 2078 return 0; 2079} 2080 2081static int radio_s_input(struct file *file, void *fh, unsigned int i) 2082{ 2083 return 0; 2084} 2085 2086static int radio_queryctrl(struct file *file, void *priv, 2087 struct v4l2_queryctrl *qc) 2088{ 2089 int i; 2090 2091 if (qc->id < V4L2_CID_BASE || 2092 qc->id >= V4L2_CID_LASTP1) 2093 return -EINVAL; 2094 2095 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) { 2096 if (qc->id && qc->id == ac97_qctrl[i].id) { 2097 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc)); 2098 return 0; 2099 } 2100 } 2101 2102 return -EINVAL; 2103} 2104 2105/* 2106 * em28xx_v4l2_open() 2107 * inits the device and starts isoc transfer 2108 */ 2109static int em28xx_v4l2_open(struct file *filp) 2110{ 2111 int errCode = 0, radio = 0; 2112 struct video_device *vdev = video_devdata(filp); 2113 struct em28xx *dev = video_drvdata(filp); 2114 enum v4l2_buf_type fh_type = 0; 2115 struct em28xx_fh *fh; 2116 enum v4l2_field field; 2117 2118 switch (vdev->vfl_type) { 2119 case VFL_TYPE_GRABBER: 2120 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2121 break; 2122 case VFL_TYPE_VBI: 2123 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; 2124 break; 2125 case VFL_TYPE_RADIO: 2126 radio = 1; 2127 break; 2128 } 2129 2130 mutex_lock(&dev->lock); 2131 2132 em28xx_videodbg("open dev=%s type=%s users=%d\n", 2133 video_device_node_name(vdev), v4l2_type_names[fh_type], 2134 dev->users); 2135 2136 2137 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL); 2138 if (!fh) { 2139 em28xx_errdev("em28xx-video.c: Out of memory?!\n"); 2140 mutex_unlock(&dev->lock); 2141 return -ENOMEM; 2142 } 2143 fh->dev = dev; 2144 fh->radio = radio; 2145 fh->type = fh_type; 2146 filp->private_data = fh; 2147 2148 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { 2149 em28xx_set_mode(dev, EM28XX_ANALOG_MODE); 2150 em28xx_set_alternate(dev); 2151 em28xx_resolution_set(dev); 2152 2153 /* Needed, since GPIO might have disabled power of 2154 some i2c device 2155 */ 2156 em28xx_wake_i2c(dev); 2157 2158 } 2159 if (fh->radio) { 2160 em28xx_videodbg("video_open: setting radio device\n"); 2161 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio); 2162 } 2163 2164 dev->users++; 2165 2166 if (dev->progressive) 2167 field = V4L2_FIELD_NONE; 2168 else 2169 field = V4L2_FIELD_INTERLACED; 2170 2171 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops, 2172 NULL, &dev->slock, 2173 V4L2_BUF_TYPE_VIDEO_CAPTURE, field, 2174 sizeof(struct em28xx_buffer), fh); 2175 2176 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops, 2177 NULL, &dev->slock, 2178 V4L2_BUF_TYPE_VBI_CAPTURE, 2179 V4L2_FIELD_SEQ_TB, 2180 sizeof(struct em28xx_buffer), fh); 2181 2182 mutex_unlock(&dev->lock); 2183 2184 return errCode; 2185} 2186 2187/* 2188 * em28xx_realease_resources() 2189 * unregisters the v4l2,i2c and usb devices 2190 * called when the device gets disconected or at module unload 2191*/ 2192void em28xx_release_analog_resources(struct em28xx *dev) 2193{ 2194 2195 2196 if (dev->radio_dev) { 2197 if (video_is_registered(dev->radio_dev)) 2198 video_unregister_device(dev->radio_dev); 2199 else 2200 video_device_release(dev->radio_dev); 2201 dev->radio_dev = NULL; 2202 } 2203 if (dev->vbi_dev) { 2204 em28xx_info("V4L2 device %s deregistered\n", 2205 video_device_node_name(dev->vbi_dev)); 2206 if (video_is_registered(dev->vbi_dev)) 2207 video_unregister_device(dev->vbi_dev); 2208 else 2209 video_device_release(dev->vbi_dev); 2210 dev->vbi_dev = NULL; 2211 } 2212 if (dev->vdev) { 2213 em28xx_info("V4L2 device %s deregistered\n", 2214 video_device_node_name(dev->vdev)); 2215 if (video_is_registered(dev->vdev)) 2216 video_unregister_device(dev->vdev); 2217 else 2218 video_device_release(dev->vdev); 2219 dev->vdev = NULL; 2220 } 2221} 2222 2223/* 2224 * em28xx_v4l2_close() 2225 * stops streaming and deallocates all resources allocated by the v4l2 2226 * calls and ioctls 2227 */ 2228static int em28xx_v4l2_close(struct file *filp) 2229{ 2230 struct em28xx_fh *fh = filp->private_data; 2231 struct em28xx *dev = fh->dev; 2232 int errCode; 2233 2234 em28xx_videodbg("users=%d\n", dev->users); 2235 2236 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) { 2237 videobuf_stop(&fh->vb_vidq); 2238 res_free(fh, EM28XX_RESOURCE_VIDEO); 2239 } 2240 2241 if (res_check(fh, EM28XX_RESOURCE_VBI)) { 2242 videobuf_stop(&fh->vb_vbiq); 2243 res_free(fh, EM28XX_RESOURCE_VBI); 2244 } 2245 2246 if (dev->users == 1) { 2247 /* the device is already disconnect, 2248 free the remaining resources */ 2249 if (dev->state & DEV_DISCONNECTED) { 2250 em28xx_release_resources(dev); 2251 kfree(dev); 2252 return 0; 2253 } 2254 2255 /* Save some power by putting tuner to sleep */ 2256 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0); 2257 2258 /* do this before setting alternate! */ 2259 em28xx_uninit_isoc(dev); 2260 em28xx_set_mode(dev, EM28XX_SUSPEND); 2261 2262 /* set alternate 0 */ 2263 dev->alt = 0; 2264 em28xx_videodbg("setting alternate 0\n"); 2265 errCode = usb_set_interface(dev->udev, 0, 0); 2266 if (errCode < 0) { 2267 em28xx_errdev("cannot change alternate number to " 2268 "0 (error=%i)\n", errCode); 2269 } 2270 } 2271 2272 videobuf_mmap_free(&fh->vb_vidq); 2273 videobuf_mmap_free(&fh->vb_vbiq); 2274 kfree(fh); 2275 dev->users--; 2276 wake_up_interruptible_nr(&dev->open, 1); 2277 return 0; 2278} 2279 2280/* 2281 * em28xx_v4l2_read() 2282 * will allocate buffers when called for the first time 2283 */ 2284static ssize_t 2285em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count, 2286 loff_t *pos) 2287{ 2288 struct em28xx_fh *fh = filp->private_data; 2289 struct em28xx *dev = fh->dev; 2290 int rc; 2291 2292 rc = check_dev(dev); 2293 if (rc < 0) 2294 return rc; 2295 2296 2297 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 2298 if (res_locked(dev, EM28XX_RESOURCE_VIDEO)) 2299 return -EBUSY; 2300 2301 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0, 2302 filp->f_flags & O_NONBLOCK); 2303 } 2304 2305 2306 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 2307 if (!res_get(fh, EM28XX_RESOURCE_VBI)) 2308 return -EBUSY; 2309 2310 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0, 2311 filp->f_flags & O_NONBLOCK); 2312 } 2313 2314 return 0; 2315} 2316 2317/* 2318 * em28xx_v4l2_poll() 2319 * will allocate buffers when called for the first time 2320 */ 2321static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait) 2322{ 2323 struct em28xx_fh *fh = filp->private_data; 2324 struct em28xx *dev = fh->dev; 2325 int rc; 2326 2327 rc = check_dev(dev); 2328 if (rc < 0) 2329 return rc; 2330 2331 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 2332 if (!res_get(fh, EM28XX_RESOURCE_VIDEO)) 2333 return POLLERR; 2334 return videobuf_poll_stream(filp, &fh->vb_vidq, wait); 2335 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 2336 if (!res_get(fh, EM28XX_RESOURCE_VBI)) 2337 return POLLERR; 2338 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait); 2339 } else { 2340 return POLLERR; 2341 } 2342} 2343 2344/* 2345 * em28xx_v4l2_mmap() 2346 */ 2347static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) 2348{ 2349 struct em28xx_fh *fh = filp->private_data; 2350 struct em28xx *dev = fh->dev; 2351 int rc; 2352 2353 rc = check_dev(dev); 2354 if (rc < 0) 2355 return rc; 2356 2357 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 2358 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma); 2359 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) 2360 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma); 2361 2362 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n", 2363 (unsigned long)vma->vm_start, 2364 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start, 2365 rc); 2366 2367 return rc; 2368} 2369 2370static const struct v4l2_file_operations em28xx_v4l_fops = { 2371 .owner = THIS_MODULE, 2372 .open = em28xx_v4l2_open, 2373 .release = em28xx_v4l2_close, 2374 .read = em28xx_v4l2_read, 2375 .poll = em28xx_v4l2_poll, 2376 .mmap = em28xx_v4l2_mmap, 2377 .ioctl = video_ioctl2, 2378}; 2379 2380static const struct v4l2_ioctl_ops video_ioctl_ops = { 2381 .vidioc_querycap = vidioc_querycap, 2382 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 2383 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 2384 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 2385 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 2386 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 2387 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap, 2388 .vidioc_g_audio = vidioc_g_audio, 2389 .vidioc_s_audio = vidioc_s_audio, 2390 .vidioc_cropcap = vidioc_cropcap, 2391 2392 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap, 2393 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, 2394 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, 2395 2396 .vidioc_reqbufs = vidioc_reqbufs, 2397 .vidioc_querybuf = vidioc_querybuf, 2398 .vidioc_qbuf = vidioc_qbuf, 2399 .vidioc_dqbuf = vidioc_dqbuf, 2400 .vidioc_g_std = vidioc_g_std, 2401 .vidioc_s_std = vidioc_s_std, 2402 .vidioc_g_parm = vidioc_g_parm, 2403 .vidioc_s_parm = vidioc_s_parm, 2404 .vidioc_enum_input = vidioc_enum_input, 2405 .vidioc_g_input = vidioc_g_input, 2406 .vidioc_s_input = vidioc_s_input, 2407 .vidioc_queryctrl = vidioc_queryctrl, 2408 .vidioc_g_ctrl = vidioc_g_ctrl, 2409 .vidioc_s_ctrl = vidioc_s_ctrl, 2410 .vidioc_streamon = vidioc_streamon, 2411 .vidioc_streamoff = vidioc_streamoff, 2412 .vidioc_g_tuner = vidioc_g_tuner, 2413 .vidioc_s_tuner = vidioc_s_tuner, 2414 .vidioc_g_frequency = vidioc_g_frequency, 2415 .vidioc_s_frequency = vidioc_s_frequency, 2416#ifdef CONFIG_VIDEO_ADV_DEBUG 2417 .vidioc_g_register = vidioc_g_register, 2418 .vidioc_s_register = vidioc_s_register, 2419 .vidioc_g_chip_ident = vidioc_g_chip_ident, 2420#endif 2421#ifdef CONFIG_VIDEO_V4L1_COMPAT 2422 .vidiocgmbuf = vidiocgmbuf, 2423#endif 2424}; 2425 2426static const struct video_device em28xx_video_template = { 2427 .fops = &em28xx_v4l_fops, 2428 .release = video_device_release, 2429 .ioctl_ops = &video_ioctl_ops, 2430 2431 .tvnorms = V4L2_STD_ALL, 2432 .current_norm = V4L2_STD_PAL, 2433}; 2434 2435static const struct v4l2_file_operations radio_fops = { 2436 .owner = THIS_MODULE, 2437 .open = em28xx_v4l2_open, 2438 .release = em28xx_v4l2_close, 2439 .ioctl = video_ioctl2, 2440}; 2441 2442static const struct v4l2_ioctl_ops radio_ioctl_ops = { 2443 .vidioc_querycap = radio_querycap, 2444 .vidioc_g_tuner = radio_g_tuner, 2445 .vidioc_enum_input = radio_enum_input, 2446 .vidioc_g_audio = radio_g_audio, 2447 .vidioc_s_tuner = radio_s_tuner, 2448 .vidioc_s_audio = radio_s_audio, 2449 .vidioc_s_input = radio_s_input, 2450 .vidioc_queryctrl = radio_queryctrl, 2451 .vidioc_g_ctrl = vidioc_g_ctrl, 2452 .vidioc_s_ctrl = vidioc_s_ctrl, 2453 .vidioc_g_frequency = vidioc_g_frequency, 2454 .vidioc_s_frequency = vidioc_s_frequency, 2455#ifdef CONFIG_VIDEO_ADV_DEBUG 2456 .vidioc_g_register = vidioc_g_register, 2457 .vidioc_s_register = vidioc_s_register, 2458#endif 2459}; 2460 2461static struct video_device em28xx_radio_template = { 2462 .name = "em28xx-radio", 2463 .fops = &radio_fops, 2464 .ioctl_ops = &radio_ioctl_ops, 2465}; 2466 2467/******************************** usb interface ******************************/ 2468 2469 2470 2471static struct video_device *em28xx_vdev_init(struct em28xx *dev, 2472 const struct video_device *template, 2473 const char *type_name) 2474{ 2475 struct video_device *vfd; 2476 2477 vfd = video_device_alloc(); 2478 if (NULL == vfd) 2479 return NULL; 2480 2481 *vfd = *template; 2482 vfd->v4l2_dev = &dev->v4l2_dev; 2483 vfd->release = video_device_release; 2484 vfd->debug = video_debug; 2485 2486 snprintf(vfd->name, sizeof(vfd->name), "%s %s", 2487 dev->name, type_name); 2488 2489 video_set_drvdata(vfd, dev); 2490 return vfd; 2491} 2492 2493int em28xx_register_analog_devices(struct em28xx *dev) 2494{ 2495 u8 val; 2496 int ret; 2497 2498 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n", 2499 dev->name, 2500 (EM28XX_VERSION_CODE >> 16) & 0xff, 2501 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff); 2502 2503 /* set default norm */ 2504 dev->norm = em28xx_video_template.current_norm; 2505 dev->interlaced = EM28XX_INTERLACED_DEFAULT; 2506 dev->ctl_input = 0; 2507 2508 /* Analog specific initialization */ 2509 dev->format = &format[0]; 2510 em28xx_set_video_format(dev, format[0].fourcc, 2511 norm_maxw(dev), norm_maxh(dev)); 2512 2513 video_mux(dev, dev->ctl_input); 2514 2515 /* Audio defaults */ 2516 dev->mute = 1; 2517 dev->volume = 0x1f; 2518 2519/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */ 2520 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK); 2521 em28xx_write_reg(dev, EM28XX_R0F_XCLK, 2522 (EM28XX_XCLK_AUDIO_UNMUTE | val)); 2523 2524 em28xx_set_outfmt(dev); 2525 em28xx_colorlevels_set_default(dev); 2526 em28xx_compression_disable(dev); 2527 2528 /* allocate and fill video video_device struct */ 2529 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video"); 2530 if (!dev->vdev) { 2531 em28xx_errdev("cannot allocate video_device.\n"); 2532 return -ENODEV; 2533 } 2534 2535 /* register v4l2 video video_device */ 2536 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER, 2537 video_nr[dev->devno]); 2538 if (ret) { 2539 em28xx_errdev("unable to register video device (error=%i).\n", 2540 ret); 2541 return ret; 2542 } 2543 2544 /* Allocate and fill vbi video_device struct */ 2545 if (em28xx_vbi_supported(dev) == 1) { 2546 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, 2547 "vbi"); 2548 2549 /* register v4l2 vbi video_device */ 2550 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, 2551 vbi_nr[dev->devno]); 2552 if (ret < 0) { 2553 em28xx_errdev("unable to register vbi device\n"); 2554 return ret; 2555 } 2556 } 2557 2558 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) { 2559 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, 2560 "radio"); 2561 if (!dev->radio_dev) { 2562 em28xx_errdev("cannot allocate video_device.\n"); 2563 return -ENODEV; 2564 } 2565 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO, 2566 radio_nr[dev->devno]); 2567 if (ret < 0) { 2568 em28xx_errdev("can't register radio device\n"); 2569 return ret; 2570 } 2571 em28xx_info("Registered radio device as %s\n", 2572 video_device_node_name(dev->radio_dev)); 2573 } 2574 2575 em28xx_info("V4L2 video device registered as %s\n", 2576 video_device_node_name(dev->vdev)); 2577 2578 if (dev->vbi_dev) 2579 em28xx_info("V4L2 VBI device registered as %s\n", 2580 video_device_node_name(dev->vbi_dev)); 2581 2582 return 0; 2583} 2584