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