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