blob: 7a653b2636780284fd20fd2f8082925a22486698 [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
Julian Scheel16427fa2012-10-04 10:04:28 -030052#define TM6000_NUM_URB_BUF 8
53
Stefan Ringel5a4b55e2010-05-19 13:58:27 -030054#define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -030055
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030056/* Declare static vars that will be used as parameters */
57static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
58static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -030059static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
Mauro Carvalho Chehabb391b0e2012-12-20 15:21:23 -020060static bool keep_urb; /* keep urb buffers allocated */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030061
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030062/* Debug level */
63int tm6000_debug;
Mauro Carvalho Chehabfaa7c132010-06-04 21:08:25 -030064EXPORT_SYMBOL_GPL(tm6000_debug);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030065
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -030066static const struct v4l2_queryctrl no_ctrl = {
67 .name = "42",
68 .flags = V4L2_CTRL_FLAG_DISABLED,
69};
70
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030071/* supported controls */
72static struct v4l2_queryctrl tm6000_qctrl[] = {
73 {
74 .id = V4L2_CID_BRIGHTNESS,
75 .type = V4L2_CTRL_TYPE_INTEGER,
76 .name = "Brightness",
77 .minimum = 0,
78 .maximum = 255,
79 .step = 1,
80 .default_value = 54,
81 .flags = 0,
82 }, {
83 .id = V4L2_CID_CONTRAST,
84 .type = V4L2_CTRL_TYPE_INTEGER,
85 .name = "Contrast",
86 .minimum = 0,
87 .maximum = 255,
88 .step = 0x1,
89 .default_value = 119,
90 .flags = 0,
91 }, {
92 .id = V4L2_CID_SATURATION,
93 .type = V4L2_CTRL_TYPE_INTEGER,
94 .name = "Saturation",
95 .minimum = 0,
96 .maximum = 255,
97 .step = 0x1,
98 .default_value = 112,
99 .flags = 0,
100 }, {
101 .id = V4L2_CID_HUE,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Hue",
104 .minimum = -128,
105 .maximum = 127,
106 .step = 0x1,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300107 .default_value = 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300108 .flags = 0,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300109 },
110 /* --- audio --- */
111 {
112 .id = V4L2_CID_AUDIO_MUTE,
113 .name = "Mute",
114 .minimum = 0,
115 .maximum = 1,
116 .type = V4L2_CTRL_TYPE_BOOLEAN,
117 }, {
118 .id = V4L2_CID_AUDIO_VOLUME,
119 .name = "Volume",
120 .minimum = -15,
121 .maximum = 15,
122 .step = 1,
123 .default_value = 0,
124 .type = V4L2_CTRL_TYPE_INTEGER,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300125 }
126};
127
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300128static const unsigned int CTRLS = ARRAY_SIZE(tm6000_qctrl);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300129static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)];
130
131static struct tm6000_fmt format[] = {
132 {
133 .name = "4:2:2, packed, YVY2",
134 .fourcc = V4L2_PIX_FMT_YUYV,
135 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300136 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300137 .name = "4:2:2, packed, UYVY",
138 .fourcc = V4L2_PIX_FMT_UYVY,
139 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300140 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300141 .name = "A/V + VBI mux packet",
142 .fourcc = V4L2_PIX_FMT_TM6000,
143 .depth = 16,
144 }
145};
146
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300147static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
148{
149 unsigned int i;
150
151 for (i = 0; i < CTRLS; i++)
152 if (tm6000_qctrl[i].id == id)
153 return tm6000_qctrl+i;
154 return NULL;
155}
156
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300157/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300158 * DMA and thread functions
159 * ------------------------------------------------------------------
160 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300161
162#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300163#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300164
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300165#define norm_minw(a) norm_maxw(a)
166#define norm_minh(a) norm_maxh(a)
167
168/*
169 * video-buf generic routine to get the next available buffer
170 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300171static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300172 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300173{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300174 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300175
176 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300177 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300178 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300179 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300180 }
181
182 *buf = list_entry(dma_q->active.next,
183 struct tm6000_buffer, vb.queue);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300184}
185
186/*
187 * Announces that a buffer were filled and request the next
188 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300189static inline void buffer_filled(struct tm6000_core *dev,
190 struct tm6000_dmaqueue *dma_q,
191 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300192{
193 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300194 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300195 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300196 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300197 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300198
199 list_del(&buf->vb.queue);
200 wake_up(&buf->vb.done);
201}
202
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300203/*
204 * Identify the tm5600/6000 buffer header type and properly handles
205 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300206static int copy_streams(u8 *data, unsigned long len,
207 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300208{
209 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300210 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Hans Verkuil81730902012-04-20 07:30:48 -0300211 u8 *ptr = data, *endp = data+len;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300212 unsigned long header = 0;
213 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300214 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300215 struct tm6000_buffer *vbuf = NULL;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300216 char *voutp = NULL;
217 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300218
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300219 if (!dev->radio) {
220 /* get video buffer */
221 get_next_buf(dma_q, &vbuf);
222
223 if (!vbuf)
224 return rc;
225 voutp = videobuf_to_vmalloc(&vbuf->vb);
226
227 if (!voutp)
228 return 0;
229 }
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300230
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300231 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300232 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300233 /* Header */
234 if (dev->isoc_ctl.tmp_buf_len > 0) {
235 /* from last urb or packet */
236 header = dev->isoc_ctl.tmp_buf;
237 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300238 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300239 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300240 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300241 4 - dev->isoc_ctl.tmp_buf_len);
242 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300243 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300244 dev->isoc_ctl.tmp_buf_len = 0;
245 } else {
246 if (ptr + 3 >= endp) {
247 /* have incomplete header */
248 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300249 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300250 dev->isoc_ctl.tmp_buf_len);
251 return rc;
252 }
253 /* Seek for sync */
254 for (; ptr < endp - 3; ptr++) {
255 if (*(ptr + 3) == 0x47)
256 break;
257 }
258 /* Get message header */
259 header = *(unsigned long *)ptr;
260 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300261 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300262
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300263 /* split the header fields */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300264 size = ((header & 0x7e) << 1);
265 if (size > 0)
266 size -= 4;
267 block = (header >> 7) & 0xf;
268 field = (header >> 11) & 0x1;
269 line = (header >> 12) & 0x1ff;
270 cmd = (header >> 21) & 0x7;
271 /* Validates haeder fields */
272 if (size > TM6000_URB_MSG_LEN)
273 size = TM6000_URB_MSG_LEN;
274 pktsize = TM6000_URB_MSG_LEN;
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300275 /*
276 * calculate position in buffer and change the buffer
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300277 */
278 switch (cmd) {
279 case TM6000_URB_MSG_VIDEO:
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300280 if (!dev->radio) {
281 if ((dev->isoc_ctl.vfield != field) &&
282 (field == 1)) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300283 /*
284 * Announces that a new buffer
285 * were filled
286 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300287 buffer_filled(dev, dma_q, vbuf);
288 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300289 "new buffer filled\n");
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300290 get_next_buf(dma_q, &vbuf);
291 if (!vbuf)
292 return rc;
293 voutp = videobuf_to_vmalloc(&vbuf->vb);
294 if (!voutp)
295 return rc;
296 memset(voutp, 0, vbuf->vb.size);
297 }
298 linewidth = vbuf->vb.width << 1;
299 pos = ((line << 1) - field - 1) *
300 linewidth + block * TM6000_URB_MSG_LEN;
301 /* Don't allow to write out of the buffer */
302 if (pos + size > vbuf->vb.size)
303 cmd = TM6000_URB_MSG_ERR;
304 dev->isoc_ctl.vfield = field;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300305 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300306 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300307 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300308 break;
309 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300310 case TM6000_URB_MSG_PTS:
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300311 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300312 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300313 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300314 } else {
315 /* Continue the last copy */
316 cmd = dev->isoc_ctl.cmd;
317 size = dev->isoc_ctl.size;
318 pos = dev->isoc_ctl.pos;
319 pktsize = dev->isoc_ctl.pktsize;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300320 field = dev->isoc_ctl.field;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300321 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300322 cpysize = (endp - ptr > size) ? size : endp - ptr;
323 if (cpysize) {
324 /* copy data in different buffers */
325 switch (cmd) {
326 case TM6000_URB_MSG_VIDEO:
327 /* Fills video buffer */
328 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300329 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300330 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300331 case TM6000_URB_MSG_AUDIO: {
332 int i;
333 for (i = 0; i < cpysize; i += 2)
334 swab16s((u16 *)(ptr + i));
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300335
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300336 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300337 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300338 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300339 case TM6000_URB_MSG_VBI:
340 /* Need some code to copy vbi buffer */
341 break;
Stefan Ringel2f349da2011-05-09 16:54:02 -0300342 case TM6000_URB_MSG_PTS: {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300343 /* Need some code to copy pts */
Stefan Ringel2f349da2011-05-09 16:54:02 -0300344 u32 pts;
345 pts = *(u32 *)ptr;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300346 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
347 field, pts);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300348 break;
349 }
Stefan Ringel2f349da2011-05-09 16:54:02 -0300350 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300351 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300352 if (ptr + pktsize > endp) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300353 /*
354 * End of URB packet, but cmd processing is not
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300355 * complete. Preserve the state for a next packet
356 */
357 dev->isoc_ctl.pos = pos + cpysize;
358 dev->isoc_ctl.size = size - cpysize;
359 dev->isoc_ctl.cmd = cmd;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300360 dev->isoc_ctl.field = field;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300361 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300362 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300363 } else {
364 dev->isoc_ctl.cmd = 0;
365 ptr += pktsize;
366 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300367 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300368 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300369}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300370
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300371/*
372 * Identify the tm5600/6000 buffer header type and properly handles
373 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300374static int copy_multiplexed(u8 *ptr, unsigned long len,
375 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300376{
377 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300378 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
379 unsigned int pos = dev->isoc_ctl.pos, cpysize;
380 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300381 struct tm6000_buffer *buf;
382 char *outp = NULL;
383
384 get_next_buf(dma_q, &buf);
385 if (buf)
386 outp = videobuf_to_vmalloc(&buf->vb);
387
388 if (!outp)
389 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300390
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300391 while (len > 0) {
392 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300393 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300394 pos += cpysize;
395 ptr += cpysize;
396 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300397 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300398 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300399 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300400 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300401 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300402 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300403 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300404 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300405 outp = videobuf_to_vmalloc(&(buf->vb));
406 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300407 return rc;
408 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300409 }
410 }
411
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300412 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300413 return rc;
414}
415
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300416static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300417 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300418{
419 char *errmsg = "Unknown";
420
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300421 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300422 case -ENOENT:
423 errmsg = "unlinked synchronuously";
424 break;
425 case -ECONNRESET:
426 errmsg = "unlinked asynchronuously";
427 break;
428 case -ENOSR:
429 errmsg = "Buffer error (overrun)";
430 break;
431 case -EPIPE:
432 errmsg = "Stalled (device not responding)";
433 break;
434 case -EOVERFLOW:
435 errmsg = "Babble (bad cable?)";
436 break;
437 case -EPROTO:
438 errmsg = "Bit-stuff error (bad cable?)";
439 break;
440 case -EILSEQ:
441 errmsg = "CRC/Timeout (could be anything)";
442 break;
443 case -ETIME:
444 errmsg = "Device does not respond";
445 break;
446 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300447 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300448 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
449 status, errmsg);
450 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300451 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300452 packet, status, errmsg);
453 }
454}
455
456
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300457/*
458 * Controls the isoc copy of each urb packet
459 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300460static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300461{
462 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300463 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
464 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300465 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300466
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300467 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300468 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300469 return 0;
470 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300471
472 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300473 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300474
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300475 if (status < 0) {
476 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300477 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300478 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300479
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300480 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300481
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300482 if (len > 0) {
483 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300484 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300485 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
486 rc = copy_multiplexed(p, len, urb);
487 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300488 return rc;
489 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300490 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300491 }
492 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300493 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300494 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300495 return rc;
496}
497
498/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300499 * URB control
500 * ------------------------------------------------------------------
501 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300502
503/*
504 * IRQ callback, called by URB callback
505 */
506static void tm6000_irq_callback(struct urb *urb)
507{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300508 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300509 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300510 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300511
Thierry Reding14f09152011-08-04 04:14:06 -0300512 switch (urb->status) {
513 case 0:
514 case -ETIMEDOUT:
515 break;
516
517 case -ECONNRESET:
518 case -ENOENT:
519 case -ESHUTDOWN:
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300520 return;
521
Thierry Reding14f09152011-08-04 04:14:06 -0300522 default:
523 tm6000_err("urb completion error %d.\n", urb->status);
524 break;
525 }
526
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300527 spin_lock(&dev->slock);
528 tm6000_isoc_copy(urb);
529 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300530
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300531 /* Reset urb buffers */
532 for (i = 0; i < urb->number_of_packets; i++) {
533 urb->iso_frame_desc[i].status = 0;
534 urb->iso_frame_desc[i].actual_length = 0;
535 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300536
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300537 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
538 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300539 tm6000_err("urb resubmit failed (error=%i)\n",
540 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300541}
542
543/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300544 * Allocate URB buffers
545 */
546static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
547{
548 int num_bufs = TM6000_NUM_URB_BUF;
549 int i;
550
551 if (dev->urb_buffer != NULL)
552 return 0;
553
554 dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
555 if (!dev->urb_buffer) {
556 tm6000_err("cannot allocate memory for urb buffers\n");
557 return -ENOMEM;
558 }
559
560 dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
561 if (!dev->urb_dma) {
562 tm6000_err("cannot allocate memory for urb dma pointers\n");
563 return -ENOMEM;
564 }
565
566 for (i = 0; i < num_bufs; i++) {
567 dev->urb_buffer[i] = usb_alloc_coherent(
568 dev->udev, dev->urb_size,
569 GFP_KERNEL, &dev->urb_dma[i]);
570 if (!dev->urb_buffer[i]) {
571 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
572 dev->urb_size, i);
573 return -ENOMEM;
574 }
575 memset(dev->urb_buffer[i], 0, dev->urb_size);
576 }
577
578 return 0;
579}
580
581/*
582 * Free URB buffers
583 */
584static int tm6000_free_urb_buffers(struct tm6000_core *dev)
585{
586 int i;
587
588 if (dev->urb_buffer == NULL)
589 return 0;
590
591 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
592 if (dev->urb_buffer[i]) {
593 usb_free_coherent(dev->udev,
594 dev->urb_size,
595 dev->urb_buffer[i],
596 dev->urb_dma[i]);
597 dev->urb_buffer[i] = NULL;
598 }
599 }
600 kfree(dev->urb_buffer);
601 kfree(dev->urb_dma);
602 dev->urb_buffer = NULL;
603 dev->urb_dma = NULL;
604
605 return 0;
606}
607
608/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300609 * Stop and Deallocate URBs
610 */
611static void tm6000_uninit_isoc(struct tm6000_core *dev)
612{
613 struct urb *urb;
614 int i;
615
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300616 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300617 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300618 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300619 if (urb) {
620 usb_kill_urb(urb);
621 usb_unlink_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300622 usb_free_urb(urb);
623 dev->isoc_ctl.urb[i] = NULL;
624 }
625 dev->isoc_ctl.transfer_buffer[i] = NULL;
626 }
627
Julian Scheel16427fa2012-10-04 10:04:28 -0300628 if (!keep_urb)
629 tm6000_free_urb_buffers(dev);
630
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300631 kfree(dev->isoc_ctl.urb);
632 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300633
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300634 dev->isoc_ctl.urb = NULL;
635 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300636 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300637}
638
639/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300640 * Assign URBs and start IRQ
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300641 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300642static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300643{
644 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Julian Scheel16427fa2012-10-04 10:04:28 -0300645 int i, j, sb_size, pipe, size, max_packets;
646 int num_bufs = TM6000_NUM_URB_BUF;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300647 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300648
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300649 /* De-allocates all pending stuff */
650 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300651 /* Stop interrupt USB pipe */
652 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300653
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300654 usb_set_interface(dev->udev,
655 dev->isoc_in.bInterfaceNumber,
656 dev->isoc_in.bAlternateSetting);
657
Dmitri Belimov641d2112010-12-22 05:57:46 -0300658 /* Start interrupt USB pipe */
659 tm6000_ir_int_start(dev);
660
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300661 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300662 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300663 USB_ENDPOINT_NUMBER_MASK);
664
665 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
666
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300667 if (size > dev->isoc_in.maxsize)
668 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300669
670 dev->isoc_ctl.max_pkt_size = size;
671
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300672 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300673 sb_size = max_packets * size;
Julian Scheel16427fa2012-10-04 10:04:28 -0300674 dev->urb_size = sb_size;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300675
676 dev->isoc_ctl.num_bufs = num_bufs;
677
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300678 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300679 if (!dev->isoc_ctl.urb) {
680 tm6000_err("cannot alloc memory for usb buffers\n");
681 return -ENOMEM;
682 }
683
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300684 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300685 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300686 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300687 tm6000_err("cannot allocate memory for usbtransfer\n");
688 kfree(dev->isoc_ctl.urb);
689 return -ENOMEM;
690 }
691
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300692 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
693 " (%d bytes) of %d bytes each to handle %u size\n",
694 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300695 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300696
Julian Scheel16427fa2012-10-04 10:04:28 -0300697
698 if (!dev->urb_buffer && tm6000_alloc_urb_buffers(dev) < 0) {
699 tm6000_err("cannot allocate memory for urb buffers\n");
700
701 /* call free, as some buffers might have been allocated */
702 tm6000_free_urb_buffers(dev);
703 kfree(dev->isoc_ctl.urb);
704 kfree(dev->isoc_ctl.transfer_buffer);
705 return -ENOMEM;
706 }
707
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300708 /* allocate urbs and transfer buffers */
709 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
710 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
711 if (!urb) {
712 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
713 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehabc1a16412007-10-15 15:43:50 -0300714 usb_free_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300715 return -ENOMEM;
716 }
717 dev->isoc_ctl.urb[i] = urb;
718
Julian Scheel16427fa2012-10-04 10:04:28 -0300719 urb->transfer_dma = dev->urb_dma[i];
720 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300721
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300722 usb_fill_bulk_urb(urb, dev->udev, pipe,
723 dev->isoc_ctl.transfer_buffer[i], sb_size,
724 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300725 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300726 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300727 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300728
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300729 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300730 urb->iso_frame_desc[j].offset = size * j;
731 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300732 }
733 }
734
735 return 0;
736}
737
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300738static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300739{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300740 struct tm6000_dmaqueue *dma_q = &dev->vidq;
741 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300742
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300743 dma_q->frame = 0;
744 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300745
746 init_waitqueue_head(&dma_q->wq);
747
748 /* submit urbs and enables IRQ */
749 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300750 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300751 if (rc) {
752 tm6000_err("submit of urb %i failed (error=%i)\n", i,
753 rc);
754 tm6000_uninit_isoc(dev);
755 return rc;
756 }
757 }
758
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300759 return 0;
760}
761
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300762/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300763 * Videobuf operations
764 * ------------------------------------------------------------------
765 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300766
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300767static int
768buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
769{
770 struct tm6000_fh *fh = vq->priv_data;
771
772 *size = fh->fmt->depth * fh->width * fh->height >> 3;
773 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300774 *count = TM6000_DEF_BUF;
775
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300776 if (*count < TM6000_MIN_BUF)
777 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300778
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300779 while (*size * *count > vid_limit * 1024 * 1024)
780 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300781
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300782 return 0;
783}
784
785static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
786{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300787 struct tm6000_fh *fh = vq->priv_data;
788 struct tm6000_core *dev = fh->dev;
789 unsigned long flags;
790
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300791 if (in_interrupt())
792 BUG();
793
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300794 /* We used to wait for the buffer to finish here, but this didn't work
795 because, as we were keeping the state as VIDEOBUF_QUEUED,
796 videobuf_queue_cancel marked it as finished for us.
797 (Also, it could wedge forever if the hardware was misconfigured.)
798
799 This should be safe; by the time we get here, the buffer isn't
800 queued anymore. If we ever start marking the buffers as
801 VIDEOBUF_ACTIVE, it won't be, though.
802 */
803 spin_lock_irqsave(&dev->slock, flags);
804 if (dev->isoc_ctl.buf == buf)
805 dev->isoc_ctl.buf = NULL;
806 spin_unlock_irqrestore(&dev->slock, flags);
807
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300808 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300809 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300810}
811
812static int
813buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
814 enum v4l2_field field)
815{
816 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300817 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300818 struct tm6000_core *dev = fh->dev;
Thierry Reding88e834a2011-08-04 04:14:12 -0300819 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300820
821 BUG_ON(NULL == fh->fmt);
822
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300823
824 /* FIXME: It assumes depth=2 */
825 /* The only currently supported format is 16 bits/pixel */
826 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
827 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
828 return -EINVAL;
829
830 if (buf->fmt != fh->fmt ||
831 buf->vb.width != fh->width ||
832 buf->vb.height != fh->height ||
833 buf->vb.field != field) {
834 buf->fmt = fh->fmt;
835 buf->vb.width = fh->width;
836 buf->vb.height = fh->height;
837 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300838 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300839 }
840
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300841 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Curtis McEnroeed7c2212011-06-01 22:21:56 -0400842 rc = videobuf_iolock(vq, &buf->vb, NULL);
843 if (rc != 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300844 goto fail;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300845 }
846
Thierry Reding88e834a2011-08-04 04:14:12 -0300847 if (!dev->isoc_ctl.num_bufs) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300848 rc = tm6000_prepare_isoc(dev);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300849 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300850 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300851
852 rc = tm6000_start_thread(dev);
853 if (rc < 0)
854 goto fail;
855
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300856 }
857
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300858 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300859 return 0;
860
861fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300862 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300863 return rc;
864}
865
866static void
867buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
868{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300869 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300870 struct tm6000_fh *fh = vq->priv_data;
871 struct tm6000_core *dev = fh->dev;
872 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300873
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300874 buf->vb.state = VIDEOBUF_QUEUED;
875 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300876}
877
878static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
879{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300880 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300881
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300882 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300883}
884
885static struct videobuf_queue_ops tm6000_video_qops = {
886 .buf_setup = buffer_setup,
887 .buf_prepare = buffer_prepare,
888 .buf_queue = buffer_queue,
889 .buf_release = buffer_release,
890};
891
892/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300893 * IOCTL handling
894 * ------------------------------------------------------------------
895 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300896
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300897static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300898{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300899 /* Is the current fh handling it? if so, that's OK */
900 if (dev->resources == fh && dev->is_res_read)
901 return true;
902
903 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300904}
905
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300906static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300907{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300908 /* Is the current fh handling it? if so, that's OK */
909 if (dev->resources == fh)
910 return true;
911
912 return false;
913}
914
915static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
916 bool is_res_read)
917{
918 /* Is the current fh handling it? if so, that's OK */
919 if (dev->resources == fh && dev->is_res_read == is_res_read)
920 return true;
921
922 /* is it free? */
923 if (dev->resources)
924 return false;
925
926 /* grab it */
927 dev->resources = fh;
928 dev->is_res_read = is_res_read;
929 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
930 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300931}
932
933static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
934{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300935 /* Is the current fh handling it? if so, that's OK */
936 if (dev->resources != fh)
937 return;
938
939 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300940 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300941}
942
943/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300944 * IOCTL vidioc handling
945 * ------------------------------------------------------------------
946 */
947static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300948 struct v4l2_capability *cap)
949{
Stefan Ringel886a3c02011-05-09 16:53:50 -0300950 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300951 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300952
953 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300954 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300955 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Stefan Ringel886a3c02011-05-09 16:53:50 -0300956 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300957 cap->device_caps |= V4L2_CAP_TUNER;
958 if (vdev->vfl_type == VFL_TYPE_GRABBER)
959 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
960 V4L2_CAP_STREAMING |
961 V4L2_CAP_READWRITE;
962 else
963 cap->device_caps |= V4L2_CAP_RADIO;
964 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
965 V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
Stefan Ringel886a3c02011-05-09 16:53:50 -0300966
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300967 return 0;
968}
969
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300970static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300971 struct v4l2_fmtdesc *f)
972{
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300973 if (f->index >= ARRAY_SIZE(format))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300974 return -EINVAL;
975
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300976 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300977 f->pixelformat = format[f->index].fourcc;
978 return 0;
979}
980
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300981static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300982 struct v4l2_format *f)
983{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300984 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300985
986 f->fmt.pix.width = fh->width;
987 f->fmt.pix.height = fh->height;
988 f->fmt.pix.field = fh->vb_vidq.field;
989 f->fmt.pix.pixelformat = fh->fmt->fourcc;
990 f->fmt.pix.bytesperline =
991 (f->fmt.pix.width * fh->fmt->depth) >> 3;
992 f->fmt.pix.sizeimage =
993 f->fmt.pix.height * f->fmt.pix.bytesperline;
994
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300995 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300996}
997
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300998static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300999{
1000 unsigned int i;
1001
1002 for (i = 0; i < ARRAY_SIZE(format); i++)
1003 if (format[i].fourcc == fourcc)
1004 return format+i;
1005 return NULL;
1006}
1007
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001008static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001009 struct v4l2_format *f)
1010{
1011 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
1012 struct tm6000_fmt *fmt;
1013 enum v4l2_field field;
1014
1015 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1016 if (NULL == fmt) {
1017 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
1018 " invalid.\n", f->fmt.pix.pixelformat);
1019 return -EINVAL;
1020 }
1021
1022 field = f->fmt.pix.field;
1023
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001024 if (field == V4L2_FIELD_ANY)
1025 field = V4L2_FIELD_SEQ_TB;
1026 else if (V4L2_FIELD_INTERLACED != field) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001027 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
1028 return -EINVAL;
1029 }
1030
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001031 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001032
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001033 f->fmt.pix.width = dev->width;
1034 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001035
1036 f->fmt.pix.width &= ~0x01;
1037
1038 f->fmt.pix.field = field;
1039
1040 f->fmt.pix.bytesperline =
1041 (f->fmt.pix.width * fmt->depth) >> 3;
1042 f->fmt.pix.sizeimage =
1043 f->fmt.pix.height * f->fmt.pix.bytesperline;
1044
1045 return 0;
1046}
1047
1048/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001049static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001050 struct v4l2_format *f)
1051{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001052 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001053 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001054 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001055 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001056 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001057
1058 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1059 fh->width = f->fmt.pix.width;
1060 fh->height = f->fmt.pix.height;
1061 fh->vb_vidq.field = f->fmt.pix.field;
1062 fh->type = f->type;
1063
1064 dev->fourcc = f->fmt.pix.pixelformat;
1065
1066 tm6000_set_fourcc_format(dev);
1067
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001068 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001069}
1070
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001071static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001072 struct v4l2_requestbuffers *p)
1073{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001074 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001075
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001076 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001077}
1078
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001079static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001080 struct v4l2_buffer *p)
1081{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001082 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001083
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001084 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001085}
1086
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001087static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001088{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001089 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001090
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001091 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001092}
1093
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001094static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001095{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001096 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001097
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001098 return videobuf_dqbuf(&fh->vb_vidq, p,
1099 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001100}
1101
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001102static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1103{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001104 struct tm6000_fh *fh = priv;
1105 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001106
1107 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1108 return -EINVAL;
1109 if (i != fh->type)
1110 return -EINVAL;
1111
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001112 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001113 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001114 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001115}
1116
1117static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1118{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001119 struct tm6000_fh *fh = priv;
1120 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001121
1122 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1123 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001124
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001125 if (i != fh->type)
1126 return -EINVAL;
1127
1128 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001129 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001130
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001131 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001132}
1133
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001134static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001135{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001136 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001137 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001138 struct tm6000_core *dev = fh->dev;
1139
Stefan Ringel4f526102010-10-27 16:48:05 -03001140 dev->norm = *norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001141 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001142
1143 fh->width = dev->width;
1144 fh->height = dev->height;
1145
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001146 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001147 return rc;
1148
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001149 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001150
1151 return 0;
1152}
1153
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001154static const char *iname[] = {
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001155 [TM6000_INPUT_TV] = "Television",
1156 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1157 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1158 [TM6000_INPUT_SVIDEO] = "S-Video",
1159};
1160
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001161static int vidioc_enum_input(struct file *file, void *priv,
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001162 struct v4l2_input *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001163{
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001164 struct tm6000_fh *fh = priv;
1165 struct tm6000_core *dev = fh->dev;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001166 unsigned int n;
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001167
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001168 n = i->index;
1169 if (n >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001170 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001171
1172 if (!dev->vinput[n].type)
1173 return -EINVAL;
1174
1175 i->index = n;
1176
1177 if (dev->vinput[n].type == TM6000_INPUT_TV)
1178 i->type = V4L2_INPUT_TYPE_TUNER;
1179 else
1180 i->type = V4L2_INPUT_TYPE_CAMERA;
1181
1182 strcpy(i->name, iname[dev->vinput[n].type]);
1183
1184 i->std = TM6000_STD;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001185
1186 return 0;
1187}
1188
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001189static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001190{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001191 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001192 struct tm6000_core *dev = fh->dev;
1193
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001194 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001195
1196 return 0;
1197}
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001198
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001199static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001200{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001201 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001202 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001203 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001204
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001205 if (i >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001206 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001207 if (!dev->vinput[i].type)
1208 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001209
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001210 dev->input = i;
1211
1212 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001213
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001214 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001215}
1216
Stefan Ringel886a3c02011-05-09 16:53:50 -03001217/* --- controls ---------------------------------------------- */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001218static int vidioc_queryctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001219 struct v4l2_queryctrl *qc)
1220{
1221 int i;
1222
1223 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
1224 if (qc->id && qc->id == tm6000_qctrl[i].id) {
1225 memcpy(qc, &(tm6000_qctrl[i]),
1226 sizeof(*qc));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001227 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001228 }
1229
1230 return -EINVAL;
1231}
1232
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001233static int vidioc_g_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001234 struct v4l2_control *ctrl)
1235{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001236 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001237 struct tm6000_core *dev = fh->dev;
1238 int val;
1239
1240 /* FIXME: Probably, those won't work! Maybe we need shadow regs */
1241 switch (ctrl->id) {
1242 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001243 val = tm6000_get_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001244 break;
1245 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001246 val = tm6000_get_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001247 return 0;
1248 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001249 val = tm6000_get_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001250 return 0;
1251 case V4L2_CID_HUE:
Mauro Carvalho Chehab9afec492010-03-11 10:26:46 -03001252 val = tm6000_get_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, 0);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001253 return 0;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001254 case V4L2_CID_AUDIO_MUTE:
1255 val = dev->ctl_mute;
1256 return 0;
1257 case V4L2_CID_AUDIO_VOLUME:
1258 val = dev->ctl_volume;
1259 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001260 default:
1261 return -EINVAL;
1262 }
1263
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001264 if (val < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001265 return val;
1266
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001267 ctrl->value = val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001268
1269 return 0;
1270}
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001271static int vidioc_s_ctrl(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001272 struct v4l2_control *ctrl)
1273{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001274 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001275 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001276 u8 val = ctrl->value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001277
1278 switch (ctrl->id) {
1279 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001280 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001281 return 0;
1282 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001283 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001284 return 0;
1285 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001286 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001287 return 0;
1288 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001289 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001290 return 0;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001291 case V4L2_CID_AUDIO_MUTE:
1292 dev->ctl_mute = val;
1293 tm6000_tvaudio_set_mute(dev, val);
1294 return 0;
1295 case V4L2_CID_AUDIO_VOLUME:
1296 dev->ctl_volume = val;
1297 tm6000_set_volume(dev, val);
1298 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001299 }
1300 return -EINVAL;
1301}
1302
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001303static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001304 struct v4l2_tuner *t)
1305{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001306 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001307 struct tm6000_core *dev = fh->dev;
1308
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001309 if (UNSET == dev->tuner_type)
1310 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001311 if (0 != t->index)
1312 return -EINVAL;
1313
1314 strcpy(t->name, "Television");
1315 t->type = V4L2_TUNER_ANALOG_TV;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001316 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001317 t->rangehigh = 0xffffffffUL;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001318 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1319
1320 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1321
1322 t->audmode = dev->amode;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001323
1324 return 0;
1325}
1326
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001327static int vidioc_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001328 struct v4l2_tuner *t)
1329{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001330 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001331 struct tm6000_core *dev = fh->dev;
1332
1333 if (UNSET == dev->tuner_type)
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001334 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001335 if (0 != t->index)
1336 return -EINVAL;
1337
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001338 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1339 dev->amode = V4L2_TUNER_MODE_STEREO;
1340 else
1341 dev->amode = t->audmode;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001342 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1343
1344 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001345
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001346 return 0;
1347}
1348
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001349static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001350 struct v4l2_frequency *f)
1351{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001352 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001353 struct tm6000_core *dev = fh->dev;
1354
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001355 if (UNSET == dev->tuner_type)
1356 return -ENOTTY;
1357 if (f->tuner)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001358 return -EINVAL;
1359
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001360 f->frequency = dev->freq;
1361
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001362 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001363
1364 return 0;
1365}
1366
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001367static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001368 struct v4l2_frequency *f)
1369{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001370 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001371 struct tm6000_core *dev = fh->dev;
1372
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001373 if (UNSET == dev->tuner_type)
1374 return -ENOTTY;
1375 if (f->tuner != 0)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001376 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001377
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001378 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001379 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001380
1381 return 0;
1382}
1383
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001384static int radio_g_tuner(struct file *file, void *priv,
1385 struct v4l2_tuner *t)
1386{
1387 struct tm6000_fh *fh = file->private_data;
1388 struct tm6000_core *dev = fh->dev;
1389
1390 if (0 != t->index)
1391 return -EINVAL;
1392
1393 memset(t, 0, sizeof(*t));
1394 strcpy(t->name, "Radio");
1395 t->type = V4L2_TUNER_RADIO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001396 t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001397 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001398 t->audmode = V4L2_TUNER_MODE_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001399
1400 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1401
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001402 return 0;
1403}
1404
1405static int radio_s_tuner(struct file *file, void *priv,
1406 struct v4l2_tuner *t)
1407{
1408 struct tm6000_fh *fh = file->private_data;
1409 struct tm6000_core *dev = fh->dev;
1410
1411 if (0 != t->index)
1412 return -EINVAL;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001413 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1414 t->audmode = V4L2_TUNER_MODE_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001415
1416 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1417
1418 return 0;
1419}
1420
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001421static int radio_queryctrl(struct file *file, void *priv,
1422 struct v4l2_queryctrl *c)
1423{
1424 const struct v4l2_queryctrl *ctrl;
1425
1426 if (c->id < V4L2_CID_BASE ||
1427 c->id >= V4L2_CID_LASTP1)
1428 return -EINVAL;
1429 if (c->id == V4L2_CID_AUDIO_MUTE) {
1430 ctrl = ctrl_by_id(c->id);
1431 *c = *ctrl;
1432 } else
1433 *c = no_ctrl;
1434
1435 return 0;
1436}
1437
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001438/* ------------------------------------------------------------------
1439 File operations for the device
1440 ------------------------------------------------------------------*/
1441
Hans Verkuil14a09da2012-06-24 07:26:46 -03001442static int __tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001443{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001444 struct video_device *vdev = video_devdata(file);
1445 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001446 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001447 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001448 int i, rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001449 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001450
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001451 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1452 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001453
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001454 switch (vdev->vfl_type) {
1455 case VFL_TYPE_GRABBER:
1456 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1457 break;
1458 case VFL_TYPE_VBI:
1459 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1460 break;
1461 case VFL_TYPE_RADIO:
1462 radio = 1;
1463 break;
1464 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001465
1466 /* If more than one user, mutex should be added */
1467 dev->users++;
1468
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001469 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1470 video_device_node_name(vdev), v4l2_type_names[type],
1471 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001472
1473 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001474 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001475 if (NULL == fh) {
1476 dev->users--;
1477 return -ENOMEM;
1478 }
1479
1480 file->private_data = fh;
1481 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001482 fh->radio = radio;
1483 dev->radio = radio;
1484 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001485 dev->fourcc = format[0].fourcc;
1486
1487 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001488
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001489 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001490
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001491 fh->width = dev->width;
1492 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001493
1494 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1495 "dev->vidq=0x%08lx\n",
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001496 (unsigned long)fh, (unsigned long)dev,
1497 (unsigned long)&dev->vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001498 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001499 "queued=%d\n", list_empty(&dev->vidq.queued));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001500 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001501 "active=%d\n", list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001502
1503 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001504 rc = tm6000_init_analog_mode(dev);
1505 if (rc < 0)
1506 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001507
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001508 if (dev->mode != TM6000_MODE_ANALOG) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001509 /* Put all controls at a sane state */
1510 for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++)
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001511 qctl_regs[i] = tm6000_qctrl[i].default_value;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001512
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001513 dev->mode = TM6000_MODE_ANALOG;
1514 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001515
Thierry Redingaa4a5832011-08-04 04:14:15 -03001516 if (!fh->radio) {
1517 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1518 NULL, &dev->slock,
1519 fh->type,
1520 V4L2_FIELD_INTERLACED,
1521 sizeof(struct tm6000_buffer), fh, &dev->lock);
1522 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001523 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001524 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001525 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1526 tm6000_prepare_isoc(dev);
1527 tm6000_start_thread(dev);
1528 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001529
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001530 return 0;
1531}
1532
Hans Verkuil14a09da2012-06-24 07:26:46 -03001533static int tm6000_open(struct file *file)
1534{
1535 struct video_device *vdev = video_devdata(file);
1536 int res;
1537
1538 mutex_lock(vdev->lock);
1539 res = __tm6000_open(file);
1540 mutex_unlock(vdev->lock);
1541 return res;
1542}
1543
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001544static ssize_t
1545tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1546{
Hans Verkuil14a09da2012-06-24 07:26:46 -03001547 struct tm6000_fh *fh = file->private_data;
1548 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001549
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001550 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil14a09da2012-06-24 07:26:46 -03001551 int res;
1552
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001553 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001554 return -EBUSY;
1555
Hans Verkuil14a09da2012-06-24 07:26:46 -03001556 if (mutex_lock_interruptible(&dev->lock))
1557 return -ERESTARTSYS;
1558 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001559 file->f_flags & O_NONBLOCK);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001560 mutex_unlock(&dev->lock);
1561 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001562 }
1563 return 0;
1564}
1565
1566static unsigned int
Hans Verkuil14a09da2012-06-24 07:26:46 -03001567__tm6000_poll(struct file *file, struct poll_table_struct *wait)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001568{
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
Hans Verkuil14a09da2012-06-24 07:26:46 -03001594static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1595{
1596 struct tm6000_fh *fh = file->private_data;
1597 struct tm6000_core *dev = fh->dev;
1598 unsigned int res;
1599
1600 mutex_lock(&dev->lock);
1601 res = __tm6000_poll(file, wait);
1602 mutex_unlock(&dev->lock);
1603 return res;
1604}
1605
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001606static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001607{
1608 struct tm6000_fh *fh = file->private_data;
1609 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001610 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001611
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001612 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1613 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001614
Hans Verkuil14a09da2012-06-24 07:26:46 -03001615 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001616 dev->users--;
1617
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001618 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001619
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001620 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001621 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001622
Stefan Ringel8159c182011-11-28 15:46:18 -03001623 /* Stop interrupt USB pipe */
1624 tm6000_ir_int_stop(dev);
1625
1626 usb_reset_configuration(dev->udev);
1627
Thierry Reding4be9c8f2011-12-06 09:39:35 -03001628 if (dev->int_in.endp)
Stefan Ringel8159c182011-11-28 15:46:18 -03001629 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001630 dev->isoc_in.bInterfaceNumber, 2);
Stefan Ringel8159c182011-11-28 15:46:18 -03001631 else
1632 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001633 dev->isoc_in.bInterfaceNumber, 0);
Stefan Ringel8159c182011-11-28 15:46:18 -03001634
1635 /* Start interrupt USB pipe */
1636 tm6000_ir_int_start(dev);
1637
Thierry Redingaa4a5832011-08-04 04:14:15 -03001638 if (!fh->radio)
1639 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001640 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001641
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001642 kfree(fh);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001643 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001644
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001645 return 0;
1646}
1647
1648static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1649{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001650 struct tm6000_fh *fh = file->private_data;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001651 struct tm6000_core *dev = fh->dev;
1652 int res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001653
Hans Verkuil14a09da2012-06-24 07:26:46 -03001654 if (mutex_lock_interruptible(&dev->lock))
1655 return -ERESTARTSYS;
1656 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1657 mutex_unlock(&dev->lock);
1658 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001659}
1660
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001661static struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001662 .owner = THIS_MODULE,
1663 .open = tm6000_open,
1664 .release = tm6000_release,
1665 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1666 .read = tm6000_read,
1667 .poll = tm6000_poll,
1668 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001669};
1670
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001671static const struct v4l2_ioctl_ops video_ioctl_ops = {
1672 .vidioc_querycap = vidioc_querycap,
1673 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1674 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1675 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1676 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1677 .vidioc_s_std = vidioc_s_std,
1678 .vidioc_enum_input = vidioc_enum_input,
1679 .vidioc_g_input = vidioc_g_input,
1680 .vidioc_s_input = vidioc_s_input,
1681 .vidioc_queryctrl = vidioc_queryctrl,
1682 .vidioc_g_ctrl = vidioc_g_ctrl,
1683 .vidioc_s_ctrl = vidioc_s_ctrl,
1684 .vidioc_g_tuner = vidioc_g_tuner,
1685 .vidioc_s_tuner = vidioc_s_tuner,
1686 .vidioc_g_frequency = vidioc_g_frequency,
1687 .vidioc_s_frequency = vidioc_s_frequency,
1688 .vidioc_streamon = vidioc_streamon,
1689 .vidioc_streamoff = vidioc_streamoff,
1690 .vidioc_reqbufs = vidioc_reqbufs,
1691 .vidioc_querybuf = vidioc_querybuf,
1692 .vidioc_qbuf = vidioc_qbuf,
1693 .vidioc_dqbuf = vidioc_dqbuf,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001694};
1695
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001696static struct video_device tm6000_template = {
1697 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001698 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001699 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001700 .release = video_device_release,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001701 .tvnorms = TM6000_STD,
1702 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001703};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001704
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001705static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001706 .owner = THIS_MODULE,
1707 .open = tm6000_open,
1708 .release = tm6000_release,
1709 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001710};
1711
1712static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001713 .vidioc_querycap = vidioc_querycap,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001714 .vidioc_g_tuner = radio_g_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001715 .vidioc_s_tuner = radio_s_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001716 .vidioc_queryctrl = radio_queryctrl,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001717 .vidioc_g_ctrl = vidioc_g_ctrl,
1718 .vidioc_s_ctrl = vidioc_s_ctrl,
1719 .vidioc_g_frequency = vidioc_g_frequency,
1720 .vidioc_s_frequency = vidioc_s_frequency,
1721};
1722
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001723static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001724 .name = "tm6000",
1725 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001726 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001727};
1728
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001729/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001730 * Initialization and module stuff
1731 * ------------------------------------------------------------------
1732 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001733
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001734static struct video_device *vdev_init(struct tm6000_core *dev,
1735 const struct video_device
1736 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001737{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001738 struct video_device *vfd;
1739
1740 vfd = video_device_alloc();
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001741 if (NULL == vfd)
1742 return NULL;
1743
1744 *vfd = *template;
1745 vfd->v4l2_dev = &dev->v4l2_dev;
1746 vfd->release = video_device_release;
1747 vfd->debug = tm6000_debug;
1748 vfd->lock = &dev->lock;
1749
1750 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1751
1752 video_set_drvdata(vfd, dev);
1753 return vfd;
1754}
1755
1756int tm6000_v4l2_register(struct tm6000_core *dev)
1757{
1758 int ret = -1;
1759
1760 dev->vfd = vdev_init(dev, &tm6000_template, "video");
1761
1762 if (!dev->vfd) {
1763 printk(KERN_INFO "%s: can't register video device\n",
1764 dev->name);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001765 return -ENOMEM;
1766 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001767
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001768 /* init video dma queues */
1769 INIT_LIST_HEAD(&dev->vidq.active);
1770 INIT_LIST_HEAD(&dev->vidq.queued);
1771
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001772 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001773
1774 if (ret < 0) {
1775 printk(KERN_INFO "%s: can't register video device\n",
1776 dev->name);
1777 return ret;
1778 }
1779
1780 printk(KERN_INFO "%s: registered device %s\n",
1781 dev->name, video_device_node_name(dev->vfd));
1782
Stefan Ringel14169102011-05-09 16:53:49 -03001783 if (dev->caps.has_radio) {
1784 dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
1785 "radio");
1786 if (!dev->radio_dev) {
1787 printk(KERN_INFO "%s: can't register radio device\n",
1788 dev->name);
Peter Senna Tschudin97b55f62012-09-06 11:23:49 -03001789 ret = -ENXIO;
Stefan Ringel14169102011-05-09 16:53:49 -03001790 return ret; /* FIXME release resource */
1791 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001792
Stefan Ringel14169102011-05-09 16:53:49 -03001793 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1794 radio_nr);
1795 if (ret < 0) {
1796 printk(KERN_INFO "%s: can't register radio device\n",
1797 dev->name);
1798 return ret; /* FIXME release resource */
1799 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001800
Stefan Ringel14169102011-05-09 16:53:49 -03001801 printk(KERN_INFO "%s: registered device %s\n",
1802 dev->name, video_device_node_name(dev->radio_dev));
1803 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001804
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001805 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001806 return ret;
1807}
1808
1809int tm6000_v4l2_unregister(struct tm6000_core *dev)
1810{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001811 video_unregister_device(dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001812
Julian Scheel16427fa2012-10-04 10:04:28 -03001813 /* if URB buffers are still allocated free them now */
1814 tm6000_free_urb_buffers(dev);
1815
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001816 if (dev->radio_dev) {
1817 if (video_is_registered(dev->radio_dev))
1818 video_unregister_device(dev->radio_dev);
1819 else
1820 video_device_release(dev->radio_dev);
1821 dev->radio_dev = NULL;
1822 }
1823
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001824 return 0;
1825}
1826
1827int tm6000_v4l2_exit(void)
1828{
1829 return 0;
1830}
1831
1832module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001833MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001834
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001835module_param_named(debug, tm6000_debug, int, 0444);
1836MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001837
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001838module_param(vid_limit, int, 0644);
1839MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001840
Julian Scheel16427fa2012-10-04 10:04:28 -03001841module_param(keep_urb, bool, 0);
1842MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");