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