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