[media] v4l: fix compiler warnings
[deliverable/linux.git] / drivers / media / video / 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
14f09154
TR
516 switch (urb->status) {
517 case 0:
518 case -ETIMEDOUT:
519 break;
520
521 case -ECONNRESET:
522 case -ENOENT:
523 case -ESHUTDOWN:
721f507b 524 return;
a228618c 525
14f09154
TR
526 default:
527 tm6000_err("urb completion error %d.\n", urb->status);
528 break;
529 }
530
e8a4845d
MCC
531 spin_lock(&dev->slock);
532 tm6000_isoc_copy(urb);
533 spin_unlock(&dev->slock);
9701dc94 534
e8a4845d
MCC
535 /* Reset urb buffers */
536 for (i = 0; i < urb->number_of_packets; i++) {
537 urb->iso_frame_desc[i].status = 0;
538 urb->iso_frame_desc[i].actual_length = 0;
539 }
9701dc94 540
c4acf48c
MCC
541 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
542 if (urb->status)
9701dc94
MCC
543 tm6000_err("urb resubmit failed (error=%i)\n",
544 urb->status);
9701dc94
MCC
545}
546
547/*
548 * Stop and Deallocate URBs
549 */
550static void tm6000_uninit_isoc(struct tm6000_core *dev)
551{
552 struct urb *urb;
553 int i;
554
ee1fc07c 555 dev->isoc_ctl.buf = NULL;
9701dc94 556 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
60fbfdfd 557 urb = dev->isoc_ctl.urb[i];
9701dc94
MCC
558 if (urb) {
559 usb_kill_urb(urb);
560 usb_unlink_urb(urb);
561 if (dev->isoc_ctl.transfer_buffer[i]) {
5a11b6fe 562 usb_free_coherent(dev->udev,
9701dc94
MCC
563 urb->transfer_buffer_length,
564 dev->isoc_ctl.transfer_buffer[i],
565 urb->transfer_dma);
566 }
567 usb_free_urb(urb);
568 dev->isoc_ctl.urb[i] = NULL;
569 }
570 dev->isoc_ctl.transfer_buffer[i] = NULL;
571 }
572
60fbfdfd
RP
573 kfree(dev->isoc_ctl.urb);
574 kfree(dev->isoc_ctl.transfer_buffer);
c144c037 575
60fbfdfd
RP
576 dev->isoc_ctl.urb = NULL;
577 dev->isoc_ctl.transfer_buffer = NULL;
c144c037 578 dev->isoc_ctl.num_bufs = 0;
9701dc94
MCC
579}
580
9701dc94
MCC
581/*
582 * Allocate URBs and start IRQ
583 */
8aff8ba9 584static int tm6000_prepare_isoc(struct tm6000_core *dev)
9701dc94
MCC
585{
586 struct tm6000_dmaqueue *dma_q = &dev->vidq;
5a4b55e2 587 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
9701dc94 588 struct urb *urb;
204193d9 589
9701dc94
MCC
590 /* De-allocates all pending stuff */
591 tm6000_uninit_isoc(dev);
641d2116
DB
592 /* Stop interrupt USB pipe */
593 tm6000_ir_int_stop(dev);
9701dc94 594
6ae635c4
MCC
595 usb_set_interface(dev->udev,
596 dev->isoc_in.bInterfaceNumber,
597 dev->isoc_in.bAlternateSetting);
598
641d2116
DB
599 /* Start interrupt USB pipe */
600 tm6000_ir_int_start(dev);
601
ee1fc07c 602 pipe = usb_rcvisocpipe(dev->udev,
6ae635c4 603 dev->isoc_in.endp->desc.bEndpointAddress &
ee1fc07c
MCC
604 USB_ENDPOINT_NUMBER_MASK);
605
606 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
607
6ae635c4
MCC
608 if (size > dev->isoc_in.maxsize)
609 size = dev->isoc_in.maxsize;
ee1fc07c
MCC
610
611 dev->isoc_ctl.max_pkt_size = size;
612
8aff8ba9 613 max_packets = TM6000_MAX_ISO_PACKETS;
ee1fc07c
MCC
614 sb_size = max_packets * size;
615
616 dev->isoc_ctl.num_bufs = num_bufs;
617
c144c037 618 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
9701dc94
MCC
619 if (!dev->isoc_ctl.urb) {
620 tm6000_err("cannot alloc memory for usb buffers\n");
621 return -ENOMEM;
622 }
623
ee1fc07c 624 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
9701dc94 625 GFP_KERNEL);
f8960ee7 626 if (!dev->isoc_ctl.transfer_buffer) {
9701dc94
MCC
627 tm6000_err("cannot allocate memory for usbtransfer\n");
628 kfree(dev->isoc_ctl.urb);
629 return -ENOMEM;
630 }
631
ee1fc07c
MCC
632 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
633 " (%d bytes) of %d bytes each to handle %u size\n",
634 max_packets, num_bufs, sb_size,
6ae635c4 635 dev->isoc_in.maxsize, size);
9701dc94
MCC
636
637 /* allocate urbs and transfer buffers */
638 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
639 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
640 if (!urb) {
641 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
642 tm6000_uninit_isoc(dev);
c1a16414 643 usb_free_urb(urb);
9701dc94
MCC
644 return -ENOMEM;
645 }
646 dev->isoc_ctl.urb[i] = urb;
647
5a11b6fe 648 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
c13dd704 649 sb_size, GFP_KERNEL, &urb->transfer_dma);
9701dc94 650 if (!dev->isoc_ctl.transfer_buffer[i]) {
60fbfdfd 651 tm6000_err("unable to allocate %i bytes for transfer"
a228618c
MCC
652 " buffer %i%s\n",
653 sb_size, i,
60fbfdfd 654 in_interrupt() ? " while in int" : "");
9701dc94
MCC
655 tm6000_uninit_isoc(dev);
656 return -ENOMEM;
657 }
658 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
659
ee1fc07c
MCC
660 usb_fill_bulk_urb(urb, dev->udev, pipe,
661 dev->isoc_ctl.transfer_buffer[i], sb_size,
662 tm6000_irq_callback, dma_q);
6ae635c4 663 urb->interval = dev->isoc_in.endp->desc.bInterval;
9701dc94 664 urb->number_of_packets = max_packets;
ee1fc07c 665 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
9701dc94 666
9701dc94 667 for (j = 0; j < max_packets; j++) {
ee1fc07c
MCC
668 urb->iso_frame_desc[j].offset = size * j;
669 urb->iso_frame_desc[j].length = size;
9701dc94
MCC
670 }
671 }
672
673 return 0;
674}
675
60fbfdfd 676static int tm6000_start_thread(struct tm6000_core *dev)
9701dc94 677{
c144c037
MCC
678 struct tm6000_dmaqueue *dma_q = &dev->vidq;
679 int i;
9701dc94 680
60fbfdfd
RP
681 dma_q->frame = 0;
682 dma_q->ini_jiffies = jiffies;
9701dc94
MCC
683
684 init_waitqueue_head(&dma_q->wq);
685
686 /* submit urbs and enables IRQ */
687 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
c144c037 688 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
9701dc94
MCC
689 if (rc) {
690 tm6000_err("submit of urb %i failed (error=%i)\n", i,
691 rc);
692 tm6000_uninit_isoc(dev);
693 return rc;
694 }
695 }
696
9701dc94
MCC
697 return 0;
698}
699
9701dc94 700/* ------------------------------------------------------------------
60fbfdfd
RP
701 * Videobuf operations
702 * ------------------------------------------------------------------
703 */
95a83824 704
9701dc94
MCC
705static int
706buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
707{
708 struct tm6000_fh *fh = vq->priv_data;
709
710 *size = fh->fmt->depth * fh->width * fh->height >> 3;
711 if (0 == *count)
95a83824
ML
712 *count = TM6000_DEF_BUF;
713
60fbfdfd
RP
714 if (*count < TM6000_MIN_BUF)
715 *count = TM6000_MIN_BUF;
95a83824 716
9701dc94
MCC
717 while (*size * *count > vid_limit * 1024 * 1024)
718 (*count)--;
95a83824 719
9701dc94
MCC
720 return 0;
721}
722
723static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
724{
721f507b
MCC
725 struct tm6000_fh *fh = vq->priv_data;
726 struct tm6000_core *dev = fh->dev;
727 unsigned long flags;
728
9701dc94
MCC
729 if (in_interrupt())
730 BUG();
731
721f507b
MCC
732 /* We used to wait for the buffer to finish here, but this didn't work
733 because, as we were keeping the state as VIDEOBUF_QUEUED,
734 videobuf_queue_cancel marked it as finished for us.
735 (Also, it could wedge forever if the hardware was misconfigured.)
736
737 This should be safe; by the time we get here, the buffer isn't
738 queued anymore. If we ever start marking the buffers as
739 VIDEOBUF_ACTIVE, it won't be, though.
740 */
741 spin_lock_irqsave(&dev->slock, flags);
742 if (dev->isoc_ctl.buf == buf)
743 dev->isoc_ctl.buf = NULL;
744 spin_unlock_irqrestore(&dev->slock, flags);
745
9701dc94 746 videobuf_vmalloc_free(&buf->vb);
47878f16 747 buf->vb.state = VIDEOBUF_NEEDS_INIT;
9701dc94
MCC
748}
749
750static int
751buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
752 enum v4l2_field field)
753{
754 struct tm6000_fh *fh = vq->priv_data;
60fbfdfd 755 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
9701dc94 756 struct tm6000_core *dev = fh->dev;
88e834a8 757 int rc = 0;
9701dc94
MCC
758
759 BUG_ON(NULL == fh->fmt);
760
9701dc94
MCC
761
762 /* FIXME: It assumes depth=2 */
763 /* The only currently supported format is 16 bits/pixel */
764 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
765 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
766 return -EINVAL;
767
768 if (buf->fmt != fh->fmt ||
769 buf->vb.width != fh->width ||
770 buf->vb.height != fh->height ||
771 buf->vb.field != field) {
772 buf->fmt = fh->fmt;
773 buf->vb.width = fh->width;
774 buf->vb.height = fh->height;
775 buf->vb.field = field;
47878f16 776 buf->vb.state = VIDEOBUF_NEEDS_INIT;
9701dc94
MCC
777 }
778
47878f16 779 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
ed7c221c
CM
780 rc = videobuf_iolock(vq, &buf->vb, NULL);
781 if (rc != 0)
9701dc94 782 goto fail;
9701dc94
MCC
783 }
784
88e834a8 785 if (!dev->isoc_ctl.num_bufs) {
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->capabilities = V4L2_CAP_VIDEO_CAPTURE |
893 V4L2_CAP_STREAMING |
886a3c0b 894 V4L2_CAP_AUDIO |
9701dc94 895 V4L2_CAP_READWRITE;
886a3c0b
SR
896
897 if (dev->tuner_type != TUNER_ABSENT)
898 cap->capabilities |= V4L2_CAP_TUNER;
899
9701dc94
MCC
900 return 0;
901}
902
60fbfdfd 903static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
904 struct v4l2_fmtdesc *f)
905{
906 if (unlikely(f->index >= ARRAY_SIZE(format)))
907 return -EINVAL;
908
60fbfdfd 909 strlcpy(f->description, format[f->index].name, sizeof(f->description));
9701dc94
MCC
910 f->pixelformat = format[f->index].fourcc;
911 return 0;
912}
913
60fbfdfd 914static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
915 struct v4l2_format *f)
916{
60fbfdfd 917 struct tm6000_fh *fh = priv;
9701dc94
MCC
918
919 f->fmt.pix.width = fh->width;
920 f->fmt.pix.height = fh->height;
921 f->fmt.pix.field = fh->vb_vidq.field;
922 f->fmt.pix.pixelformat = fh->fmt->fourcc;
923 f->fmt.pix.bytesperline =
924 (f->fmt.pix.width * fh->fmt->depth) >> 3;
925 f->fmt.pix.sizeimage =
926 f->fmt.pix.height * f->fmt.pix.bytesperline;
927
60fbfdfd 928 return 0;
9701dc94
MCC
929}
930
60fbfdfd 931static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
9701dc94
MCC
932{
933 unsigned int i;
934
935 for (i = 0; i < ARRAY_SIZE(format); i++)
936 if (format[i].fourcc == fourcc)
937 return format+i;
938 return NULL;
939}
940
60fbfdfd 941static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
942 struct v4l2_format *f)
943{
944 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
945 struct tm6000_fmt *fmt;
946 enum v4l2_field field;
947
948 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
949 if (NULL == fmt) {
950 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
951 " invalid.\n", f->fmt.pix.pixelformat);
952 return -EINVAL;
953 }
954
955 field = f->fmt.pix.field;
956
60fbfdfd
RP
957 if (field == V4L2_FIELD_ANY)
958 field = V4L2_FIELD_SEQ_TB;
959 else if (V4L2_FIELD_INTERLACED != field) {
9701dc94
MCC
960 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
961 return -EINVAL;
962 }
963
60fbfdfd 964 tm6000_get_std_res(dev);
9701dc94 965
4475c044
MCC
966 f->fmt.pix.width = dev->width;
967 f->fmt.pix.height = dev->height;
9701dc94
MCC
968
969 f->fmt.pix.width &= ~0x01;
970
971 f->fmt.pix.field = field;
972
973 f->fmt.pix.bytesperline =
974 (f->fmt.pix.width * fmt->depth) >> 3;
975 f->fmt.pix.sizeimage =
976 f->fmt.pix.height * f->fmt.pix.bytesperline;
977
978 return 0;
979}
980
981/*FIXME: This seems to be generic enough to be at videodev2 */
60fbfdfd 982static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
9701dc94
MCC
983 struct v4l2_format *f)
984{
60fbfdfd 985 struct tm6000_fh *fh = priv;
9701dc94 986 struct tm6000_core *dev = fh->dev;
60fbfdfd 987 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
9701dc94 988 if (ret < 0)
60fbfdfd 989 return ret;
9701dc94
MCC
990
991 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
992 fh->width = f->fmt.pix.width;
993 fh->height = f->fmt.pix.height;
994 fh->vb_vidq.field = f->fmt.pix.field;
995 fh->type = f->type;
996
997 dev->fourcc = f->fmt.pix.pixelformat;
998
999 tm6000_set_fourcc_format(dev);
1000
60fbfdfd 1001 return 0;
9701dc94
MCC
1002}
1003
60fbfdfd 1004static int vidioc_reqbufs(struct file *file, void *priv,
9701dc94
MCC
1005 struct v4l2_requestbuffers *p)
1006{
60fbfdfd 1007 struct tm6000_fh *fh = priv;
9701dc94 1008
60fbfdfd 1009 return videobuf_reqbufs(&fh->vb_vidq, p);
9701dc94
MCC
1010}
1011
60fbfdfd 1012static int vidioc_querybuf(struct file *file, void *priv,
9701dc94
MCC
1013 struct v4l2_buffer *p)
1014{
60fbfdfd 1015 struct tm6000_fh *fh = priv;
9701dc94 1016
60fbfdfd 1017 return videobuf_querybuf(&fh->vb_vidq, p);
9701dc94
MCC
1018}
1019
60fbfdfd 1020static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
9701dc94 1021{
60fbfdfd 1022 struct tm6000_fh *fh = priv;
9701dc94 1023
60fbfdfd 1024 return videobuf_qbuf(&fh->vb_vidq, p);
9701dc94
MCC
1025}
1026
60fbfdfd 1027static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
9701dc94 1028{
60fbfdfd 1029 struct tm6000_fh *fh = priv;
9701dc94 1030
60fbfdfd
RP
1031 return videobuf_dqbuf(&fh->vb_vidq, p,
1032 file->f_flags & O_NONBLOCK);
9701dc94
MCC
1033}
1034
9701dc94
MCC
1035static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1036{
3d1a51db
TR
1037 struct tm6000_fh *fh = priv;
1038 struct tm6000_core *dev = fh->dev;
9701dc94
MCC
1039
1040 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1041 return -EINVAL;
1042 if (i != fh->type)
1043 return -EINVAL;
1044
9efd85df 1045 if (!res_get(dev, fh, false))
9701dc94 1046 return -EBUSY;
ed7c221c 1047 return videobuf_streamon(&fh->vb_vidq);
9701dc94
MCC
1048}
1049
1050static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1051{
3d1a51db
TR
1052 struct tm6000_fh *fh = priv;
1053 struct tm6000_core *dev = fh->dev;
9701dc94
MCC
1054
1055 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1056 return -EINVAL;
3d1a51db 1057
9701dc94
MCC
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 1069 int rc = 0;
3d1a51db 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 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1468 video_device_node_name(vdev));
9701dc94 1469
8aff8ba9
DB
1470 switch (vdev->vfl_type) {
1471 case VFL_TYPE_GRABBER:
1472 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1473 break;
1474 case VFL_TYPE_VBI:
1475 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1476 break;
1477 case VFL_TYPE_RADIO:
1478 radio = 1;
1479 break;
1480 }
9701dc94
MCC
1481
1482 /* If more than one user, mutex should be added */
1483 dev->users++;
1484
0a34df53
MCC
1485 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1486 video_device_node_name(vdev), v4l2_type_names[type],
1487 dev->users);
9701dc94
MCC
1488
1489 /* allocate + initialize per filehandle data */
60fbfdfd 1490 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
9701dc94
MCC
1491 if (NULL == fh) {
1492 dev->users--;
1493 return -ENOMEM;
1494 }
1495
1496 file->private_data = fh;
1497 fh->dev = dev;
8aff8ba9
DB
1498 fh->radio = radio;
1499 dev->radio = radio;
1500 fh->type = type;
9701dc94
MCC
1501 dev->fourcc = format[0].fourcc;
1502
1503 fh->fmt = format_by_fourcc(dev->fourcc);
4475c044 1504
ed7c221c 1505 tm6000_get_std_res(dev);
4475c044 1506
3d1a51db
TR
1507 fh->width = dev->width;
1508 fh->height = dev->height;
9701dc94
MCC
1509
1510 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1511 "dev->vidq=0x%08lx\n",
3d1a51db
TR
1512 (unsigned long)fh, (unsigned long)dev,
1513 (unsigned long)&dev->vidq);
9701dc94 1514 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
ed7c221c 1515 "queued=%d\n", list_empty(&dev->vidq.queued));
9701dc94 1516 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
ed7c221c 1517 "active=%d\n", list_empty(&dev->vidq.active));
9701dc94
MCC
1518
1519 /* initialize hardware on analog mode */
589851d5
MCC
1520 rc = tm6000_init_analog_mode(dev);
1521 if (rc < 0)
1522 return rc;
9701dc94 1523
589851d5 1524 if (dev->mode != TM6000_MODE_ANALOG) {
9701dc94
MCC
1525 /* Put all controls at a sane state */
1526 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
589851d5 1527 qctl_regs[i] = tm6000_qctrl[i].default_value;
9701dc94 1528
589851d5
MCC
1529 dev->mode = TM6000_MODE_ANALOG;
1530 }
9701dc94 1531
aa4a583d
TR
1532 if (!fh->radio) {
1533 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1534 NULL, &dev->slock,
1535 fh->type,
1536 V4L2_FIELD_INTERLACED,
1537 sizeof(struct tm6000_buffer), fh, &dev->lock);
1538 } else {
8aff8ba9 1539 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
0f6040e8
SR
1540 dev->input = 5;
1541 tm6000_set_audio_rinput(dev);
8aff8ba9
DB
1542 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1543 tm6000_prepare_isoc(dev);
1544 tm6000_start_thread(dev);
1545 }
8aff8ba9 1546
9701dc94
MCC
1547 return 0;
1548}
1549
1550static ssize_t
1551tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1552{
1553 struct tm6000_fh *fh = file->private_data;
1554
ed7c221c 1555 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
9efd85df 1556 if (!res_get(fh->dev, fh, true))
9701dc94
MCC
1557 return -EBUSY;
1558
1559 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1560 file->f_flags & O_NONBLOCK);
1561 }
1562 return 0;
1563}
1564
1565static unsigned int
1566tm6000_poll(struct file *file, struct poll_table_struct *wait)
1567{
1568 struct tm6000_fh *fh = file->private_data;
1569 struct tm6000_buffer *buf;
1570
1571 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1572 return POLLERR;
1573
9efd85df
MCC
1574 if (!!is_res_streaming(fh->dev, fh))
1575 return POLLERR;
1576
1577 if (!is_res_read(fh->dev, fh)) {
9701dc94
MCC
1578 /* streaming capture */
1579 if (list_empty(&fh->vb_vidq.stream))
1580 return POLLERR;
ed7c221c 1581 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
9701dc94
MCC
1582 } else {
1583 /* read() capture */
3d1a51db 1584 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
9701dc94
MCC
1585 }
1586 poll_wait(file, &buf->vb.done, wait);
47878f16
MCC
1587 if (buf->vb.state == VIDEOBUF_DONE ||
1588 buf->vb.state == VIDEOBUF_ERROR)
60fbfdfd 1589 return POLLIN | POLLRDNORM;
9701dc94
MCC
1590 return 0;
1591}
1592
64d339d4 1593static int tm6000_release(struct file *file)
9701dc94
MCC
1594{
1595 struct tm6000_fh *fh = file->private_data;
1596 struct tm6000_core *dev = fh->dev;
0a34df53 1597 struct video_device *vdev = video_devdata(file);
9701dc94 1598
0a34df53
MCC
1599 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1600 video_device_node_name(vdev), dev->users);
7c3f53ec 1601
a58d35cb
MCC
1602 dev->users--;
1603
9efd85df 1604 res_free(dev, fh);
dd0c8abf 1605
a58d35cb 1606 if (!dev->users) {
c144c037 1607 tm6000_uninit_isoc(dev);
aa4a583d 1608
8159c184
SR
1609 /* Stop interrupt USB pipe */
1610 tm6000_ir_int_stop(dev);
1611
1612 usb_reset_configuration(dev->udev);
1613
4be9c8fb 1614 if (dev->int_in.endp)
8159c184 1615 usb_set_interface(dev->udev,
875f0e3d 1616 dev->isoc_in.bInterfaceNumber, 2);
8159c184
SR
1617 else
1618 usb_set_interface(dev->udev,
875f0e3d 1619 dev->isoc_in.bInterfaceNumber, 0);
8159c184
SR
1620
1621 /* Start interrupt USB pipe */
1622 tm6000_ir_int_start(dev);
1623
aa4a583d
TR
1624 if (!fh->radio)
1625 videobuf_mmap_free(&fh->vb_vidq);
a58d35cb 1626 }
9701dc94 1627
60fbfdfd 1628 kfree(fh);
9701dc94 1629
9701dc94
MCC
1630 return 0;
1631}
1632
1633static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1634{
3d1a51db 1635 struct tm6000_fh *fh = file->private_data;
9701dc94 1636
3d1a51db 1637 return videobuf_mmap_mapper(&fh->vb_vidq, vma);
9701dc94
MCC
1638}
1639
64d339d4 1640static struct v4l2_file_operations tm6000_fops = {
3d1a51db
TR
1641 .owner = THIS_MODULE,
1642 .open = tm6000_open,
1643 .release = tm6000_release,
1644 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1645 .read = tm6000_read,
1646 .poll = tm6000_poll,
1647 .mmap = tm6000_mmap,
9701dc94
MCC
1648};
1649
2a8145d4
MCC
1650static const struct v4l2_ioctl_ops video_ioctl_ops = {
1651 .vidioc_querycap = vidioc_querycap,
1652 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1653 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1654 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1655 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1656 .vidioc_s_std = vidioc_s_std,
1657 .vidioc_enum_input = vidioc_enum_input,
1658 .vidioc_g_input = vidioc_g_input,
1659 .vidioc_s_input = vidioc_s_input,
1660 .vidioc_queryctrl = vidioc_queryctrl,
1661 .vidioc_g_ctrl = vidioc_g_ctrl,
1662 .vidioc_s_ctrl = vidioc_s_ctrl,
1663 .vidioc_g_tuner = vidioc_g_tuner,
1664 .vidioc_s_tuner = vidioc_s_tuner,
1665 .vidioc_g_frequency = vidioc_g_frequency,
1666 .vidioc_s_frequency = vidioc_s_frequency,
1667 .vidioc_streamon = vidioc_streamon,
1668 .vidioc_streamoff = vidioc_streamoff,
1669 .vidioc_reqbufs = vidioc_reqbufs,
1670 .vidioc_querybuf = vidioc_querybuf,
1671 .vidioc_qbuf = vidioc_qbuf,
1672 .vidioc_dqbuf = vidioc_dqbuf,
2a8145d4
MCC
1673};
1674
9701dc94
MCC
1675static struct video_device tm6000_template = {
1676 .name = "tm6000",
9701dc94 1677 .fops = &tm6000_fops,
2a8145d4 1678 .ioctl_ops = &video_ioctl_ops,
9701dc94 1679 .release = video_device_release,
2a8145d4
MCC
1680 .tvnorms = TM6000_STD,
1681 .current_norm = V4L2_STD_NTSC_M,
9701dc94 1682};
2a8145d4 1683
8aff8ba9 1684static const struct v4l2_file_operations radio_fops = {
1f385717
SR
1685 .owner = THIS_MODULE,
1686 .open = tm6000_open,
1687 .release = tm6000_release,
1688 .unlocked_ioctl = video_ioctl2,
8aff8ba9
DB
1689};
1690
1691static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1692 .vidioc_querycap = radio_querycap,
1693 .vidioc_g_tuner = radio_g_tuner,
1694 .vidioc_enum_input = radio_enum_input,
1695 .vidioc_g_audio = radio_g_audio,
1696 .vidioc_s_tuner = radio_s_tuner,
1697 .vidioc_s_audio = radio_s_audio,
1698 .vidioc_s_input = radio_s_input,
1699 .vidioc_s_std = radio_s_std,
1700 .vidioc_queryctrl = radio_queryctrl,
1701 .vidioc_g_input = radio_g_input,
1702 .vidioc_g_ctrl = vidioc_g_ctrl,
1703 .vidioc_s_ctrl = vidioc_s_ctrl,
1704 .vidioc_g_frequency = vidioc_g_frequency,
1705 .vidioc_s_frequency = vidioc_s_frequency,
1706};
1707
3d1a51db 1708static struct video_device tm6000_radio_template = {
8aff8ba9
DB
1709 .name = "tm6000",
1710 .fops = &radio_fops,
ed7c221c 1711 .ioctl_ops = &radio_ioctl_ops,
8aff8ba9
DB
1712};
1713
9701dc94 1714/* -----------------------------------------------------------------
60fbfdfd
RP
1715 * Initialization and module stuff
1716 * ------------------------------------------------------------------
1717 */
9701dc94 1718
3c61be44
DB
1719static struct video_device *vdev_init(struct tm6000_core *dev,
1720 const struct video_device
1721 *template, const char *type_name)
9701dc94 1722{
7c3f53ec
ML
1723 struct video_device *vfd;
1724
1725 vfd = video_device_alloc();
3c61be44
DB
1726 if (NULL == vfd)
1727 return NULL;
1728
1729 *vfd = *template;
1730 vfd->v4l2_dev = &dev->v4l2_dev;
1731 vfd->release = video_device_release;
1732 vfd->debug = tm6000_debug;
1733 vfd->lock = &dev->lock;
5126f259
HV
1734 /* Locking in file operations other than ioctl should be done
1735 by the driver, not the V4L2 core.
1736 This driver needs auditing so that this flag can be removed. */
1737 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
3c61be44
DB
1738
1739 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1740
1741 video_set_drvdata(vfd, dev);
1742 return vfd;
1743}
1744
1745int tm6000_v4l2_register(struct tm6000_core *dev)
1746{
1747 int ret = -1;
1748
1749 dev->vfd = vdev_init(dev, &tm6000_template, "video");
1750
1751 if (!dev->vfd) {
1752 printk(KERN_INFO "%s: can't register video device\n",
1753 dev->name);
7c3f53ec
ML
1754 return -ENOMEM;
1755 }
9701dc94 1756
9701dc94
MCC
1757 /* init video dma queues */
1758 INIT_LIST_HEAD(&dev->vidq.active);
1759 INIT_LIST_HEAD(&dev->vidq.queued);
1760
3c61be44 1761 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
dc961136 1762
3c61be44
DB
1763 if (ret < 0) {
1764 printk(KERN_INFO "%s: can't register video device\n",
1765 dev->name);
1766 return ret;
1767 }
1768
1769 printk(KERN_INFO "%s: registered device %s\n",
1770 dev->name, video_device_node_name(dev->vfd));
9701dc94 1771
14169107
SR
1772 if (dev->caps.has_radio) {
1773 dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
1774 "radio");
1775 if (!dev->radio_dev) {
1776 printk(KERN_INFO "%s: can't register radio device\n",
1777 dev->name);
1778 return ret; /* FIXME release resource */
1779 }
8aff8ba9 1780
14169107
SR
1781 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1782 radio_nr);
1783 if (ret < 0) {
1784 printk(KERN_INFO "%s: can't register radio device\n",
1785 dev->name);
1786 return ret; /* FIXME release resource */
1787 }
8aff8ba9 1788
14169107
SR
1789 printk(KERN_INFO "%s: registered device %s\n",
1790 dev->name, video_device_node_name(dev->radio_dev));
1791 }
8aff8ba9 1792
e28f49b0 1793 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
9701dc94
MCC
1794 return ret;
1795}
1796
1797int tm6000_v4l2_unregister(struct tm6000_core *dev)
1798{
7c3f53ec 1799 video_unregister_device(dev->vfd);
22927e8e 1800
8aff8ba9
DB
1801 if (dev->radio_dev) {
1802 if (video_is_registered(dev->radio_dev))
1803 video_unregister_device(dev->radio_dev);
1804 else
1805 video_device_release(dev->radio_dev);
1806 dev->radio_dev = NULL;
1807 }
1808
9701dc94
MCC
1809 return 0;
1810}
1811
1812int tm6000_v4l2_exit(void)
1813{
1814 return 0;
1815}
1816
1817module_param(video_nr, int, 0);
60fbfdfd 1818MODULE_PARM_DESC(video_nr, "Allow changing video device number");
9701dc94 1819
60fbfdfd
RP
1820module_param_named(debug, tm6000_debug, int, 0444);
1821MODULE_PARM_DESC(debug, "activates debug info");
9701dc94 1822
60fbfdfd
RP
1823module_param(vid_limit, int, 0644);
1824MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
9701dc94 1825
This page took 0.277695 seconds and 5 git commands to generate.