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