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