blob: f656fd7a39a27de037923320ea605c0fe14c1e83 [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 */
Thierry Reding3d1a51d2011-08-04 04:14:01 -030022
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030023#include <linux/module.h>
24#include <linux/delay.h>
25#include <linux/errno.h>
26#include <linux/fs.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/ioport.h>
31#include <linux/init.h>
32#include <linux/sched.h>
33#include <linux/random.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030034#include <linux/usb.h>
35#include <linux/videodev2.h>
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -030036#include <media/v4l2-ioctl.h>
Stefan Ringel886a3c02011-05-09 16:53:50 -030037#include <media/tuner.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030038#include <linux/interrupt.h>
39#include <linux/kthread.h>
40#include <linux/highmem.h>
41#include <linux/freezer.h>
42
43#include "tm6000-regs.h"
44#include "tm6000.h"
45
46#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
47
Michel Ludwig95a83822007-07-24 08:06:45 -030048/* Limits minimum and default number of buffers */
49#define TM6000_MIN_BUF 4
50#define TM6000_DEF_BUF 8
51
Stefan Ringel5a4b55e2010-05-19 13:58:27 -030052#define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -030053
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030054/* Declare static vars that will be used as parameters */
55static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
56static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -030057static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030058
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030059/* Debug level */
60int tm6000_debug;
Mauro Carvalho Chehabfaa7c132010-06-04 21:08:25 -030061EXPORT_SYMBOL_GPL(tm6000_debug);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030062
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -030063static const struct v4l2_queryctrl no_ctrl = {
64 .name = "42",
65 .flags = V4L2_CTRL_FLAG_DISABLED,
66};
67
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030068/* supported controls */
69static struct v4l2_queryctrl tm6000_qctrl[] = {
70 {
71 .id = V4L2_CID_BRIGHTNESS,
72 .type = V4L2_CTRL_TYPE_INTEGER,
73 .name = "Brightness",
74 .minimum = 0,
75 .maximum = 255,
76 .step = 1,
77 .default_value = 54,
78 .flags = 0,
79 }, {
80 .id = V4L2_CID_CONTRAST,
81 .type = V4L2_CTRL_TYPE_INTEGER,
82 .name = "Contrast",
83 .minimum = 0,
84 .maximum = 255,
85 .step = 0x1,
86 .default_value = 119,
87 .flags = 0,
88 }, {
89 .id = V4L2_CID_SATURATION,
90 .type = V4L2_CTRL_TYPE_INTEGER,
91 .name = "Saturation",
92 .minimum = 0,
93 .maximum = 255,
94 .step = 0x1,
95 .default_value = 112,
96 .flags = 0,
97 }, {
98 .id = V4L2_CID_HUE,
99 .type = V4L2_CTRL_TYPE_INTEGER,
100 .name = "Hue",
101 .minimum = -128,
102 .maximum = 127,
103 .step = 0x1,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300104 .default_value = 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300105 .flags = 0,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300106 },
107 /* --- audio --- */
108 {
109 .id = V4L2_CID_AUDIO_MUTE,
110 .name = "Mute",
111 .minimum = 0,
112 .maximum = 1,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 }, {
115 .id = V4L2_CID_AUDIO_VOLUME,
116 .name = "Volume",
117 .minimum = -15,
118 .maximum = 15,
119 .step = 1,
120 .default_value = 0,
121 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300122 }
123};
124
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300125static const unsigned int CTRLS = ARRAY_SIZE(tm6000_qctrl);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300126static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
127
128static struct tm6000_fmt format[] = {
129 {
130 .name = "4:2:2, packed, YVY2",
131 .fourcc = V4L2_PIX_FMT_YUYV,
132 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300133 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300134 .name = "4:2:2, packed, UYVY",
135 .fourcc = V4L2_PIX_FMT_UYVY,
136 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300137 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300138 .name = "A/V + VBI mux packet",
139 .fourcc = V4L2_PIX_FMT_TM6000,
140 .depth = 16,
141 }
142};
143
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300144static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
145{
146 unsigned int i;
147
148 for (i = 0; i < CTRLS; i++)
149 if (tm6000_qctrl[i].id == id)
150 return tm6000_qctrl+i;
151 return NULL;
152}
153
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300154/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300155 * DMA and thread functions
156 * ------------------------------------------------------------------
157 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300158
159#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300160#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300161
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300162#define norm_minw(a) norm_maxw(a)
163#define norm_minh(a) norm_maxh(a)
164
165/*
166 * video-buf generic routine to get the next available buffer
167 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300168static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300169 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300170{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300171 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300172
173 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300174 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300175 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300176 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300177 }
178
179 *buf = list_entry(dma_q->active.next,
180 struct tm6000_buffer, vb.queue);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300181}
182
183/*
184 * Announces that a buffer were filled and request the next
185 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300186static inline void buffer_filled(struct tm6000_core *dev,
187 struct tm6000_dmaqueue *dma_q,
188 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300189{
190 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300191 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300192 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300193 buf->vb.field_count++;
194 do_gettimeofday(&buf->vb.ts);
195
196 list_del(&buf->vb.queue);
197 wake_up(&buf->vb.done);
198}
199
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300200/*
201 * Identify the tm5600/6000 buffer header type and properly handles
202 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300203static int copy_streams(u8 *data, unsigned long len,
204 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300205{
206 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300207 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Hans Verkuil81730902012-04-20 07:30:48 -0300208 u8 *ptr = data, *endp = data+len;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300209 unsigned long header = 0;
210 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300211 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300212 struct tm6000_buffer *vbuf = NULL;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300213 char *voutp = NULL;
214 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300215
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300216 if (!dev->radio) {
217 /* get video buffer */
218 get_next_buf(dma_q, &vbuf);
219
220 if (!vbuf)
221 return rc;
222 voutp = videobuf_to_vmalloc(&vbuf->vb);
223
224 if (!voutp)
225 return 0;
226 }
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300227
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300228 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300229 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300230 /* Header */
231 if (dev->isoc_ctl.tmp_buf_len > 0) {
232 /* from last urb or packet */
233 header = dev->isoc_ctl.tmp_buf;
234 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300235 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300236 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300237 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300238 4 - dev->isoc_ctl.tmp_buf_len);
239 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300240 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300241 dev->isoc_ctl.tmp_buf_len = 0;
242 } else {
243 if (ptr + 3 >= endp) {
244 /* have incomplete header */
245 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300246 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300247 dev->isoc_ctl.tmp_buf_len);
248 return rc;
249 }
250 /* Seek for sync */
251 for (; ptr < endp - 3; ptr++) {
252 if (*(ptr + 3) == 0x47)
253 break;
254 }
255 /* Get message header */
256 header = *(unsigned long *)ptr;
257 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300258 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300259
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300260 /* split the header fields */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300261 size = ((header & 0x7e) << 1);
262 if (size > 0)
263 size -= 4;
264 block = (header >> 7) & 0xf;
265 field = (header >> 11) & 0x1;
266 line = (header >> 12) & 0x1ff;
267 cmd = (header >> 21) & 0x7;
268 /* Validates haeder fields */
269 if (size > TM6000_URB_MSG_LEN)
270 size = TM6000_URB_MSG_LEN;
271 pktsize = TM6000_URB_MSG_LEN;
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300272 /*
273 * calculate position in buffer and change the buffer
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300274 */
275 switch (cmd) {
276 case TM6000_URB_MSG_VIDEO:
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300277 if (!dev->radio) {
278 if ((dev->isoc_ctl.vfield != field) &&
279 (field == 1)) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300280 /*
281 * Announces that a new buffer
282 * were filled
283 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300284 buffer_filled(dev, dma_q, vbuf);
285 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300286 "new buffer filled\n");
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300287 get_next_buf(dma_q, &vbuf);
288 if (!vbuf)
289 return rc;
290 voutp = videobuf_to_vmalloc(&vbuf->vb);
291 if (!voutp)
292 return rc;
293 memset(voutp, 0, vbuf->vb.size);
294 }
295 linewidth = vbuf->vb.width << 1;
296 pos = ((line << 1) - field - 1) *
297 linewidth + block * TM6000_URB_MSG_LEN;
298 /* Don't allow to write out of the buffer */
299 if (pos + size > vbuf->vb.size)
300 cmd = TM6000_URB_MSG_ERR;
301 dev->isoc_ctl.vfield = field;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300302 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300303 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300304 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300305 break;
306 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300307 case TM6000_URB_MSG_PTS:
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300308 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300309 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300310 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300311 } else {
312 /* Continue the last copy */
313 cmd = dev->isoc_ctl.cmd;
314 size = dev->isoc_ctl.size;
315 pos = dev->isoc_ctl.pos;
316 pktsize = dev->isoc_ctl.pktsize;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300317 field = dev->isoc_ctl.field;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300318 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300319 cpysize = (endp - ptr > size) ? size : endp - ptr;
320 if (cpysize) {
321 /* copy data in different buffers */
322 switch (cmd) {
323 case TM6000_URB_MSG_VIDEO:
324 /* Fills video buffer */
325 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300326 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300327 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300328 case TM6000_URB_MSG_AUDIO: {
329 int i;
330 for (i = 0; i < cpysize; i += 2)
331 swab16s((u16 *)(ptr + i));
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300332
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300333 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300334 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300335 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300336 case TM6000_URB_MSG_VBI:
337 /* Need some code to copy vbi buffer */
338 break;
Stefan Ringel2f349da2011-05-09 16:54:02 -0300339 case TM6000_URB_MSG_PTS: {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300340 /* Need some code to copy pts */
Stefan Ringel2f349da2011-05-09 16:54:02 -0300341 u32 pts;
342 pts = *(u32 *)ptr;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300343 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
344 field, pts);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300345 break;
346 }
Stefan Ringel2f349da2011-05-09 16:54:02 -0300347 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300348 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300349 if (ptr + pktsize > endp) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300350 /*
351 * End of URB packet, but cmd processing is not
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300352 * complete. Preserve the state for a next packet
353 */
354 dev->isoc_ctl.pos = pos + cpysize;
355 dev->isoc_ctl.size = size - cpysize;
356 dev->isoc_ctl.cmd = cmd;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300357 dev->isoc_ctl.field = field;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300358 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300359 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300360 } else {
361 dev->isoc_ctl.cmd = 0;
362 ptr += pktsize;
363 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300364 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300365 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300366}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300367
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300368/*
369 * Identify the tm5600/6000 buffer header type and properly handles
370 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300371static int copy_multiplexed(u8 *ptr, unsigned long len,
372 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300373{
374 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300375 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
376 unsigned int pos = dev->isoc_ctl.pos, cpysize;
377 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300378 struct tm6000_buffer *buf;
379 char *outp = NULL;
380
381 get_next_buf(dma_q, &buf);
382 if (buf)
383 outp = videobuf_to_vmalloc(&buf->vb);
384
385 if (!outp)
386 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300387
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300388 while (len > 0) {
389 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300390 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300391 pos += cpysize;
392 ptr += cpysize;
393 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300394 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300395 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300396 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300397 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300398 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300399 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300400 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300401 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300402 outp = videobuf_to_vmalloc(&(buf->vb));
403 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300404 return rc;
405 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300406 }
407 }
408
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300409 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300410 return rc;
411}
412
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300413static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300414 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300415{
416 char *errmsg = "Unknown";
417
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300418 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300419 case -ENOENT:
420 errmsg = "unlinked synchronuously";
421 break;
422 case -ECONNRESET:
423 errmsg = "unlinked asynchronuously";
424 break;
425 case -ENOSR:
426 errmsg = "Buffer error (overrun)";
427 break;
428 case -EPIPE:
429 errmsg = "Stalled (device not responding)";
430 break;
431 case -EOVERFLOW:
432 errmsg = "Babble (bad cable?)";
433 break;
434 case -EPROTO:
435 errmsg = "Bit-stuff error (bad cable?)";
436 break;
437 case -EILSEQ:
438 errmsg = "CRC/Timeout (could be anything)";
439 break;
440 case -ETIME:
441 errmsg = "Device does not respond";
442 break;
443 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300444 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300445 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
446 status, errmsg);
447 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300448 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300449 packet, status, errmsg);
450 }
451}
452
453
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300454/*
455 * Controls the isoc copy of each urb packet
456 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300457static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300458{
459 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300460 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
461 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300462 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300463
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300464 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300465 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300466 return 0;
467 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300468
469 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300470 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300471
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300472 if (status < 0) {
473 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300474 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300475 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300476
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300477 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300478
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300479 if (len > 0) {
480 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300481 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300482 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
483 rc = copy_multiplexed(p, len, urb);
484 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300485 return rc;
486 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300487 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300488 }
489 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300490 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300491 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300492 return rc;
493}
494
495/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300496 * URB control
497 * ------------------------------------------------------------------
498 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300499
500/*
501 * IRQ callback, called by URB callback
502 */
503static void tm6000_irq_callback(struct urb *urb)
504{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300505 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300506 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300507 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300508
Thierry Reding14f09152011-08-04 04:14:06 -0300509 switch (urb->status) {
510 case 0:
511 case -ETIMEDOUT:
512 break;
513
514 case -ECONNRESET:
515 case -ENOENT:
516 case -ESHUTDOWN:
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300517 return;
518
Thierry Reding14f09152011-08-04 04:14:06 -0300519 default:
520 tm6000_err("urb completion error %d.\n", urb->status);
521 break;
522 }
523
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300524 spin_lock(&dev->slock);
525 tm6000_isoc_copy(urb);
526 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300527
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300528 /* Reset urb buffers */
529 for (i = 0; i < urb->number_of_packets; i++) {
530 urb->iso_frame_desc[i].status = 0;
531 urb->iso_frame_desc[i].actual_length = 0;
532 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300533
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300534 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
535 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300536 tm6000_err("urb resubmit failed (error=%i)\n",
537 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300538}
539
540/*
541 * Stop and Deallocate URBs
542 */
543static void tm6000_uninit_isoc(struct tm6000_core *dev)
544{
545 struct urb *urb;
546 int i;
547
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300548 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300549 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300550 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300551 if (urb) {
552 usb_kill_urb(urb);
553 usb_unlink_urb(urb);
554 if (dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300555 usb_free_coherent(dev->udev,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300556 urb->transfer_buffer_length,
557 dev->isoc_ctl.transfer_buffer[i],
558 urb->transfer_dma);
559 }
560 usb_free_urb(urb);
561 dev->isoc_ctl.urb[i] = NULL;
562 }
563 dev->isoc_ctl.transfer_buffer[i] = NULL;
564 }
565
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300566 kfree(dev->isoc_ctl.urb);
567 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300568
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300569 dev->isoc_ctl.urb = NULL;
570 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300571 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300572}
573
574/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300575 * Allocate URBs and start IRQ
576 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300577static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300578{
579 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Stefan Ringel5a4b55e2010-05-19 13:58:27 -0300580 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300581 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300582
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300583 /* De-allocates all pending stuff */
584 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300585 /* Stop interrupt USB pipe */
586 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300587
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300588 usb_set_interface(dev->udev,
589 dev->isoc_in.bInterfaceNumber,
590 dev->isoc_in.bAlternateSetting);
591
Dmitri Belimov641d2112010-12-22 05:57:46 -0300592 /* Start interrupt USB pipe */
593 tm6000_ir_int_start(dev);
594
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300595 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300596 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300597 USB_ENDPOINT_NUMBER_MASK);
598
599 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
600
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300601 if (size > dev->isoc_in.maxsize)
602 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300603
604 dev->isoc_ctl.max_pkt_size = size;
605
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300606 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300607 sb_size = max_packets * size;
608
609 dev->isoc_ctl.num_bufs = num_bufs;
610
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300611 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300612 if (!dev->isoc_ctl.urb) {
613 tm6000_err("cannot alloc memory for usb buffers\n");
614 return -ENOMEM;
615 }
616
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300617 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300618 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300619 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300620 tm6000_err("cannot allocate memory for usbtransfer\n");
621 kfree(dev->isoc_ctl.urb);
622 return -ENOMEM;
623 }
624
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300625 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
626 " (%d bytes) of %d bytes each to handle %u size\n",
627 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300628 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300629
630 /* allocate urbs and transfer buffers */
631 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
632 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
633 if (!urb) {
634 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
635 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehabc1a16412007-10-15 15:43:50 -0300636 usb_free_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300637 return -ENOMEM;
638 }
639 dev->isoc_ctl.urb[i] = urb;
640
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300641 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
Mauro Carvalho Chehabc13dd702007-09-19 07:36:34 -0300642 sb_size, GFP_KERNEL, &urb->transfer_dma);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300643 if (!dev->isoc_ctl.transfer_buffer[i]) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300644 tm6000_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300645 " buffer %i%s\n",
646 sb_size, i,
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300647 in_interrupt() ? " while in int" : "");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300648 tm6000_uninit_isoc(dev);
649 return -ENOMEM;
650 }
651 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
652
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300653 usb_fill_bulk_urb(urb, dev->udev, pipe,
654 dev->isoc_ctl.transfer_buffer[i], sb_size,
655 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300656 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300657 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300658 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300659
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300660 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300661 urb->iso_frame_desc[j].offset = size * j;
662 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300663 }
664 }
665
666 return 0;
667}
668
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300669static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300670{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300671 struct tm6000_dmaqueue *dma_q = &dev->vidq;
672 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300673
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300674 dma_q->frame = 0;
675 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300676
677 init_waitqueue_head(&dma_q->wq);
678
679 /* submit urbs and enables IRQ */
680 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300681 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300682 if (rc) {
683 tm6000_err("submit of urb %i failed (error=%i)\n", i,
684 rc);
685 tm6000_uninit_isoc(dev);
686 return rc;
687 }
688 }
689
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300690 return 0;
691}
692
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300693/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300694 * Videobuf operations
695 * ------------------------------------------------------------------
696 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300697
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300698static int
699buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
700{
701 struct tm6000_fh *fh = vq->priv_data;
702
703 *size = fh->fmt->depth * fh->width * fh->height >> 3;
704 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300705 *count = TM6000_DEF_BUF;
706
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300707 if (*count < TM6000_MIN_BUF)
708 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300709
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300710 while (*size * *count > vid_limit * 1024 * 1024)
711 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300712
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300713 return 0;
714}
715
716static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
717{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300718 struct tm6000_fh *fh = vq->priv_data;
719 struct tm6000_core *dev = fh->dev;
720 unsigned long flags;
721
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300722 if (in_interrupt())
723 BUG();
724
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300725 /* We used to wait for the buffer to finish here, but this didn't work
726 because, as we were keeping the state as VIDEOBUF_QUEUED,
727 videobuf_queue_cancel marked it as finished for us.
728 (Also, it could wedge forever if the hardware was misconfigured.)
729
730 This should be safe; by the time we get here, the buffer isn't
731 queued anymore. If we ever start marking the buffers as
732 VIDEOBUF_ACTIVE, it won't be, though.
733 */
734 spin_lock_irqsave(&dev->slock, flags);
735 if (dev->isoc_ctl.buf == buf)
736 dev->isoc_ctl.buf = NULL;
737 spin_unlock_irqrestore(&dev->slock, flags);
738
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300739 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300740 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300741}
742
743static int
744buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
745 enum v4l2_field field)
746{
747 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300748 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300749 struct tm6000_core *dev = fh->dev;
Thierry Reding88e834a2011-08-04 04:14:12 -0300750 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300751
752 BUG_ON(NULL == fh->fmt);
753
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300754
755 /* FIXME: It assumes depth=2 */
756 /* The only currently supported format is 16 bits/pixel */
757 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
758 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
759 return -EINVAL;
760
761 if (buf->fmt != fh->fmt ||
762 buf->vb.width != fh->width ||
763 buf->vb.height != fh->height ||
764 buf->vb.field != field) {
765 buf->fmt = fh->fmt;
766 buf->vb.width = fh->width;
767 buf->vb.height = fh->height;
768 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300769 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300770 }
771
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300772 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Curtis McEnroeed7c2212011-06-01 22:21:56 -0400773 rc = videobuf_iolock(vq, &buf->vb, NULL);
774 if (rc != 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300775 goto fail;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300776 }
777
Thierry Reding88e834a2011-08-04 04:14:12 -0300778 if (!dev->isoc_ctl.num_bufs) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300779 rc = tm6000_prepare_isoc(dev);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300780 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300781 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300782
783 rc = tm6000_start_thread(dev);
784 if (rc < 0)
785 goto fail;
786
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300787 }
788
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300789 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300790 return 0;
791
792fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300793 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300794 return rc;
795}
796
797static void
798buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
799{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300800 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300801 struct tm6000_fh *fh = vq->priv_data;
802 struct tm6000_core *dev = fh->dev;
803 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300804
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300805 buf->vb.state = VIDEOBUF_QUEUED;
806 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300807}
808
809static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
810{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300811 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300812
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300813 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300814}
815
816static struct videobuf_queue_ops tm6000_video_qops = {
817 .buf_setup = buffer_setup,
818 .buf_prepare = buffer_prepare,
819 .buf_queue = buffer_queue,
820 .buf_release = buffer_release,
821};
822
823/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300824 * IOCTL handling
825 * ------------------------------------------------------------------
826 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300827
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300828static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300829{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300830 /* Is the current fh handling it? if so, that's OK */
831 if (dev->resources == fh && dev->is_res_read)
832 return true;
833
834 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300835}
836
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300837static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300838{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300839 /* Is the current fh handling it? if so, that's OK */
840 if (dev->resources == fh)
841 return true;
842
843 return false;
844}
845
846static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
847 bool is_res_read)
848{
849 /* Is the current fh handling it? if so, that's OK */
850 if (dev->resources == fh && dev->is_res_read == is_res_read)
851 return true;
852
853 /* is it free? */
854 if (dev->resources)
855 return false;
856
857 /* grab it */
858 dev->resources = fh;
859 dev->is_res_read = is_res_read;
860 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
861 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300862}
863
864static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
865{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300866 /* Is the current fh handling it? if so, that's OK */
867 if (dev->resources != fh)
868 return;
869
870 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300871 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300872}
873
874/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300875 * IOCTL vidioc handling
876 * ------------------------------------------------------------------
877 */
878static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300879 struct v4l2_capability *cap)
880{
Stefan Ringel886a3c02011-05-09 16:53:50 -0300881 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300882
883 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300884 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300885 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
886 V4L2_CAP_STREAMING |
Stefan Ringel886a3c02011-05-09 16:53:50 -0300887 V4L2_CAP_AUDIO |
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300888 V4L2_CAP_READWRITE;
Stefan Ringel886a3c02011-05-09 16:53:50 -0300889
890 if (dev->tuner_type != TUNER_ABSENT)
891 cap->capabilities |= V4L2_CAP_TUNER;
892
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300893 return 0;
894}
895
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300896static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300897 struct v4l2_fmtdesc *f)
898{
899 if (unlikely(f->index >= ARRAY_SIZE(format)))
900 return -EINVAL;
901
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300902 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300903 f->pixelformat = format[f->index].fourcc;
904 return 0;
905}
906
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300907static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300908 struct v4l2_format *f)
909{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300910 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300911
912 f->fmt.pix.width = fh->width;
913 f->fmt.pix.height = fh->height;
914 f->fmt.pix.field = fh->vb_vidq.field;
915 f->fmt.pix.pixelformat = fh->fmt->fourcc;
916 f->fmt.pix.bytesperline =
917 (f->fmt.pix.width * fh->fmt->depth) >> 3;
918 f->fmt.pix.sizeimage =
919 f->fmt.pix.height * f->fmt.pix.bytesperline;
920
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300921 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300922}
923
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300924static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300925{
926 unsigned int i;
927
928 for (i = 0; i < ARRAY_SIZE(format); i++)
929 if (format[i].fourcc == fourcc)
930 return format+i;
931 return NULL;
932}
933
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300934static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300935 struct v4l2_format *f)
936{
937 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
938 struct tm6000_fmt *fmt;
939 enum v4l2_field field;
940
941 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
942 if (NULL == fmt) {
943 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
944 " invalid.\n", f->fmt.pix.pixelformat);
945 return -EINVAL;
946 }
947
948 field = f->fmt.pix.field;
949
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300950 if (field == V4L2_FIELD_ANY)
951 field = V4L2_FIELD_SEQ_TB;
952 else if (V4L2_FIELD_INTERLACED != field) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300953 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
954 return -EINVAL;
955 }
956
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300957 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300958
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300959 f->fmt.pix.width = dev->width;
960 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300961
962 f->fmt.pix.width &= ~0x01;
963
964 f->fmt.pix.field = field;
965
966 f->fmt.pix.bytesperline =
967 (f->fmt.pix.width * fmt->depth) >> 3;
968 f->fmt.pix.sizeimage =
969 f->fmt.pix.height * f->fmt.pix.bytesperline;
970
971 return 0;
972}
973
974/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300975static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300976 struct v4l2_format *f)
977{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300978 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300979 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300980 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300981 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300982 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300983
984 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
985 fh->width = f->fmt.pix.width;
986 fh->height = f->fmt.pix.height;
987 fh->vb_vidq.field = f->fmt.pix.field;
988 fh->type = f->type;
989
990 dev->fourcc = f->fmt.pix.pixelformat;
991
992 tm6000_set_fourcc_format(dev);
993
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300994 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300995}
996
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300997static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300998 struct v4l2_requestbuffers *p)
999{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001000 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001001
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001002 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001003}
1004
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001005static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001006 struct v4l2_buffer *p)
1007{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001008 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001009
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001010 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001011}
1012
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001013static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001014{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001015 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001016
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001017 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001018}
1019
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001020static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001021{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001022 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001023
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001024 return videobuf_dqbuf(&fh->vb_vidq, p,
1025 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001026}
1027
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001028static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1029{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001030 struct tm6000_fh *fh = priv;
1031 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001032
1033 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1034 return -EINVAL;
1035 if (i != fh->type)
1036 return -EINVAL;
1037
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001038 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001039 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001040 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001041}
1042
1043static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1044{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001045 struct tm6000_fh *fh = priv;
1046 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001047
1048 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1049 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001050
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001051 if (i != fh->type)
1052 return -EINVAL;
1053
1054 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001055 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001056
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001057 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001058}
1059
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001060static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001061{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001062 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001063 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001064 struct tm6000_core *dev = fh->dev;
1065
Stefan Ringel4f526102010-10-27 16:48:05 -03001066 dev->norm = *norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001067 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001068
1069 fh->width = dev->width;
1070 fh->height = dev->height;
1071
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001072 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001073 return rc;
1074
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001075 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001076
1077 return 0;
1078}
1079
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001080static const char *iname[] = {
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001081 [TM6000_INPUT_TV] = "Television",
1082 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1083 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1084 [TM6000_INPUT_SVIDEO] = "S-Video",
1085};
1086
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001087static int vidioc_enum_input(struct file *file, void *priv,
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001088 struct v4l2_input *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001089{
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001090 struct tm6000_fh *fh = priv;
1091 struct tm6000_core *dev = fh->dev;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001092 unsigned int n;
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001093
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001094 n = i->index;
1095 if (n >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001096 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001097
1098 if (!dev->vinput[n].type)
1099 return -EINVAL;
1100
1101 i->index = n;
1102
1103 if (dev->vinput[n].type == TM6000_INPUT_TV)
1104 i->type = V4L2_INPUT_TYPE_TUNER;
1105 else
1106 i->type = V4L2_INPUT_TYPE_CAMERA;
1107
1108 strcpy(i->name, iname[dev->vinput[n].type]);
1109
1110 i->std = TM6000_STD;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001111
1112 return 0;
1113}
1114
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001115static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001116{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001117 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001118 struct tm6000_core *dev = fh->dev;
1119
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001120 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001121
1122 return 0;
1123}
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001124
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001125static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001126{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001127 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001128 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001129 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001130
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001131 if (i >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001132 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001133 if (!dev->vinput[i].type)
1134 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001135
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001136 dev->input = i;
1137
1138 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001139
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001140 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001141}
1142
Stefan Ringel886a3c02011-05-09 16:53:50 -03001143/* --- controls ---------------------------------------------- */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001144static int vidioc_queryctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001145 struct v4l2_queryctrl *qc)
1146{
1147 int i;
1148
1149 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1150 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1151 memcpy(qc, &(tm6000_qctrl[i]),
1152 sizeof(*qc));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001153 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001154 }
1155
1156 return -EINVAL;
1157}
1158
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001159static int vidioc_g_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001160 struct v4l2_control *ctrl)
1161{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001162 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001163 struct tm6000_core *dev = fh->dev;
1164 int val;
1165
1166 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1167 switch (ctrl->id) {
1168 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001169 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001170 break;
1171 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001172 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001173 return 0;
1174 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001175 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001176 return 0;
1177 case V4L2_CID_HUE:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001178 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001179 return 0;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001180 case V4L2_CID_AUDIO_MUTE:
1181 val = dev->ctl_mute;
1182 return 0;
1183 case V4L2_CID_AUDIO_VOLUME:
1184 val = dev->ctl_volume;
1185 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001186 default:
1187 return -EINVAL;
1188 }
1189
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001190 if (val < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001191 return val;
1192
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001193 ctrl->value = val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001194
1195 return 0;
1196}
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001197static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001198 struct v4l2_control *ctrl)
1199{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001200 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001201 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001202 u8 val = ctrl->value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001203
1204 switch (ctrl->id) {
1205 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001206 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001207 return 0;
1208 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001209 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001210 return 0;
1211 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001212 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001213 return 0;
1214 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001215 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001216 return 0;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001217 case V4L2_CID_AUDIO_MUTE:
1218 dev->ctl_mute = val;
1219 tm6000_tvaudio_set_mute(dev, val);
1220 return 0;
1221 case V4L2_CID_AUDIO_VOLUME:
1222 dev->ctl_volume = val;
1223 tm6000_set_volume(dev, val);
1224 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001225 }
1226 return -EINVAL;
1227}
1228
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001229static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001230 struct v4l2_tuner *t)
1231{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001232 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001233 struct tm6000_core *dev = fh->dev;
1234
1235 if (unlikely(UNSET == dev->tuner_type))
1236 return -EINVAL;
1237 if (0 != t->index)
1238 return -EINVAL;
1239
1240 strcpy(t->name, "Television");
1241 t->type = V4L2_TUNER_ANALOG_TV;
1242 t->capability = V4L2_TUNER_CAP_NORM;
1243 t->rangehigh = 0xffffffffUL;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001244 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1245
1246 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1247
1248 t->audmode = dev->amode;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001249
1250 return 0;
1251}
1252
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001253static int vidioc_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001254 struct v4l2_tuner *t)
1255{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001256 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001257 struct tm6000_core *dev = fh->dev;
1258
1259 if (UNSET == dev->tuner_type)
1260 return -EINVAL;
1261 if (0 != t->index)
1262 return -EINVAL;
1263
Stefan Ringel886a3c02011-05-09 16:53:50 -03001264 dev->amode = t->audmode;
1265 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1266
1267 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001268
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001269 return 0;
1270}
1271
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001272static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001273 struct v4l2_frequency *f)
1274{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001275 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001276 struct tm6000_core *dev = fh->dev;
1277
1278 if (unlikely(UNSET == dev->tuner_type))
1279 return -EINVAL;
1280
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001281 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001282 f->frequency = dev->freq;
1283
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001284 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001285
1286 return 0;
1287}
1288
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001289static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001290 struct v4l2_frequency *f)
1291{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001292 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001293 struct tm6000_core *dev = fh->dev;
1294
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001295 if (unlikely(UNSET == dev->tuner_type))
1296 return -EINVAL;
1297 if (unlikely(f->tuner != 0))
1298 return -EINVAL;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001299 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1300 return -EINVAL;
1301 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1302 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001303
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001304 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001305 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001306
1307 return 0;
1308}
1309
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001310static int radio_querycap(struct file *file, void *priv,
1311 struct v4l2_capability *cap)
1312{
1313 struct tm6000_fh *fh = file->private_data;
1314 struct tm6000_core *dev = fh->dev;
1315
1316 strcpy(cap->driver, "tm6000");
1317 strlcpy(cap->card, dev->name, sizeof(dev->name));
1318 sprintf(cap->bus_info, "USB%04x:%04x",
1319 le16_to_cpu(dev->udev->descriptor.idVendor),
1320 le16_to_cpu(dev->udev->descriptor.idProduct));
1321 cap->version = dev->dev_type;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001322 cap->capabilities = V4L2_CAP_TUNER |
1323 V4L2_CAP_AUDIO |
1324 V4L2_CAP_RADIO |
1325 V4L2_CAP_READWRITE |
1326 V4L2_CAP_STREAMING;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001327
1328 return 0;
1329}
1330
1331static int radio_g_tuner(struct file *file, void *priv,
1332 struct v4l2_tuner *t)
1333{
1334 struct tm6000_fh *fh = file->private_data;
1335 struct tm6000_core *dev = fh->dev;
1336
1337 if (0 != t->index)
1338 return -EINVAL;
1339
1340 memset(t, 0, sizeof(*t));
1341 strcpy(t->name, "Radio");
1342 t->type = V4L2_TUNER_RADIO;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001343 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001344
1345 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1346
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001347 return 0;
1348}
1349
1350static int radio_s_tuner(struct file *file, void *priv,
1351 struct v4l2_tuner *t)
1352{
1353 struct tm6000_fh *fh = file->private_data;
1354 struct tm6000_core *dev = fh->dev;
1355
1356 if (0 != t->index)
1357 return -EINVAL;
1358
1359 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1360
1361 return 0;
1362}
1363
1364static int radio_enum_input(struct file *file, void *priv,
1365 struct v4l2_input *i)
1366{
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001367 struct tm6000_fh *fh = priv;
1368 struct tm6000_core *dev = fh->dev;
1369
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001370 if (i->index != 0)
1371 return -EINVAL;
1372
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001373 if (!dev->rinput.type)
1374 return -EINVAL;
1375
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001376 strcpy(i->name, "Radio");
1377 i->type = V4L2_INPUT_TYPE_TUNER;
1378
1379 return 0;
1380}
1381
1382static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
1383{
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001384 struct tm6000_fh *fh = priv;
1385 struct tm6000_core *dev = fh->dev;
1386
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001387 if (dev->input != 5)
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001388 return -EINVAL;
1389
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001390 *i = dev->input - 5;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001391
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001392 return 0;
1393}
1394
1395static int radio_g_audio(struct file *file, void *priv,
1396 struct v4l2_audio *a)
1397{
1398 memset(a, 0, sizeof(*a));
1399 strcpy(a->name, "Radio");
1400 return 0;
1401}
1402
1403static int radio_s_audio(struct file *file, void *priv,
Hans Verkuil0e8025b92012-09-04 11:59:31 -03001404 const struct v4l2_audio *a)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001405{
1406 return 0;
1407}
1408
1409static int radio_s_input(struct file *filp, void *priv, unsigned int i)
1410{
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001411 struct tm6000_fh *fh = priv;
1412 struct tm6000_core *dev = fh->dev;
1413
1414 if (i)
1415 return -EINVAL;
1416
1417 if (!dev->rinput.type)
1418 return -EINVAL;
1419
1420 dev->input = i + 5;
1421
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001422 return 0;
1423}
1424
1425static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
1426{
1427 return 0;
1428}
1429
1430static int radio_queryctrl(struct file *file, void *priv,
1431 struct v4l2_queryctrl *c)
1432{
1433 const struct v4l2_queryctrl *ctrl;
1434
1435 if (c->id < V4L2_CID_BASE ||
1436 c->id >= V4L2_CID_LASTP1)
1437 return -EINVAL;
1438 if (c->id == V4L2_CID_AUDIO_MUTE) {
1439 ctrl = ctrl_by_id(c->id);
1440 *c = *ctrl;
1441 } else
1442 *c = no_ctrl;
1443
1444 return 0;
1445}
1446
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001447/* ------------------------------------------------------------------
1448 File operations for the device
1449 ------------------------------------------------------------------*/
1450
Hans Verkuil14a09da2012-06-24 07:26:46 -03001451static int __tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001452{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001453 struct video_device *vdev = video_devdata(file);
1454 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001455 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001456 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001457 int i, rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001458 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001459
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001460 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1461 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001462
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001463 switch (vdev->vfl_type) {
1464 case VFL_TYPE_GRABBER:
1465 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1466 break;
1467 case VFL_TYPE_VBI:
1468 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1469 break;
1470 case VFL_TYPE_RADIO:
1471 radio = 1;
1472 break;
1473 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001474
1475 /* If more than one user, mutex should be added */
1476 dev->users++;
1477
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001478 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1479 video_device_node_name(vdev), v4l2_type_names[type],
1480 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001481
1482 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001483 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001484 if (NULL == fh) {
1485 dev->users--;
1486 return -ENOMEM;
1487 }
1488
1489 file->private_data = fh;
1490 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001491 fh->radio = radio;
1492 dev->radio = radio;
1493 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001494 dev->fourcc = format[0].fourcc;
1495
1496 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001497
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001498 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001499
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001500 fh->width = dev->width;
1501 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001502
1503 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1504 "dev->vidq=0x%08lx\n",
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001505 (unsigned long)fh, (unsigned long)dev,
1506 (unsigned long)&dev->vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001507 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001508 "queued=%d\n", list_empty(&dev->vidq.queued));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001509 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001510 "active=%d\n", list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001511
1512 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001513 rc = tm6000_init_analog_mode(dev);
1514 if (rc < 0)
1515 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001516
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001517 if (dev->mode != TM6000_MODE_ANALOG) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001518 /* Put all controls at a sane state */
1519 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001520 qctl_regs[i] = tm6000_qctrl[i].default_value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001521
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001522 dev->mode = TM6000_MODE_ANALOG;
1523 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001524
Thierry Redingaa4a5832011-08-04 04:14:15 -03001525 if (!fh->radio) {
1526 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1527 NULL, &dev->slock,
1528 fh->type,
1529 V4L2_FIELD_INTERLACED,
1530 sizeof(struct tm6000_buffer), fh, &dev->lock);
1531 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001532 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001533 dev->input = 5;
1534 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001535 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1536 tm6000_prepare_isoc(dev);
1537 tm6000_start_thread(dev);
1538 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001539
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001540 return 0;
1541}
1542
Hans Verkuil14a09da2012-06-24 07:26:46 -03001543static int tm6000_open(struct file *file)
1544{
1545 struct video_device *vdev = video_devdata(file);
1546 int res;
1547
1548 mutex_lock(vdev->lock);
1549 res = __tm6000_open(file);
1550 mutex_unlock(vdev->lock);
1551 return res;
1552}
1553
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001554static ssize_t
1555tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1556{
Hans Verkuil14a09da2012-06-24 07:26:46 -03001557 struct tm6000_fh *fh = file->private_data;
1558 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001559
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001560 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil14a09da2012-06-24 07:26:46 -03001561 int res;
1562
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001563 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001564 return -EBUSY;
1565
Hans Verkuil14a09da2012-06-24 07:26:46 -03001566 if (mutex_lock_interruptible(&dev->lock))
1567 return -ERESTARTSYS;
1568 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001569 file->f_flags & O_NONBLOCK);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001570 mutex_unlock(&dev->lock);
1571 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001572 }
1573 return 0;
1574}
1575
1576static unsigned int
Hans Verkuil14a09da2012-06-24 07:26:46 -03001577__tm6000_poll(struct file *file, struct poll_table_struct *wait)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001578{
1579 struct tm6000_fh *fh = file->private_data;
1580 struct tm6000_buffer *buf;
1581
1582 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1583 return POLLERR;
1584
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001585 if (!!is_res_streaming(fh->dev, fh))
1586 return POLLERR;
1587
1588 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001589 /* streaming capture */
1590 if (list_empty(&fh->vb_vidq.stream))
1591 return POLLERR;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001592 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001593 } else {
1594 /* read() capture */
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001595 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001596 }
1597 poll_wait(file, &buf->vb.done, wait);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -03001598 if (buf->vb.state == VIDEOBUF_DONE ||
1599 buf->vb.state == VIDEOBUF_ERROR)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001600 return POLLIN | POLLRDNORM;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001601 return 0;
1602}
1603
Hans Verkuil14a09da2012-06-24 07:26:46 -03001604static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1605{
1606 struct tm6000_fh *fh = file->private_data;
1607 struct tm6000_core *dev = fh->dev;
1608 unsigned int res;
1609
1610 mutex_lock(&dev->lock);
1611 res = __tm6000_poll(file, wait);
1612 mutex_unlock(&dev->lock);
1613 return res;
1614}
1615
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001616static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001617{
1618 struct tm6000_fh *fh = file->private_data;
1619 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001620 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001621
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001622 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1623 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001624
Hans Verkuil14a09da2012-06-24 07:26:46 -03001625 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001626 dev->users--;
1627
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001628 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001629
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001630 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001631 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001632
Stefan Ringel8159c182011-11-28 15:46:18 -03001633 /* Stop interrupt USB pipe */
1634 tm6000_ir_int_stop(dev);
1635
1636 usb_reset_configuration(dev->udev);
1637
Thierry Reding4be9c8f2011-12-06 09:39:35 -03001638 if (dev->int_in.endp)
Stefan Ringel8159c182011-11-28 15:46:18 -03001639 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001640 dev->isoc_in.bInterfaceNumber, 2);
Stefan Ringel8159c182011-11-28 15:46:18 -03001641 else
1642 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001643 dev->isoc_in.bInterfaceNumber, 0);
Stefan Ringel8159c182011-11-28 15:46:18 -03001644
1645 /* Start interrupt USB pipe */
1646 tm6000_ir_int_start(dev);
1647
Thierry Redingaa4a5832011-08-04 04:14:15 -03001648 if (!fh->radio)
1649 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001650 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001651
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001652 kfree(fh);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001653 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001654
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001655 return 0;
1656}
1657
1658static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1659{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001660 struct tm6000_fh *fh = file->private_data;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001661 struct tm6000_core *dev = fh->dev;
1662 int res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001663
Hans Verkuil14a09da2012-06-24 07:26:46 -03001664 if (mutex_lock_interruptible(&dev->lock))
1665 return -ERESTARTSYS;
1666 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1667 mutex_unlock(&dev->lock);
1668 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001669}
1670
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001671static struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001672 .owner = THIS_MODULE,
1673 .open = tm6000_open,
1674 .release = tm6000_release,
1675 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1676 .read = tm6000_read,
1677 .poll = tm6000_poll,
1678 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001679};
1680
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001681static const struct v4l2_ioctl_ops video_ioctl_ops = {
1682 .vidioc_querycap = vidioc_querycap,
1683 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1684 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1685 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1686 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1687 .vidioc_s_std = vidioc_s_std,
1688 .vidioc_enum_input = vidioc_enum_input,
1689 .vidioc_g_input = vidioc_g_input,
1690 .vidioc_s_input = vidioc_s_input,
1691 .vidioc_queryctrl = vidioc_queryctrl,
1692 .vidioc_g_ctrl = vidioc_g_ctrl,
1693 .vidioc_s_ctrl = vidioc_s_ctrl,
1694 .vidioc_g_tuner = vidioc_g_tuner,
1695 .vidioc_s_tuner = vidioc_s_tuner,
1696 .vidioc_g_frequency = vidioc_g_frequency,
1697 .vidioc_s_frequency = vidioc_s_frequency,
1698 .vidioc_streamon = vidioc_streamon,
1699 .vidioc_streamoff = vidioc_streamoff,
1700 .vidioc_reqbufs = vidioc_reqbufs,
1701 .vidioc_querybuf = vidioc_querybuf,
1702 .vidioc_qbuf = vidioc_qbuf,
1703 .vidioc_dqbuf = vidioc_dqbuf,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001704};
1705
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001706static struct video_device tm6000_template = {
1707 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001708 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001709 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001710 .release = video_device_release,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001711 .tvnorms = TM6000_STD,
1712 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001713};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001714
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001715static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001716 .owner = THIS_MODULE,
1717 .open = tm6000_open,
1718 .release = tm6000_release,
1719 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001720};
1721
1722static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1723 .vidioc_querycap = radio_querycap,
1724 .vidioc_g_tuner = radio_g_tuner,
1725 .vidioc_enum_input = radio_enum_input,
1726 .vidioc_g_audio = radio_g_audio,
1727 .vidioc_s_tuner = radio_s_tuner,
1728 .vidioc_s_audio = radio_s_audio,
1729 .vidioc_s_input = radio_s_input,
1730 .vidioc_s_std = radio_s_std,
1731 .vidioc_queryctrl = radio_queryctrl,
1732 .vidioc_g_input = radio_g_input,
1733 .vidioc_g_ctrl = vidioc_g_ctrl,
1734 .vidioc_s_ctrl = vidioc_s_ctrl,
1735 .vidioc_g_frequency = vidioc_g_frequency,
1736 .vidioc_s_frequency = vidioc_s_frequency,
1737};
1738
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001739static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001740 .name = "tm6000",
1741 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001742 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001743};
1744
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001745/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001746 * Initialization and module stuff
1747 * ------------------------------------------------------------------
1748 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001749
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001750static struct video_device *vdev_init(struct tm6000_core *dev,
1751 const struct video_device
1752 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001753{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001754 struct video_device *vfd;
1755
1756 vfd = video_device_alloc();
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001757 if (NULL == vfd)
1758 return NULL;
1759
1760 *vfd = *template;
1761 vfd->v4l2_dev = &dev->v4l2_dev;
1762 vfd->release = video_device_release;
1763 vfd->debug = tm6000_debug;
1764 vfd->lock = &dev->lock;
1765
1766 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1767
1768 video_set_drvdata(vfd, dev);
1769 return vfd;
1770}
1771
1772int tm6000_v4l2_register(struct tm6000_core *dev)
1773{
1774 int ret = -1;
1775
1776 dev->vfd = vdev_init(dev, &tm6000_template, "video");
1777
1778 if (!dev->vfd) {
1779 printk(KERN_INFO "%s: can't register video device\n",
1780 dev->name);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001781 return -ENOMEM;
1782 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001783
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001784 /* init video dma queues */
1785 INIT_LIST_HEAD(&dev->vidq.active);
1786 INIT_LIST_HEAD(&dev->vidq.queued);
1787
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001788 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001789
1790 if (ret < 0) {
1791 printk(KERN_INFO "%s: can't register video device\n",
1792 dev->name);
1793 return ret;
1794 }
1795
1796 printk(KERN_INFO "%s: registered device %s\n",
1797 dev->name, video_device_node_name(dev->vfd));
1798
Stefan Ringel14169102011-05-09 16:53:49 -03001799 if (dev->caps.has_radio) {
1800 dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
1801 "radio");
1802 if (!dev->radio_dev) {
1803 printk(KERN_INFO "%s: can't register radio device\n",
1804 dev->name);
Peter Senna Tschudin97b55f62012-09-06 11:23:49 -03001805 ret = -ENXIO;
Stefan Ringel14169102011-05-09 16:53:49 -03001806 return ret; /* FIXME release resource */
1807 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001808
Stefan Ringel14169102011-05-09 16:53:49 -03001809 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1810 radio_nr);
1811 if (ret < 0) {
1812 printk(KERN_INFO "%s: can't register radio device\n",
1813 dev->name);
1814 return ret; /* FIXME release resource */
1815 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001816
Stefan Ringel14169102011-05-09 16:53:49 -03001817 printk(KERN_INFO "%s: registered device %s\n",
1818 dev->name, video_device_node_name(dev->radio_dev));
1819 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001820
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001821 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001822 return ret;
1823}
1824
1825int tm6000_v4l2_unregister(struct tm6000_core *dev)
1826{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001827 video_unregister_device(dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001828
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001829 if (dev->radio_dev) {
1830 if (video_is_registered(dev->radio_dev))
1831 video_unregister_device(dev->radio_dev);
1832 else
1833 video_device_release(dev->radio_dev);
1834 dev->radio_dev = NULL;
1835 }
1836
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001837 return 0;
1838}
1839
1840int tm6000_v4l2_exit(void)
1841{
1842 return 0;
1843}
1844
1845module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001846MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001847
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001848module_param_named(debug, tm6000_debug, int, 0444);
1849MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001850
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001851module_param(vid_limit, int, 0644);
1852MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001853