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