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