blob: c5690b2a89244769f6af1bfc4b061f663599e32c [file] [log] [blame]
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001/*
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03002 * tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3 *
4 * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org>
5 *
6 * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
7 * - Fixed module load/unload
8 *
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.
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030021 */
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/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300121 * DMA and thread functions
122 * ------------------------------------------------------------------
123 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300124
125#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300126#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300127
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300128#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 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300134static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300135 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300136{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300137 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300138 char *outp;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300139
140 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300141 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300142 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300143 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300144 }
145
146 *buf = list_entry(dma_q->active.next,
147 struct tm6000_buffer, vb.queue);
148
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300149 if (!buf)
150 return;
151
152 /* Cleans up buffer - Usefull for testing for frame/URB loss */
153 outp = videobuf_to_vmalloc(&(*buf)->vb);
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300154
155 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300156}
157
158/*
159 * Announces that a buffer were filled and request the next
160 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300161static inline void buffer_filled(struct tm6000_core *dev,
162 struct tm6000_dmaqueue *dma_q,
163 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300164{
165 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300166 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300167 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300168 buf->vb.field_count++;
169 do_gettimeofday(&buf->vb.ts);
170
171 list_del(&buf->vb.queue);
172 wake_up(&buf->vb.done);
173}
174
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300175const char *tm6000_msg_type[] = {
176 "unknown(0)", /* 0 */
177 "video", /* 1 */
178 "audio", /* 2 */
179 "vbi", /* 3 */
180 "pts", /* 4 */
181 "err", /* 5 */
182 "unknown(6)", /* 6 */
183 "unknown(7)", /* 7 */
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300184};
185
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300186/*
187 * Identify the tm5600/6000 buffer header type and properly handles
188 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300189static int copy_streams(u8 *data, unsigned long len,
190 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300191{
192 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300193 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
194 u8 *ptr = data, *endp = data+len, c;
195 unsigned long header = 0;
196 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300197 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
198 struct tm6000_buffer *vbuf;
199 char *voutp = NULL;
200 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300201
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300202 /* get video buffer */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300203 get_next_buf(dma_q, &vbuf);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300204 if (!vbuf)
205 return rc;
206 voutp = videobuf_to_vmalloc(&vbuf->vb);
207 if (!voutp)
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300208 return 0;
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300209
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300210 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300211 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300212 /* Header */
213 if (dev->isoc_ctl.tmp_buf_len > 0) {
214 /* from last urb or packet */
215 header = dev->isoc_ctl.tmp_buf;
216 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300217 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300218 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300219 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300220 4 - dev->isoc_ctl.tmp_buf_len);
221 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300222 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300223 dev->isoc_ctl.tmp_buf_len = 0;
224 } else {
225 if (ptr + 3 >= endp) {
226 /* have incomplete header */
227 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300228 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300229 dev->isoc_ctl.tmp_buf_len);
230 return rc;
231 }
232 /* Seek for sync */
233 for (; ptr < endp - 3; ptr++) {
234 if (*(ptr + 3) == 0x47)
235 break;
236 }
237 /* Get message header */
238 header = *(unsigned long *)ptr;
239 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300240 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300241
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300242 /* split the header fields */
243 c = (header >> 24) & 0xff;
244 size = ((header & 0x7e) << 1);
245 if (size > 0)
246 size -= 4;
247 block = (header >> 7) & 0xf;
248 field = (header >> 11) & 0x1;
249 line = (header >> 12) & 0x1ff;
250 cmd = (header >> 21) & 0x7;
251 /* Validates haeder fields */
252 if (size > TM6000_URB_MSG_LEN)
253 size = TM6000_URB_MSG_LEN;
254 pktsize = TM6000_URB_MSG_LEN;
255 /* calculate position in buffer
256 * and change the buffer
257 */
258 switch (cmd) {
259 case TM6000_URB_MSG_VIDEO:
260 if ((dev->isoc_ctl.vfield != field) &&
261 (field == 1)) {
262 /* Announces that a new buffer
263 * were filled
264 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300265 buffer_filled(dev, dma_q, vbuf);
266 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300267 "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300268 get_next_buf(dma_q, &vbuf);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300269 if (!vbuf)
270 return rc;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300271 voutp = videobuf_to_vmalloc(&vbuf->vb);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300272 if (!voutp)
273 return rc;
Mauro Carvalho Chehab758bb0b2010-06-07 12:32:27 -0300274 memset(voutp, 0, vbuf->vb.size);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300275 }
276 linewidth = vbuf->vb.width << 1;
277 pos = ((line << 1) - field - 1) * linewidth +
278 block * TM6000_URB_MSG_LEN;
279 /* Don't allow to write out of the buffer */
280 if (pos + size > vbuf->vb.size)
281 cmd = TM6000_URB_MSG_ERR;
282 dev->isoc_ctl.vfield = field;
283 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300284 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300285 break;
286 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300287 case TM6000_URB_MSG_PTS:
Mauro Carvalho Chehabd0669c82010-06-07 12:10:14 -0300288 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300289 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300290 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300291 } else {
292 /* Continue the last copy */
293 cmd = dev->isoc_ctl.cmd;
294 size = dev->isoc_ctl.size;
295 pos = dev->isoc_ctl.pos;
296 pktsize = dev->isoc_ctl.pktsize;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300297 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300298 cpysize = (endp - ptr > size) ? size : endp - ptr;
299 if (cpysize) {
300 /* copy data in different buffers */
301 switch (cmd) {
302 case TM6000_URB_MSG_VIDEO:
303 /* Fills video buffer */
304 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300305 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300306 break;
307 case TM6000_URB_MSG_AUDIO:
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300308 /* Need some code to copy audio buffer */
309 if (dev->fourcc == V4L2_PIX_FMT_YUYV) {
310 /* Swap word bytes */
311 int i;
312
313 for (i = 0; i < cpysize; i += 2)
314 swab16s((u16 *)(ptr + i));
315 }
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300316 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300317 break;
318 case TM6000_URB_MSG_VBI:
319 /* Need some code to copy vbi buffer */
320 break;
321 case TM6000_URB_MSG_PTS:
322 /* Need some code to copy pts */
323 break;
324 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300325 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300326 if (ptr + pktsize > endp) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300327 /* End of URB packet, but cmd processing is not
328 * complete. Preserve the state for a next packet
329 */
330 dev->isoc_ctl.pos = pos + cpysize;
331 dev->isoc_ctl.size = size - cpysize;
332 dev->isoc_ctl.cmd = cmd;
333 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300334 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300335 } else {
336 dev->isoc_ctl.cmd = 0;
337 ptr += pktsize;
338 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300339 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300340 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300341}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300342
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300343/*
344 * Identify the tm5600/6000 buffer header type and properly handles
345 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300346static int copy_multiplexed(u8 *ptr, unsigned long len,
347 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300348{
349 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300350 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
351 unsigned int pos = dev->isoc_ctl.pos, cpysize;
352 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300353 struct tm6000_buffer *buf;
354 char *outp = NULL;
355
356 get_next_buf(dma_q, &buf);
357 if (buf)
358 outp = videobuf_to_vmalloc(&buf->vb);
359
360 if (!outp)
361 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300362
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300363 while (len > 0) {
364 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300365 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300366 pos += cpysize;
367 ptr += cpysize;
368 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300369 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300370 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300371 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300372 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300373 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300374 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300375 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300376 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300377 outp = videobuf_to_vmalloc(&(buf->vb));
378 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300379 return rc;
380 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300381 }
382 }
383
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300384 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300385 return rc;
386}
387
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300388static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300389 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300390{
391 char *errmsg = "Unknown";
392
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300393 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300394 case -ENOENT:
395 errmsg = "unlinked synchronuously";
396 break;
397 case -ECONNRESET:
398 errmsg = "unlinked asynchronuously";
399 break;
400 case -ENOSR:
401 errmsg = "Buffer error (overrun)";
402 break;
403 case -EPIPE:
404 errmsg = "Stalled (device not responding)";
405 break;
406 case -EOVERFLOW:
407 errmsg = "Babble (bad cable?)";
408 break;
409 case -EPROTO:
410 errmsg = "Bit-stuff error (bad cable?)";
411 break;
412 case -EILSEQ:
413 errmsg = "CRC/Timeout (could be anything)";
414 break;
415 case -ETIME:
416 errmsg = "Device does not respond";
417 break;
418 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300419 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300420 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
421 status, errmsg);
422 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300423 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300424 packet, status, errmsg);
425 }
426}
427
428
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300429/*
430 * Controls the isoc copy of each urb packet
431 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300432static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300433{
434 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300435 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
436 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300437 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300438
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300439 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300440 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300441 return 0;
442 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300443
444 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300445 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300446
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300447 if (status < 0) {
448 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300449 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300450 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300451
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300452 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300453
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300454 if (len > 0) {
455 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300456 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300457 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
458 rc = copy_multiplexed(p, len, urb);
459 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300460 return rc;
461 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300462 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300463 }
464 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300465 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300466 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300467 return rc;
468}
469
470/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300471 * URB control
472 * ------------------------------------------------------------------
473 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300474
475/*
476 * IRQ callback, called by URB callback
477 */
478static void tm6000_irq_callback(struct urb *urb)
479{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300480 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300481 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300482 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300483
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300484 if (!dev)
485 return;
486
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300487 spin_lock(&dev->slock);
488 tm6000_isoc_copy(urb);
489 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300490
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300491 /* Reset urb buffers */
492 for (i = 0; i < urb->number_of_packets; i++) {
493 urb->iso_frame_desc[i].status = 0;
494 urb->iso_frame_desc[i].actual_length = 0;
495 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300496
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300497 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
498 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300499 tm6000_err("urb resubmit failed (error=%i)\n",
500 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300501}
502
503/*
504 * Stop and Deallocate URBs
505 */
506static void tm6000_uninit_isoc(struct tm6000_core *dev)
507{
508 struct urb *urb;
509 int i;
510
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300511 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300512 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300513 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300514 if (urb) {
515 usb_kill_urb(urb);
516 usb_unlink_urb(urb);
517 if (dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300518 usb_free_coherent(dev->udev,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300519 urb->transfer_buffer_length,
520 dev->isoc_ctl.transfer_buffer[i],
521 urb->transfer_dma);
522 }
523 usb_free_urb(urb);
524 dev->isoc_ctl.urb[i] = NULL;
525 }
526 dev->isoc_ctl.transfer_buffer[i] = NULL;
527 }
528
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300529 kfree(dev->isoc_ctl.urb);
530 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300531
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300532 dev->isoc_ctl.urb = NULL;
533 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300534 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300535}
536
537/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300538 * Allocate URBs and start IRQ
539 */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300540static int tm6000_prepare_isoc(struct tm6000_core *dev, unsigned int framesize)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300541{
542 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Stefan Ringel5a4b55e2010-05-19 13:58:27 -0300543 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300544 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300545
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300546 /* De-allocates all pending stuff */
547 tm6000_uninit_isoc(dev);
548
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300549 usb_set_interface(dev->udev,
550 dev->isoc_in.bInterfaceNumber,
551 dev->isoc_in.bAlternateSetting);
552
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300553 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300554 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300555 USB_ENDPOINT_NUMBER_MASK);
556
557 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
558
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300559 if (size > dev->isoc_in.maxsize)
560 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300561
562 dev->isoc_ctl.max_pkt_size = size;
563
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300564 max_packets = (framesize + size - 1) / size;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300565
566 if (max_packets > TM6000_MAX_ISO_PACKETS)
567 max_packets = TM6000_MAX_ISO_PACKETS;
568
569 sb_size = max_packets * size;
570
571 dev->isoc_ctl.num_bufs = num_bufs;
572
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300573 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300574 if (!dev->isoc_ctl.urb) {
575 tm6000_err("cannot alloc memory for usb buffers\n");
576 return -ENOMEM;
577 }
578
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300579 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300580 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300581 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300582 tm6000_err("cannot allocate memory for usbtransfer\n");
583 kfree(dev->isoc_ctl.urb);
584 return -ENOMEM;
585 }
586
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300587 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
588 " (%d bytes) of %d bytes each to handle %u size\n",
589 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300590 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300591
592 /* allocate urbs and transfer buffers */
593 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
594 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
595 if (!urb) {
596 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
597 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehabc1a16412007-10-15 15:43:50 -0300598 usb_free_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300599 return -ENOMEM;
600 }
601 dev->isoc_ctl.urb[i] = urb;
602
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300603 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
Mauro Carvalho Chehabc13dd702007-09-19 07:36:34 -0300604 sb_size, GFP_KERNEL, &urb->transfer_dma);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300605 if (!dev->isoc_ctl.transfer_buffer[i]) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300606 tm6000_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300607 " buffer %i%s\n",
608 sb_size, i,
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300609 in_interrupt() ? " while in int" : "");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300610 tm6000_uninit_isoc(dev);
611 return -ENOMEM;
612 }
613 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
614
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300615 usb_fill_bulk_urb(urb, dev->udev, pipe,
616 dev->isoc_ctl.transfer_buffer[i], sb_size,
617 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300618 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300619 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300620 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300621
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300622 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300623 urb->iso_frame_desc[j].offset = size * j;
624 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300625 }
626 }
627
628 return 0;
629}
630
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300631static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300632{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300633 struct tm6000_dmaqueue *dma_q = &dev->vidq;
634 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300635
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300636 dma_q->frame = 0;
637 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300638
639 init_waitqueue_head(&dma_q->wq);
640
641 /* submit urbs and enables IRQ */
642 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300643 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300644 if (rc) {
645 tm6000_err("submit of urb %i failed (error=%i)\n", i,
646 rc);
647 tm6000_uninit_isoc(dev);
648 return rc;
649 }
650 }
651
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300652 return 0;
653}
654
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300655/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300656 * Videobuf operations
657 * ------------------------------------------------------------------
658 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300659
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300660static int
661buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
662{
663 struct tm6000_fh *fh = vq->priv_data;
664
665 *size = fh->fmt->depth * fh->width * fh->height >> 3;
666 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300667 *count = TM6000_DEF_BUF;
668
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300669 if (*count < TM6000_MIN_BUF)
670 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300671
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300672 while (*size * *count > vid_limit * 1024 * 1024)
673 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300674
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300675 return 0;
676}
677
678static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
679{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300680 struct tm6000_fh *fh = vq->priv_data;
681 struct tm6000_core *dev = fh->dev;
682 unsigned long flags;
683
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300684 if (in_interrupt())
685 BUG();
686
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300687 /* We used to wait for the buffer to finish here, but this didn't work
688 because, as we were keeping the state as VIDEOBUF_QUEUED,
689 videobuf_queue_cancel marked it as finished for us.
690 (Also, it could wedge forever if the hardware was misconfigured.)
691
692 This should be safe; by the time we get here, the buffer isn't
693 queued anymore. If we ever start marking the buffers as
694 VIDEOBUF_ACTIVE, it won't be, though.
695 */
696 spin_lock_irqsave(&dev->slock, flags);
697 if (dev->isoc_ctl.buf == buf)
698 dev->isoc_ctl.buf = NULL;
699 spin_unlock_irqrestore(&dev->slock, flags);
700
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300701 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300702 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300703}
704
705static int
706buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
707 enum v4l2_field field)
708{
709 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300710 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300711 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300712 int rc = 0, urb_init = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300713
714 BUG_ON(NULL == fh->fmt);
715
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300716
717 /* FIXME: It assumes depth=2 */
718 /* The only currently supported format is 16 bits/pixel */
719 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
720 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
721 return -EINVAL;
722
723 if (buf->fmt != fh->fmt ||
724 buf->vb.width != fh->width ||
725 buf->vb.height != fh->height ||
726 buf->vb.field != field) {
727 buf->fmt = fh->fmt;
728 buf->vb.width = fh->width;
729 buf->vb.height = fh->height;
730 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300731 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300732 }
733
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300734 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300735 if (0 != (rc = videobuf_iolock(vq, &buf->vb, NULL)))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300736 goto fail;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300737 urb_init = 1;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300738 }
739
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300740 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300741 urb_init = 1;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300742
743 if (urb_init) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300744 rc = tm6000_prepare_isoc(dev, buf->vb.size);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300745 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300746 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300747
748 rc = tm6000_start_thread(dev);
749 if (rc < 0)
750 goto fail;
751
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300752 }
753
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300754 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300755 return 0;
756
757fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300758 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300759 return rc;
760}
761
762static void
763buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
764{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300765 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300766 struct tm6000_fh *fh = vq->priv_data;
767 struct tm6000_core *dev = fh->dev;
768 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300769
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300770 buf->vb.state = VIDEOBUF_QUEUED;
771 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300772}
773
774static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
775{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300776 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300777
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300778 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300779}
780
781static struct videobuf_queue_ops tm6000_video_qops = {
782 .buf_setup = buffer_setup,
783 .buf_prepare = buffer_prepare,
784 .buf_queue = buffer_queue,
785 .buf_release = buffer_release,
786};
787
788/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300789 * IOCTL handling
790 * ------------------------------------------------------------------
791 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300792
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300793static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300794{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300795 /* Is the current fh handling it? if so, that's OK */
796 if (dev->resources == fh && dev->is_res_read)
797 return true;
798
799 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300800}
801
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300802static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300803{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300804 /* Is the current fh handling it? if so, that's OK */
805 if (dev->resources == fh)
806 return true;
807
808 return false;
809}
810
811static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
812 bool is_res_read)
813{
814 /* Is the current fh handling it? if so, that's OK */
815 if (dev->resources == fh && dev->is_res_read == is_res_read)
816 return true;
817
818 /* is it free? */
819 if (dev->resources)
820 return false;
821
822 /* grab it */
823 dev->resources = fh;
824 dev->is_res_read = is_res_read;
825 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
826 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300827}
828
829static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
830{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300831 /* Is the current fh handling it? if so, that's OK */
832 if (dev->resources != fh)
833 return;
834
835 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300836 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300837}
838
839/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300840 * IOCTL vidioc handling
841 * ------------------------------------------------------------------
842 */
843static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300844 struct v4l2_capability *cap)
845{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300846
847 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300848 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300849 cap->version = TM6000_VERSION;
850 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
851 V4L2_CAP_STREAMING |
852 V4L2_CAP_TUNER |
853 V4L2_CAP_READWRITE;
854 return 0;
855}
856
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300857static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300858 struct v4l2_fmtdesc *f)
859{
860 if (unlikely(f->index >= ARRAY_SIZE(format)))
861 return -EINVAL;
862
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300863 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300864 f->pixelformat = format[f->index].fourcc;
865 return 0;
866}
867
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300868static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300869 struct v4l2_format *f)
870{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300871 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300872
873 f->fmt.pix.width = fh->width;
874 f->fmt.pix.height = fh->height;
875 f->fmt.pix.field = fh->vb_vidq.field;
876 f->fmt.pix.pixelformat = fh->fmt->fourcc;
877 f->fmt.pix.bytesperline =
878 (f->fmt.pix.width * fh->fmt->depth) >> 3;
879 f->fmt.pix.sizeimage =
880 f->fmt.pix.height * f->fmt.pix.bytesperline;
881
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300882 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300883}
884
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300885static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300886{
887 unsigned int i;
888
889 for (i = 0; i < ARRAY_SIZE(format); i++)
890 if (format[i].fourcc == fourcc)
891 return format+i;
892 return NULL;
893}
894
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300895static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300896 struct v4l2_format *f)
897{
898 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
899 struct tm6000_fmt *fmt;
900 enum v4l2_field field;
901
902 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
903 if (NULL == fmt) {
904 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
905 " invalid.\n", f->fmt.pix.pixelformat);
906 return -EINVAL;
907 }
908
909 field = f->fmt.pix.field;
910
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300911 if (field == V4L2_FIELD_ANY)
912 field = V4L2_FIELD_SEQ_TB;
913 else if (V4L2_FIELD_INTERLACED != field) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300914 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
915 return -EINVAL;
916 }
917
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300918 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300919
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300920 f->fmt.pix.width = dev->width;
921 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300922
923 f->fmt.pix.width &= ~0x01;
924
925 f->fmt.pix.field = field;
926
927 f->fmt.pix.bytesperline =
928 (f->fmt.pix.width * fmt->depth) >> 3;
929 f->fmt.pix.sizeimage =
930 f->fmt.pix.height * f->fmt.pix.bytesperline;
931
932 return 0;
933}
934
935/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300936static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300937 struct v4l2_format *f)
938{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300939 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300940 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300941 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300942 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300943 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300944
945 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
946 fh->width = f->fmt.pix.width;
947 fh->height = f->fmt.pix.height;
948 fh->vb_vidq.field = f->fmt.pix.field;
949 fh->type = f->type;
950
951 dev->fourcc = f->fmt.pix.pixelformat;
952
953 tm6000_set_fourcc_format(dev);
954
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300955 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300956}
957
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300958static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300959 struct v4l2_requestbuffers *p)
960{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300961 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300962
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300963 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300964}
965
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300966static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300967 struct v4l2_buffer *p)
968{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300969 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300970
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300971 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300972}
973
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300974static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300975{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300976 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300977
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300978 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300979}
980
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300981static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300982{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300983 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300984
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300985 return videobuf_dqbuf(&fh->vb_vidq, p,
986 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300987}
988
989#ifdef CONFIG_VIDEO_V4L1_COMPAT
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300990static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300991{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300992 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300993
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300994 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300995}
996#endif
997
998static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
999{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001000 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001001 struct tm6000_core *dev = fh->dev;
1002
1003 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1004 return -EINVAL;
1005 if (i != fh->type)
1006 return -EINVAL;
1007
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001008 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001009 return -EBUSY;
1010 return (videobuf_streamon(&fh->vb_vidq));
1011}
1012
1013static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1014{
1015 struct tm6000_fh *fh=priv;
1016 struct tm6000_core *dev = fh->dev;
1017
1018 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1019 return -EINVAL;
1020 if (i != fh->type)
1021 return -EINVAL;
1022
1023 videobuf_streamoff(&fh->vb_vidq);
1024 res_free(dev,fh);
1025
1026 return (0);
1027}
1028
1029static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm)
1030{
1031 int rc=0;
1032 struct tm6000_fh *fh=priv;
1033 struct tm6000_core *dev = fh->dev;
1034
Stefan Ringel4f526102010-10-27 16:48:05 -03001035 dev->norm = *norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001036 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001037
1038 fh->width = dev->width;
1039 fh->height = dev->height;
1040
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001041 if (rc<0)
1042 return rc;
1043
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001044 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001045
1046 return 0;
1047}
1048
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001049static int vidioc_enum_input(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001050 struct v4l2_input *inp)
1051{
1052 switch (inp->index) {
1053 case TM6000_INPUT_TV:
1054 inp->type = V4L2_INPUT_TYPE_TUNER;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001055 strcpy(inp->name, "Television");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001056 break;
1057 case TM6000_INPUT_COMPOSITE:
1058 inp->type = V4L2_INPUT_TYPE_CAMERA;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001059 strcpy(inp->name, "Composite");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001060 break;
1061 case TM6000_INPUT_SVIDEO:
1062 inp->type = V4L2_INPUT_TYPE_CAMERA;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001063 strcpy(inp->name, "S-Video");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001064 break;
1065 default:
1066 return -EINVAL;
1067 }
1068 inp->std = TM6000_STD;
1069
1070 return 0;
1071}
1072
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001073static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001074{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001075 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001076 struct tm6000_core *dev = fh->dev;
1077
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001078 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001079
1080 return 0;
1081}
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001082static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001083{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001084 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001085 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001086 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001087 char buf[1];
1088
1089 switch (i) {
1090 case TM6000_INPUT_TV:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001091 dev->input = i;
1092 *buf = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001093 break;
1094 case TM6000_INPUT_COMPOSITE:
1095 case TM6000_INPUT_SVIDEO:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001096 dev->input = i;
1097 *buf = 1;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001098 break;
1099 default:
1100 return -EINVAL;
1101 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001102 rc = tm6000_read_write_usb(dev, USB_DIR_OUT | USB_TYPE_VENDOR,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001103 REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1);
1104
1105 if (!rc) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001106 dev->input = i;
1107 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001108 }
1109
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001110 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001111}
1112
1113 /* --- controls ---------------------------------------------- */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001114static int vidioc_queryctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001115 struct v4l2_queryctrl *qc)
1116{
1117 int i;
1118
1119 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1120 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1121 memcpy(qc, &(tm6000_qctrl[i]),
1122 sizeof(*qc));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001123 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001124 }
1125
1126 return -EINVAL;
1127}
1128
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001129static int vidioc_g_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001130 struct v4l2_control *ctrl)
1131{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001132 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001133 struct tm6000_core *dev = fh->dev;
1134 int val;
1135
1136 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1137 switch (ctrl->id) {
1138 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001139 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001140 break;
1141 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001142 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001143 return 0;
1144 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001145 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001146 return 0;
1147 case V4L2_CID_HUE:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001148 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001149 return 0;
1150 default:
1151 return -EINVAL;
1152 }
1153
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001154 if (val < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001155 return val;
1156
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001157 ctrl->value = val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001158
1159 return 0;
1160}
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001161static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001162 struct v4l2_control *ctrl)
1163{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001164 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001165 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001166 u8 val = ctrl->value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001167
1168 switch (ctrl->id) {
1169 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001170 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001171 return 0;
1172 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001173 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001174 return 0;
1175 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001176 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001177 return 0;
1178 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001179 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001180 return 0;
1181 }
1182 return -EINVAL;
1183}
1184
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001185static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001186 struct v4l2_tuner *t)
1187{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001188 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001189 struct tm6000_core *dev = fh->dev;
1190
1191 if (unlikely(UNSET == dev->tuner_type))
1192 return -EINVAL;
1193 if (0 != t->index)
1194 return -EINVAL;
1195
1196 strcpy(t->name, "Television");
1197 t->type = V4L2_TUNER_ANALOG_TV;
1198 t->capability = V4L2_TUNER_CAP_NORM;
1199 t->rangehigh = 0xffffffffUL;
1200 t->rxsubchans = V4L2_TUNER_SUB_MONO;
1201
1202 return 0;
1203}
1204
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001205static int vidioc_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001206 struct v4l2_tuner *t)
1207{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001208 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001209 struct tm6000_core *dev = fh->dev;
1210
1211 if (UNSET == dev->tuner_type)
1212 return -EINVAL;
1213 if (0 != t->index)
1214 return -EINVAL;
1215
1216 return 0;
1217}
1218
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001219static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001220 struct v4l2_frequency *f)
1221{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001222 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001223 struct tm6000_core *dev = fh->dev;
1224
1225 if (unlikely(UNSET == dev->tuner_type))
1226 return -EINVAL;
1227
1228 f->type = V4L2_TUNER_ANALOG_TV;
1229 f->frequency = dev->freq;
1230
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001231 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001232
1233 return 0;
1234}
1235
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001236static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001237 struct v4l2_frequency *f)
1238{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001239 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001240 struct tm6000_core *dev = fh->dev;
1241
1242 if (unlikely(f->type != V4L2_TUNER_ANALOG_TV))
1243 return -EINVAL;
1244
1245 if (unlikely(UNSET == dev->tuner_type))
1246 return -EINVAL;
1247 if (unlikely(f->tuner != 0))
1248 return -EINVAL;
1249
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001250 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001251 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001252
1253 return 0;
1254}
1255
1256/* ------------------------------------------------------------------
1257 File operations for the device
1258 ------------------------------------------------------------------*/
1259
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001260static int tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001261{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001262 struct video_device *vdev = video_devdata(file);
1263 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001264 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001265 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001266 int i, rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001267
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001268 printk(KERN_INFO "tm6000: open called (dev=%s)\n",
1269 video_device_node_name(vdev));
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001270
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001271 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1272 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001273
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001274
1275 /* If more than one user, mutex should be added */
1276 dev->users++;
1277
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001278 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1279 video_device_node_name(vdev), v4l2_type_names[type],
1280 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001281
1282 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001283 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001284 if (NULL == fh) {
1285 dev->users--;
1286 return -ENOMEM;
1287 }
1288
1289 file->private_data = fh;
1290 fh->dev = dev;
1291
1292 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1293 dev->fourcc = format[0].fourcc;
1294
1295 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001296
1297 tm6000_get_std_res (dev);
1298
1299 fh->width = dev->width;
1300 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001301
1302 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1303 "dev->vidq=0x%08lx\n",
1304 (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq);
1305 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1306 "queued=%d\n",list_empty(&dev->vidq.queued));
1307 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
1308 "active=%d\n",list_empty(&dev->vidq.active));
1309
1310 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001311 rc = tm6000_init_analog_mode(dev);
1312 if (rc < 0)
1313 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001314
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001315 if (dev->mode != TM6000_MODE_ANALOG) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001316 /* Put all controls at a sane state */
1317 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001318 qctl_regs[i] = tm6000_qctrl[i].default_value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001319
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001320 dev->mode = TM6000_MODE_ANALOG;
1321 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001322
1323 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1324 NULL, &dev->slock,
1325 fh->type,
1326 V4L2_FIELD_INTERLACED,
Mauro Carvalho Chehabdc961132010-10-09 17:05:14 -03001327 sizeof(struct tm6000_buffer), fh, &dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001328
1329 return 0;
1330}
1331
1332static ssize_t
1333tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1334{
1335 struct tm6000_fh *fh = file->private_data;
1336
1337 if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001338 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001339 return -EBUSY;
1340
1341 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1342 file->f_flags & O_NONBLOCK);
1343 }
1344 return 0;
1345}
1346
1347static unsigned int
1348tm6000_poll(struct file *file, struct poll_table_struct *wait)
1349{
1350 struct tm6000_fh *fh = file->private_data;
1351 struct tm6000_buffer *buf;
1352
1353 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1354 return POLLERR;
1355
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001356 if (!!is_res_streaming(fh->dev, fh))
1357 return POLLERR;
1358
1359 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001360 /* streaming capture */
1361 if (list_empty(&fh->vb_vidq.stream))
1362 return POLLERR;
1363 buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream);
1364 } else {
1365 /* read() capture */
Mauro Carvalho Chehabdc6a02a2007-08-27 07:55:38 -03001366 return videobuf_poll_stream(file, &fh->vb_vidq,
1367 wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001368 }
1369 poll_wait(file, &buf->vb.done, wait);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -03001370 if (buf->vb.state == VIDEOBUF_DONE ||
1371 buf->vb.state == VIDEOBUF_ERROR)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001372 return POLLIN | POLLRDNORM;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001373 return 0;
1374}
1375
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001376static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001377{
1378 struct tm6000_fh *fh = file->private_data;
1379 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001380 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001381
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001382 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1383 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001384
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001385 dev->users--;
1386
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001387 res_free(dev, fh);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001388 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001389 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001390 videobuf_mmap_free(&fh->vb_vidq);
1391 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001392
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001393 kfree(fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001394
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001395 return 0;
1396}
1397
1398static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1399{
1400 struct tm6000_fh *fh = file->private_data;
1401 int ret;
1402
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001403 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001404
1405 return ret;
1406}
1407
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001408static struct v4l2_file_operations tm6000_fops = {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001409 .owner = THIS_MODULE,
1410 .open = tm6000_open,
1411 .release = tm6000_release,
Mauro Carvalho Chehabdc961132010-10-09 17:05:14 -03001412 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001413 .read = tm6000_read,
1414 .poll = tm6000_poll,
1415 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001416};
1417
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001418static const struct v4l2_ioctl_ops video_ioctl_ops = {
1419 .vidioc_querycap = vidioc_querycap,
1420 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1421 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1422 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1423 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1424 .vidioc_s_std = vidioc_s_std,
1425 .vidioc_enum_input = vidioc_enum_input,
1426 .vidioc_g_input = vidioc_g_input,
1427 .vidioc_s_input = vidioc_s_input,
1428 .vidioc_queryctrl = vidioc_queryctrl,
1429 .vidioc_g_ctrl = vidioc_g_ctrl,
1430 .vidioc_s_ctrl = vidioc_s_ctrl,
1431 .vidioc_g_tuner = vidioc_g_tuner,
1432 .vidioc_s_tuner = vidioc_s_tuner,
1433 .vidioc_g_frequency = vidioc_g_frequency,
1434 .vidioc_s_frequency = vidioc_s_frequency,
1435 .vidioc_streamon = vidioc_streamon,
1436 .vidioc_streamoff = vidioc_streamoff,
1437 .vidioc_reqbufs = vidioc_reqbufs,
1438 .vidioc_querybuf = vidioc_querybuf,
1439 .vidioc_qbuf = vidioc_qbuf,
1440 .vidioc_dqbuf = vidioc_dqbuf,
1441#ifdef CONFIG_VIDEO_V4L1_COMPAT
1442 .vidiocgmbuf = vidiocgmbuf,
1443#endif
1444};
1445
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001446static struct video_device tm6000_template = {
1447 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001448 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001449 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001450 .release = video_device_release,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001451 .tvnorms = TM6000_STD,
1452 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001453};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001454
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001455/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001456 * Initialization and module stuff
1457 * ------------------------------------------------------------------
1458 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001459
1460int tm6000_v4l2_register(struct tm6000_core *dev)
1461{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001462 int ret = -1;
1463 struct video_device *vfd;
1464
1465 vfd = video_device_alloc();
1466 if(!vfd) {
1467 return -ENOMEM;
1468 }
1469 dev->vfd = vfd;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001470
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001471 /* init video dma queues */
1472 INIT_LIST_HEAD(&dev->vidq.active);
1473 INIT_LIST_HEAD(&dev->vidq.queued);
1474
Mauro Carvalho Chehabdc961132010-10-09 17:05:14 -03001475 memcpy(dev->vfd, &tm6000_template, sizeof(*(dev->vfd)));
1476 dev->vfd->debug = tm6000_debug;
1477 dev->vfd->lock = &dev->lock;
1478
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001479 vfd->v4l2_dev = &dev->v4l2_dev;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001480 video_set_drvdata(vfd, dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001481
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001482 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001483 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001484 return ret;
1485}
1486
1487int tm6000_v4l2_unregister(struct tm6000_core *dev)
1488{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001489 video_unregister_device(dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001490
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001491 return 0;
1492}
1493
1494int tm6000_v4l2_exit(void)
1495{
1496 return 0;
1497}
1498
1499module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001500MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001501
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001502module_param_named(debug, tm6000_debug, int, 0444);
1503MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001504
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001505module_param(vid_limit, int, 0644);
1506MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001507