blob: 4329fbcf2de2c8c3f54c3cfc220c1f3842c7a5ac [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
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030066static struct tm6000_fmt format[] = {
67 {
68 .name = "4:2:2, packed, YVY2",
69 .fourcc = V4L2_PIX_FMT_YUYV,
70 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030071 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030072 .name = "4:2:2, packed, UYVY",
73 .fourcc = V4L2_PIX_FMT_UYVY,
74 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030075 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030076 .name = "A/V + VBI mux packet",
77 .fourcc = V4L2_PIX_FMT_TM6000,
78 .depth = 16,
79 }
80};
81
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030082/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -030083 * DMA and thread functions
84 * ------------------------------------------------------------------
85 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030086
87#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -030088#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030089
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030090#define norm_minw(a) norm_maxw(a)
91#define norm_minh(a) norm_maxh(a)
92
93/*
94 * video-buf generic routine to get the next available buffer
95 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -030096static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030097 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030098{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030099 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300100
101 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300102 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300103 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300104 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300105 }
106
107 *buf = list_entry(dma_q->active.next,
108 struct tm6000_buffer, vb.queue);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300109}
110
111/*
112 * Announces that a buffer were filled and request the next
113 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300114static inline void buffer_filled(struct tm6000_core *dev,
115 struct tm6000_dmaqueue *dma_q,
116 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300117{
118 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300119 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300120 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300121 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300122 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300123
124 list_del(&buf->vb.queue);
125 wake_up(&buf->vb.done);
126}
127
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300128/*
129 * Identify the tm5600/6000 buffer header type and properly handles
130 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300131static int copy_streams(u8 *data, unsigned long len,
132 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300133{
134 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300135 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Hans Verkuil81730902012-04-20 07:30:48 -0300136 u8 *ptr = data, *endp = data+len;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300137 unsigned long header = 0;
138 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300139 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300140 struct tm6000_buffer *vbuf = NULL;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300141 char *voutp = NULL;
142 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300143
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300144 if (!dev->radio) {
145 /* get video buffer */
146 get_next_buf(dma_q, &vbuf);
147
148 if (!vbuf)
149 return rc;
150 voutp = videobuf_to_vmalloc(&vbuf->vb);
151
152 if (!voutp)
153 return 0;
154 }
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300155
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300156 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300157 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300158 /* Header */
159 if (dev->isoc_ctl.tmp_buf_len > 0) {
160 /* from last urb or packet */
161 header = dev->isoc_ctl.tmp_buf;
162 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300163 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300164 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300165 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300166 4 - dev->isoc_ctl.tmp_buf_len);
167 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300168 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300169 dev->isoc_ctl.tmp_buf_len = 0;
170 } else {
171 if (ptr + 3 >= endp) {
172 /* have incomplete header */
173 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300174 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300175 dev->isoc_ctl.tmp_buf_len);
176 return rc;
177 }
178 /* Seek for sync */
179 for (; ptr < endp - 3; ptr++) {
180 if (*(ptr + 3) == 0x47)
181 break;
182 }
183 /* Get message header */
184 header = *(unsigned long *)ptr;
185 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300186 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300187
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300188 /* split the header fields */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300189 size = ((header & 0x7e) << 1);
190 if (size > 0)
191 size -= 4;
192 block = (header >> 7) & 0xf;
193 field = (header >> 11) & 0x1;
194 line = (header >> 12) & 0x1ff;
195 cmd = (header >> 21) & 0x7;
196 /* Validates haeder fields */
197 if (size > TM6000_URB_MSG_LEN)
198 size = TM6000_URB_MSG_LEN;
199 pktsize = TM6000_URB_MSG_LEN;
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300200 /*
201 * calculate position in buffer and change the buffer
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300202 */
203 switch (cmd) {
204 case TM6000_URB_MSG_VIDEO:
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300205 if (!dev->radio) {
206 if ((dev->isoc_ctl.vfield != field) &&
207 (field == 1)) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300208 /*
209 * Announces that a new buffer
210 * were filled
211 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300212 buffer_filled(dev, dma_q, vbuf);
213 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300214 "new buffer filled\n");
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300215 get_next_buf(dma_q, &vbuf);
216 if (!vbuf)
217 return rc;
218 voutp = videobuf_to_vmalloc(&vbuf->vb);
219 if (!voutp)
220 return rc;
221 memset(voutp, 0, vbuf->vb.size);
222 }
223 linewidth = vbuf->vb.width << 1;
224 pos = ((line << 1) - field - 1) *
225 linewidth + block * TM6000_URB_MSG_LEN;
226 /* Don't allow to write out of the buffer */
227 if (pos + size > vbuf->vb.size)
228 cmd = TM6000_URB_MSG_ERR;
229 dev->isoc_ctl.vfield = field;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300230 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300231 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300232 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300233 break;
234 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300235 case TM6000_URB_MSG_PTS:
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300236 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300237 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300238 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300239 } else {
240 /* Continue the last copy */
241 cmd = dev->isoc_ctl.cmd;
242 size = dev->isoc_ctl.size;
243 pos = dev->isoc_ctl.pos;
244 pktsize = dev->isoc_ctl.pktsize;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300245 field = dev->isoc_ctl.field;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300246 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300247 cpysize = (endp - ptr > size) ? size : endp - ptr;
248 if (cpysize) {
249 /* copy data in different buffers */
250 switch (cmd) {
251 case TM6000_URB_MSG_VIDEO:
252 /* Fills video buffer */
253 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300254 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300255 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300256 case TM6000_URB_MSG_AUDIO: {
257 int i;
258 for (i = 0; i < cpysize; i += 2)
259 swab16s((u16 *)(ptr + i));
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300260
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300261 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300262 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300263 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300264 case TM6000_URB_MSG_VBI:
265 /* Need some code to copy vbi buffer */
266 break;
Stefan Ringel2f349da2011-05-09 16:54:02 -0300267 case TM6000_URB_MSG_PTS: {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300268 /* Need some code to copy pts */
Stefan Ringel2f349da2011-05-09 16:54:02 -0300269 u32 pts;
270 pts = *(u32 *)ptr;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300271 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
272 field, pts);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300273 break;
274 }
Stefan Ringel2f349da2011-05-09 16:54:02 -0300275 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300276 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300277 if (ptr + pktsize > endp) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300278 /*
279 * End of URB packet, but cmd processing is not
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300280 * complete. Preserve the state for a next packet
281 */
282 dev->isoc_ctl.pos = pos + cpysize;
283 dev->isoc_ctl.size = size - cpysize;
284 dev->isoc_ctl.cmd = cmd;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300285 dev->isoc_ctl.field = field;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300286 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300287 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300288 } else {
289 dev->isoc_ctl.cmd = 0;
290 ptr += pktsize;
291 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300292 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300293 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300294}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300295
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300296/*
297 * Identify the tm5600/6000 buffer header type and properly handles
298 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300299static int copy_multiplexed(u8 *ptr, unsigned long len,
300 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300301{
302 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300303 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
304 unsigned int pos = dev->isoc_ctl.pos, cpysize;
305 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300306 struct tm6000_buffer *buf;
307 char *outp = NULL;
308
309 get_next_buf(dma_q, &buf);
310 if (buf)
311 outp = videobuf_to_vmalloc(&buf->vb);
312
313 if (!outp)
314 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300315
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300316 while (len > 0) {
317 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300318 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300319 pos += cpysize;
320 ptr += cpysize;
321 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300322 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300323 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300324 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300325 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300326 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300327 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300328 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300329 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300330 outp = videobuf_to_vmalloc(&(buf->vb));
331 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300332 return rc;
333 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300334 }
335 }
336
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300337 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300338 return rc;
339}
340
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300341static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300342 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300343{
344 char *errmsg = "Unknown";
345
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300346 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300347 case -ENOENT:
348 errmsg = "unlinked synchronuously";
349 break;
350 case -ECONNRESET:
351 errmsg = "unlinked asynchronuously";
352 break;
353 case -ENOSR:
354 errmsg = "Buffer error (overrun)";
355 break;
356 case -EPIPE:
357 errmsg = "Stalled (device not responding)";
358 break;
359 case -EOVERFLOW:
360 errmsg = "Babble (bad cable?)";
361 break;
362 case -EPROTO:
363 errmsg = "Bit-stuff error (bad cable?)";
364 break;
365 case -EILSEQ:
366 errmsg = "CRC/Timeout (could be anything)";
367 break;
368 case -ETIME:
369 errmsg = "Device does not respond";
370 break;
371 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300372 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300373 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
374 status, errmsg);
375 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300376 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300377 packet, status, errmsg);
378 }
379}
380
381
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300382/*
383 * Controls the isoc copy of each urb packet
384 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300385static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300386{
387 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300388 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
389 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300390 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300391
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300392 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300393 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300394 return 0;
395 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300396
397 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300398 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300399
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300400 if (status < 0) {
401 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300402 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300403 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300404
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300405 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300406
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300407 if (len > 0) {
408 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300409 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300410 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
411 rc = copy_multiplexed(p, len, urb);
412 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300413 return rc;
414 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300415 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300416 }
417 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300418 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300419 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300420 return rc;
421}
422
423/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300424 * URB control
425 * ------------------------------------------------------------------
426 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300427
428/*
429 * IRQ callback, called by URB callback
430 */
431static void tm6000_irq_callback(struct urb *urb)
432{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300433 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300434 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300435 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300436
Thierry Reding14f09152011-08-04 04:14:06 -0300437 switch (urb->status) {
438 case 0:
439 case -ETIMEDOUT:
440 break;
441
442 case -ECONNRESET:
443 case -ENOENT:
444 case -ESHUTDOWN:
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300445 return;
446
Thierry Reding14f09152011-08-04 04:14:06 -0300447 default:
448 tm6000_err("urb completion error %d.\n", urb->status);
449 break;
450 }
451
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300452 spin_lock(&dev->slock);
453 tm6000_isoc_copy(urb);
454 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300455
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300456 /* Reset urb buffers */
457 for (i = 0; i < urb->number_of_packets; i++) {
458 urb->iso_frame_desc[i].status = 0;
459 urb->iso_frame_desc[i].actual_length = 0;
460 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300461
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300462 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
463 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300464 tm6000_err("urb resubmit failed (error=%i)\n",
465 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300466}
467
468/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300469 * Allocate URB buffers
470 */
471static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
472{
473 int num_bufs = TM6000_NUM_URB_BUF;
474 int i;
475
476 if (dev->urb_buffer != NULL)
477 return 0;
478
479 dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
480 if (!dev->urb_buffer) {
481 tm6000_err("cannot allocate memory for urb buffers\n");
482 return -ENOMEM;
483 }
484
485 dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
486 if (!dev->urb_dma) {
487 tm6000_err("cannot allocate memory for urb dma pointers\n");
488 return -ENOMEM;
489 }
490
491 for (i = 0; i < num_bufs; i++) {
492 dev->urb_buffer[i] = usb_alloc_coherent(
493 dev->udev, dev->urb_size,
494 GFP_KERNEL, &dev->urb_dma[i]);
495 if (!dev->urb_buffer[i]) {
496 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
497 dev->urb_size, i);
498 return -ENOMEM;
499 }
500 memset(dev->urb_buffer[i], 0, dev->urb_size);
501 }
502
503 return 0;
504}
505
506/*
507 * Free URB buffers
508 */
509static int tm6000_free_urb_buffers(struct tm6000_core *dev)
510{
511 int i;
512
513 if (dev->urb_buffer == NULL)
514 return 0;
515
516 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
517 if (dev->urb_buffer[i]) {
518 usb_free_coherent(dev->udev,
519 dev->urb_size,
520 dev->urb_buffer[i],
521 dev->urb_dma[i]);
522 dev->urb_buffer[i] = NULL;
523 }
524 }
525 kfree(dev->urb_buffer);
526 kfree(dev->urb_dma);
527 dev->urb_buffer = NULL;
528 dev->urb_dma = NULL;
529
530 return 0;
531}
532
533/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300534 * Stop and Deallocate URBs
535 */
536static void tm6000_uninit_isoc(struct tm6000_core *dev)
537{
538 struct urb *urb;
539 int i;
540
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300541 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300542 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300543 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300544 if (urb) {
545 usb_kill_urb(urb);
546 usb_unlink_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300547 usb_free_urb(urb);
548 dev->isoc_ctl.urb[i] = NULL;
549 }
550 dev->isoc_ctl.transfer_buffer[i] = NULL;
551 }
552
Julian Scheel16427fa2012-10-04 10:04:28 -0300553 if (!keep_urb)
554 tm6000_free_urb_buffers(dev);
555
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300556 kfree(dev->isoc_ctl.urb);
557 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300558
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300559 dev->isoc_ctl.urb = NULL;
560 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300561 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300562}
563
564/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300565 * Assign URBs and start IRQ
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300566 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300567static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300568{
569 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Julian Scheel16427fa2012-10-04 10:04:28 -0300570 int i, j, sb_size, pipe, size, max_packets;
571 int num_bufs = TM6000_NUM_URB_BUF;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300572 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300573
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300574 /* De-allocates all pending stuff */
575 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300576 /* Stop interrupt USB pipe */
577 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300578
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300579 usb_set_interface(dev->udev,
580 dev->isoc_in.bInterfaceNumber,
581 dev->isoc_in.bAlternateSetting);
582
Dmitri Belimov641d2112010-12-22 05:57:46 -0300583 /* Start interrupt USB pipe */
584 tm6000_ir_int_start(dev);
585
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300586 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300587 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300588 USB_ENDPOINT_NUMBER_MASK);
589
590 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
591
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300592 if (size > dev->isoc_in.maxsize)
593 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300594
595 dev->isoc_ctl.max_pkt_size = size;
596
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300597 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300598 sb_size = max_packets * size;
Julian Scheel16427fa2012-10-04 10:04:28 -0300599 dev->urb_size = sb_size;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300600
601 dev->isoc_ctl.num_bufs = num_bufs;
602
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300603 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300604 if (!dev->isoc_ctl.urb) {
605 tm6000_err("cannot alloc memory for usb buffers\n");
606 return -ENOMEM;
607 }
608
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300609 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300610 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300611 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300612 tm6000_err("cannot allocate memory for usbtransfer\n");
613 kfree(dev->isoc_ctl.urb);
614 return -ENOMEM;
615 }
616
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300617 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
618 " (%d bytes) of %d bytes each to handle %u size\n",
619 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300620 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300621
Julian Scheel16427fa2012-10-04 10:04:28 -0300622
623 if (!dev->urb_buffer && tm6000_alloc_urb_buffers(dev) < 0) {
624 tm6000_err("cannot allocate memory for urb buffers\n");
625
626 /* call free, as some buffers might have been allocated */
627 tm6000_free_urb_buffers(dev);
628 kfree(dev->isoc_ctl.urb);
629 kfree(dev->isoc_ctl.transfer_buffer);
630 return -ENOMEM;
631 }
632
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300633 /* allocate urbs and transfer buffers */
634 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
635 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
636 if (!urb) {
637 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
638 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehabc1a16412007-10-15 15:43:50 -0300639 usb_free_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300640 return -ENOMEM;
641 }
642 dev->isoc_ctl.urb[i] = urb;
643
Julian Scheel16427fa2012-10-04 10:04:28 -0300644 urb->transfer_dma = dev->urb_dma[i];
645 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300646
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300647 usb_fill_bulk_urb(urb, dev->udev, pipe,
648 dev->isoc_ctl.transfer_buffer[i], sb_size,
649 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300650 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300651 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300652 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300653
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300654 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300655 urb->iso_frame_desc[j].offset = size * j;
656 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300657 }
658 }
659
660 return 0;
661}
662
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300663static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300664{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300665 struct tm6000_dmaqueue *dma_q = &dev->vidq;
666 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300667
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300668 dma_q->frame = 0;
669 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300670
671 init_waitqueue_head(&dma_q->wq);
672
673 /* submit urbs and enables IRQ */
674 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300675 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300676 if (rc) {
677 tm6000_err("submit of urb %i failed (error=%i)\n", i,
678 rc);
679 tm6000_uninit_isoc(dev);
680 return rc;
681 }
682 }
683
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300684 return 0;
685}
686
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300687/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300688 * Videobuf operations
689 * ------------------------------------------------------------------
690 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300691
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300692static int
693buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
694{
695 struct tm6000_fh *fh = vq->priv_data;
696
697 *size = fh->fmt->depth * fh->width * fh->height >> 3;
698 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300699 *count = TM6000_DEF_BUF;
700
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300701 if (*count < TM6000_MIN_BUF)
702 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300703
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300704 while (*size * *count > vid_limit * 1024 * 1024)
705 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300706
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300707 return 0;
708}
709
710static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
711{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300712 struct tm6000_fh *fh = vq->priv_data;
713 struct tm6000_core *dev = fh->dev;
714 unsigned long flags;
715
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300716 if (in_interrupt())
717 BUG();
718
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300719 /* We used to wait for the buffer to finish here, but this didn't work
720 because, as we were keeping the state as VIDEOBUF_QUEUED,
721 videobuf_queue_cancel marked it as finished for us.
722 (Also, it could wedge forever if the hardware was misconfigured.)
723
724 This should be safe; by the time we get here, the buffer isn't
725 queued anymore. If we ever start marking the buffers as
726 VIDEOBUF_ACTIVE, it won't be, though.
727 */
728 spin_lock_irqsave(&dev->slock, flags);
729 if (dev->isoc_ctl.buf == buf)
730 dev->isoc_ctl.buf = NULL;
731 spin_unlock_irqrestore(&dev->slock, flags);
732
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300733 videobuf_vmalloc_free(&buf->vb);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300734 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300735}
736
737static int
738buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
739 enum v4l2_field field)
740{
741 struct tm6000_fh *fh = vq->priv_data;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300742 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300743 struct tm6000_core *dev = fh->dev;
Thierry Reding88e834a2011-08-04 04:14:12 -0300744 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300745
746 BUG_ON(NULL == fh->fmt);
747
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300748
749 /* FIXME: It assumes depth=2 */
750 /* The only currently supported format is 16 bits/pixel */
751 buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3;
752 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
753 return -EINVAL;
754
755 if (buf->fmt != fh->fmt ||
756 buf->vb.width != fh->width ||
757 buf->vb.height != fh->height ||
758 buf->vb.field != field) {
759 buf->fmt = fh->fmt;
760 buf->vb.width = fh->width;
761 buf->vb.height = fh->height;
762 buf->vb.field = field;
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300763 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300764 }
765
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300766 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Curtis McEnroeed7c2212011-06-01 22:21:56 -0400767 rc = videobuf_iolock(vq, &buf->vb, NULL);
768 if (rc != 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300769 goto fail;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300770 }
771
Thierry Reding88e834a2011-08-04 04:14:12 -0300772 if (!dev->isoc_ctl.num_bufs) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300773 rc = tm6000_prepare_isoc(dev);
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300774 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300775 goto fail;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300776
777 rc = tm6000_start_thread(dev);
778 if (rc < 0)
779 goto fail;
780
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300781 }
782
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300783 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300784 return 0;
785
786fail:
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300787 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300788 return rc;
789}
790
791static void
792buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
793{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300794 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300795 struct tm6000_fh *fh = vq->priv_data;
796 struct tm6000_core *dev = fh->dev;
797 struct tm6000_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300798
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300799 buf->vb.state = VIDEOBUF_QUEUED;
800 list_add_tail(&buf->vb.queue, &vidq->active);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300801}
802
803static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
804{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300805 struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300806
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300807 free_buffer(vq, buf);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300808}
809
810static struct videobuf_queue_ops tm6000_video_qops = {
811 .buf_setup = buffer_setup,
812 .buf_prepare = buffer_prepare,
813 .buf_queue = buffer_queue,
814 .buf_release = buffer_release,
815};
816
817/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300818 * IOCTL handling
819 * ------------------------------------------------------------------
820 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300821
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300822static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300823{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300824 /* Is the current fh handling it? if so, that's OK */
825 if (dev->resources == fh && dev->is_res_read)
826 return true;
827
828 return false;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300829}
830
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300831static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300832{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300833 /* Is the current fh handling it? if so, that's OK */
834 if (dev->resources == fh)
835 return true;
836
837 return false;
838}
839
840static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh,
841 bool is_res_read)
842{
843 /* Is the current fh handling it? if so, that's OK */
844 if (dev->resources == fh && dev->is_res_read == is_res_read)
845 return true;
846
847 /* is it free? */
848 if (dev->resources)
849 return false;
850
851 /* grab it */
852 dev->resources = fh;
853 dev->is_res_read = is_res_read;
854 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n");
855 return true;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300856}
857
858static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh)
859{
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -0300860 /* Is the current fh handling it? if so, that's OK */
861 if (dev->resources != fh)
862 return;
863
864 dev->resources = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300865 dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300866}
867
868/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300869 * IOCTL vidioc handling
870 * ------------------------------------------------------------------
871 */
872static int vidioc_querycap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300873 struct v4l2_capability *cap)
874{
Stefan Ringel886a3c02011-05-09 16:53:50 -0300875 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300876 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300877
878 strlcpy(cap->driver, "tm6000", sizeof(cap->driver));
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300879 strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card));
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300880 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Stefan Ringel886a3c02011-05-09 16:53:50 -0300881 if (dev->tuner_type != TUNER_ABSENT)
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300882 cap->device_caps |= V4L2_CAP_TUNER;
883 if (vdev->vfl_type == VFL_TYPE_GRABBER)
884 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
885 V4L2_CAP_STREAMING |
886 V4L2_CAP_READWRITE;
887 else
888 cap->device_caps |= V4L2_CAP_RADIO;
889 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
890 V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
Stefan Ringel886a3c02011-05-09 16:53:50 -0300891
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300892 return 0;
893}
894
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300895static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300896 struct v4l2_fmtdesc *f)
897{
Hans Verkuil2c2a0532012-09-11 07:35:30 -0300898 if (f->index >= ARRAY_SIZE(format))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300899 return -EINVAL;
900
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300901 strlcpy(f->description, format[f->index].name, sizeof(f->description));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300902 f->pixelformat = format[f->index].fourcc;
903 return 0;
904}
905
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300906static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300907 struct v4l2_format *f)
908{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300909 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300910
911 f->fmt.pix.width = fh->width;
912 f->fmt.pix.height = fh->height;
913 f->fmt.pix.field = fh->vb_vidq.field;
914 f->fmt.pix.pixelformat = fh->fmt->fourcc;
915 f->fmt.pix.bytesperline =
916 (f->fmt.pix.width * fh->fmt->depth) >> 3;
917 f->fmt.pix.sizeimage =
918 f->fmt.pix.height * f->fmt.pix.bytesperline;
919
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300920 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300921}
922
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300923static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300924{
925 unsigned int i;
926
927 for (i = 0; i < ARRAY_SIZE(format); i++)
928 if (format[i].fourcc == fourcc)
929 return format+i;
930 return NULL;
931}
932
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300933static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300934 struct v4l2_format *f)
935{
936 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
937 struct tm6000_fmt *fmt;
938 enum v4l2_field field;
939
940 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
941 if (NULL == fmt) {
942 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)"
943 " invalid.\n", f->fmt.pix.pixelformat);
944 return -EINVAL;
945 }
946
947 field = f->fmt.pix.field;
948
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300949 if (field == V4L2_FIELD_ANY)
950 field = V4L2_FIELD_SEQ_TB;
951 else if (V4L2_FIELD_INTERLACED != field) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300952 dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n");
953 return -EINVAL;
954 }
955
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300956 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300957
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300958 f->fmt.pix.width = dev->width;
959 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300960
961 f->fmt.pix.width &= ~0x01;
962
963 f->fmt.pix.field = field;
964
965 f->fmt.pix.bytesperline =
966 (f->fmt.pix.width * fmt->depth) >> 3;
967 f->fmt.pix.sizeimage =
968 f->fmt.pix.height * f->fmt.pix.bytesperline;
969
970 return 0;
971}
972
973/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300974static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300975 struct v4l2_format *f)
976{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300977 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300978 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300979 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300980 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300981 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300982
983 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
984 fh->width = f->fmt.pix.width;
985 fh->height = f->fmt.pix.height;
986 fh->vb_vidq.field = f->fmt.pix.field;
987 fh->type = f->type;
988
989 dev->fourcc = f->fmt.pix.pixelformat;
990
991 tm6000_set_fourcc_format(dev);
992
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300993 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300994}
995
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300996static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300997 struct v4l2_requestbuffers *p)
998{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300999 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001000
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001001 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001002}
1003
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001004static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001005 struct v4l2_buffer *p)
1006{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001007 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001008
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001009 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001010}
1011
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001012static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001013{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001014 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001015
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001016 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001017}
1018
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001019static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001020{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001021 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001022
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001023 return videobuf_dqbuf(&fh->vb_vidq, p,
1024 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001025}
1026
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001027static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1028{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001029 struct tm6000_fh *fh = priv;
1030 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001031
1032 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1033 return -EINVAL;
1034 if (i != fh->type)
1035 return -EINVAL;
1036
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001037 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001038 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001039 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001040}
1041
1042static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1043{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001044 struct tm6000_fh *fh = priv;
1045 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001046
1047 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1048 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001049
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001050 if (i != fh->type)
1051 return -EINVAL;
1052
1053 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001054 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001055
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001056 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001057}
1058
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001059static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001060{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001061 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001062 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001063 struct tm6000_core *dev = fh->dev;
1064
Stefan Ringel4f526102010-10-27 16:48:05 -03001065 dev->norm = *norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001066 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001067
1068 fh->width = dev->width;
1069 fh->height = dev->height;
1070
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001071 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001072 return rc;
1073
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001074 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001075
1076 return 0;
1077}
1078
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001079static const char *iname[] = {
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001080 [TM6000_INPUT_TV] = "Television",
1081 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1082 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1083 [TM6000_INPUT_SVIDEO] = "S-Video",
1084};
1085
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001086static int vidioc_enum_input(struct file *file, void *priv,
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001087 struct v4l2_input *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001088{
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001089 struct tm6000_fh *fh = priv;
1090 struct tm6000_core *dev = fh->dev;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001091 unsigned int n;
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001092
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001093 n = i->index;
1094 if (n >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001095 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001096
1097 if (!dev->vinput[n].type)
1098 return -EINVAL;
1099
1100 i->index = n;
1101
1102 if (dev->vinput[n].type == TM6000_INPUT_TV)
1103 i->type = V4L2_INPUT_TYPE_TUNER;
1104 else
1105 i->type = V4L2_INPUT_TYPE_CAMERA;
1106
1107 strcpy(i->name, iname[dev->vinput[n].type]);
1108
1109 i->std = TM6000_STD;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001110
1111 return 0;
1112}
1113
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001114static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001115{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001116 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001117 struct tm6000_core *dev = fh->dev;
1118
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001119 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001120
1121 return 0;
1122}
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001123
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001124static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001125{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001126 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001127 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001128 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001129
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001130 if (i >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001131 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001132 if (!dev->vinput[i].type)
1133 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001134
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001135 dev->input = i;
1136
1137 rc = vidioc_s_std(file, priv, &dev->vfd->current_norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001138
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001139 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001140}
1141
Stefan Ringel886a3c02011-05-09 16:53:50 -03001142/* --- controls ---------------------------------------------- */
Hans Verkuil9f747352013-01-31 08:23:01 -03001143
1144static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001145{
Hans Verkuil9f747352013-01-31 08:23:01 -03001146 struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1147 u8 val = ctrl->val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001148
1149 switch (ctrl->id) {
1150 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001151 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001152 return 0;
1153 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001154 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001155 return 0;
1156 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001157 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001158 return 0;
1159 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001160 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001161 return 0;
Hans Verkuil9f747352013-01-31 08:23:01 -03001162 }
1163 return -EINVAL;
1164}
1165
1166static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1167 .s_ctrl = tm6000_s_ctrl,
1168};
1169
1170static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1171{
1172 struct tm6000_core *dev = container_of(ctrl->handler,
1173 struct tm6000_core, radio_ctrl_handler);
1174 u8 val = ctrl->val;
1175
1176 switch (ctrl->id) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001177 case V4L2_CID_AUDIO_MUTE:
1178 dev->ctl_mute = val;
1179 tm6000_tvaudio_set_mute(dev, val);
1180 return 0;
1181 case V4L2_CID_AUDIO_VOLUME:
1182 dev->ctl_volume = val;
1183 tm6000_set_volume(dev, val);
1184 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001185 }
1186 return -EINVAL;
1187}
1188
Hans Verkuil9f747352013-01-31 08:23:01 -03001189static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1190 .s_ctrl = tm6000_radio_s_ctrl,
1191};
1192
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001193static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001194 struct v4l2_tuner *t)
1195{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001196 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001197 struct tm6000_core *dev = fh->dev;
1198
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001199 if (UNSET == dev->tuner_type)
1200 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001201 if (0 != t->index)
1202 return -EINVAL;
1203
1204 strcpy(t->name, "Television");
1205 t->type = V4L2_TUNER_ANALOG_TV;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001206 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001207 t->rangehigh = 0xffffffffUL;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001208 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1209
1210 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1211
1212 t->audmode = dev->amode;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001213
1214 return 0;
1215}
1216
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001217static int vidioc_s_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001218 struct v4l2_tuner *t)
1219{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001220 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001221 struct tm6000_core *dev = fh->dev;
1222
1223 if (UNSET == dev->tuner_type)
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001224 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001225 if (0 != t->index)
1226 return -EINVAL;
1227
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001228 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1229 dev->amode = V4L2_TUNER_MODE_STEREO;
1230 else
1231 dev->amode = t->audmode;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001232 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1233
1234 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001235
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001236 return 0;
1237}
1238
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001239static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001240 struct v4l2_frequency *f)
1241{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001242 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001243 struct tm6000_core *dev = fh->dev;
1244
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001245 if (UNSET == dev->tuner_type)
1246 return -ENOTTY;
1247 if (f->tuner)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001248 return -EINVAL;
1249
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001250 f->frequency = dev->freq;
1251
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001252 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001253
1254 return 0;
1255}
1256
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001257static int vidioc_s_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001258 struct v4l2_frequency *f)
1259{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001260 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001261 struct tm6000_core *dev = fh->dev;
1262
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001263 if (UNSET == dev->tuner_type)
1264 return -ENOTTY;
1265 if (f->tuner != 0)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001266 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001267
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001268 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001269 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001270
1271 return 0;
1272}
1273
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001274static int radio_g_tuner(struct file *file, void *priv,
1275 struct v4l2_tuner *t)
1276{
1277 struct tm6000_fh *fh = file->private_data;
1278 struct tm6000_core *dev = fh->dev;
1279
1280 if (0 != t->index)
1281 return -EINVAL;
1282
1283 memset(t, 0, sizeof(*t));
1284 strcpy(t->name, "Radio");
1285 t->type = V4L2_TUNER_RADIO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001286 t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001287 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001288 t->audmode = V4L2_TUNER_MODE_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001289
1290 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1291
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001292 return 0;
1293}
1294
1295static int radio_s_tuner(struct file *file, void *priv,
1296 struct v4l2_tuner *t)
1297{
1298 struct tm6000_fh *fh = file->private_data;
1299 struct tm6000_core *dev = fh->dev;
1300
1301 if (0 != t->index)
1302 return -EINVAL;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001303 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1304 t->audmode = V4L2_TUNER_MODE_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001305
1306 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1307
1308 return 0;
1309}
1310
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001311/* ------------------------------------------------------------------
1312 File operations for the device
1313 ------------------------------------------------------------------*/
1314
Hans Verkuil14a09da2012-06-24 07:26:46 -03001315static int __tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001316{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001317 struct video_device *vdev = video_devdata(file);
1318 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001319 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001320 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil9f747352013-01-31 08:23:01 -03001321 int rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001322 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001323
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001324 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1325 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001326
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001327 switch (vdev->vfl_type) {
1328 case VFL_TYPE_GRABBER:
1329 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1330 break;
1331 case VFL_TYPE_VBI:
1332 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1333 break;
1334 case VFL_TYPE_RADIO:
1335 radio = 1;
1336 break;
1337 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001338
1339 /* If more than one user, mutex should be added */
1340 dev->users++;
1341
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001342 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1343 video_device_node_name(vdev), v4l2_type_names[type],
1344 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001345
1346 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001347 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001348 if (NULL == fh) {
1349 dev->users--;
1350 return -ENOMEM;
1351 }
1352
1353 file->private_data = fh;
1354 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001355 fh->radio = radio;
1356 dev->radio = radio;
1357 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001358 dev->fourcc = format[0].fourcc;
1359
1360 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001361
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001362 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001363
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001364 fh->width = dev->width;
1365 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001366
1367 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1368 "dev->vidq=0x%08lx\n",
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001369 (unsigned long)fh, (unsigned long)dev,
1370 (unsigned long)&dev->vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001371 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001372 "queued=%d\n", list_empty(&dev->vidq.queued));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001373 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001374 "active=%d\n", list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001375
1376 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001377 rc = tm6000_init_analog_mode(dev);
1378 if (rc < 0)
1379 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001380
Hans Verkuil9f747352013-01-31 08:23:01 -03001381 dev->mode = TM6000_MODE_ANALOG;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001382
Thierry Redingaa4a5832011-08-04 04:14:15 -03001383 if (!fh->radio) {
1384 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1385 NULL, &dev->slock,
1386 fh->type,
1387 V4L2_FIELD_INTERLACED,
1388 sizeof(struct tm6000_buffer), fh, &dev->lock);
1389 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001390 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001391 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001392 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1393 tm6000_prepare_isoc(dev);
1394 tm6000_start_thread(dev);
1395 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001396
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001397 return 0;
1398}
1399
Hans Verkuil14a09da2012-06-24 07:26:46 -03001400static int tm6000_open(struct file *file)
1401{
1402 struct video_device *vdev = video_devdata(file);
1403 int res;
1404
1405 mutex_lock(vdev->lock);
1406 res = __tm6000_open(file);
1407 mutex_unlock(vdev->lock);
1408 return res;
1409}
1410
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001411static ssize_t
1412tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1413{
Hans Verkuil14a09da2012-06-24 07:26:46 -03001414 struct tm6000_fh *fh = file->private_data;
1415 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001416
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001417 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil14a09da2012-06-24 07:26:46 -03001418 int res;
1419
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001420 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001421 return -EBUSY;
1422
Hans Verkuil14a09da2012-06-24 07:26:46 -03001423 if (mutex_lock_interruptible(&dev->lock))
1424 return -ERESTARTSYS;
1425 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001426 file->f_flags & O_NONBLOCK);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001427 mutex_unlock(&dev->lock);
1428 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001429 }
1430 return 0;
1431}
1432
1433static unsigned int
Hans Verkuil14a09da2012-06-24 07:26:46 -03001434__tm6000_poll(struct file *file, struct poll_table_struct *wait)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001435{
1436 struct tm6000_fh *fh = file->private_data;
1437 struct tm6000_buffer *buf;
1438
1439 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1440 return POLLERR;
1441
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001442 if (!!is_res_streaming(fh->dev, fh))
1443 return POLLERR;
1444
1445 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001446 /* streaming capture */
1447 if (list_empty(&fh->vb_vidq.stream))
1448 return POLLERR;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001449 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001450 } else {
1451 /* read() capture */
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001452 return videobuf_poll_stream(file, &fh->vb_vidq, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001453 }
1454 poll_wait(file, &buf->vb.done, wait);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -03001455 if (buf->vb.state == VIDEOBUF_DONE ||
1456 buf->vb.state == VIDEOBUF_ERROR)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001457 return POLLIN | POLLRDNORM;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001458 return 0;
1459}
1460
Hans Verkuil14a09da2012-06-24 07:26:46 -03001461static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1462{
1463 struct tm6000_fh *fh = file->private_data;
1464 struct tm6000_core *dev = fh->dev;
1465 unsigned int res;
1466
1467 mutex_lock(&dev->lock);
1468 res = __tm6000_poll(file, wait);
1469 mutex_unlock(&dev->lock);
1470 return res;
1471}
1472
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001473static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001474{
1475 struct tm6000_fh *fh = file->private_data;
1476 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001477 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001478
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001479 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1480 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001481
Hans Verkuil14a09da2012-06-24 07:26:46 -03001482 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001483 dev->users--;
1484
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001485 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001486
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001487 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001488 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001489
Stefan Ringel8159c182011-11-28 15:46:18 -03001490 /* Stop interrupt USB pipe */
1491 tm6000_ir_int_stop(dev);
1492
1493 usb_reset_configuration(dev->udev);
1494
Thierry Reding4be9c8f2011-12-06 09:39:35 -03001495 if (dev->int_in.endp)
Stefan Ringel8159c182011-11-28 15:46:18 -03001496 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001497 dev->isoc_in.bInterfaceNumber, 2);
Stefan Ringel8159c182011-11-28 15:46:18 -03001498 else
1499 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001500 dev->isoc_in.bInterfaceNumber, 0);
Stefan Ringel8159c182011-11-28 15:46:18 -03001501
1502 /* Start interrupt USB pipe */
1503 tm6000_ir_int_start(dev);
1504
Thierry Redingaa4a5832011-08-04 04:14:15 -03001505 if (!fh->radio)
1506 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001507 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001508
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001509 kfree(fh);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001510 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001511
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001512 return 0;
1513}
1514
1515static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1516{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001517 struct tm6000_fh *fh = file->private_data;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001518 struct tm6000_core *dev = fh->dev;
1519 int res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001520
Hans Verkuil14a09da2012-06-24 07:26:46 -03001521 if (mutex_lock_interruptible(&dev->lock))
1522 return -ERESTARTSYS;
1523 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1524 mutex_unlock(&dev->lock);
1525 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001526}
1527
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001528static struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001529 .owner = THIS_MODULE,
1530 .open = tm6000_open,
1531 .release = tm6000_release,
1532 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1533 .read = tm6000_read,
1534 .poll = tm6000_poll,
1535 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001536};
1537
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001538static const struct v4l2_ioctl_ops video_ioctl_ops = {
1539 .vidioc_querycap = vidioc_querycap,
1540 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1541 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1542 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1543 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1544 .vidioc_s_std = vidioc_s_std,
1545 .vidioc_enum_input = vidioc_enum_input,
1546 .vidioc_g_input = vidioc_g_input,
1547 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001548 .vidioc_g_tuner = vidioc_g_tuner,
1549 .vidioc_s_tuner = vidioc_s_tuner,
1550 .vidioc_g_frequency = vidioc_g_frequency,
1551 .vidioc_s_frequency = vidioc_s_frequency,
1552 .vidioc_streamon = vidioc_streamon,
1553 .vidioc_streamoff = vidioc_streamoff,
1554 .vidioc_reqbufs = vidioc_reqbufs,
1555 .vidioc_querybuf = vidioc_querybuf,
1556 .vidioc_qbuf = vidioc_qbuf,
1557 .vidioc_dqbuf = vidioc_dqbuf,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001558};
1559
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001560static struct video_device tm6000_template = {
1561 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001562 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001563 .ioctl_ops = &video_ioctl_ops,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001564 .release = video_device_release,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001565 .tvnorms = TM6000_STD,
1566 .current_norm = V4L2_STD_NTSC_M,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001567};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001568
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001569static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001570 .owner = THIS_MODULE,
1571 .open = tm6000_open,
1572 .release = tm6000_release,
1573 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001574};
1575
1576static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001577 .vidioc_querycap = vidioc_querycap,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001578 .vidioc_g_tuner = radio_g_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001579 .vidioc_s_tuner = radio_s_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001580 .vidioc_g_frequency = vidioc_g_frequency,
1581 .vidioc_s_frequency = vidioc_s_frequency,
1582};
1583
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001584static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001585 .name = "tm6000",
1586 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001587 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001588};
1589
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001590/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001591 * Initialization and module stuff
1592 * ------------------------------------------------------------------
1593 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001594
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001595static struct video_device *vdev_init(struct tm6000_core *dev,
1596 const struct video_device
1597 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001598{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001599 struct video_device *vfd;
1600
1601 vfd = video_device_alloc();
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001602 if (NULL == vfd)
1603 return NULL;
1604
1605 *vfd = *template;
1606 vfd->v4l2_dev = &dev->v4l2_dev;
1607 vfd->release = video_device_release;
1608 vfd->debug = tm6000_debug;
1609 vfd->lock = &dev->lock;
1610
1611 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1612
1613 video_set_drvdata(vfd, dev);
1614 return vfd;
1615}
1616
1617int tm6000_v4l2_register(struct tm6000_core *dev)
1618{
Hans Verkuil9f747352013-01-31 08:23:01 -03001619 int ret = 0;
1620
1621 v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1622 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1623 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1624 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1625 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1626 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1627 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1628 V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1629 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1630 V4L2_CID_CONTRAST, 0, 255, 1, 119);
1631 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1632 V4L2_CID_SATURATION, 0, 255, 1, 112);
1633 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1634 V4L2_CID_HUE, -128, 127, 1, 0);
1635 v4l2_ctrl_add_handler(&dev->ctrl_handler,
1636 &dev->radio_ctrl_handler, NULL);
1637
1638 if (dev->radio_ctrl_handler.error)
1639 ret = dev->radio_ctrl_handler.error;
1640 if (!ret && dev->ctrl_handler.error)
1641 ret = dev->ctrl_handler.error;
1642 if (ret)
1643 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001644
1645 dev->vfd = vdev_init(dev, &tm6000_template, "video");
1646
1647 if (!dev->vfd) {
1648 printk(KERN_INFO "%s: can't register video device\n",
1649 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001650 ret = -ENOMEM;
1651 goto free_ctrl;
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001652 }
Hans Verkuil9f747352013-01-31 08:23:01 -03001653 dev->vfd->ctrl_handler = &dev->ctrl_handler;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001654
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001655 /* init video dma queues */
1656 INIT_LIST_HEAD(&dev->vidq.active);
1657 INIT_LIST_HEAD(&dev->vidq.queued);
1658
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001659 ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001660
1661 if (ret < 0) {
1662 printk(KERN_INFO "%s: can't register video device\n",
1663 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001664 video_device_release(dev->vfd);
1665 dev->vfd = NULL;
1666 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001667 }
1668
1669 printk(KERN_INFO "%s: registered device %s\n",
1670 dev->name, video_device_node_name(dev->vfd));
1671
Stefan Ringel14169102011-05-09 16:53:49 -03001672 if (dev->caps.has_radio) {
1673 dev->radio_dev = vdev_init(dev, &tm6000_radio_template,
1674 "radio");
1675 if (!dev->radio_dev) {
1676 printk(KERN_INFO "%s: can't register radio device\n",
1677 dev->name);
Peter Senna Tschudin97b55f62012-09-06 11:23:49 -03001678 ret = -ENXIO;
Hans Verkuil9f747352013-01-31 08:23:01 -03001679 goto unreg_video;
Stefan Ringel14169102011-05-09 16:53:49 -03001680 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001681
Hans Verkuil9f747352013-01-31 08:23:01 -03001682 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
Stefan Ringel14169102011-05-09 16:53:49 -03001683 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1684 radio_nr);
1685 if (ret < 0) {
1686 printk(KERN_INFO "%s: can't register radio device\n",
1687 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001688 video_device_release(dev->radio_dev);
1689 goto unreg_video;
Stefan Ringel14169102011-05-09 16:53:49 -03001690 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001691
Stefan Ringel14169102011-05-09 16:53:49 -03001692 printk(KERN_INFO "%s: registered device %s\n",
1693 dev->name, video_device_node_name(dev->radio_dev));
1694 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001695
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001696 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001697 return ret;
Hans Verkuil9f747352013-01-31 08:23:01 -03001698
1699unreg_video:
1700 video_unregister_device(dev->vfd);
1701free_ctrl:
1702 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1703 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1704 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001705}
1706
1707int tm6000_v4l2_unregister(struct tm6000_core *dev)
1708{
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001709 video_unregister_device(dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001710
Julian Scheel16427fa2012-10-04 10:04:28 -03001711 /* if URB buffers are still allocated free them now */
1712 tm6000_free_urb_buffers(dev);
1713
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001714 if (dev->radio_dev) {
1715 if (video_is_registered(dev->radio_dev))
1716 video_unregister_device(dev->radio_dev);
1717 else
1718 video_device_release(dev->radio_dev);
1719 dev->radio_dev = NULL;
1720 }
1721
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001722 return 0;
1723}
1724
1725int tm6000_v4l2_exit(void)
1726{
1727 return 0;
1728}
1729
1730module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001731MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001732
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001733module_param_named(debug, tm6000_debug, int, 0444);
1734MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001735
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001736module_param(vid_limit, int, 0644);
1737MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001738
Julian Scheel16427fa2012-10-04 10:04:28 -03001739module_param(keep_urb, bool, 0);
1740MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");