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