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