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