blob: 4db3535a6b423411125c0333e0f0c81fa7d7180d [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 Chehab721f5072008-10-26 09:18:53 -0300172 char *outp;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300173
174 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300175 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300176 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300177 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300178 }
179
180 *buf = list_entry(dma_q->active.next,
181 struct tm6000_buffer, vb.queue);
182
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300183 /* Cleans up buffer - Useful for testing for frame/URB loss */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300184 outp = videobuf_to_vmalloc(&(*buf)->vb);
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300185
186 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300187}
188
189/*
190 * Announces that a buffer were filled and request the next
191 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300192static inline void buffer_filled(struct tm6000_core *dev,
193 struct tm6000_dmaqueue *dma_q,
194 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300195{
196 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300197 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300198 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300199 buf->vb.field_count++;
200 do_gettimeofday(&buf->vb.ts);
201
202 list_del(&buf->vb.queue);
203 wake_up(&buf->vb.done);
204}
205
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300206/*
207 * Identify the tm5600/6000 buffer header type and properly handles
208 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300209static int copy_streams(u8 *data, unsigned long len,
210 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300211{
212 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300213 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
214 u8 *ptr = data, *endp = data+len, c;
215 unsigned long header = 0;
216 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300217 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300218 struct tm6000_buffer *vbuf = NULL;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300219 char *voutp = NULL;
220 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300221
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300222 if (!dev->radio) {
223 /* get video buffer */
224 get_next_buf(dma_q, &vbuf);
225
226 if (!vbuf)
227 return rc;
228 voutp = videobuf_to_vmalloc(&vbuf->vb);
229
230 if (!voutp)
231 return 0;
232 }
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300233
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300234 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300235 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300236 /* Header */
237 if (dev->isoc_ctl.tmp_buf_len > 0) {
238 /* from last urb or packet */
239 header = dev->isoc_ctl.tmp_buf;
240 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300241 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300242 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300243 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300244 4 - dev->isoc_ctl.tmp_buf_len);
245 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300246 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300247 dev->isoc_ctl.tmp_buf_len = 0;
248 } else {
249 if (ptr + 3 >= endp) {
250 /* have incomplete header */
251 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300252 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300253 dev->isoc_ctl.tmp_buf_len);
254 return rc;
255 }
256 /* Seek for sync */
257 for (; ptr < endp - 3; ptr++) {
258 if (*(ptr + 3) == 0x47)
259 break;
260 }
261 /* Get message header */
262 header = *(unsigned long *)ptr;
263 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300264 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300265
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300266 /* split the header fields */
267 c = (header >> 24) & 0xff;
268 size = ((header & 0x7e) << 1);
269 if (size > 0)
270 size -= 4;
271 block = (header >> 7) & 0xf;
272 field = (header >> 11) & 0x1;
273 line = (header >> 12) & 0x1ff;
274 cmd = (header >> 21) & 0x7;
275 /* Validates haeder fields */
276 if (size > TM6000_URB_MSG_LEN)
277 size = TM6000_URB_MSG_LEN;
278 pktsize = TM6000_URB_MSG_LEN;
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300279 /*
280 * calculate position in buffer and change the buffer
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300281 */
282 switch (cmd) {
283 case TM6000_URB_MSG_VIDEO:
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300284 if (!dev->radio) {
285 if ((dev->isoc_ctl.vfield != field) &&
286 (field == 1)) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300287 /*
288 * Announces that a new buffer
289 * were filled
290 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300291 buffer_filled(dev, dma_q, vbuf);
292 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300293 "new buffer filled\n");
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300294 get_next_buf(dma_q, &vbuf);
295 if (!vbuf)
296 return rc;
297 voutp = videobuf_to_vmalloc(&vbuf->vb);
298 if (!voutp)
299 return rc;
300 memset(voutp, 0, vbuf->vb.size);
301 }
302 linewidth = vbuf->vb.width << 1;
303 pos = ((line << 1) - field - 1) *
304 linewidth + block * TM6000_URB_MSG_LEN;
305 /* Don't allow to write out of the buffer */
306 if (pos + size > vbuf->vb.size)
307 cmd = TM6000_URB_MSG_ERR;
308 dev->isoc_ctl.vfield = field;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300309 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300310 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300311 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300312 break;
313 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300314 case TM6000_URB_MSG_PTS:
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300315 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300316 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300317 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300318 } else {
319 /* Continue the last copy */
320 cmd = dev->isoc_ctl.cmd;
321 size = dev->isoc_ctl.size;
322 pos = dev->isoc_ctl.pos;
323 pktsize = dev->isoc_ctl.pktsize;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300324 field = dev->isoc_ctl.field;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300325 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300326 cpysize = (endp - ptr > size) ? size : endp - ptr;
327 if (cpysize) {
328 /* copy data in different buffers */
329 switch (cmd) {
330 case TM6000_URB_MSG_VIDEO:
331 /* Fills video buffer */
332 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300333 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300334 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300335 case TM6000_URB_MSG_AUDIO: {
336 int i;
337 for (i = 0; i < cpysize; i += 2)
338 swab16s((u16 *)(ptr + i));
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300339
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300340 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300341 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300342 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300343 case TM6000_URB_MSG_VBI:
344 /* Need some code to copy vbi buffer */
345 break;
Stefan Ringel2f349da2011-05-09 16:54:02 -0300346 case TM6000_URB_MSG_PTS: {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300347 /* Need some code to copy pts */
Stefan Ringel2f349da2011-05-09 16:54:02 -0300348 u32 pts;
349 pts = *(u32 *)ptr;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300350 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
351 field, pts);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300352 break;
353 }
Stefan Ringel2f349da2011-05-09 16:54:02 -0300354 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300355 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300356 if (ptr + pktsize > endp) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300357 /*
358 * End of URB packet, but cmd processing is not
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300359 * complete. Preserve the state for a next packet
360 */
361 dev->isoc_ctl.pos = pos + cpysize;
362 dev->isoc_ctl.size = size - cpysize;
363 dev->isoc_ctl.cmd = cmd;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300364 dev->isoc_ctl.field = field;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300365 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300366 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300367 } else {
368 dev->isoc_ctl.cmd = 0;
369 ptr += pktsize;
370 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300371 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300372 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300373}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300374
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300375/*
376 * Identify the tm5600/6000 buffer header type and properly handles
377 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300378static int copy_multiplexed(u8 *ptr, unsigned long len,
379 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300380{
381 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300382 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
383 unsigned int pos = dev->isoc_ctl.pos, cpysize;
384 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300385 struct tm6000_buffer *buf;
386 char *outp = NULL;
387
388 get_next_buf(dma_q, &buf);
389 if (buf)
390 outp = videobuf_to_vmalloc(&buf->vb);
391
392 if (!outp)
393 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300394
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300395 while (len > 0) {
396 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300397 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300398 pos += cpysize;
399 ptr += cpysize;
400 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300401 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300402 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300403 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300404 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300405 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300406 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300407 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300408 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300409 outp = videobuf_to_vmalloc(&(buf->vb));
410 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300411 return rc;
412 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300413 }
414 }
415
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300416 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300417 return rc;
418}
419
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300420static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300421 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300422{
423 char *errmsg = "Unknown";
424
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300425 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300426 case -ENOENT:
427 errmsg = "unlinked synchronuously";
428 break;
429 case -ECONNRESET:
430 errmsg = "unlinked asynchronuously";
431 break;
432 case -ENOSR:
433 errmsg = "Buffer error (overrun)";
434 break;
435 case -EPIPE:
436 errmsg = "Stalled (device not responding)";
437 break;
438 case -EOVERFLOW:
439 errmsg = "Babble (bad cable?)";
440 break;
441 case -EPROTO:
442 errmsg = "Bit-stuff error (bad cable?)";
443 break;
444 case -EILSEQ:
445 errmsg = "CRC/Timeout (could be anything)";
446 break;
447 case -ETIME:
448 errmsg = "Device does not respond";
449 break;
450 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300451 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300452 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
453 status, errmsg);
454 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300455 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300456 packet, status, errmsg);
457 }
458}
459
460
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300461/*
462 * Controls the isoc copy of each urb packet
463 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300464static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300465{
466 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300467 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
468 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300469 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300470
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300471 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300472 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300473 return 0;
474 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300475
476 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300477 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300478
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300479 if (status < 0) {
480 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300481 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300482 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300483
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300484 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300485
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300486 if (len > 0) {
487 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300488 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300489 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
490 rc = copy_multiplexed(p, len, urb);
491 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300492 return rc;
493 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300494 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300495 }
496 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300497 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300498 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300499 return rc;
500}
501
502/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300503 * URB control
504 * ------------------------------------------------------------------
505 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300506
507/*
508 * IRQ callback, called by URB callback
509 */
510static void tm6000_irq_callback(struct urb *urb)
511{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300512 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300513 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300514 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300515
Thierry Reding14f09152011-08-04 04:14:06 -0300516 switch (urb->status) {
517 case 0:
518 case -ETIMEDOUT:
519 break;
520
521 case -ECONNRESET:
522 case -ENOENT:
523 case -ESHUTDOWN:
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300524 return;
525
Thierry Reding14f09152011-08-04 04:14:06 -0300526 default:
527 tm6000_err("urb completion error %d.\n", urb->status);
528 break;
529 }
530
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300531 spin_lock(&dev->slock);
532 tm6000_isoc_copy(urb);
533 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300534
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300535 /* Reset urb buffers */
536 for (i = 0; i < urb->number_of_packets; i++) {
537 urb->iso_frame_desc[i].status = 0;
538 urb->iso_frame_desc[i].actual_length = 0;
539 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300540
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300541 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
542 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300543 tm6000_err("urb resubmit failed (error=%i)\n",
544 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300545}
546
547/*
548 * Stop and Deallocate URBs
549 */
550static void tm6000_uninit_isoc(struct tm6000_core *dev)
551{
552 struct urb *urb;
553 int i;
554
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300555 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300556 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300557 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300558 if (urb) {
559 usb_kill_urb(urb);
560 usb_unlink_urb(urb);
561 if (dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300562 usb_free_coherent(dev->udev,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300563 urb->transfer_buffer_length,
564 dev->isoc_ctl.transfer_buffer[i],
565 urb->transfer_dma);
566 }
567 usb_free_urb(urb);
568 dev->isoc_ctl.urb[i] = NULL;
569 }
570 dev->isoc_ctl.transfer_buffer[i] = NULL;
571 }
572
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300573 kfree(dev->isoc_ctl.urb);
574 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300575
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300576 dev->isoc_ctl.urb = NULL;
577 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300578 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300579}
580
581/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300582 * Allocate URBs and start IRQ
583 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300584static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300585{
586 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Stefan Ringel5a4b55e2010-05-19 13:58:27 -0300587 int i, j, sb_size, pipe, size, max_packets, num_bufs = 8;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300588 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300589
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300590 /* De-allocates all pending stuff */
591 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300592 /* Stop interrupt USB pipe */
593 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300594
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300595 usb_set_interface(dev->udev,
596 dev->isoc_in.bInterfaceNumber,
597 dev->isoc_in.bAlternateSetting);
598
Dmitri Belimov641d2112010-12-22 05:57:46 -0300599 /* Start interrupt USB pipe */
600 tm6000_ir_int_start(dev);
601
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300602 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300603 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300604 USB_ENDPOINT_NUMBER_MASK);
605
606 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
607
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300608 if (size > dev->isoc_in.maxsize)
609 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300610
611 dev->isoc_ctl.max_pkt_size = size;
612
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300613 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300614 sb_size = max_packets * size;
615
616 dev->isoc_ctl.num_bufs = num_bufs;
617
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300618 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300619 if (!dev->isoc_ctl.urb) {
620 tm6000_err("cannot alloc memory for usb buffers\n");
621 return -ENOMEM;
622 }
623
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300624 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300625 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300626 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300627 tm6000_err("cannot allocate memory for usbtransfer\n");
628 kfree(dev->isoc_ctl.urb);
629 return -ENOMEM;
630 }
631
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300632 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
633 " (%d bytes) of %d bytes each to handle %u size\n",
634 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300635 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300636
637 /* allocate urbs and transfer buffers */
638 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
639 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
640 if (!urb) {
641 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
642 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehabc1a16412007-10-15 15:43:50 -0300643 usb_free_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300644 return -ENOMEM;
645 }
646 dev->isoc_ctl.urb[i] = urb;
647
Mauro Carvalho Chehab5a11b6f2010-05-03 15:17:57 -0300648 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev,
Mauro Carvalho Chehabc13dd702007-09-19 07:36:34 -0300649 sb_size, GFP_KERNEL, &urb->transfer_dma);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300650 if (!dev->isoc_ctl.transfer_buffer[i]) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300651 tm6000_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300652 " buffer %i%s\n",
653 sb_size, i,
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300654 in_interrupt() ? " while in int" : "");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300655 tm6000_uninit_isoc(dev);
656 return -ENOMEM;
657 }
658 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
659
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300660 usb_fill_bulk_urb(urb, dev->udev, pipe,
661 dev->isoc_ctl.transfer_buffer[i], sb_size,
662 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300663 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300664 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300665 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300666
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300667 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300668 urb->iso_frame_desc[j].offset = size * j;
669 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300670 }
671 }
672
673 return 0;
674}
675
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300676static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300677{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300678 struct tm6000_dmaqueue *dma_q = &dev->vidq;
679 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300680
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300681 dma_q->frame = 0;
682 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300683
684 init_waitqueue_head(&dma_q->wq);
685
686 /* submit urbs and enables IRQ */
687 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300688 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300689 if (rc) {
690 tm6000_err("submit of urb %i failed (error=%i)\n", i,
691 rc);
692 tm6000_uninit_isoc(dev);
693 return rc;
694 }
695 }
696
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300697 return 0;
698}
699
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300700/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300701 * Videobuf operations
702 * ------------------------------------------------------------------
703 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300704
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300705static int
706buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
707{
708 struct tm6000_fh *fh = vq->priv_data;
709
710 *size = fh->fmt->depth * fh->width * fh->height >> 3;
711 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300712 *count = TM6000_DEF_BUF;
713
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300714 if (*count < TM6000_MIN_BUF)
715 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300716
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300717 while (*size * *count > vid_limit * 1024 * 1024)
718 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300719
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300720 return 0;
721}
722
723static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
724{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300725 struct tm6000_fh *fh = vq->priv_data;
726 struct tm6000_core *dev = fh->dev;
727 unsigned long flags;
728
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300729 if (in_interrupt())
730 BUG();
731
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300732 /* We used to wait for the buffer to finish here, but this didn't work
733 because, as we were keeping the state as VIDEOBUF_QUEUED,
734 videobuf_queue_cancel marked it as finished for us.
735 (Also, it could wedge forever if the hardware was misconfigured.)
736
737 This should be safe; by the time we get here, the buffer isn't
738 queued anymore. If we ever start marking the buffers as
739 VIDEOBUF_ACTIVE, it won't be, though.
740 */
741 spin_lock_irqsave(&dev->slock, flags);
742 if (dev->isoc_ctl.buf == buf)
743 dev->isoc_ctl.buf = NULL;
744 spin_unlock_irqrestore(&dev->slock, flags);
745
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300746 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300747 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300748}
749
750static int
751buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
752 enum v4l2_field field)
753{
754 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300755 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300756 struct tm6000_core *dev = fh->dev;
Thierry Reding88e834a2011-08-04 04:14:12 -0300757 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300758
759 BUG_ON(NULL == fh->fmt);
760
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300761
762 /* FIXME: It assumes depth=2 */
763 /* The only currently supported format is 16 bits/pixel */
764 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
765 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
766 return -EINVAL;
767
768 if (buf->fmt != fh->fmt ||
769 buf->vb.width != fh->width ||
770 buf->vb.height != fh->height ||
771 buf->vb.field != field) {
772 buf->fmt = fh->fmt;
773 buf->vb.width = fh->width;
774 buf->vb.height = fh->height;
775 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300776 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300777 }
778
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300779 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Curtis McEnroeed7c2212011-06-01 22:21:56 -0400780 rc = videobuf_iolock(vq, &buf->vb, NULL);
781 if (rc != 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300782 goto fail;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300783 }
784
Thierry Reding88e834a2011-08-04 04:14:12 -0300785 if (!dev->isoc_ctl.num_bufs) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300786 rc = tm6000_prepare_isoc(dev);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300787 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300788 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300789
790 rc = tm6000_start_thread(dev);
791 if (rc < 0)
792 goto fail;
793
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300794 }
795
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300796 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300797 return 0;
798
799fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300800 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300801 return rc;
802}
803
804static void
805buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
806{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300807 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300808 struct tm6000_fh *fh = vq->priv_data;
809 struct tm6000_core *dev = fh->dev;
810 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300811
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300812 buf->vb.state = VIDEOBUF_QUEUED;
813 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300814}
815
816static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
817{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300818 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300819
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300820 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300821}
822
823static struct videobuf_queue_ops tm6000_video_qops = {
824 .buf_setup = buffer_setup,
825 .buf_prepare = buffer_prepare,
826 .buf_queue = buffer_queue,
827 .buf_release = buffer_release,
828};
829
830/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300831 * IOCTL handling
832 * ------------------------------------------------------------------
833 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300834
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300835static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300836{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300837 /* Is the current fh handling it? if so, that's OK */
838 if (dev->resources == fh && dev->is_res_read)
839 return true;
840
841 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300842}
843
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300844static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300845{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300846 /* Is the current fh handling it? if so, that's OK */
847 if (dev->resources == fh)
848 return true;
849
850 return false;
851}
852
853static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
854 bool is_res_read)
855{
856 /* Is the current fh handling it? if so, that's OK */
857 if (dev->resources == fh && dev->is_res_read == is_res_read)
858 return true;
859
860 /* is it free? */
861 if (dev->resources)
862 return false;
863
864 /* grab it */
865 dev->resources = fh;
866 dev->is_res_read = is_res_read;
867 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
868 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300869}
870
871static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
872{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300873 /* Is the current fh handling it? if so, that's OK */
874 if (dev->resources != fh)
875 return;
876
877 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300878 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300879}
880
881/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300882 * IOCTL vidioc handling
883 * ------------------------------------------------------------------
884 */
885static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300886 struct v4l2_capability *cap)
887{
Stefan Ringel886a3c02011-05-09 16:53:50 -0300888 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300889
890 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300891 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300892 cap->version = TM6000_VERSION;
893 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
894 V4L2_CAP_STREAMING |
Stefan Ringel886a3c02011-05-09 16:53:50 -0300895 V4L2_CAP_AUDIO |
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300896 V4L2_CAP_READWRITE;
Stefan Ringel886a3c02011-05-09 16:53:50 -0300897
898 if (dev->tuner_type != TUNER_ABSENT)
899 cap->capabilities |= V4L2_CAP_TUNER;
900
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300901 return 0;
902}
903
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300904static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300905 struct v4l2_fmtdesc *f)
906{
907 if (unlikely(f->index >= ARRAY_SIZE(format)))
908 return -EINVAL;
909
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300910 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300911 f->pixelformat = format[f->index].fourcc;
912 return 0;
913}
914
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300915static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300916 struct v4l2_format *f)
917{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300918 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300919
920 f->fmt.pix.width = fh->width;
921 f->fmt.pix.height = fh->height;
922 f->fmt.pix.field = fh->vb_vidq.field;
923 f->fmt.pix.pixelformat = fh->fmt->fourcc;
924 f->fmt.pix.bytesperline =
925 (f->fmt.pix.width * fh->fmt->depth) >> 3;
926 f->fmt.pix.sizeimage =
927 f->fmt.pix.height * f->fmt.pix.bytesperline;
928
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300929 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300930}
931
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300932static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300933{
934 unsigned int i;
935
936 for (i = 0; i < ARRAY_SIZE(format); i++)
937 if (format[i].fourcc == fourcc)
938 return format+i;
939 return NULL;
940}
941
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300942static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300943 struct v4l2_format *f)
944{
945 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
946 struct tm6000_fmt *fmt;
947 enum v4l2_field field;
948
949 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
950 if (NULL == fmt) {
951 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
952 " invalid.\n", f->fmt.pix.pixelformat);
953 return -EINVAL;
954 }
955
956 field = f->fmt.pix.field;
957
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300958 if (field == V4L2_FIELD_ANY)
959 field = V4L2_FIELD_SEQ_TB;
960 else if (V4L2_FIELD_INTERLACED != field) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300961 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
962 return -EINVAL;
963 }
964
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300965 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300966
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300967 f->fmt.pix.width = dev->width;
968 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300969
970 f->fmt.pix.width &= ~0x01;
971
972 f->fmt.pix.field = field;
973
974 f->fmt.pix.bytesperline =
975 (f->fmt.pix.width * fmt->depth) >> 3;
976 f->fmt.pix.sizeimage =
977 f->fmt.pix.height * f->fmt.pix.bytesperline;
978
979 return 0;
980}
981
982/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300983static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300984 struct v4l2_format *f)
985{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300986 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300987 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300988 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300989 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300990 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300991
992 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
993 fh->width = f->fmt.pix.width;
994 fh->height = f->fmt.pix.height;
995 fh->vb_vidq.field = f->fmt.pix.field;
996 fh->type = f->type;
997
998 dev->fourcc = f->fmt.pix.pixelformat;
999
1000 tm6000_set_fourcc_format(dev);
1001
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001002 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001003}
1004
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001005static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001006 struct v4l2_requestbuffers *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_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001011}
1012
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001013static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001014 struct v4l2_buffer *p)
1015{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001016 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001017
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001018 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001019}
1020
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001021static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001022{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001023 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001024
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001025 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001026}
1027
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001028static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001029{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001030 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001031
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001032 return videobuf_dqbuf(&fh->vb_vidq, p,
1033 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001034}
1035
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001036static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1037{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001038 struct tm6000_fh *fh = priv;
1039 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001040
1041 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1042 return -EINVAL;
1043 if (i != fh->type)
1044 return -EINVAL;
1045
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001046 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001047 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001048 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001049}
1050
1051static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1052{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001053 struct tm6000_fh *fh = priv;
1054 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001055
1056 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1057 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001058
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001059 if (i != fh->type)
1060 return -EINVAL;
1061
1062 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001063 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001064
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001065 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001066}
1067
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001068static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001069{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001070 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001071 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001072 struct tm6000_core *dev = fh->dev;
1073
Stefan Ringel4f526102010-10-27 16:48:05 -03001074 dev->norm = *norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001075 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001076
1077 fh->width = dev->width;
1078 fh->height = dev->height;
1079
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001080 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001081 return rc;
1082
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001083 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001084
1085 return 0;
1086}
1087
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001088static const char *iname[] = {
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001089 [TM6000_INPUT_TV] = "Television",
1090 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1091 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1092 [TM6000_INPUT_SVIDEO] = "S-Video",
1093};
1094
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001095static int vidioc_enum_input(struct file *file, void *priv,
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001096 struct v4l2_input *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001097{
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001098 struct tm6000_fh *fh = priv;
1099 struct tm6000_core *dev = fh->dev;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001100 unsigned int n;
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001101
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001102 n = i->index;
1103 if (n >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001104 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001105
1106 if (!dev->vinput[n].type)
1107 return -EINVAL;
1108
1109 i->index = n;
1110
1111 if (dev->vinput[n].type == TM6000_INPUT_TV)
1112 i->type = V4L2_INPUT_TYPE_TUNER;
1113 else
1114 i->type = V4L2_INPUT_TYPE_CAMERA;
1115
1116 strcpy(i->name, iname[dev->vinput[n].type]);
1117
1118 i->std = TM6000_STD;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001119
1120 return 0;
1121}
1122
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001123static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001124{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001125 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001126 struct tm6000_core *dev = fh->dev;
1127
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001128 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001129
1130 return 0;
1131}
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001132
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001133static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001134{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001135 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001136 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001137 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001138
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001139 if (i >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001140 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001141 if (!dev->vinput[i].type)
1142 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001143
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001144 dev->input = i;
1145
1146 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001147
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001148 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001149}
1150
Stefan Ringel886a3c02011-05-09 16:53:50 -03001151/* --- controls ---------------------------------------------- */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001152static int vidioc_queryctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001153 struct v4l2_queryctrl *qc)
1154{
1155 int i;
1156
1157 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1158 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1159 memcpy(qc, &(tm6000_qctrl[i]),
1160 sizeof(*qc));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001161 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001162 }
1163
1164 return -EINVAL;
1165}
1166
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001167static int vidioc_g_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001168 struct v4l2_control *ctrl)
1169{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001170 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001171 struct tm6000_core *dev = fh->dev;
1172 int val;
1173
1174 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1175 switch (ctrl->id) {
1176 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001177 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001178 break;
1179 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001180 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001181 return 0;
1182 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001183 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001184 return 0;
1185 case V4L2_CID_HUE:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001186 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001187 return 0;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001188 case V4L2_CID_AUDIO_MUTE:
1189 val = dev->ctl_mute;
1190 return 0;
1191 case V4L2_CID_AUDIO_VOLUME:
1192 val = dev->ctl_volume;
1193 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001194 default:
1195 return -EINVAL;
1196 }
1197
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001198 if (val < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001199 return val;
1200
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001201 ctrl->value = val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001202
1203 return 0;
1204}
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001205static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001206 struct v4l2_control *ctrl)
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;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001210 u8 val = ctrl->value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001211
1212 switch (ctrl->id) {
1213 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001214 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001215 return 0;
1216 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001217 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001218 return 0;
1219 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001220 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001221 return 0;
1222 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001223 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001224 return 0;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001225 case V4L2_CID_AUDIO_MUTE:
1226 dev->ctl_mute = val;
1227 tm6000_tvaudio_set_mute(dev, val);
1228 return 0;
1229 case V4L2_CID_AUDIO_VOLUME:
1230 dev->ctl_volume = val;
1231 tm6000_set_volume(dev, val);
1232 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001233 }
1234 return -EINVAL;
1235}
1236
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001237static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001238 struct v4l2_tuner *t)
1239{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001240 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001241 struct tm6000_core *dev = fh->dev;
1242
1243 if (unlikely(UNSET == dev->tuner_type))
1244 return -EINVAL;
1245 if (0 != t->index)
1246 return -EINVAL;
1247
1248 strcpy(t->name, "Television");
1249 t->type = V4L2_TUNER_ANALOG_TV;
1250 t->capability = V4L2_TUNER_CAP_NORM;
1251 t->rangehigh = 0xffffffffUL;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001252 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1253
1254 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1255
1256 t->audmode = dev->amode;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001257
1258 return 0;
1259}
1260
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001261static int vidioc_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001262 struct v4l2_tuner *t)
1263{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001264 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001265 struct tm6000_core *dev = fh->dev;
1266
1267 if (UNSET == dev->tuner_type)
1268 return -EINVAL;
1269 if (0 != t->index)
1270 return -EINVAL;
1271
Stefan Ringel886a3c02011-05-09 16:53:50 -03001272 dev->amode = t->audmode;
1273 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1274
1275 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001276
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001277 return 0;
1278}
1279
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001280static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001281 struct v4l2_frequency *f)
1282{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001283 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001284 struct tm6000_core *dev = fh->dev;
1285
1286 if (unlikely(UNSET == dev->tuner_type))
1287 return -EINVAL;
1288
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001289 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001290 f->frequency = dev->freq;
1291
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001292 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001293
1294 return 0;
1295}
1296
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001297static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001298 struct v4l2_frequency *f)
1299{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001300 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001301 struct tm6000_core *dev = fh->dev;
1302
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001303 if (unlikely(UNSET == dev->tuner_type))
1304 return -EINVAL;
1305 if (unlikely(f->tuner != 0))
1306 return -EINVAL;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001307 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1308 return -EINVAL;
1309 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1310 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001311
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001312 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001313 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001314
1315 return 0;
1316}
1317
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001318static int radio_querycap(struct file *file, void *priv,
1319 struct v4l2_capability *cap)
1320{
1321 struct tm6000_fh *fh = file->private_data;
1322 struct tm6000_core *dev = fh->dev;
1323
1324 strcpy(cap->driver, "tm6000");
1325 strlcpy(cap->card, dev->name, sizeof(dev->name));
1326 sprintf(cap->bus_info, "USB%04x:%04x",
1327 le16_to_cpu(dev->udev->descriptor.idVendor),
1328 le16_to_cpu(dev->udev->descriptor.idProduct));
1329 cap->version = dev->dev_type;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001330 cap->capabilities = V4L2_CAP_TUNER |
1331 V4L2_CAP_AUDIO |
1332 V4L2_CAP_RADIO |
1333 V4L2_CAP_READWRITE |
1334 V4L2_CAP_STREAMING;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001335
1336 return 0;
1337}
1338
1339static int radio_g_tuner(struct file *file, void *priv,
1340 struct v4l2_tuner *t)
1341{
1342 struct tm6000_fh *fh = file->private_data;
1343 struct tm6000_core *dev = fh->dev;
1344
1345 if (0 != t->index)
1346 return -EINVAL;
1347
1348 memset(t, 0, sizeof(*t));
1349 strcpy(t->name, "Radio");
1350 t->type = V4L2_TUNER_RADIO;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001351 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001352
1353 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1354
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001355 return 0;
1356}
1357
1358static int radio_s_tuner(struct file *file, void *priv,
1359 struct v4l2_tuner *t)
1360{
1361 struct tm6000_fh *fh = file->private_data;
1362 struct tm6000_core *dev = fh->dev;
1363
1364 if (0 != t->index)
1365 return -EINVAL;
1366
1367 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1368
1369 return 0;
1370}
1371
1372static int radio_enum_input(struct file *file, void *priv,
1373 struct v4l2_input *i)
1374{
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001375 struct tm6000_fh *fh = priv;
1376 struct tm6000_core *dev = fh->dev;
1377
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001378 if (i->index != 0)
1379 return -EINVAL;
1380
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001381 if (!dev->rinput.type)
1382 return -EINVAL;
1383
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001384 strcpy(i->name, "Radio");
1385 i->type = V4L2_INPUT_TYPE_TUNER;
1386
1387 return 0;
1388}
1389
1390static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
1391{
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001392 struct tm6000_fh *fh = priv;
1393 struct tm6000_core *dev = fh->dev;
1394
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001395 if (dev->input != 5)
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001396 return -EINVAL;
1397
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001398 *i = dev->input - 5;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001399
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001400 return 0;
1401}
1402
1403static int radio_g_audio(struct file *file, void *priv,
1404 struct v4l2_audio *a)
1405{
1406 memset(a, 0, sizeof(*a));
1407 strcpy(a->name, "Radio");
1408 return 0;
1409}
1410
1411static int radio_s_audio(struct file *file, void *priv,
1412 struct v4l2_audio *a)
1413{
1414 return 0;
1415}
1416
1417static int radio_s_input(struct file *filp, void *priv, unsigned int i)
1418{
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001419 struct tm6000_fh *fh = priv;
1420 struct tm6000_core *dev = fh->dev;
1421
1422 if (i)
1423 return -EINVAL;
1424
1425 if (!dev->rinput.type)
1426 return -EINVAL;
1427
1428 dev->input = i + 5;
1429
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001430 return 0;
1431}
1432
1433static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
1434{
1435 return 0;
1436}
1437
1438static int radio_queryctrl(struct file *file, void *priv,
1439 struct v4l2_queryctrl *c)
1440{
1441 const struct v4l2_queryctrl *ctrl;
1442
1443 if (c->id < V4L2_CID_BASE ||
1444 c->id >= V4L2_CID_LASTP1)
1445 return -EINVAL;
1446 if (c->id == V4L2_CID_AUDIO_MUTE) {
1447 ctrl = ctrl_by_id(c->id);
1448 *c = *ctrl;
1449 } else
1450 *c = no_ctrl;
1451
1452 return 0;
1453}
1454
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001455/* ------------------------------------------------------------------
1456 File operations for the device
1457 ------------------------------------------------------------------*/
1458
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001459static int tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001460{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001461 struct video_device *vdev = video_devdata(file);
1462 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001463 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001464 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001465 int i, rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001466 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001467
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001468 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1469 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001470
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001471 switch (vdev->vfl_type) {
1472 case VFL_TYPE_GRABBER:
1473 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1474 break;
1475 case VFL_TYPE_VBI:
1476 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1477 break;
1478 case VFL_TYPE_RADIO:
1479 radio = 1;
1480 break;
1481 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001482
1483 /* If more than one user, mutex should be added */
1484 dev->users++;
1485
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001486 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1487 video_device_node_name(vdev), v4l2_type_names[type],
1488 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001489
1490 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001491 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001492 if (NULL == fh) {
1493 dev->users--;
1494 return -ENOMEM;
1495 }
1496
1497 file->private_data = fh;
1498 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001499 fh->radio = radio;
1500 dev->radio = radio;
1501 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001502 dev->fourcc = format[0].fourcc;
1503
1504 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001505
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001506 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001507
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001508 fh->width = dev->width;
1509 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001510
1511 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1512 "dev->vidq=0x%08lx\n",
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001513 (unsigned long)fh, (unsigned long)dev,
1514 (unsigned long)&dev->vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001515 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001516 "queued=%d\n", list_empty(&dev->vidq.queued));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001517 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001518 "active=%d\n", list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001519
1520 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001521 rc = tm6000_init_analog_mode(dev);
1522 if (rc < 0)
1523 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001524
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001525 if (dev->mode != TM6000_MODE_ANALOG) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001526 /* Put all controls at a sane state */
1527 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001528 qctl_regs[i] = tm6000_qctrl[i].default_value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001529
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001530 dev->mode = TM6000_MODE_ANALOG;
1531 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001532
Thierry Redingaa4a5832011-08-04 04:14:15 -03001533 if (!fh->radio) {
1534 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1535 NULL, &dev->slock,
1536 fh->type,
1537 V4L2_FIELD_INTERLACED,
1538 sizeof(struct tm6000_buffer), fh, &dev->lock);
1539 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001540 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001541 dev->input = 5;
1542 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001543 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1544 tm6000_prepare_isoc(dev);
1545 tm6000_start_thread(dev);
1546 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001547
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001548 return 0;
1549}
1550
1551static ssize_t
1552tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1553{
1554 struct tm6000_fh *fh = file->private_data;
1555
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001556 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001557 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001558 return -EBUSY;
1559
1560 return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
1561 file->f_flags & O_NONBLOCK);
1562 }
1563 return 0;
1564}
1565
1566static unsigned int
1567tm6000_poll(struct file *file, struct poll_table_struct *wait)
1568{
1569 struct tm6000_fh *fh = file->private_data;
1570 struct tm6000_buffer *buf;
1571
1572 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1573 return POLLERR;
1574
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001575 if (!!is_res_streaming(fh->dev, fh))
1576 return POLLERR;
1577
1578 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001579 /* streaming capture */
1580 if (list_empty(&fh->vb_vidq.stream))
1581 return POLLERR;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001582 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001583 } else {
1584 /* read() capture */
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001585 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001586 }
1587 poll_wait(file, &buf->vb.done, wait);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -03001588 if (buf->vb.state == VIDEOBUF_DONE ||
1589 buf->vb.state == VIDEOBUF_ERROR)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001590 return POLLIN | POLLRDNORM;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001591 return 0;
1592}
1593
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001594static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001595{
1596 struct tm6000_fh *fh = file->private_data;
1597 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001598 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001599
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001600 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1601 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001602
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001603 dev->users--;
1604
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001605 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001606
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001607 if (!dev->users) {
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001608 int err;
1609
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001610 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001611
Stefan Ringel8159c182011-11-28 15:46:18 -03001612 /* Stop interrupt USB pipe */
1613 tm6000_ir_int_stop(dev);
1614
1615 usb_reset_configuration(dev->udev);
1616
1617 if (&dev->int_in)
1618 usb_set_interface(dev->udev,
1619 dev->isoc_in.bInterfaceNumber,
1620 2);
1621 else
1622 usb_set_interface(dev->udev,
1623 dev->isoc_in.bInterfaceNumber,
1624 0);
1625
1626 /* Start interrupt USB pipe */
1627 tm6000_ir_int_start(dev);
1628
Thierry Redingaa4a5832011-08-04 04:14:15 -03001629 if (!fh->radio)
1630 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001631 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001632
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001633 kfree(fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001634
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001635 return 0;
1636}
1637
1638static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1639{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001640 struct tm6000_fh *fh = file->private_data;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001641
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001642 return videobuf_mmap_mapper(&fh->vb_vidq, vma);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001643}
1644
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001645static struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001646 .owner = THIS_MODULE,
1647 .open = tm6000_open,
1648 .release = tm6000_release,
1649 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1650 .read = tm6000_read,
1651 .poll = tm6000_poll,
1652 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001653};
1654
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001655static const struct v4l2_ioctl_ops video_ioctl_ops = {
1656 .vidioc_querycap = vidioc_querycap,
1657 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1658 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1659 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1660 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1661 .vidioc_s_std = vidioc_s_std,
1662 .vidioc_enum_input = vidioc_enum_input,
1663 .vidioc_g_input = vidioc_g_input,
1664 .vidioc_s_input = vidioc_s_input,
1665 .vidioc_queryctrl = vidioc_queryctrl,
1666 .vidioc_g_ctrl = vidioc_g_ctrl,
1667 .vidioc_s_ctrl = vidioc_s_ctrl,
1668 .vidioc_g_tuner = vidioc_g_tuner,
1669 .vidioc_s_tuner = vidioc_s_tuner,
1670 .vidioc_g_frequency = vidioc_g_frequency,
1671 .vidioc_s_frequency = vidioc_s_frequency,
1672 .vidioc_streamon = vidioc_streamon,
1673 .vidioc_streamoff = vidioc_streamoff,
1674 .vidioc_reqbufs = vidioc_reqbufs,
1675 .vidioc_querybuf = vidioc_querybuf,
1676 .vidioc_qbuf = vidioc_qbuf,
1677 .vidioc_dqbuf = vidioc_dqbuf,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001678};
1679
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001680static struct video_device tm6000_template = {
1681 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001682 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001683 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001684 .release = video_device_release,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001685 .tvnorms = TM6000_STD,
1686 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001687};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001688
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001689static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001690 .owner = THIS_MODULE,
1691 .open = tm6000_open,
1692 .release = tm6000_release,
1693 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001694};
1695
1696static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1697 .vidioc_querycap = radio_querycap,
1698 .vidioc_g_tuner = radio_g_tuner,
1699 .vidioc_enum_input = radio_enum_input,
1700 .vidioc_g_audio = radio_g_audio,
1701 .vidioc_s_tuner = radio_s_tuner,
1702 .vidioc_s_audio = radio_s_audio,
1703 .vidioc_s_input = radio_s_input,
1704 .vidioc_s_std = radio_s_std,
1705 .vidioc_queryctrl = radio_queryctrl,
1706 .vidioc_g_input = radio_g_input,
1707 .vidioc_g_ctrl = vidioc_g_ctrl,
1708 .vidioc_s_ctrl = vidioc_s_ctrl,
1709 .vidioc_g_frequency = vidioc_g_frequency,
1710 .vidioc_s_frequency = vidioc_s_frequency,
1711};
1712
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001713static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001714 .name = "tm6000",
1715 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001716 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001717};
1718
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001719/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001720 * Initialization and module stuff
1721 * ------------------------------------------------------------------
1722 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001723
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001724static struct video_device *vdev_init(struct tm6000_core *dev,
1725 const struct video_device
1726 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001727{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001728 struct video_device *vfd;
1729
1730 vfd = video_device_alloc();
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001731 if (NULL == vfd)
1732 return NULL;
1733
1734 *vfd = *template;
1735 vfd->v4l2_dev = &dev->v4l2_dev;
1736 vfd->release = video_device_release;
1737 vfd->debug = tm6000_debug;
1738 vfd->lock = &dev->lock;
1739
1740 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1741
1742 video_set_drvdata(vfd, dev);
1743 return vfd;
1744}
1745
1746int tm6000_v4l2_register(struct tm6000_core *dev)
1747{
1748 int ret = -1;
1749
1750 dev->vfd = vdev_init(dev, &tm6000_template, "video");
1751
1752 if (!dev->vfd) {
1753 printk(KERN_INFO "%s: can't register video device\n",
1754 dev->name);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001755 return -ENOMEM;
1756 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001757
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001758 /* init video dma queues */
1759 INIT_LIST_HEAD(&dev->vidq.active);
1760 INIT_LIST_HEAD(&dev->vidq.queued);
1761
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001762 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001763
1764 if (ret < 0) {
1765 printk(KERN_INFO "%s: can't register video device\n",
1766 dev->name);
1767 return ret;
1768 }
1769
1770 printk(KERN_INFO "%s: registered device %s\n",
1771 dev->name, video_device_node_name(dev->vfd));
1772
Stefan Ringel14169102011-05-09 16:53:49 -03001773 if (dev->caps.has_radio) {
1774 dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
1775 "radio");
1776 if (!dev->radio_dev) {
1777 printk(KERN_INFO "%s: can't register radio device\n",
1778 dev->name);
1779 return ret; /* FIXME release resource */
1780 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001781
Stefan Ringel14169102011-05-09 16:53:49 -03001782 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1783 radio_nr);
1784 if (ret < 0) {
1785 printk(KERN_INFO "%s: can't register radio device\n",
1786 dev->name);
1787 return ret; /* FIXME release resource */
1788 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001789
Stefan Ringel14169102011-05-09 16:53:49 -03001790 printk(KERN_INFO "%s: registered device %s\n",
1791 dev->name, video_device_node_name(dev->radio_dev));
1792 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001793
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001794 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001795 return ret;
1796}
1797
1798int tm6000_v4l2_unregister(struct tm6000_core *dev)
1799{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001800 video_unregister_device(dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001801
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001802 if (dev->radio_dev) {
1803 if (video_is_registered(dev->radio_dev))
1804 video_unregister_device(dev->radio_dev);
1805 else
1806 video_device_release(dev->radio_dev);
1807 dev->radio_dev = NULL;
1808 }
1809
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001810 return 0;
1811}
1812
1813int tm6000_v4l2_exit(void)
1814{
1815 return 0;
1816}
1817
1818module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001819MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001820
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001821module_param_named(debug, tm6000_debug, int, 0444);
1822MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001823
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001824module_param(vid_limit, int, 0644);
1825MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001826