blob: fa5e8bda2ae4a7dfea2135fafd25ce02b8a39662 [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>
Hans Verkuil770056c2012-09-11 11:50:37 -030037#include <media/v4l2-event.h>
Stefan Ringel886a3c02011-05-09 16:53:50 -030038#include <media/tuner.h>
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030039#include <linux/interrupt.h>
40#include <linux/kthread.h>
41#include <linux/highmem.h>
42#include <linux/freezer.h>
43
44#include "tm6000-regs.h"
45#include "tm6000.h"
46
47#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
48
Michel Ludwig95a83822007-07-24 08:06:45 -030049/* Limits minimum and default number of buffers */
50#define TM6000_MIN_BUF 4
51#define TM6000_DEF_BUF 8
52
Julian Scheel16427fa2012-10-04 10:04:28 -030053#define TM6000_NUM_URB_BUF 8
54
Stefan Ringel5a4b55e2010-05-19 13:58:27 -030055#define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -030056
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030057/* Declare static vars that will be used as parameters */
58static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
59static int video_nr = -1; /* /dev/videoN, -1 for autodetect */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -030060static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */
Mauro Carvalho Chehabb391b0e2012-12-20 15:21:23 -020061static bool keep_urb; /* keep urb buffers allocated */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030062
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030063/* Debug level */
64int tm6000_debug;
Mauro Carvalho Chehabfaa7c132010-06-04 21:08:25 -030065EXPORT_SYMBOL_GPL(tm6000_debug);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030066
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030067static struct tm6000_fmt format[] = {
68 {
69 .name = "4:2:2, packed, YVY2",
70 .fourcc = V4L2_PIX_FMT_YUYV,
71 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030072 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030073 .name = "4:2:2, packed, UYVY",
74 .fourcc = V4L2_PIX_FMT_UYVY,
75 .depth = 16,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030076 }, {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030077 .name = "A/V + VBI mux packet",
78 .fourcc = V4L2_PIX_FMT_TM6000,
79 .depth = 16,
80 }
81};
82
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030083/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -030084 * DMA and thread functions
85 * ------------------------------------------------------------------
86 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030087
88#define norm_maxw(a) 720
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -030089#define norm_maxh(a) 576
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030090
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030091#define norm_minw(a) norm_maxw(a)
92#define norm_minh(a) norm_maxh(a)
93
94/*
95 * video-buf generic routine to get the next available buffer
96 */
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -030097static inline void get_next_buf(struct tm6000_dmaqueue *dma_q,
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -030098 struct tm6000_buffer **buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -030099{
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300100 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300101
102 if (list_empty(&dma_q->active)) {
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300103 dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n");
Mauro Carvalho Chehab1f9305b2008-11-28 06:44:06 -0300104 *buf = NULL;
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300105 return;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300106 }
107
108 *buf = list_entry(dma_q->active.next,
109 struct tm6000_buffer, vb.queue);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300110}
111
112/*
113 * Announces that a buffer were filled and request the next
114 */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300115static inline void buffer_filled(struct tm6000_core *dev,
116 struct tm6000_dmaqueue *dma_q,
117 struct tm6000_buffer *buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300118{
119 /* Advice that buffer was filled */
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300120 dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i);
Mauro Carvalho Chehab47878f12007-11-15 16:37:35 -0300121 buf->vb.state = VIDEOBUF_DONE;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300122 buf->vb.field_count++;
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300123 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300124
125 list_del(&buf->vb.queue);
126 wake_up(&buf->vb.done);
127}
128
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300129/*
130 * Identify the tm5600/6000 buffer header type and properly handles
131 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300132static int copy_streams(u8 *data, unsigned long len,
133 struct urb *urb)
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300134{
135 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300136 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Hans Verkuil81730902012-04-20 07:30:48 -0300137 u8 *ptr = data, *endp = data+len;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300138 unsigned long header = 0;
139 int rc = 0;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300140 unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300141 struct tm6000_buffer *vbuf = NULL;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300142 char *voutp = NULL;
143 unsigned int linewidth;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300144
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300145 if (!dev->radio) {
146 /* get video buffer */
147 get_next_buf(dma_q, &vbuf);
148
149 if (!vbuf)
150 return rc;
151 voutp = videobuf_to_vmalloc(&vbuf->vb);
152
153 if (!voutp)
154 return 0;
155 }
Mauro Carvalho Chehabe2c95002007-09-19 15:39:22 -0300156
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300157 for (ptr = data; ptr < endp;) {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300158 if (!dev->isoc_ctl.cmd) {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300159 /* Header */
160 if (dev->isoc_ctl.tmp_buf_len > 0) {
161 /* from last urb or packet */
162 header = dev->isoc_ctl.tmp_buf;
163 if (4 - dev->isoc_ctl.tmp_buf_len > 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300164 memcpy((u8 *)&header +
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300165 dev->isoc_ctl.tmp_buf_len,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300166 ptr,
Mauro Carvalho Chehab801dd3b2010-05-02 17:14:33 -0300167 4 - dev->isoc_ctl.tmp_buf_len);
168 ptr += 4 - dev->isoc_ctl.tmp_buf_len;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300169 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300170 dev->isoc_ctl.tmp_buf_len = 0;
171 } else {
172 if (ptr + 3 >= endp) {
173 /* have incomplete header */
174 dev->isoc_ctl.tmp_buf_len = endp - ptr;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300175 memcpy(&dev->isoc_ctl.tmp_buf, ptr,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300176 dev->isoc_ctl.tmp_buf_len);
177 return rc;
178 }
179 /* Seek for sync */
180 for (; ptr < endp - 3; ptr++) {
181 if (*(ptr + 3) == 0x47)
182 break;
183 }
184 /* Get message header */
185 header = *(unsigned long *)ptr;
186 ptr += 4;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300187 }
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300188
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300189 /* split the header fields */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300190 size = ((header & 0x7e) << 1);
191 if (size > 0)
192 size -= 4;
193 block = (header >> 7) & 0xf;
194 field = (header >> 11) & 0x1;
195 line = (header >> 12) & 0x1ff;
196 cmd = (header >> 21) & 0x7;
197 /* Validates haeder fields */
198 if (size > TM6000_URB_MSG_LEN)
199 size = TM6000_URB_MSG_LEN;
200 pktsize = TM6000_URB_MSG_LEN;
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300201 /*
202 * calculate position in buffer and change the buffer
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300203 */
204 switch (cmd) {
205 case TM6000_URB_MSG_VIDEO:
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300206 if (!dev->radio) {
207 if ((dev->isoc_ctl.vfield != field) &&
208 (field == 1)) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300209 /*
210 * Announces that a new buffer
211 * were filled
212 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300213 buffer_filled(dev, dma_q, vbuf);
214 dprintk(dev, V4L2_DEBUG_ISOC,
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300215 "new buffer filled\n");
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300216 get_next_buf(dma_q, &vbuf);
217 if (!vbuf)
218 return rc;
219 voutp = videobuf_to_vmalloc(&vbuf->vb);
220 if (!voutp)
221 return rc;
222 memset(voutp, 0, vbuf->vb.size);
223 }
224 linewidth = vbuf->vb.width << 1;
225 pos = ((line << 1) - field - 1) *
226 linewidth + block * TM6000_URB_MSG_LEN;
227 /* Don't allow to write out of the buffer */
228 if (pos + size > vbuf->vb.size)
229 cmd = TM6000_URB_MSG_ERR;
230 dev->isoc_ctl.vfield = field;
Jarod Wilsoncc73b4b2011-04-11 18:49:24 -0300231 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300232 break;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300233 case TM6000_URB_MSG_VBI:
Mauro Carvalho Chehab23ba9462010-06-07 11:57:28 -0300234 break;
235 case TM6000_URB_MSG_AUDIO:
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300236 case TM6000_URB_MSG_PTS:
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300237 size = pktsize; /* Size is always 180 bytes */
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300238 break;
Mauro Carvalho Chehabcc6c60d2007-09-19 16:24:05 -0300239 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300240 } else {
241 /* Continue the last copy */
242 cmd = dev->isoc_ctl.cmd;
243 size = dev->isoc_ctl.size;
244 pos = dev->isoc_ctl.pos;
245 pktsize = dev->isoc_ctl.pktsize;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300246 field = dev->isoc_ctl.field;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300247 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300248 cpysize = (endp - ptr > size) ? size : endp - ptr;
249 if (cpysize) {
250 /* copy data in different buffers */
251 switch (cmd) {
252 case TM6000_URB_MSG_VIDEO:
253 /* Fills video buffer */
254 if (vbuf)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300255 memcpy(&voutp[pos], ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300256 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300257 case TM6000_URB_MSG_AUDIO: {
258 int i;
259 for (i = 0; i < cpysize; i += 2)
260 swab16s((u16 *)(ptr + i));
Dmitri Belimov73f4d262010-09-20 17:07:15 -0300261
Mauro Carvalho Chehabb17b8692010-06-03 17:16:28 -0300262 tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300263 break;
Stefan Ringel7ecff8c2011-05-09 16:54:00 -0300264 }
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300265 case TM6000_URB_MSG_VBI:
266 /* Need some code to copy vbi buffer */
267 break;
Stefan Ringel2f349da2011-05-09 16:54:02 -0300268 case TM6000_URB_MSG_PTS: {
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300269 /* Need some code to copy pts */
Stefan Ringel2f349da2011-05-09 16:54:02 -0300270 u32 pts;
271 pts = *(u32 *)ptr;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300272 dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x",
273 field, pts);
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300274 break;
275 }
Stefan Ringel2f349da2011-05-09 16:54:02 -0300276 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300277 }
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300278 if (ptr + pktsize > endp) {
Thierry Reding3d1a51d2011-08-04 04:14:01 -0300279 /*
280 * End of URB packet, but cmd processing is not
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300281 * complete. Preserve the state for a next packet
282 */
283 dev->isoc_ctl.pos = pos + cpysize;
284 dev->isoc_ctl.size = size - cpysize;
285 dev->isoc_ctl.cmd = cmd;
Stefan Ringel423c79e2011-05-21 03:05:38 -0300286 dev->isoc_ctl.field = field;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300287 dev->isoc_ctl.pktsize = pktsize - (endp - ptr);
Mauro Carvalho Chehabccfb3022010-06-30 17:24:28 -0300288 ptr += endp - ptr;
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300289 } else {
290 dev->isoc_ctl.cmd = 0;
291 ptr += pktsize;
292 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300293 }
Mauro Carvalho Chehaba2286182007-09-22 02:06:25 -0300294 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300295}
Stefan Ringel83cb9a52010-06-01 12:47:42 -0300296
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300297/*
298 * Identify the tm5600/6000 buffer header type and properly handles
299 */
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300300static int copy_multiplexed(u8 *ptr, unsigned long len,
301 struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300302{
303 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300304 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
305 unsigned int pos = dev->isoc_ctl.pos, cpysize;
306 int rc = 1;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300307 struct tm6000_buffer *buf;
308 char *outp = NULL;
309
310 get_next_buf(dma_q, &buf);
311 if (buf)
312 outp = videobuf_to_vmalloc(&buf->vb);
313
314 if (!outp)
315 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300316
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300317 while (len > 0) {
318 cpysize = min(len, buf->vb.size-pos);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300319 memcpy(&outp[pos], ptr, cpysize);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300320 pos += cpysize;
321 ptr += cpysize;
322 len -= cpysize;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300323 if (pos >= buf->vb.size) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300324 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300325 /* Announces that a new buffer were filled */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300326 buffer_filled(dev, dma_q, buf);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300327 dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n");
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300328 get_next_buf(dma_q, &buf);
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300329 if (!buf)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300330 break;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300331 outp = videobuf_to_vmalloc(&(buf->vb));
332 if (!outp)
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300333 return rc;
334 pos = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300335 }
336 }
337
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300338 dev->isoc_ctl.pos = pos;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300339 return rc;
340}
341
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300342static inline void print_err_status(struct tm6000_core *dev,
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300343 int packet, int status)
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300344{
345 char *errmsg = "Unknown";
346
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300347 switch (status) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300348 case -ENOENT:
349 errmsg = "unlinked synchronuously";
350 break;
351 case -ECONNRESET:
352 errmsg = "unlinked asynchronuously";
353 break;
354 case -ENOSR:
355 errmsg = "Buffer error (overrun)";
356 break;
357 case -EPIPE:
358 errmsg = "Stalled (device not responding)";
359 break;
360 case -EOVERFLOW:
361 errmsg = "Babble (bad cable?)";
362 break;
363 case -EPROTO:
364 errmsg = "Bit-stuff error (bad cable?)";
365 break;
366 case -EILSEQ:
367 errmsg = "CRC/Timeout (could be anything)";
368 break;
369 case -ETIME:
370 errmsg = "Device does not respond";
371 break;
372 }
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300373 if (packet < 0) {
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300374 dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n",
375 status, errmsg);
376 } else {
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300377 dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n",
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300378 packet, status, errmsg);
379 }
380}
381
382
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300383/*
384 * Controls the isoc copy of each urb packet
385 */
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300386static inline int tm6000_isoc_copy(struct urb *urb)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300387{
388 struct tm6000_dmaqueue *dma_q = urb->context;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300389 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
390 int i, len = 0, rc = 1, status;
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300391 char *p;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300392
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300393 if (urb->status < 0) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300394 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300395 return 0;
396 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300397
398 for (i = 0; i < urb->number_of_packets; i++) {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300399 status = urb->iso_frame_desc[i].status;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300400
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300401 if (status < 0) {
402 print_err_status(dev, i, status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300403 continue;
Mauro Carvalho Chehab5f796752007-08-27 07:55:05 -0300404 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300405
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300406 len = urb->iso_frame_desc[i].actual_length;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300407
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300408 if (len > 0) {
409 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300410 if (!urb->iso_frame_desc[i].status) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300411 if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) {
412 rc = copy_multiplexed(p, len, urb);
413 if (rc <= 0)
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300414 return rc;
415 } else {
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300416 copy_streams(p, len, urb);
Mauro Carvalho Chehabed0236a2008-04-09 08:07:20 -0300417 }
418 }
Stefan Ringel4b6ed9f2010-05-19 13:58:26 -0300419 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300420 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300421 return rc;
422}
423
424/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300425 * URB control
426 * ------------------------------------------------------------------
427 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300428
429/*
430 * IRQ callback, called by URB callback
431 */
432static void tm6000_irq_callback(struct urb *urb)
433{
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300434 struct tm6000_dmaqueue *dma_q = urb->context;
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300435 struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq);
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300436 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300437
Thierry Reding14f09152011-08-04 04:14:06 -0300438 switch (urb->status) {
439 case 0:
440 case -ETIMEDOUT:
441 break;
442
443 case -ECONNRESET:
444 case -ENOENT:
445 case -ESHUTDOWN:
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300446 return;
447
Thierry Reding14f09152011-08-04 04:14:06 -0300448 default:
449 tm6000_err("urb completion error %d.\n", urb->status);
450 break;
451 }
452
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300453 spin_lock(&dev->slock);
454 tm6000_isoc_copy(urb);
455 spin_unlock(&dev->slock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300456
Mauro Carvalho Chehabe8a48452008-11-28 07:39:00 -0300457 /* Reset urb buffers */
458 for (i = 0; i < urb->number_of_packets; i++) {
459 urb->iso_frame_desc[i].status = 0;
460 urb->iso_frame_desc[i].actual_length = 0;
461 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300462
Mauro Carvalho Chehabc4acf482008-01-09 22:44:51 -0300463 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
464 if (urb->status)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300465 tm6000_err("urb resubmit failed (error=%i)\n",
466 urb->status);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300467}
468
469/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300470 * Allocate URB buffers
471 */
472static int tm6000_alloc_urb_buffers(struct tm6000_core *dev)
473{
474 int num_bufs = TM6000_NUM_URB_BUF;
475 int i;
476
477 if (dev->urb_buffer != NULL)
478 return 0;
479
480 dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
481 if (!dev->urb_buffer) {
482 tm6000_err("cannot allocate memory for urb buffers\n");
483 return -ENOMEM;
484 }
485
486 dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL);
487 if (!dev->urb_dma) {
488 tm6000_err("cannot allocate memory for urb dma pointers\n");
489 return -ENOMEM;
490 }
491
492 for (i = 0; i < num_bufs; i++) {
493 dev->urb_buffer[i] = usb_alloc_coherent(
494 dev->udev, dev->urb_size,
495 GFP_KERNEL, &dev->urb_dma[i]);
496 if (!dev->urb_buffer[i]) {
497 tm6000_err("unable to allocate %i bytes for transfer buffer %i\n",
498 dev->urb_size, i);
499 return -ENOMEM;
500 }
501 memset(dev->urb_buffer[i], 0, dev->urb_size);
502 }
503
504 return 0;
505}
506
507/*
508 * Free URB buffers
509 */
510static int tm6000_free_urb_buffers(struct tm6000_core *dev)
511{
512 int i;
513
514 if (dev->urb_buffer == NULL)
515 return 0;
516
517 for (i = 0; i < TM6000_NUM_URB_BUF; i++) {
518 if (dev->urb_buffer[i]) {
519 usb_free_coherent(dev->udev,
520 dev->urb_size,
521 dev->urb_buffer[i],
522 dev->urb_dma[i]);
523 dev->urb_buffer[i] = NULL;
524 }
525 }
526 kfree(dev->urb_buffer);
527 kfree(dev->urb_dma);
528 dev->urb_buffer = NULL;
529 dev->urb_dma = NULL;
530
531 return 0;
532}
533
534/*
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300535 * Stop and Deallocate URBs
536 */
537static void tm6000_uninit_isoc(struct tm6000_core *dev)
538{
539 struct urb *urb;
540 int i;
541
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300542 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300543 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300544 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300545 if (urb) {
546 usb_kill_urb(urb);
547 usb_unlink_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300548 usb_free_urb(urb);
549 dev->isoc_ctl.urb[i] = NULL;
550 }
551 dev->isoc_ctl.transfer_buffer[i] = NULL;
552 }
553
Julian Scheel16427fa2012-10-04 10:04:28 -0300554 if (!keep_urb)
555 tm6000_free_urb_buffers(dev);
556
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300557 kfree(dev->isoc_ctl.urb);
558 kfree(dev->isoc_ctl.transfer_buffer);
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300559
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300560 dev->isoc_ctl.urb = NULL;
561 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300562 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300563}
564
565/*
Julian Scheel16427fa2012-10-04 10:04:28 -0300566 * Assign URBs and start IRQ
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300567 */
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300568static int tm6000_prepare_isoc(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300569{
570 struct tm6000_dmaqueue *dma_q = &dev->vidq;
Julian Scheel16427fa2012-10-04 10:04:28 -0300571 int i, j, sb_size, pipe, size, max_packets;
572 int num_bufs = TM6000_NUM_URB_BUF;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300573 struct urb *urb;
Mauro Carvalho Chehab204193d2008-01-09 18:12:39 -0300574
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300575 /* De-allocates all pending stuff */
576 tm6000_uninit_isoc(dev);
Dmitri Belimov641d2112010-12-22 05:57:46 -0300577 /* Stop interrupt USB pipe */
578 tm6000_ir_int_stop(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300579
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300580 usb_set_interface(dev->udev,
581 dev->isoc_in.bInterfaceNumber,
582 dev->isoc_in.bAlternateSetting);
583
Dmitri Belimov641d2112010-12-22 05:57:46 -0300584 /* Start interrupt USB pipe */
585 tm6000_ir_int_start(dev);
586
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300587 pipe = usb_rcvisocpipe(dev->udev,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300588 dev->isoc_in.endp->desc.bEndpointAddress &
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300589 USB_ENDPOINT_NUMBER_MASK);
590
591 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
592
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300593 if (size > dev->isoc_in.maxsize)
594 size = dev->isoc_in.maxsize;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300595
596 dev->isoc_ctl.max_pkt_size = size;
597
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -0300598 max_packets = TM6000_MAX_ISO_PACKETS;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300599 sb_size = max_packets * size;
Julian Scheel16427fa2012-10-04 10:04:28 -0300600 dev->urb_size = sb_size;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300601
602 dev->isoc_ctl.num_bufs = num_bufs;
603
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300604 dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300605 if (!dev->isoc_ctl.urb) {
606 tm6000_err("cannot alloc memory for usb buffers\n");
607 return -ENOMEM;
608 }
609
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300610 dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300611 GFP_KERNEL);
Julia Lawallf8960ee2010-02-11 03:30:30 -0300612 if (!dev->isoc_ctl.transfer_buffer) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300613 tm6000_err("cannot allocate memory for usbtransfer\n");
614 kfree(dev->isoc_ctl.urb);
615 return -ENOMEM;
616 }
617
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300618 dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets"
619 " (%d bytes) of %d bytes each to handle %u size\n",
620 max_packets, num_bufs, sb_size,
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300621 dev->isoc_in.maxsize, size);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300622
Julian Scheel16427fa2012-10-04 10:04:28 -0300623
Mauro Carvalho Chehab485bdbb2015-06-05 09:33:44 -0300624 if (tm6000_alloc_urb_buffers(dev) < 0) {
Julian Scheel16427fa2012-10-04 10:04:28 -0300625 tm6000_err("cannot allocate memory for urb buffers\n");
626
627 /* call free, as some buffers might have been allocated */
628 tm6000_free_urb_buffers(dev);
629 kfree(dev->isoc_ctl.urb);
630 kfree(dev->isoc_ctl.transfer_buffer);
631 return -ENOMEM;
632 }
633
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300634 /* allocate urbs and transfer buffers */
635 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
636 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
637 if (!urb) {
638 tm6000_err("cannot alloc isoc_ctl.urb %i\n", i);
639 tm6000_uninit_isoc(dev);
Mauro Carvalho Chehabc1a16412007-10-15 15:43:50 -0300640 usb_free_urb(urb);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300641 return -ENOMEM;
642 }
643 dev->isoc_ctl.urb[i] = urb;
644
Julian Scheel16427fa2012-10-04 10:04:28 -0300645 urb->transfer_dma = dev->urb_dma[i];
646 dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i];
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300647
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300648 usb_fill_bulk_urb(urb, dev->udev, pipe,
649 dev->isoc_ctl.transfer_buffer[i], sb_size,
650 tm6000_irq_callback, dma_q);
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300651 urb->interval = dev->isoc_in.endp->desc.bInterval;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300652 urb->number_of_packets = max_packets;
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300653 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300654
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300655 for (j = 0; j < max_packets; j++) {
Mauro Carvalho Chehabee1fc072008-01-10 17:27:26 -0300656 urb->iso_frame_desc[j].offset = size * j;
657 urb->iso_frame_desc[j].length = size;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300658 }
659 }
660
661 return 0;
662}
663
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300664static int tm6000_start_thread(struct tm6000_core *dev)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300665{
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300666 struct tm6000_dmaqueue *dma_q = &dev->vidq;
667 int i;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300668
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300669 dma_q->frame = 0;
670 dma_q->ini_jiffies = jiffies;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300671
672 init_waitqueue_head(&dma_q->wq);
673
674 /* submit urbs and enables IRQ */
675 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -0300676 int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300677 if (rc) {
678 tm6000_err("submit of urb %i failed (error=%i)\n", i,
679 rc);
680 tm6000_uninit_isoc(dev);
681 return rc;
682 }
683 }
684
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300685 return 0;
686}
687
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300688/* ------------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300689 * Videobuf operations
690 * ------------------------------------------------------------------
691 */
Michel Ludwig95a83822007-07-24 08:06:45 -0300692
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300693static int
694buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
695{
696 struct tm6000_fh *fh = vq->priv_data;
697
698 *size = fh->fmt->depth * fh->width * fh->height >> 3;
699 if (0 == *count)
Michel Ludwig95a83822007-07-24 08:06:45 -0300700 *count = TM6000_DEF_BUF;
701
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300702 if (*count < TM6000_MIN_BUF)
703 *count = TM6000_MIN_BUF;
Michel Ludwig95a83822007-07-24 08:06:45 -0300704
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300705 while (*size * *count > vid_limit * 1024 * 1024)
706 (*count)--;
Michel Ludwig95a83822007-07-24 08:06:45 -0300707
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300708 return 0;
709}
710
711static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf)
712{
Mauro Carvalho Chehab721f5072008-10-26 09:18:53 -0300713 struct tm6000_fh *fh = vq->priv_data;
714 struct tm6000_core *dev = fh->dev;
715 unsigned long flags;
716
Mauro Carvalho Chehab09f20822015-05-19 08:00:56 -0300717 BUG_ON(in_interrupt());
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300718
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;
Hans Verkuile6185782012-09-11 11:51:02 -0300915 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300916 f->fmt.pix.bytesperline =
917 (f->fmt.pix.width * fh->fmt->depth) >> 3;
918 f->fmt.pix.sizeimage =
919 f->fmt.pix.height * f->fmt.pix.bytesperline;
920
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300921 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300922}
923
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300924static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300925{
926 unsigned int i;
927
928 for (i = 0; i < ARRAY_SIZE(format); i++)
929 if (format[i].fourcc == fourcc)
930 return format+i;
931 return NULL;
932}
933
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300934static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300935 struct v4l2_format *f)
936{
937 struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev;
938 struct tm6000_fmt *fmt;
939 enum v4l2_field field;
940
941 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
942 if (NULL == fmt) {
Hans Verkuil21a7e052014-12-01 10:10:43 -0300943 dprintk(dev, 2, "Fourcc format (0x%08x)"
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300944 " invalid.\n", f->fmt.pix.pixelformat);
945 return -EINVAL;
946 }
947
948 field = f->fmt.pix.field;
949
Hans Verkuiled572562013-02-01 09:08:46 -0300950 field = V4L2_FIELD_INTERLACED;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300951
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300952 tm6000_get_std_res(dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300953
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -0300954 f->fmt.pix.width = dev->width;
955 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300956
957 f->fmt.pix.width &= ~0x01;
958
959 f->fmt.pix.field = field;
960
961 f->fmt.pix.bytesperline =
962 (f->fmt.pix.width * fmt->depth) >> 3;
963 f->fmt.pix.sizeimage =
964 f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuile6185782012-09-11 11:51:02 -0300965 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300966
967 return 0;
968}
969
970/*FIXME: This seems to be generic enough to be at videodev2 */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300971static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300972 struct v4l2_format *f)
973{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300974 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300975 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300976 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300977 if (ret < 0)
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300978 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300979
980 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
981 fh->width = f->fmt.pix.width;
982 fh->height = f->fmt.pix.height;
983 fh->vb_vidq.field = f->fmt.pix.field;
984 fh->type = f->type;
985
986 dev->fourcc = f->fmt.pix.pixelformat;
987
988 tm6000_set_fourcc_format(dev);
989
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300990 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300991}
992
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300993static int vidioc_reqbufs(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300994 struct v4l2_requestbuffers *p)
995{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300996 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300997
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -0300998 return videobuf_reqbufs(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -0300999}
1000
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001001static int vidioc_querybuf(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001002 struct v4l2_buffer *p)
1003{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001004 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001005
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001006 return videobuf_querybuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001007}
1008
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001009static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001010{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001011 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001012
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001013 return videobuf_qbuf(&fh->vb_vidq, p);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001014}
1015
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001016static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001017{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001018 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001019
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001020 return videobuf_dqbuf(&fh->vb_vidq, p,
1021 file->f_flags & O_NONBLOCK);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001022}
1023
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001024static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1025{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001026 struct tm6000_fh *fh = priv;
1027 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001028
1029 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1030 return -EINVAL;
1031 if (i != fh->type)
1032 return -EINVAL;
1033
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001034 if (!res_get(dev, fh, false))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001035 return -EBUSY;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001036 return videobuf_streamon(&fh->vb_vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001037}
1038
1039static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1040{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001041 struct tm6000_fh *fh = priv;
1042 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001043
1044 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1045 return -EINVAL;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001046
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001047 if (i != fh->type)
1048 return -EINVAL;
1049
1050 videobuf_streamoff(&fh->vb_vidq);
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001051 res_free(dev, fh);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001052
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001053 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001054}
1055
Hans Verkuil314527a2013-03-15 06:10:40 -03001056static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001057{
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001058 int rc = 0;
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001059 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001060 struct tm6000_core *dev = fh->dev;
1061
Hans Verkuil314527a2013-03-15 06:10:40 -03001062 dev->norm = norm;
Mauro Carvalho Chehab709944e2010-10-07 02:28:24 -03001063 rc = tm6000_init_analog_mode(dev);
Mauro Carvalho Chehab71e7cfa2007-09-06 20:12:10 -03001064
1065 fh->width = dev->width;
1066 fh->height = dev->height;
1067
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001068 if (rc < 0)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001069 return rc;
1070
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001071 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001072
1073 return 0;
1074}
1075
Hans Verkuil804be2d2013-06-03 05:36:44 -03001076static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1077{
1078 struct tm6000_fh *fh = priv;
1079 struct tm6000_core *dev = fh->dev;
1080
1081 *norm = dev->norm;
1082 return 0;
1083}
1084
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001085static const char *iname[] = {
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001086 [TM6000_INPUT_TV] = "Television",
1087 [TM6000_INPUT_COMPOSITE1] = "Composite 1",
1088 [TM6000_INPUT_COMPOSITE2] = "Composite 2",
1089 [TM6000_INPUT_SVIDEO] = "S-Video",
1090};
1091
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001092static int vidioc_enum_input(struct file *file, void *priv,
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001093 struct v4l2_input *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001094{
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001095 struct tm6000_fh *fh = priv;
1096 struct tm6000_core *dev = fh->dev;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001097 unsigned int n;
Dmitri Belimov2aefbc12011-03-17 21:08:55 -03001098
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001099 n = i->index;
1100 if (n >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001101 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001102
1103 if (!dev->vinput[n].type)
1104 return -EINVAL;
1105
1106 i->index = n;
1107
1108 if (dev->vinput[n].type == TM6000_INPUT_TV)
1109 i->type = V4L2_INPUT_TYPE_TUNER;
1110 else
1111 i->type = V4L2_INPUT_TYPE_CAMERA;
1112
1113 strcpy(i->name, iname[dev->vinput[n].type]);
1114
1115 i->std = TM6000_STD;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001116
1117 return 0;
1118}
1119
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001120static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001121{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001122 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001123 struct tm6000_core *dev = fh->dev;
1124
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001125 *i = dev->input;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001126
1127 return 0;
1128}
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001129
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001130static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001131{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001132 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001133 struct tm6000_core *dev = fh->dev;
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001134 int rc = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001135
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001136 if (i >= 3)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001137 return -EINVAL;
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001138 if (!dev->vinput[i].type)
1139 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001140
Stefan Ringelb8f7bd82011-05-09 16:53:52 -03001141 dev->input = i;
1142
Hans Verkuil804be2d2013-06-03 05:36:44 -03001143 rc = vidioc_s_std(file, priv, dev->norm);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001144
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001145 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001146}
1147
Stefan Ringel886a3c02011-05-09 16:53:50 -03001148/* --- controls ---------------------------------------------- */
Hans Verkuil9f747352013-01-31 08:23:01 -03001149
1150static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001151{
Hans Verkuil9f747352013-01-31 08:23:01 -03001152 struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler);
1153 u8 val = ctrl->val;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001154
1155 switch (ctrl->id) {
1156 case V4L2_CID_CONTRAST:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001157 tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001158 return 0;
1159 case V4L2_CID_BRIGHTNESS:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001160 tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001161 return 0;
1162 case V4L2_CID_SATURATION:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001163 tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001164 return 0;
1165 case V4L2_CID_HUE:
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001166 tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001167 return 0;
Hans Verkuil9f747352013-01-31 08:23:01 -03001168 }
1169 return -EINVAL;
1170}
1171
1172static const struct v4l2_ctrl_ops tm6000_ctrl_ops = {
1173 .s_ctrl = tm6000_s_ctrl,
1174};
1175
1176static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl)
1177{
1178 struct tm6000_core *dev = container_of(ctrl->handler,
1179 struct tm6000_core, radio_ctrl_handler);
1180 u8 val = ctrl->val;
1181
1182 switch (ctrl->id) {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001183 case V4L2_CID_AUDIO_MUTE:
1184 dev->ctl_mute = val;
1185 tm6000_tvaudio_set_mute(dev, val);
1186 return 0;
1187 case V4L2_CID_AUDIO_VOLUME:
1188 dev->ctl_volume = val;
1189 tm6000_set_volume(dev, val);
1190 return 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001191 }
1192 return -EINVAL;
1193}
1194
Hans Verkuil9f747352013-01-31 08:23:01 -03001195static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = {
1196 .s_ctrl = tm6000_radio_s_ctrl,
1197};
1198
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001199static int vidioc_g_tuner(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001200 struct v4l2_tuner *t)
1201{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001202 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001203 struct tm6000_core *dev = fh->dev;
1204
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001205 if (UNSET == dev->tuner_type)
1206 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001207 if (0 != t->index)
1208 return -EINVAL;
1209
1210 strcpy(t->name, "Television");
1211 t->type = V4L2_TUNER_ANALOG_TV;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001212 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001213 t->rangehigh = 0xffffffffUL;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001214 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
1215
1216 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1217
1218 t->audmode = dev->amode;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001219
1220 return 0;
1221}
1222
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001223static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001224 const struct v4l2_tuner *t)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001225{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001226 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001227 struct tm6000_core *dev = fh->dev;
1228
1229 if (UNSET == dev->tuner_type)
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001230 return -ENOTTY;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001231 if (0 != t->index)
1232 return -EINVAL;
1233
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001234 if (t->audmode > V4L2_TUNER_MODE_STEREO)
1235 dev->amode = V4L2_TUNER_MODE_STEREO;
1236 else
1237 dev->amode = t->audmode;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001238 dprintk(dev, 3, "audio mode: %x\n", t->audmode);
1239
1240 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001241
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001242 return 0;
1243}
1244
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001245static int vidioc_g_frequency(struct file *file, void *priv,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001246 struct v4l2_frequency *f)
1247{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001248 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001249 struct tm6000_core *dev = fh->dev;
1250
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001251 if (UNSET == dev->tuner_type)
1252 return -ENOTTY;
1253 if (f->tuner)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001254 return -EINVAL;
1255
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001256 f->frequency = dev->freq;
1257
Mauro Carvalho Chehab427f7fa2009-09-14 16:37:13 -03001258 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001259
1260 return 0;
1261}
1262
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001263static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -03001264 const struct v4l2_frequency *f)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001265{
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001266 struct tm6000_fh *fh = priv;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001267 struct tm6000_core *dev = fh->dev;
1268
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001269 if (UNSET == dev->tuner_type)
1270 return -ENOTTY;
1271 if (f->tuner != 0)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001272 return -EINVAL;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001273
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001274 dev->freq = f->frequency;
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001275 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001276
1277 return 0;
1278}
1279
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001280static int radio_g_tuner(struct file *file, void *priv,
1281 struct v4l2_tuner *t)
1282{
1283 struct tm6000_fh *fh = file->private_data;
1284 struct tm6000_core *dev = fh->dev;
1285
1286 if (0 != t->index)
1287 return -EINVAL;
1288
1289 memset(t, 0, sizeof(*t));
1290 strcpy(t->name, "Radio");
1291 t->type = V4L2_TUNER_RADIO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001292 t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Stefan Ringel886a3c02011-05-09 16:53:50 -03001293 t->rxsubchans = V4L2_TUNER_SUB_STEREO;
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001294 t->audmode = V4L2_TUNER_MODE_STEREO;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001295
1296 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1297
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001298 return 0;
1299}
1300
1301static int radio_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -03001302 const struct v4l2_tuner *t)
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001303{
1304 struct tm6000_fh *fh = file->private_data;
1305 struct tm6000_core *dev = fh->dev;
1306
1307 if (0 != t->index)
1308 return -EINVAL;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001309 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001310 return 0;
1311}
1312
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001313/* ------------------------------------------------------------------
1314 File operations for the device
1315 ------------------------------------------------------------------*/
1316
Hans Verkuil14a09da2012-06-24 07:26:46 -03001317static int __tm6000_open(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001318{
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001319 struct video_device *vdev = video_devdata(file);
1320 struct tm6000_core *dev = video_drvdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001321 struct tm6000_fh *fh;
Mauro Carvalho Chehabe8d04162010-05-18 04:27:27 -03001322 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil9f747352013-01-31 08:23:01 -03001323 int rc;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001324 int radio = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001325
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001326 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n",
1327 video_device_node_name(vdev));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001328
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001329 switch (vdev->vfl_type) {
1330 case VFL_TYPE_GRABBER:
1331 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1332 break;
1333 case VFL_TYPE_VBI:
1334 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1335 break;
1336 case VFL_TYPE_RADIO:
1337 radio = 1;
1338 break;
1339 }
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001340
1341 /* If more than one user, mutex should be added */
1342 dev->users++;
1343
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001344 dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n",
1345 video_device_node_name(vdev), v4l2_type_names[type],
1346 dev->users);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001347
1348 /* allocate + initialize per filehandle data */
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001349 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001350 if (NULL == fh) {
1351 dev->users--;
1352 return -ENOMEM;
1353 }
1354
Hans Verkuil770056c2012-09-11 11:50:37 -03001355 v4l2_fh_init(&fh->fh, vdev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001356 file->private_data = fh;
1357 fh->dev = dev;
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001358 fh->radio = radio;
1359 dev->radio = radio;
1360 fh->type = type;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001361 dev->fourcc = format[0].fourcc;
1362
1363 fh->fmt = format_by_fourcc(dev->fourcc);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001364
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001365 tm6000_get_std_res(dev);
Mauro Carvalho Chehab4475c042007-09-03 21:51:45 -03001366
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001367 fh->width = dev->width;
1368 fh->height = dev->height;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001369
1370 dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, "
1371 "dev->vidq=0x%08lx\n",
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001372 (unsigned long)fh, (unsigned long)dev,
1373 (unsigned long)&dev->vidq);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001374 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001375 "queued=%d\n", list_empty(&dev->vidq.queued));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001376 dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty "
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001377 "active=%d\n", list_empty(&dev->vidq.active));
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001378
1379 /* initialize hardware on analog mode */
Mauro Carvalho Chehab589851d2010-10-12 12:11:55 -03001380 rc = tm6000_init_analog_mode(dev);
1381 if (rc < 0)
1382 return rc;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001383
Hans Verkuil9f747352013-01-31 08:23:01 -03001384 dev->mode = TM6000_MODE_ANALOG;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001385
Thierry Redingaa4a5832011-08-04 04:14:15 -03001386 if (!fh->radio) {
1387 videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops,
1388 NULL, &dev->slock,
1389 fh->type,
1390 V4L2_FIELD_INTERLACED,
1391 sizeof(struct tm6000_buffer), fh, &dev->lock);
1392 } else {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001393 dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n");
Stefan Ringel0f6040e2011-05-09 16:53:53 -03001394 tm6000_set_audio_rinput(dev);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001395 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
1396 tm6000_prepare_isoc(dev);
1397 tm6000_start_thread(dev);
1398 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001399 v4l2_fh_add(&fh->fh);
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001400
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001401 return 0;
1402}
1403
Hans Verkuil14a09da2012-06-24 07:26:46 -03001404static int tm6000_open(struct file *file)
1405{
1406 struct video_device *vdev = video_devdata(file);
1407 int res;
1408
1409 mutex_lock(vdev->lock);
1410 res = __tm6000_open(file);
1411 mutex_unlock(vdev->lock);
1412 return res;
1413}
1414
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001415static ssize_t
1416tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos)
1417{
Hans Verkuil14a09da2012-06-24 07:26:46 -03001418 struct tm6000_fh *fh = file->private_data;
1419 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001420
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001421 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil14a09da2012-06-24 07:26:46 -03001422 int res;
1423
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001424 if (!res_get(fh->dev, fh, true))
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001425 return -EBUSY;
1426
Hans Verkuil14a09da2012-06-24 07:26:46 -03001427 if (mutex_lock_interruptible(&dev->lock))
1428 return -ERESTARTSYS;
1429 res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001430 file->f_flags & O_NONBLOCK);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001431 mutex_unlock(&dev->lock);
1432 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001433 }
1434 return 0;
1435}
1436
1437static unsigned int
Hans Verkuil14a09da2012-06-24 07:26:46 -03001438__tm6000_poll(struct file *file, struct poll_table_struct *wait)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001439{
Hans Verkuil770056c2012-09-11 11:50:37 -03001440 unsigned long req_events = poll_requested_events(wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001441 struct tm6000_fh *fh = file->private_data;
1442 struct tm6000_buffer *buf;
Hans Verkuil770056c2012-09-11 11:50:37 -03001443 int res = 0;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001444
Hans Verkuil770056c2012-09-11 11:50:37 -03001445 if (v4l2_event_pending(&fh->fh))
1446 res = POLLPRI;
1447 else if (req_events & POLLPRI)
1448 poll_wait(file, &fh->fh.wait, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001449 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
Hans Verkuil770056c2012-09-11 11:50:37 -03001450 return res | POLLERR;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001451
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001452 if (!!is_res_streaming(fh->dev, fh))
Hans Verkuil770056c2012-09-11 11:50:37 -03001453 return res | POLLERR;
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001454
1455 if (!is_res_read(fh->dev, fh)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001456 /* streaming capture */
1457 if (list_empty(&fh->vb_vidq.stream))
Hans Verkuil770056c2012-09-11 11:50:37 -03001458 return res | POLLERR;
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001459 buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream);
Hans Verkuil82f0efb2013-02-07 07:05:43 -03001460 poll_wait(file, &buf->vb.done, wait);
1461 if (buf->vb.state == VIDEOBUF_DONE ||
1462 buf->vb.state == VIDEOBUF_ERROR)
1463 return res | POLLIN | POLLRDNORM;
Hans Verkuil770056c2012-09-11 11:50:37 -03001464 } else if (req_events & (POLLIN | POLLRDNORM)) {
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001465 /* read() capture */
Hans Verkuil770056c2012-09-11 11:50:37 -03001466 return res | videobuf_poll_stream(file, &fh->vb_vidq, wait);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001467 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001468 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001469}
1470
Hans Verkuil14a09da2012-06-24 07:26:46 -03001471static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait)
1472{
1473 struct tm6000_fh *fh = file->private_data;
1474 struct tm6000_core *dev = fh->dev;
1475 unsigned int res;
1476
1477 mutex_lock(&dev->lock);
1478 res = __tm6000_poll(file, wait);
1479 mutex_unlock(&dev->lock);
1480 return res;
1481}
1482
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001483static int tm6000_release(struct file *file)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001484{
1485 struct tm6000_fh *fh = file->private_data;
1486 struct tm6000_core *dev = fh->dev;
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001487 struct video_device *vdev = video_devdata(file);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001488
Mauro Carvalho Chehab0a34df52010-05-18 00:43:18 -03001489 dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n",
1490 video_device_node_name(vdev), dev->users);
Michel Ludwig7c3f53e2007-06-16 23:21:48 -03001491
Hans Verkuil14a09da2012-06-24 07:26:46 -03001492 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001493 dev->users--;
1494
Mauro Carvalho Chehab9efd85d2010-10-11 10:48:11 -03001495 res_free(dev, fh);
Thierry Redingdd0c8ab2011-08-04 04:14:13 -03001496
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001497 if (!dev->users) {
Mauro Carvalho Chehabc144c032008-04-09 01:49:19 -03001498 tm6000_uninit_isoc(dev);
Thierry Redingaa4a5832011-08-04 04:14:15 -03001499
Stefan Ringel8159c182011-11-28 15:46:18 -03001500 /* Stop interrupt USB pipe */
1501 tm6000_ir_int_stop(dev);
1502
1503 usb_reset_configuration(dev->udev);
1504
Thierry Reding4be9c8f2011-12-06 09:39:35 -03001505 if (dev->int_in.endp)
Stefan Ringel8159c182011-11-28 15:46:18 -03001506 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001507 dev->isoc_in.bInterfaceNumber, 2);
Stefan Ringel8159c182011-11-28 15:46:18 -03001508 else
1509 usb_set_interface(dev->udev,
Thierry Reding875f0e32011-12-06 09:39:36 -03001510 dev->isoc_in.bInterfaceNumber, 0);
Stefan Ringel8159c182011-11-28 15:46:18 -03001511
1512 /* Start interrupt USB pipe */
1513 tm6000_ir_int_start(dev);
1514
Thierry Redingaa4a5832011-08-04 04:14:15 -03001515 if (!fh->radio)
1516 videobuf_mmap_free(&fh->vb_vidq);
Mauro Carvalho Chehaba58d35c2007-06-17 17:14:12 -03001517 }
Hans Verkuil770056c2012-09-11 11:50:37 -03001518 v4l2_fh_del(&fh->fh);
1519 v4l2_fh_exit(&fh->fh);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001520 kfree(fh);
Hans Verkuil14a09da2012-06-24 07:26:46 -03001521 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001522
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001523 return 0;
1524}
1525
1526static int tm6000_mmap(struct file *file, struct vm_area_struct * vma)
1527{
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001528 struct tm6000_fh *fh = file->private_data;
Hans Verkuil14a09da2012-06-24 07:26:46 -03001529 struct tm6000_core *dev = fh->dev;
1530 int res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001531
Hans Verkuil14a09da2012-06-24 07:26:46 -03001532 if (mutex_lock_interruptible(&dev->lock))
1533 return -ERESTARTSYS;
1534 res = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1535 mutex_unlock(&dev->lock);
1536 return res;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001537}
1538
Mauro Carvalho Chehab64d339d2009-09-14 17:16:32 -03001539static struct v4l2_file_operations tm6000_fops = {
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001540 .owner = THIS_MODULE,
1541 .open = tm6000_open,
1542 .release = tm6000_release,
1543 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1544 .read = tm6000_read,
1545 .poll = tm6000_poll,
1546 .mmap = tm6000_mmap,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001547};
1548
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001549static const struct v4l2_ioctl_ops video_ioctl_ops = {
1550 .vidioc_querycap = vidioc_querycap,
1551 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1552 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1553 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1554 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1555 .vidioc_s_std = vidioc_s_std,
Hans Verkuil804be2d2013-06-03 05:36:44 -03001556 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001557 .vidioc_enum_input = vidioc_enum_input,
1558 .vidioc_g_input = vidioc_g_input,
1559 .vidioc_s_input = vidioc_s_input,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001560 .vidioc_g_tuner = vidioc_g_tuner,
1561 .vidioc_s_tuner = vidioc_s_tuner,
1562 .vidioc_g_frequency = vidioc_g_frequency,
1563 .vidioc_s_frequency = vidioc_s_frequency,
1564 .vidioc_streamon = vidioc_streamon,
1565 .vidioc_streamoff = vidioc_streamoff,
1566 .vidioc_reqbufs = vidioc_reqbufs,
1567 .vidioc_querybuf = vidioc_querybuf,
1568 .vidioc_qbuf = vidioc_qbuf,
1569 .vidioc_dqbuf = vidioc_dqbuf,
Hans Verkuil770056c2012-09-11 11:50:37 -03001570 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1571 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001572};
1573
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001574static struct video_device tm6000_template = {
1575 .name = "tm6000",
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001576 .fops = &tm6000_fops,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001577 .ioctl_ops = &video_ioctl_ops,
Hans Verkuil65b88c02015-03-09 13:34:10 -03001578 .release = video_device_release_empty,
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001579 .tvnorms = TM6000_STD,
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001580};
Mauro Carvalho Chehab2a8145d2008-10-25 10:43:04 -03001581
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001582static const struct v4l2_file_operations radio_fops = {
Stefan Ringel1f385712011-05-09 16:54:01 -03001583 .owner = THIS_MODULE,
1584 .open = tm6000_open,
Hans Verkuil52dec542012-09-11 11:53:51 -03001585 .poll = v4l2_ctrl_poll,
Stefan Ringel1f385712011-05-09 16:54:01 -03001586 .release = tm6000_release,
1587 .unlocked_ioctl = video_ioctl2,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001588};
1589
1590static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Hans Verkuil2c2a0532012-09-11 07:35:30 -03001591 .vidioc_querycap = vidioc_querycap,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001592 .vidioc_g_tuner = radio_g_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001593 .vidioc_s_tuner = radio_s_tuner,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001594 .vidioc_g_frequency = vidioc_g_frequency,
1595 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil770056c2012-09-11 11:50:37 -03001596 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1597 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001598};
1599
Thierry Reding3d1a51d2011-08-04 04:14:01 -03001600static struct video_device tm6000_radio_template = {
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001601 .name = "tm6000",
1602 .fops = &radio_fops,
Curtis McEnroeed7c2212011-06-01 22:21:56 -04001603 .ioctl_ops = &radio_ioctl_ops,
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001604};
1605
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001606/* -----------------------------------------------------------------
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001607 * Initialization and module stuff
1608 * ------------------------------------------------------------------
1609 */
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001610
Hans Verkuil65b88c02015-03-09 13:34:10 -03001611static void vdev_init(struct tm6000_core *dev,
1612 struct video_device *vfd,
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001613 const struct video_device
1614 *template, const char *type_name)
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001615{
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001616 *vfd = *template;
1617 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil65b88c02015-03-09 13:34:10 -03001618 vfd->release = video_device_release_empty;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001619 vfd->lock = &dev->lock;
1620
1621 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1622
1623 video_set_drvdata(vfd, dev);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001624}
1625
1626int tm6000_v4l2_register(struct tm6000_core *dev)
1627{
Hans Verkuil9f747352013-01-31 08:23:01 -03001628 int ret = 0;
1629
1630 v4l2_ctrl_handler_init(&dev->ctrl_handler, 6);
1631 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2);
1632 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1633 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1634 v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops,
1635 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
1636 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1637 V4L2_CID_BRIGHTNESS, 0, 255, 1, 54);
1638 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1639 V4L2_CID_CONTRAST, 0, 255, 1, 119);
1640 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1641 V4L2_CID_SATURATION, 0, 255, 1, 112);
1642 v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops,
1643 V4L2_CID_HUE, -128, 127, 1, 0);
1644 v4l2_ctrl_add_handler(&dev->ctrl_handler,
1645 &dev->radio_ctrl_handler, NULL);
1646
1647 if (dev->radio_ctrl_handler.error)
1648 ret = dev->radio_ctrl_handler.error;
1649 if (!ret && dev->ctrl_handler.error)
1650 ret = dev->ctrl_handler.error;
1651 if (ret)
1652 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001653
Hans Verkuil65b88c02015-03-09 13:34:10 -03001654 vdev_init(dev, &dev->vfd, &tm6000_template, "video");
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001655
Hans Verkuil65b88c02015-03-09 13:34:10 -03001656 dev->vfd.ctrl_handler = &dev->ctrl_handler;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001657
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001658 /* init video dma queues */
1659 INIT_LIST_HEAD(&dev->vidq.active);
1660 INIT_LIST_HEAD(&dev->vidq.queued);
1661
Hans Verkuil65b88c02015-03-09 13:34:10 -03001662 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, video_nr);
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001663
1664 if (ret < 0) {
1665 printk(KERN_INFO "%s: can't register video device\n",
1666 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001667 goto free_ctrl;
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001668 }
1669
1670 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001671 dev->name, video_device_node_name(&dev->vfd));
Dmitri Belimov3c61be42011-01-13 00:46:07 -03001672
Stefan Ringel14169102011-05-09 16:53:49 -03001673 if (dev->caps.has_radio) {
Hans Verkuil65b88c02015-03-09 13:34:10 -03001674 vdev_init(dev, &dev->radio_dev, &tm6000_radio_template,
Stefan Ringel14169102011-05-09 16:53:49 -03001675 "radio");
Hans Verkuil65b88c02015-03-09 13:34:10 -03001676 dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1677 ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
Stefan Ringel14169102011-05-09 16:53:49 -03001678 radio_nr);
1679 if (ret < 0) {
1680 printk(KERN_INFO "%s: can't register radio device\n",
1681 dev->name);
Hans Verkuil9f747352013-01-31 08:23:01 -03001682 goto unreg_video;
Stefan Ringel14169102011-05-09 16:53:49 -03001683 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001684
Stefan Ringel14169102011-05-09 16:53:49 -03001685 printk(KERN_INFO "%s: registered device %s\n",
Hans Verkuil65b88c02015-03-09 13:34:10 -03001686 dev->name, video_device_node_name(&dev->radio_dev));
Stefan Ringel14169102011-05-09 16:53:49 -03001687 }
Dmitri Belimov8aff8ba2011-02-17 22:11:05 -03001688
Dmitri Belimove28f49b2010-02-22 06:32:15 -03001689 printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001690 return ret;
Hans Verkuil9f747352013-01-31 08:23:01 -03001691
1692unreg_video:
Hans Verkuil65b88c02015-03-09 13:34:10 -03001693 video_unregister_device(&dev->vfd);
Hans Verkuil9f747352013-01-31 08:23:01 -03001694free_ctrl:
1695 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1696 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1697 return ret;
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001698}
1699
1700int tm6000_v4l2_unregister(struct tm6000_core *dev)
1701{
Hans Verkuil65b88c02015-03-09 13:34:10 -03001702 video_unregister_device(&dev->vfd);
Michel Ludwig22927e82007-06-14 17:19:59 -03001703
Julian Scheel16427fa2012-10-04 10:04:28 -03001704 /* if URB buffers are still allocated free them now */
1705 tm6000_free_urb_buffers(dev);
1706
Hans Verkuil65b88c02015-03-09 13:34:10 -03001707 video_unregister_device(&dev->radio_dev);
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001708 return 0;
1709}
1710
1711int tm6000_v4l2_exit(void)
1712{
1713 return 0;
1714}
1715
1716module_param(video_nr, int, 0);
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001717MODULE_PARM_DESC(video_nr, "Allow changing video device number");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001718
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001719module_param_named(debug, tm6000_debug, int, 0444);
1720MODULE_PARM_DESC(debug, "activates debug info");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001721
Ruslan Pisarev60fbfdf2010-10-20 06:35:12 -03001722module_param(vid_limit, int, 0644);
1723MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
Mauro Carvalho Chehab9701dc92009-09-14 09:42:41 -03001724
Julian Scheel16427fa2012-10-04 10:04:28 -03001725module_param(keep_urb, bool, 0);
1726MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user");