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