blob: 4c22c658494009ed197d4ca5891fd8e3078e5346 [file] [log] [blame]
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001/*
Dmitri Belimove28f49b2010-02-22 06:32:15 -03002 tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03003
4 Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
5
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03006 Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 - Fixed module load/unload
8
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03009 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>
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -030036#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030037#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
Michel Ludwig95a83822007-07-24 08:06:45 -030047/* Limits minimum and default number of buffers */
48#define TM6000_MIN_BUF 4
49#define TM6000_DEF_BUF 8
50
Stefan Ringel5a4b55e2010-05-19 13:58:27 -030051#define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -030052
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030053/* 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
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030057/* Debug level */
58int tm6000_debug;
Mauro Carvalho Chehabfaa7c132010-06-04 21:08:25 -030059EXPORT_SYMBOL_GPL(tm6000_debug);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030060
61/* supported controls */
62static struct v4l2_queryctrl tm6000_qctrl[] = {
63 {
64 .id = V4L2_CID_BRIGHTNESS,
65 .type = V4L2_CTRL_TYPE_INTEGER,
66 .name = "Brightness",
67 .minimum = 0,
68 .maximum = 255,
69 .step = 1,
70 .default_value = 54,
71 .flags = 0,
72 }, {
73 .id = V4L2_CID_CONTRAST,
74 .type = V4L2_CTRL_TYPE_INTEGER,
75 .name = "Contrast",
76 .minimum = 0,
77 .maximum = 255,
78 .step = 0x1,
79 .default_value = 119,
80 .flags = 0,
81 }, {
82 .id = V4L2_CID_SATURATION,
83 .type = V4L2_CTRL_TYPE_INTEGER,
84 .name = "Saturation",
85 .minimum = 0,
86 .maximum = 255,
87 .step = 0x1,
88 .default_value = 112,
89 .flags = 0,
90 }, {
91 .id = V4L2_CID_HUE,
92 .type = V4L2_CTRL_TYPE_INTEGER,
93 .name = "Hue",
94 .minimum = -128,
95 .maximum = 127,
96 .step = 0x1,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030097 .default_value = 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030098 .flags = 0,
99 }
100};
101
102static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
103
104static struct tm6000_fmt format[] = {
105 {
106 .name = "4:2:2, packed, YVY2",
107 .fourcc = V4L2_PIX_FMT_YUYV,
108 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300109 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300110 .name = "4:2:2, packed, UYVY",
111 .fourcc = V4L2_PIX_FMT_UYVY,
112 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300113 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300114 .name = "A/V + VBI mux packet",
115 .fourcc = V4L2_PIX_FMT_TM6000,
116 .depth = 16,
117 }
118};
119
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300120/* ------------------------------------------------------------------
121 DMA and thread functions
122 ------------------------------------------------------------------*/
123
124#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300125#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300126
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300127#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 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300133static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300134 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300135{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300136 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300137 char *outp;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300138
139 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300140 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300141 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300142 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300143 }
144
145 *buf = list_entry(dma_q->active.next,
146 struct tm6000_buffer, vb.queue);
147
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300148 if (!buf)
149 return;
150
151 /* Cleans up buffer - Usefull for testing for frame/URB loss */
152 outp = videobuf_to_vmalloc(&(*buf)->vb);
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300153
154 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300155}
156
157/*
158 * Announces that a buffer were filled and request the next
159 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300160static inline void buffer_filled(struct tm6000_core *dev,
161 struct tm6000_dmaqueue *dma_q,
162 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300163{
164 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300165 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300166 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300167 buf->vb.field_count++;
168 do_gettimeofday(&buf->vb.ts);
169
170 list_del(&buf->vb.queue);
171 wake_up(&buf->vb.done);
172}
173
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300174const char *tm6000_msg_type[] = {
175 "unknown(0)", /* 0 */
176 "video", /* 1 */
177 "audio", /* 2 */
178 "vbi", /* 3 */
179 "pts", /* 4 */
180 "err", /* 5 */
181 "unknown(6)", /* 6 */
182 "unknown(7)", /* 7 */
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300183};
184
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300185/*
186 * Identify the tm5600/6000 buffer header type and properly handles
187 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300188static int copy_streams(u8 *data, unsigned long len,
189 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300190{
191 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300192 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300193 u8 *ptr=data, *endp=data+len, c;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300194 unsigned long header=0;
195 int rc=0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300196 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
197 struct tm6000_buffer *vbuf;
198 char *voutp = NULL;
199 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300200
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300201 /* get video buffer */
202 get_next_buf (dma_q, &vbuf);
203 if (!vbuf)
204 return rc;
205 voutp = videobuf_to_vmalloc(&vbuf->vb);
206 if (!voutp)
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300207 return 0;
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300208
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300209 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300210 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300211 /* Header */
212 if (dev->isoc_ctl.tmp_buf_len > 0) {
213 /* from last urb or packet */
214 header = dev->isoc_ctl.tmp_buf;
215 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
216 memcpy ((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300217 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300218 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300219 4 - dev->isoc_ctl.tmp_buf_len);
220 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300221 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300222 dev->isoc_ctl.tmp_buf_len = 0;
223 } else {
224 if (ptr + 3 >= endp) {
225 /* have incomplete header */
226 dev->isoc_ctl.tmp_buf_len = endp - ptr;
227 memcpy (&dev->isoc_ctl.tmp_buf, ptr,
228 dev->isoc_ctl.tmp_buf_len);
229 return rc;
230 }
231 /* Seek for sync */
232 for (; ptr < endp - 3; ptr++) {
233 if (*(ptr + 3) == 0x47)
234 break;
235 }
236 /* Get message header */
237 header = *(unsigned long *)ptr;
238 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300239 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300240
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300241 /* split the header fields */
242 c = (header >> 24) & 0xff;
243 size = ((header & 0x7e) << 1);
244 if (size > 0)
245 size -= 4;
246 block = (header >> 7) & 0xf;
247 field = (header >> 11) & 0x1;
248 line = (header >> 12) & 0x1ff;
249 cmd = (header >> 21) & 0x7;
250 /* Validates haeder fields */
251 if (size > TM6000_URB_MSG_LEN)
252 size = TM6000_URB_MSG_LEN;
253 pktsize = TM6000_URB_MSG_LEN;
254 /* calculate position in buffer
255 * and change the buffer
256 */
257 switch (cmd) {
258 case TM6000_URB_MSG_VIDEO:
259 if ((dev->isoc_ctl.vfield != field) &&
260 (field == 1)) {
261 /* Announces that a new buffer
262 * were filled
263 */
264 buffer_filled (dev, dma_q, vbuf);
265 dprintk (dev, V4L2_DEBUG_ISOC,
266 "new buffer filled\n");
267 get_next_buf (dma_q, &vbuf);
268 if (!vbuf)
269 return rc;
270 voutp = videobuf_to_vmalloc (&vbuf->vb);
271 if (!voutp)
272 return rc;
Mauro Carvalho Chehab758bb0b2010-06-07 12:32:27 -0300273 memset(voutp, 0, vbuf->vb.size);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300274 }
275 linewidth = vbuf->vb.width << 1;
276 pos = ((line << 1) - field - 1) * linewidth +
277 block * TM6000_URB_MSG_LEN;
278 /* Don't allow to write out of the buffer */
279 if (pos + size > vbuf->vb.size)
280 cmd = TM6000_URB_MSG_ERR;
281 dev->isoc_ctl.vfield = field;
282 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300283 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300284 break;
285 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300286 case TM6000_URB_MSG_PTS:
Mauro Carvalho Chehabd0669c82010-06-07 12:10:14 -0300287 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300288 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300289 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300290 } 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;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300296 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300297 cpysize = (endp - ptr > size) ? size : endp - ptr;
298 if (cpysize) {
299 /* copy data in different buffers */
300 switch (cmd) {
301 case TM6000_URB_MSG_VIDEO:
302 /* Fills video buffer */
303 if (vbuf)
304 memcpy (&voutp[pos], ptr, cpysize);
305 break;
306 case TM6000_URB_MSG_AUDIO:
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300307 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300308 break;
309 case TM6000_URB_MSG_VBI:
310 /* Need some code to copy vbi buffer */
311 break;
312 case TM6000_URB_MSG_PTS:
313 /* Need some code to copy pts */
314 break;
315 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300316 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300317 if (ptr + pktsize > endp) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300318 /* End of URB packet, but cmd processing is not
319 * complete. Preserve the state for a next packet
320 */
321 dev->isoc_ctl.pos = pos + cpysize;
322 dev->isoc_ctl.size = size - cpysize;
323 dev->isoc_ctl.cmd = cmd;
324 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300325 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300326 } else {
327 dev->isoc_ctl.cmd = 0;
328 ptr += pktsize;
329 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300330 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300331 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300332}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300333
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300334/*
335 * Identify the tm5600/6000 buffer header type and properly handles
336 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300337static int copy_multiplexed(u8 *ptr, unsigned long len,
338 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300339{
340 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300341 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
342 unsigned int pos=dev->isoc_ctl.pos,cpysize;
343 int rc=1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300344 struct tm6000_buffer *buf;
345 char *outp = NULL;
346
347 get_next_buf(dma_q, &buf);
348 if (buf)
349 outp = videobuf_to_vmalloc(&buf->vb);
350
351 if (!outp)
352 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300353
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300354 while (len>0) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300355 cpysize=min(len,buf->vb.size-pos);
356 //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos);
357 memcpy(&outp[pos], ptr, cpysize);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300358 pos+=cpysize;
359 ptr+=cpysize;
360 len-=cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300361 if (pos >= buf->vb.size) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300362 pos=0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300363 /* Announces that a new buffer were filled */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300364 buffer_filled (dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300365 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300366 get_next_buf (dma_q, &buf);
367 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300368 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300369 outp = videobuf_to_vmalloc(&(buf->vb));
370 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300371 return rc;
372 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300373 }
374 }
375
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300376 dev->isoc_ctl.pos=pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300377 return rc;
378}
379
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300380static void inline print_err_status (struct tm6000_core *dev,
381 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300382{
383 char *errmsg = "Unknown";
384
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300385 switch(status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300386 case -ENOENT:
387 errmsg = "unlinked synchronuously";
388 break;
389 case -ECONNRESET:
390 errmsg = "unlinked asynchronuously";
391 break;
392 case -ENOSR:
393 errmsg = "Buffer error (overrun)";
394 break;
395 case -EPIPE:
396 errmsg = "Stalled (device not responding)";
397 break;
398 case -EOVERFLOW:
399 errmsg = "Babble (bad cable?)";
400 break;
401 case -EPROTO:
402 errmsg = "Bit-stuff error (bad cable?)";
403 break;
404 case -EILSEQ:
405 errmsg = "CRC/Timeout (could be anything)";
406 break;
407 case -ETIME:
408 errmsg = "Device does not respond";
409 break;
410 }
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300411 if (packet<0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300412 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
413 status, errmsg);
414 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300415 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300416 packet, status, errmsg);
417 }
418}
419
420
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300421/*
422 * Controls the isoc copy of each urb packet
423 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300424static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300425{
426 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300427 struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300428 int i, len=0, rc=1, status;
429 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300430
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300431 if (urb->status < 0) {
432 print_err_status (dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300433 return 0;
434 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300435
436 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300437 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300438
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300439 if (status<0) {
440 print_err_status (dev,i,status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300441 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300442 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300443
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300444 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300445
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300446 if (len > 0) {
447 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300448 if (!urb->iso_frame_desc[i].status) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300449 if ((dev->fourcc)==V4L2_PIX_FMT_TM6000) {
450 rc=copy_multiplexed(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300451 if (rc<=0)
452 return rc;
453 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300454 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300455 }
456 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300457 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300458 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300459 return rc;
460}
461
462/* ------------------------------------------------------------------
463 URB control
464 ------------------------------------------------------------------*/
465
466/*
467 * IRQ callback, called by URB callback
468 */
469static void tm6000_irq_callback(struct urb *urb)
470{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300471 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300472 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300473 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300474
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300475 if (!dev)
476 return;
477
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300478 spin_lock(&dev->slock);
479 tm6000_isoc_copy(urb);
480 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300481
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300482 /* Reset urb buffers */
483 for (i = 0; i < urb->number_of_packets; i++) {
484 urb->iso_frame_desc[i].status = 0;
485 urb->iso_frame_desc[i].actual_length = 0;
486 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300487
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300488 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
489 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300490 tm6000_err("urb resubmit failed (error=%i)\n",
491 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300492}
493
494/*
495 * Stop and Deallocate URBs
496 */
497static void tm6000_uninit_isoc(struct tm6000_core *dev)
498{
499 struct urb *urb;
500 int i;
501
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300502 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300503 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
504 urb=dev->isoc_ctl.urb[i];
505 if (urb) {
506 usb_kill_urb(urb);
507 usb_unlink_urb(urb);
508 if (dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300509 usb_free_coherent(dev->udev,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300510 urb->transfer_buffer_length,
511 dev->isoc_ctl.transfer_buffer[i],
512 urb->transfer_dma);
513 }
514 usb_free_urb(urb);
515 dev->isoc_ctl.urb[i] = NULL;
516 }
517 dev->isoc_ctl.transfer_buffer[i] = NULL;
518 }
519
520 kfree (dev->isoc_ctl.urb);
521 kfree (dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300522
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300523 dev->isoc_ctl.urb=NULL;
524 dev->isoc_ctl.transfer_buffer=NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300525 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300526}
527
528/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300529 * Allocate URBs and start IRQ
530 */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300531static int tm6000_prepare_isoc(struct tm6000_core *dev, unsigned int framesize)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300532{
533 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Stefan Ringel5a4b55e2010-05-19 13:58:27 -0300534 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300535 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300536
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300537 /* De-allocates all pending stuff */
538 tm6000_uninit_isoc(dev);
539
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300540 usb_set_interface(dev->udev,
541 dev->isoc_in.bInterfaceNumber,
542 dev->isoc_in.bAlternateSetting);
543
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300544 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300545 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300546 USB_ENDPOINT_NUMBER_MASK);
547
548 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
549
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300550 if (size > dev->isoc_in.maxsize)
551 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300552
553 dev->isoc_ctl.max_pkt_size = size;
554
555 max_packets = ( framesize + size - 1) / size;
556
557 if (max_packets > TM6000_MAX_ISO_PACKETS)
558 max_packets = TM6000_MAX_ISO_PACKETS;
559
560 sb_size = max_packets * size;
561
562 dev->isoc_ctl.num_bufs = num_bufs;
563
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300564 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300565 if (!dev->isoc_ctl.urb) {
566 tm6000_err("cannot alloc memory for usb buffers\n");
567 return -ENOMEM;
568 }
569
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300570 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300571 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300572 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300573 tm6000_err("cannot allocate memory for usbtransfer\n");
574 kfree(dev->isoc_ctl.urb);
575 return -ENOMEM;
576 }
577
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300578 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
579 " (%d bytes) of %d bytes each to handle %u size\n",
580 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300581 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300582
583 /* allocate urbs and transfer buffers */
584 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
585 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
586 if (!urb) {
587 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
588 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehabc1a16412007-10-15 15:43:50 -0300589 usb_free_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300590 return -ENOMEM;
591 }
592 dev->isoc_ctl.urb[i] = urb;
593
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300594 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
Mauro Carvalho Chehabc13dd702007-09-19 07:36:34 -0300595 sb_size, GFP_KERNEL, &urb->transfer_dma);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300596 if (!dev->isoc_ctl.transfer_buffer[i]) {
597 tm6000_err ("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300598 " buffer %i%s\n",
599 sb_size, i,
600 in_interrupt()?" while in int":"");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300601 tm6000_uninit_isoc(dev);
602 return -ENOMEM;
603 }
604 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
605
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300606 usb_fill_bulk_urb(urb, dev->udev, pipe,
607 dev->isoc_ctl.transfer_buffer[i], sb_size,
608 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300609 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300610 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300611 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300612
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300613 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300614 urb->iso_frame_desc[j].offset = size * j;
615 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300616 }
617 }
618
619 return 0;
620}
621
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300622static int tm6000_start_thread( struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300623{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300624 struct tm6000_dmaqueue *dma_q = &dev->vidq;
625 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300626
627 dma_q->frame=0;
628 dma_q->ini_jiffies=jiffies;
629
630 init_waitqueue_head(&dma_q->wq);
631
632 /* submit urbs and enables IRQ */
633 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300634 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300635 if (rc) {
636 tm6000_err("submit of urb %i failed (error=%i)\n", i,
637 rc);
638 tm6000_uninit_isoc(dev);
639 return rc;
640 }
641 }
642
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300643 return 0;
644}
645
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300646/* ------------------------------------------------------------------
647 Videobuf operations
648 ------------------------------------------------------------------*/
Michel Ludwig95a83822007-07-24 08:06:45 -0300649
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300650static int
651buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
652{
653 struct tm6000_fh *fh = vq->priv_data;
654
655 *size = fh->fmt->depth * fh->width * fh->height >> 3;
656 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300657 *count = TM6000_DEF_BUF;
658
659 if (*count < TM6000_MIN_BUF) {
660 *count=TM6000_MIN_BUF;
661 }
662
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300663 while (*size * *count > vid_limit * 1024 * 1024)
664 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300665
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300666 return 0;
667}
668
669static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
670{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300671 struct tm6000_fh *fh = vq->priv_data;
672 struct tm6000_core *dev = fh->dev;
673 unsigned long flags;
674
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300675 if (in_interrupt())
676 BUG();
677
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300678 /* We used to wait for the buffer to finish here, but this didn't work
679 because, as we were keeping the state as VIDEOBUF_QUEUED,
680 videobuf_queue_cancel marked it as finished for us.
681 (Also, it could wedge forever if the hardware was misconfigured.)
682
683 This should be safe; by the time we get here, the buffer isn't
684 queued anymore. If we ever start marking the buffers as
685 VIDEOBUF_ACTIVE, it won't be, though.
686 */
687 spin_lock_irqsave(&dev->slock, flags);
688 if (dev->isoc_ctl.buf == buf)
689 dev->isoc_ctl.buf = NULL;
690 spin_unlock_irqrestore(&dev->slock, flags);
691
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300692 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300693 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300694}
695
696static int
697buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
698 enum v4l2_field field)
699{
700 struct tm6000_fh *fh = vq->priv_data;
701 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
702 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300703 int rc = 0, urb_init = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300704
705 BUG_ON(NULL == fh->fmt);
706
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300707
708 /* FIXME: It assumes depth=2 */
709 /* The only currently supported format is 16 bits/pixel */
710 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
711 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
712 return -EINVAL;
713
714 if (buf->fmt != fh->fmt ||
715 buf->vb.width != fh->width ||
716 buf->vb.height != fh->height ||
717 buf->vb.field != field) {
718 buf->fmt = fh->fmt;
719 buf->vb.width = fh->width;
720 buf->vb.height = fh->height;
721 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300722 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300723 }
724
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300725 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300726 if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300727 goto fail;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300728 urb_init = 1;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300729 }
730
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300731 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300732 urb_init = 1;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300733
734 if (urb_init) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300735 rc = tm6000_prepare_isoc(dev, buf->vb.size);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300736 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300737 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300738
739 rc = tm6000_start_thread(dev);
740 if (rc < 0)
741 goto fail;
742
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300743 }
744
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300745 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300746 return 0;
747
748fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300749 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300750 return rc;
751}
752
753static void
754buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
755{
756 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
757 struct tm6000_fh *fh = vq->priv_data;
758 struct tm6000_core *dev = fh->dev;
759 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300760
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300761 buf->vb.state = VIDEOBUF_QUEUED;
762 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300763}
764
765static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
766{
767 struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300768
769 free_buffer(vq,buf);
770}
771
772static struct videobuf_queue_ops tm6000_video_qops = {
773 .buf_setup = buffer_setup,
774 .buf_prepare = buffer_prepare,
775 .buf_queue = buffer_queue,
776 .buf_release = buffer_release,
777};
778
779/* ------------------------------------------------------------------
780 IOCTL handling
781 ------------------------------------------------------------------*/
782
783static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh)
784{
785 /* is it free? */
786 mutex_lock(&dev->lock);
787 if (dev->resources) {
788 /* no, someone else uses it */
789 mutex_unlock(&dev->lock);
790 return 0;
791 }
792 /* it's free, grab it */
793 dev->resources =1;
794 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
795 mutex_unlock(&dev->lock);
796 return 1;
797}
798
799static int res_locked(struct tm6000_core *dev)
800{
801 return (dev->resources);
802}
803
804static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
805{
806 mutex_lock(&dev->lock);
807 dev->resources = 0;
808 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
809 mutex_unlock(&dev->lock);
810}
811
812/* ------------------------------------------------------------------
813 IOCTL vidioc handling
814 ------------------------------------------------------------------*/
815static int vidioc_querycap (struct file *file, void *priv,
816 struct v4l2_capability *cap)
817{
818 // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
819
820 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Dmitri Belimove28f49b2010-02-22 06:32:15 -0300821 strlcpy(cap->card,"Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300822 // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
823 cap->version = TM6000_VERSION;
824 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
825 V4L2_CAP_STREAMING |
826 V4L2_CAP_TUNER |
827 V4L2_CAP_READWRITE;
828 return 0;
829}
830
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -0300831static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300832 struct v4l2_fmtdesc *f)
833{
834 if (unlikely(f->index >= ARRAY_SIZE(format)))
835 return -EINVAL;
836
837 strlcpy(f->description,format[f->index].name,sizeof(f->description));
838 f->pixelformat = format[f->index].fourcc;
839 return 0;
840}
841
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -0300842static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300843 struct v4l2_format *f)
844{
845 struct tm6000_fh *fh=priv;
846
847 f->fmt.pix.width = fh->width;
848 f->fmt.pix.height = fh->height;
849 f->fmt.pix.field = fh->vb_vidq.field;
850 f->fmt.pix.pixelformat = fh->fmt->fourcc;
851 f->fmt.pix.bytesperline =
852 (f->fmt.pix.width * fh->fmt->depth) >> 3;
853 f->fmt.pix.sizeimage =
854 f->fmt.pix.height * f->fmt.pix.bytesperline;
855
856 return (0);
857}
858
859static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc)
860{
861 unsigned int i;
862
863 for (i = 0; i < ARRAY_SIZE(format); i++)
864 if (format[i].fourcc == fourcc)
865 return format+i;
866 return NULL;
867}
868
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -0300869static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300870 struct v4l2_format *f)
871{
872 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
873 struct tm6000_fmt *fmt;
874 enum v4l2_field field;
875
876 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
877 if (NULL == fmt) {
878 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
879 " invalid.\n", f->fmt.pix.pixelformat);
880 return -EINVAL;
881 }
882
883 field = f->fmt.pix.field;
884
885 if (field == V4L2_FIELD_ANY) {
886// field=V4L2_FIELD_INTERLACED;
887 field=V4L2_FIELD_SEQ_TB;
888 } else if (V4L2_FIELD_INTERLACED != field) {
889 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
890 return -EINVAL;
891 }
892
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300893 tm6000_get_std_res (dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300894
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300895 f->fmt.pix.width = dev->width;
896 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300897
898 f->fmt.pix.width &= ~0x01;
899
900 f->fmt.pix.field = field;
901
902 f->fmt.pix.bytesperline =
903 (f->fmt.pix.width * fmt->depth) >> 3;
904 f->fmt.pix.sizeimage =
905 f->fmt.pix.height * f->fmt.pix.bytesperline;
906
907 return 0;
908}
909
910/*FIXME: This seems to be generic enough to be at videodev2 */
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -0300911static int vidioc_s_fmt_vid_cap (struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300912 struct v4l2_format *f)
913{
914 struct tm6000_fh *fh=priv;
915 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -0300916 int ret = vidioc_try_fmt_vid_cap(file,fh,f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300917 if (ret < 0)
918 return (ret);
919
920 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
921 fh->width = f->fmt.pix.width;
922 fh->height = f->fmt.pix.height;
923 fh->vb_vidq.field = f->fmt.pix.field;
924 fh->type = f->type;
925
926 dev->fourcc = f->fmt.pix.pixelformat;
927
928 tm6000_set_fourcc_format(dev);
929
930 return (0);
931}
932
933static int vidioc_reqbufs (struct file *file, void *priv,
934 struct v4l2_requestbuffers *p)
935{
936 struct tm6000_fh *fh=priv;
937
938 return (videobuf_reqbufs(&fh->vb_vidq, p));
939}
940
941static int vidioc_querybuf (struct file *file, void *priv,
942 struct v4l2_buffer *p)
943{
944 struct tm6000_fh *fh=priv;
945
946 return (videobuf_querybuf(&fh->vb_vidq, p));
947}
948
949static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
950{
951 struct tm6000_fh *fh=priv;
952
953 return (videobuf_qbuf(&fh->vb_vidq, p));
954}
955
956static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
957{
958 struct tm6000_fh *fh=priv;
959
960 return (videobuf_dqbuf(&fh->vb_vidq, p,
961 file->f_flags & O_NONBLOCK));
962}
963
964#ifdef CONFIG_VIDEO_V4L1_COMPAT
965static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
966{
967 struct tm6000_fh *fh=priv;
968
969 return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);
970}
971#endif
972
973static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
974{
975 struct tm6000_fh *fh=priv;
976 struct tm6000_core *dev = fh->dev;
977
978 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
979 return -EINVAL;
980 if (i != fh->type)
981 return -EINVAL;
982
983 if (!res_get(dev,fh))
984 return -EBUSY;
985 return (videobuf_streamon(&fh->vb_vidq));
986}
987
988static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
989{
990 struct tm6000_fh *fh=priv;
991 struct tm6000_core *dev = fh->dev;
992
993 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
994 return -EINVAL;
995 if (i != fh->type)
996 return -EINVAL;
997
998 videobuf_streamoff(&fh->vb_vidq);
999 res_free(dev,fh);
1000
1001 return (0);
1002}
1003
1004static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1005{
1006 int rc=0;
1007 struct tm6000_fh *fh=priv;
1008 struct tm6000_core *dev = fh->dev;
1009
1010 rc=tm6000_set_standard (dev, norm);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001011
1012 fh->width = dev->width;
1013 fh->height = dev->height;
1014
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001015 if (rc<0)
1016 return rc;
1017
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001018 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001019
1020 return 0;
1021}
1022
1023static int vidioc_enum_input (struct file *file, void *priv,
1024 struct v4l2_input *inp)
1025{
1026 switch (inp->index) {
1027 case TM6000_INPUT_TV:
1028 inp->type = V4L2_INPUT_TYPE_TUNER;
1029 strcpy(inp->name,"Television");
1030 break;
1031 case TM6000_INPUT_COMPOSITE:
1032 inp->type = V4L2_INPUT_TYPE_CAMERA;
1033 strcpy(inp->name,"Composite");
1034 break;
1035 case TM6000_INPUT_SVIDEO:
1036 inp->type = V4L2_INPUT_TYPE_CAMERA;
1037 strcpy(inp->name,"S-Video");
1038 break;
1039 default:
1040 return -EINVAL;
1041 }
1042 inp->std = TM6000_STD;
1043
1044 return 0;
1045}
1046
1047static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1048{
1049 struct tm6000_fh *fh=priv;
1050 struct tm6000_core *dev = fh->dev;
1051
1052 *i=dev->input;
1053
1054 return 0;
1055}
1056static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1057{
1058 struct tm6000_fh *fh=priv;
1059 struct tm6000_core *dev = fh->dev;
1060 int rc=0;
1061 char buf[1];
1062
1063 switch (i) {
1064 case TM6000_INPUT_TV:
1065 dev->input=i;
1066 *buf=0;
1067 break;
1068 case TM6000_INPUT_COMPOSITE:
1069 case TM6000_INPUT_SVIDEO:
1070 dev->input=i;
1071 *buf=1;
1072 break;
1073 default:
1074 return -EINVAL;
1075 }
1076 rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR,
1077 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1078
1079 if (!rc) {
1080 dev->input=i;
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001081 rc=vidioc_s_std (file, priv, &dev->vfd->current_norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001082 }
1083
1084 return (rc);
1085}
1086
1087 /* --- controls ---------------------------------------------- */
1088static int vidioc_queryctrl (struct file *file, void *priv,
1089 struct v4l2_queryctrl *qc)
1090{
1091 int i;
1092
1093 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1094 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1095 memcpy(qc, &(tm6000_qctrl[i]),
1096 sizeof(*qc));
1097 return (0);
1098 }
1099
1100 return -EINVAL;
1101}
1102
1103static int vidioc_g_ctrl (struct file *file, void *priv,
1104 struct v4l2_control *ctrl)
1105{
1106 struct tm6000_fh *fh=priv;
1107 struct tm6000_core *dev = fh->dev;
1108 int val;
1109
1110 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1111 switch (ctrl->id) {
1112 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001113 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001114 break;
1115 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001116 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001117 return 0;
1118 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001119 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001120 return 0;
1121 case V4L2_CID_HUE:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001122 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001123 return 0;
1124 default:
1125 return -EINVAL;
1126 }
1127
1128 if (val<0)
1129 return val;
1130
1131 ctrl->value=val;
1132
1133 return 0;
1134}
1135static int vidioc_s_ctrl (struct file *file, void *priv,
1136 struct v4l2_control *ctrl)
1137{
1138 struct tm6000_fh *fh =priv;
1139 struct tm6000_core *dev = fh->dev;
1140 u8 val=ctrl->value;
1141
1142 switch (ctrl->id) {
1143 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001144 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001145 return 0;
1146 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001147 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001148 return 0;
1149 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001150 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001151 return 0;
1152 case V4L2_CID_HUE:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001153 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001154 return 0;
1155 }
1156 return -EINVAL;
1157}
1158
1159static int vidioc_g_tuner (struct file *file, void *priv,
1160 struct v4l2_tuner *t)
1161{
1162 struct tm6000_fh *fh =priv;
1163 struct tm6000_core *dev = fh->dev;
1164
1165 if (unlikely(UNSET == dev->tuner_type))
1166 return -EINVAL;
1167 if (0 != t->index)
1168 return -EINVAL;
1169
1170 strcpy(t->name, "Television");
1171 t->type = V4L2_TUNER_ANALOG_TV;
1172 t->capability = V4L2_TUNER_CAP_NORM;
1173 t->rangehigh = 0xffffffffUL;
1174 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1175
1176 return 0;
1177}
1178
1179static int vidioc_s_tuner (struct file *file, void *priv,
1180 struct v4l2_tuner *t)
1181{
1182 struct tm6000_fh *fh =priv;
1183 struct tm6000_core *dev = fh->dev;
1184
1185 if (UNSET == dev->tuner_type)
1186 return -EINVAL;
1187 if (0 != t->index)
1188 return -EINVAL;
1189
1190 return 0;
1191}
1192
1193static int vidioc_g_frequency (struct file *file, void *priv,
1194 struct v4l2_frequency *f)
1195{
1196 struct tm6000_fh *fh =priv;
1197 struct tm6000_core *dev = fh->dev;
1198
1199 if (unlikely(UNSET == dev->tuner_type))
1200 return -EINVAL;
1201
1202 f->type = V4L2_TUNER_ANALOG_TV;
1203 f->frequency = dev->freq;
1204
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001205 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001206
1207 return 0;
1208}
1209
1210static int vidioc_s_frequency (struct file *file, void *priv,
1211 struct v4l2_frequency *f)
1212{
1213 struct tm6000_fh *fh =priv;
1214 struct tm6000_core *dev = fh->dev;
1215
1216 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1217 return -EINVAL;
1218
1219 if (unlikely(UNSET == dev->tuner_type))
1220 return -EINVAL;
1221 if (unlikely(f->tuner != 0))
1222 return -EINVAL;
1223
1224// mutex_lock(&dev->lock);
1225 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001226 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001227// mutex_unlock(&dev->lock);
1228
1229 return 0;
1230}
1231
1232/* ------------------------------------------------------------------
1233 File operations for the device
1234 ------------------------------------------------------------------*/
1235
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001236static int tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001237{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001238 struct video_device *vdev = video_devdata(file);
1239 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001240 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001241 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001242 int i,rc;
1243
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001244 printk(KERN_INFO "tm6000: open called (dev=%s)\n",
1245 video_device_node_name(vdev));
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001246
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001247 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1248 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001249
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001250
1251 /* If more than one user, mutex should be added */
1252 dev->users++;
1253
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001254 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1255 video_device_node_name(vdev), v4l2_type_names[type],
1256 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001257
1258 /* allocate + initialize per filehandle data */
1259 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
1260 if (NULL == fh) {
1261 dev->users--;
1262 return -ENOMEM;
1263 }
1264
1265 file->private_data = fh;
1266 fh->dev = dev;
1267
1268 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1269 dev->fourcc = format[0].fourcc;
1270
1271 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001272
1273 tm6000_get_std_res (dev);
1274
1275 fh->width = dev->width;
1276 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001277
1278 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1279 "dev->vidq=0x%08lx\n",
1280 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1281 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1282 "queued=%d\n",list_empty(&dev->vidq.queued));
1283 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1284 "active=%d\n",list_empty(&dev->vidq.active));
1285
1286 /* initialize hardware on analog mode */
1287 if (dev->mode!=TM6000_MODE_ANALOG) {
1288 rc=tm6000_init_analog_mode (dev);
1289 if (rc<0)
1290 return rc;
1291
1292 /* Put all controls at a sane state */
1293 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1294 qctl_regs[i] =tm6000_qctrl[i].default_value;
1295
1296 dev->mode=TM6000_MODE_ANALOG;
1297 }
1298
1299 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1300 NULL, &dev->slock,
1301 fh->type,
1302 V4L2_FIELD_INTERLACED,
Hans Verkuil08bff032010-09-20 17:39:46 -03001303 sizeof(struct tm6000_buffer), fh, NULL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001304
1305 return 0;
1306}
1307
1308static ssize_t
1309tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1310{
1311 struct tm6000_fh *fh = file->private_data;
1312
1313 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1314 if (res_locked(fh->dev))
1315 return -EBUSY;
1316
1317 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1318 file->f_flags & O_NONBLOCK);
1319 }
1320 return 0;
1321}
1322
1323static unsigned int
1324tm6000_poll(struct file *file, struct poll_table_struct *wait)
1325{
1326 struct tm6000_fh *fh = file->private_data;
1327 struct tm6000_buffer *buf;
1328
1329 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1330 return POLLERR;
1331
1332 if (res_get(fh->dev,fh)) {
1333 /* streaming capture */
1334 if (list_empty(&fh->vb_vidq.stream))
1335 return POLLERR;
1336 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1337 } else {
1338 /* read() capture */
Mauro Carvalho Chehabdc6a02a2007-08-27 07:55:38 -03001339 return videobuf_poll_stream(file, &fh->vb_vidq,
1340 wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001341 }
1342 poll_wait(file, &buf->vb.done, wait);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -03001343 if (buf->vb.state == VIDEOBUF_DONE ||
1344 buf->vb.state == VIDEOBUF_ERROR)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001345 return POLLIN|POLLRDNORM;
1346 return 0;
1347}
1348
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001349static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001350{
1351 struct tm6000_fh *fh = file->private_data;
1352 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001353 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001354
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001355 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1356 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001357
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001358 dev->users--;
1359
1360 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001361 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001362 videobuf_mmap_free(&fh->vb_vidq);
1363 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001364
1365 kfree (fh);
1366
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001367 return 0;
1368}
1369
1370static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1371{
1372 struct tm6000_fh *fh = file->private_data;
1373 int ret;
1374
1375 ret=videobuf_mmap_mapper(&fh->vb_vidq, vma);
1376
1377 return ret;
1378}
1379
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001380static struct v4l2_file_operations tm6000_fops = {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001381 .owner = THIS_MODULE,
1382 .open = tm6000_open,
1383 .release = tm6000_release,
1384 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1385 .read = tm6000_read,
1386 .poll = tm6000_poll,
1387 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001388};
1389
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001390static const struct v4l2_ioctl_ops video_ioctl_ops = {
1391 .vidioc_querycap = vidioc_querycap,
1392 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1393 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1394 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1395 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1396 .vidioc_s_std = vidioc_s_std,
1397 .vidioc_enum_input = vidioc_enum_input,
1398 .vidioc_g_input = vidioc_g_input,
1399 .vidioc_s_input = vidioc_s_input,
1400 .vidioc_queryctrl = vidioc_queryctrl,
1401 .vidioc_g_ctrl = vidioc_g_ctrl,
1402 .vidioc_s_ctrl = vidioc_s_ctrl,
1403 .vidioc_g_tuner = vidioc_g_tuner,
1404 .vidioc_s_tuner = vidioc_s_tuner,
1405 .vidioc_g_frequency = vidioc_g_frequency,
1406 .vidioc_s_frequency = vidioc_s_frequency,
1407 .vidioc_streamon = vidioc_streamon,
1408 .vidioc_streamoff = vidioc_streamoff,
1409 .vidioc_reqbufs = vidioc_reqbufs,
1410 .vidioc_querybuf = vidioc_querybuf,
1411 .vidioc_qbuf = vidioc_qbuf,
1412 .vidioc_dqbuf = vidioc_dqbuf,
1413#ifdef CONFIG_VIDEO_V4L1_COMPAT
1414 .vidiocgmbuf = vidiocgmbuf,
1415#endif
1416};
1417
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001418static struct video_device tm6000_template = {
1419 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001420 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001421 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001422 .release = video_device_release,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001423 .tvnorms = TM6000_STD,
1424 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001425};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001426
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001427/* -----------------------------------------------------------------
1428 Initialization and module stuff
1429 ------------------------------------------------------------------*/
1430
1431int tm6000_v4l2_register(struct tm6000_core *dev)
1432{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001433 int ret = -1;
1434 struct video_device *vfd;
1435
1436 vfd = video_device_alloc();
1437 if(!vfd) {
1438 return -ENOMEM;
1439 }
1440 dev->vfd = vfd;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001441
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001442 /* init video dma queues */
1443 INIT_LIST_HEAD(&dev->vidq.active);
1444 INIT_LIST_HEAD(&dev->vidq.queued);
1445
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001446 memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1447 dev->vfd->debug=tm6000_debug;
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001448 vfd->v4l2_dev = &dev->v4l2_dev;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001449 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001450
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001451 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001452 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001453 return ret;
1454}
1455
1456int tm6000_v4l2_unregister(struct tm6000_core *dev)
1457{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001458 video_unregister_device(dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001459
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001460 return 0;
1461}
1462
1463int tm6000_v4l2_exit(void)
1464{
1465 return 0;
1466}
1467
1468module_param(video_nr, int, 0);
1469MODULE_PARM_DESC(video_nr,"Allow changing video device number");
1470
1471module_param_named (debug, tm6000_debug, int, 0444);
1472MODULE_PARM_DESC(debug,"activates debug info");
1473
1474module_param(vid_limit,int,0644);
1475MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
1476