[media] tm6000: fix querycap and input/tuner compliance issues
[deliverable/linux.git] / drivers / media / usb / tm6000 / tm6000-video.c
CommitLineData
9701dc94 1/*
60fbfdfd
RP
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.
9701dc94 21 */
3d1a51db 22
9701dc94
MCC
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>
9701dc94
MCC
34#include <linux/usb.h>
35#include <linux/videodev2.h>
2a8145d4 36#include <media/v4l2-ioctl.h>
886a3c0b 37#include <media/tuner.h>
9701dc94
MCC
38#include <linux/interrupt.h>
39#include <linux/kthread.h>
40#include <linux/highmem.h>
41#include <linux/freezer.h>
42
43#include "tm6000-regs.h"
44#include "tm6000.h"
45
46#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
47
95a83824
ML
48/* Limits minimum and default number of buffers */
49#define TM6000_MIN_BUF 4
50#define TM6000_DEF_BUF 8
51
16427faf
JS
52#define TM6000_NUM_URB_BUF 8
53
5a4b55e2 54#define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
ee1fc07c 55
9701dc94
MCC
56/* Declare static vars that will be used as parameters */
57static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
58static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
8aff8ba9 59static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
b391b0ef 60static bool keep_urb; /* keep urb buffers allocated */
9701dc94 61
9701dc94
MCC
62/* Debug level */
63int tm6000_debug;
faa7c134 64EXPORT_SYMBOL_GPL(tm6000_debug);
9701dc94 65
8aff8ba9
DB
66static const struct v4l2_queryctrl no_ctrl = {
67 .name = "42",
68 .flags = V4L2_CTRL_FLAG_DISABLED,
69};
70
9701dc94
MCC
71/* supported controls */
72static struct v4l2_queryctrl tm6000_qctrl[] = {
73 {
74 .id = V4L2_CID_BRIGHTNESS,
75 .type = V4L2_CTRL_TYPE_INTEGER,
76 .name = "Brightness",
77 .minimum = 0,
78 .maximum = 255,
79 .step = 1,
80 .default_value = 54,
81 .flags = 0,
82 }, {
83 .id = V4L2_CID_CONTRAST,
84 .type = V4L2_CTRL_TYPE_INTEGER,
85 .name = "Contrast",
86 .minimum = 0,
87 .maximum = 255,
88 .step = 0x1,
89 .default_value = 119,
90 .flags = 0,
91 }, {
92 .id = V4L2_CID_SATURATION,
93 .type = V4L2_CTRL_TYPE_INTEGER,
94 .name = "Saturation",
95 .minimum = 0,
96 .maximum = 255,
97 .step = 0x1,
98 .default_value = 112,
99 .flags = 0,
100 }, {
101 .id = V4L2_CID_HUE,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Hue",
104 .minimum = -128,
105 .maximum = 127,
106 .step = 0x1,
c4acf48c 107 .default_value = 0,
9701dc94 108 .flags = 0,
8aff8ba9
DB
109 },
110 /* --- audio --- */
111 {
112 .id = V4L2_CID_AUDIO_MUTE,
113 .name = "Mute",
114 .minimum = 0,
115 .maximum = 1,
116 .type = V4L2_CTRL_TYPE_BOOLEAN,
117 }, {
118 .id = V4L2_CID_AUDIO_VOLUME,
119 .name = "Volume",
120 .minimum = -15,
121 .maximum = 15,
122 .step = 1,
123 .default_value = 0,
124 .type = V4L2_CTRL_TYPE_INTEGER,
9701dc94
MCC
125 }
126};
127
8aff8ba9 128static const unsigned int CTRLS = ARRAY_SIZE(tm6000_qctrl);
9701dc94
MCC
129static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
130
131static struct tm6000_fmt format[] = {
132 {
133 .name = "4:2:2, packed, YVY2",
134 .fourcc = V4L2_PIX_FMT_YUYV,
135 .depth = 16,
c4acf48c 136 }, {
9701dc94
MCC
137 .name = "4:2:2, packed, UYVY",
138 .fourcc = V4L2_PIX_FMT_UYVY,
139 .depth = 16,
c4acf48c 140 }, {
9701dc94
MCC
141 .name = "A/V + VBI mux packet",
142 .fourcc = V4L2_PIX_FMT_TM6000,
143 .depth = 16,
144 }
145};
146
8aff8ba9
DB
147static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
148{
149 unsigned int i;
150
151 for (i = 0; i < CTRLS; i++)
152 if (tm6000_qctrl[i].id == id)
153 return tm6000_qctrl+i;
154 return NULL;
155}
156
9701dc94 157/* ------------------------------------------------------------------
60fbfdfd
RP
158 * DMA and thread functions
159 * ------------------------------------------------------------------
160 */
9701dc94
MCC
161
162#define norm_maxw(a) 720
4475c044 163#define norm_maxh(a) 576
9701dc94 164
9701dc94
MCC
165#define norm_minw(a) norm_maxw(a)
166#define norm_minh(a) norm_maxh(a)
167
168/*
169 * video-buf generic routine to get the next available buffer
170 */
721f507b 171static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
c4acf48c 172 struct tm6000_buffer **buf)
9701dc94 173{
c4acf48c 174 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
9701dc94
MCC
175
176 if (list_empty(&dma_q->active)) {
c4acf48c 177 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
1f9305b7 178 *buf = NULL;
721f507b 179 return;
9701dc94
MCC
180 }
181
182 *buf = list_entry(dma_q->active.next,
183 struct tm6000_buffer, vb.queue);
9701dc94
MCC
184}
185
186/*
187 * Announces that a buffer were filled and request the next
188 */
c4acf48c
MCC
189static inline void buffer_filled(struct tm6000_core *dev,
190 struct tm6000_dmaqueue *dma_q,
191 struct tm6000_buffer *buf)
9701dc94
MCC
192{
193 /* Advice that buffer was filled */
c4acf48c 194 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
47878f16 195 buf->vb.state = VIDEOBUF_DONE;
9701dc94 196 buf->vb.field_count++;
8e6057b5 197 v4l2_get_timestamp(&buf->vb.ts);
9701dc94
MCC
198
199 list_del(&buf->vb.queue);
200 wake_up(&buf->vb.done);
201}
202
9701dc94
MCC
203/*
204 * Identify the tm5600/6000 buffer header type and properly handles
205 */
4b6ed9fd
SR
206static int copy_streams(u8 *data, unsigned long len,
207 struct urb *urb)
e2c9500d
MCC
208{
209 struct tm6000_dmaqueue *dma_q = urb->context;
60fbfdfd 210 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
8173090a 211 u8 *ptr = data, *endp = data+len;
60fbfdfd
RP
212 unsigned long header = 0;
213 int rc = 0;
83cb9a50 214 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
cc73b4b5 215 struct tm6000_buffer *vbuf = NULL;
83cb9a50
SR
216 char *voutp = NULL;
217 unsigned int linewidth;
218
8aff8ba9
DB
219 if (!dev->radio) {
220 /* get video buffer */
221 get_next_buf(dma_q, &vbuf);
222
223 if (!vbuf)
224 return rc;
225 voutp = videobuf_to_vmalloc(&vbuf->vb);
226
227 if (!voutp)
228 return 0;
229 }
ed0236af 230
83cb9a50 231 for (ptr = data; ptr < endp;) {
ed0236af 232 if (!dev->isoc_ctl.cmd) {
83cb9a50
SR
233 /* Header */
234 if (dev->isoc_ctl.tmp_buf_len > 0) {
235 /* from last urb or packet */
236 header = dev->isoc_ctl.tmp_buf;
237 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
60fbfdfd 238 memcpy((u8 *)&header +
801dd3b3 239 dev->isoc_ctl.tmp_buf_len,
ed0236af 240 ptr,
801dd3b3
MCC
241 4 - dev->isoc_ctl.tmp_buf_len);
242 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
ed0236af 243 }
83cb9a50
SR
244 dev->isoc_ctl.tmp_buf_len = 0;
245 } else {
246 if (ptr + 3 >= endp) {
247 /* have incomplete header */
248 dev->isoc_ctl.tmp_buf_len = endp - ptr;
60fbfdfd 249 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
83cb9a50
SR
250 dev->isoc_ctl.tmp_buf_len);
251 return rc;
252 }
253 /* Seek for sync */
254 for (; ptr < endp - 3; ptr++) {
255 if (*(ptr + 3) == 0x47)
256 break;
257 }
258 /* Get message header */
259 header = *(unsigned long *)ptr;
260 ptr += 4;
ed0236af 261 }
23ba9463 262
83cb9a50 263 /* split the header fields */
83cb9a50
SR
264 size = ((header & 0x7e) << 1);
265 if (size > 0)
266 size -= 4;
267 block = (header >> 7) & 0xf;
268 field = (header >> 11) & 0x1;
269 line = (header >> 12) & 0x1ff;
270 cmd = (header >> 21) & 0x7;
271 /* Validates haeder fields */
272 if (size > TM6000_URB_MSG_LEN)
273 size = TM6000_URB_MSG_LEN;
274 pktsize = TM6000_URB_MSG_LEN;
3d1a51db
TR
275 /*
276 * calculate position in buffer and change the buffer
83cb9a50
SR
277 */
278 switch (cmd) {
279 case TM6000_URB_MSG_VIDEO:
8aff8ba9
DB
280 if (!dev->radio) {
281 if ((dev->isoc_ctl.vfield != field) &&
282 (field == 1)) {
3d1a51db
TR
283 /*
284 * Announces that a new buffer
285 * were filled
286 */
8aff8ba9
DB
287 buffer_filled(dev, dma_q, vbuf);
288 dprintk(dev, V4L2_DEBUG_ISOC,
83cb9a50 289 "new buffer filled\n");
8aff8ba9
DB
290 get_next_buf(dma_q, &vbuf);
291 if (!vbuf)
292 return rc;
293 voutp = videobuf_to_vmalloc(&vbuf->vb);
294 if (!voutp)
295 return rc;
296 memset(voutp, 0, vbuf->vb.size);
297 }
298 linewidth = vbuf->vb.width << 1;
299 pos = ((line << 1) - field - 1) *
300 linewidth + block * TM6000_URB_MSG_LEN;
301 /* Don't allow to write out of the buffer */
302 if (pos + size > vbuf->vb.size)
303 cmd = TM6000_URB_MSG_ERR;
304 dev->isoc_ctl.vfield = field;
cc73b4b5 305 }
83cb9a50 306 break;
83cb9a50 307 case TM6000_URB_MSG_VBI:
23ba9463
MCC
308 break;
309 case TM6000_URB_MSG_AUDIO:
83cb9a50 310 case TM6000_URB_MSG_PTS:
3d1a51db 311 size = pktsize; /* Size is always 180 bytes */
83cb9a50 312 break;
9701dc94 313 }
83cb9a50
SR
314 } else {
315 /* Continue the last copy */
316 cmd = dev->isoc_ctl.cmd;
317 size = dev->isoc_ctl.size;
318 pos = dev->isoc_ctl.pos;
319 pktsize = dev->isoc_ctl.pktsize;
423c79e3 320 field = dev->isoc_ctl.field;
83cb9a50
SR
321 }
322 cpysize = (endp - ptr > size) ? size : endp - ptr;
323 if (cpysize) {
324 /* copy data in different buffers */
325 switch (cmd) {
326 case TM6000_URB_MSG_VIDEO:
327 /* Fills video buffer */
328 if (vbuf)
60fbfdfd 329 memcpy(&voutp[pos], ptr, cpysize);
83cb9a50 330 break;
7ecff8c9
SR
331 case TM6000_URB_MSG_AUDIO: {
332 int i;
333 for (i = 0; i < cpysize; i += 2)
334 swab16s((u16 *)(ptr + i));
73f4d265 335
b17b8699 336 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
83cb9a50 337 break;
7ecff8c9 338 }
83cb9a50
SR
339 case TM6000_URB_MSG_VBI:
340 /* Need some code to copy vbi buffer */
341 break;
2f349daa 342 case TM6000_URB_MSG_PTS: {
83cb9a50 343 /* Need some code to copy pts */
2f349daa
SR
344 u32 pts;
345 pts = *(u32 *)ptr;
423c79e3
SR
346 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
347 field, pts);
83cb9a50 348 break;
cc6c60d9 349 }
2f349daa 350 }
ed0236af 351 }
ccfb3028 352 if (ptr + pktsize > endp) {
3d1a51db
TR
353 /*
354 * End of URB packet, but cmd processing is not
83cb9a50
SR
355 * complete. Preserve the state for a next packet
356 */
357 dev->isoc_ctl.pos = pos + cpysize;
358 dev->isoc_ctl.size = size - cpysize;
359 dev->isoc_ctl.cmd = cmd;
423c79e3 360 dev->isoc_ctl.field = field;
83cb9a50 361 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
ccfb3028 362 ptr += endp - ptr;
83cb9a50
SR
363 } else {
364 dev->isoc_ctl.cmd = 0;
365 ptr += pktsize;
a228618c 366 }
9701dc94 367 }
a228618c 368 return 0;
9701dc94 369}
83cb9a50 370
9701dc94
MCC
371/*
372 * Identify the tm5600/6000 buffer header type and properly handles
373 */
4b6ed9fd
SR
374static int copy_multiplexed(u8 *ptr, unsigned long len,
375 struct urb *urb)
9701dc94
MCC
376{
377 struct tm6000_dmaqueue *dma_q = urb->context;
60fbfdfd
RP
378 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
379 unsigned int pos = dev->isoc_ctl.pos, cpysize;
380 int rc = 1;
4b6ed9fd
SR
381 struct tm6000_buffer *buf;
382 char *outp = NULL;
383
384 get_next_buf(dma_q, &buf);
385 if (buf)
386 outp = videobuf_to_vmalloc(&buf->vb);
387
388 if (!outp)
389 return 0;
ed0236af 390
60fbfdfd
RP
391 while (len > 0) {
392 cpysize = min(len, buf->vb.size-pos);
4b6ed9fd 393 memcpy(&outp[pos], ptr, cpysize);
60fbfdfd
RP
394 pos += cpysize;
395 ptr += cpysize;
396 len -= cpysize;
4b6ed9fd 397 if (pos >= buf->vb.size) {
60fbfdfd 398 pos = 0;
9701dc94 399 /* Announces that a new buffer were filled */
60fbfdfd 400 buffer_filled(dev, dma_q, buf);
5f796752 401 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
60fbfdfd 402 get_next_buf(dma_q, &buf);
4b6ed9fd 403 if (!buf)
9701dc94 404 break;
4b6ed9fd
SR
405 outp = videobuf_to_vmalloc(&(buf->vb));
406 if (!outp)
e8a4845d
MCC
407 return rc;
408 pos = 0;
9701dc94
MCC
409 }
410 }
411
60fbfdfd 412 dev->isoc_ctl.pos = pos;
9701dc94
MCC
413 return rc;
414}
415
60fbfdfd 416static inline void print_err_status(struct tm6000_core *dev,
ed0236af 417 int packet, int status)
5f796752
MCC
418{
419 char *errmsg = "Unknown";
420
60fbfdfd 421 switch (status) {
5f796752
MCC
422 case -ENOENT:
423 errmsg = "unlinked synchronuously";
424 break;
425 case -ECONNRESET:
426 errmsg = "unlinked asynchronuously";
427 break;
428 case -ENOSR:
429 errmsg = "Buffer error (overrun)";
430 break;
431 case -EPIPE:
432 errmsg = "Stalled (device not responding)";
433 break;
434 case -EOVERFLOW:
435 errmsg = "Babble (bad cable?)";
436 break;
437 case -EPROTO:
438 errmsg = "Bit-stuff error (bad cable?)";
439 break;
440 case -EILSEQ:
441 errmsg = "CRC/Timeout (could be anything)";
442 break;
443 case -ETIME:
444 errmsg = "Device does not respond";
445 break;
446 }
60fbfdfd 447 if (packet < 0) {
5f796752
MCC
448 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
449 status, errmsg);
450 } else {
ed0236af 451 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
5f796752
MCC
452 packet, status, errmsg);
453 }
454}
455
456
9701dc94
MCC
457/*
458 * Controls the isoc copy of each urb packet
459 */
e8a4845d 460static inline int tm6000_isoc_copy(struct urb *urb)
9701dc94
MCC
461{
462 struct tm6000_dmaqueue *dma_q = urb->context;
60fbfdfd
RP
463 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
464 int i, len = 0, rc = 1, status;
4b6ed9fd 465 char *p;
9701dc94 466
4b6ed9fd 467 if (urb->status < 0) {
60fbfdfd 468 print_err_status(dev, -1, urb->status);
5f796752
MCC
469 return 0;
470 }
9701dc94
MCC
471
472 for (i = 0; i < urb->number_of_packets; i++) {
4b6ed9fd 473 status = urb->iso_frame_desc[i].status;
9701dc94 474
60fbfdfd
RP
475 if (status < 0) {
476 print_err_status(dev, i, status);
9701dc94 477 continue;
5f796752 478 }
9701dc94 479
4b6ed9fd 480 len = urb->iso_frame_desc[i].actual_length;
ed0236af 481
4b6ed9fd
SR
482 if (len > 0) {
483 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
ed0236af 484 if (!urb->iso_frame_desc[i].status) {
60fbfdfd
RP
485 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
486 rc = copy_multiplexed(p, len, urb);
487 if (rc <= 0)
ed0236af
MCC
488 return rc;
489 } else {
4b6ed9fd 490 copy_streams(p, len, urb);
ed0236af
MCC
491 }
492 }
4b6ed9fd 493 }
9701dc94 494 }
9701dc94
MCC
495 return rc;
496}
497
498/* ------------------------------------------------------------------
60fbfdfd
RP
499 * URB control
500 * ------------------------------------------------------------------
501 */
9701dc94
MCC
502
503/*
504 * IRQ callback, called by URB callback
505 */
506static void tm6000_irq_callback(struct urb *urb)
507{
9701dc94 508 struct tm6000_dmaqueue *dma_q = urb->context;
c4acf48c 509 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
e8a4845d 510 int i;
9701dc94 511
14f09154
TR
512 switch (urb->status) {
513 case 0:
514 case -ETIMEDOUT:
515 break;
516
517 case -ECONNRESET:
518 case -ENOENT:
519 case -ESHUTDOWN:
721f507b 520 return;
a228618c 521
14f09154
TR
522 default:
523 tm6000_err("urb completion error %d.\n", urb->status);
524 break;
525 }
526
e8a4845d
MCC
527 spin_lock(&dev->slock);
528 tm6000_isoc_copy(urb);
529 spin_unlock(&dev->slock);
9701dc94 530
e8a4845d
MCC
531 /* Reset urb buffers */
532 for (i = 0; i < urb->number_of_packets; i++) {
533 urb->iso_frame_desc[i].status = 0;
534 urb->iso_frame_desc[i].actual_length = 0;
535 }
9701dc94 536
c4acf48c
MCC
537 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
538 if (urb->status)
9701dc94
MCC
539 tm6000_err("urb resubmit failed (error=%i)\n",
540 urb->status);
9701dc94
MCC
541}
542
16427faf
JS
543/*
544 * Allocate URB buffers
545 */
546static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
547{
548 int num_bufs = TM6000_NUM_URB_BUF;
549 int i;
550
551 if (dev->urb_buffer != NULL)
552 return 0;
553
554 dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
555 if (!dev->urb_buffer) {
556 tm6000_err("cannot allocate memory for urb buffers\n");
557 return -ENOMEM;
558 }
559
560 dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
561 if (!dev->urb_dma) {
562 tm6000_err("cannot allocate memory for urb dma pointers\n");
563 return -ENOMEM;
564 }
565
566 for (i = 0; i < num_bufs; i++) {
567 dev->urb_buffer[i] = usb_alloc_coherent(
568 dev->udev, dev->urb_size,
569 GFP_KERNEL, &dev->urb_dma[i]);
570 if (!dev->urb_buffer[i]) {
571 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
572 dev->urb_size, i);
573 return -ENOMEM;
574 }
575 memset(dev->urb_buffer[i], 0, dev->urb_size);
576 }
577
578 return 0;
579}
580
581/*
582 * Free URB buffers
583 */
584static int tm6000_free_urb_buffers(struct tm6000_core *dev)
585{
586 int i;
587
588 if (dev->urb_buffer == NULL)
589 return 0;
590
591 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
592 if (dev->urb_buffer[i]) {
593 usb_free_coherent(dev->udev,
594 dev->urb_size,
595 dev->urb_buffer[i],
596 dev->urb_dma[i]);
597 dev->urb_buffer[i] = NULL;
598 }
599 }
600 kfree(dev->urb_buffer);
601 kfree(dev->urb_dma);
602 dev->urb_buffer = NULL;
603 dev->urb_dma = NULL;
604
605 return 0;
606}
607
9701dc94
MCC
608/*
609 * Stop and Deallocate URBs
610 */
611static void tm6000_uninit_isoc(struct tm6000_core *dev)
612{
613 struct urb *urb;
614 int i;
615
ee1fc07c 616 dev->isoc_ctl.buf = NULL;
9701dc94 617 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
60fbfdfd 618 urb = dev->isoc_ctl.urb[i];
9701dc94
MCC
619 if (urb) {
620 usb_kill_urb(urb);
621 usb_unlink_urb(urb);
9701dc94
MCC
622 usb_free_urb(urb);
623 dev->isoc_ctl.urb[i] = NULL;
624 }
625 dev->isoc_ctl.transfer_buffer[i] = NULL;
626 }
627
16427faf
JS
628 if (!keep_urb)
629 tm6000_free_urb_buffers(dev);
630
60fbfdfd
RP
631 kfree(dev->isoc_ctl.urb);
632 kfree(dev->isoc_ctl.transfer_buffer);
c144c037 633
60fbfdfd
RP
634 dev->isoc_ctl.urb = NULL;
635 dev->isoc_ctl.transfer_buffer = NULL;
c144c037 636 dev->isoc_ctl.num_bufs = 0;
9701dc94
MCC
637}
638
9701dc94 639/*
16427faf 640 * Assign URBs and start IRQ
9701dc94 641 */
8aff8ba9 642static int tm6000_prepare_isoc(struct tm6000_core *dev)
9701dc94
MCC
643{
644 struct tm6000_dmaqueue *dma_q = &dev->vidq;
16427faf
JS
645 int i, j, sb_size, pipe, size, max_packets;
646 int num_bufs = TM6000_NUM_URB_BUF;
9701dc94 647 struct urb *urb;
204193d9 648
9701dc94
MCC
649 /* De-allocates all pending stuff */
650 tm6000_uninit_isoc(dev);
641d2116
DB
651 /* Stop interrupt USB pipe */
652 tm6000_ir_int_stop(dev);
9701dc94 653
6ae635c4
MCC
654 usb_set_interface(dev->udev,
655 dev->isoc_in.bInterfaceNumber,
656 dev->isoc_in.bAlternateSetting);
657
641d2116
DB
658 /* Start interrupt USB pipe */
659 tm6000_ir_int_start(dev);
660
ee1fc07c 661 pipe = usb_rcvisocpipe(dev->udev,
6ae635c4 662 dev->isoc_in.endp->desc.bEndpointAddress &
ee1fc07c
MCC
663 USB_ENDPOINT_NUMBER_MASK);
664
665 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
666
6ae635c4
MCC
667 if (size > dev->isoc_in.maxsize)
668 size = dev->isoc_in.maxsize;
ee1fc07c
MCC
669
670 dev->isoc_ctl.max_pkt_size = size;
671
8aff8ba9 672 max_packets = TM6000_MAX_ISO_PACKETS;
ee1fc07c 673 sb_size = max_packets * size;
16427faf 674 dev->urb_size = sb_size;
ee1fc07c
MCC
675
676 dev->isoc_ctl.num_bufs = num_bufs;
677
c144c037 678 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
9701dc94
MCC
679 if (!dev->isoc_ctl.urb) {
680 tm6000_err("cannot alloc memory for usb buffers\n");
681 return -ENOMEM;
682 }
683
ee1fc07c 684 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
9701dc94 685 GFP_KERNEL);
f8960ee7 686 if (!dev->isoc_ctl.transfer_buffer) {
9701dc94
MCC
687 tm6000_err("cannot allocate memory for usbtransfer\n");
688 kfree(dev->isoc_ctl.urb);
689 return -ENOMEM;
690 }
691
ee1fc07c
MCC
692 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
693 " (%d bytes) of %d bytes each to handle %u size\n",
694 max_packets, num_bufs, sb_size,
6ae635c4 695 dev->isoc_in.maxsize, size);
9701dc94 696
16427faf
JS
697
698 if (!dev->urb_buffer && tm6000_alloc_urb_buffers(dev) < 0) {
699 tm6000_err("cannot allocate memory for urb buffers\n");
700
701 /* call free, as some buffers might have been allocated */
702 tm6000_free_urb_buffers(dev);
703 kfree(dev->isoc_ctl.urb);
704 kfree(dev->isoc_ctl.transfer_buffer);
705 return -ENOMEM;
706 }
707
9701dc94
MCC
708 /* allocate urbs and transfer buffers */
709 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
710 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
711 if (!urb) {
712 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
713 tm6000_uninit_isoc(dev);
c1a16414 714 usb_free_urb(urb);
9701dc94
MCC
715 return -ENOMEM;
716 }
717 dev->isoc_ctl.urb[i] = urb;
718
16427faf
JS
719 urb->transfer_dma = dev->urb_dma[i];
720 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
9701dc94 721
ee1fc07c
MCC
722 usb_fill_bulk_urb(urb, dev->udev, pipe,
723 dev->isoc_ctl.transfer_buffer[i], sb_size,
724 tm6000_irq_callback, dma_q);
6ae635c4 725 urb->interval = dev->isoc_in.endp->desc.bInterval;
9701dc94 726 urb->number_of_packets = max_packets;
ee1fc07c 727 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
9701dc94 728
9701dc94 729 for (j = 0; j < max_packets; j++) {
ee1fc07c
MCC
730 urb->iso_frame_desc[j].offset = size * j;
731 urb->iso_frame_desc[j].length = size;
9701dc94
MCC
732 }
733 }
734
735 return 0;
736}
737
60fbfdfd 738static int tm6000_start_thread(struct tm6000_core *dev)
9701dc94 739{
c144c037
MCC
740 struct tm6000_dmaqueue *dma_q = &dev->vidq;
741 int i;
9701dc94 742
60fbfdfd
RP
743 dma_q->frame = 0;
744 dma_q->ini_jiffies = jiffies;
9701dc94
MCC
745
746 init_waitqueue_head(&dma_q->wq);
747
748 /* submit urbs and enables IRQ */
749 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
c144c037 750 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
9701dc94
MCC
751 if (rc) {
752 tm6000_err("submit of urb %i failed (error=%i)\n", i,
753 rc);
754 tm6000_uninit_isoc(dev);
755 return rc;
756 }
757 }
758
9701dc94
MCC
759 return 0;
760}
761
9701dc94 762/* ------------------------------------------------------------------
60fbfdfd
RP
763 * Videobuf operations
764 * ------------------------------------------------------------------
765 */
95a83824 766
9701dc94
MCC
767static int
768buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
769{
770 struct tm6000_fh *fh = vq->priv_data;
771
772 *size = fh->fmt->depth * fh->width * fh->height >> 3;
773 if (0 == *count)
95a83824
ML
774 *count = TM6000_DEF_BUF;
775
60fbfdfd
RP
776 if (*count < TM6000_MIN_BUF)
777 *count = TM6000_MIN_BUF;
95a83824 778
9701dc94
MCC
779 while (*size * *count > vid_limit * 1024 * 1024)
780 (*count)--;
95a83824 781
9701dc94
MCC
782 return 0;
783}
784
785static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
786{
721f507b
MCC
787 struct tm6000_fh *fh = vq->priv_data;
788 struct tm6000_core *dev = fh->dev;
789 unsigned long flags;
790
9701dc94
MCC
791 if (in_interrupt())
792 BUG();
793
721f507b
MCC
794 /* We used to wait for the buffer to finish here, but this didn't work
795 because, as we were keeping the state as VIDEOBUF_QUEUED,
796 videobuf_queue_cancel marked it as finished for us.
797 (Also, it could wedge forever if the hardware was misconfigured.)
798
799 This should be safe; by the time we get here, the buffer isn't
800 queued anymore. If we ever start marking the buffers as
801 VIDEOBUF_ACTIVE, it won't be, though.
802 */
803 spin_lock_irqsave(&dev->slock, flags);
804 if (dev->isoc_ctl.buf == buf)
805 dev->isoc_ctl.buf = NULL;
806 spin_unlock_irqrestore(&dev->slock, flags);
807
9701dc94 808 videobuf_vmalloc_free(&buf->vb);
47878f16 809 buf->vb.state = VIDEOBUF_NEEDS_INIT;
9701dc94
MCC
810}
811
812static int
813buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
814 enum v4l2_field field)
815{
816 struct tm6000_fh *fh = vq->priv_data;
60fbfdfd 817 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
9701dc94 818 struct tm6000_core *dev = fh->dev;
88e834a8 819 int rc = 0;
9701dc94
MCC
820
821 BUG_ON(NULL == fh->fmt);
822
9701dc94
MCC
823
824 /* FIXME: It assumes depth=2 */
825 /* The only currently supported format is 16 bits/pixel */
826 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
827 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
828 return -EINVAL;
829
830 if (buf->fmt != fh->fmt ||
831 buf->vb.width != fh->width ||
832 buf->vb.height != fh->height ||
833 buf->vb.field != field) {
834 buf->fmt = fh->fmt;
835 buf->vb.width = fh->width;
836 buf->vb.height = fh->height;
837 buf->vb.field = field;
47878f16 838 buf->vb.state = VIDEOBUF_NEEDS_INIT;
9701dc94
MCC
839 }
840
47878f16 841 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
ed7c221c
CM
842 rc = videobuf_iolock(vq, &buf->vb, NULL);
843 if (rc != 0)
9701dc94 844 goto fail;
9701dc94
MCC
845 }
846
88e834a8 847 if (!dev->isoc_ctl.num_bufs) {
8aff8ba9 848 rc = tm6000_prepare_isoc(dev);
c144c037
MCC
849 if (rc < 0)
850 goto fail;
ee1fc07c 851
c144c037 852 rc = tm6000_start_thread(dev);
ee1fc07c 853 if (rc < 0)
9701dc94 854 goto fail;
c144c037 855
9701dc94
MCC
856 }
857
47878f16 858 buf->vb.state = VIDEOBUF_PREPARED;
9701dc94
MCC
859 return 0;
860
861fail:
ee1fc07c 862 free_buffer(vq, buf);
9701dc94
MCC
863 return rc;
864}
865
866static void
867buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
868{
60fbfdfd 869 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
9701dc94
MCC
870 struct tm6000_fh *fh = vq->priv_data;
871 struct tm6000_core *dev = fh->dev;
872 struct tm6000_dmaqueue *vidq = &dev->vidq;
c144c037
MCC
873
874 buf->vb.state = VIDEOBUF_QUEUED;
875 list_add_tail(&buf->vb.queue, &vidq->active);
9701dc94
MCC
876}
877
878static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
879{
60fbfdfd 880 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
9701dc94 881
60fbfdfd 882 free_buffer(vq, buf);
9701dc94
MCC
883}
884
885static struct videobuf_queue_ops tm6000_video_qops = {
886 .buf_setup = buffer_setup,
887 .buf_prepare = buffer_prepare,
888 .buf_queue = buffer_queue,
889 .buf_release = buffer_release,
890};
891
892/* ------------------------------------------------------------------
60fbfdfd
RP
893 * IOCTL handling
894 * ------------------------------------------------------------------
895 */
9701dc94 896
9efd85df 897static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
9701dc94 898{
9efd85df
MCC
899 /* Is the current fh handling it? if so, that's OK */
900 if (dev->resources == fh && dev->is_res_read)
901 return true;
902
903 return false;
904}
905
906static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
907{
908 /* Is the current fh handling it? if so, that's OK */
909 if (dev->resources == fh)
910 return true;
911
912 return false;
9701dc94
MCC
913}
914
9efd85df
MCC
915static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
916 bool is_res_read)
9701dc94 917{
9efd85df
MCC
918 /* Is the current fh handling it? if so, that's OK */
919 if (dev->resources == fh && dev->is_res_read == is_res_read)
920 return true;
921
922 /* is it free? */
923 if (dev->resources)
924 return false;
925
926 /* grab it */
927 dev->resources = fh;
928 dev->is_res_read = is_res_read;
929 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
930 return true;
9701dc94
MCC
931}
932
933static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
934{
9efd85df
MCC
935 /* Is the current fh handling it? if so, that's OK */
936 if (dev->resources != fh)
937 return;
938
939 dev->resources = NULL;
9701dc94 940 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
9701dc94
MCC
941}
942
943/* ------------------------------------------------------------------
60fbfdfd
RP
944 * IOCTL vidioc handling
945 * ------------------------------------------------------------------
946 */
947static int vidioc_querycap(struct file *file, void *priv,
9701dc94
MCC
948 struct v4l2_capability *cap)
949{
886a3c0b 950 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
2c2a0536 951 struct video_device *vdev = video_devdata(file);
9701dc94
MCC
952
953 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
60fbfdfd 954 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
2c2a0536 955 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
886a3c0b 956 if (dev->tuner_type != TUNER_ABSENT)
2c2a0536
HV
957 cap->device_caps |= V4L2_CAP_TUNER;
958 if (vdev->vfl_type == VFL_TYPE_GRABBER)
959 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
960 V4L2_CAP_STREAMING |
961 V4L2_CAP_READWRITE;
962 else
963 cap->device_caps |= V4L2_CAP_RADIO;
964 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
965 V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
886a3c0b 966
9701dc94
MCC
967 return 0;
968}
969
60fbfdfd 970static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
971 struct v4l2_fmtdesc *f)
972{
2c2a0536 973 if (f->index >= ARRAY_SIZE(format))
9701dc94
MCC
974 return -EINVAL;
975
60fbfdfd 976 strlcpy(f->description, format[f->index].name, sizeof(f->description));
9701dc94
MCC
977 f->pixelformat = format[f->index].fourcc;
978 return 0;
979}
980
60fbfdfd 981static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
982 struct v4l2_format *f)
983{
60fbfdfd 984 struct tm6000_fh *fh = priv;
9701dc94
MCC
985
986 f->fmt.pix.width = fh->width;
987 f->fmt.pix.height = fh->height;
988 f->fmt.pix.field = fh->vb_vidq.field;
989 f->fmt.pix.pixelformat = fh->fmt->fourcc;
990 f->fmt.pix.bytesperline =
991 (f->fmt.pix.width * fh->fmt->depth) >> 3;
992 f->fmt.pix.sizeimage =
993 f->fmt.pix.height * f->fmt.pix.bytesperline;
994
60fbfdfd 995 return 0;
9701dc94
MCC
996}
997
60fbfdfd 998static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
9701dc94
MCC
999{
1000 unsigned int i;
1001
1002 for (i = 0; i < ARRAY_SIZE(format); i++)
1003 if (format[i].fourcc == fourcc)
1004 return format+i;
1005 return NULL;
1006}
1007
60fbfdfd 1008static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
1009 struct v4l2_format *f)
1010{
1011 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
1012 struct tm6000_fmt *fmt;
1013 enum v4l2_field field;
1014
1015 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1016 if (NULL == fmt) {
1017 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
1018 " invalid.\n", f->fmt.pix.pixelformat);
1019 return -EINVAL;
1020 }
1021
1022 field = f->fmt.pix.field;
1023
60fbfdfd
RP
1024 if (field == V4L2_FIELD_ANY)
1025 field = V4L2_FIELD_SEQ_TB;
1026 else if (V4L2_FIELD_INTERLACED != field) {
9701dc94
MCC
1027 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
1028 return -EINVAL;
1029 }
1030
60fbfdfd 1031 tm6000_get_std_res(dev);
9701dc94 1032
4475c044
MCC
1033 f->fmt.pix.width = dev->width;
1034 f->fmt.pix.height = dev->height;
9701dc94
MCC
1035
1036 f->fmt.pix.width &= ~0x01;
1037
1038 f->fmt.pix.field = field;
1039
1040 f->fmt.pix.bytesperline =
1041 (f->fmt.pix.width * fmt->depth) >> 3;
1042 f->fmt.pix.sizeimage =
1043 f->fmt.pix.height * f->fmt.pix.bytesperline;
1044
1045 return 0;
1046}
1047
1048/*FIXME: This seems to be generic enough to be at videodev2 */
60fbfdfd 1049static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
1050 struct v4l2_format *f)
1051{
60fbfdfd 1052 struct tm6000_fh *fh = priv;
9701dc94 1053 struct tm6000_core *dev = fh->dev;
60fbfdfd 1054 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
9701dc94 1055 if (ret < 0)
60fbfdfd 1056 return ret;
9701dc94
MCC
1057
1058 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1059 fh->width = f->fmt.pix.width;
1060 fh->height = f->fmt.pix.height;
1061 fh->vb_vidq.field = f->fmt.pix.field;
1062 fh->type = f->type;
1063
1064 dev->fourcc = f->fmt.pix.pixelformat;
1065
1066 tm6000_set_fourcc_format(dev);
1067
60fbfdfd 1068 return 0;
9701dc94
MCC
1069}
1070
60fbfdfd 1071static int vidioc_reqbufs(struct file *file, void *priv,
9701dc94
MCC
1072 struct v4l2_requestbuffers *p)
1073{
60fbfdfd 1074 struct tm6000_fh *fh = priv;
9701dc94 1075
60fbfdfd 1076 return videobuf_reqbufs(&fh->vb_vidq, p);
9701dc94
MCC
1077}
1078
60fbfdfd 1079static int vidioc_querybuf(struct file *file, void *priv,
9701dc94
MCC
1080 struct v4l2_buffer *p)
1081{
60fbfdfd 1082 struct tm6000_fh *fh = priv;
9701dc94 1083
60fbfdfd 1084 return videobuf_querybuf(&fh->vb_vidq, p);
9701dc94
MCC
1085}
1086
60fbfdfd 1087static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
9701dc94 1088{
60fbfdfd 1089 struct tm6000_fh *fh = priv;
9701dc94 1090
60fbfdfd 1091 return videobuf_qbuf(&fh->vb_vidq, p);
9701dc94
MCC
1092}
1093
60fbfdfd 1094static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
9701dc94 1095{
60fbfdfd 1096 struct tm6000_fh *fh = priv;
9701dc94 1097
60fbfdfd
RP
1098 return videobuf_dqbuf(&fh->vb_vidq, p,
1099 file->f_flags & O_NONBLOCK);
9701dc94
MCC
1100}
1101
9701dc94
MCC
1102static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1103{
3d1a51db
TR
1104 struct tm6000_fh *fh = priv;
1105 struct tm6000_core *dev = fh->dev;
9701dc94
MCC
1106
1107 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1108 return -EINVAL;
1109 if (i != fh->type)
1110 return -EINVAL;
1111
9efd85df 1112 if (!res_get(dev, fh, false))
9701dc94 1113 return -EBUSY;
ed7c221c 1114 return videobuf_streamon(&fh->vb_vidq);
9701dc94
MCC
1115}
1116
1117static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1118{
3d1a51db
TR
1119 struct tm6000_fh *fh = priv;
1120 struct tm6000_core *dev = fh->dev;
9701dc94
MCC
1121
1122 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1123 return -EINVAL;
3d1a51db 1124
9701dc94
MCC
1125 if (i != fh->type)
1126 return -EINVAL;
1127
1128 videobuf_streamoff(&fh->vb_vidq);
ed7c221c 1129 res_free(dev, fh);
9701dc94 1130
ed7c221c 1131 return 0;
9701dc94
MCC
1132}
1133
ed7c221c 1134static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
9701dc94 1135{
ed7c221c 1136 int rc = 0;
3d1a51db 1137 struct tm6000_fh *fh = priv;
9701dc94
MCC
1138 struct tm6000_core *dev = fh->dev;
1139
4f52610e 1140 dev->norm = *norm;
709944ea 1141 rc = tm6000_init_analog_mode(dev);
71e7cfae
MCC
1142
1143 fh->width = dev->width;
1144 fh->height = dev->height;
1145
ed7c221c 1146 if (rc < 0)
9701dc94
MCC
1147 return rc;
1148
427f7fac 1149 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
9701dc94
MCC
1150
1151 return 0;
1152}
1153
ed7c221c 1154static const char *iname[] = {
b8f7bd87
SR
1155 [TM6000_INPUT_TV] = "Television",
1156 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1157 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1158 [TM6000_INPUT_SVIDEO] = "S-Video",
1159};
1160
60fbfdfd 1161static int vidioc_enum_input(struct file *file, void *priv,
b8f7bd87 1162 struct v4l2_input *i)
9701dc94 1163{
2aefbc1a
DB
1164 struct tm6000_fh *fh = priv;
1165 struct tm6000_core *dev = fh->dev;
b8f7bd87 1166 unsigned int n;
2aefbc1a 1167
b8f7bd87
SR
1168 n = i->index;
1169 if (n >= 3)
9701dc94 1170 return -EINVAL;
b8f7bd87
SR
1171
1172 if (!dev->vinput[n].type)
1173 return -EINVAL;
1174
1175 i->index = n;
1176
1177 if (dev->vinput[n].type == TM6000_INPUT_TV)
1178 i->type = V4L2_INPUT_TYPE_TUNER;
1179 else
1180 i->type = V4L2_INPUT_TYPE_CAMERA;
1181
1182 strcpy(i->name, iname[dev->vinput[n].type]);
1183
1184 i->std = TM6000_STD;
9701dc94
MCC
1185
1186 return 0;
1187}
1188
60fbfdfd 1189static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
9701dc94 1190{
60fbfdfd 1191 struct tm6000_fh *fh = priv;
9701dc94
MCC
1192 struct tm6000_core *dev = fh->dev;
1193
60fbfdfd 1194 *i = dev->input;
9701dc94
MCC
1195
1196 return 0;
1197}
b8f7bd87 1198
60fbfdfd 1199static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
9701dc94 1200{
60fbfdfd 1201 struct tm6000_fh *fh = priv;
9701dc94 1202 struct tm6000_core *dev = fh->dev;
60fbfdfd 1203 int rc = 0;
9701dc94 1204
b8f7bd87
SR
1205 if (i >= 3)
1206 return -EINVAL;
1207 if (!dev->vinput[i].type)
9701dc94 1208 return -EINVAL;
9701dc94 1209
b8f7bd87
SR
1210 dev->input = i;
1211
1212 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
9701dc94 1213
60fbfdfd 1214 return rc;
9701dc94
MCC
1215}
1216
886a3c0b 1217/* --- controls ---------------------------------------------- */
60fbfdfd 1218static int vidioc_queryctrl(struct file *file, void *priv,
9701dc94
MCC
1219 struct v4l2_queryctrl *qc)
1220{
1221 int i;
1222
1223 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1224 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1225 memcpy(qc, &(tm6000_qctrl[i]),
1226 sizeof(*qc));
60fbfdfd 1227 return 0;
9701dc94
MCC
1228 }
1229
1230 return -EINVAL;
1231}
1232
60fbfdfd 1233static int vidioc_g_ctrl(struct file *file, void *priv,
9701dc94
MCC
1234 struct v4l2_control *ctrl)
1235{
60fbfdfd 1236 struct tm6000_fh *fh = priv;
9701dc94
MCC
1237 struct tm6000_core *dev = fh->dev;
1238 int val;
1239
1240 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1241 switch (ctrl->id) {
1242 case V4L2_CID_CONTRAST:
9afec493 1243 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
9701dc94
MCC
1244 break;
1245 case V4L2_CID_BRIGHTNESS:
9afec493 1246 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
9701dc94
MCC
1247 return 0;
1248 case V4L2_CID_SATURATION:
9afec493 1249 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
9701dc94
MCC
1250 return 0;
1251 case V4L2_CID_HUE:
9afec493 1252 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
9701dc94 1253 return 0;
8aff8ba9
DB
1254 case V4L2_CID_AUDIO_MUTE:
1255 val = dev->ctl_mute;
1256 return 0;
1257 case V4L2_CID_AUDIO_VOLUME:
1258 val = dev->ctl_volume;
1259 return 0;
9701dc94
MCC
1260 default:
1261 return -EINVAL;
1262 }
1263
60fbfdfd 1264 if (val < 0)
9701dc94
MCC
1265 return val;
1266
60fbfdfd 1267 ctrl->value = val;
9701dc94
MCC
1268
1269 return 0;
1270}
60fbfdfd 1271static int vidioc_s_ctrl(struct file *file, void *priv,
9701dc94
MCC
1272 struct v4l2_control *ctrl)
1273{
60fbfdfd 1274 struct tm6000_fh *fh = priv;
9701dc94 1275 struct tm6000_core *dev = fh->dev;
60fbfdfd 1276 u8 val = ctrl->value;
9701dc94
MCC
1277
1278 switch (ctrl->id) {
1279 case V4L2_CID_CONTRAST:
60fbfdfd 1280 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
9701dc94
MCC
1281 return 0;
1282 case V4L2_CID_BRIGHTNESS:
60fbfdfd 1283 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
9701dc94
MCC
1284 return 0;
1285 case V4L2_CID_SATURATION:
60fbfdfd 1286 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
9701dc94
MCC
1287 return 0;
1288 case V4L2_CID_HUE:
60fbfdfd 1289 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
9701dc94 1290 return 0;
8aff8ba9
DB
1291 case V4L2_CID_AUDIO_MUTE:
1292 dev->ctl_mute = val;
1293 tm6000_tvaudio_set_mute(dev, val);
1294 return 0;
1295 case V4L2_CID_AUDIO_VOLUME:
1296 dev->ctl_volume = val;
1297 tm6000_set_volume(dev, val);
1298 return 0;
9701dc94
MCC
1299 }
1300 return -EINVAL;
1301}
1302
60fbfdfd 1303static int vidioc_g_tuner(struct file *file, void *priv,
9701dc94
MCC
1304 struct v4l2_tuner *t)
1305{
60fbfdfd 1306 struct tm6000_fh *fh = priv;
9701dc94
MCC
1307 struct tm6000_core *dev = fh->dev;
1308
2c2a0536
HV
1309 if (UNSET == dev->tuner_type)
1310 return -ENOTTY;
9701dc94
MCC
1311 if (0 != t->index)
1312 return -EINVAL;
1313
1314 strcpy(t->name, "Television");
1315 t->type = V4L2_TUNER_ANALOG_TV;
2c2a0536 1316 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
9701dc94 1317 t->rangehigh = 0xffffffffUL;
886a3c0b
SR
1318 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1319
1320 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1321
1322 t->audmode = dev->amode;
9701dc94
MCC
1323
1324 return 0;
1325}
1326
60fbfdfd 1327static int vidioc_s_tuner(struct file *file, void *priv,
9701dc94
MCC
1328 struct v4l2_tuner *t)
1329{
60fbfdfd 1330 struct tm6000_fh *fh = priv;
9701dc94
MCC
1331 struct tm6000_core *dev = fh->dev;
1332
1333 if (UNSET == dev->tuner_type)
2c2a0536 1334 return -ENOTTY;
9701dc94
MCC
1335 if (0 != t->index)
1336 return -EINVAL;
1337
2c2a0536
HV
1338 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1339 dev->amode = V4L2_TUNER_MODE_STEREO;
1340 else
1341 dev->amode = t->audmode;
886a3c0b
SR
1342 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1343
1344 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
0f6040e8 1345
9701dc94
MCC
1346 return 0;
1347}
1348
60fbfdfd 1349static int vidioc_g_frequency(struct file *file, void *priv,
9701dc94
MCC
1350 struct v4l2_frequency *f)
1351{
60fbfdfd 1352 struct tm6000_fh *fh = priv;
9701dc94
MCC
1353 struct tm6000_core *dev = fh->dev;
1354
2c2a0536
HV
1355 if (UNSET == dev->tuner_type)
1356 return -ENOTTY;
1357 if (f->tuner)
9701dc94
MCC
1358 return -EINVAL;
1359
9701dc94
MCC
1360 f->frequency = dev->freq;
1361
427f7fac 1362 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
9701dc94
MCC
1363
1364 return 0;
1365}
1366
60fbfdfd 1367static int vidioc_s_frequency(struct file *file, void *priv,
9701dc94
MCC
1368 struct v4l2_frequency *f)
1369{
60fbfdfd 1370 struct tm6000_fh *fh = priv;
9701dc94
MCC
1371 struct tm6000_core *dev = fh->dev;
1372
2c2a0536
HV
1373 if (UNSET == dev->tuner_type)
1374 return -ENOTTY;
1375 if (f->tuner != 0)
8aff8ba9 1376 return -EINVAL;
9701dc94 1377
9701dc94 1378 dev->freq = f->frequency;
64d339d4 1379 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
9701dc94
MCC
1380
1381 return 0;
1382}
1383
8aff8ba9
DB
1384static int radio_g_tuner(struct file *file, void *priv,
1385 struct v4l2_tuner *t)
1386{
1387 struct tm6000_fh *fh = file->private_data;
1388 struct tm6000_core *dev = fh->dev;
1389
1390 if (0 != t->index)
1391 return -EINVAL;
1392
1393 memset(t, 0, sizeof(*t));
1394 strcpy(t->name, "Radio");
1395 t->type = V4L2_TUNER_RADIO;
2c2a0536 1396 t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
886a3c0b 1397 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
2c2a0536 1398 t->audmode = V4L2_TUNER_MODE_STEREO;
8aff8ba9
DB
1399
1400 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1401
8aff8ba9
DB
1402 return 0;
1403}
1404
1405static int radio_s_tuner(struct file *file, void *priv,
1406 struct v4l2_tuner *t)
1407{
1408 struct tm6000_fh *fh = file->private_data;
1409 struct tm6000_core *dev = fh->dev;
1410
1411 if (0 != t->index)
1412 return -EINVAL;
2c2a0536
HV
1413 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1414 t->audmode = V4L2_TUNER_MODE_STEREO;
8aff8ba9
DB
1415
1416 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1417
1418 return 0;
1419}
1420
8aff8ba9
DB
1421static int radio_queryctrl(struct file *file, void *priv,
1422 struct v4l2_queryctrl *c)
1423{
1424 const struct v4l2_queryctrl *ctrl;
1425
1426 if (c->id < V4L2_CID_BASE ||
1427 c->id >= V4L2_CID_LASTP1)
1428 return -EINVAL;
1429 if (c->id == V4L2_CID_AUDIO_MUTE) {
1430 ctrl = ctrl_by_id(c->id);
1431 *c = *ctrl;
1432 } else
1433 *c = no_ctrl;
1434
1435 return 0;
1436}
1437
9701dc94
MCC
1438/* ------------------------------------------------------------------
1439 File operations for the device
1440 ------------------------------------------------------------------*/
1441
14a09dac 1442static int __tm6000_open(struct file *file)
9701dc94 1443{
0a34df53
MCC
1444 struct video_device *vdev = video_devdata(file);
1445 struct tm6000_core *dev = video_drvdata(file);
9701dc94 1446 struct tm6000_fh *fh;
e8d04167 1447 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
60fbfdfd 1448 int i, rc;
8aff8ba9 1449 int radio = 0;
9701dc94 1450
0a34df53
MCC
1451 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1452 video_device_node_name(vdev));
9701dc94 1453
8aff8ba9
DB
1454 switch (vdev->vfl_type) {
1455 case VFL_TYPE_GRABBER:
1456 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1457 break;
1458 case VFL_TYPE_VBI:
1459 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1460 break;
1461 case VFL_TYPE_RADIO:
1462 radio = 1;
1463 break;
1464 }
9701dc94
MCC
1465
1466 /* If more than one user, mutex should be added */
1467 dev->users++;
1468
0a34df53
MCC
1469 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1470 video_device_node_name(vdev), v4l2_type_names[type],
1471 dev->users);
9701dc94
MCC
1472
1473 /* allocate + initialize per filehandle data */
60fbfdfd 1474 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
9701dc94
MCC
1475 if (NULL == fh) {
1476 dev->users--;
1477 return -ENOMEM;
1478 }
1479
1480 file->private_data = fh;
1481 fh->dev = dev;
8aff8ba9
DB
1482 fh->radio = radio;
1483 dev->radio = radio;
1484 fh->type = type;
9701dc94
MCC
1485 dev->fourcc = format[0].fourcc;
1486
1487 fh->fmt = format_by_fourcc(dev->fourcc);
4475c044 1488
ed7c221c 1489 tm6000_get_std_res(dev);
4475c044 1490
3d1a51db
TR
1491 fh->width = dev->width;
1492 fh->height = dev->height;
9701dc94
MCC
1493
1494 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1495 "dev->vidq=0x%08lx\n",
3d1a51db
TR
1496 (unsigned long)fh, (unsigned long)dev,
1497 (unsigned long)&dev->vidq);
9701dc94 1498 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
ed7c221c 1499 "queued=%d\n", list_empty(&dev->vidq.queued));
9701dc94 1500 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
ed7c221c 1501 "active=%d\n", list_empty(&dev->vidq.active));
9701dc94
MCC
1502
1503 /* initialize hardware on analog mode */
589851d5
MCC
1504 rc = tm6000_init_analog_mode(dev);
1505 if (rc < 0)
1506 return rc;
9701dc94 1507
589851d5 1508 if (dev->mode != TM6000_MODE_ANALOG) {
9701dc94
MCC
1509 /* Put all controls at a sane state */
1510 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
589851d5 1511 qctl_regs[i] = tm6000_qctrl[i].default_value;
9701dc94 1512
589851d5
MCC
1513 dev->mode = TM6000_MODE_ANALOG;
1514 }
9701dc94 1515
aa4a583d
TR
1516 if (!fh->radio) {
1517 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1518 NULL, &dev->slock,
1519 fh->type,
1520 V4L2_FIELD_INTERLACED,
1521 sizeof(struct tm6000_buffer), fh, &dev->lock);
1522 } else {
8aff8ba9 1523 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
0f6040e8 1524 tm6000_set_audio_rinput(dev);
8aff8ba9
DB
1525 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1526 tm6000_prepare_isoc(dev);
1527 tm6000_start_thread(dev);
1528 }
8aff8ba9 1529
9701dc94
MCC
1530 return 0;
1531}
1532
14a09dac
HV
1533static int tm6000_open(struct file *file)
1534{
1535 struct video_device *vdev = video_devdata(file);
1536 int res;
1537
1538 mutex_lock(vdev->lock);
1539 res = __tm6000_open(file);
1540 mutex_unlock(vdev->lock);
1541 return res;
1542}
1543
9701dc94
MCC
1544static ssize_t
1545tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1546{
14a09dac
HV
1547 struct tm6000_fh *fh = file->private_data;
1548 struct tm6000_core *dev = fh->dev;
9701dc94 1549
ed7c221c 1550 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
14a09dac
HV
1551 int res;
1552
9efd85df 1553 if (!res_get(fh->dev, fh, true))
9701dc94
MCC
1554 return -EBUSY;
1555
14a09dac
HV
1556 if (mutex_lock_interruptible(&dev->lock))
1557 return -ERESTARTSYS;
1558 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
9701dc94 1559 file->f_flags & O_NONBLOCK);
14a09dac
HV
1560 mutex_unlock(&dev->lock);
1561 return res;
9701dc94
MCC
1562 }
1563 return 0;
1564}
1565
1566static unsigned int
14a09dac 1567__tm6000_poll(struct file *file, struct poll_table_struct *wait)
9701dc94
MCC
1568{
1569 struct tm6000_fh *fh = file->private_data;
1570 struct tm6000_buffer *buf;
1571
1572 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1573 return POLLERR;
1574
9efd85df
MCC
1575 if (!!is_res_streaming(fh->dev, fh))
1576 return POLLERR;
1577
1578 if (!is_res_read(fh->dev, fh)) {
9701dc94
MCC
1579 /* streaming capture */
1580 if (list_empty(&fh->vb_vidq.stream))
1581 return POLLERR;
ed7c221c 1582 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
9701dc94
MCC
1583 } else {
1584 /* read() capture */
3d1a51db 1585 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
9701dc94
MCC
1586 }
1587 poll_wait(file, &buf->vb.done, wait);
47878f16
MCC
1588 if (buf->vb.state == VIDEOBUF_DONE ||
1589 buf->vb.state == VIDEOBUF_ERROR)
60fbfdfd 1590 return POLLIN | POLLRDNORM;
9701dc94
MCC
1591 return 0;
1592}
1593
14a09dac
HV
1594static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1595{
1596 struct tm6000_fh *fh = file->private_data;
1597 struct tm6000_core *dev = fh->dev;
1598 unsigned int res;
1599
1600 mutex_lock(&dev->lock);
1601 res = __tm6000_poll(file, wait);
1602 mutex_unlock(&dev->lock);
1603 return res;
1604}
1605
64d339d4 1606static int tm6000_release(struct file *file)
9701dc94
MCC
1607{
1608 struct tm6000_fh *fh = file->private_data;
1609 struct tm6000_core *dev = fh->dev;
0a34df53 1610 struct video_device *vdev = video_devdata(file);
9701dc94 1611
0a34df53
MCC
1612 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1613 video_device_node_name(vdev), dev->users);
7c3f53ec 1614
14a09dac 1615 mutex_lock(&dev->lock);
a58d35cb
MCC
1616 dev->users--;
1617
9efd85df 1618 res_free(dev, fh);
dd0c8abf 1619
a58d35cb 1620 if (!dev->users) {
c144c037 1621 tm6000_uninit_isoc(dev);
aa4a583d 1622
8159c184
SR
1623 /* Stop interrupt USB pipe */
1624 tm6000_ir_int_stop(dev);
1625
1626 usb_reset_configuration(dev->udev);
1627
4be9c8fb 1628 if (dev->int_in.endp)
8159c184 1629 usb_set_interface(dev->udev,
875f0e3d 1630 dev->isoc_in.bInterfaceNumber, 2);
8159c184
SR
1631 else
1632 usb_set_interface(dev->udev,
875f0e3d 1633 dev->isoc_in.bInterfaceNumber, 0);
8159c184
SR
1634
1635 /* Start interrupt USB pipe */
1636 tm6000_ir_int_start(dev);
1637
aa4a583d
TR
1638 if (!fh->radio)
1639 videobuf_mmap_free(&fh->vb_vidq);
a58d35cb 1640 }
9701dc94 1641
60fbfdfd 1642 kfree(fh);
14a09dac 1643 mutex_unlock(&dev->lock);
9701dc94 1644
9701dc94
MCC
1645 return 0;
1646}
1647
1648static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1649{
3d1a51db 1650 struct tm6000_fh *fh = file->private_data;
14a09dac
HV
1651 struct tm6000_core *dev = fh->dev;
1652 int res;
9701dc94 1653
14a09dac
HV
1654 if (mutex_lock_interruptible(&dev->lock))
1655 return -ERESTARTSYS;
1656 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1657 mutex_unlock(&dev->lock);
1658 return res;
9701dc94
MCC
1659}
1660
64d339d4 1661static struct v4l2_file_operations tm6000_fops = {
3d1a51db
TR
1662 .owner = THIS_MODULE,
1663 .open = tm6000_open,
1664 .release = tm6000_release,
1665 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1666 .read = tm6000_read,
1667 .poll = tm6000_poll,
1668 .mmap = tm6000_mmap,
9701dc94
MCC
1669};
1670
2a8145d4
MCC
1671static const struct v4l2_ioctl_ops video_ioctl_ops = {
1672 .vidioc_querycap = vidioc_querycap,
1673 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1674 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1675 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1676 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1677 .vidioc_s_std = vidioc_s_std,
1678 .vidioc_enum_input = vidioc_enum_input,
1679 .vidioc_g_input = vidioc_g_input,
1680 .vidioc_s_input = vidioc_s_input,
1681 .vidioc_queryctrl = vidioc_queryctrl,
1682 .vidioc_g_ctrl = vidioc_g_ctrl,
1683 .vidioc_s_ctrl = vidioc_s_ctrl,
1684 .vidioc_g_tuner = vidioc_g_tuner,
1685 .vidioc_s_tuner = vidioc_s_tuner,
1686 .vidioc_g_frequency = vidioc_g_frequency,
1687 .vidioc_s_frequency = vidioc_s_frequency,
1688 .vidioc_streamon = vidioc_streamon,
1689 .vidioc_streamoff = vidioc_streamoff,
1690 .vidioc_reqbufs = vidioc_reqbufs,
1691 .vidioc_querybuf = vidioc_querybuf,
1692 .vidioc_qbuf = vidioc_qbuf,
1693 .vidioc_dqbuf = vidioc_dqbuf,
2a8145d4
MCC
1694};
1695
9701dc94
MCC
1696static struct video_device tm6000_template = {
1697 .name = "tm6000",
9701dc94 1698 .fops = &tm6000_fops,
2a8145d4 1699 .ioctl_ops = &video_ioctl_ops,
9701dc94 1700 .release = video_device_release,
2a8145d4
MCC
1701 .tvnorms = TM6000_STD,
1702 .current_norm = V4L2_STD_NTSC_M,
9701dc94 1703};
2a8145d4 1704
8aff8ba9 1705static const struct v4l2_file_operations radio_fops = {
1f385717
SR
1706 .owner = THIS_MODULE,
1707 .open = tm6000_open,
1708 .release = tm6000_release,
1709 .unlocked_ioctl = video_ioctl2,
8aff8ba9
DB
1710};
1711
1712static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2c2a0536 1713 .vidioc_querycap = vidioc_querycap,
8aff8ba9 1714 .vidioc_g_tuner = radio_g_tuner,
8aff8ba9 1715 .vidioc_s_tuner = radio_s_tuner,
8aff8ba9 1716 .vidioc_queryctrl = radio_queryctrl,
8aff8ba9
DB
1717 .vidioc_g_ctrl = vidioc_g_ctrl,
1718 .vidioc_s_ctrl = vidioc_s_ctrl,
1719 .vidioc_g_frequency = vidioc_g_frequency,
1720 .vidioc_s_frequency = vidioc_s_frequency,
1721};
1722
3d1a51db 1723static struct video_device tm6000_radio_template = {
8aff8ba9
DB
1724 .name = "tm6000",
1725 .fops = &radio_fops,
ed7c221c 1726 .ioctl_ops = &radio_ioctl_ops,
8aff8ba9
DB
1727};
1728
9701dc94 1729/* -----------------------------------------------------------------
60fbfdfd
RP
1730 * Initialization and module stuff
1731 * ------------------------------------------------------------------
1732 */
9701dc94 1733
3c61be44
DB
1734static struct video_device *vdev_init(struct tm6000_core *dev,
1735 const struct video_device
1736 *template, const char *type_name)
9701dc94 1737{
7c3f53ec
ML
1738 struct video_device *vfd;
1739
1740 vfd = video_device_alloc();
3c61be44
DB
1741 if (NULL == vfd)
1742 return NULL;
1743
1744 *vfd = *template;
1745 vfd->v4l2_dev = &dev->v4l2_dev;
1746 vfd->release = video_device_release;
1747 vfd->debug = tm6000_debug;
1748 vfd->lock = &dev->lock;
1749
1750 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1751
1752 video_set_drvdata(vfd, dev);
1753 return vfd;
1754}
1755
1756int tm6000_v4l2_register(struct tm6000_core *dev)
1757{
1758 int ret = -1;
1759
1760 dev->vfd = vdev_init(dev, &tm6000_template, "video");
1761
1762 if (!dev->vfd) {
1763 printk(KERN_INFO "%s: can't register video device\n",
1764 dev->name);
7c3f53ec
ML
1765 return -ENOMEM;
1766 }
9701dc94 1767
9701dc94
MCC
1768 /* init video dma queues */
1769 INIT_LIST_HEAD(&dev->vidq.active);
1770 INIT_LIST_HEAD(&dev->vidq.queued);
1771
3c61be44 1772 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
dc961136 1773
3c61be44
DB
1774 if (ret < 0) {
1775 printk(KERN_INFO "%s: can't register video device\n",
1776 dev->name);
1777 return ret;
1778 }
1779
1780 printk(KERN_INFO "%s: registered device %s\n",
1781 dev->name, video_device_node_name(dev->vfd));
9701dc94 1782
14169107
SR
1783 if (dev->caps.has_radio) {
1784 dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
1785 "radio");
1786 if (!dev->radio_dev) {
1787 printk(KERN_INFO "%s: can't register radio device\n",
1788 dev->name);
97b55f6a 1789 ret = -ENXIO;
14169107
SR
1790 return ret; /* FIXME release resource */
1791 }
8aff8ba9 1792
14169107
SR
1793 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1794 radio_nr);
1795 if (ret < 0) {
1796 printk(KERN_INFO "%s: can't register radio device\n",
1797 dev->name);
1798 return ret; /* FIXME release resource */
1799 }
8aff8ba9 1800
14169107
SR
1801 printk(KERN_INFO "%s: registered device %s\n",
1802 dev->name, video_device_node_name(dev->radio_dev));
1803 }
8aff8ba9 1804
e28f49b0 1805 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
9701dc94
MCC
1806 return ret;
1807}
1808
1809int tm6000_v4l2_unregister(struct tm6000_core *dev)
1810{
7c3f53ec 1811 video_unregister_device(dev->vfd);
22927e8e 1812
16427faf
JS
1813 /* if URB buffers are still allocated free them now */
1814 tm6000_free_urb_buffers(dev);
1815
8aff8ba9
DB
1816 if (dev->radio_dev) {
1817 if (video_is_registered(dev->radio_dev))
1818 video_unregister_device(dev->radio_dev);
1819 else
1820 video_device_release(dev->radio_dev);
1821 dev->radio_dev = NULL;
1822 }
1823
9701dc94
MCC
1824 return 0;
1825}
1826
1827int tm6000_v4l2_exit(void)
1828{
1829 return 0;
1830}
1831
1832module_param(video_nr, int, 0);
60fbfdfd 1833MODULE_PARM_DESC(video_nr, "Allow changing video device number");
9701dc94 1834
60fbfdfd
RP
1835module_param_named(debug, tm6000_debug, int, 0444);
1836MODULE_PARM_DESC(debug, "activates debug info");
9701dc94 1837
60fbfdfd
RP
1838module_param(vid_limit, int, 0644);
1839MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
9701dc94 1840
16427faf
JS
1841module_param(keep_urb, bool, 0);
1842MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");
This page took 0.429596 seconds and 5 git commands to generate.