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