linux/drivers/media/usb/tm6000/tm6000-video.c
<<
>>
Prefs
   1/*
   2 *   tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
   3 *
   4 *  Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
   5 *
   6 *  Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
   7 *      - Fixed module load/unload
   8 *
   9 *  This program is free software; you can redistribute it and/or modify
  10 *  it under the terms of the GNU General Public License as published by
  11 *  the Free Software Foundation version 2
  12 *
  13 *  This program is distributed in the hope that it will be useful,
  14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *  GNU General Public License for more details.
  17 */
  18
  19#include <linux/module.h>
  20#include <linux/delay.h>
  21#include <linux/errno.h>
  22#include <linux/fs.h>
  23#include <linux/kernel.h>
  24#include <linux/slab.h>
  25#include <linux/mm.h>
  26#include <linux/ioport.h>
  27#include <linux/init.h>
  28#include <linux/sched.h>
  29#include <linux/random.h>
  30#include <linux/usb.h>
  31#include <linux/videodev2.h>
  32#include <media/v4l2-ioctl.h>
  33#include <media/v4l2-event.h>
  34#include <media/tuner.h>
  35#include <linux/interrupt.h>
  36#include <linux/kthread.h>
  37#include <linux/highmem.h>
  38#include <linux/freezer.h>
  39
  40#include "tm6000-regs.h"
  41#include "tm6000.h"
  42
  43#define BUFFER_TIMEOUT     msecs_to_jiffies(2000)  /* 2 seconds */
  44
  45/* Limits minimum and default number of buffers */
  46#define TM6000_MIN_BUF 4
  47#define TM6000_DEF_BUF 8
  48
  49#define TM6000_NUM_URB_BUF 8
  50
  51#define TM6000_MAX_ISO_PACKETS  46      /* Max number of ISO packets */
  52
  53/* Declare static vars that will be used as parameters */
  54static unsigned int vid_limit = 16;     /* Video memory limit, in Mb */
  55static int video_nr = -1;               /* /dev/videoN, -1 for autodetect */
  56static int radio_nr = -1;               /* /dev/radioN, -1 for autodetect */
  57static bool keep_urb;                   /* keep urb buffers allocated */
  58
  59/* Debug level */
  60int tm6000_debug;
  61EXPORT_SYMBOL_GPL(tm6000_debug);
  62
  63static struct tm6000_fmt format[] = {
  64        {
  65                .name     = "4:2:2, packed, YVY2",
  66                .fourcc   = V4L2_PIX_FMT_YUYV,
  67                .depth    = 16,
  68        }, {
  69                .name     = "4:2:2, packed, UYVY",
  70                .fourcc   = V4L2_PIX_FMT_UYVY,
  71                .depth    = 16,
  72        }, {
  73                .name     = "A/V + VBI mux packet",
  74                .fourcc   = V4L2_PIX_FMT_TM6000,
  75                .depth    = 16,
  76        }
  77};
  78
  79/* ------------------------------------------------------------------
  80 *      DMA and thread functions
  81 * ------------------------------------------------------------------
  82 */
  83
  84#define norm_maxw(a) 720
  85#define norm_maxh(a) 576
  86
  87#define norm_minw(a) norm_maxw(a)
  88#define norm_minh(a) norm_maxh(a)
  89
  90/*
  91 * video-buf generic routine to get the next available buffer
  92 */
  93static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
  94                               struct tm6000_buffer   **buf)
  95{
  96        struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
  97
  98        if (list_empty(&dma_q->active)) {
  99                dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
 100                *buf = NULL;
 101                return;
 102        }
 103
 104        *buf = list_entry(dma_q->active.next,
 105                        struct tm6000_buffer, vb.queue);
 106}
 107
 108/*
 109 * Announces that a buffer were filled and request the next
 110 */
 111static inline void buffer_filled(struct tm6000_core *dev,
 112                                 struct tm6000_dmaqueue *dma_q,
 113                                 struct tm6000_buffer *buf)
 114{
 115        /* Advice that buffer was filled */
 116        dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
 117        buf->vb.state = VIDEOBUF_DONE;
 118        buf->vb.field_count++;
 119        v4l2_get_timestamp(&buf->vb.ts);
 120
 121        list_del(&buf->vb.queue);
 122        wake_up(&buf->vb.done);
 123}
 124
 125/*
 126 * Identify the tm5600/6000 buffer header type and properly handles
 127 */
 128static int copy_streams(u8 *data, unsigned long len,
 129                        struct urb *urb)
 130{
 131        struct tm6000_dmaqueue  *dma_q = urb->context;
 132        struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
 133        u8 *ptr = data, *endp = data+len;
 134        unsigned long header = 0;
 135        int rc = 0;
 136        unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
 137        struct tm6000_buffer *vbuf = NULL;
 138        char *voutp = NULL;
 139        unsigned int linewidth;
 140
 141        if (!dev->radio) {
 142                /* get video buffer */
 143                get_next_buf(dma_q, &vbuf);
 144
 145                if (!vbuf)
 146                        return rc;
 147                voutp = videobuf_to_vmalloc(&vbuf->vb);
 148
 149                if (!voutp)
 150                        return 0;
 151        }
 152
 153        for (ptr = data; ptr < endp;) {
 154                if (!dev->isoc_ctl.cmd) {
 155                        /* Header */
 156                        if (dev->isoc_ctl.tmp_buf_len > 0) {
 157                                /* from last urb or packet */
 158                                header = dev->isoc_ctl.tmp_buf;
 159                                if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
 160                                        memcpy((u8 *)&header +
 161                                                dev->isoc_ctl.tmp_buf_len,
 162                                                ptr,
 163                                                4 - dev->isoc_ctl.tmp_buf_len);
 164                                        ptr += 4 - dev->isoc_ctl.tmp_buf_len;
 165                                }
 166                                dev->isoc_ctl.tmp_buf_len = 0;
 167                        } else {
 168                                if (ptr + 3 >= endp) {
 169                                        /* have incomplete header */
 170                                        dev->isoc_ctl.tmp_buf_len = endp - ptr;
 171                                        memcpy(&dev->isoc_ctl.tmp_buf, ptr,
 172                                                dev->isoc_ctl.tmp_buf_len);
 173                                        return rc;
 174                                }
 175                                /* Seek for sync */
 176                                for (; ptr < endp - 3; ptr++) {
 177                                        if (*(ptr + 3) == 0x47)
 178                                                break;
 179                                }
 180                                /* Get message header */
 181                                header = *(unsigned long *)ptr;
 182                                ptr += 4;
 183                        }
 184
 185                        /* split the header fields */
 186                        size = ((header & 0x7e) << 1);
 187                        if (size > 0)
 188                                size -= 4;
 189                        block = (header >> 7) & 0xf;
 190                        field = (header >> 11) & 0x1;
 191                        line  = (header >> 12) & 0x1ff;
 192                        cmd   = (header >> 21) & 0x7;
 193                        /* Validates haeder fields */
 194                        if (size > TM6000_URB_MSG_LEN)
 195                                size = TM6000_URB_MSG_LEN;
 196                        pktsize = TM6000_URB_MSG_LEN;
 197                        /*
 198                         * calculate position in buffer and change the buffer
 199                         */
 200                        switch (cmd) {
 201                        case TM6000_URB_MSG_VIDEO:
 202                                if (!dev->radio) {
 203                                        if ((dev->isoc_ctl.vfield != field) &&
 204                                                (field == 1)) {
 205                                                /*
 206                                                 * Announces that a new buffer
 207                                                 * were filled
 208                                                 */
 209                                                buffer_filled(dev, dma_q, vbuf);
 210                                                dprintk(dev, V4L2_DEBUG_ISOC,
 211                                                        "new buffer filled\n");
 212                                                get_next_buf(dma_q, &vbuf);
 213                                                if (!vbuf)
 214                                                        return rc;
 215                                                voutp = videobuf_to_vmalloc(&vbuf->vb);
 216                                                if (!voutp)
 217                                                        return rc;
 218                                                memset(voutp, 0, vbuf->vb.size);
 219                                        }
 220                                        linewidth = vbuf->vb.width << 1;
 221                                        pos = ((line << 1) - field - 1) *
 222                                        linewidth + block * TM6000_URB_MSG_LEN;
 223                                        /* Don't allow to write out of the buffer */
 224                                        if (pos + size > vbuf->vb.size)
 225                                                cmd = TM6000_URB_MSG_ERR;
 226                                        dev->isoc_ctl.vfield = field;
 227                                }
 228                                break;
 229                        case TM6000_URB_MSG_VBI:
 230                                break;
 231                        case TM6000_URB_MSG_AUDIO:
 232                        case TM6000_URB_MSG_PTS:
 233                                size = pktsize; /* Size is always 180 bytes */
 234                                break;
 235                        }
 236                } else {
 237                        /* Continue the last copy */
 238                        cmd = dev->isoc_ctl.cmd;
 239                        size = dev->isoc_ctl.size;
 240                        pos = dev->isoc_ctl.pos;
 241                        pktsize = dev->isoc_ctl.pktsize;
 242                        field = dev->isoc_ctl.field;
 243                }
 244                cpysize = (endp - ptr > size) ? size : endp - ptr;
 245                if (cpysize) {
 246                        /* copy data in different buffers */
 247                        switch (cmd) {
 248                        case TM6000_URB_MSG_VIDEO:
 249                                /* Fills video buffer */
 250                                if (vbuf)
 251                                        memcpy(&voutp[pos], ptr, cpysize);
 252                                break;
 253                        case TM6000_URB_MSG_AUDIO: {
 254                                int i;
 255                                for (i = 0; i < cpysize; i += 2)
 256                                        swab16s((u16 *)(ptr + i));
 257
 258                                tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
 259                                break;
 260                        }
 261                        case TM6000_URB_MSG_VBI:
 262                                /* Need some code to copy vbi buffer */
 263                                break;
 264                        case TM6000_URB_MSG_PTS: {
 265                                /* Need some code to copy pts */
 266                                u32 pts;
 267                                pts = *(u32 *)ptr;
 268                                dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
 269                                        field, pts);
 270                                break;
 271                        }
 272                        }
 273                }
 274                if (ptr + pktsize > endp) {
 275                        /*
 276                         * End of URB packet, but cmd processing is not
 277                         * complete. Preserve the state for a next packet
 278                         */
 279                        dev->isoc_ctl.pos = pos + cpysize;
 280                        dev->isoc_ctl.size = size - cpysize;
 281                        dev->isoc_ctl.cmd = cmd;
 282                        dev->isoc_ctl.field = field;
 283                        dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
 284                        ptr += endp - ptr;
 285                } else {
 286                        dev->isoc_ctl.cmd = 0;
 287                        ptr += pktsize;
 288                }
 289        }
 290        return 0;
 291}
 292
 293/*
 294 * Identify the tm5600/6000 buffer header type and properly handles
 295 */
 296static int copy_multiplexed(u8 *ptr, unsigned long len,
 297                        struct urb *urb)
 298{
 299        struct tm6000_dmaqueue  *dma_q = urb->context;
 300        struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
 301        unsigned int pos = dev->isoc_ctl.pos, cpysize;
 302        int rc = 1;
 303        struct tm6000_buffer *buf;
 304        char *outp = NULL;
 305
 306        get_next_buf(dma_q, &buf);
 307        if (buf)
 308                outp = videobuf_to_vmalloc(&buf->vb);
 309
 310        if (!outp)
 311                return 0;
 312
 313        while (len > 0) {
 314                cpysize = min(len, buf->vb.size-pos);
 315                memcpy(&outp[pos], ptr, cpysize);
 316                pos += cpysize;
 317                ptr += cpysize;
 318                len -= cpysize;
 319                if (pos >= buf->vb.size) {
 320                        pos = 0;
 321                        /* Announces that a new buffer were filled */
 322                        buffer_filled(dev, dma_q, buf);
 323                        dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
 324                        get_next_buf(dma_q, &buf);
 325                        if (!buf)
 326                                break;
 327                        outp = videobuf_to_vmalloc(&(buf->vb));
 328                        if (!outp)
 329                                return rc;
 330                        pos = 0;
 331                }
 332        }
 333
 334        dev->isoc_ctl.pos = pos;
 335        return rc;
 336}
 337
 338static inline void print_err_status(struct tm6000_core *dev,
 339                                     int packet, int status)
 340{
 341        char *errmsg = "Unknown";
 342
 343        switch (status) {
 344        case -ENOENT:
 345                errmsg = "unlinked synchronously";
 346                break;
 347        case -ECONNRESET:
 348                errmsg = "unlinked asynchronously";
 349                break;
 350        case -ENOSR:
 351                errmsg = "Buffer error (overrun)";
 352                break;
 353        case -EPIPE:
 354                errmsg = "Stalled (device not responding)";
 355                break;
 356        case -EOVERFLOW:
 357                errmsg = "Babble (bad cable?)";
 358                break;
 359        case -EPROTO:
 360                errmsg = "Bit-stuff error (bad cable?)";
 361                break;
 362        case -EILSEQ:
 363                errmsg = "CRC/Timeout (could be anything)";
 364                break;
 365        case -ETIME:
 366                errmsg = "Device does not respond";
 367                break;
 368        }
 369        if (packet < 0) {
 370                dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
 371                        status, errmsg);
 372        } else {
 373                dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
 374                        packet, status, errmsg);
 375        }
 376}
 377
 378
 379/*
 380 * Controls the isoc copy of each urb packet
 381 */
 382static inline int tm6000_isoc_copy(struct urb *urb)
 383{
 384        struct tm6000_dmaqueue  *dma_q = urb->context;
 385        struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
 386        int i, len = 0, rc = 1, status;
 387        char *p;
 388
 389        if (urb->status < 0) {
 390                print_err_status(dev, -1, urb->status);
 391                return 0;
 392        }
 393
 394        for (i = 0; i < urb->number_of_packets; i++) {
 395                status = urb->iso_frame_desc[i].status;
 396
 397                if (status < 0) {
 398                        print_err_status(dev, i, status);
 399                        continue;
 400                }
 401
 402                len = urb->iso_frame_desc[i].actual_length;
 403
 404                if (len > 0) {
 405                        p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
 406                        if (!urb->iso_frame_desc[i].status) {
 407                                if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
 408                                        rc = copy_multiplexed(p, len, urb);
 409                                        if (rc <= 0)
 410                                                return rc;
 411                                } else {
 412                                        copy_streams(p, len, urb);
 413                                }
 414                        }
 415                }
 416        }
 417        return rc;
 418}
 419
 420/* ------------------------------------------------------------------
 421 *      URB control
 422 * ------------------------------------------------------------------
 423 */
 424
 425/*
 426 * IRQ callback, called by URB callback
 427 */
 428static void tm6000_irq_callback(struct urb *urb)
 429{
 430        struct tm6000_dmaqueue  *dma_q = urb->context;
 431        struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
 432        int i;
 433
 434        switch (urb->status) {
 435        case 0:
 436        case -ETIMEDOUT:
 437                break;
 438
 439        case -ECONNRESET:
 440        case -ENOENT:
 441        case -ESHUTDOWN:
 442                return;
 443
 444        default:
 445                tm6000_err("urb completion error %d.\n", urb->status);
 446                break;
 447        }
 448
 449        spin_lock(&dev->slock);
 450        tm6000_isoc_copy(urb);
 451        spin_unlock(&dev->slock);
 452
 453        /* Reset urb buffers */
 454        for (i = 0; i < urb->number_of_packets; i++) {
 455                urb->iso_frame_desc[i].status = 0;
 456                urb->iso_frame_desc[i].actual_length = 0;
 457        }
 458
 459        urb->status = usb_submit_urb(urb, GFP_ATOMIC);
 460        if (urb->status)
 461                tm6000_err("urb resubmit failed (error=%i)\n",
 462                        urb->status);
 463}
 464
 465/*
 466 * Allocate URB buffers
 467 */
 468static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
 469{
 470        int num_bufs = TM6000_NUM_URB_BUF;
 471        int i;
 472
 473        if (dev->urb_buffer)
 474                return 0;
 475
 476        dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
 477        if (!dev->urb_buffer)
 478                return -ENOMEM;
 479
 480        dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
 481        if (!dev->urb_dma)
 482                return -ENOMEM;
 483
 484        for (i = 0; i < num_bufs; i++) {
 485                dev->urb_buffer[i] = usb_alloc_coherent(
 486                                        dev->udev, dev->urb_size,
 487                                        GFP_KERNEL, &dev->urb_dma[i]);
 488                if (!dev->urb_buffer[i]) {
 489                        tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
 490                                    dev->urb_size, i);
 491                        return -ENOMEM;
 492                }
 493                memset(dev->urb_buffer[i], 0, dev->urb_size);
 494        }
 495
 496        return 0;
 497}
 498
 499/*
 500 * Free URB buffers
 501 */
 502static int tm6000_free_urb_buffers(struct tm6000_core *dev)
 503{
 504        int i;
 505
 506        if (!dev->urb_buffer)
 507                return 0;
 508
 509        for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
 510                if (dev->urb_buffer[i]) {
 511                        usb_free_coherent(dev->udev,
 512                                        dev->urb_size,
 513                                        dev->urb_buffer[i],
 514                                        dev->urb_dma[i]);
 515                        dev->urb_buffer[i] = NULL;
 516                }
 517        }
 518        kfree(dev->urb_buffer);
 519        kfree(dev->urb_dma);
 520        dev->urb_buffer = NULL;
 521        dev->urb_dma = NULL;
 522
 523        return 0;
 524}
 525
 526/*
 527 * Stop and Deallocate URBs
 528 */
 529static void tm6000_uninit_isoc(struct tm6000_core *dev)
 530{
 531        struct urb *urb;
 532        int i;
 533
 534        dev->isoc_ctl.buf = NULL;
 535        for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
 536                urb = dev->isoc_ctl.urb[i];
 537                if (urb) {
 538                        usb_kill_urb(urb);
 539                        usb_unlink_urb(urb);
 540                        usb_free_urb(urb);
 541                        dev->isoc_ctl.urb[i] = NULL;
 542                }
 543                dev->isoc_ctl.transfer_buffer[i] = NULL;
 544        }
 545
 546        if (!keep_urb)
 547                tm6000_free_urb_buffers(dev);
 548
 549        kfree(dev->isoc_ctl.urb);
 550        kfree(dev->isoc_ctl.transfer_buffer);
 551
 552        dev->isoc_ctl.urb = NULL;
 553        dev->isoc_ctl.transfer_buffer = NULL;
 554        dev->isoc_ctl.num_bufs = 0;
 555}
 556
 557/*
 558 * Assign URBs and start IRQ
 559 */
 560static int tm6000_prepare_isoc(struct tm6000_core *dev)
 561{
 562        struct tm6000_dmaqueue *dma_q = &dev->vidq;
 563        int i, j, sb_size, pipe, size, max_packets;
 564        int num_bufs = TM6000_NUM_URB_BUF;
 565        struct urb *urb;
 566
 567        /* De-allocates all pending stuff */
 568        tm6000_uninit_isoc(dev);
 569        /* Stop interrupt USB pipe */
 570        tm6000_ir_int_stop(dev);
 571
 572        usb_set_interface(dev->udev,
 573                          dev->isoc_in.bInterfaceNumber,
 574                          dev->isoc_in.bAlternateSetting);
 575
 576        /* Start interrupt USB pipe */
 577        tm6000_ir_int_start(dev);
 578
 579        pipe = usb_rcvisocpipe(dev->udev,
 580                               dev->isoc_in.endp->desc.bEndpointAddress &
 581                               USB_ENDPOINT_NUMBER_MASK);
 582
 583        size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
 584
 585        if (size > dev->isoc_in.maxsize)
 586                size = dev->isoc_in.maxsize;
 587
 588        dev->isoc_ctl.max_pkt_size = size;
 589
 590        max_packets = TM6000_MAX_ISO_PACKETS;
 591        sb_size = max_packets * size;
 592        dev->urb_size = sb_size;
 593
 594        dev->isoc_ctl.num_bufs = num_bufs;
 595
 596        dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
 597        if (!dev->isoc_ctl.urb)
 598                return -ENOMEM;
 599
 600        dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
 601                                   GFP_KERNEL);
 602        if (!dev->isoc_ctl.transfer_buffer) {
 603                kfree(dev->isoc_ctl.urb);
 604                return -ENOMEM;
 605        }
 606
 607        dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
 608                    max_packets, num_bufs, sb_size,
 609                    dev->isoc_in.maxsize, size);
 610
 611
 612        if (tm6000_alloc_urb_buffers(dev) < 0) {
 613                tm6000_err("cannot allocate memory for urb buffers\n");
 614
 615                /* call free, as some buffers might have been allocated */
 616                tm6000_free_urb_buffers(dev);
 617                kfree(dev->isoc_ctl.urb);
 618                kfree(dev->isoc_ctl.transfer_buffer);
 619                return -ENOMEM;
 620        }
 621
 622        /* allocate urbs and transfer buffers */
 623        for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
 624                urb = usb_alloc_urb(max_packets, GFP_KERNEL);
 625                if (!urb) {
 626                        tm6000_uninit_isoc(dev);
 627                        tm6000_free_urb_buffers(dev);
 628                        return -ENOMEM;
 629                }
 630                dev->isoc_ctl.urb[i] = urb;
 631
 632                urb->transfer_dma = dev->urb_dma[i];
 633                dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
 634
 635                usb_fill_bulk_urb(urb, dev->udev, pipe,
 636                                  dev->isoc_ctl.transfer_buffer[i], sb_size,
 637                                  tm6000_irq_callback, dma_q);
 638                urb->interval = dev->isoc_in.endp->desc.bInterval;
 639                urb->number_of_packets = max_packets;
 640                urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
 641
 642                for (j = 0; j < max_packets; j++) {
 643                        urb->iso_frame_desc[j].offset = size * j;
 644                        urb->iso_frame_desc[j].length = size;
 645                }
 646        }
 647
 648        return 0;
 649}
 650
 651static int tm6000_start_thread(struct tm6000_core *dev)
 652{
 653        struct tm6000_dmaqueue *dma_q = &dev->vidq;
 654        int i;
 655
 656        dma_q->frame = 0;
 657        dma_q->ini_jiffies = jiffies;
 658
 659        init_waitqueue_head(&dma_q->wq);
 660
 661        /* submit urbs and enables IRQ */
 662        for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
 663                int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
 664                if (rc) {
 665                        tm6000_err("submit of urb %i failed (error=%i)\n", i,
 666                                   rc);
 667                        tm6000_uninit_isoc(dev);
 668                        return rc;
 669                }
 670        }
 671
 672        return 0;
 673}
 674
 675/* ------------------------------------------------------------------
 676 *      Videobuf operations
 677 * ------------------------------------------------------------------
 678 */
 679
 680static int
 681buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
 682{
 683        struct tm6000_fh *fh = vq->priv_data;
 684
 685        *size = fh->fmt->depth * fh->width * fh->height >> 3;
 686        if (0 == *count)
 687                *count = TM6000_DEF_BUF;
 688
 689        if (*count < TM6000_MIN_BUF)
 690                *count = TM6000_MIN_BUF;
 691
 692        while (*size * *count > vid_limit * 1024 * 1024)
 693                (*count)--;
 694
 695        return 0;
 696}
 697
 698static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
 699{
 700        struct tm6000_fh *fh = vq->priv_data;
 701        struct tm6000_core   *dev = fh->dev;
 702        unsigned long flags;
 703
 704        BUG_ON(in_interrupt());
 705
 706        /* We used to wait for the buffer to finish here, but this didn't work
 707           because, as we were keeping the state as VIDEOBUF_QUEUED,
 708           videobuf_queue_cancel marked it as finished for us.
 709           (Also, it could wedge forever if the hardware was misconfigured.)
 710
 711           This should be safe; by the time we get here, the buffer isn't
 712           queued anymore. If we ever start marking the buffers as
 713           VIDEOBUF_ACTIVE, it won't be, though.
 714        */
 715        spin_lock_irqsave(&dev->slock, flags);
 716        if (dev->isoc_ctl.buf == buf)
 717                dev->isoc_ctl.buf = NULL;
 718        spin_unlock_irqrestore(&dev->slock, flags);
 719
 720        videobuf_vmalloc_free(&buf->vb);
 721        buf->vb.state = VIDEOBUF_NEEDS_INIT;
 722}
 723
 724static int
 725buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
 726                                                enum v4l2_field field)
 727{
 728        struct tm6000_fh     *fh  = vq->priv_data;
 729        struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
 730        struct tm6000_core   *dev = fh->dev;
 731        int rc = 0;
 732
 733        BUG_ON(NULL == fh->fmt);
 734
 735
 736        /* FIXME: It assumes depth=2 */
 737        /* The only currently supported format is 16 bits/pixel */
 738        buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
 739        if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
 740                return -EINVAL;
 741
 742        if (buf->fmt       != fh->fmt    ||
 743            buf->vb.width  != fh->width  ||
 744            buf->vb.height != fh->height ||
 745            buf->vb.field  != field) {
 746                buf->fmt       = fh->fmt;
 747                buf->vb.width  = fh->width;
 748                buf->vb.height = fh->height;
 749                buf->vb.field  = field;
 750                buf->vb.state = VIDEOBUF_NEEDS_INIT;
 751        }
 752
 753        if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
 754                rc = videobuf_iolock(vq, &buf->vb, NULL);
 755                if (rc != 0)
 756                        goto fail;
 757        }
 758
 759        if (!dev->isoc_ctl.num_bufs) {
 760                rc = tm6000_prepare_isoc(dev);
 761                if (rc < 0)
 762                        goto fail;
 763
 764                rc = tm6000_start_thread(dev);
 765                if (rc < 0)
 766                        goto fail;
 767
 768        }
 769
 770        buf->vb.state = VIDEOBUF_PREPARED;
 771        return 0;
 772
 773fail:
 774        free_buffer(vq, buf);
 775        return rc;
 776}
 777
 778static void
 779buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
 780{
 781        struct tm6000_buffer    *buf     = container_of(vb, struct tm6000_buffer, vb);
 782        struct tm6000_fh        *fh      = vq->priv_data;
 783        struct tm6000_core      *dev     = fh->dev;
 784        struct tm6000_dmaqueue  *vidq    = &dev->vidq;
 785
 786        buf->vb.state = VIDEOBUF_QUEUED;
 787        list_add_tail(&buf->vb.queue, &vidq->active);
 788}
 789
 790static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
 791{
 792        struct tm6000_buffer   *buf  = container_of(vb, struct tm6000_buffer, vb);
 793
 794        free_buffer(vq, buf);
 795}
 796
 797static const struct videobuf_queue_ops tm6000_video_qops = {
 798        .buf_setup      = buffer_setup,
 799        .buf_prepare    = buffer_prepare,
 800        .buf_queue      = buffer_queue,
 801        .buf_release    = buffer_release,
 802};
 803
 804/* ------------------------------------------------------------------
 805 *      IOCTL handling
 806 * ------------------------------------------------------------------
 807 */
 808
 809static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
 810{
 811        /* Is the current fh handling it? if so, that's OK */
 812        if (dev->resources == fh && dev->is_res_read)
 813                return true;
 814
 815        return false;
 816}
 817
 818static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
 819{
 820        /* Is the current fh handling it? if so, that's OK */
 821        if (dev->resources == fh)
 822                return true;
 823
 824        return false;
 825}
 826
 827static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
 828                   bool is_res_read)
 829{
 830        /* Is the current fh handling it? if so, that's OK */
 831        if (dev->resources == fh && dev->is_res_read == is_res_read)
 832                return true;
 833
 834        /* is it free? */
 835        if (dev->resources)
 836                return false;
 837
 838        /* grab it */
 839        dev->resources = fh;
 840        dev->is_res_read = is_res_read;
 841        dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
 842        return true;
 843}
 844
 845static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
 846{
 847        /* Is the current fh handling it? if so, that's OK */
 848        if (dev->resources != fh)
 849                return;
 850
 851        dev->resources = NULL;
 852        dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
 853}
 854
 855/* ------------------------------------------------------------------
 856 *      IOCTL vidioc handling
 857 * ------------------------------------------------------------------
 858 */
 859static int vidioc_querycap(struct file *file, void  *priv,
 860                                        struct v4l2_capability *cap)
 861{
 862        struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
 863        struct video_device *vdev = video_devdata(file);
 864
 865        strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
 866        strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
 867        usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
 868        if (dev->tuner_type != TUNER_ABSENT)
 869                cap->device_caps |= V4L2_CAP_TUNER;
 870        if (vdev->vfl_type == VFL_TYPE_GRABBER)
 871                cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
 872                                V4L2_CAP_STREAMING |
 873                                V4L2_CAP_READWRITE;
 874        else
 875                cap->device_caps |= V4L2_CAP_RADIO;
 876        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
 877                V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
 878
 879        return 0;
 880}
 881
 882static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 883                                        struct v4l2_fmtdesc *f)
 884{
 885        if (f->index >= ARRAY_SIZE(format))
 886                return -EINVAL;
 887
 888        strlcpy(f->description, format[f->index].name, sizeof(f->description));
 889        f->pixelformat = format[f->index].fourcc;
 890        return 0;
 891}
 892
 893static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 894                                        struct v4l2_format *f)
 895{
 896        struct tm6000_fh  *fh = priv;
 897
 898        f->fmt.pix.width        = fh->width;
 899        f->fmt.pix.height       = fh->height;
 900        f->fmt.pix.field        = fh->vb_vidq.field;
 901        f->fmt.pix.pixelformat  = fh->fmt->fourcc;
 902        f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
 903        f->fmt.pix.bytesperline =
 904                (f->fmt.pix.width * fh->fmt->depth) >> 3;
 905        f->fmt.pix.sizeimage =
 906                f->fmt.pix.height * f->fmt.pix.bytesperline;
 907
 908        return 0;
 909}
 910
 911static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
 912{
 913        unsigned int i;
 914
 915        for (i = 0; i < ARRAY_SIZE(format); i++)
 916                if (format[i].fourcc == fourcc)
 917                        return format+i;
 918        return NULL;
 919}
 920
 921static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 922                        struct v4l2_format *f)
 923{
 924        struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
 925        struct tm6000_fmt *fmt;
 926        enum v4l2_field field;
 927
 928        fmt = format_by_fourcc(f->fmt.pix.pixelformat);
 929        if (NULL == fmt) {
 930                dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
 931                        f->fmt.pix.pixelformat);
 932                return -EINVAL;
 933        }
 934
 935        field = f->fmt.pix.field;
 936
 937        field = V4L2_FIELD_INTERLACED;
 938
 939        tm6000_get_std_res(dev);
 940
 941        f->fmt.pix.width  = dev->width;
 942        f->fmt.pix.height = dev->height;
 943
 944        f->fmt.pix.width &= ~0x01;
 945
 946        f->fmt.pix.field = field;
 947
 948        f->fmt.pix.bytesperline =
 949                (f->fmt.pix.width * fmt->depth) >> 3;
 950        f->fmt.pix.sizeimage =
 951                f->fmt.pix.height * f->fmt.pix.bytesperline;
 952        f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
 953
 954        return 0;
 955}
 956
 957/*FIXME: This seems to be generic enough to be at videodev2 */
 958static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 959                                        struct v4l2_format *f)
 960{
 961        struct tm6000_fh  *fh = priv;
 962        struct tm6000_core *dev = fh->dev;
 963        int ret = vidioc_try_fmt_vid_cap(file, fh, f);
 964        if (ret < 0)
 965                return ret;
 966
 967        fh->fmt           = format_by_fourcc(f->fmt.pix.pixelformat);
 968        fh->width         = f->fmt.pix.width;
 969        fh->height        = f->fmt.pix.height;
 970        fh->vb_vidq.field = f->fmt.pix.field;
 971        fh->type          = f->type;
 972
 973        dev->fourcc       = f->fmt.pix.pixelformat;
 974
 975        tm6000_set_fourcc_format(dev);
 976
 977        return 0;
 978}
 979
 980static int vidioc_reqbufs(struct file *file, void *priv,
 981                           struct v4l2_requestbuffers *p)
 982{
 983        struct tm6000_fh  *fh = priv;
 984
 985        return videobuf_reqbufs(&fh->vb_vidq, p);
 986}
 987
 988static int vidioc_querybuf(struct file *file, void *priv,
 989                            struct v4l2_buffer *p)
 990{
 991        struct tm6000_fh  *fh = priv;
 992
 993        return videobuf_querybuf(&fh->vb_vidq, p);
 994}
 995
 996static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
 997{
 998        struct tm6000_fh  *fh = priv;
 999
1000        return videobuf_qbuf(&fh->vb_vidq, p);
1001}
1002
1003static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1004{
1005        struct tm6000_fh  *fh = priv;
1006
1007        return videobuf_dqbuf(&fh->vb_vidq, p,
1008                                file->f_flags & O_NONBLOCK);
1009}
1010
1011static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1012{
1013        struct tm6000_fh *fh = priv;
1014        struct tm6000_core *dev = fh->dev;
1015
1016        if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1017                return -EINVAL;
1018        if (i != fh->type)
1019                return -EINVAL;
1020
1021        if (!res_get(dev, fh, false))
1022                return -EBUSY;
1023        return videobuf_streamon(&fh->vb_vidq);
1024}
1025
1026static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1027{
1028        struct tm6000_fh *fh = priv;
1029        struct tm6000_core *dev = fh->dev;
1030
1031        if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1032                return -EINVAL;
1033
1034        if (i != fh->type)
1035                return -EINVAL;
1036
1037        videobuf_streamoff(&fh->vb_vidq);
1038        res_free(dev, fh);
1039
1040        return 0;
1041}
1042
1043static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1044{
1045        int rc = 0;
1046        struct tm6000_fh *fh = priv;
1047        struct tm6000_core *dev = fh->dev;
1048
1049        dev->norm = norm;
1050        rc = tm6000_init_analog_mode(dev);
1051
1052        fh->width  = dev->width;
1053        fh->height = dev->height;
1054
1055        if (rc < 0)
1056                return rc;
1057
1058        v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
1059
1060        return 0;
1061}
1062
1063static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1064{
1065        struct tm6000_fh *fh = priv;
1066        struct tm6000_core *dev = fh->dev;
1067
1068        *norm = dev->norm;
1069        return 0;
1070}
1071
1072static const char *iname[] = {
1073        [TM6000_INPUT_TV] = "Television",
1074        [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1075        [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1076        [TM6000_INPUT_SVIDEO] = "S-Video",
1077};
1078
1079static int vidioc_enum_input(struct file *file, void *priv,
1080                                struct v4l2_input *i)
1081{
1082        struct tm6000_fh   *fh = priv;
1083        struct tm6000_core *dev = fh->dev;
1084        unsigned int n;
1085
1086        n = i->index;
1087        if (n >= 3)
1088                return -EINVAL;
1089
1090        if (!dev->vinput[n].type)
1091                return -EINVAL;
1092
1093        i->index = n;
1094
1095        if (dev->vinput[n].type == TM6000_INPUT_TV)
1096                i->type = V4L2_INPUT_TYPE_TUNER;
1097        else
1098                i->type = V4L2_INPUT_TYPE_CAMERA;
1099
1100        strcpy(i->name, iname[dev->vinput[n].type]);
1101
1102        i->std = TM6000_STD;
1103
1104        return 0;
1105}
1106
1107static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1108{
1109        struct tm6000_fh   *fh = priv;
1110        struct tm6000_core *dev = fh->dev;
1111
1112        *i = dev->input;
1113
1114        return 0;
1115}
1116
1117static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1118{
1119        struct tm6000_fh   *fh = priv;
1120        struct tm6000_core *dev = fh->dev;
1121        int rc = 0;
1122
1123        if (i >= 3)
1124                return -EINVAL;
1125        if (!dev->vinput[i].type)
1126                return -EINVAL;
1127
1128        dev->input = i;
1129
1130        rc = vidioc_s_std(file, priv, dev->norm);
1131
1132        return rc;
1133}
1134
1135/* --- controls ---------------------------------------------- */
1136
1137static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
1138{
1139        struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1140        u8  val = ctrl->val;
1141
1142        switch (ctrl->id) {
1143        case V4L2_CID_CONTRAST:
1144                tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1145                return 0;
1146        case V4L2_CID_BRIGHTNESS:
1147                tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1148                return 0;
1149        case V4L2_CID_SATURATION:
1150                tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1151                return 0;
1152        case V4L2_CID_HUE:
1153                tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1154                return 0;
1155        }
1156        return -EINVAL;
1157}
1158
1159static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1160        .s_ctrl = tm6000_s_ctrl,
1161};
1162
1163static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1164{
1165        struct tm6000_core *dev = container_of(ctrl->handler,
1166                        struct tm6000_core, radio_ctrl_handler);
1167        u8  val = ctrl->val;
1168
1169        switch (ctrl->id) {
1170        case V4L2_CID_AUDIO_MUTE:
1171                dev->ctl_mute = val;
1172                tm6000_tvaudio_set_mute(dev, val);
1173                return 0;
1174        case V4L2_CID_AUDIO_VOLUME:
1175                dev->ctl_volume = val;
1176                tm6000_set_volume(dev, val);
1177                return 0;
1178        }
1179        return -EINVAL;
1180}
1181
1182static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1183        .s_ctrl = tm6000_radio_s_ctrl,
1184};
1185
1186static int vidioc_g_tuner(struct file *file, void *priv,
1187                                struct v4l2_tuner *t)
1188{
1189        struct tm6000_fh   *fh  = priv;
1190        struct tm6000_core *dev = fh->dev;
1191
1192        if (UNSET == dev->tuner_type)
1193                return -ENOTTY;
1194        if (0 != t->index)
1195                return -EINVAL;
1196
1197        strcpy(t->name, "Television");
1198        t->type       = V4L2_TUNER_ANALOG_TV;
1199        t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
1200        t->rangehigh  = 0xffffffffUL;
1201        t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1202
1203        v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1204
1205        t->audmode = dev->amode;
1206
1207        return 0;
1208}
1209
1210static int vidioc_s_tuner(struct file *file, void *priv,
1211                                const struct v4l2_tuner *t)
1212{
1213        struct tm6000_fh   *fh  = priv;
1214        struct tm6000_core *dev = fh->dev;
1215
1216        if (UNSET == dev->tuner_type)
1217                return -ENOTTY;
1218        if (0 != t->index)
1219                return -EINVAL;
1220
1221        if (t->audmode > V4L2_TUNER_MODE_STEREO)
1222                dev->amode = V4L2_TUNER_MODE_STEREO;
1223        else
1224                dev->amode = t->audmode;
1225        dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1226
1227        v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1228
1229        return 0;
1230}
1231
1232static int vidioc_g_frequency(struct file *file, void *priv,
1233                                struct v4l2_frequency *f)
1234{
1235        struct tm6000_fh   *fh  = priv;
1236        struct tm6000_core *dev = fh->dev;
1237
1238        if (UNSET == dev->tuner_type)
1239                return -ENOTTY;
1240        if (f->tuner)
1241                return -EINVAL;
1242
1243        f->frequency = dev->freq;
1244
1245        v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1246
1247        return 0;
1248}
1249
1250static int vidioc_s_frequency(struct file *file, void *priv,
1251                                const struct v4l2_frequency *f)
1252{
1253        struct tm6000_fh   *fh  = priv;
1254        struct tm6000_core *dev = fh->dev;
1255
1256        if (UNSET == dev->tuner_type)
1257                return -ENOTTY;
1258        if (f->tuner != 0)
1259                return -EINVAL;
1260
1261        dev->freq = f->frequency;
1262        v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1263
1264        return 0;
1265}
1266
1267static int radio_g_tuner(struct file *file, void *priv,
1268                                        struct v4l2_tuner *t)
1269{
1270        struct tm6000_fh *fh = file->private_data;
1271        struct tm6000_core *dev = fh->dev;
1272
1273        if (0 != t->index)
1274                return -EINVAL;
1275
1276        memset(t, 0, sizeof(*t));
1277        strcpy(t->name, "Radio");
1278        t->type = V4L2_TUNER_RADIO;
1279        t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1280        t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1281        t->audmode = V4L2_TUNER_MODE_STEREO;
1282
1283        v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1284
1285        return 0;
1286}
1287
1288static int radio_s_tuner(struct file *file, void *priv,
1289                                        const struct v4l2_tuner *t)
1290{
1291        struct tm6000_fh *fh = file->private_data;
1292        struct tm6000_core *dev = fh->dev;
1293
1294        if (0 != t->index)
1295                return -EINVAL;
1296        v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1297        return 0;
1298}
1299
1300/* ------------------------------------------------------------------
1301        File operations for the device
1302   ------------------------------------------------------------------*/
1303
1304static int __tm6000_open(struct file *file)
1305{
1306        struct video_device *vdev = video_devdata(file);
1307        struct tm6000_core *dev = video_drvdata(file);
1308        struct tm6000_fh *fh;
1309        enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1310        int rc;
1311        int radio = 0;
1312
1313        dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1314                video_device_node_name(vdev));
1315
1316        switch (vdev->vfl_type) {
1317        case VFL_TYPE_GRABBER:
1318                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1319                break;
1320        case VFL_TYPE_VBI:
1321                type = V4L2_BUF_TYPE_VBI_CAPTURE;
1322                break;
1323        case VFL_TYPE_RADIO:
1324                radio = 1;
1325                break;
1326        }
1327
1328        /* If more than one user, mutex should be added */
1329        dev->users++;
1330
1331        dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1332                video_device_node_name(vdev), v4l2_type_names[type],
1333                dev->users);
1334
1335        /* allocate + initialize per filehandle data */
1336        fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1337        if (NULL == fh) {
1338                dev->users--;
1339                return -ENOMEM;
1340        }
1341
1342        v4l2_fh_init(&fh->fh, vdev);
1343        file->private_data = fh;
1344        fh->dev      = dev;
1345        fh->radio    = radio;
1346        dev->radio   = radio;
1347        fh->type     = type;
1348        dev->fourcc  = format[0].fourcc;
1349
1350        fh->fmt      = format_by_fourcc(dev->fourcc);
1351
1352        tm6000_get_std_res(dev);
1353
1354        fh->width = dev->width;
1355        fh->height = dev->height;
1356
1357        dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1358                        (unsigned long)fh, (unsigned long)dev,
1359                        (unsigned long)&dev->vidq);
1360        dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
1361                list_empty(&dev->vidq.queued));
1362        dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
1363                list_empty(&dev->vidq.active));
1364
1365        /* initialize hardware on analog mode */
1366        rc = tm6000_init_analog_mode(dev);
1367        if (rc < 0) {
1368                v4l2_fh_exit(&fh->fh);
1369                kfree(fh);
1370                return rc;
1371        }
1372
1373        dev->mode = TM6000_MODE_ANALOG;
1374
1375        if (!fh->radio) {
1376                videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1377                                NULL, &dev->slock,
1378                                fh->type,
1379                                V4L2_FIELD_INTERLACED,
1380                                sizeof(struct tm6000_buffer), fh, &dev->lock);
1381        } else {
1382                dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
1383                tm6000_set_audio_rinput(dev);
1384                v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1385                tm6000_prepare_isoc(dev);
1386                tm6000_start_thread(dev);
1387        }
1388        v4l2_fh_add(&fh->fh);
1389
1390        return 0;
1391}
1392
1393static int tm6000_open(struct file *file)
1394{
1395        struct video_device *vdev = video_devdata(file);
1396        int res;
1397
1398        mutex_lock(vdev->lock);
1399        res = __tm6000_open(file);
1400        mutex_unlock(vdev->lock);
1401        return res;
1402}
1403
1404static ssize_t
1405tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1406{
1407        struct tm6000_fh *fh = file->private_data;
1408        struct tm6000_core *dev = fh->dev;
1409
1410        if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1411                int res;
1412
1413                if (!res_get(fh->dev, fh, true))
1414                        return -EBUSY;
1415
1416                if (mutex_lock_interruptible(&dev->lock))
1417                        return -ERESTARTSYS;
1418                res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1419                                        file->f_flags & O_NONBLOCK);
1420                mutex_unlock(&dev->lock);
1421                return res;
1422        }
1423        return 0;
1424}
1425
1426static unsigned int
1427__tm6000_poll(struct file *file, struct poll_table_struct *wait)
1428{
1429        unsigned long req_events = poll_requested_events(wait);
1430        struct tm6000_fh        *fh = file->private_data;
1431        struct tm6000_buffer    *buf;
1432        int res = 0;
1433
1434        if (v4l2_event_pending(&fh->fh))
1435                res = POLLPRI;
1436        else if (req_events & POLLPRI)
1437                poll_wait(file, &fh->fh.wait, wait);
1438        if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1439                return res | POLLERR;
1440
1441        if (!!is_res_streaming(fh->dev, fh))
1442                return res | POLLERR;
1443
1444        if (!is_res_read(fh->dev, fh)) {
1445                /* streaming capture */
1446                if (list_empty(&fh->vb_vidq.stream))
1447                        return res | POLLERR;
1448                buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
1449                poll_wait(file, &buf->vb.done, wait);
1450                if (buf->vb.state == VIDEOBUF_DONE ||
1451                    buf->vb.state == VIDEOBUF_ERROR)
1452                        return res | POLLIN | POLLRDNORM;
1453        } else if (req_events & (POLLIN | POLLRDNORM)) {
1454                /* read() capture */
1455                return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
1456        }
1457        return res;
1458}
1459
1460static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1461{
1462        struct tm6000_fh *fh = file->private_data;
1463        struct tm6000_core *dev = fh->dev;
1464        unsigned int res;
1465
1466        mutex_lock(&dev->lock);
1467        res = __tm6000_poll(file, wait);
1468        mutex_unlock(&dev->lock);
1469        return res;
1470}
1471
1472static int tm6000_release(struct file *file)
1473{
1474        struct tm6000_fh         *fh = file->private_data;
1475        struct tm6000_core      *dev = fh->dev;
1476        struct video_device    *vdev = video_devdata(file);
1477
1478        dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1479                video_device_node_name(vdev), dev->users);
1480
1481        mutex_lock(&dev->lock);
1482        dev->users--;
1483
1484        res_free(dev, fh);
1485
1486        if (!dev->users) {
1487                tm6000_uninit_isoc(dev);
1488
1489                /* Stop interrupt USB pipe */
1490                tm6000_ir_int_stop(dev);
1491
1492                usb_reset_configuration(dev->udev);
1493
1494                if (dev->int_in.endp)
1495                        usb_set_interface(dev->udev,
1496                                        dev->isoc_in.bInterfaceNumber, 2);
1497                else
1498                        usb_set_interface(dev->udev,
1499                                        dev->isoc_in.bInterfaceNumber, 0);
1500
1501                /* Start interrupt USB pipe */
1502                tm6000_ir_int_start(dev);
1503
1504                if (!fh->radio)
1505                        videobuf_mmap_free(&fh->vb_vidq);
1506        }
1507        v4l2_fh_del(&fh->fh);
1508        v4l2_fh_exit(&fh->fh);
1509        kfree(fh);
1510        mutex_unlock(&dev->lock);
1511
1512        return 0;
1513}
1514
1515static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1516{
1517        struct tm6000_fh *fh = file->private_data;
1518        struct tm6000_core *dev = fh->dev;
1519        int res;
1520
1521        if (mutex_lock_interruptible(&dev->lock))
1522                return -ERESTARTSYS;
1523        res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1524        mutex_unlock(&dev->lock);
1525        return res;
1526}
1527
1528static const struct v4l2_file_operations tm6000_fops = {
1529        .owner = THIS_MODULE,
1530        .open = tm6000_open,
1531        .release = tm6000_release,
1532        .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1533        .read = tm6000_read,
1534        .poll = tm6000_poll,
1535        .mmap = tm6000_mmap,
1536};
1537
1538static const struct v4l2_ioctl_ops video_ioctl_ops = {
1539        .vidioc_querycap          = vidioc_querycap,
1540        .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1541        .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1542        .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1543        .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1544        .vidioc_s_std             = vidioc_s_std,
1545        .vidioc_g_std             = vidioc_g_std,
1546        .vidioc_enum_input        = vidioc_enum_input,
1547        .vidioc_g_input           = vidioc_g_input,
1548        .vidioc_s_input           = vidioc_s_input,
1549        .vidioc_g_tuner           = vidioc_g_tuner,
1550        .vidioc_s_tuner           = vidioc_s_tuner,
1551        .vidioc_g_frequency       = vidioc_g_frequency,
1552        .vidioc_s_frequency       = vidioc_s_frequency,
1553        .vidioc_streamon          = vidioc_streamon,
1554        .vidioc_streamoff         = vidioc_streamoff,
1555        .vidioc_reqbufs           = vidioc_reqbufs,
1556        .vidioc_querybuf          = vidioc_querybuf,
1557        .vidioc_qbuf              = vidioc_qbuf,
1558        .vidioc_dqbuf             = vidioc_dqbuf,
1559        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1560        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1561};
1562
1563static struct video_device tm6000_template = {
1564        .name           = "tm6000",
1565        .fops           = &tm6000_fops,
1566        .ioctl_ops      = &video_ioctl_ops,
1567        .release        = video_device_release_empty,
1568        .tvnorms        = TM6000_STD,
1569};
1570
1571static const struct v4l2_file_operations radio_fops = {
1572        .owner          = THIS_MODULE,
1573        .open           = tm6000_open,
1574        .poll           = v4l2_ctrl_poll,
1575        .release        = tm6000_release,
1576        .unlocked_ioctl = video_ioctl2,
1577};
1578
1579static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1580        .vidioc_querycap        = vidioc_querycap,
1581        .vidioc_g_tuner         = radio_g_tuner,
1582        .vidioc_s_tuner         = radio_s_tuner,
1583        .vidioc_g_frequency     = vidioc_g_frequency,
1584        .vidioc_s_frequency     = vidioc_s_frequency,
1585        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1586        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1587};
1588
1589static struct video_device tm6000_radio_template = {
1590        .name                   = "tm6000",
1591        .fops                   = &radio_fops,
1592        .ioctl_ops              = &radio_ioctl_ops,
1593};
1594
1595/* -----------------------------------------------------------------
1596 *      Initialization and module stuff
1597 * ------------------------------------------------------------------
1598 */
1599
1600static void vdev_init(struct tm6000_core *dev,
1601                struct video_device *vfd,
1602                const struct video_device
1603                *template, const char *type_name)
1604{
1605        *vfd = *template;
1606        vfd->v4l2_dev = &dev->v4l2_dev;
1607        vfd->release = video_device_release_empty;
1608        vfd->lock = &dev->lock;
1609
1610        snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1611
1612        video_set_drvdata(vfd, dev);
1613}
1614
1615int tm6000_v4l2_register(struct tm6000_core *dev)
1616{
1617        int ret = 0;
1618
1619        v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1620        v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1621        v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1622                        V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1623        v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1624                        V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1625        v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1626                        V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1627        v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1628                        V4L2_CID_CONTRAST, 0, 255, 1, 119);
1629        v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1630                        V4L2_CID_SATURATION, 0, 255, 1, 112);
1631        v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1632                        V4L2_CID_HUE, -128, 127, 1, 0);
1633        v4l2_ctrl_add_handler(&dev->ctrl_handler,
1634                        &dev->radio_ctrl_handler, NULL);
1635
1636        if (dev->radio_ctrl_handler.error)
1637                ret = dev->radio_ctrl_handler.error;
1638        if (!ret && dev->ctrl_handler.error)
1639                ret = dev->ctrl_handler.error;
1640        if (ret)
1641                goto free_ctrl;
1642
1643        vdev_init(dev, &dev->vfd, &tm6000_template, "video");
1644
1645        dev->vfd.ctrl_handler = &dev->ctrl_handler;
1646
1647        /* init video dma queues */
1648        INIT_LIST_HEAD(&dev->vidq.active);
1649        INIT_LIST_HEAD(&dev->vidq.queued);
1650
1651        ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
1652
1653        if (ret < 0) {
1654                printk(KERN_INFO "%s: can't register video device\n",
1655                       dev->name);
1656                goto free_ctrl;
1657        }
1658
1659        printk(KERN_INFO "%s: registered device %s\n",
1660               dev->name, video_device_node_name(&dev->vfd));
1661
1662        if (dev->caps.has_radio) {
1663                vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
1664                                                           "radio");
1665                dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1666                ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
1667                                            radio_nr);
1668                if (ret < 0) {
1669                        printk(KERN_INFO "%s: can't register radio device\n",
1670                               dev->name);
1671                        goto unreg_video;
1672                }
1673
1674                printk(KERN_INFO "%s: registered device %s\n",
1675                       dev->name, video_device_node_name(&dev->radio_dev));
1676        }
1677
1678        printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1679        return ret;
1680
1681unreg_video:
1682        video_unregister_device(&dev->vfd);
1683free_ctrl:
1684        v4l2_ctrl_handler_free(&dev->ctrl_handler);
1685        v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1686        return ret;
1687}
1688
1689int tm6000_v4l2_unregister(struct tm6000_core *dev)
1690{
1691        video_unregister_device(&dev->vfd);
1692
1693        /* if URB buffers are still allocated free them now */
1694        tm6000_free_urb_buffers(dev);
1695
1696        video_unregister_device(&dev->radio_dev);
1697        return 0;
1698}
1699
1700int tm6000_v4l2_exit(void)
1701{
1702        return 0;
1703}
1704
1705module_param(video_nr, int, 0);
1706MODULE_PARM_DESC(video_nr, "Allow changing video device number");
1707
1708module_param_named(debug, tm6000_debug, int, 0444);
1709MODULE_PARM_DESC(debug, "activates debug info");
1710
1711module_param(vid_limit, int, 0644);
1712MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
1713
1714module_param(keep_urb, bool, 0);
1715MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");
1716