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