powerpc/mm/4k: don't allocate larger pmd page table for 4k
[linux/fpc-iii.git] / drivers / media / usb / tm6000 / tm6000-video.c
blobd9f3fa5db8dd7c1997c570dcd22ed99080e61dad
1 /*
2 * tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
6 * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 * - Fixed module load/unload
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
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.
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.
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/fs.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/mm.h>
30 #include <linux/ioport.h>
31 #include <linux/init.h>
32 #include <linux/sched.h>
33 #include <linux/random.h>
34 #include <linux/usb.h>
35 #include <linux/videodev2.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-event.h>
38 #include <media/tuner.h>
39 #include <linux/interrupt.h>
40 #include <linux/kthread.h>
41 #include <linux/highmem.h>
42 #include <linux/freezer.h>
44 #include "tm6000-regs.h"
45 #include "tm6000.h"
47 #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
49 /* Limits minimum and default number of buffers */
50 #define TM6000_MIN_BUF 4
51 #define TM6000_DEF_BUF 8
53 #define TM6000_NUM_URB_BUF 8
55 #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
57 /* Declare static vars that will be used as parameters */
58 static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
59 static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
60 static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
61 static bool keep_urb; /* keep urb buffers allocated */
63 /* Debug level */
64 int tm6000_debug;
65 EXPORT_SYMBOL_GPL(tm6000_debug);
67 static struct tm6000_fmt format[] = {
69 .name = "4:2:2, packed, YVY2",
70 .fourcc = V4L2_PIX_FMT_YUYV,
71 .depth = 16,
72 }, {
73 .name = "4:2:2, packed, UYVY",
74 .fourcc = V4L2_PIX_FMT_UYVY,
75 .depth = 16,
76 }, {
77 .name = "A/V + VBI mux packet",
78 .fourcc = V4L2_PIX_FMT_TM6000,
79 .depth = 16,
83 /* ------------------------------------------------------------------
84 * DMA and thread functions
85 * ------------------------------------------------------------------
88 #define norm_maxw(a) 720
89 #define norm_maxh(a) 576
91 #define norm_minw(a) norm_maxw(a)
92 #define norm_minh(a) norm_maxh(a)
95 * video-buf generic routine to get the next available buffer
97 static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
98 struct tm6000_buffer **buf)
100 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
102 if (list_empty(&dma_q->active)) {
103 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
104 *buf = NULL;
105 return;
108 *buf = list_entry(dma_q->active.next,
109 struct tm6000_buffer, vb.queue);
113 * Announces that a buffer were filled and request the next
115 static inline void buffer_filled(struct tm6000_core *dev,
116 struct tm6000_dmaqueue *dma_q,
117 struct tm6000_buffer *buf)
119 /* Advice that buffer was filled */
120 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
121 buf->vb.state = VIDEOBUF_DONE;
122 buf->vb.field_count++;
123 v4l2_get_timestamp(&buf->vb.ts);
125 list_del(&buf->vb.queue);
126 wake_up(&buf->vb.done);
130 * Identify the tm5600/6000 buffer header type and properly handles
132 static int copy_streams(u8 *data, unsigned long len,
133 struct urb *urb)
135 struct tm6000_dmaqueue *dma_q = urb->context;
136 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
137 u8 *ptr = data, *endp = data+len;
138 unsigned long header = 0;
139 int rc = 0;
140 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
141 struct tm6000_buffer *vbuf = NULL;
142 char *voutp = NULL;
143 unsigned int linewidth;
145 if (!dev->radio) {
146 /* get video buffer */
147 get_next_buf(dma_q, &vbuf);
149 if (!vbuf)
150 return rc;
151 voutp = videobuf_to_vmalloc(&vbuf->vb);
153 if (!voutp)
154 return 0;
157 for (ptr = data; ptr < endp;) {
158 if (!dev->isoc_ctl.cmd) {
159 /* Header */
160 if (dev->isoc_ctl.tmp_buf_len > 0) {
161 /* from last urb or packet */
162 header = dev->isoc_ctl.tmp_buf;
163 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
164 memcpy((u8 *)&header +
165 dev->isoc_ctl.tmp_buf_len,
166 ptr,
167 4 - dev->isoc_ctl.tmp_buf_len);
168 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
170 dev->isoc_ctl.tmp_buf_len = 0;
171 } else {
172 if (ptr + 3 >= endp) {
173 /* have incomplete header */
174 dev->isoc_ctl.tmp_buf_len = endp - ptr;
175 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
176 dev->isoc_ctl.tmp_buf_len);
177 return rc;
179 /* Seek for sync */
180 for (; ptr < endp - 3; ptr++) {
181 if (*(ptr + 3) == 0x47)
182 break;
184 /* Get message header */
185 header = *(unsigned long *)ptr;
186 ptr += 4;
189 /* split the header fields */
190 size = ((header & 0x7e) << 1);
191 if (size > 0)
192 size -= 4;
193 block = (header >> 7) & 0xf;
194 field = (header >> 11) & 0x1;
195 line = (header >> 12) & 0x1ff;
196 cmd = (header >> 21) & 0x7;
197 /* Validates haeder fields */
198 if (size > TM6000_URB_MSG_LEN)
199 size = TM6000_URB_MSG_LEN;
200 pktsize = TM6000_URB_MSG_LEN;
202 * calculate position in buffer and change the buffer
204 switch (cmd) {
205 case TM6000_URB_MSG_VIDEO:
206 if (!dev->radio) {
207 if ((dev->isoc_ctl.vfield != field) &&
208 (field == 1)) {
210 * Announces that a new buffer
211 * were filled
213 buffer_filled(dev, dma_q, vbuf);
214 dprintk(dev, V4L2_DEBUG_ISOC,
215 "new buffer filled\n");
216 get_next_buf(dma_q, &vbuf);
217 if (!vbuf)
218 return rc;
219 voutp = videobuf_to_vmalloc(&vbuf->vb);
220 if (!voutp)
221 return rc;
222 memset(voutp, 0, vbuf->vb.size);
224 linewidth = vbuf->vb.width << 1;
225 pos = ((line << 1) - field - 1) *
226 linewidth + block * TM6000_URB_MSG_LEN;
227 /* Don't allow to write out of the buffer */
228 if (pos + size > vbuf->vb.size)
229 cmd = TM6000_URB_MSG_ERR;
230 dev->isoc_ctl.vfield = field;
232 break;
233 case TM6000_URB_MSG_VBI:
234 break;
235 case TM6000_URB_MSG_AUDIO:
236 case TM6000_URB_MSG_PTS:
237 size = pktsize; /* Size is always 180 bytes */
238 break;
240 } else {
241 /* Continue the last copy */
242 cmd = dev->isoc_ctl.cmd;
243 size = dev->isoc_ctl.size;
244 pos = dev->isoc_ctl.pos;
245 pktsize = dev->isoc_ctl.pktsize;
246 field = dev->isoc_ctl.field;
248 cpysize = (endp - ptr > size) ? size : endp - ptr;
249 if (cpysize) {
250 /* copy data in different buffers */
251 switch (cmd) {
252 case TM6000_URB_MSG_VIDEO:
253 /* Fills video buffer */
254 if (vbuf)
255 memcpy(&voutp[pos], ptr, cpysize);
256 break;
257 case TM6000_URB_MSG_AUDIO: {
258 int i;
259 for (i = 0; i < cpysize; i += 2)
260 swab16s((u16 *)(ptr + i));
262 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
263 break;
265 case TM6000_URB_MSG_VBI:
266 /* Need some code to copy vbi buffer */
267 break;
268 case TM6000_URB_MSG_PTS: {
269 /* Need some code to copy pts */
270 u32 pts;
271 pts = *(u32 *)ptr;
272 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
273 field, pts);
274 break;
278 if (ptr + pktsize > endp) {
280 * End of URB packet, but cmd processing is not
281 * complete. Preserve the state for a next packet
283 dev->isoc_ctl.pos = pos + cpysize;
284 dev->isoc_ctl.size = size - cpysize;
285 dev->isoc_ctl.cmd = cmd;
286 dev->isoc_ctl.field = field;
287 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
288 ptr += endp - ptr;
289 } else {
290 dev->isoc_ctl.cmd = 0;
291 ptr += pktsize;
294 return 0;
298 * Identify the tm5600/6000 buffer header type and properly handles
300 static int copy_multiplexed(u8 *ptr, unsigned long len,
301 struct urb *urb)
303 struct tm6000_dmaqueue *dma_q = urb->context;
304 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
305 unsigned int pos = dev->isoc_ctl.pos, cpysize;
306 int rc = 1;
307 struct tm6000_buffer *buf;
308 char *outp = NULL;
310 get_next_buf(dma_q, &buf);
311 if (buf)
312 outp = videobuf_to_vmalloc(&buf->vb);
314 if (!outp)
315 return 0;
317 while (len > 0) {
318 cpysize = min(len, buf->vb.size-pos);
319 memcpy(&outp[pos], ptr, cpysize);
320 pos += cpysize;
321 ptr += cpysize;
322 len -= cpysize;
323 if (pos >= buf->vb.size) {
324 pos = 0;
325 /* Announces that a new buffer were filled */
326 buffer_filled(dev, dma_q, buf);
327 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
328 get_next_buf(dma_q, &buf);
329 if (!buf)
330 break;
331 outp = videobuf_to_vmalloc(&(buf->vb));
332 if (!outp)
333 return rc;
334 pos = 0;
338 dev->isoc_ctl.pos = pos;
339 return rc;
342 static inline void print_err_status(struct tm6000_core *dev,
343 int packet, int status)
345 char *errmsg = "Unknown";
347 switch (status) {
348 case -ENOENT:
349 errmsg = "unlinked synchronuously";
350 break;
351 case -ECONNRESET:
352 errmsg = "unlinked asynchronuously";
353 break;
354 case -ENOSR:
355 errmsg = "Buffer error (overrun)";
356 break;
357 case -EPIPE:
358 errmsg = "Stalled (device not responding)";
359 break;
360 case -EOVERFLOW:
361 errmsg = "Babble (bad cable?)";
362 break;
363 case -EPROTO:
364 errmsg = "Bit-stuff error (bad cable?)";
365 break;
366 case -EILSEQ:
367 errmsg = "CRC/Timeout (could be anything)";
368 break;
369 case -ETIME:
370 errmsg = "Device does not respond";
371 break;
373 if (packet < 0) {
374 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
375 status, errmsg);
376 } else {
377 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
378 packet, status, errmsg);
384 * Controls the isoc copy of each urb packet
386 static inline int tm6000_isoc_copy(struct urb *urb)
388 struct tm6000_dmaqueue *dma_q = urb->context;
389 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
390 int i, len = 0, rc = 1, status;
391 char *p;
393 if (urb->status < 0) {
394 print_err_status(dev, -1, urb->status);
395 return 0;
398 for (i = 0; i < urb->number_of_packets; i++) {
399 status = urb->iso_frame_desc[i].status;
401 if (status < 0) {
402 print_err_status(dev, i, status);
403 continue;
406 len = urb->iso_frame_desc[i].actual_length;
408 if (len > 0) {
409 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
410 if (!urb->iso_frame_desc[i].status) {
411 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
412 rc = copy_multiplexed(p, len, urb);
413 if (rc <= 0)
414 return rc;
415 } else {
416 copy_streams(p, len, urb);
421 return rc;
424 /* ------------------------------------------------------------------
425 * URB control
426 * ------------------------------------------------------------------
430 * IRQ callback, called by URB callback
432 static void tm6000_irq_callback(struct urb *urb)
434 struct tm6000_dmaqueue *dma_q = urb->context;
435 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
436 int i;
438 switch (urb->status) {
439 case 0:
440 case -ETIMEDOUT:
441 break;
443 case -ECONNRESET:
444 case -ENOENT:
445 case -ESHUTDOWN:
446 return;
448 default:
449 tm6000_err("urb completion error %d.\n", urb->status);
450 break;
453 spin_lock(&dev->slock);
454 tm6000_isoc_copy(urb);
455 spin_unlock(&dev->slock);
457 /* Reset urb buffers */
458 for (i = 0; i < urb->number_of_packets; i++) {
459 urb->iso_frame_desc[i].status = 0;
460 urb->iso_frame_desc[i].actual_length = 0;
463 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
464 if (urb->status)
465 tm6000_err("urb resubmit failed (error=%i)\n",
466 urb->status);
470 * Allocate URB buffers
472 static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
474 int num_bufs = TM6000_NUM_URB_BUF;
475 int i;
477 if (dev->urb_buffer != NULL)
478 return 0;
480 dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
481 if (!dev->urb_buffer) {
482 tm6000_err("cannot allocate memory for urb buffers\n");
483 return -ENOMEM;
486 dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
487 if (!dev->urb_dma) {
488 tm6000_err("cannot allocate memory for urb dma pointers\n");
489 return -ENOMEM;
492 for (i = 0; i < num_bufs; i++) {
493 dev->urb_buffer[i] = usb_alloc_coherent(
494 dev->udev, dev->urb_size,
495 GFP_KERNEL, &dev->urb_dma[i]);
496 if (!dev->urb_buffer[i]) {
497 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
498 dev->urb_size, i);
499 return -ENOMEM;
501 memset(dev->urb_buffer[i], 0, dev->urb_size);
504 return 0;
508 * Free URB buffers
510 static int tm6000_free_urb_buffers(struct tm6000_core *dev)
512 int i;
514 if (dev->urb_buffer == NULL)
515 return 0;
517 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
518 if (dev->urb_buffer[i]) {
519 usb_free_coherent(dev->udev,
520 dev->urb_size,
521 dev->urb_buffer[i],
522 dev->urb_dma[i]);
523 dev->urb_buffer[i] = NULL;
526 kfree(dev->urb_buffer);
527 kfree(dev->urb_dma);
528 dev->urb_buffer = NULL;
529 dev->urb_dma = NULL;
531 return 0;
535 * Stop and Deallocate URBs
537 static void tm6000_uninit_isoc(struct tm6000_core *dev)
539 struct urb *urb;
540 int i;
542 dev->isoc_ctl.buf = NULL;
543 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
544 urb = dev->isoc_ctl.urb[i];
545 if (urb) {
546 usb_kill_urb(urb);
547 usb_unlink_urb(urb);
548 usb_free_urb(urb);
549 dev->isoc_ctl.urb[i] = NULL;
551 dev->isoc_ctl.transfer_buffer[i] = NULL;
554 if (!keep_urb)
555 tm6000_free_urb_buffers(dev);
557 kfree(dev->isoc_ctl.urb);
558 kfree(dev->isoc_ctl.transfer_buffer);
560 dev->isoc_ctl.urb = NULL;
561 dev->isoc_ctl.transfer_buffer = NULL;
562 dev->isoc_ctl.num_bufs = 0;
566 * Assign URBs and start IRQ
568 static int tm6000_prepare_isoc(struct tm6000_core *dev)
570 struct tm6000_dmaqueue *dma_q = &dev->vidq;
571 int i, j, sb_size, pipe, size, max_packets;
572 int num_bufs = TM6000_NUM_URB_BUF;
573 struct urb *urb;
575 /* De-allocates all pending stuff */
576 tm6000_uninit_isoc(dev);
577 /* Stop interrupt USB pipe */
578 tm6000_ir_int_stop(dev);
580 usb_set_interface(dev->udev,
581 dev->isoc_in.bInterfaceNumber,
582 dev->isoc_in.bAlternateSetting);
584 /* Start interrupt USB pipe */
585 tm6000_ir_int_start(dev);
587 pipe = usb_rcvisocpipe(dev->udev,
588 dev->isoc_in.endp->desc.bEndpointAddress &
589 USB_ENDPOINT_NUMBER_MASK);
591 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
593 if (size > dev->isoc_in.maxsize)
594 size = dev->isoc_in.maxsize;
596 dev->isoc_ctl.max_pkt_size = size;
598 max_packets = TM6000_MAX_ISO_PACKETS;
599 sb_size = max_packets * size;
600 dev->urb_size = sb_size;
602 dev->isoc_ctl.num_bufs = num_bufs;
604 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
605 if (!dev->isoc_ctl.urb) {
606 tm6000_err("cannot alloc memory for usb buffers\n");
607 return -ENOMEM;
610 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
611 GFP_KERNEL);
612 if (!dev->isoc_ctl.transfer_buffer) {
613 tm6000_err("cannot allocate memory for usbtransfer\n");
614 kfree(dev->isoc_ctl.urb);
615 return -ENOMEM;
618 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets (%d bytes) of %d bytes each to handle %u size\n",
619 max_packets, num_bufs, sb_size,
620 dev->isoc_in.maxsize, size);
623 if (tm6000_alloc_urb_buffers(dev) < 0) {
624 tm6000_err("cannot allocate memory for urb buffers\n");
626 /* call free, as some buffers might have been allocated */
627 tm6000_free_urb_buffers(dev);
628 kfree(dev->isoc_ctl.urb);
629 kfree(dev->isoc_ctl.transfer_buffer);
630 return -ENOMEM;
633 /* allocate urbs and transfer buffers */
634 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
635 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
636 if (!urb) {
637 tm6000_uninit_isoc(dev);
638 usb_free_urb(urb);
639 return -ENOMEM;
641 dev->isoc_ctl.urb[i] = urb;
643 urb->transfer_dma = dev->urb_dma[i];
644 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
646 usb_fill_bulk_urb(urb, dev->udev, pipe,
647 dev->isoc_ctl.transfer_buffer[i], sb_size,
648 tm6000_irq_callback, dma_q);
649 urb->interval = dev->isoc_in.endp->desc.bInterval;
650 urb->number_of_packets = max_packets;
651 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
653 for (j = 0; j < max_packets; j++) {
654 urb->iso_frame_desc[j].offset = size * j;
655 urb->iso_frame_desc[j].length = size;
659 return 0;
662 static int tm6000_start_thread(struct tm6000_core *dev)
664 struct tm6000_dmaqueue *dma_q = &dev->vidq;
665 int i;
667 dma_q->frame = 0;
668 dma_q->ini_jiffies = jiffies;
670 init_waitqueue_head(&dma_q->wq);
672 /* submit urbs and enables IRQ */
673 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
674 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
675 if (rc) {
676 tm6000_err("submit of urb %i failed (error=%i)\n", i,
677 rc);
678 tm6000_uninit_isoc(dev);
679 return rc;
683 return 0;
686 /* ------------------------------------------------------------------
687 * Videobuf operations
688 * ------------------------------------------------------------------
691 static int
692 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
694 struct tm6000_fh *fh = vq->priv_data;
696 *size = fh->fmt->depth * fh->width * fh->height >> 3;
697 if (0 == *count)
698 *count = TM6000_DEF_BUF;
700 if (*count < TM6000_MIN_BUF)
701 *count = TM6000_MIN_BUF;
703 while (*size * *count > vid_limit * 1024 * 1024)
704 (*count)--;
706 return 0;
709 static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
711 struct tm6000_fh *fh = vq->priv_data;
712 struct tm6000_core *dev = fh->dev;
713 unsigned long flags;
715 BUG_ON(in_interrupt());
717 /* We used to wait for the buffer to finish here, but this didn't work
718 because, as we were keeping the state as VIDEOBUF_QUEUED,
719 videobuf_queue_cancel marked it as finished for us.
720 (Also, it could wedge forever if the hardware was misconfigured.)
722 This should be safe; by the time we get here, the buffer isn't
723 queued anymore. If we ever start marking the buffers as
724 VIDEOBUF_ACTIVE, it won't be, though.
726 spin_lock_irqsave(&dev->slock, flags);
727 if (dev->isoc_ctl.buf == buf)
728 dev->isoc_ctl.buf = NULL;
729 spin_unlock_irqrestore(&dev->slock, flags);
731 videobuf_vmalloc_free(&buf->vb);
732 buf->vb.state = VIDEOBUF_NEEDS_INIT;
735 static int
736 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
737 enum v4l2_field field)
739 struct tm6000_fh *fh = vq->priv_data;
740 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
741 struct tm6000_core *dev = fh->dev;
742 int rc = 0;
744 BUG_ON(NULL == fh->fmt);
747 /* FIXME: It assumes depth=2 */
748 /* The only currently supported format is 16 bits/pixel */
749 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
750 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
751 return -EINVAL;
753 if (buf->fmt != fh->fmt ||
754 buf->vb.width != fh->width ||
755 buf->vb.height != fh->height ||
756 buf->vb.field != field) {
757 buf->fmt = fh->fmt;
758 buf->vb.width = fh->width;
759 buf->vb.height = fh->height;
760 buf->vb.field = field;
761 buf->vb.state = VIDEOBUF_NEEDS_INIT;
764 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
765 rc = videobuf_iolock(vq, &buf->vb, NULL);
766 if (rc != 0)
767 goto fail;
770 if (!dev->isoc_ctl.num_bufs) {
771 rc = tm6000_prepare_isoc(dev);
772 if (rc < 0)
773 goto fail;
775 rc = tm6000_start_thread(dev);
776 if (rc < 0)
777 goto fail;
781 buf->vb.state = VIDEOBUF_PREPARED;
782 return 0;
784 fail:
785 free_buffer(vq, buf);
786 return rc;
789 static void
790 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
792 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
793 struct tm6000_fh *fh = vq->priv_data;
794 struct tm6000_core *dev = fh->dev;
795 struct tm6000_dmaqueue *vidq = &dev->vidq;
797 buf->vb.state = VIDEOBUF_QUEUED;
798 list_add_tail(&buf->vb.queue, &vidq->active);
801 static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
803 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
805 free_buffer(vq, buf);
808 static struct videobuf_queue_ops tm6000_video_qops = {
809 .buf_setup = buffer_setup,
810 .buf_prepare = buffer_prepare,
811 .buf_queue = buffer_queue,
812 .buf_release = buffer_release,
815 /* ------------------------------------------------------------------
816 * IOCTL handling
817 * ------------------------------------------------------------------
820 static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
822 /* Is the current fh handling it? if so, that's OK */
823 if (dev->resources == fh && dev->is_res_read)
824 return true;
826 return false;
829 static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
831 /* Is the current fh handling it? if so, that's OK */
832 if (dev->resources == fh)
833 return true;
835 return false;
838 static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
839 bool is_res_read)
841 /* Is the current fh handling it? if so, that's OK */
842 if (dev->resources == fh && dev->is_res_read == is_res_read)
843 return true;
845 /* is it free? */
846 if (dev->resources)
847 return false;
849 /* grab it */
850 dev->resources = fh;
851 dev->is_res_read = is_res_read;
852 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
853 return true;
856 static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
858 /* Is the current fh handling it? if so, that's OK */
859 if (dev->resources != fh)
860 return;
862 dev->resources = NULL;
863 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
866 /* ------------------------------------------------------------------
867 * IOCTL vidioc handling
868 * ------------------------------------------------------------------
870 static int vidioc_querycap(struct file *file, void *priv,
871 struct v4l2_capability *cap)
873 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
874 struct video_device *vdev = video_devdata(file);
876 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
877 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
878 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
879 if (dev->tuner_type != TUNER_ABSENT)
880 cap->device_caps |= V4L2_CAP_TUNER;
881 if (vdev->vfl_type == VFL_TYPE_GRABBER)
882 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
883 V4L2_CAP_STREAMING |
884 V4L2_CAP_READWRITE;
885 else
886 cap->device_caps |= V4L2_CAP_RADIO;
887 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
888 V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
890 return 0;
893 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
894 struct v4l2_fmtdesc *f)
896 if (f->index >= ARRAY_SIZE(format))
897 return -EINVAL;
899 strlcpy(f->description, format[f->index].name, sizeof(f->description));
900 f->pixelformat = format[f->index].fourcc;
901 return 0;
904 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
905 struct v4l2_format *f)
907 struct tm6000_fh *fh = priv;
909 f->fmt.pix.width = fh->width;
910 f->fmt.pix.height = fh->height;
911 f->fmt.pix.field = fh->vb_vidq.field;
912 f->fmt.pix.pixelformat = fh->fmt->fourcc;
913 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
914 f->fmt.pix.bytesperline =
915 (f->fmt.pix.width * fh->fmt->depth) >> 3;
916 f->fmt.pix.sizeimage =
917 f->fmt.pix.height * f->fmt.pix.bytesperline;
919 return 0;
922 static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
924 unsigned int i;
926 for (i = 0; i < ARRAY_SIZE(format); i++)
927 if (format[i].fourcc == fourcc)
928 return format+i;
929 return NULL;
932 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
933 struct v4l2_format *f)
935 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
936 struct tm6000_fmt *fmt;
937 enum v4l2_field field;
939 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
940 if (NULL == fmt) {
941 dprintk(dev, 2, "Fourcc format (0x%08x) invalid.\n",
942 f->fmt.pix.pixelformat);
943 return -EINVAL;
946 field = f->fmt.pix.field;
948 field = V4L2_FIELD_INTERLACED;
950 tm6000_get_std_res(dev);
952 f->fmt.pix.width = dev->width;
953 f->fmt.pix.height = dev->height;
955 f->fmt.pix.width &= ~0x01;
957 f->fmt.pix.field = field;
959 f->fmt.pix.bytesperline =
960 (f->fmt.pix.width * fmt->depth) >> 3;
961 f->fmt.pix.sizeimage =
962 f->fmt.pix.height * f->fmt.pix.bytesperline;
963 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
965 return 0;
968 /*FIXME: This seems to be generic enough to be at videodev2 */
969 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
970 struct v4l2_format *f)
972 struct tm6000_fh *fh = priv;
973 struct tm6000_core *dev = fh->dev;
974 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
975 if (ret < 0)
976 return ret;
978 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
979 fh->width = f->fmt.pix.width;
980 fh->height = f->fmt.pix.height;
981 fh->vb_vidq.field = f->fmt.pix.field;
982 fh->type = f->type;
984 dev->fourcc = f->fmt.pix.pixelformat;
986 tm6000_set_fourcc_format(dev);
988 return 0;
991 static int vidioc_reqbufs(struct file *file, void *priv,
992 struct v4l2_requestbuffers *p)
994 struct tm6000_fh *fh = priv;
996 return videobuf_reqbufs(&fh->vb_vidq, p);
999 static int vidioc_querybuf(struct file *file, void *priv,
1000 struct v4l2_buffer *p)
1002 struct tm6000_fh *fh = priv;
1004 return videobuf_querybuf(&fh->vb_vidq, p);
1007 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1009 struct tm6000_fh *fh = priv;
1011 return videobuf_qbuf(&fh->vb_vidq, p);
1014 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1016 struct tm6000_fh *fh = priv;
1018 return videobuf_dqbuf(&fh->vb_vidq, p,
1019 file->f_flags & O_NONBLOCK);
1022 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1024 struct tm6000_fh *fh = priv;
1025 struct tm6000_core *dev = fh->dev;
1027 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1028 return -EINVAL;
1029 if (i != fh->type)
1030 return -EINVAL;
1032 if (!res_get(dev, fh, false))
1033 return -EBUSY;
1034 return videobuf_streamon(&fh->vb_vidq);
1037 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1039 struct tm6000_fh *fh = priv;
1040 struct tm6000_core *dev = fh->dev;
1042 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1043 return -EINVAL;
1045 if (i != fh->type)
1046 return -EINVAL;
1048 videobuf_streamoff(&fh->vb_vidq);
1049 res_free(dev, fh);
1051 return 0;
1054 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1056 int rc = 0;
1057 struct tm6000_fh *fh = priv;
1058 struct tm6000_core *dev = fh->dev;
1060 dev->norm = norm;
1061 rc = tm6000_init_analog_mode(dev);
1063 fh->width = dev->width;
1064 fh->height = dev->height;
1066 if (rc < 0)
1067 return rc;
1069 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
1071 return 0;
1074 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1076 struct tm6000_fh *fh = priv;
1077 struct tm6000_core *dev = fh->dev;
1079 *norm = dev->norm;
1080 return 0;
1083 static const char *iname[] = {
1084 [TM6000_INPUT_TV] = "Television",
1085 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1086 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1087 [TM6000_INPUT_SVIDEO] = "S-Video",
1090 static int vidioc_enum_input(struct file *file, void *priv,
1091 struct v4l2_input *i)
1093 struct tm6000_fh *fh = priv;
1094 struct tm6000_core *dev = fh->dev;
1095 unsigned int n;
1097 n = i->index;
1098 if (n >= 3)
1099 return -EINVAL;
1101 if (!dev->vinput[n].type)
1102 return -EINVAL;
1104 i->index = n;
1106 if (dev->vinput[n].type == TM6000_INPUT_TV)
1107 i->type = V4L2_INPUT_TYPE_TUNER;
1108 else
1109 i->type = V4L2_INPUT_TYPE_CAMERA;
1111 strcpy(i->name, iname[dev->vinput[n].type]);
1113 i->std = TM6000_STD;
1115 return 0;
1118 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1120 struct tm6000_fh *fh = priv;
1121 struct tm6000_core *dev = fh->dev;
1123 *i = dev->input;
1125 return 0;
1128 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1130 struct tm6000_fh *fh = priv;
1131 struct tm6000_core *dev = fh->dev;
1132 int rc = 0;
1134 if (i >= 3)
1135 return -EINVAL;
1136 if (!dev->vinput[i].type)
1137 return -EINVAL;
1139 dev->input = i;
1141 rc = vidioc_s_std(file, priv, dev->norm);
1143 return rc;
1146 /* --- controls ---------------------------------------------- */
1148 static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
1150 struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1151 u8 val = ctrl->val;
1153 switch (ctrl->id) {
1154 case V4L2_CID_CONTRAST:
1155 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
1156 return 0;
1157 case V4L2_CID_BRIGHTNESS:
1158 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
1159 return 0;
1160 case V4L2_CID_SATURATION:
1161 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
1162 return 0;
1163 case V4L2_CID_HUE:
1164 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
1165 return 0;
1167 return -EINVAL;
1170 static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1171 .s_ctrl = tm6000_s_ctrl,
1174 static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1176 struct tm6000_core *dev = container_of(ctrl->handler,
1177 struct tm6000_core, radio_ctrl_handler);
1178 u8 val = ctrl->val;
1180 switch (ctrl->id) {
1181 case V4L2_CID_AUDIO_MUTE:
1182 dev->ctl_mute = val;
1183 tm6000_tvaudio_set_mute(dev, val);
1184 return 0;
1185 case V4L2_CID_AUDIO_VOLUME:
1186 dev->ctl_volume = val;
1187 tm6000_set_volume(dev, val);
1188 return 0;
1190 return -EINVAL;
1193 static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1194 .s_ctrl = tm6000_radio_s_ctrl,
1197 static int vidioc_g_tuner(struct file *file, void *priv,
1198 struct v4l2_tuner *t)
1200 struct tm6000_fh *fh = priv;
1201 struct tm6000_core *dev = fh->dev;
1203 if (UNSET == dev->tuner_type)
1204 return -ENOTTY;
1205 if (0 != t->index)
1206 return -EINVAL;
1208 strcpy(t->name, "Television");
1209 t->type = V4L2_TUNER_ANALOG_TV;
1210 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
1211 t->rangehigh = 0xffffffffUL;
1212 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1214 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1216 t->audmode = dev->amode;
1218 return 0;
1221 static int vidioc_s_tuner(struct file *file, void *priv,
1222 const struct v4l2_tuner *t)
1224 struct tm6000_fh *fh = priv;
1225 struct tm6000_core *dev = fh->dev;
1227 if (UNSET == dev->tuner_type)
1228 return -ENOTTY;
1229 if (0 != t->index)
1230 return -EINVAL;
1232 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1233 dev->amode = V4L2_TUNER_MODE_STEREO;
1234 else
1235 dev->amode = t->audmode;
1236 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1238 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1240 return 0;
1243 static int vidioc_g_frequency(struct file *file, void *priv,
1244 struct v4l2_frequency *f)
1246 struct tm6000_fh *fh = priv;
1247 struct tm6000_core *dev = fh->dev;
1249 if (UNSET == dev->tuner_type)
1250 return -ENOTTY;
1251 if (f->tuner)
1252 return -EINVAL;
1254 f->frequency = dev->freq;
1256 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1258 return 0;
1261 static int vidioc_s_frequency(struct file *file, void *priv,
1262 const struct v4l2_frequency *f)
1264 struct tm6000_fh *fh = priv;
1265 struct tm6000_core *dev = fh->dev;
1267 if (UNSET == dev->tuner_type)
1268 return -ENOTTY;
1269 if (f->tuner != 0)
1270 return -EINVAL;
1272 dev->freq = f->frequency;
1273 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1275 return 0;
1278 static int radio_g_tuner(struct file *file, void *priv,
1279 struct v4l2_tuner *t)
1281 struct tm6000_fh *fh = file->private_data;
1282 struct tm6000_core *dev = fh->dev;
1284 if (0 != t->index)
1285 return -EINVAL;
1287 memset(t, 0, sizeof(*t));
1288 strcpy(t->name, "Radio");
1289 t->type = V4L2_TUNER_RADIO;
1290 t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1291 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1292 t->audmode = V4L2_TUNER_MODE_STEREO;
1294 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1296 return 0;
1299 static int radio_s_tuner(struct file *file, void *priv,
1300 const struct v4l2_tuner *t)
1302 struct tm6000_fh *fh = file->private_data;
1303 struct tm6000_core *dev = fh->dev;
1305 if (0 != t->index)
1306 return -EINVAL;
1307 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1308 return 0;
1311 /* ------------------------------------------------------------------
1312 File operations for the device
1313 ------------------------------------------------------------------*/
1315 static int __tm6000_open(struct file *file)
1317 struct video_device *vdev = video_devdata(file);
1318 struct tm6000_core *dev = video_drvdata(file);
1319 struct tm6000_fh *fh;
1320 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1321 int rc;
1322 int radio = 0;
1324 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1325 video_device_node_name(vdev));
1327 switch (vdev->vfl_type) {
1328 case VFL_TYPE_GRABBER:
1329 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1330 break;
1331 case VFL_TYPE_VBI:
1332 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1333 break;
1334 case VFL_TYPE_RADIO:
1335 radio = 1;
1336 break;
1339 /* If more than one user, mutex should be added */
1340 dev->users++;
1342 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1343 video_device_node_name(vdev), v4l2_type_names[type],
1344 dev->users);
1346 /* allocate + initialize per filehandle data */
1347 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1348 if (NULL == fh) {
1349 dev->users--;
1350 return -ENOMEM;
1353 v4l2_fh_init(&fh->fh, vdev);
1354 file->private_data = fh;
1355 fh->dev = dev;
1356 fh->radio = radio;
1357 dev->radio = radio;
1358 fh->type = type;
1359 dev->fourcc = format[0].fourcc;
1361 fh->fmt = format_by_fourcc(dev->fourcc);
1363 tm6000_get_std_res(dev);
1365 fh->width = dev->width;
1366 fh->height = dev->height;
1368 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1369 (unsigned long)fh, (unsigned long)dev,
1370 (unsigned long)&dev->vidq);
1371 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty queued=%d\n",
1372 list_empty(&dev->vidq.queued));
1373 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty active=%d\n",
1374 list_empty(&dev->vidq.active));
1376 /* initialize hardware on analog mode */
1377 rc = tm6000_init_analog_mode(dev);
1378 if (rc < 0)
1379 return rc;
1381 dev->mode = TM6000_MODE_ANALOG;
1383 if (!fh->radio) {
1384 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1385 NULL, &dev->slock,
1386 fh->type,
1387 V4L2_FIELD_INTERLACED,
1388 sizeof(struct tm6000_buffer), fh, &dev->lock);
1389 } else {
1390 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
1391 tm6000_set_audio_rinput(dev);
1392 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1393 tm6000_prepare_isoc(dev);
1394 tm6000_start_thread(dev);
1396 v4l2_fh_add(&fh->fh);
1398 return 0;
1401 static int tm6000_open(struct file *file)
1403 struct video_device *vdev = video_devdata(file);
1404 int res;
1406 mutex_lock(vdev->lock);
1407 res = __tm6000_open(file);
1408 mutex_unlock(vdev->lock);
1409 return res;
1412 static ssize_t
1413 tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1415 struct tm6000_fh *fh = file->private_data;
1416 struct tm6000_core *dev = fh->dev;
1418 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1419 int res;
1421 if (!res_get(fh->dev, fh, true))
1422 return -EBUSY;
1424 if (mutex_lock_interruptible(&dev->lock))
1425 return -ERESTARTSYS;
1426 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1427 file->f_flags & O_NONBLOCK);
1428 mutex_unlock(&dev->lock);
1429 return res;
1431 return 0;
1434 static unsigned int
1435 __tm6000_poll(struct file *file, struct poll_table_struct *wait)
1437 unsigned long req_events = poll_requested_events(wait);
1438 struct tm6000_fh *fh = file->private_data;
1439 struct tm6000_buffer *buf;
1440 int res = 0;
1442 if (v4l2_event_pending(&fh->fh))
1443 res = POLLPRI;
1444 else if (req_events & POLLPRI)
1445 poll_wait(file, &fh->fh.wait, wait);
1446 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1447 return res | POLLERR;
1449 if (!!is_res_streaming(fh->dev, fh))
1450 return res | POLLERR;
1452 if (!is_res_read(fh->dev, fh)) {
1453 /* streaming capture */
1454 if (list_empty(&fh->vb_vidq.stream))
1455 return res | POLLERR;
1456 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
1457 poll_wait(file, &buf->vb.done, wait);
1458 if (buf->vb.state == VIDEOBUF_DONE ||
1459 buf->vb.state == VIDEOBUF_ERROR)
1460 return res | POLLIN | POLLRDNORM;
1461 } else if (req_events & (POLLIN | POLLRDNORM)) {
1462 /* read() capture */
1463 return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
1465 return res;
1468 static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1470 struct tm6000_fh *fh = file->private_data;
1471 struct tm6000_core *dev = fh->dev;
1472 unsigned int res;
1474 mutex_lock(&dev->lock);
1475 res = __tm6000_poll(file, wait);
1476 mutex_unlock(&dev->lock);
1477 return res;
1480 static int tm6000_release(struct file *file)
1482 struct tm6000_fh *fh = file->private_data;
1483 struct tm6000_core *dev = fh->dev;
1484 struct video_device *vdev = video_devdata(file);
1486 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1487 video_device_node_name(vdev), dev->users);
1489 mutex_lock(&dev->lock);
1490 dev->users--;
1492 res_free(dev, fh);
1494 if (!dev->users) {
1495 tm6000_uninit_isoc(dev);
1497 /* Stop interrupt USB pipe */
1498 tm6000_ir_int_stop(dev);
1500 usb_reset_configuration(dev->udev);
1502 if (dev->int_in.endp)
1503 usb_set_interface(dev->udev,
1504 dev->isoc_in.bInterfaceNumber, 2);
1505 else
1506 usb_set_interface(dev->udev,
1507 dev->isoc_in.bInterfaceNumber, 0);
1509 /* Start interrupt USB pipe */
1510 tm6000_ir_int_start(dev);
1512 if (!fh->radio)
1513 videobuf_mmap_free(&fh->vb_vidq);
1515 v4l2_fh_del(&fh->fh);
1516 v4l2_fh_exit(&fh->fh);
1517 kfree(fh);
1518 mutex_unlock(&dev->lock);
1520 return 0;
1523 static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1525 struct tm6000_fh *fh = file->private_data;
1526 struct tm6000_core *dev = fh->dev;
1527 int res;
1529 if (mutex_lock_interruptible(&dev->lock))
1530 return -ERESTARTSYS;
1531 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1532 mutex_unlock(&dev->lock);
1533 return res;
1536 static struct v4l2_file_operations tm6000_fops = {
1537 .owner = THIS_MODULE,
1538 .open = tm6000_open,
1539 .release = tm6000_release,
1540 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1541 .read = tm6000_read,
1542 .poll = tm6000_poll,
1543 .mmap = tm6000_mmap,
1546 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1547 .vidioc_querycap = vidioc_querycap,
1548 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1549 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1550 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1551 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1552 .vidioc_s_std = vidioc_s_std,
1553 .vidioc_g_std = vidioc_g_std,
1554 .vidioc_enum_input = vidioc_enum_input,
1555 .vidioc_g_input = vidioc_g_input,
1556 .vidioc_s_input = vidioc_s_input,
1557 .vidioc_g_tuner = vidioc_g_tuner,
1558 .vidioc_s_tuner = vidioc_s_tuner,
1559 .vidioc_g_frequency = vidioc_g_frequency,
1560 .vidioc_s_frequency = vidioc_s_frequency,
1561 .vidioc_streamon = vidioc_streamon,
1562 .vidioc_streamoff = vidioc_streamoff,
1563 .vidioc_reqbufs = vidioc_reqbufs,
1564 .vidioc_querybuf = vidioc_querybuf,
1565 .vidioc_qbuf = vidioc_qbuf,
1566 .vidioc_dqbuf = vidioc_dqbuf,
1567 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1568 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1571 static struct video_device tm6000_template = {
1572 .name = "tm6000",
1573 .fops = &tm6000_fops,
1574 .ioctl_ops = &video_ioctl_ops,
1575 .release = video_device_release_empty,
1576 .tvnorms = TM6000_STD,
1579 static const struct v4l2_file_operations radio_fops = {
1580 .owner = THIS_MODULE,
1581 .open = tm6000_open,
1582 .poll = v4l2_ctrl_poll,
1583 .release = tm6000_release,
1584 .unlocked_ioctl = video_ioctl2,
1587 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1588 .vidioc_querycap = vidioc_querycap,
1589 .vidioc_g_tuner = radio_g_tuner,
1590 .vidioc_s_tuner = radio_s_tuner,
1591 .vidioc_g_frequency = vidioc_g_frequency,
1592 .vidioc_s_frequency = vidioc_s_frequency,
1593 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1594 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1597 static struct video_device tm6000_radio_template = {
1598 .name = "tm6000",
1599 .fops = &radio_fops,
1600 .ioctl_ops = &radio_ioctl_ops,
1603 /* -----------------------------------------------------------------
1604 * Initialization and module stuff
1605 * ------------------------------------------------------------------
1608 static void vdev_init(struct tm6000_core *dev,
1609 struct video_device *vfd,
1610 const struct video_device
1611 *template, const char *type_name)
1613 *vfd = *template;
1614 vfd->v4l2_dev = &dev->v4l2_dev;
1615 vfd->release = video_device_release_empty;
1616 vfd->lock = &dev->lock;
1618 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1620 video_set_drvdata(vfd, dev);
1623 int tm6000_v4l2_register(struct tm6000_core *dev)
1625 int ret = 0;
1627 v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1628 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1629 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1630 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1631 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1632 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1633 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1634 V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1635 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1636 V4L2_CID_CONTRAST, 0, 255, 1, 119);
1637 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1638 V4L2_CID_SATURATION, 0, 255, 1, 112);
1639 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1640 V4L2_CID_HUE, -128, 127, 1, 0);
1641 v4l2_ctrl_add_handler(&dev->ctrl_handler,
1642 &dev->radio_ctrl_handler, NULL);
1644 if (dev->radio_ctrl_handler.error)
1645 ret = dev->radio_ctrl_handler.error;
1646 if (!ret && dev->ctrl_handler.error)
1647 ret = dev->ctrl_handler.error;
1648 if (ret)
1649 goto free_ctrl;
1651 vdev_init(dev, &dev->vfd, &tm6000_template, "video");
1653 dev->vfd.ctrl_handler = &dev->ctrl_handler;
1655 /* init video dma queues */
1656 INIT_LIST_HEAD(&dev->vidq.active);
1657 INIT_LIST_HEAD(&dev->vidq.queued);
1659 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
1661 if (ret < 0) {
1662 printk(KERN_INFO "%s: can't register video device\n",
1663 dev->name);
1664 goto free_ctrl;
1667 printk(KERN_INFO "%s: registered device %s\n",
1668 dev->name, video_device_node_name(&dev->vfd));
1670 if (dev->caps.has_radio) {
1671 vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
1672 "radio");
1673 dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1674 ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
1675 radio_nr);
1676 if (ret < 0) {
1677 printk(KERN_INFO "%s: can't register radio device\n",
1678 dev->name);
1679 goto unreg_video;
1682 printk(KERN_INFO "%s: registered device %s\n",
1683 dev->name, video_device_node_name(&dev->radio_dev));
1686 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
1687 return ret;
1689 unreg_video:
1690 video_unregister_device(&dev->vfd);
1691 free_ctrl:
1692 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1693 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1694 return ret;
1697 int tm6000_v4l2_unregister(struct tm6000_core *dev)
1699 video_unregister_device(&dev->vfd);
1701 /* if URB buffers are still allocated free them now */
1702 tm6000_free_urb_buffers(dev);
1704 video_unregister_device(&dev->radio_dev);
1705 return 0;
1708 int tm6000_v4l2_exit(void)
1710 return 0;
1713 module_param(video_nr, int, 0);
1714 MODULE_PARM_DESC(video_nr, "Allow changing video device number");
1716 module_param_named(debug, tm6000_debug, int, 0444);
1717 MODULE_PARM_DESC(debug, "activates debug info");
1719 module_param(vid_limit, int, 0644);
1720 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
1722 module_param(keep_urb, bool, 0);
1723 MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");