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