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