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