blob: f96c7a77efb66223af5460f2bf6a6d0569228a89 [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
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03006 Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 - Fixed module load/unload
8
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03009 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
Michel Ludwig95a83822007-07-24 08:06:45 -030049/* Limits minimum and default number of buffers */
50#define TM6000_MIN_BUF 4
51#define TM6000_DEF_BUF 8
52
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030053/* 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
128#define norm_maxh(a) 480
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 */
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300166 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n",buf,buf->vb.i);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300167 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);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300317 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300318
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
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300332static 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
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300373/*
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
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300388 if (urb->status<0) {
389 print_err_status (dev,-1,urb->status);
390 return 0;
391 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300392
393 for (i = 0; i < urb->number_of_packets; i++) {
394 int status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300395
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300396 if (status<0) {
397 print_err_status (dev,i,status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300398 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300399 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300400
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);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300422 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300423 }
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++) {
Michel Ludwig2cd4fd12007-06-17 17:12:32 -0300615 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300616 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 ------------------------------------------------------------------*/
Michel Ludwig95a83822007-07-24 08:06:45 -0300720
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300721static 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)
Michel Ludwig95a83822007-07-24 08:06:45 -0300728 *count = TM6000_DEF_BUF;
729
730 if (*count < TM6000_MIN_BUF) {
731 *count=TM6000_MIN_BUF;
732 }
733
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300734 while (*size * *count > vid_limit * 1024 * 1024)
735 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300736
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300737 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
761 if (fh->width < norm_minw(core) || fh->width > norm_maxw(core) ||
762 fh->height < norm_minh(core) || fh->height > norm_maxh(core)) {
763 dprintk(dev, V4L2_DEBUG_QUEUE, "Window size (%dx%d) is out of "
764 "supported range\n", fh->width, fh->height);
765 dprintk(dev, V4L2_DEBUG_QUEUE, "Valid range is from (%dx%d) to "
766 "(%dx%d)\n", norm_minw(core), norm_minh(core),
767 norm_maxw(core),norm_maxh(core));
768 return -EINVAL;
769 }
770
771 /* FIXME: It assumes depth=2 */
772 /* The only currently supported format is 16 bits/pixel */
773 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
774 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
775 return -EINVAL;
776
777 if (buf->fmt != fh->fmt ||
778 buf->vb.width != fh->width ||
779 buf->vb.height != fh->height ||
780 buf->vb.field != field) {
781 buf->fmt = fh->fmt;
782 buf->vb.width = fh->width;
783 buf->vb.height = fh->height;
784 buf->vb.field = field;
785 buf->vb.state = STATE_NEEDS_INIT;
786 }
787
788 if (STATE_NEEDS_INIT == buf->vb.state) {
789 if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL)))
790 goto fail;
791 urb_init=1;
792 }
793
794
795 if (!dev->isoc_ctl.num_bufs)
796 urb_init=1;
797
798 if (urb_init) {
799 /* Should allocate/request at least h
800 res x v res x 2 bytes/pixel */
801 urbsize=(buf->vb.size+dev->max_isoc_in-1)/dev->max_isoc_in;
802
803 /* Hack to allocate memory for Video + Audio */
804 /* FIXME: should also consider header ovehead of
805 4 bytes/180 bytes */
806 urbsize+=((48000*4+24)/25+dev->max_isoc_in-1)/dev->max_isoc_in;
807
808 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d packets to handle "
809 "%lu size\n", urbsize,buf->vb.size);
810 rc = tm6000_prepare_isoc(dev, urbsize, 2);
811
812 if (rc<0)
813 goto fail;
814 }
815
816 buf->vb.state = STATE_PREPARED;
817 return 0;
818
819fail:
820 free_buffer(vq,buf);
821 return rc;
822}
823
824static void
825buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
826{
827 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
828 struct tm6000_fh *fh = vq->priv_data;
829 struct tm6000_core *dev = fh->dev;
830 struct tm6000_dmaqueue *vidq = &dev->vidq;
831 struct tm6000_buffer *prev;
832
833 if (!list_empty(&vidq->queued)) {
834 list_add_tail(&buf->vb.queue,&vidq->queued);
835 buf->vb.state = STATE_QUEUED;
836 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - "
837 "append to queued\n", buf, buf->vb.i);
838 } else if (list_empty(&vidq->active)) {
839 list_add_tail(&buf->vb.queue,&vidq->active);
840 buf->vb.state = STATE_ACTIVE;
841 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
842 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - "
843 "first active\n", buf, buf->vb.i);
844 tm6000_start_thread(vidq, buf);
845 } else {
846 prev = list_entry(vidq->active.prev, struct tm6000_buffer, vb.queue);
847 if (prev->vb.width == buf->vb.width &&
848 prev->vb.height == buf->vb.height &&
849 prev->fmt == buf->fmt) {
850 list_add_tail(&buf->vb.queue,&vidq->active);
851 buf->vb.state = STATE_ACTIVE;
852 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -"
853 " append to active\n", buf, buf->vb.i);
854 } else {
855 list_add_tail(&buf->vb.queue,&vidq->queued);
856 buf->vb.state = STATE_QUEUED;
857 dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -"
858 " first queued\n", buf, buf->vb.i);
859 }
860 }
861}
862
863static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
864{
865 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
866 struct tm6000_fh *fh = vq->priv_data;
867 struct tm6000_core *dev = (struct tm6000_core*)fh->dev;
868 struct tm6000_dmaqueue *vidq = &dev->vidq;
869
870 tm6000_stop_thread(vidq);
871
872 free_buffer(vq,buf);
873}
874
875static struct videobuf_queue_ops tm6000_video_qops = {
876 .buf_setup = buffer_setup,
877 .buf_prepare = buffer_prepare,
878 .buf_queue = buffer_queue,
879 .buf_release = buffer_release,
880};
881
882/* ------------------------------------------------------------------
883 IOCTL handling
884 ------------------------------------------------------------------*/
885
886static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
887{
888 /* is it free? */
889 mutex_lock(&dev->lock);
890 if (dev->resources) {
891 /* no, someone else uses it */
892 mutex_unlock(&dev->lock);
893 return 0;
894 }
895 /* it's free, grab it */
896 dev->resources =1;
897 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
898 mutex_unlock(&dev->lock);
899 return 1;
900}
901
902static int res_locked(struct tm6000_core *dev)
903{
904 return (dev->resources);
905}
906
907static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
908{
909 mutex_lock(&dev->lock);
910 dev->resources = 0;
911 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
912 mutex_unlock(&dev->lock);
913}
914
915/* ------------------------------------------------------------------
916 IOCTL vidioc handling
917 ------------------------------------------------------------------*/
918static int vidioc_querycap (struct file *file, void *priv,
919 struct v4l2_capability *cap)
920{
921 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
922
923 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
924 strlcpy(cap->card,"Trident TVMaster TM5600/6000", sizeof(cap->card));
925 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
926 cap->version = TM6000_VERSION;
927 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
928 V4L2_CAP_STREAMING |
929 V4L2_CAP_TUNER |
930 V4L2_CAP_READWRITE;
931 return 0;
932}
933
934static int vidioc_enum_fmt_cap (struct file *file, void *priv,
935 struct v4l2_fmtdesc *f)
936{
937 if (unlikely(f->index >= ARRAY_SIZE(format)))
938 return -EINVAL;
939
940 strlcpy(f->description,format[f->index].name,sizeof(f->description));
941 f->pixelformat = format[f->index].fourcc;
942 return 0;
943}
944
945static int vidioc_g_fmt_cap (struct file *file, void *priv,
946 struct v4l2_format *f)
947{
948 struct tm6000_fh *fh=priv;
949
950 f->fmt.pix.width = fh->width;
951 f->fmt.pix.height = fh->height;
952 f->fmt.pix.field = fh->vb_vidq.field;
953 f->fmt.pix.pixelformat = fh->fmt->fourcc;
954 f->fmt.pix.bytesperline =
955 (f->fmt.pix.width * fh->fmt->depth) >> 3;
956 f->fmt.pix.sizeimage =
957 f->fmt.pix.height * f->fmt.pix.bytesperline;
958
959 return (0);
960}
961
962static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
963{
964 unsigned int i;
965
966 for (i = 0; i < ARRAY_SIZE(format); i++)
967 if (format[i].fourcc == fourcc)
968 return format+i;
969 return NULL;
970}
971
972static int vidioc_try_fmt_cap (struct file *file, void *priv,
973 struct v4l2_format *f)
974{
975 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
976 struct tm6000_fmt *fmt;
977 enum v4l2_field field;
978
979 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
980 if (NULL == fmt) {
981 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
982 " invalid.\n", f->fmt.pix.pixelformat);
983 return -EINVAL;
984 }
985
986 field = f->fmt.pix.field;
987
988 if (field == V4L2_FIELD_ANY) {
989// field=V4L2_FIELD_INTERLACED;
990 field=V4L2_FIELD_SEQ_TB;
991 } else if (V4L2_FIELD_INTERLACED != field) {
992 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
993 return -EINVAL;
994 }
995
996 if (f->fmt.pix.width < norm_minw(core))
997 f->fmt.pix.width = norm_minw(core);
998
999 if (f->fmt.pix.width > norm_maxw(core))
1000 f->fmt.pix.width = norm_maxw(core);
1001
1002 if (f->fmt.pix.height < norm_minh(core))
1003 f->fmt.pix.height = norm_minh(core);
1004
1005 if (f->fmt.pix.height > norm_maxh(core))
1006 f->fmt.pix.height = norm_maxh(core);
1007
1008 f->fmt.pix.width &= ~0x01;
1009
1010 f->fmt.pix.field = field;
1011
1012 f->fmt.pix.bytesperline =
1013 (f->fmt.pix.width * fmt->depth) >> 3;
1014 f->fmt.pix.sizeimage =
1015 f->fmt.pix.height * f->fmt.pix.bytesperline;
1016
1017 return 0;
1018}
1019
1020/*FIXME: This seems to be generic enough to be at videodev2 */
1021static int vidioc_s_fmt_cap (struct file *file, void *priv,
1022 struct v4l2_format *f)
1023{
1024 struct tm6000_fh *fh=priv;
1025 struct tm6000_core *dev = fh->dev;
1026 int ret = vidioc_try_fmt_cap(file,fh,f);
1027 if (ret < 0)
1028 return (ret);
1029
1030 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1031 fh->width = f->fmt.pix.width;
1032 fh->height = f->fmt.pix.height;
1033 fh->vb_vidq.field = f->fmt.pix.field;
1034 fh->type = f->type;
1035
1036 dev->fourcc = f->fmt.pix.pixelformat;
1037
1038 tm6000_set_fourcc_format(dev);
1039
1040 return (0);
1041}
1042
1043static int vidioc_reqbufs (struct file *file, void *priv,
1044 struct v4l2_requestbuffers *p)
1045{
1046 struct tm6000_fh *fh=priv;
1047
1048 return (videobuf_reqbufs(&fh->vb_vidq, p));
1049}
1050
1051static int vidioc_querybuf (struct file *file, void *priv,
1052 struct v4l2_buffer *p)
1053{
1054 struct tm6000_fh *fh=priv;
1055
1056 return (videobuf_querybuf(&fh->vb_vidq, p));
1057}
1058
1059static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1060{
1061 struct tm6000_fh *fh=priv;
1062
1063 return (videobuf_qbuf(&fh->vb_vidq, p));
1064}
1065
1066static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1067{
1068 struct tm6000_fh *fh=priv;
1069
1070 return (videobuf_dqbuf(&fh->vb_vidq, p,
1071 file->f_flags & O_NONBLOCK));
1072}
1073
1074#ifdef CONFIG_VIDEO_V4L1_COMPAT
1075static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1076{
1077 struct tm6000_fh *fh=priv;
1078
1079 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
1080}
1081#endif
1082
1083static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1084{
1085 struct tm6000_fh *fh=priv;
1086 struct tm6000_core *dev = fh->dev;
1087
1088 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1089 return -EINVAL;
1090 if (i != fh->type)
1091 return -EINVAL;
1092
1093 if (!res_get(dev,fh))
1094 return -EBUSY;
1095 return (videobuf_streamon(&fh->vb_vidq));
1096}
1097
1098static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1099{
1100 struct tm6000_fh *fh=priv;
1101 struct tm6000_core *dev = fh->dev;
1102
1103 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1104 return -EINVAL;
1105 if (i != fh->type)
1106 return -EINVAL;
1107
1108 videobuf_streamoff(&fh->vb_vidq);
1109 res_free(dev,fh);
1110
1111 return (0);
1112}
1113
1114static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1115{
1116 int rc=0;
1117 struct tm6000_fh *fh=priv;
1118 struct tm6000_core *dev = fh->dev;
1119
1120 rc=tm6000_set_standard (dev, norm);
1121 if (rc<0)
1122 return rc;
1123
1124 tm6000_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
1125
1126 return 0;
1127}
1128
1129static int vidioc_enum_input (struct file *file, void *priv,
1130 struct v4l2_input *inp)
1131{
1132 switch (inp->index) {
1133 case TM6000_INPUT_TV:
1134 inp->type = V4L2_INPUT_TYPE_TUNER;
1135 strcpy(inp->name,"Television");
1136 break;
1137 case TM6000_INPUT_COMPOSITE:
1138 inp->type = V4L2_INPUT_TYPE_CAMERA;
1139 strcpy(inp->name,"Composite");
1140 break;
1141 case TM6000_INPUT_SVIDEO:
1142 inp->type = V4L2_INPUT_TYPE_CAMERA;
1143 strcpy(inp->name,"S-Video");
1144 break;
1145 default:
1146 return -EINVAL;
1147 }
1148 inp->std = TM6000_STD;
1149
1150 return 0;
1151}
1152
1153static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1154{
1155 struct tm6000_fh *fh=priv;
1156 struct tm6000_core *dev = fh->dev;
1157
1158 *i=dev->input;
1159
1160 return 0;
1161}
1162static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1163{
1164 struct tm6000_fh *fh=priv;
1165 struct tm6000_core *dev = fh->dev;
1166 int rc=0;
1167 char buf[1];
1168
1169 switch (i) {
1170 case TM6000_INPUT_TV:
1171 dev->input=i;
1172 *buf=0;
1173 break;
1174 case TM6000_INPUT_COMPOSITE:
1175 case TM6000_INPUT_SVIDEO:
1176 dev->input=i;
1177 *buf=1;
1178 break;
1179 default:
1180 return -EINVAL;
1181 }
1182 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1183 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1184
1185 if (!rc) {
1186 dev->input=i;
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001187 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001188 }
1189
1190 return (rc);
1191}
1192
1193 /* --- controls ---------------------------------------------- */
1194static int vidioc_queryctrl (struct file *file, void *priv,
1195 struct v4l2_queryctrl *qc)
1196{
1197 int i;
1198
1199 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1200 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1201 memcpy(qc, &(tm6000_qctrl[i]),
1202 sizeof(*qc));
1203 return (0);
1204 }
1205
1206 return -EINVAL;
1207}
1208
1209static int vidioc_g_ctrl (struct file *file, void *priv,
1210 struct v4l2_control *ctrl)
1211{
1212 struct tm6000_fh *fh=priv;
1213 struct tm6000_core *dev = fh->dev;
1214 int val;
1215
1216 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1217 switch (ctrl->id) {
1218 case V4L2_CID_CONTRAST:
1219 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x08, 0);
1220 break;
1221 case V4L2_CID_BRIGHTNESS:
1222 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x09, 0);
1223 return 0;
1224 case V4L2_CID_SATURATION:
1225 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, 0);
1226 return 0;
1227 case V4L2_CID_HUE:
1228 val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, 0);
1229 return 0;
1230 default:
1231 return -EINVAL;
1232 }
1233
1234 if (val<0)
1235 return val;
1236
1237 ctrl->value=val;
1238
1239 return 0;
1240}
1241static int vidioc_s_ctrl (struct file *file, void *priv,
1242 struct v4l2_control *ctrl)
1243{
1244 struct tm6000_fh *fh =priv;
1245 struct tm6000_core *dev = fh->dev;
1246 u8 val=ctrl->value;
1247
1248 switch (ctrl->id) {
1249 case V4L2_CID_CONTRAST:
1250 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x08, val);
1251 return 0;
1252 case V4L2_CID_BRIGHTNESS:
1253 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x09, val);
1254 return 0;
1255 case V4L2_CID_SATURATION:
1256 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, val);
1257 return 0;
1258 case V4L2_CID_HUE:
1259 tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, val);
1260 return 0;
1261 }
1262 return -EINVAL;
1263}
1264
1265static int vidioc_g_tuner (struct file *file, void *priv,
1266 struct v4l2_tuner *t)
1267{
1268 struct tm6000_fh *fh =priv;
1269 struct tm6000_core *dev = fh->dev;
1270
1271 if (unlikely(UNSET == dev->tuner_type))
1272 return -EINVAL;
1273 if (0 != t->index)
1274 return -EINVAL;
1275
1276 strcpy(t->name, "Television");
1277 t->type = V4L2_TUNER_ANALOG_TV;
1278 t->capability = V4L2_TUNER_CAP_NORM;
1279 t->rangehigh = 0xffffffffUL;
1280 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1281
1282 return 0;
1283}
1284
1285static int vidioc_s_tuner (struct file *file, void *priv,
1286 struct v4l2_tuner *t)
1287{
1288 struct tm6000_fh *fh =priv;
1289 struct tm6000_core *dev = fh->dev;
1290
1291 if (UNSET == dev->tuner_type)
1292 return -EINVAL;
1293 if (0 != t->index)
1294 return -EINVAL;
1295
1296 return 0;
1297}
1298
1299static int vidioc_g_frequency (struct file *file, void *priv,
1300 struct v4l2_frequency *f)
1301{
1302 struct tm6000_fh *fh =priv;
1303 struct tm6000_core *dev = fh->dev;
1304
1305 if (unlikely(UNSET == dev->tuner_type))
1306 return -EINVAL;
1307
1308 f->type = V4L2_TUNER_ANALOG_TV;
1309 f->frequency = dev->freq;
1310
1311 tm6000_i2c_call_clients(dev,VIDIOC_G_FREQUENCY,f);
1312
1313 return 0;
1314}
1315
1316static int vidioc_s_frequency (struct file *file, void *priv,
1317 struct v4l2_frequency *f)
1318{
1319 struct tm6000_fh *fh =priv;
1320 struct tm6000_core *dev = fh->dev;
1321
1322 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1323 return -EINVAL;
1324
1325 if (unlikely(UNSET == dev->tuner_type))
1326 return -EINVAL;
1327 if (unlikely(f->tuner != 0))
1328 return -EINVAL;
1329
1330// mutex_lock(&dev->lock);
1331 dev->freq = f->frequency;
1332 tm6000_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1333// mutex_unlock(&dev->lock);
1334
1335 return 0;
1336}
1337
1338/* ------------------------------------------------------------------
1339 File operations for the device
1340 ------------------------------------------------------------------*/
1341
1342static int tm6000_open(struct inode *inode, struct file *file)
1343{
1344 int minor = iminor(inode);
1345 struct tm6000_core *h,*dev = NULL;
1346 struct tm6000_fh *fh;
1347 struct list_head *list;
1348 enum v4l2_buf_type type = 0;
1349 int i,rc;
1350
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001351 printk(KERN_INFO "tm6000: open called (minor=%d)\n",minor);
1352
1353
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001354 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called "
1355 "(minor=%d)\n",minor);
1356
1357 list_for_each(list,&tm6000_corelist) {
1358 h = list_entry(list, struct tm6000_core, tm6000_corelist);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001359 if (h->vfd->minor == minor) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001360 dev = h;
1361 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1362 }
1363 }
1364 if (NULL == dev)
1365 return -ENODEV;
1366
1367
1368 /* If more than one user, mutex should be added */
1369 dev->users++;
1370
1371 dprintk(dev, V4L2_DEBUG_OPEN, "open minor=%d type=%s users=%d\n",
1372 minor,v4l2_type_names[type],dev->users);
1373
1374 /* allocate + initialize per filehandle data */
1375 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1376 if (NULL == fh) {
1377 dev->users--;
1378 return -ENOMEM;
1379 }
1380
1381 file->private_data = fh;
1382 fh->dev = dev;
1383
1384 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1385 dev->fourcc = format[0].fourcc;
1386
1387 fh->fmt = format_by_fourcc(dev->fourcc);
1388 fh->width = norm_maxw();
1389 fh->height = norm_maxh();
1390
1391 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1392 "dev->vidq=0x%08lx\n",
1393 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1394 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1395 "queued=%d\n",list_empty(&dev->vidq.queued));
1396 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1397 "active=%d\n",list_empty(&dev->vidq.active));
1398
1399 /* initialize hardware on analog mode */
1400 if (dev->mode!=TM6000_MODE_ANALOG) {
1401 rc=tm6000_init_analog_mode (dev);
1402 if (rc<0)
1403 return rc;
1404
1405 /* Put all controls at a sane state */
1406 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1407 qctl_regs[i] =tm6000_qctrl[i].default_value;
1408
1409 dev->mode=TM6000_MODE_ANALOG;
1410 }
1411
1412 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1413 NULL, &dev->slock,
1414 fh->type,
1415 V4L2_FIELD_INTERLACED,
1416 sizeof(struct tm6000_buffer),fh);
1417
1418 return 0;
1419}
1420
1421static ssize_t
1422tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1423{
1424 struct tm6000_fh *fh = file->private_data;
1425
1426 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1427 if (res_locked(fh->dev))
1428 return -EBUSY;
1429
1430 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1431 file->f_flags & O_NONBLOCK);
1432 }
1433 return 0;
1434}
1435
1436static unsigned int
1437tm6000_poll(struct file *file, struct poll_table_struct *wait)
1438{
1439 struct tm6000_fh *fh = file->private_data;
1440 struct tm6000_buffer *buf;
1441
1442 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1443 return POLLERR;
1444
1445 if (res_get(fh->dev,fh)) {
1446 /* streaming capture */
1447 if (list_empty(&fh->vb_vidq.stream))
1448 return POLLERR;
1449 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1450 } else {
1451 /* read() capture */
Mauro Carvalho Chehabdc6a02a2007-08-27 07:55:38 -03001452 return videobuf_poll_stream(file, &fh->vb_vidq,
1453 wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001454 }
1455 poll_wait(file, &buf->vb.done, wait);
1456 if (buf->vb.state == STATE_DONE ||
1457 buf->vb.state == STATE_ERROR)
1458 return POLLIN|POLLRDNORM;
1459 return 0;
1460}
1461
1462static int tm6000_release(struct inode *inode, struct file *file)
1463{
1464 struct tm6000_fh *fh = file->private_data;
1465 struct tm6000_core *dev = fh->dev;
1466 struct tm6000_dmaqueue *vidq = &dev->vidq;
1467 int minor = iminor(inode);
1468
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001469 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (minor=%d, users=%d)\n",minor,dev->users);
1470
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001471 dev->users--;
1472
1473 if (!dev->users) {
1474 tm6000_stop_thread(vidq);
1475 videobuf_mmap_free(&fh->vb_vidq);
1476 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001477
1478 kfree (fh);
1479
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001480 return 0;
1481}
1482
1483static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1484{
1485 struct tm6000_fh *fh = file->private_data;
1486 int ret;
1487
1488 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1489
1490 return ret;
1491}
1492
1493static struct file_operations tm6000_fops = {
1494 .owner = THIS_MODULE,
1495 .open = tm6000_open,
1496 .release = tm6000_release,
1497 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1498 .read = tm6000_read,
1499 .poll = tm6000_poll,
1500 .mmap = tm6000_mmap,
1501 .llseek = no_llseek,
1502};
1503
1504static struct video_device tm6000_template = {
1505 .name = "tm6000",
1506 .type = VID_TYPE_CAPTURE,
1507 .fops = &tm6000_fops,
1508 .minor = -1,
1509 .release = video_device_release,
1510
1511 .vidioc_querycap = vidioc_querycap,
1512 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1513 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1514 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1515 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1516 .vidioc_s_std = vidioc_s_std,
1517 .vidioc_enum_input = vidioc_enum_input,
1518 .vidioc_g_input = vidioc_g_input,
1519 .vidioc_s_input = vidioc_s_input,
1520 .vidioc_queryctrl = vidioc_queryctrl,
1521 .vidioc_g_ctrl = vidioc_g_ctrl,
1522 .vidioc_s_ctrl = vidioc_s_ctrl,
1523 .vidioc_g_tuner = vidioc_g_tuner,
1524 .vidioc_s_tuner = vidioc_s_tuner,
1525 .vidioc_g_frequency = vidioc_g_frequency,
1526 .vidioc_s_frequency = vidioc_s_frequency,
1527 .vidioc_streamon = vidioc_streamon,
1528 .vidioc_streamoff = vidioc_streamoff,
1529 .vidioc_reqbufs = vidioc_reqbufs,
1530 .vidioc_querybuf = vidioc_querybuf,
1531 .vidioc_qbuf = vidioc_qbuf,
1532 .vidioc_dqbuf = vidioc_dqbuf,
1533#ifdef CONFIG_VIDEO_V4L1_COMPAT
1534 .vidiocgmbuf = vidiocgmbuf,
1535#endif
1536 .tvnorms = TM6000_STD,
1537 .current_norm = V4L2_STD_NTSC_M,
1538};
1539/* -----------------------------------------------------------------
1540 Initialization and module stuff
1541 ------------------------------------------------------------------*/
1542
1543int tm6000_v4l2_register(struct tm6000_core *dev)
1544{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001545 int ret = -1;
1546 struct video_device *vfd;
1547
1548 vfd = video_device_alloc();
1549 if(!vfd) {
1550 return -ENOMEM;
1551 }
1552 dev->vfd = vfd;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001553
1554 list_add_tail(&dev->tm6000_corelist,&tm6000_corelist);
1555
1556 /* init video dma queues */
1557 INIT_LIST_HEAD(&dev->vidq.active);
1558 INIT_LIST_HEAD(&dev->vidq.queued);
1559
1560 dev->vidq.timeout.function = tm6000_vid_timeout;
1561 dev->vidq.timeout.data = (unsigned long)dev;
1562 init_timer(&dev->vidq.timeout);
1563
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001564 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1565 dev->vfd->debug=tm6000_debug;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001566
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001567 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001568 printk(KERN_INFO "Trident TVMaster TM5600/TM6000 USB2 board (Load status: %d)\n", ret);
1569 return ret;
1570}
1571
1572int tm6000_v4l2_unregister(struct tm6000_core *dev)
1573{
1574 struct tm6000_core *h;
Michel Ludwig22927e82007-06-14 17:19:59 -03001575 struct list_head *pos, *tmp;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001576
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001577 video_unregister_device(dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001578
1579 list_for_each_safe(pos, tmp, &tm6000_corelist) {
1580 h = list_entry(pos, struct tm6000_core, tm6000_corelist);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001581 if (h == dev) {
Michel Ludwig8c9d26f2007-06-15 11:02:56 -03001582 list_del(pos);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001583 }
1584 }
1585
1586 return 0;
1587}
1588
1589int tm6000_v4l2_exit(void)
1590{
1591 return 0;
1592}
1593
1594module_param(video_nr, int, 0);
1595MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1596
1597module_param_named (debug, tm6000_debug, int, 0444);
1598MODULE_PARM_DESC(debug,"activates debug info");
1599
1600module_param(vid_limit,int,0644);
1601MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
1602