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