root/drivers/media/usb/tm6000/tm6000-video.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_next_buf
  2. buffer_filled
  3. copy_streams
  4. copy_multiplexed
  5. print_err_status
  6. tm6000_isoc_copy
  7. tm6000_irq_callback
  8. tm6000_alloc_urb_buffers
  9. tm6000_free_urb_buffers
  10. tm6000_uninit_isoc
  11. tm6000_prepare_isoc
  12. tm6000_start_thread
  13. buffer_setup
  14. free_buffer
  15. buffer_prepare
  16. buffer_queue
  17. buffer_release
  18. is_res_read
  19. is_res_streaming
  20. res_get
  21. res_free
  22. vidioc_querycap
  23. vidioc_enum_fmt_vid_cap
  24. vidioc_g_fmt_vid_cap
  25. format_by_fourcc
  26. vidioc_try_fmt_vid_cap
  27. vidioc_s_fmt_vid_cap
  28. vidioc_reqbufs
  29. vidioc_querybuf
  30. vidioc_qbuf
  31. vidioc_dqbuf
  32. vidioc_streamon
  33. vidioc_streamoff
  34. vidioc_s_std
  35. vidioc_g_std
  36. vidioc_enum_input
  37. vidioc_g_input
  38. vidioc_s_input
  39. tm6000_s_ctrl
  40. tm6000_radio_s_ctrl
  41. vidioc_g_tuner
  42. vidioc_s_tuner
  43. vidioc_g_frequency
  44. vidioc_s_frequency
  45. radio_g_tuner
  46. radio_s_tuner
  47. __tm6000_open
  48. tm6000_open
  49. tm6000_read
  50. __tm6000_poll
  51. tm6000_poll
  52. tm6000_release
  53. tm6000_mmap
  54. vdev_init
  55. tm6000_v4l2_register
  56. tm6000_v4l2_unregister
  57. tm6000_v4l2_exit

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

/* [<][>][^][v][top][bottom][index][help] */