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