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