V4L/DVB (12794): tm6000: handle also PAL/SECAM resolutions
[linux-block.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,
569 sb_size, GFP_KERNEL,
570 &dev->isoc_ctl.urb[i]->transfer_dma);
571 if (!dev->isoc_ctl.transfer_buffer[i]) {
572 tm6000_err ("unable to allocate %i bytes for transfer"
573 " buffer %i\n", sb_size, i);
574 tm6000_uninit_isoc(dev);
575 return -ENOMEM;
576 }
577 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
578
579 pipe=usb_rcvisocpipe(dev->udev,
580 dev->isoc_in->desc.bEndpointAddress &
581 USB_ENDPOINT_NUMBER_MASK);
582 usb_fill_int_urb(urb, dev->udev, pipe,
583 dev->isoc_ctl.transfer_buffer[i],sb_size,
584 tm6000_irq_callback, dma_q,
585 dev->isoc_in->desc.bInterval);
586
587 urb->number_of_packets = max_packets;
588 urb->transfer_flags = URB_ISO_ASAP;
589
590 k = 0;
591 for (j = 0; j < max_packets; j++) {
592 urb->iso_frame_desc[j].offset = k;
593 urb->iso_frame_desc[j].length =
594 dev->isoc_ctl.max_pkt_size;
595 k += dev->isoc_ctl.max_pkt_size;
596 }
597 }
598
599 return 0;
600}
601
602static int tm6000_start_thread( struct tm6000_dmaqueue *dma_q,
603 struct tm6000_buffer *buf)
604{
605 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
606 int i,rc;
607
608 dma_q->frame=0;
609 dma_q->ini_jiffies=jiffies;
610
611 init_waitqueue_head(&dma_q->wq);
612
613 /* submit urbs and enables IRQ */
614 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
2cd4fd1e 615 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
9701dc94
MCC
616 if (rc) {
617 tm6000_err("submit of urb %i failed (error=%i)\n", i,
618 rc);
619 tm6000_uninit_isoc(dev);
620 return rc;
621 }
622 }
623
624 if (rc<0)
625 return rc;
626
627 return 0;
628}
629
630static int restart_video_queue(struct tm6000_dmaqueue *dma_q)
631{
632 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
633
634 struct tm6000_buffer *buf, *prev;
635 struct list_head *item;
636
637 dprintk(dev, V4L2_DEBUG_QUEUE, "%s dma_q=0x%08lx\n",
638 __FUNCTION__,(unsigned long)dma_q);
639
640 if (!list_empty(&dma_q->active)) {
641 buf = list_entry(dma_q->active.next, struct tm6000_buffer, vb.queue);
642 dprintk(dev, V4L2_DEBUG_QUEUE,
643 "restart_queue [%p/%d]: restart dma\n", buf, buf->vb.i);
644
645 dprintk(dev, V4L2_DEBUG_QUEUE, "Restarting video dma\n");
646 tm6000_stop_thread(dma_q);
647 tm6000_start_thread(dma_q, buf);
648
649 /* cancel all outstanding capture / vbi requests */
650 list_for_each(item,&dma_q->active) {
651 buf = list_entry(item, struct tm6000_buffer, vb.queue);
652
653 list_del(&buf->vb.queue);
654 buf->vb.state = STATE_ERROR;
655 wake_up(&buf->vb.done);
656 }
657 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
658
659 return 0;
660 }
661
662 prev = NULL;
663 for (;;) {
664 if (list_empty(&dma_q->queued))
665 return 0;
666 buf = list_entry(dma_q->queued.next, struct tm6000_buffer, vb.queue);
667 if (NULL == prev) {
668 list_del(&buf->vb.queue);
669 list_add_tail(&buf->vb.queue,&dma_q->active);
670
671 dprintk(dev, V4L2_DEBUG_QUEUE, "Restarting video dma\n");
672 tm6000_stop_thread(dma_q);
673 tm6000_start_thread(dma_q, buf);
674
675 buf->vb.state = STATE_ACTIVE;
676 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
677 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] restart_queue -"
678 " first active\n", buf, buf->vb.i);
679
680 } else if (prev->vb.width == buf->vb.width &&
681 prev->vb.height == buf->vb.height &&
682 prev->fmt == buf->fmt) {
683 list_del(&buf->vb.queue);
684 list_add_tail(&buf->vb.queue,&dma_q->active);
685 buf->vb.state = STATE_ACTIVE;
686 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] restart_queue -"
687 " move to active\n",buf,buf->vb.i);
688 } else {
689 return 0;
690 }
691 prev = buf;
692 }
693}
694
695static void tm6000_vid_timeout(unsigned long data)
696{
697 struct tm6000_core *dev = (struct tm6000_core*)data;
698 struct tm6000_dmaqueue *vidq = &dev->vidq;
699 struct tm6000_buffer *buf;
700 unsigned long flags;
701
702 spin_lock_irqsave(&dev->slock,flags);
703 while (!list_empty(&vidq->active)) {
704 buf = list_entry(vidq->active.next, struct tm6000_buffer,
705 vb.queue);
706 list_del(&buf->vb.queue);
707 buf->vb.state = STATE_ERROR;
708 wake_up(&buf->vb.done);
709 dprintk(dev, V4L2_DEBUG_QUEUE, "tm6000/0: [%p/%d] timeout\n",
710 buf, buf->vb.i);
711 }
712
713 restart_video_queue(vidq);
714 spin_unlock_irqrestore(&dev->slock,flags);
715}
716
717/* ------------------------------------------------------------------
718 Videobuf operations
719 ------------------------------------------------------------------*/
95a83824 720
9701dc94
MCC
721static int
722buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
723{
724 struct tm6000_fh *fh = vq->priv_data;
725
726 *size = fh->fmt->depth * fh->width * fh->height >> 3;
727 if (0 == *count)
95a83824
ML
728 *count = TM6000_DEF_BUF;
729
730 if (*count < TM6000_MIN_BUF) {
731 *count=TM6000_MIN_BUF;
732 }
733
9701dc94
MCC
734 while (*size * *count > vid_limit * 1024 * 1024)
735 (*count)--;
95a83824 736
9701dc94
MCC
737 return 0;
738}
739
740static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
741{
742 if (in_interrupt())
743 BUG();
744
745 videobuf_waiton(&buf->vb,0,0);
746 videobuf_vmalloc_free(&buf->vb);
747 buf->vb.state = STATE_NEEDS_INIT;
748}
749
750static int
751buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
752 enum v4l2_field field)
753{
754 struct tm6000_fh *fh = vq->priv_data;
755 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
756 struct tm6000_core *dev = fh->dev;
757 int rc=0, urbsize, urb_init=0;
758
759 BUG_ON(NULL == fh->fmt);
760
9701dc94
MCC
761
762 /* FIXME: It assumes depth=2 */
763 /* The only currently supported format is 16 bits/pixel */
764 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
765 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
766 return -EINVAL;
767
768 if (buf->fmt != fh->fmt ||
769 buf->vb.width != fh->width ||
770 buf->vb.height != fh->height ||
771 buf->vb.field != field) {
772 buf->fmt = fh->fmt;
773 buf->vb.width = fh->width;
774 buf->vb.height = fh->height;
775 buf->vb.field = field;
776 buf->vb.state = STATE_NEEDS_INIT;
777 }
778
779 if (STATE_NEEDS_INIT == buf->vb.state) {
780 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
781 goto fail;
782 urb_init=1;
783 }
784
785
786 if (!dev->isoc_ctl.num_bufs)
787 urb_init=1;
788
789 if (urb_init) {
790 /* Should allocate/request at least h
791 res x v res x 2 bytes/pixel */
792 urbsize=(buf->vb.size+dev->max_isoc_in-1)/dev->max_isoc_in;
793
794 /* Hack to allocate memory for Video + Audio */
795 /* FIXME: should also consider header ovehead of
796 4 bytes/180 bytes */
797 urbsize+=((48000*4+24)/25+dev->max_isoc_in-1)/dev->max_isoc_in;
798
799 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d packets to handle "
800 "%lu size\n", urbsize,buf->vb.size);
801 rc = tm6000_prepare_isoc(dev, urbsize, 2);
802
803 if (rc<0)
804 goto fail;
805 }
806
807 buf->vb.state = STATE_PREPARED;
808 return 0;
809
810fail:
811 free_buffer(vq,buf);
812 return rc;
813}
814
815static void
816buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
817{
818 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
819 struct tm6000_fh *fh = vq->priv_data;
820 struct tm6000_core *dev = fh->dev;
821 struct tm6000_dmaqueue *vidq = &dev->vidq;
822 struct tm6000_buffer *prev;
823
824 if (!list_empty(&vidq->queued)) {
825 list_add_tail(&buf->vb.queue,&vidq->queued);
826 buf->vb.state = STATE_QUEUED;
827 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - "
828 "append to queued\n", buf, buf->vb.i);
829 } else if (list_empty(&vidq->active)) {
830 list_add_tail(&buf->vb.queue,&vidq->active);
831 buf->vb.state = STATE_ACTIVE;
832 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
833 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - "
834 "first active\n", buf, buf->vb.i);
835 tm6000_start_thread(vidq, buf);
836 } else {
837 prev = list_entry(vidq->active.prev, struct tm6000_buffer, vb.queue);
838 if (prev->vb.width == buf->vb.width &&
839 prev->vb.height == buf->vb.height &&
840 prev->fmt == buf->fmt) {
841 list_add_tail(&buf->vb.queue,&vidq->active);
842 buf->vb.state = STATE_ACTIVE;
843 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -"
844 " append to active\n", buf, buf->vb.i);
845 } else {
846 list_add_tail(&buf->vb.queue,&vidq->queued);
847 buf->vb.state = STATE_QUEUED;
848 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -"
849 " first queued\n", buf, buf->vb.i);
850 }
851 }
852}
853
854static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
855{
856 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
857 struct tm6000_fh *fh = vq->priv_data;
858 struct tm6000_core *dev = (struct tm6000_core*)fh->dev;
859 struct tm6000_dmaqueue *vidq = &dev->vidq;
860
861 tm6000_stop_thread(vidq);
862
863 free_buffer(vq,buf);
864}
865
866static struct videobuf_queue_ops tm6000_video_qops = {
867 .buf_setup = buffer_setup,
868 .buf_prepare = buffer_prepare,
869 .buf_queue = buffer_queue,
870 .buf_release = buffer_release,
871};
872
873/* ------------------------------------------------------------------
874 IOCTL handling
875 ------------------------------------------------------------------*/
876
877static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
878{
879 /* is it free? */
880 mutex_lock(&dev->lock);
881 if (dev->resources) {
882 /* no, someone else uses it */
883 mutex_unlock(&dev->lock);
884 return 0;
885 }
886 /* it's free, grab it */
887 dev->resources =1;
888 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
889 mutex_unlock(&dev->lock);
890 return 1;
891}
892
893static int res_locked(struct tm6000_core *dev)
894{
895 return (dev->resources);
896}
897
898static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
899{
900 mutex_lock(&dev->lock);
901 dev->resources = 0;
902 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
903 mutex_unlock(&dev->lock);
904}
905
906/* ------------------------------------------------------------------
907 IOCTL vidioc handling
908 ------------------------------------------------------------------*/
909static int vidioc_querycap (struct file *file, void *priv,
910 struct v4l2_capability *cap)
911{
912 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
913
914 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
915 strlcpy(cap->card,"Trident TVMaster TM5600/6000", sizeof(cap->card));
916 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
917 cap->version = TM6000_VERSION;
918 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
919 V4L2_CAP_STREAMING |
920 V4L2_CAP_TUNER |
921 V4L2_CAP_READWRITE;
922 return 0;
923}
924
925static int vidioc_enum_fmt_cap (struct file *file, void *priv,
926 struct v4l2_fmtdesc *f)
927{
928 if (unlikely(f->index >= ARRAY_SIZE(format)))
929 return -EINVAL;
930
931 strlcpy(f->description,format[f->index].name,sizeof(f->description));
932 f->pixelformat = format[f->index].fourcc;
933 return 0;
934}
935
936static int vidioc_g_fmt_cap (struct file *file, void *priv,
937 struct v4l2_format *f)
938{
939 struct tm6000_fh *fh=priv;
940
941 f->fmt.pix.width = fh->width;
942 f->fmt.pix.height = fh->height;
943 f->fmt.pix.field = fh->vb_vidq.field;
944 f->fmt.pix.pixelformat = fh->fmt->fourcc;
945 f->fmt.pix.bytesperline =
946 (f->fmt.pix.width * fh->fmt->depth) >> 3;
947 f->fmt.pix.sizeimage =
948 f->fmt.pix.height * f->fmt.pix.bytesperline;
949
950 return (0);
951}
952
953static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
954{
955 unsigned int i;
956
957 for (i = 0; i < ARRAY_SIZE(format); i++)
958 if (format[i].fourcc == fourcc)
959 return format+i;
960 return NULL;
961}
962
963static int vidioc_try_fmt_cap (struct file *file, void *priv,
964 struct v4l2_format *f)
965{
966 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
967 struct tm6000_fmt *fmt;
968 enum v4l2_field field;
969
970 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
971 if (NULL == fmt) {
972 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
973 " invalid.\n", f->fmt.pix.pixelformat);
974 return -EINVAL;
975 }
976
977 field = f->fmt.pix.field;
978
979 if (field == V4L2_FIELD_ANY) {
980// field=V4L2_FIELD_INTERLACED;
981 field=V4L2_FIELD_SEQ_TB;
982 } else if (V4L2_FIELD_INTERLACED != field) {
983 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
984 return -EINVAL;
985 }
986
4475c044 987 tm6000_get_std_res (dev);
9701dc94 988
4475c044
MCC
989 f->fmt.pix.width = dev->width;
990 f->fmt.pix.height = dev->height;
9701dc94
MCC
991
992 f->fmt.pix.width &= ~0x01;
993
994 f->fmt.pix.field = field;
995
996 f->fmt.pix.bytesperline =
997 (f->fmt.pix.width * fmt->depth) >> 3;
998 f->fmt.pix.sizeimage =
999 f->fmt.pix.height * f->fmt.pix.bytesperline;
1000
1001 return 0;
1002}
1003
1004/*FIXME: This seems to be generic enough to be at videodev2 */
1005static int vidioc_s_fmt_cap (struct file *file, void *priv,
1006 struct v4l2_format *f)
1007{
1008 struct tm6000_fh *fh=priv;
1009 struct tm6000_core *dev = fh->dev;
1010 int ret = vidioc_try_fmt_cap(file,fh,f);
1011 if (ret < 0)
1012 return (ret);
1013
1014 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1015 fh->width = f->fmt.pix.width;
1016 fh->height = f->fmt.pix.height;
1017 fh->vb_vidq.field = f->fmt.pix.field;
1018 fh->type = f->type;
1019
1020 dev->fourcc = f->fmt.pix.pixelformat;
1021
1022 tm6000_set_fourcc_format(dev);
1023
1024 return (0);
1025}
1026
1027static int vidioc_reqbufs (struct file *file, void *priv,
1028 struct v4l2_requestbuffers *p)
1029{
1030 struct tm6000_fh *fh=priv;
1031
1032 return (videobuf_reqbufs(&fh->vb_vidq, p));
1033}
1034
1035static int vidioc_querybuf (struct file *file, void *priv,
1036 struct v4l2_buffer *p)
1037{
1038 struct tm6000_fh *fh=priv;
1039
1040 return (videobuf_querybuf(&fh->vb_vidq, p));
1041}
1042
1043static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1044{
1045 struct tm6000_fh *fh=priv;
1046
1047 return (videobuf_qbuf(&fh->vb_vidq, p));
1048}
1049
1050static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1051{
1052 struct tm6000_fh *fh=priv;
1053
1054 return (videobuf_dqbuf(&fh->vb_vidq, p,
1055 file->f_flags & O_NONBLOCK));
1056}
1057
1058#ifdef CONFIG_VIDEO_V4L1_COMPAT
1059static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1060{
1061 struct tm6000_fh *fh=priv;
1062
1063 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1064}
1065#endif
1066
1067static int vidioc_streamon(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 if (!res_get(dev,fh))
1078 return -EBUSY;
1079 return (videobuf_streamon(&fh->vb_vidq));
1080}
1081
1082static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1083{
1084 struct tm6000_fh *fh=priv;
1085 struct tm6000_core *dev = fh->dev;
1086
1087 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1088 return -EINVAL;
1089 if (i != fh->type)
1090 return -EINVAL;
1091
1092 videobuf_streamoff(&fh->vb_vidq);
1093 res_free(dev,fh);
1094
1095 return (0);
1096}
1097
1098static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1099{
1100 int rc=0;
1101 struct tm6000_fh *fh=priv;
1102 struct tm6000_core *dev = fh->dev;
1103
1104 rc=tm6000_set_standard (dev, norm);
1105 if (rc<0)
1106 return rc;
1107
1108 tm6000_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1109
1110 return 0;
1111}
1112
1113static int vidioc_enum_input (struct file *file, void *priv,
1114 struct v4l2_input *inp)
1115{
1116 switch (inp->index) {
1117 case TM6000_INPUT_TV:
1118 inp->type = V4L2_INPUT_TYPE_TUNER;
1119 strcpy(inp->name,"Television");
1120 break;
1121 case TM6000_INPUT_COMPOSITE:
1122 inp->type = V4L2_INPUT_TYPE_CAMERA;
1123 strcpy(inp->name,"Composite");
1124 break;
1125 case TM6000_INPUT_SVIDEO:
1126 inp->type = V4L2_INPUT_TYPE_CAMERA;
1127 strcpy(inp->name,"S-Video");
1128 break;
1129 default:
1130 return -EINVAL;
1131 }
1132 inp->std = TM6000_STD;
1133
1134 return 0;
1135}
1136
1137static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1138{
1139 struct tm6000_fh *fh=priv;
1140 struct tm6000_core *dev = fh->dev;
1141
1142 *i=dev->input;
1143
1144 return 0;
1145}
1146static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1147{
1148 struct tm6000_fh *fh=priv;
1149 struct tm6000_core *dev = fh->dev;
1150 int rc=0;
1151 char buf[1];
1152
1153 switch (i) {
1154 case TM6000_INPUT_TV:
1155 dev->input=i;
1156 *buf=0;
1157 break;
1158 case TM6000_INPUT_COMPOSITE:
1159 case TM6000_INPUT_SVIDEO:
1160 dev->input=i;
1161 *buf=1;
1162 break;
1163 default:
1164 return -EINVAL;
1165 }
1166 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1167 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1168
1169 if (!rc) {
1170 dev->input=i;
7c3f53ec 1171 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
9701dc94
MCC
1172 }
1173
1174 return (rc);
1175}
1176
1177 /* --- controls ---------------------------------------------- */
1178static int vidioc_queryctrl (struct file *file, void *priv,
1179 struct v4l2_queryctrl *qc)
1180{
1181 int i;
1182
1183 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1184 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1185 memcpy(qc, &(tm6000_qctrl[i]),
1186 sizeof(*qc));
1187 return (0);
1188 }
1189
1190 return -EINVAL;
1191}
1192
1193static int vidioc_g_ctrl (struct file *file, void *priv,
1194 struct v4l2_control *ctrl)
1195{
1196 struct tm6000_fh *fh=priv;
1197 struct tm6000_core *dev = fh->dev;
1198 int val;
1199
1200 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1201 switch (ctrl->id) {
1202 case V4L2_CID_CONTRAST:
1203 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x08, 0);
1204 break;
1205 case V4L2_CID_BRIGHTNESS:
1206 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x09, 0);
1207 return 0;
1208 case V4L2_CID_SATURATION:
1209 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, 0);
1210 return 0;
1211 case V4L2_CID_HUE:
1212 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, 0);
1213 return 0;
1214 default:
1215 return -EINVAL;
1216 }
1217
1218 if (val<0)
1219 return val;
1220
1221 ctrl->value=val;
1222
1223 return 0;
1224}
1225static int vidioc_s_ctrl (struct file *file, void *priv,
1226 struct v4l2_control *ctrl)
1227{
1228 struct tm6000_fh *fh =priv;
1229 struct tm6000_core *dev = fh->dev;
1230 u8 val=ctrl->value;
1231
1232 switch (ctrl->id) {
1233 case V4L2_CID_CONTRAST:
1234 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x08, val);
1235 return 0;
1236 case V4L2_CID_BRIGHTNESS:
1237 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x09, val);
1238 return 0;
1239 case V4L2_CID_SATURATION:
1240 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, val);
1241 return 0;
1242 case V4L2_CID_HUE:
1243 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, val);
1244 return 0;
1245 }
1246 return -EINVAL;
1247}
1248
1249static int vidioc_g_tuner (struct file *file, void *priv,
1250 struct v4l2_tuner *t)
1251{
1252 struct tm6000_fh *fh =priv;
1253 struct tm6000_core *dev = fh->dev;
1254
1255 if (unlikely(UNSET == dev->tuner_type))
1256 return -EINVAL;
1257 if (0 != t->index)
1258 return -EINVAL;
1259
1260 strcpy(t->name, "Television");
1261 t->type = V4L2_TUNER_ANALOG_TV;
1262 t->capability = V4L2_TUNER_CAP_NORM;
1263 t->rangehigh = 0xffffffffUL;
1264 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1265
1266 return 0;
1267}
1268
1269static int vidioc_s_tuner (struct file *file, void *priv,
1270 struct v4l2_tuner *t)
1271{
1272 struct tm6000_fh *fh =priv;
1273 struct tm6000_core *dev = fh->dev;
1274
1275 if (UNSET == dev->tuner_type)
1276 return -EINVAL;
1277 if (0 != t->index)
1278 return -EINVAL;
1279
1280 return 0;
1281}
1282
1283static int vidioc_g_frequency (struct file *file, void *priv,
1284 struct v4l2_frequency *f)
1285{
1286 struct tm6000_fh *fh =priv;
1287 struct tm6000_core *dev = fh->dev;
1288
1289 if (unlikely(UNSET == dev->tuner_type))
1290 return -EINVAL;
1291
1292 f->type = V4L2_TUNER_ANALOG_TV;
1293 f->frequency = dev->freq;
1294
1295 tm6000_i2c_call_clients(dev,VIDIOC_G_FREQUENCY,f);
1296
1297 return 0;
1298}
1299
1300static int vidioc_s_frequency (struct file *file, void *priv,
1301 struct v4l2_frequency *f)
1302{
1303 struct tm6000_fh *fh =priv;
1304 struct tm6000_core *dev = fh->dev;
1305
1306 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1307 return -EINVAL;
1308
1309 if (unlikely(UNSET == dev->tuner_type))
1310 return -EINVAL;
1311 if (unlikely(f->tuner != 0))
1312 return -EINVAL;
1313
1314// mutex_lock(&dev->lock);
1315 dev->freq = f->frequency;
1316 tm6000_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1317// mutex_unlock(&dev->lock);
1318
1319 return 0;
1320}
1321
1322/* ------------------------------------------------------------------
1323 File operations for the device
1324 ------------------------------------------------------------------*/
1325
1326static int tm6000_open(struct inode *inode, struct file *file)
1327{
1328 int minor = iminor(inode);
1329 struct tm6000_core *h,*dev = NULL;
1330 struct tm6000_fh *fh;
1331 struct list_head *list;
1332 enum v4l2_buf_type type = 0;
1333 int i,rc;
1334
7c3f53ec
ML
1335 printk(KERN_INFO "tm6000: open called (minor=%d)\n",minor);
1336
1337
9701dc94
MCC
1338 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called "
1339 "(minor=%d)\n",minor);
1340
1341 list_for_each(list,&tm6000_corelist) {
1342 h = list_entry(list, struct tm6000_core, tm6000_corelist);
7c3f53ec 1343 if (h->vfd->minor == minor) {
9701dc94
MCC
1344 dev = h;
1345 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1346 }
1347 }
1348 if (NULL == dev)
1349 return -ENODEV;
1350
1351
1352 /* If more than one user, mutex should be added */
1353 dev->users++;
1354
1355 dprintk(dev, V4L2_DEBUG_OPEN, "open minor=%d type=%s users=%d\n",
1356 minor,v4l2_type_names[type],dev->users);
1357
1358 /* allocate + initialize per filehandle data */
1359 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1360 if (NULL == fh) {
1361 dev->users--;
1362 return -ENOMEM;
1363 }
1364
1365 file->private_data = fh;
1366 fh->dev = dev;
1367
1368 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1369 dev->fourcc = format[0].fourcc;
1370
1371 fh->fmt = format_by_fourcc(dev->fourcc);
4475c044
MCC
1372
1373 tm6000_get_std_res (dev);
1374
1375 fh->width = dev->width;
1376 fh->height = dev->height;
9701dc94
MCC
1377
1378 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1379 "dev->vidq=0x%08lx\n",
1380 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1381 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1382 "queued=%d\n",list_empty(&dev->vidq.queued));
1383 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1384 "active=%d\n",list_empty(&dev->vidq.active));
1385
1386 /* initialize hardware on analog mode */
1387 if (dev->mode!=TM6000_MODE_ANALOG) {
1388 rc=tm6000_init_analog_mode (dev);
1389 if (rc<0)
1390 return rc;
1391
1392 /* Put all controls at a sane state */
1393 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1394 qctl_regs[i] =tm6000_qctrl[i].default_value;
1395
1396 dev->mode=TM6000_MODE_ANALOG;
1397 }
1398
1399 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1400 NULL, &dev->slock,
1401 fh->type,
1402 V4L2_FIELD_INTERLACED,
1403 sizeof(struct tm6000_buffer),fh);
1404
1405 return 0;
1406}
1407
1408static ssize_t
1409tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1410{
1411 struct tm6000_fh *fh = file->private_data;
1412
1413 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1414 if (res_locked(fh->dev))
1415 return -EBUSY;
1416
1417 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1418 file->f_flags & O_NONBLOCK);
1419 }
1420 return 0;
1421}
1422
1423static unsigned int
1424tm6000_poll(struct file *file, struct poll_table_struct *wait)
1425{
1426 struct tm6000_fh *fh = file->private_data;
1427 struct tm6000_buffer *buf;
1428
1429 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1430 return POLLERR;
1431
1432 if (res_get(fh->dev,fh)) {
1433 /* streaming capture */
1434 if (list_empty(&fh->vb_vidq.stream))
1435 return POLLERR;
1436 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1437 } else {
1438 /* read() capture */
dc6a02aa
MCC
1439 return videobuf_poll_stream(file, &fh->vb_vidq,
1440 wait);
9701dc94
MCC
1441 }
1442 poll_wait(file, &buf->vb.done, wait);
1443 if (buf->vb.state == STATE_DONE ||
1444 buf->vb.state == STATE_ERROR)
1445 return POLLIN|POLLRDNORM;
1446 return 0;
1447}
1448
1449static int tm6000_release(struct inode *inode, struct file *file)
1450{
1451 struct tm6000_fh *fh = file->private_data;
1452 struct tm6000_core *dev = fh->dev;
1453 struct tm6000_dmaqueue *vidq = &dev->vidq;
1454 int minor = iminor(inode);
1455
7c3f53ec
ML
1456 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (minor=%d, users=%d)\n",minor,dev->users);
1457
a58d35cb
MCC
1458 dev->users--;
1459
1460 if (!dev->users) {
1461 tm6000_stop_thread(vidq);
1462 videobuf_mmap_free(&fh->vb_vidq);
1463 }
9701dc94
MCC
1464
1465 kfree (fh);
1466
9701dc94
MCC
1467 return 0;
1468}
1469
1470static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1471{
1472 struct tm6000_fh *fh = file->private_data;
1473 int ret;
1474
1475 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1476
1477 return ret;
1478}
1479
1480static struct file_operations tm6000_fops = {
1481 .owner = THIS_MODULE,
1482 .open = tm6000_open,
1483 .release = tm6000_release,
1484 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1485 .read = tm6000_read,
1486 .poll = tm6000_poll,
1487 .mmap = tm6000_mmap,
1488 .llseek = no_llseek,
1489};
1490
1491static struct video_device tm6000_template = {
1492 .name = "tm6000",
1493 .type = VID_TYPE_CAPTURE,
1494 .fops = &tm6000_fops,
1495 .minor = -1,
1496 .release = video_device_release,
1497
1498 .vidioc_querycap = vidioc_querycap,
1499 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1500 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1501 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1502 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1503 .vidioc_s_std = vidioc_s_std,
1504 .vidioc_enum_input = vidioc_enum_input,
1505 .vidioc_g_input = vidioc_g_input,
1506 .vidioc_s_input = vidioc_s_input,
1507 .vidioc_queryctrl = vidioc_queryctrl,
1508 .vidioc_g_ctrl = vidioc_g_ctrl,
1509 .vidioc_s_ctrl = vidioc_s_ctrl,
1510 .vidioc_g_tuner = vidioc_g_tuner,
1511 .vidioc_s_tuner = vidioc_s_tuner,
1512 .vidioc_g_frequency = vidioc_g_frequency,
1513 .vidioc_s_frequency = vidioc_s_frequency,
1514 .vidioc_streamon = vidioc_streamon,
1515 .vidioc_streamoff = vidioc_streamoff,
1516 .vidioc_reqbufs = vidioc_reqbufs,
1517 .vidioc_querybuf = vidioc_querybuf,
1518 .vidioc_qbuf = vidioc_qbuf,
1519 .vidioc_dqbuf = vidioc_dqbuf,
1520#ifdef CONFIG_VIDEO_V4L1_COMPAT
1521 .vidiocgmbuf = vidiocgmbuf,
1522#endif
1523 .tvnorms = TM6000_STD,
1524 .current_norm = V4L2_STD_NTSC_M,
1525};
1526/* -----------------------------------------------------------------
1527 Initialization and module stuff
1528 ------------------------------------------------------------------*/
1529
1530int tm6000_v4l2_register(struct tm6000_core *dev)
1531{
7c3f53ec
ML
1532 int ret = -1;
1533 struct video_device *vfd;
1534
1535 vfd = video_device_alloc();
1536 if(!vfd) {
1537 return -ENOMEM;
1538 }
1539 dev->vfd = vfd;
9701dc94
MCC
1540
1541 list_add_tail(&dev->tm6000_corelist,&tm6000_corelist);
1542
1543 /* init video dma queues */
1544 INIT_LIST_HEAD(&dev->vidq.active);
1545 INIT_LIST_HEAD(&dev->vidq.queued);
1546
1547 dev->vidq.timeout.function = tm6000_vid_timeout;
1548 dev->vidq.timeout.data = (unsigned long)dev;
1549 init_timer(&dev->vidq.timeout);
1550
7c3f53ec
ML
1551 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1552 dev->vfd->debug=tm6000_debug;
9701dc94 1553
7c3f53ec 1554 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
9701dc94
MCC
1555 printk(KERN_INFO "Trident TVMaster TM5600/TM6000 USB2 board (Load status: %d)\n", ret);
1556 return ret;
1557}
1558
1559int tm6000_v4l2_unregister(struct tm6000_core *dev)
1560{
1561 struct tm6000_core *h;
22927e8e 1562 struct list_head *pos, *tmp;
9701dc94 1563
7c3f53ec 1564 video_unregister_device(dev->vfd);
22927e8e
ML
1565
1566 list_for_each_safe(pos, tmp, &tm6000_corelist) {
1567 h = list_entry(pos, struct tm6000_core, tm6000_corelist);
9701dc94 1568 if (h == dev) {
8c9d26fd 1569 list_del(pos);
9701dc94
MCC
1570 }
1571 }
1572
1573 return 0;
1574}
1575
1576int tm6000_v4l2_exit(void)
1577{
1578 return 0;
1579}
1580
1581module_param(video_nr, int, 0);
1582MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1583
1584module_param_named (debug, tm6000_debug, int, 0444);
1585MODULE_PARM_DESC(debug,"activates debug info");
1586
1587module_param(vid_limit,int,0644);
1588MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
1589